[builder.py] How to Expand an input to a shape which is determined by another input? i.e., 'shape' is determined at runtime #1797
Replies: 1 comment
-
|
Thank you for your efforts! The line, One note is that some of the rotary embedding logic can usually be pre-computed ahead of time. For example, the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I am trying to add Qwen2.5VL-3B > Text Rotary embedding logic to my copy of onnxruntime-genai > python/py/models/builder.py
I am trying to use node "Expand" for initializing rotary sin-cos embeddings for Qwen2.5VL-3B.
The 'shape' input to Expand is made from position_ids.shape[1], where position_ids is an input and does not have a fixed shape.
See python3.11/site-packages/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py > Qwen2_5_VLRotaryEmbedding > forward:
inv_freq_expanded = self.inv_freq[None, None, :, None].float().expand(3, position_ids.shape[1], -1, 1)
So, in my copy of builder.py I tried to make a "Shape" node to obtain shape[1]
self.make_shape("/model/rot_emb_cache_from_scratch/shape_of_position_ids", position_ids, shape=[1])
This produces output node '/model/rot_emb_cache_from_scratch/shape_of_position_ids/output' providing value for position_ids.shape[1]
Now, how do I make this value as an input to "Expand" node?
As mentioned above, the transformers code I am trying to mirror is:
inv_freq_expanded = inv_freq[None, None, :, None].float().expand(3, position_ids.shape[1], -1, 1)
So I do
reshaped_inv_freq_for_expansion = inv_freq[None, None, :, None].float()
self.make_initializer(reshaped_inv_freq_for_expansion,
'/model/rot_emb_cache_from_scratch/inv_freq_before_expansion',
self.io_dtype)
self.make_expand('/model/rot_emb_cache_from_scratch/inv_freq_expand',
['/model/rot_emb_cache_from_scratch/inv_freq_before_expansion'],
self.io_dtype, shape=[3, ' HOW DO I PASS VALUE HERE?', -1, 1])
How do I make a tensor from the output '/model/rot_emb_cache_from_scratch/shape_of_position_ids/output' and pass it to Expand node as shape?
I tried searching online and asking code assistants, it seems like we cannot attach a tensor to ONNX graph if the value is determined at runtime. Any workarounds?
Beta Was this translation helpful? Give feedback.
All reactions