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
6 changes: 3 additions & 3 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ jobs:

- name: Check for errors and warnings
run: |
if [[ $(grep ERROR doc.txt | grep -v 'l-plot-tiny-llm-export') ]]; then
if [[ $(grep ERROR doc.txt | grep -v 'l-plot-tiny-llm-export' | grep -v 'Unexpected section title or transition.') ]]; then
echo "Documentation produces errors."
grep ERROR doc.txt | grep -v 'l-plot-tiny-llm-export'
grep ERROR doc.txt | grep -v 'l-plot-tiny-llm-export' | grep -v 'Unexpected section title or transition.'
exit 1
fi
if [[ $(grep WARNING doc.txt | grep -v 'l-plot-tiny-llm-export' | grep -v 'Inline emphasis start-string' | grep -v 'Definition list ends without a blank line' | grep -v 'Unexpected section title or transition' | grep -v 'Inline strong start-string' | grep -v 'MambaCache') ]]; then
if [[ $(grep WARNING doc.txt | grep -v 'l-plot-tiny-llm-export' | grep -v 'Inline emphasis start-string' | grep -v 'Definition list ends without a blank line' | grep -v 'Unexpected section title or transition' | grep -v 'Inline strong start-string' | grep -v 'MambaCache' ) ]]; then
echo "Documentation produces warnings."
grep WARNING doc.txt | grep -v 'l-plot-tiny-llm-export' | grep -v 'Inline emphasis start-string' | grep -v 'Definition list ends without a blank line' | grep -v 'Unexpected section title or transition' | grep -v 'Inline strong start-string' | grep -v 'MambaCache'
exit 1
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOGS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Change Logs
0.8.8
+++++

* :pr:`371`: fix make_fake_with_dynamic_dimensions

0.8.7
+++++

Expand Down
16 changes: 16 additions & 0 deletions _unittests/ut_torch_models/test_validate_whole_models1.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,22 @@ def test_n_validate_phi35_mini_instruct(self):
self.assertIn("If", op_types)
self.clean_dump()

@hide_stdout()
@requires_transformers("4.57")
def test_o_validate_model_export_fake(self):
mid = "arnir0/Tiny-LLM"
summary, data = validate_model(
mid,
do_run=True,
verbose=10,
exporter="custom-fake",
dump_folder="dump_test/validate_model_export_fake",
patch=True,
)
self.assertIsInstance(summary, dict)
self.assertIsInstance(data, dict)
self.clean_dump()


