@huggingface/transformers 4.0.0-next.7 → 4.0.0-next.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/dist/ort-wasm-simd-threaded.jsep.mjs +26 -26
- package/dist/transformers.js +1002 -587
- package/dist/transformers.min.js +23 -19
- package/dist/transformers.node.cjs +1030 -585
- package/dist/transformers.node.min.cjs +21 -17
- package/dist/transformers.node.min.mjs +21 -17
- package/dist/transformers.node.mjs +1000 -585
- package/dist/transformers.web.js +887 -472
- package/dist/transformers.web.min.js +21 -17
- package/package.json +3 -3
- package/src/configs.js +28 -22
- package/src/env.js +1 -1
- package/src/image_processors_utils.js +25 -15
- package/src/models/chmv2/image_processing_chmv2.js +3 -0
- package/src/models/chmv2/modeling_chmv2.js +4 -0
- package/src/models/deepseek_v3/modeling_deepseek_v3.js +5 -0
- package/src/models/eurobert/modeling_eurobert.js +41 -0
- package/src/models/gemma3/image_processing_gemma3.js +3 -0
- package/src/models/gemma3/modeling_gemma3.js +4 -1
- package/src/models/gemma3/processing_gemma3.js +45 -0
- package/src/models/glm46v/image_processing_glm46v.js +12 -0
- package/src/models/glm46v/processing_glm46v.js +5 -0
- package/src/models/glm_moe_dsa/modeling_glm_moe_dsa.js +5 -0
- package/src/models/glm_ocr/modeling_glm_ocr.js +78 -0
- package/src/models/image_processors.js +3 -0
- package/src/models/lfm2_vl/image_processing_lfm2_vl.js +1 -1
- package/src/models/lighton_ocr/modeling_lighton_ocr.js +3 -0
- package/src/models/mistral4/modeling_mistral4.js +5 -0
- package/src/models/modeling_utils.js +48 -25
- package/src/models/models.js +10 -1
- package/src/models/nemotron_h/modeling_nemotron_h.js +5 -0
- package/src/models/processors.js +2 -0
- package/src/models/qwen2_vl/modeling_qwen2_vl.js +226 -168
- package/src/models/qwen2_vl/processing_qwen2_vl.js +5 -4
- package/src/models/registry.js +19 -8
- package/src/models/solar_open/modeling_solar_open.js +5 -0
- package/src/pipelines.js +1 -0
- package/src/utils/hub.js +4 -1
- package/src/utils/model_registry/ModelRegistry.js +36 -0
- package/src/utils/model_registry/get_available_dtypes.js +68 -0
- package/src/utils/model_registry/get_file_metadata.js +1 -0
- package/src/utils/model_registry/get_model_files.js +7 -60
- package/src/utils/model_registry/resolve_model_type.js +66 -0
- package/types/configs.d.ts.map +1 -1
- package/types/image_processors_utils.d.ts +3 -2
- package/types/image_processors_utils.d.ts.map +1 -1
- package/types/models/{ast/modeling_ast.d.ts → audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.d.ts} +1 -1
- package/types/models/audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.d.ts.map +1 -0
- package/types/models/chmv2/image_processing_chmv2.d.ts +4 -0
- package/types/models/chmv2/image_processing_chmv2.d.ts.map +1 -0
- package/types/models/chmv2/modeling_chmv2.d.ts +6 -0
- package/types/models/chmv2/modeling_chmv2.d.ts.map +1 -0
- package/types/models/deepseek_v3/modeling_deepseek_v3.d.ts +8 -0
- package/types/models/deepseek_v3/modeling_deepseek_v3.d.ts.map +1 -0
- package/types/models/eurobert/modeling_eurobert.d.ts +36 -0
- package/types/models/eurobert/modeling_eurobert.d.ts.map +1 -0
- package/types/models/gemma3/image_processing_gemma3.d.ts +4 -0
- package/types/models/gemma3/image_processing_gemma3.d.ts.map +1 -0
- package/types/models/gemma3/modeling_gemma3.d.ts +4 -1
- package/types/models/gemma3/modeling_gemma3.d.ts.map +1 -1
- package/types/models/gemma3/processing_gemma3.d.ts +20 -0
- package/types/models/gemma3/processing_gemma3.d.ts.map +1 -0
- package/types/models/glm46v/image_processing_glm46v.d.ts +4 -0
- package/types/models/glm46v/image_processing_glm46v.d.ts.map +1 -0
- package/types/models/glm46v/processing_glm46v.d.ts +4 -0
- package/types/models/glm46v/processing_glm46v.d.ts.map +1 -0
- package/types/models/glm_moe_dsa/modeling_glm_moe_dsa.d.ts +8 -0
- package/types/models/glm_moe_dsa/modeling_glm_moe_dsa.d.ts.map +1 -0
- package/types/models/glm_ocr/modeling_glm_ocr.d.ts +26 -0
- package/types/models/glm_ocr/modeling_glm_ocr.d.ts.map +1 -0
- package/types/models/image_processors.d.ts +3 -0
- package/types/models/lighton_ocr/modeling_lighton_ocr.d.ts +4 -0
- package/types/models/lighton_ocr/modeling_lighton_ocr.d.ts.map +1 -0
- package/types/models/mistral4/modeling_mistral4.d.ts +8 -0
- package/types/models/mistral4/modeling_mistral4.d.ts.map +1 -0
- package/types/models/modeling_utils.d.ts +2 -3
- package/types/models/modeling_utils.d.ts.map +1 -1
- package/types/models/models.d.ts +10 -1
- package/types/models/nemotron_h/modeling_nemotron_h.d.ts +8 -0
- package/types/models/nemotron_h/modeling_nemotron_h.d.ts.map +1 -0
- package/types/models/processors.d.ts +2 -0
- package/types/models/qwen2_vl/modeling_qwen2_vl.d.ts +41 -6
- package/types/models/qwen2_vl/modeling_qwen2_vl.d.ts.map +1 -1
- package/types/models/qwen2_vl/processing_qwen2_vl.d.ts +1 -0
- package/types/models/qwen2_vl/processing_qwen2_vl.d.ts.map +1 -1
- package/types/models/registry.d.ts.map +1 -1
- package/types/models/solar_open/modeling_solar_open.d.ts +8 -0
- package/types/models/solar_open/modeling_solar_open.d.ts.map +1 -0
- package/types/pipelines.d.ts +1 -0
- package/types/pipelines.d.ts.map +1 -1
- package/types/utils/hub.d.ts.map +1 -1
- package/types/utils/model_registry/ModelRegistry.d.ts +27 -0
- package/types/utils/model_registry/ModelRegistry.d.ts.map +1 -1
- package/types/utils/model_registry/get_available_dtypes.d.ts +26 -0
- package/types/utils/model_registry/get_available_dtypes.d.ts.map +1 -0
- package/types/utils/model_registry/get_model_files.d.ts +25 -0
- package/types/utils/model_registry/get_model_files.d.ts.map +1 -1
- package/types/utils/model_registry/resolve_model_type.d.ts +24 -0
- package/types/utils/model_registry/resolve_model_type.d.ts.map +1 -0
- package/types/models/ast/modeling_ast.d.ts.map +0 -1
- /package/src/models/{ast/modeling_ast.js → audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.js} +0 -0
package/src/models/models.js
CHANGED
|
@@ -2,7 +2,7 @@ export * from './albert/modeling_albert.js';
|
|
|
2
2
|
export * from './apertus/modeling_apertus.js';
|
|
3
3
|
export * from './afmoe/modeling_afmoe.js';
|
|
4
4
|
export * from './arcee/modeling_arcee.js';
|
|
5
|
-
export * from './
|
|
5
|
+
export * from './audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.js';
|
|
6
6
|
export * from './bart/modeling_bart.js';
|
|
7
7
|
export * from './beit/modeling_beit.js';
|
|
8
8
|
export * from './bert/modeling_bert.js';
|
|
@@ -12,6 +12,7 @@ export * from './bloom/modeling_bloom.js';
|
|
|
12
12
|
export * from './camembert/modeling_camembert.js';
|
|
13
13
|
export * from './chatterbox/modeling_chatterbox.js';
|
|
14
14
|
export * from './chinese_clip/modeling_chinese_clip.js';
|
|
15
|
+
export * from './chmv2/modeling_chmv2.js';
|
|
15
16
|
export * from './clap/modeling_clap.js';
|
|
16
17
|
export * from './clip/modeling_clip.js';
|
|
17
18
|
export * from './clipseg/modeling_clipseg.js';
|
|
@@ -24,6 +25,7 @@ export * from './convnextv2/modeling_convnextv2.js';
|
|
|
24
25
|
export * from './d_fine/modeling_d_fine.js';
|
|
25
26
|
export * from './dac/modeling_dac.js';
|
|
26
27
|
export * from './deberta/modeling_deberta.js';
|
|
28
|
+
export * from './deepseek_v3/modeling_deepseek_v3.js';
|
|
27
29
|
export * from './deberta_v2/modeling_deberta_v2.js';
|
|
28
30
|
export * from './decision_transformer/modeling_decision_transformer.js';
|
|
29
31
|
export * from './deit/modeling_deit.js';
|
|
@@ -41,6 +43,7 @@ export * from './efficientnet/modeling_efficientnet.js';
|
|
|
41
43
|
export * from './electra/modeling_electra.js';
|
|
42
44
|
export * from './ernie4_5/modeling_ernie4_5.js';
|
|
43
45
|
export * from './esm/modeling_esm.js';
|
|
46
|
+
export * from './eurobert/modeling_eurobert.js';
|
|
44
47
|
export * from './exaone/modeling_exaone.js';
|
|
45
48
|
export * from './falcon/modeling_falcon.js';
|
|
46
49
|
export * from './falcon_h1/modeling_falcon_h1.js';
|
|
@@ -51,6 +54,8 @@ export * from './gemma2/modeling_gemma2.js';
|
|
|
51
54
|
export * from './gemma3/modeling_gemma3.js';
|
|
52
55
|
export * from './gemma3n/modeling_gemma3n.js';
|
|
53
56
|
export * from './glm/modeling_glm.js';
|
|
57
|
+
export * from './glm_moe_dsa/modeling_glm_moe_dsa.js';
|
|
58
|
+
export * from './glm_ocr/modeling_glm_ocr.js';
|
|
54
59
|
export * from './glpn/modeling_glpn.js';
|
|
55
60
|
export * from './gpt_bigcode/modeling_gpt_bigcode.js';
|
|
56
61
|
export * from './gpt_neo/modeling_gpt_neo.js';
|
|
@@ -72,6 +77,7 @@ export * from './ijepa/modeling_ijepa.js';
|
|
|
72
77
|
export * from './jais/modeling_jais.js';
|
|
73
78
|
export * from './jina_clip/modeling_jina_clip.js';
|
|
74
79
|
export * from './lfm2/modeling_lfm2.js';
|
|
80
|
+
export * from './lighton_ocr/modeling_lighton_ocr.js';
|
|
75
81
|
export * from './lfm2_moe/modeling_lfm2_moe.js';
|
|
76
82
|
export * from './lfm2_vl/modeling_lfm2_vl.js';
|
|
77
83
|
export * from './llama/modeling_llama.js';
|
|
@@ -88,6 +94,7 @@ export * from './metric3dv2/modeling_metric3dv2.js';
|
|
|
88
94
|
export * from './mgp_str/modeling_mgp_str.js';
|
|
89
95
|
export * from './mimi/modeling_mimi.js';
|
|
90
96
|
export * from './mistral/modeling_mistral.js';
|
|
97
|
+
export * from './mistral4/modeling_mistral4.js';
|
|
91
98
|
export * from './mobilebert/modeling_mobilebert.js';
|
|
92
99
|
export * from './mobilellm/modeling_mobilellm.js';
|
|
93
100
|
export * from './mobilenet_v1/modeling_mobilenet_v1.js';
|
|
@@ -105,6 +112,7 @@ export * from './mt5/modeling_mt5.js';
|
|
|
105
112
|
export * from './multi_modality/modeling_multi_modality.js';
|
|
106
113
|
export * from './musicgen/modeling_musicgen.js';
|
|
107
114
|
export * from './nanochat/modeling_nanochat.js';
|
|
115
|
+
export * from './nemotron_h/modeling_nemotron_h.js';
|
|
108
116
|
export * from './neobert/modeling_neobert.js';
|
|
109
117
|
export * from './nomic_bert/modeling_nomic_bert.js';
|
|
110
118
|
export * from './olmo/modeling_olmo.js';
|
|
@@ -148,6 +156,7 @@ export * from './segformer/modeling_segformer.js';
|
|
|
148
156
|
export * from './siglip/modeling_siglip.js';
|
|
149
157
|
export * from './smollm3/modeling_smollm3.js';
|
|
150
158
|
export * from './snac/modeling_snac.js';
|
|
159
|
+
export * from './solar_open/modeling_solar_open.js';
|
|
151
160
|
export * from './speecht5/modeling_speecht5.js';
|
|
152
161
|
export * from './squeezebert/modeling_squeezebert.js';
|
|
153
162
|
export * from './stablelm/modeling_stablelm.js';
|
package/src/models/processors.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export * from './chatterbox/processing_chatterbox.js';
|
|
2
2
|
export * from './florence2/processing_florence2.js';
|
|
3
|
+
export * from './gemma3/processing_gemma3.js';
|
|
3
4
|
export * from './gemma3n/processing_gemma3n.js';
|
|
5
|
+
export * from './glm46v/processing_glm46v.js';
|
|
4
6
|
export * from './granite_speech/processing_granite_speech.js';
|
|
5
7
|
export * from './grounding_dino/processing_grounding_dino.js';
|
|
6
8
|
export * from './idefics3/processing_idefics3.js';
|
|
@@ -22,6 +22,167 @@ export class Qwen2VLForConditionalGeneration extends Qwen2VLPreTrainedModel {
|
|
|
22
22
|
// embed_tokens + decoder_model_merged (no vision_encoder) via MultimodalLanguageModelOnly type.
|
|
23
23
|
image_grid_thw_name = 'grid_thw';
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Compute text-only 3D rope position IDs (all 3 dims get the same 1D positions).
|
|
27
|
+
* @param {Tensor} input_ids
|
|
28
|
+
* @param {Tensor} attention_mask
|
|
29
|
+
* @returns {[Tensor, Tensor]} [position_ids, mrope_position_deltas]
|
|
30
|
+
*/
|
|
31
|
+
_get_text_only_rope_index(input_ids, attention_mask) {
|
|
32
|
+
if (attention_mask) {
|
|
33
|
+
const { data, dims } = cumsum_masked_fill(attention_mask);
|
|
34
|
+
|
|
35
|
+
const position_ids = BigInt64Array.from({ length: 3 * data.length }, (_, i) => data[i % data.length]);
|
|
36
|
+
/** @type {bigint[]} */
|
|
37
|
+
const mrope_position_deltas = Array.from(
|
|
38
|
+
{ length: dims[0] },
|
|
39
|
+
(_, i) => max(data.subarray(dims[1] * i, dims[1] * (i + 1)))[0] + 1n + BigInt(dims[1]),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
return [
|
|
43
|
+
new Tensor('int64', position_ids, [3, ...dims]),
|
|
44
|
+
new Tensor('int64', mrope_position_deltas, [mrope_position_deltas.length, 1]),
|
|
45
|
+
];
|
|
46
|
+
} else {
|
|
47
|
+
const [batch_size, seq_length] = input_ids.dims;
|
|
48
|
+
const position_ids = BigInt64Array.from({ length: 3 * batch_size * seq_length }, (_, i) =>
|
|
49
|
+
BigInt(Math.floor((i % seq_length) / batch_size)),
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
return [new Tensor('int64', position_ids, [3, ...input_ids.dims]), zeros([batch_size, 1])];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Reorder per-segment position ID lists from [seg1[t,h,w], seg2[t,h,w], ...] into
|
|
58
|
+
* global [all_t, all_h, all_w] order, then write back into the position_ids array
|
|
59
|
+
* respecting attention mask.
|
|
60
|
+
* @param {number[][]} llm_pos_ids_list List of per-segment position arrays, each of length 3*seg_len
|
|
61
|
+
* @param {number[]} attn_mask Attention mask for this batch element
|
|
62
|
+
* @param {number[][][]} position_ids_list [3][batch][seq] output array to write into
|
|
63
|
+
* @param {number} batch_idx Current batch index
|
|
64
|
+
* @returns {number[]} Flat reordered positions of length total_len
|
|
65
|
+
*/
|
|
66
|
+
_reorder_and_write_positions(llm_pos_ids_list, attn_mask, position_ids_list, batch_idx) {
|
|
67
|
+
const total_len = llm_pos_ids_list.reduce((acc, x) => acc + x.length, 0);
|
|
68
|
+
const llm_positions = new Array(total_len);
|
|
69
|
+
let index = 0;
|
|
70
|
+
for (let x = 0; x < 3; ++x) {
|
|
71
|
+
for (const val of llm_pos_ids_list) {
|
|
72
|
+
const seg_len = val.length / 3;
|
|
73
|
+
for (let z = x * seg_len; z < (x + 1) * seg_len; ++z) {
|
|
74
|
+
llm_positions[index++] = val[z];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let count = 0;
|
|
80
|
+
for (let y = 0; y < attn_mask.length; ++y) {
|
|
81
|
+
if (attn_mask[y] == 1) {
|
|
82
|
+
for (let x = 0; x < 3; ++x) {
|
|
83
|
+
position_ids_list[x][batch_idx][y] = llm_positions[(x * total_len) / 3 + count];
|
|
84
|
+
}
|
|
85
|
+
++count;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return llm_positions;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Build per-batch position ID segments for multimodal rope.
|
|
94
|
+
* Override this in subclasses to change how vision/text segments are identified and positioned.
|
|
95
|
+
* @param {object} params
|
|
96
|
+
* @param {any[]} params.filtered_ids - attention-masked token IDs for this batch element
|
|
97
|
+
* @param {any[][]} params.image_grid_thw_list - all image grid dimensions
|
|
98
|
+
* @param {any[][]} params.video_grid_thw_list - all video grid dimensions
|
|
99
|
+
* @param {number} params.spatial_merge_size
|
|
100
|
+
* @param {{image_index: number, video_index: number}} params.state - mutable counters shared across batches
|
|
101
|
+
* @returns {number[][]} llm_pos_ids_list - segments of [t..., h..., w...] positions
|
|
102
|
+
*/
|
|
103
|
+
_get_multimodal_rope_positions({
|
|
104
|
+
filtered_ids,
|
|
105
|
+
image_grid_thw_list,
|
|
106
|
+
video_grid_thw_list,
|
|
107
|
+
spatial_merge_size,
|
|
108
|
+
state,
|
|
109
|
+
}) {
|
|
110
|
+
// @ts-ignore
|
|
111
|
+
const { image_token_id, video_token_id, vision_start_token_id } = this.config;
|
|
112
|
+
|
|
113
|
+
const ids = filtered_ids;
|
|
114
|
+
const vision_start_indices = ids.reduce((acc, x, idx) => {
|
|
115
|
+
if (x == vision_start_token_id) acc.push(idx);
|
|
116
|
+
return acc;
|
|
117
|
+
}, []);
|
|
118
|
+
|
|
119
|
+
const vision_tokens = vision_start_indices.map((x) => ids[x + 1]);
|
|
120
|
+
const image_nums = vision_tokens.filter((x) => x == image_token_id).length;
|
|
121
|
+
const video_nums = vision_tokens.filter((x) => x == video_token_id).length;
|
|
122
|
+
|
|
123
|
+
/** @type {number[][]} */
|
|
124
|
+
const llm_pos_ids_list = [];
|
|
125
|
+
let st = 0;
|
|
126
|
+
let remain_images = image_nums;
|
|
127
|
+
let remain_videos = video_nums;
|
|
128
|
+
for (let j = 0; j < vision_tokens.length; ++j) {
|
|
129
|
+
const next_image_token = ids.findIndex((x, i) => i > st && x == image_token_id);
|
|
130
|
+
const next_video_token = ids.findIndex((x, i) => i > st && x == video_token_id);
|
|
131
|
+
|
|
132
|
+
const ed_image = remain_images > 0 && next_image_token !== -1 ? next_image_token : ids.length + 1;
|
|
133
|
+
const ed_video = remain_videos > 0 && next_video_token !== -1 ? next_video_token : ids.length + 1;
|
|
134
|
+
|
|
135
|
+
let ed;
|
|
136
|
+
let t, h, w;
|
|
137
|
+
if (ed_image < ed_video) {
|
|
138
|
+
[t, h, w] = image_grid_thw_list[state.image_index];
|
|
139
|
+
++state.image_index;
|
|
140
|
+
--remain_images;
|
|
141
|
+
ed = ed_image;
|
|
142
|
+
} else {
|
|
143
|
+
[t, h, w] = video_grid_thw_list[state.video_index];
|
|
144
|
+
++state.video_index;
|
|
145
|
+
--remain_videos;
|
|
146
|
+
ed = ed_video;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const [llm_grid_t, llm_grid_h, llm_grid_w] = [
|
|
150
|
+
Number(t),
|
|
151
|
+
Math.floor(Number(h) / spatial_merge_size),
|
|
152
|
+
Math.floor(Number(w) / spatial_merge_size),
|
|
153
|
+
];
|
|
154
|
+
const text_len = ed - st;
|
|
155
|
+
const st_idx = llm_pos_ids_list.length > 0 ? max(llm_pos_ids_list.at(-1))[0] + 1 : 0;
|
|
156
|
+
|
|
157
|
+
llm_pos_ids_list.push(Array.from({ length: 3 * text_len }, (_, i) => st_idx + (i % text_len)));
|
|
158
|
+
|
|
159
|
+
const offset = text_len + st_idx;
|
|
160
|
+
const grid_size = llm_grid_t * llm_grid_h * llm_grid_w;
|
|
161
|
+
const t_index = Array.from(
|
|
162
|
+
{ length: grid_size },
|
|
163
|
+
(_, i) => offset + Math.floor(i / (llm_grid_h * llm_grid_w)),
|
|
164
|
+
);
|
|
165
|
+
const h_index = Array.from(
|
|
166
|
+
{ length: grid_size },
|
|
167
|
+
(_, i) => offset + (Math.floor(i / llm_grid_w) % llm_grid_h),
|
|
168
|
+
);
|
|
169
|
+
const w_index = Array.from({ length: grid_size }, (_, i) => offset + (i % llm_grid_w));
|
|
170
|
+
|
|
171
|
+
llm_pos_ids_list.push([t_index, h_index, w_index].flat());
|
|
172
|
+
|
|
173
|
+
st = ed + grid_size;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (st < ids.length) {
|
|
177
|
+
const st_idx = llm_pos_ids_list.length > 0 ? max(llm_pos_ids_list.at(-1))[0] + 1 : 0;
|
|
178
|
+
const text_len = ids.length - st;
|
|
179
|
+
|
|
180
|
+
llm_pos_ids_list.push(Array.from({ length: 3 * text_len }, (_, i) => st_idx + (i % text_len)));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return llm_pos_ids_list;
|
|
184
|
+
}
|
|
185
|
+
|
|
25
186
|
/**
|
|
26
187
|
* Calculate the 3D rope index based on image and video's temporal, height and width in LLM.
|
|
27
188
|
*
|
|
@@ -51,137 +212,49 @@ export class Qwen2VLForConditionalGeneration extends Qwen2VLPreTrainedModel {
|
|
|
51
212
|
* @param {Tensor} input_ids Indices of input sequence tokens in the vocabulary. Tensor of shape `(batch_size, sequence_length)`.
|
|
52
213
|
* @param {Tensor} image_grid_thw (Optional) The temporal, height and width of feature shape of each image in LLM. Tensor of shape `(num_images, 3)`.
|
|
53
214
|
* @param {Tensor} video_grid_thw (Optional) The temporal, height and width of feature shape of each video in LLM. Tensor of shape `(num_videos, 3)`.
|
|
54
|
-
* @param {Tensor} attention_mask (Optional) Mask to avoid performing attention on padding token indices. Tensor of shape `(batch_size, sequence_length)`.
|
|
55
|
-
*
|
|
56
|
-
* - 0 for tokens that are **masked**.
|
|
57
|
-
* @returns {[Tensor, Tensor]} [position_ids, mrope_position_deltas] with:
|
|
58
|
-
* - position_ids: Tensor of shape `(3, batch_size, sequence_length)`.
|
|
59
|
-
* - mrope_position_deltas: Tensor of shape `(batch_size)`.
|
|
215
|
+
* @param {Tensor} attention_mask (Optional) Mask to avoid performing attention on padding token indices. Tensor of shape `(batch_size, sequence_length)`.
|
|
216
|
+
* @returns {[Tensor, Tensor]} [position_ids, mrope_position_deltas]
|
|
60
217
|
*/
|
|
61
218
|
get_rope_index(input_ids, image_grid_thw, video_grid_thw, attention_mask) {
|
|
62
219
|
// @ts-ignore
|
|
63
|
-
const { vision_config
|
|
220
|
+
const { vision_config } = this.config;
|
|
64
221
|
const spatial_merge_size = vision_config.spatial_merge_size ?? 2;
|
|
65
222
|
|
|
66
|
-
const mrope_position_deltas = [];
|
|
67
223
|
if (image_grid_thw || video_grid_thw) {
|
|
68
|
-
|
|
224
|
+
const total_input_ids = input_ids.tolist();
|
|
69
225
|
if (!attention_mask) {
|
|
70
226
|
attention_mask = ones_like(input_ids);
|
|
71
227
|
}
|
|
72
228
|
|
|
73
229
|
const attention_mask_list = attention_mask.tolist();
|
|
74
|
-
const position_ids_list = Array.from({ length: 3 }, (
|
|
75
|
-
Array.from({ length: input_ids.dims[0] }, (
|
|
230
|
+
const position_ids_list = Array.from({ length: 3 }, () =>
|
|
231
|
+
Array.from({ length: input_ids.dims[0] }, () => Array.from({ length: input_ids.dims[1] }, () => 0)),
|
|
76
232
|
);
|
|
77
233
|
|
|
78
234
|
const image_grid_thw_list = image_grid_thw ? image_grid_thw.tolist() : [];
|
|
79
235
|
const video_grid_thw_list = video_grid_thw ? video_grid_thw.tolist() : [];
|
|
236
|
+
const state = { image_index: 0, video_index: 0 };
|
|
80
237
|
|
|
81
|
-
|
|
82
|
-
let video_index = 0;
|
|
238
|
+
const mrope_position_deltas = [];
|
|
83
239
|
for (let i = 0; i < total_input_ids.length; ++i) {
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
for (let j = 0; j < vision_tokens.length; ++j) {
|
|
101
|
-
const next_image_token = ids.findIndex((x, i) => i > st && x == image_token_id);
|
|
102
|
-
const next_video_token = ids.findIndex((x, i) => i > st && x == video_token_id);
|
|
103
|
-
|
|
104
|
-
const ed_image = remain_images > 0 && next_image_token !== -1 ? next_image_token : ids.length + 1;
|
|
105
|
-
|
|
106
|
-
const ed_video = remain_videos > 0 && next_video_token !== -1 ? next_video_token : ids.length + 1;
|
|
107
|
-
|
|
108
|
-
let ed;
|
|
109
|
-
let t, h, w;
|
|
110
|
-
if (ed_image < ed_video) {
|
|
111
|
-
[t, h, w] = image_grid_thw_list[image_index];
|
|
112
|
-
++image_index;
|
|
113
|
-
--remain_images;
|
|
114
|
-
ed = ed_image;
|
|
115
|
-
} else {
|
|
116
|
-
[t, h, w] = video_grid_thw_list[video_index];
|
|
117
|
-
++video_index;
|
|
118
|
-
--remain_videos;
|
|
119
|
-
ed = ed_video;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const [llm_grid_t, llm_grid_h, llm_grid_w] = [
|
|
123
|
-
Number(t),
|
|
124
|
-
Math.floor(Number(h) / spatial_merge_size),
|
|
125
|
-
Math.floor(Number(w) / spatial_merge_size),
|
|
126
|
-
];
|
|
127
|
-
const text_len = ed - st;
|
|
128
|
-
const st_idx = llm_pos_ids_list.length > 0 ? max(llm_pos_ids_list.at(-1))[0] + 1 : 0;
|
|
129
|
-
|
|
130
|
-
llm_pos_ids_list.push(Array.from({ length: 3 * text_len }, (_, i) => st_idx + (i % text_len)));
|
|
131
|
-
|
|
132
|
-
const offset = text_len + st_idx;
|
|
133
|
-
const grid_size = llm_grid_t * llm_grid_h * llm_grid_w;
|
|
134
|
-
const t_index = Array.from(
|
|
135
|
-
{ length: grid_size },
|
|
136
|
-
(_, i) => offset + Math.floor(i / (llm_grid_h * llm_grid_w)),
|
|
137
|
-
);
|
|
138
|
-
const h_index = Array.from(
|
|
139
|
-
{ length: grid_size },
|
|
140
|
-
(_, i) => offset + (Math.floor(i / llm_grid_w) % llm_grid_h),
|
|
141
|
-
);
|
|
142
|
-
const w_index = Array.from({ length: grid_size }, (_, i) => offset + (i % llm_grid_w));
|
|
143
|
-
|
|
144
|
-
llm_pos_ids_list.push([t_index, h_index, w_index].flat());
|
|
145
|
-
|
|
146
|
-
st = ed + grid_size;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (st < ids.length) {
|
|
150
|
-
const st_idx = llm_pos_ids_list.length > 0 ? max(llm_pos_ids_list.at(-1))[0] + 1 : 0;
|
|
151
|
-
const text_len = ids.length - st;
|
|
152
|
-
|
|
153
|
-
llm_pos_ids_list.push(Array.from({ length: 3 * text_len }, (_, i) => st_idx + (i % text_len)));
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// NOTE: Each item in llm_pos_ids_list is an array of shape (3, text_len),
|
|
157
|
-
// meaning to perform concatenation along dim=1, we can do the following:
|
|
158
|
-
const num_items = llm_pos_ids_list.reduce((acc, x) => acc + x.length, 0);
|
|
159
|
-
/** @type {number[]} */
|
|
160
|
-
const llm_positions = new Array(num_items);
|
|
161
|
-
let index = 0;
|
|
162
|
-
for (let x = 0; x < 3; ++x) {
|
|
163
|
-
for (let y = 0; y < llm_pos_ids_list.length; ++y) {
|
|
164
|
-
const val = llm_pos_ids_list[y];
|
|
165
|
-
const text_len = val.length / 3;
|
|
166
|
-
for (let z = x * text_len; z < (x + 1) * text_len; ++z) {
|
|
167
|
-
llm_positions[index++] = val[z];
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
let count = 0;
|
|
173
|
-
const attn_mask = attention_mask_list[i];
|
|
174
|
-
for (let y = 0; y < attn_mask.length; ++y) {
|
|
175
|
-
if (attn_mask[y] == 1) {
|
|
176
|
-
for (let x = 0; x < 3; ++x) {
|
|
177
|
-
position_ids_list[x][i][y] = llm_positions[(x * num_items) / 3 + count];
|
|
178
|
-
}
|
|
179
|
-
++count;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
240
|
+
const filtered_ids = total_input_ids[i].filter((_, j) => attention_mask_list[i][j] == 1);
|
|
241
|
+
|
|
242
|
+
const llm_pos_ids_list = this._get_multimodal_rope_positions({
|
|
243
|
+
filtered_ids,
|
|
244
|
+
image_grid_thw_list,
|
|
245
|
+
video_grid_thw_list,
|
|
246
|
+
spatial_merge_size,
|
|
247
|
+
state,
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const llm_positions = this._reorder_and_write_positions(
|
|
251
|
+
llm_pos_ids_list,
|
|
252
|
+
attention_mask_list[i],
|
|
253
|
+
position_ids_list,
|
|
254
|
+
i,
|
|
255
|
+
);
|
|
182
256
|
|
|
183
|
-
|
|
184
|
-
mrope_position_deltas.push(max_llm_positions + 1 - total_input_ids[i].length);
|
|
257
|
+
mrope_position_deltas.push(max(llm_positions)[0] + 1 - total_input_ids[i].length);
|
|
185
258
|
}
|
|
186
259
|
|
|
187
260
|
return [
|
|
@@ -189,29 +262,7 @@ export class Qwen2VLForConditionalGeneration extends Qwen2VLPreTrainedModel {
|
|
|
189
262
|
new Tensor('int64', mrope_position_deltas, [mrope_position_deltas.length, 1]),
|
|
190
263
|
];
|
|
191
264
|
} else {
|
|
192
|
-
|
|
193
|
-
if (attention_mask) {
|
|
194
|
-
const { data, dims } = cumsum_masked_fill(attention_mask);
|
|
195
|
-
|
|
196
|
-
const position_ids = BigInt64Array.from({ length: 3 * data.length }, (_, i) => data[i % data.length]);
|
|
197
|
-
/** @type {bigint[]} */
|
|
198
|
-
const mrope_position_deltas = Array.from(
|
|
199
|
-
{ length: dims[0] },
|
|
200
|
-
(_, i) => max(data.subarray(dims[1] * i, dims[1] * (i + 1)))[0] + 1n + BigInt(dims[1]),
|
|
201
|
-
);
|
|
202
|
-
|
|
203
|
-
return [
|
|
204
|
-
new Tensor('int64', position_ids, [3, ...dims]),
|
|
205
|
-
new Tensor('int64', mrope_position_deltas, [mrope_position_deltas.length, 1]),
|
|
206
|
-
];
|
|
207
|
-
} else {
|
|
208
|
-
const [batch_size, seq_length] = input_ids.dims;
|
|
209
|
-
const position_ids = BigInt64Array.from({ length: 3 * batch_size * seq_length }, (_, i) =>
|
|
210
|
-
BigInt(Math.floor((i % seq_length) / batch_size)),
|
|
211
|
-
);
|
|
212
|
-
|
|
213
|
-
return [new Tensor('int64', position_ids, [3, ...input_ids.dims]), zeros([batch_size, 1])];
|
|
214
|
-
}
|
|
265
|
+
return this._get_text_only_rope_index(input_ids, attention_mask);
|
|
215
266
|
}
|
|
216
267
|
}
|
|
217
268
|
|
|
@@ -235,50 +286,57 @@ export class Qwen2VLForConditionalGeneration extends Qwen2VLPreTrainedModel {
|
|
|
235
286
|
|
|
236
287
|
prepare_inputs_for_generation(input_ids, model_inputs, generation_config) {
|
|
237
288
|
// Overwritten -- in specific circumstances we don't want to forward image inputs to the model
|
|
238
|
-
if (model_inputs.attention_mask
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
289
|
+
if (!model_inputs.attention_mask || model_inputs.position_ids) {
|
|
290
|
+
return model_inputs;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const session = this.sessions['decoder_model_merged'] ?? this.sessions['model'];
|
|
294
|
+
if (!session.inputNames.includes('position_ids')) {
|
|
295
|
+
return model_inputs;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Calculate position_ids and rope_deltas
|
|
299
|
+
if (!model_inputs.past_key_values) {
|
|
300
|
+
[model_inputs.position_ids, model_inputs.rope_deltas] = this.get_rope_index(
|
|
301
|
+
model_inputs.input_ids,
|
|
302
|
+
model_inputs.image_grid_thw,
|
|
303
|
+
model_inputs.video_grid_thw,
|
|
304
|
+
model_inputs.attention_mask,
|
|
305
|
+
);
|
|
306
|
+
} else {
|
|
307
|
+
model_inputs.pixel_values = null;
|
|
308
|
+
// model_inputs.pixel_values_videos = null;
|
|
309
|
+
|
|
310
|
+
const past_length = model_inputs.past_key_values.get_seq_length();
|
|
311
|
+
|
|
312
|
+
if (past_length < model_inputs.input_ids.dims[1]) {
|
|
313
|
+
// Externally provided `past_key_values` with full input_ids:
|
|
314
|
+
// Compute full position_ids, then slice to only the new (unprocessed) tokens.
|
|
315
|
+
const [full_position_ids, rope_deltas] = this.get_rope_index(
|
|
242
316
|
model_inputs.input_ids,
|
|
243
317
|
model_inputs.image_grid_thw,
|
|
244
318
|
model_inputs.video_grid_thw,
|
|
245
319
|
model_inputs.attention_mask,
|
|
246
320
|
);
|
|
321
|
+
model_inputs.rope_deltas = rope_deltas;
|
|
322
|
+
model_inputs.position_ids = full_position_ids.slice(null, null, [past_length, null]);
|
|
323
|
+
model_inputs.input_ids = model_inputs.input_ids.slice(null, [past_length, null]);
|
|
247
324
|
} else {
|
|
248
|
-
|
|
249
|
-
//
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
if (past_length < model_inputs.input_ids.dims[1]) {
|
|
254
|
-
// Externally provided `past_key_values` with full input_ids:
|
|
255
|
-
// Compute full position_ids, then slice to only the new (unprocessed) tokens.
|
|
256
|
-
const [full_position_ids, rope_deltas] = this.get_rope_index(
|
|
325
|
+
// Auto-regressive case: single new token.
|
|
326
|
+
// `rope_deltas` may be absent when generation starts from externally provided `past_key_values`.
|
|
327
|
+
// In that case, recompute from current inputs instead of relying on persisted model state.
|
|
328
|
+
if (!model_inputs.rope_deltas) {
|
|
329
|
+
[, model_inputs.rope_deltas] = this.get_rope_index(
|
|
257
330
|
model_inputs.input_ids,
|
|
258
331
|
model_inputs.image_grid_thw,
|
|
259
332
|
model_inputs.video_grid_thw,
|
|
260
333
|
model_inputs.attention_mask,
|
|
261
334
|
);
|
|
262
|
-
model_inputs.rope_deltas = rope_deltas;
|
|
263
|
-
model_inputs.position_ids = full_position_ids.slice(null, null, [past_length, null]);
|
|
264
|
-
model_inputs.input_ids = model_inputs.input_ids.slice(null, [past_length, null]);
|
|
265
|
-
} else {
|
|
266
|
-
// Auto-regressive case: single new token.
|
|
267
|
-
// `rope_deltas` may be absent when generation starts from externally provided `past_key_values`.
|
|
268
|
-
// In that case, recompute from current inputs instead of relying on persisted model state.
|
|
269
|
-
if (!model_inputs.rope_deltas) {
|
|
270
|
-
[, model_inputs.rope_deltas] = this.get_rope_index(
|
|
271
|
-
model_inputs.input_ids,
|
|
272
|
-
model_inputs.image_grid_thw,
|
|
273
|
-
model_inputs.video_grid_thw,
|
|
274
|
-
model_inputs.attention_mask,
|
|
275
|
-
);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
const delta = BigInt(past_length);
|
|
279
|
-
const rope_deltas_list = model_inputs.rope_deltas.map((x) => delta + x);
|
|
280
|
-
model_inputs.position_ids = stack([rope_deltas_list, rope_deltas_list, rope_deltas_list], 0);
|
|
281
335
|
}
|
|
336
|
+
|
|
337
|
+
const delta = BigInt(past_length);
|
|
338
|
+
const rope_deltas_list = model_inputs.rope_deltas.map((x) => delta + x);
|
|
339
|
+
model_inputs.position_ids = stack([rope_deltas_list, rope_deltas_list, rope_deltas_list], 0);
|
|
282
340
|
}
|
|
283
341
|
}
|
|
284
342
|
|
|
@@ -6,6 +6,7 @@ import { RawImage } from '../../utils/image.js';
|
|
|
6
6
|
export class Qwen2VLProcessor extends Processor {
|
|
7
7
|
static image_processor_class = AutoImageProcessor;
|
|
8
8
|
static tokenizer_class = AutoTokenizer;
|
|
9
|
+
static image_token = '<|image_pad|>';
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
*
|
|
@@ -31,13 +32,14 @@ export class Qwen2VLProcessor extends Processor {
|
|
|
31
32
|
let merge_length = this.image_processor.config.merge_size ** 2;
|
|
32
33
|
let index = 0;
|
|
33
34
|
|
|
35
|
+
const image_token = /** @type {typeof Qwen2VLProcessor} */ (this.constructor).image_token;
|
|
34
36
|
const image_grid_thw_list = image_grid_thw.tolist();
|
|
35
37
|
text = text.map((t) => {
|
|
36
|
-
while (t.includes(
|
|
38
|
+
while (t.includes(image_token)) {
|
|
37
39
|
const prod = Number(image_grid_thw_list[index++].reduce((a, b) => a * b, 1n));
|
|
38
|
-
t = t.replace(
|
|
40
|
+
t = t.replace(image_token, '<|placeholder|>'.repeat(Math.floor(prod / merge_length)));
|
|
39
41
|
}
|
|
40
|
-
return t.replaceAll('<|placeholder|>',
|
|
42
|
+
return t.replaceAll('<|placeholder|>', image_token);
|
|
41
43
|
});
|
|
42
44
|
}
|
|
43
45
|
|
|
@@ -46,7 +48,6 @@ export class Qwen2VLProcessor extends Processor {
|
|
|
46
48
|
return {
|
|
47
49
|
...text_inputs,
|
|
48
50
|
...image_inputs,
|
|
49
|
-
// TODO: ...videos_inputs,
|
|
50
51
|
};
|
|
51
52
|
}
|
|
52
53
|
}
|