11# type: ignore
22"""
3- Tests for sharing a dynamic dimension across inputs via ``Input(name_dims =...)``.
3+ Tests for sharing a dynamic dimension across inputs via ``Input(shared_dims =...)``.
44
55Background: when a model takes multiple inputs whose dynamic axes must be
66**equal at runtime** (e.g. HF encoders with ``input_ids`` / ``attention_mask``
99constraint check for any forward() that broadcasts across those axes (here:
1010``embed(input_ids) * mask.unsqueeze(-1)``), raising ``ConstraintViolationError``.
1111
12- ``Input(name_dims ={axis: name})`` lets the caller tag a dynamic axis with a
12+ ``Input(shared_dims ={axis: name})`` lets the caller tag a dynamic axis with a
1313name; the same name across inputs is exported as a single shared ``Dim``. All
1414the dynamic-shape intent lives on the ``Input`` objects -- no separate
1515``dynamic_shapes`` argument and no ``torch.export`` knowledge required at the
@@ -54,14 +54,14 @@ def _named_input(name: str, seq: int = 16, batch_min: int = 1, batch_max: int =
5454 max_shape = (batch_max , seq ),
5555 dtype = torch .int64 ,
5656 name = name ,
57- name_dims = {0 : "B" },
57+ shared_dims = {0 : "B" },
5858 )
5959
6060
6161@pytest .mark .unit
6262@pytest .mark .critical
63- def test_name_dims_shared_batch_kwarg_inputs ():
64- """Shared batch axis declared via ``Input(name_dims ={0: "B"})`` on both
63+ def test_shared_dims_shared_batch_kwarg_inputs ():
64+ """Shared batch axis declared via ``Input(shared_dims ={0: "B"})`` on both
6565 kwarg inputs -- same name => one exported symbol; engine matches eager."""
6666 model = _SharedBatchEncoder ().eval ().cuda ()
6767
@@ -91,12 +91,12 @@ def test_name_dims_shared_batch_kwarg_inputs():
9191 cos_sim = cosine_similarity (ref , out )
9292 assertions .assertTrue (
9393 cos_sim > COSINE_THRESHOLD ,
94- f"name_dims shared batch (kwargs) out-of-tolerance at bs={ bs } : cos_sim={ cos_sim } " ,
94+ f"shared_dims shared batch (kwargs) out-of-tolerance at bs={ bs } : cos_sim={ cos_sim } " ,
9595 )
9696
9797
9898@pytest .mark .unit
99- def test_name_dims_shared_batch_positional_inputs ():
99+ def test_shared_dims_shared_batch_positional_inputs ():
100100 """Same feature with positional ``inputs=[...]`` instead of kwargs."""
101101 model = _SharedBatchEncoder ().eval ().cuda ()
102102
@@ -125,12 +125,12 @@ def test_name_dims_shared_batch_positional_inputs():
125125 cos_sim = cosine_similarity (ref , out )
126126 assertions .assertTrue (
127127 cos_sim > COSINE_THRESHOLD ,
128- f"name_dims shared batch (positional) out-of-tolerance at bs={ bs } : cos_sim={ cos_sim } " ,
128+ f"shared_dims shared batch (positional) out-of-tolerance at bs={ bs } : cos_sim={ cos_sim } " ,
129129 )
130130
131131
132132@pytest .mark .unit
133- def test_name_dims_shared_batch_mixed_args_and_kwargs ():
133+ def test_shared_dims_shared_batch_mixed_args_and_kwargs ():
134134 """input_ids passed positionally, attention_mask as a kwarg; both share "B"."""
135135 model = _SharedBatchEncoder ().eval ().cuda ()
136136
@@ -155,12 +155,12 @@ def test_name_dims_shared_batch_mixed_args_and_kwargs():
155155 cos_sim = cosine_similarity (ref , out )
156156 assertions .assertTrue (
157157 cos_sim > COSINE_THRESHOLD ,
158- f"name_dims shared batch (mixed) out-of-tolerance at bs={ bs } : cos_sim={ cos_sim } " ,
158+ f"shared_dims shared batch (mixed) out-of-tolerance at bs={ bs } : cos_sim={ cos_sim } " ,
159159 )
160160
161161
162162@pytest .mark .unit
163- def test_name_dims_conflicting_ranges_raises ():
163+ def test_shared_dims_conflicting_ranges_raises ():
164164 """Same name with different (min, max) across inputs is a user error."""
165165 from torch_tensorrt .dynamo ._tracer import build_dim_registry
166166
@@ -172,23 +172,23 @@ def test_name_dims_conflicting_ranges_raises():
172172 max_shape = (4 , seq ),
173173 dtype = torch .int64 ,
174174 name = "input_ids" ,
175- name_dims = {0 : "B" },
175+ shared_dims = {0 : "B" },
176176 ),
177177 "attention_mask" : torchtrt .Input (
178178 min_shape = (1 , seq ),
179179 opt_shape = (8 , seq ),
180180 max_shape = (8 , seq ),
181181 dtype = torch .int64 ,
182182 name = "attention_mask" ,
183- name_dims = {0 : "B" },
183+ shared_dims = {0 : "B" },
184184 ),
185185 }
186186 with assertions .assertRaises (ValueError ):
187187 build_dim_registry ((), inputs )
188188
189189
190190@pytest .mark .unit
191- def test_name_dims_rejected_on_static_axis ():
191+ def test_shared_dims_rejected_on_static_axis ():
192192 """Naming a static axis (min == max) is rejected at Input construction."""
193193 with assertions .assertRaises (ValueError ):
194194 torchtrt .Input (
@@ -197,12 +197,12 @@ def test_name_dims_rejected_on_static_axis():
197197 max_shape = (1 , 16 ),
198198 dtype = torch .int64 ,
199199 name = "x" ,
200- name_dims = {0 : "B" },
200+ shared_dims = {0 : "B" },
201201 )
202202
203203
204204@pytest .mark .unit
205- def test_name_dims_rejected_on_out_of_range_axis ():
205+ def test_shared_dims_rejected_on_out_of_range_axis ():
206206 """An axis index outside the input's rank is rejected at construction."""
207207 with assertions .assertRaises (ValueError ):
208208 torchtrt .Input (
@@ -211,13 +211,13 @@ def test_name_dims_rejected_on_out_of_range_axis():
211211 max_shape = (4 , 16 ),
212212 dtype = torch .int64 ,
213213 name = "x" ,
214- name_dims = {5 : "B" }, # rank is 2; axis 5 does not exist
214+ shared_dims = {5 : "B" }, # rank is 2; axis 5 does not exist
215215 )
216216
217217
218218@pytest .mark .unit
219219def test_default_path_unchanged_for_static_inputs ():
220- """Sanity check: a fully static input with no name_dims is unchanged."""
220+ """Sanity check: a fully static input with no shared_dims is unchanged."""
221221
222222 class StaticModel (nn .Module ):
223223 def __init__ (self ):
0 commit comments