if __name__ == "__main__":
unittest.main(verbosity=2)
1 change: 1 addition & 0 deletions onnx_diagnostic/export/shape_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ def make_fake_with_dynamic_dimensions(
This uses function :func:`onnx_diagnostic.helpers.fake_tensor_helper.make_fake`.
Parameter ``existing`` is used to reused the same object when the dynamic
dimension is given the same name as another one.
This function works with caches only if ``transformers>=4.57``.

A simple tensor:

Expand Down
23 changes: 20 additions & 3 deletions onnx_diagnostic/helpers/fake_tensor_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,18 @@ def make_fake_with_dynamic_dimensions(self, x: Any, dynamic_shapes: Any) -> Any:
"""
See
:func:`onnx_diagnostic.export.shape_helper.make_fake_with_dynamic_dimensions`.
If caches are used, it requires ``transformers>=4.57``.
"""
if x is None:
return None, None
if isinstance(x, (list, tuple)):
if type(x) in (list, tuple):
return x.__class__(
[
self.make_fake_with_dynamic_dimensions(i, dynamic_shapes=ds)
for i, ds in zip(x, dynamic_shapes)
]
)
if isinstance(x, dict):
if type(x) is dict:
return {
k: self.make_fake_with_dynamic_dimensions(v, dynamic_shapes=dynamic_shapes[k])
for k, v in x.items()
Expand Down Expand Up @@ -187,6 +188,17 @@ def make_fake_with_dynamic_dimensions(self, x: Any, dynamic_shapes: Any) -> Any:
x.cross_attention_cache, dynamic_shapes=dynamic_shapes[1]
)
return x
if x.__class__.__name__ == "BaseModelOutput":
assert (
list(x.keys()) == ["last_hidden_state"] and x.last_hidden_state is not None
), (
f"Field 'last_hidden_state' is empty for {type(x)} or other fields "
f"{list(x.keys())} are used."
)
x.last_hidden_state = self.make_fake_with_dynamic_dimensions(
x.last_hidden_state, dynamic_shapes=dynamic_shapes[0]
)
return x
if hasattr(x, "shape"):
assert dynamic_shapes is None or isinstance(dynamic_shapes, dict), (
f"dynamic_shapes must be a dictionary at this stage but "
Expand All @@ -197,9 +209,11 @@ def make_fake_with_dynamic_dimensions(self, x: Any, dynamic_shapes: Any) -> Any:
for idim, dim in enumerate(x.shape):
if dynamic_shapes is not None and idim in dynamic_shapes:
s = dynamic_shapes[idim]
if s.__class__.__name__ == "Dim":
s = s.__name__
assert isinstance(s, str), (
f"Unexpected type {type(s)} in dynamic_shapes={dynamic_shapes} "
f"at index {idim}"
f"at index {idim}, self._mapping_str={self._mapping_str}"
)
if s in self._mapping_str:
dim = self._mapping_str[s]
Expand All @@ -221,6 +235,9 @@ def make_fake_with_dynamic_dimensions(self, x: Any, dynamic_shapes: Any) -> Any:
assert t.device == x.device, f"device mismatch {x.device} -> {t.device}"
assert t.dtype == x.dtype, f"dtype mismatch {x.dtype} -> {t.dtype}"
return t
if isinstance(x, (int, bool, float)):
# It is a constant, we don't change that.
return x
from ..helpers import string_type

raise TypeError(
Expand Down
9 changes: 9 additions & 0 deletions onnx_diagnostic/torch_models/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2330,6 +2330,7 @@ def call_torch_export_custom(
"custom-dec",
"custom-decall",
"custom-fake",
"custom-tracing",
}
assert exporter in available, f"Unexpected value for exporter={exporter!r} in {available}"
assert "model" in data, f"model is missing from data: {sorted(data)}"
Expand All @@ -2342,11 +2343,16 @@ def call_torch_export_custom(
f"Options strict cannot be specified in the exporter name {exporter!r} "
f"and in the options {exporter_options}"
)
assert ("-tracing" not in exporter) or ("tracing" not in exporter_options), (
f"Options tracing cannot be specified in the exporter name {exporter!r} "
f"and in the options {exporter_options}"
)
summary: Dict[str, Union[str, int, float]] = {}
strict = "-strict" in exporter or exporter_options.pop("strict", False)
args, kwargs = split_args_kwargs(data["inputs_export"])
ds = data.get("dynamic_shapes", None)
fake = "-fake" in exporter or exporter_options.pop("fake", False)
tracing = "-tracing" in exporter or exporter_options.pop("tracing", False)
if fake:
from onnx_diagnostic.export.shape_helper import make_fake_with_dynamic_dimensions

Expand All @@ -2370,6 +2376,7 @@ def call_torch_export_custom(
summary["export_exporter"] = exporter
summary["export_optimization"] = optimization or ""
summary["export_strict"] = strict
summary["export_tracing"] = tracing
summary["export_fake"] = fake
summary["export_args"] = string_type(args, with_shape=True)
summary["export_kwargs"] = string_type(kwargs, with_shape=True)
Expand All @@ -2392,6 +2399,7 @@ def call_torch_export_custom(
)
)
large_model = bool(exporter_options.pop("large_model", True))
exporter_options.pop("tracing", False)
return_optimize_report = bool(exporter_options.pop("return_optimize_report", True))
export_modules_as_functions = bool(
exporter_options.pop("export_modules_as_functions", False)
Expand All @@ -2405,6 +2413,7 @@ def call_torch_export_custom(
summary["export_external_threshold"] = str(external_threshold)

export_options = ExportOptions(
tracing=tracing,
strict=strict,
decomposition_table=decomposition_table,
save_ep=(
Expand Down
Loading