Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOGS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Change Logs
0.7.9
+++++

* :pr:`214`: fix modelbuilder export
* :pr:`213`: use DYNAMIC on batch size

0.7.8
+++++

Expand Down
2 changes: 1 addition & 1 deletion _doc/examples/plot_export_locate_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def forward(self, x: torch.Tensor, ys: list[torch.Tensor]):
with torch_export_patches(stop_if_static=1, verbose=1):
try:
torch.export.export(model, inputs, dynamic_shapes=dyn_shapes)
except (AssertionError, torch._dynamo.exc.TorchRuntimeError) as e:
except (AssertionError, ValueError, torch._dynamo.exc.TorchRuntimeError) as e:
print("-- It failed as excepted.")
print(f"-- final error is {e}")
print("-- Stack Trace")
Expand Down
64 changes: 10 additions & 54 deletions _unittests/ut_tasks/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,38 +57,16 @@ def test_automatic_speech_recognition_float32(self):
model, inputs, ds = data["model"], data["inputs"], data["dynamic_shapes"]
model(**data["inputs"])
model(**data["inputs2"])
Dim = torch.export.Dim
self.maxDiff = None
self.assertIn("{0:Dim(batch),1:DYN(seq_length)}", self.string_type(ds))
self.assertIn("{0:DYN(batch),1:DYN(seq_length)}", self.string_type(ds))
self.assertEqualAny(
{
"decoder_input_ids": {
0: Dim("batch", min=1, max=1024),
1: "seq_length",
},
"decoder_input_ids": {0: "batch", 1: "seq_length"},
"cache_position": {0: "seq_length"},
"encoder_outputs": [{0: Dim("batch", min=1, max=1024)}],
"encoder_outputs": [{0: "batch"}],
"past_key_values": [
[
[
{0: Dim("batch", min=1, max=1024)},
{0: Dim("batch", min=1, max=1024)},
],
[
{0: Dim("batch", min=1, max=1024)},
{0: Dim("batch", min=1, max=1024)},
],
],
[
[
{0: Dim("batch", min=1, max=1024)},
{0: Dim("batch", min=1, max=1024)},
],
[
{0: Dim("batch", min=1, max=1024)},
{0: Dim("batch", min=1, max=1024)},
],
],
[[{0: "batch"}, {0: "batch"}], [{0: "batch"}, {0: "batch"}]],
[[{0: "batch"}, {0: "batch"}], [{0: "batch"}, {0: "batch"}]],
],
},
ds,
Expand Down Expand Up @@ -134,38 +112,16 @@ def test_automatic_speech_recognition_float16(self):
model, inputs, ds = data["model"], data["inputs"], data["dynamic_shapes"]
model = to_any(model, torch.float16)
model(**data["inputs2"])
Dim = torch.export.Dim
self.maxDiff = None
self.assertIn("{0:Dim(batch),1:DYN(seq_length)}", self.string_type(ds))
self.assertIn("{0:DYN(batch),1:DYN(seq_length)}", self.string_type(ds))
self.assertEqualAny(
{
"decoder_input_ids": {
0: Dim("batch", min=1, max=1024),
1: "seq_length",
},
"decoder_input_ids": {0: "batch", 1: "seq_length"},
"cache_position": {0: "seq_length"},
"encoder_outputs": [{0: Dim("batch", min=1, max=1024)}],
"encoder_outputs": [{0: "batch"}],
"past_key_values": [
[
[
{0: Dim("batch", min=1, max=1024)},
{0: Dim("batch", min=1, max=1024)},
],
[
{0: Dim("batch", min=1, max=1024)},
{0: Dim("batch", min=1, max=1024)},
],
],
[
[
{0: Dim("batch", min=1, max=1024)},
{0: Dim("batch", min=1, max=1024)},
],
[
{0: Dim("batch", min=1, max=1024)},
{0: Dim("batch", min=1, max=1024)},
],
],
[[{0: "batch"}, {0: "batch"}], [{0: "batch"}, {0: "batch"}]],
[[{0: "batch"}, {0: "batch"}], [{0: "batch"}, {0: "batch"}]],
],
},
ds,
Expand Down
5 changes: 5 additions & 0 deletions onnx_diagnostic/helpers/model_builder_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,12 @@ def create_model_builder(
arch_map = {
"ChatGLMForConditionalGeneration": builder.ChatGLMModel,
"ChatGLMModel": builder.ChatGLMModel,
"Ernie4_5_ForCausalLM": builder.ErnieModel,
"GemmaForCausalLM": builder.Gemma2Model,
"Gemma3ForCausalLM": builder.Gemma3Model,
"Gemma3ForConditionalGeneration": builder.Gemma3Model,
"GraniteForCausalLM": builder.GraniteModel,
"GptOssForCausalLM": builder.GPTOSSModel,
"LlamaForCausalLM": builder.LlamaModel,
"MistralForCausalLM": builder.MistralModel,
"NemotronForCausalLM": builder.NemotronModel,
Expand Down Expand Up @@ -235,6 +237,7 @@ def create_model_builder(
"Phi4MMForCausalLM": builder.Phi4MMModel,
"Qwen2ForCausalLM": builder.QwenModel,
"Qwen3ForCausalLM": builder.Qwen3Model,
"SmolLM3ForCausalLM": builder.SmolLM3Model,
}

assert config.architectures[0] in arch_map, (
Expand Down Expand Up @@ -276,6 +279,8 @@ def _post(onnx_model):
for key in text_config:
if not hasattr(config, key):
setattr(config, key, getattr(text_config, key))
elif config.architectures[0] == "GptOssForCausalLM":
delattr(config, "quantization_config")
elif (
config.architectures[0] == "PhiMoEForCausalLM"
and config.max_position_embeddings != config.original_max_position_embeddings
Expand Down
5 changes: 4 additions & 1 deletion onnx_diagnostic/torch_export_patches/patches/patch_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ def _set_replacement(
# Precondition: a == tgt
assert isinstance(a, sympy.Symbol)

if self.allow_complex_guards_as_runtime_asserts and not _is_supported_equivalence(tgt):
if (
getattr(self, "allow_complex_guards_as_runtime_asserts", False)
or getattr(self, "prefer_deferred_runtime_asserts_over_guards", False)
) and not _is_supported_equivalence(tgt):
# continuing leads to placeholder shapes
# having complex expressions that we can't resolve
return
Expand Down
12 changes: 9 additions & 3 deletions onnx_diagnostic/torch_models/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,15 @@ def validate_model(
cpl = CoupleInputsDynamicShapes(
tuple(), data[k], dynamic_shapes=data["dynamic_shapes"]
)
data[k] = cpl.change_dynamic_dimensions(
desired_values=dict(batch=1), only_desired=True
)
if patch_kwargs.get("patch", False):
with torch_export_patches(**patch_kwargs): # type: ignore[arg-type]
data[k] = cpl.change_dynamic_dimensions(
desired_values=dict(batch=1), only_desired=True
)
else:
data[k] = cpl.change_dynamic_dimensions(
desired_values=dict(batch=1), only_desired=True
)
if verbose:
print(f"[validate_model] batch=1 --> {string_type(data[k], with_shape=True)}")

Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ matplotlib
onnx-array-api>=0.3.1
onnx
git+https://github.com/onnx/ir-py.git
git+https://github.com/microsoft/onnxscript.git
onnxscript
openpyxl
packaging
pandas
Expand Down
Loading