From 76f8477a0fa2795091fce055d923127f053888dc Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Tue, 31 Dec 2024 10:33:52 -0800 Subject: [PATCH 01/36] Fix CI tests - Bump ort and onnx versions - Remove dort tests as they are obsolete --- noxfile.py | 34 +++------------------------------- 1 file changed, 3 insertions(+), 31 deletions(-) diff --git a/noxfile.py b/noxfile.py index 1c1e39355..ba28476ef 100644 --- a/noxfile.py +++ b/noxfile.py @@ -30,8 +30,8 @@ "typing_extensions", "ml-dtypes", ) -ONNX = "onnx==1.16" -ONNX_RUNTIME = "onnxruntime==1.17.1" +ONNX = "onnx==1.17" +ONNX_RUNTIME = "onnxruntime==1.20.1" PYTORCH = "torch==2.3.1" TORCHVISON = "torchvision==0.18.1" TRANSFORMERS = "transformers==4.37.2" @@ -104,6 +104,7 @@ def test_ort_nightly(session): PYTORCH, TORCHVISON, ONNX, + TRANSFORMERS, *ONNX_RUNTIME_NIGHTLY_DEPENDENCIES, ) session.install("-r", "requirements/ci/requirements-ort-nightly.txt") @@ -132,32 +133,3 @@ def test_experimental_torchlib_tracing(session): *session.posargs, env={"TORCHLIB_EXPERIMENTAL_PREFER_TRACING": "1"}, ) - - -@nox.session(tags=["test-dort"]) -def test_dort(session): - """Test the conversion of a couple of models from transformers.""" - session.install( - *COMMON_TEST_DEPENDENCIES, - ) - torch_version, transformers_version = session.posargs - - if torch_version == "nightly": - session.install( - "--pre", - "torch", - "torchvision", - "torchaudio", - "--index-url", - "https://download.pytorch.org/whl/nightly/cpu", - ) - else: - session.install("torch", "torchvision", "torchaudio") - - session.install("torch", "torchvision", "torchaudio") - session.install(f"transformers=={transformers_version}") - session.install("onnxruntime-training==1.17.1") - - session.run("pip", "list") - session.run("pytest", "onnxscript") - session.run("pytest", "tests") From cd6f437623c8011e0f6ed9979d586f7775af814c Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Tue, 31 Dec 2024 10:35:11 -0800 Subject: [PATCH 02/36] req --- requirements-dev.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 2e719029e..103fab8ab 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,5 +1,5 @@ setuptools>=61.0.0 -numpy<2.0 +numpy onnx-weekly>=1.17.0.dev20240325 onnxruntime>=1.17.0 typing_extensions @@ -30,8 +30,8 @@ pytest-subtests pytest-xdist pytest!=7.1.0 pyyaml -torch>=2.1 -torchvision>=0.16.0 +torch>=2.3 +torchvision>=0.18.0 transformers>=4.37.2 # Lint From 4c8361651e97c30d2791949565e8c02fc90514e8 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Tue, 31 Dec 2024 10:37:40 -0800 Subject: [PATCH 03/36] Update main.yaml --- .github/workflows/main.yaml | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 292ab6ad3..9c9a4cf19 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -92,32 +92,6 @@ jobs: name: Error reports (${{ matrix.name }}-${{ matrix.os }}) path: error_reports - dort: - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest] - transformers: ["4.37.2", "4.41.2", "4.42.3"] - torch: ["release", "nightly"] - python_version: ["3.11"] - nox-tag: ["test-dort"] - name: - - dort - runs-on: ${{ matrix.os }} - steps: - - uses: actions/checkout@v4 - - name: Setup Python ${{ matrix.python_version }} - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python_version }} - - name: Install nox - run: python -m pip install nox - - name: Pull Test Data - run: git lfs pull - - run: | - nox -t ${{ matrix.nox-tag }} --forcecolor -- ${{ matrix.torch }} ${{ matrix.transformers }} - name: Run tests - build_docs: strategy: fail-fast: false From 6156557562c45931542986e1a5e0604ccde4860f Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Thu, 2 Jan 2025 11:16:47 -0800 Subject: [PATCH 04/36] Remove test --- .../tools/transformers_models/mistral_test.py | 29 ------------------- 1 file changed, 29 deletions(-) diff --git a/onnxscript/tools/transformers_models/mistral_test.py b/onnxscript/tools/transformers_models/mistral_test.py index 7498b9a15..fb06ecbd5 100644 --- a/onnxscript/tools/transformers_models/mistral_test.py +++ b/onnxscript/tools/transformers_models/mistral_test.py @@ -2,7 +2,6 @@ # Licensed under the MIT License. # pylint: disable=not-callable -import copy import sys import unittest @@ -18,7 +17,6 @@ from onnxscript._internal.version_utils import ( has_transformers, ignore_warnings, - onnxruntime_older_than, torch_older_than, transformers_older_than, ) @@ -113,33 +111,6 @@ def test_phi_export_cuda(self): results = sess.run(None, feeds) np.testing.assert_allclose(expected[0].detach().cpu().numpy(), results[0], atol=1e-5) - @unittest.skipIf(sys.platform == "win32", reason="not supported yet on Windows") - @unittest.skipIf(not has_transformers(), reason="transformers is missing") - @unittest.skipIf(onnxruntime_older_than("1.18.0"), reason="Trilu not imeplemnted") - @ignore_warnings(UserWarning) - def test_mistral_dort_static(self): - model, input_tensors_many, _ = ( - onnxscript.tools.transformers_models.mistral.get_mistral_model() - ) - input_tensors = input_tensors_many[0] - expected = model(*input_tensors) - - local_aot_ort = onnxscript.tools.training_helper.make_aot_ort(dynamic=False) - - compiled_model = torch.compile( - copy.deepcopy(model), - backend=local_aot_ort, - dynamic=False, - fullgraph=True, - ) - - results = compiled_model(*input_tensors) - torch.testing.assert_close(expected[0], results[0], atol=1e-5, rtol=1e-5) - - expected_gradients = onnxscript.tools.training_helper.train_loop(model, *input_tensors) - gradients = onnxscript.tools.training_helper.train_loop(compiled_model, *input_tensors) - torch.testing.assert_close(expected_gradients[0], gradients[0], atol=1e-5, rtol=1e-5) - if __name__ == "__main__": unittest.main(verbosity=2) From e428591104fee38201218a31db2a2e710d1e67a6 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Thu, 2 Jan 2025 11:18:32 -0800 Subject: [PATCH 05/36] nox --- noxfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noxfile.py b/noxfile.py index ba28476ef..343507544 100644 --- a/noxfile.py +++ b/noxfile.py @@ -31,7 +31,7 @@ "ml-dtypes", ) ONNX = "onnx==1.17" -ONNX_RUNTIME = "onnxruntime==1.20.1" +ONNX_RUNTIME = "onnxruntime==1.19.2" PYTORCH = "torch==2.3.1" TORCHVISON = "torchvision==0.18.1" TRANSFORMERS = "transformers==4.37.2" From dcb5694b8e77da3e1e6d5fec51faed9691c4c7ac Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Thu, 2 Jan 2025 11:22:03 -0800 Subject: [PATCH 06/36] eval --- onnxscript/evaluator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxscript/evaluator.py b/onnxscript/evaluator.py index ba235a5e4..3a6c47e29 100644 --- a/onnxscript/evaluator.py +++ b/onnxscript/evaluator.py @@ -388,7 +388,7 @@ def _numpy_to_onnxscript_value( """Converts an ORT encoding of an ONNX value into the encoding used by onnxscript.""" if isinstance(v, np.ndarray): return tensor.Tensor(v) - if np.issctype(type(v)): # noqa: NPY201 + if issubclass(v, np.generic): # Numpy scalar types that are not ndarray # https://numpy.org/doc/stable/reference/arrays.scalars.html return tensor.Tensor(np.array(v)) From fe82d4b117876e8217e75d41772322b2e8f345b0 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Thu, 2 Jan 2025 11:27:05 -0800 Subject: [PATCH 07/36] subclass --- onnxscript/evaluator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxscript/evaluator.py b/onnxscript/evaluator.py index 3a6c47e29..26a7158ca 100644 --- a/onnxscript/evaluator.py +++ b/onnxscript/evaluator.py @@ -388,7 +388,7 @@ def _numpy_to_onnxscript_value( """Converts an ORT encoding of an ONNX value into the encoding used by onnxscript.""" if isinstance(v, np.ndarray): return tensor.Tensor(v) - if issubclass(v, np.generic): + if issubclass(type(v), np.generic): # Numpy scalar types that are not ndarray # https://numpy.org/doc/stable/reference/arrays.scalars.html return tensor.Tensor(np.array(v)) From 8b697217499654f477d0618e5d8d52cab3b7a523 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Thu, 2 Jan 2025 11:44:10 -0800 Subject: [PATCH 08/36] nightly --- requirements/ci/requirements-ort-nightly.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/ci/requirements-ort-nightly.txt b/requirements/ci/requirements-ort-nightly.txt index 349b61034..8d7a8184b 100644 --- a/requirements/ci/requirements-ort-nightly.txt +++ b/requirements/ci/requirements-ort-nightly.txt @@ -1,3 +1,3 @@ # https://aiinfra.visualstudio.com/PublicPackages/_artifacts/feed/ORT-Nightly/PyPI/ort-nightly/overview --index-url=https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/ORT-Nightly/pypi/simple/ -ort-nightly==1.18.0.dev20240329005 +ort-nightly==1.20.0.dev20241015001 From ff7a0a290216e6ea25e0ed74b3df42226f93cc11 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Thu, 2 Jan 2025 12:01:17 -0800 Subject: [PATCH 09/36] eager --- onnxscript/evaluator.py | 4 +++- tests/models/sequences.py | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/onnxscript/evaluator.py b/onnxscript/evaluator.py index 26a7158ca..97551567b 100644 --- a/onnxscript/evaluator.py +++ b/onnxscript/evaluator.py @@ -387,7 +387,9 @@ def _numpy_to_onnxscript_value( ): """Converts an ORT encoding of an ONNX value into the encoding used by onnxscript.""" if isinstance(v, np.ndarray): - return tensor.Tensor(v) + # ORT may reuse buffers when the output numpy array is provided back as input. + # We need to make a copy to ensure that the tensor is not modified in-place. + return tensor.Tensor(v.copy()) if issubclass(type(v), np.generic): # Numpy scalar types that are not ndarray # https://numpy.org/doc/stable/reference/arrays.scalars.html diff --git a/tests/models/sequences.py b/tests/models/sequences.py index 4039add08..8a5079185 100644 --- a/tests/models/sequences.py +++ b/tests/models/sequences.py @@ -3,7 +3,6 @@ from onnxscript import script from onnxscript.onnx_opset import opset15 as op -from onnxscript.onnx_types import FLOAT @script() From 06d556ed829aeebcca0e9a6a83dec8e395cbf67b Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Thu, 2 Jan 2025 12:14:29 -0800 Subject: [PATCH 10/36] numpy --- onnxscript/optimizer/_constant_folding.py | 13 ++++--------- onnxscript/rewriter/broadcast_to_matmul.py | 2 +- requirements-dev.txt | 2 +- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/onnxscript/optimizer/_constant_folding.py b/onnxscript/optimizer/_constant_folding.py index 4053bb2a1..c9a61475f 100644 --- a/onnxscript/optimizer/_constant_folding.py +++ b/onnxscript/optimizer/_constant_folding.py @@ -242,10 +242,12 @@ def _get_numpy_value(val: ir.Value | None) -> np.ndarray | None: const_value = val.const_value if const_value is not None: try: - return const_value.numpy() + array = const_value.numpy() except FileNotFoundError: # External data is not available. return None + assert isinstance(array, np.ndarray) + return array return None @@ -255,14 +257,7 @@ def _get_bool_value(val: ir.Value | None) -> bool | None: value = _get_numpy_value(val) if value is None: return None - # TODO: cleanup following checks, which seem redundant. But need to also ensure - # the invariant when setting the value (and also use clearly defined representation - # types in evaluators, such a reference-evaluator). - if isinstance(value, bool): - return value - if isinstance(value, np.bool_): - return bool(value) - if isinstance(value, np.ndarray) and value.size == 1 and value.dtype == bool: + if value.size == 1 and value.dtype is bool: return value.item(0) return None diff --git a/onnxscript/rewriter/broadcast_to_matmul.py b/onnxscript/rewriter/broadcast_to_matmul.py index df216d977..4ce77c855 100644 --- a/onnxscript/rewriter/broadcast_to_matmul.py +++ b/onnxscript/rewriter/broadcast_to_matmul.py @@ -55,7 +55,7 @@ def check_if_not_need_reshape( return False input_a_shape = input_a_shape.numpy() # type: ignore[assignment] input_b_shape = input_b_shape.numpy() # type: ignore[assignment] - shape_c = shape_c_tensor.numpy().tolist() + shape_c = shape_c_tensor.numpy().tolist() # type: ignore[assignment] a_rank = len(input_a_shape) b_rank = len(input_b_shape) diff --git a/requirements-dev.txt b/requirements-dev.txt index 103fab8ab..466de2c71 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,5 +1,5 @@ setuptools>=61.0.0 -numpy +numpy<2.2 onnx-weekly>=1.17.0.dev20240325 onnxruntime>=1.17.0 typing_extensions From 6ba054b7568f72f9444ccc1f85cda6073035af4a Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Thu, 2 Jan 2025 12:20:05 -0800 Subject: [PATCH 11/36] lint --- .github/workflows/lint.yaml | 2 ++ requirements-dev.txt | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 7fe76a6de..084514a0e 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -56,6 +56,8 @@ jobs: python -m pip install --upgrade pip python -m pip install --upgrade setuptools python -m pip install -q -r requirements-dev.txt + # numpy 2.2 has some typing changes that break the mypy CI but it's otherwise fine + python -m pip install numpy<2.2 # Install packages python -m pip install -e . lintrunner init diff --git a/requirements-dev.txt b/requirements-dev.txt index 466de2c71..103fab8ab 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,5 +1,5 @@ setuptools>=61.0.0 -numpy<2.2 +numpy onnx-weekly>=1.17.0.dev20240325 onnxruntime>=1.17.0 typing_extensions From 37a981cfd26ac83e3478c86a1867418fd53fe12d Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Thu, 2 Jan 2025 12:29:12 -0800 Subject: [PATCH 12/36] onnxruntime --- requirements/ci/requirements-ort-nightly.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements/ci/requirements-ort-nightly.txt b/requirements/ci/requirements-ort-nightly.txt index 8d7a8184b..97baeabcf 100644 --- a/requirements/ci/requirements-ort-nightly.txt +++ b/requirements/ci/requirements-ort-nightly.txt @@ -1,3 +1,3 @@ -# https://aiinfra.visualstudio.com/PublicPackages/_artifacts/feed/ORT-Nightly/PyPI/ort-nightly/overview +# https://aiinfra.visualstudio.com/PublicPackages/_artifacts/feed/ORT-Nightly/PyPI/onnxruntime/overview --index-url=https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/ORT-Nightly/pypi/simple/ -ort-nightly==1.20.0.dev20241015001 +onnxruntime==1.21.0.dev20250101002 From 04ca4b5395db14ff53f6b6bdaa846459e6d075d0 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 09:24:00 -0800 Subject: [PATCH 13/36] constant folding --- onnxscript/optimizer/_constant_folding.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/onnxscript/optimizer/_constant_folding.py b/onnxscript/optimizer/_constant_folding.py index c9a61475f..c7b6720d4 100644 --- a/onnxscript/optimizer/_constant_folding.py +++ b/onnxscript/optimizer/_constant_folding.py @@ -711,10 +711,6 @@ def get_type(value: ir.Value) -> onnx.TypeProto | None: ) def new_constant(self, irvalue: ir.Value, value): - # TODO(rama): Why do we need the conversion below? - if isinstance(value, (int, float, np.ScalarType)): - value = np.array(value) - if not isinstance(value, np.ndarray): # ONNX does not have a way to represent non-tensor constants, eg. a sequence. # So, a constant-value of type sequence is not folded, but it can be used @@ -726,7 +722,9 @@ def new_constant(self, irvalue: ir.Value, value): ) return None - irvalue.const_value = _convenience.tensor(value) + tensor = ir.tensor(value) + tensor.name = irvalue.name + irvalue.const_value = tensor if value.nbytes > self._output_size_limit: logger.info( @@ -736,8 +734,6 @@ def new_constant(self, irvalue: ir.Value, value): ) return None - tensor = onnx.numpy_helper.from_array(value, irvalue.name) - logger.debug( "New constant for value %s dtype: %s shape: %s", irvalue.name, @@ -745,8 +741,13 @@ def new_constant(self, irvalue: ir.Value, value): value.shape, ) - attributes = _convenience.convert_attributes({"value": tensor}) - node = ir.Node("", "Constant", inputs=[], attributes=attributes, num_outputs=1) + node = ir.Node( + "", + "Constant", + inputs=[], + attributes=ir.convenience.convert_attributes({"value": tensor}), + num_outputs=1, + ) return node def process_node(self, node: ir.Node): @@ -832,7 +833,7 @@ def convert(av): def replace_node(self, node: ir.Node, replacement, root: ir.Graph | ir.Function): logger.debug("Replacing node: %s::%s %s", node.domain, node.op_type, node.name) - _convenience.replace_nodes_and_values( + ir.convenience.replace_nodes_and_values( root, node, [node], replacement.new_nodes, node.outputs, replacement.new_outputs ) From 75a355defa2188a5955e6c084beaa77227a0987b Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 09:27:06 -0800 Subject: [PATCH 14/36] bool --- onnxscript/optimizer/_constant_folding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxscript/optimizer/_constant_folding.py b/onnxscript/optimizer/_constant_folding.py index c7b6720d4..e49740143 100644 --- a/onnxscript/optimizer/_constant_folding.py +++ b/onnxscript/optimizer/_constant_folding.py @@ -257,7 +257,7 @@ def _get_bool_value(val: ir.Value | None) -> bool | None: value = _get_numpy_value(val) if value is None: return None - if value.size == 1 and value.dtype is bool: + if value.size == 1 and value.dtype == np.bool_: return value.item(0) return None From 6b69ec19a061ed99ad45c497075722cd8a0405d4 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 09:38:26 -0800 Subject: [PATCH 15/36] 1.21.0.dev20241108002 --- requirements/ci/requirements-ort-nightly.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/ci/requirements-ort-nightly.txt b/requirements/ci/requirements-ort-nightly.txt index 97baeabcf..100222d57 100644 --- a/requirements/ci/requirements-ort-nightly.txt +++ b/requirements/ci/requirements-ort-nightly.txt @@ -1,3 +1,3 @@ # https://aiinfra.visualstudio.com/PublicPackages/_artifacts/feed/ORT-Nightly/PyPI/onnxruntime/overview --index-url=https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/ORT-Nightly/pypi/simple/ -onnxruntime==1.21.0.dev20250101002 +onnxruntime==1.21.0.dev20241108002 From c39fcc8fae0f6bd62e74f5db95d0e34d8a11cf81 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 09:39:18 -0800 Subject: [PATCH 16/36] fixme --- .github/workflows/lint.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 084514a0e..565029c3c 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -56,7 +56,7 @@ jobs: python -m pip install --upgrade pip python -m pip install --upgrade setuptools python -m pip install -q -r requirements-dev.txt - # numpy 2.2 has some typing changes that break the mypy CI but it's otherwise fine + # FIXME: numpy 2.2 has some typing changes that break the mypy CI but it's otherwise fine python -m pip install numpy<2.2 # Install packages python -m pip install -e . From 5e040118ae27d818939515b33c66e2493abc11ec Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 09:40:02 -0800 Subject: [PATCH 17/36] lint --- onnxscript/optimizer/_constant_folding.py | 1 - 1 file changed, 1 deletion(-) diff --git a/onnxscript/optimizer/_constant_folding.py b/onnxscript/optimizer/_constant_folding.py index e49740143..661a5cd82 100644 --- a/onnxscript/optimizer/_constant_folding.py +++ b/onnxscript/optimizer/_constant_folding.py @@ -16,7 +16,6 @@ import onnx.reference.ops import onnxscript.ir as ir -import onnxscript.ir._convenience as _convenience import onnxscript.rewriter.pattern as orp import onnxscript.utils.utils as utils From 8dfa02e41dd94391dab73320b579e880a9117251 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 09:43:18 -0800 Subject: [PATCH 18/36] lint --- .github/workflows/lint.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 565029c3c..098547eea 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -51,11 +51,10 @@ jobs: python-version: "3.10" - name: Install ONNXScript run: | - # The code is from azure-pipelines.yml # Install dependencies python -m pip install --upgrade pip python -m pip install --upgrade setuptools - python -m pip install -q -r requirements-dev.txt + python -m pip install -r requirements-dev.txt # FIXME: numpy 2.2 has some typing changes that break the mypy CI but it's otherwise fine python -m pip install numpy<2.2 # Install packages From ab63cc9bd3a7e54bf0ec3d62733e406a52082204 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 09:48:16 -0800 Subject: [PATCH 19/36] np --- .github/workflows/lint.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 098547eea..f53f27483 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -56,7 +56,7 @@ jobs: python -m pip install --upgrade setuptools python -m pip install -r requirements-dev.txt # FIXME: numpy 2.2 has some typing changes that break the mypy CI but it's otherwise fine - python -m pip install numpy<2.2 + python -m pip install "numpy<2.2" # Install packages python -m pip install -e . lintrunner init From 1f2008acbb3ec408e6f27290a2450a37debde7bf Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 10:32:42 -0800 Subject: [PATCH 20/36] compare --- tests/function_libs/torch_lib/ops_test_data.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/function_libs/torch_lib/ops_test_data.py b/tests/function_libs/torch_lib/ops_test_data.py index 73060623c..6c5f892c2 100644 --- a/tests/function_libs/torch_lib/ops_test_data.py +++ b/tests/function_libs/torch_lib/ops_test_data.py @@ -706,11 +706,11 @@ def _where_input_wrangler( TorchLibOpInfo("bmm", core_ops.aten_bmm), TorchLibOpInfo("broadcast_to", core_ops.aten_broadcast_to), TorchLibOpInfo("cat", core_ops.aten_cat).skip( - matcher=lambda sample: sample.input[0].equal(torch.tensor([])), + matcher=lambda sample: sample.input[0].equal(torch.tensor([]).to(sample.input[0].device)), reason="fixme: ORT aborts with zero-dim tensors. https://github.com/microsoft/onnxruntime/issues/16619", ), TorchLibOpInfo("cat", core_ops.aten_cat_complex, complex=True).skip( - matcher=lambda sample: sample.input[0].equal(torch.tensor([])), + matcher=lambda sample: sample.input[0].equal(torch.tensor([]).to(sample.input[0].device)), reason="fixme: ORT aborts with zero-dim tensors. https://github.com/microsoft/onnxruntime/issues/16619", ), TorchLibOpInfo("ceil", core_ops.aten_ceil), @@ -750,11 +750,11 @@ def _where_input_wrangler( TorchLibOpInfo("clone", core_ops.aten_clone), TorchLibOpInfo("complex", core_ops.aten_complex), TorchLibOpInfo("concat", core_ops.aten_cat).skip( - matcher=lambda sample: sample.input[0].equal(torch.tensor([])), + matcher=lambda sample: sample.input[0].equal(torch.tensor([]).to(sample.input[0].device)), reason="fixme: ORT aborts with zero-dim tensors. https://github.com/microsoft/onnxruntime/issues/16619", ), TorchLibOpInfo("concatenate", core_ops.aten_cat).skip( - matcher=lambda sample: sample.input[0].equal(torch.tensor([])), + matcher=lambda sample: sample.input[0].equal(torch.tensor([]).to(sample.input[0].device)), reason="fixme: ORT aborts with zero-dim tensors. https://github.com/microsoft/onnxruntime/issues/16619", ), TorchLibOpInfo("conj", core_ops.aten_conj), From 8b1702d00a7a6f7071fb29dfd42b60fc0c3a8ec8 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 10:32:58 -0800 Subject: [PATCH 21/36] lint --- tests/function_libs/torch_lib/ops_test_data.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/function_libs/torch_lib/ops_test_data.py b/tests/function_libs/torch_lib/ops_test_data.py index 6c5f892c2..7b6cd1b42 100644 --- a/tests/function_libs/torch_lib/ops_test_data.py +++ b/tests/function_libs/torch_lib/ops_test_data.py @@ -706,11 +706,15 @@ def _where_input_wrangler( TorchLibOpInfo("bmm", core_ops.aten_bmm), TorchLibOpInfo("broadcast_to", core_ops.aten_broadcast_to), TorchLibOpInfo("cat", core_ops.aten_cat).skip( - matcher=lambda sample: sample.input[0].equal(torch.tensor([]).to(sample.input[0].device)), + matcher=lambda sample: sample.input[0].equal( + torch.tensor([]).to(sample.input[0].device) + ), reason="fixme: ORT aborts with zero-dim tensors. https://github.com/microsoft/onnxruntime/issues/16619", ), TorchLibOpInfo("cat", core_ops.aten_cat_complex, complex=True).skip( - matcher=lambda sample: sample.input[0].equal(torch.tensor([]).to(sample.input[0].device)), + matcher=lambda sample: sample.input[0].equal( + torch.tensor([]).to(sample.input[0].device) + ), reason="fixme: ORT aborts with zero-dim tensors. https://github.com/microsoft/onnxruntime/issues/16619", ), TorchLibOpInfo("ceil", core_ops.aten_ceil), @@ -750,11 +754,15 @@ def _where_input_wrangler( TorchLibOpInfo("clone", core_ops.aten_clone), TorchLibOpInfo("complex", core_ops.aten_complex), TorchLibOpInfo("concat", core_ops.aten_cat).skip( - matcher=lambda sample: sample.input[0].equal(torch.tensor([]).to(sample.input[0].device)), + matcher=lambda sample: sample.input[0].equal( + torch.tensor([]).to(sample.input[0].device) + ), reason="fixme: ORT aborts with zero-dim tensors. https://github.com/microsoft/onnxruntime/issues/16619", ), TorchLibOpInfo("concatenate", core_ops.aten_cat).skip( - matcher=lambda sample: sample.input[0].equal(torch.tensor([]).to(sample.input[0].device)), + matcher=lambda sample: sample.input[0].equal( + torch.tensor([]).to(sample.input[0].device) + ), reason="fixme: ORT aborts with zero-dim tensors. https://github.com/microsoft/onnxruntime/issues/16619", ), TorchLibOpInfo("conj", core_ops.aten_conj), From 39c82d6aa5c90f4d46873425fbd942245d5e9415 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 10:23:43 -0800 Subject: [PATCH 22/36] 16492 --- .../function_libs/torch_lib/ops_test_data.py | 66 ++----------------- 1 file changed, 7 insertions(+), 59 deletions(-) diff --git a/tests/function_libs/torch_lib/ops_test_data.py b/tests/function_libs/torch_lib/ops_test_data.py index 7b6cd1b42..a7ef075b1 100644 --- a/tests/function_libs/torch_lib/ops_test_data.py +++ b/tests/function_libs/torch_lib/ops_test_data.py @@ -566,19 +566,11 @@ def _where_input_wrangler( "amax", core_ops.aten_amax, input_wrangler=_amin_amax_input_wrangler, - ).skip( - matcher=lambda sample: len(sample.input.shape) == 0, - enabled_if=version_utils.onnxruntime_older_than("1.16"), - reason="fixme (core dump): ORT aborts on scalar inputs to ReduceMax-18. https://github.com/microsoft/onnxruntime/issues/16492", ), TorchLibOpInfo( "amin", core_ops.aten_amin, input_wrangler=_amin_amax_input_wrangler, - ).skip( - matcher=lambda sample: len(sample.input.shape) == 0, - enabled_if=version_utils.onnxruntime_older_than("1.16"), - reason="fixme (core dump): ORT aborts on scalar inputs to ReduceMin-18. https://github.com/microsoft/onnxruntime/issues/16492", ), TorchLibOpInfo( "any", @@ -732,21 +724,11 @@ def _where_input_wrangler( reason="fixme: ORT does not implement SplitToSequence for bool inputs: https://github.com/microsoft/onnxruntime/issues/16905", ), TorchLibOpInfo("clamp_max", core_ops.aten_clamp_max) - .skip( - matcher=lambda sample: len(sample.input.shape) == 0, - enabled_if=version_utils.onnxruntime_older_than("1.16"), - reason="fixme (core dump): ORT aborts on scalar inputs to Reduce*-18. https://github.com/microsoft/onnxruntime/issues/16492", - ) .skip( reason="Size 0 inputs are not handled by design", matcher=lambda sample: sample.input.numel() == 0, ), TorchLibOpInfo("clamp_min", core_ops.aten_clamp_min) - .skip( - matcher=lambda sample: len(sample.input.shape) == 0, - enabled_if=version_utils.onnxruntime_older_than("1.16"), - reason="fixme (core dump): ORT aborts on scalar inputs to Reduce*-18. https://github.com/microsoft/onnxruntime/issues/16492", - ) .skip( reason="Size 0 inputs are not handled by design", matcher=lambda sample: sample.input.numel() == 0, @@ -985,11 +967,7 @@ def _where_input_wrangler( matcher=lambda sample: torch.numel(sample.input) == 0, reason="values of matmul of [m, 0] and [0, n] matrices are undefined", ), - TorchLibOpInfo("maximum", core_ops.aten_maximum).skip( - matcher=lambda sample: len(sample.input.shape) == 0, - enabled_if=version_utils.onnxruntime_older_than("1.16"), - reason="fixme (core dump): ORT aborts on scalar inputs to Reduce*-18. https://github.com/microsoft/onnxruntime/issues/16492", - ), + TorchLibOpInfo("maximum", core_ops.aten_maximum), TorchLibOpInfo("maximum_bool", core_ops.aten_maximum_bool), TorchLibOpInfo( "mean", @@ -1010,12 +988,6 @@ def _where_input_wrangler( TorchLibOpInfo("mH", core_ops.aten_mH), TorchLibOpInfo("mH", core_ops.aten_mH_complex, complex=True), TorchLibOpInfo("min_dim", core_ops.aten_min_dim) - .skip( - variant_name="reduction_with_dim", - matcher=lambda sample: len(sample.input.shape) == 0, - enabled_if=version_utils.onnxruntime_older_than("1.16"), - reason="fixme (core dump): ORT aborts on scalar inputs to Reduce*-18. https://github.com/microsoft/onnxruntime/issues/16492", - ) .xfail( variant_name="reduction_with_dim", dtypes=(torch.int64,), @@ -1039,11 +1011,7 @@ def _where_input_wrangler( matcher=lambda sample: len(sample.args) > 0, reason="this ATen overload only supports one tensor as input by design", ), - TorchLibOpInfo("minimum", core_ops.aten_minimum).skip( - matcher=lambda sample: len(sample.input.shape) == 0, - enabled_if=version_utils.onnxruntime_older_than("1.16"), - reason="fixme (core dump): ORT aborts on scalar inputs to Reduce*-18. https://github.com/microsoft/onnxruntime/issues/16492", - ), + TorchLibOpInfo("minimum", core_ops.aten_minimum), TorchLibOpInfo("minimum_bool", core_ops.aten_minimum_bool), TorchLibOpInfo("mm", core_ops.aten_mm).skip( matcher=lambda sample: torch.numel(sample.input) == 0, @@ -1524,12 +1492,12 @@ def _where_input_wrangler( reason="fixme: result mismatch. https://github.com/microsoft/onnxscript/issues/853", ), TorchLibOpInfo("tril", core_ops.aten_tril).xfail( - dtypes=(torch.int32, torch.bool), - reason="fixme: ORT does not have an implementation of Trilu for int32 or bool.", + dtypes=(torch.int32,), + reason="fixme: ORT does not have an implementation of Trilu for int32.", ), TorchLibOpInfo("triu", core_ops.aten_triu).xfail( - dtypes=(torch.int32, torch.bool), - reason="fixme: ORT does not have an implementation of Trilu for int32 or bool.", + dtypes=(torch.int32,), + reason="fixme: ORT does not have an implementation of Trilu for int32.", ), TorchLibOpInfo("trunc", core_ops.aten_trunc), TorchLibOpInfo( @@ -1618,21 +1586,11 @@ def _where_input_wrangler( dtypes=(torch.float16, torch.int64, torch.int32), ), TorchLibOpInfo("argmax", core_ops.aten_argmax) - .skip( - matcher=lambda sample: len(sample.input.shape) == 0, - enabled_if=version_utils.onnxruntime_older_than("1.16"), - reason="fixme (core dump): ORT aborts on scalar inputs to Reduce*-18. https://github.com/microsoft/onnxruntime/issues/16492", - ) .xfail( dtypes=(torch.int64,), reason="fixme: ORT did not implement ArgMax for int64. https://github.com/microsoft/onnxruntime/issues/16654", ), TorchLibOpInfo("argmin", core_ops.aten_argmin) - .skip( - matcher=lambda sample: len(sample.input.shape) == 0, - enabled_if=version_utils.onnxruntime_older_than("1.16"), - reason="fixme (core dump): ORT aborts on scalar inputs to Reduce*-18. https://github.com/microsoft/onnxruntime/issues/16492", - ) .xfail( dtypes=(torch.int64,), reason="fixme: ORT did not implement ArgMin for int64. https://github.com/microsoft/onnxruntime/issues/16654", @@ -1644,11 +1602,7 @@ def _where_input_wrangler( variant_name="partial_views", reason="ONNX doesn't have partial view for tensor", ), - TorchLibOpInfo("clamp", core_ops.aten_clamp).skip( - matcher=lambda sample: len(sample.input.shape) == 0, - enabled_if=version_utils.onnxruntime_older_than("1.16"), - reason="fixme (core dump): ORT aborts on scalar inputs to Reduce*-18. https://github.com/microsoft/onnxruntime/issues/16492", - ), + TorchLibOpInfo("clamp", core_ops.aten_clamp), TorchLibOpInfo( "ops.aten.col2im", nn_ops.aten_col2im, @@ -1704,12 +1658,6 @@ def _where_input_wrangler( ), TorchLibOpInfo("logit", core_ops.aten_logit, tolerance={torch.float16: (1e-1, 7e-4)}), TorchLibOpInfo("max_dim", core_ops.aten_max_dim) - .skip( - variant_name="reduction_with_dim", - matcher=lambda sample: len(sample.input.shape) == 0, - enabled_if=version_utils.onnxruntime_older_than("1.16"), - reason="fixme (core dump): ORT aborts on scalar inputs to Reduce*-18. https://github.com/microsoft/onnxruntime/issues/16492", - ) .xfail( variant_name="reduction_with_dim", dtypes=(torch.int64,), From 19915465f7c2e114e33ece2582207974af0adc64 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 10:56:13 -0800 Subject: [PATCH 23/36] update --- tests/function_libs/torch_lib/ops_test_data.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/function_libs/torch_lib/ops_test_data.py b/tests/function_libs/torch_lib/ops_test_data.py index a7ef075b1..4ed8abe73 100644 --- a/tests/function_libs/torch_lib/ops_test_data.py +++ b/tests/function_libs/torch_lib/ops_test_data.py @@ -856,6 +856,8 @@ def _where_input_wrangler( ).skip( matcher=lambda sample: sample.args[0][0].dtype != torch.bool, reason="this Aten overload only supports tensor(bool) as indices", + ).skip( + reason="FIXME: https://github.com/microsoft/onnxscript/issues/1749" ), TorchLibOpInfo( "index_put", @@ -1628,6 +1630,9 @@ def _where_input_wrangler( # Torch implemented this using the cubic convolution algorithm with alhpa=-0.75, might be different than ORT matcher=lambda sample: sample.args[1] == 2, reason="fixme: 'bicubic' mode in ORT implemented differently with Torch", + ).skip( + dtypes=(torch.float16,), + reason="fixme: Accuracy is not high enough", ), TorchLibOpInfo( "nn.functional.group_norm", From ddd683dc76ee4c1fe4a1f88f85ab53da554daec0 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 11:00:01 -0800 Subject: [PATCH 24/36] update --- .../function_libs/torch_lib/ops_test_data.py | 29 ++++--------------- 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/tests/function_libs/torch_lib/ops_test_data.py b/tests/function_libs/torch_lib/ops_test_data.py index 4ed8abe73..f7c3dd066 100644 --- a/tests/function_libs/torch_lib/ops_test_data.py +++ b/tests/function_libs/torch_lib/ops_test_data.py @@ -548,15 +548,8 @@ def _where_input_wrangler( "decomposed", dtypes=(torch.int16, torch.int32, torch.int64), reason="ONNX Runtime does not support int inputs to Gemm", - ) - .xfail( - "decomposed", - matcher=lambda sample: torch.numel(sample.input) == 0 - or torch.numel(sample.args[0]) == 0 - or torch.numel(sample.args[1]) == 0, - reason="ONNX Runtime does not support zero sized inputs", ), - TorchLibOpInfo("addmv", core_ops.aten_addmv, tolerance={torch.float16: (1e-3, 1e-2)}), + TorchLibOpInfo("addmv", core_ops.aten_addmv, tolerance={torch.float16: (2e-3, 2e-2)}), TorchLibOpInfo( "addr", core_ops.aten_addr, @@ -1079,7 +1072,7 @@ def _where_input_wrangler( TorchLibOpInfo( "ops.aten.embedding_bag", core_ops.aten_embedding_bag, - tolerance={torch.float16: (1e-2, 1e-2)}, + tolerance={torch.float16: (1e-2, 5e-2)}, compare_shape_only_for_output=(1, 2, 3), ), TorchLibOpInfo( @@ -1587,16 +1580,8 @@ def _where_input_wrangler( reason="dtype needs to be specified for non-float tensors", dtypes=(torch.float16, torch.int64, torch.int32), ), - TorchLibOpInfo("argmax", core_ops.aten_argmax) - .xfail( - dtypes=(torch.int64,), - reason="fixme: ORT did not implement ArgMax for int64. https://github.com/microsoft/onnxruntime/issues/16654", - ), - TorchLibOpInfo("argmin", core_ops.aten_argmin) - .xfail( - dtypes=(torch.int64,), - reason="fixme: ORT did not implement ArgMin for int64. https://github.com/microsoft/onnxruntime/issues/16654", - ), + TorchLibOpInfo("argmax", core_ops.aten_argmax), + TorchLibOpInfo("argmin", core_ops.aten_argmin), TorchLibOpInfo( "as_strided", core_ops.aten_as_strided, @@ -1647,6 +1632,7 @@ def _where_input_wrangler( "nn.functional.grid_sample", core_ops.aten_grid_sampler, input_wrangler=_grid_sample_input_wrangler, + tolerance={torch.float16: (9e-3, 2e-3)}, ).skip( # Torch implemented this using the cubic convolution algorithm with alhpa=-0.75, might be different than ORT matcher=lambda sample: sample.kwargs.get("mode") == "bicubic" @@ -1663,11 +1649,6 @@ def _where_input_wrangler( ), TorchLibOpInfo("logit", core_ops.aten_logit, tolerance={torch.float16: (1e-1, 7e-4)}), TorchLibOpInfo("max_dim", core_ops.aten_max_dim) - .xfail( - variant_name="reduction_with_dim", - dtypes=(torch.int64,), - reason="fixme: ORT did not implement Max for int64. https://github.com/microsoft/onnxruntime/issues/16654", - ) .xfail( variant_name="reduction_with_dim", reason="fixme: ORT Graph attribute inferencing failed https://github.com/onnx/onnx/issues/4986", From 59c2f2f3624f482dc4db3b1f4ecb42d89e5d4ada Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 19:07:18 -0800 Subject: [PATCH 25/36] topk --- onnxscript/function_libs/torch_lib/ops/core.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/onnxscript/function_libs/torch_lib/ops/core.py b/onnxscript/function_libs/torch_lib/ops/core.py index 9de7b170f..63eaeb46c 100644 --- a/onnxscript/function_libs/torch_lib/ops/core.py +++ b/onnxscript/function_libs/torch_lib/ops/core.py @@ -8272,20 +8272,20 @@ def aten_to_sparse_csr(self: TensorType) -> TensorType: raise NotImplementedError() -@torch_op("aten::topk", traceable=True) +@torch_op("aten::topk", trace_only=True) def aten_topk( - self: TReal, k: INT64, dim: int = -1, largest: bool = True, sorted: bool = True + self: TReal, k: int, dim: int = -1, largest: bool = True, sorted: bool = True ) -> Tuple[TReal, INT64]: """topk(Tensor self, int k, int dim=-1, bool largest=True, bool sorted=True) -> (Tensor values, Tensor indices)""" self_is_scalar = IsScalar(self) if self_is_scalar: - self = op.Unsqueeze(self, op.Constant(value_ints=[0])) - k = op.Reshape(op.Cast(k, to=INT64.dtype), op.Constant(value_ints=[1])) + self = op.Unsqueeze(self, [0]) + k = op.Constant(value_ints=[k]) values, indices = op.TopK(self, k, axis=dim, largest=largest, sorted=sorted) if self_is_scalar: - values = op.Squeeze(values, op.Constant(value_ints=[0])) - indices = op.Squeeze(indices, op.Constant(value_ints=[0])) + values = op.Squeeze(values, [0]) + indices = op.Squeeze(indices, [0]) return values, indices From 811e74c01d73eb70a5bf3edb275e1ac5de0a577a Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 19:16:44 -0800 Subject: [PATCH 26/36] more fixes --- .../function_libs/torch_lib/ops_test_data.py | 61 ++++++++----------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/tests/function_libs/torch_lib/ops_test_data.py b/tests/function_libs/torch_lib/ops_test_data.py index f7c3dd066..8a1e096bc 100644 --- a/tests/function_libs/torch_lib/ops_test_data.py +++ b/tests/function_libs/torch_lib/ops_test_data.py @@ -548,6 +548,13 @@ def _where_input_wrangler( "decomposed", dtypes=(torch.int16, torch.int32, torch.int64), reason="ONNX Runtime does not support int inputs to Gemm", + ) + .skip( + "decomposed", + matcher=lambda sample: torch.numel(sample.input) == 0 + or torch.numel(sample.args[0]) == 0 + or torch.numel(sample.args[1]) == 0, + reason="zero sized inputs cannot be compared", ), TorchLibOpInfo("addmv", core_ops.aten_addmv, tolerance={torch.float16: (2e-3, 2e-2)}), TorchLibOpInfo( @@ -716,13 +723,11 @@ def _where_input_wrangler( dtypes=(torch.bool,), reason="fixme: ORT does not implement SplitToSequence for bool inputs: https://github.com/microsoft/onnxruntime/issues/16905", ), - TorchLibOpInfo("clamp_max", core_ops.aten_clamp_max) - .skip( + TorchLibOpInfo("clamp_max", core_ops.aten_clamp_max).skip( reason="Size 0 inputs are not handled by design", matcher=lambda sample: sample.input.numel() == 0, ), - TorchLibOpInfo("clamp_min", core_ops.aten_clamp_min) - .skip( + TorchLibOpInfo("clamp_min", core_ops.aten_clamp_min).skip( reason="Size 0 inputs are not handled by design", matcher=lambda sample: sample.input.numel() == 0, ), @@ -767,12 +772,6 @@ def _where_input_wrangler( dtypes=(torch.float16,), # Numbers match sometimes but not other times reason="fixme: off-by-one. https://github.com/microsoft/onnxscript/issues/990", - ) - .xfail( - variant_name="floor_rounding", - dtypes=(torch.float16,), - test_class_name="TestOutputConsistencyEager", - reason="fixme: off-by-one and inverted inf. https://github.com/microsoft/onnxscript/issues/989", ), TorchLibOpInfo("div_mode_int", core_ops.aten_div_mode_int).skip( variant_name="no_rounding_mode", @@ -803,7 +802,7 @@ def _where_input_wrangler( TorchLibOpInfo("expand_as", core_ops.aten_expand_as), TorchLibOpInfo("erf", special_ops.aten_special_erf), TorchLibOpInfo( - "erfc", special_ops.aten_special_erfc, tolerance={torch.float16: (1e-2, 2e-4)} + "erfc", special_ops.aten_special_erfc, tolerance={torch.float16: (5e-1, 2e-4)} ), TorchLibOpInfo( "expm1", special_ops.aten_special_expm1, tolerance={torch.float16: (1e-2, 2e-4)} @@ -846,12 +845,12 @@ def _where_input_wrangler( TorchLibOpInfo( "index_put_bool", core_ops.aten_index_put_bool, - ).skip( + ) + .skip( matcher=lambda sample: sample.args[0][0].dtype != torch.bool, reason="this Aten overload only supports tensor(bool) as indices", - ).skip( - reason="FIXME: https://github.com/microsoft/onnxscript/issues/1749" - ), + ) + .skip(reason="FIXME: https://github.com/microsoft/onnxscript/issues/1749"), TorchLibOpInfo( "index_put", core_ops.aten_index_put, @@ -861,7 +860,6 @@ def _where_input_wrangler( reason="this Aten overload only supports tensor(int) as indices", ) .xfail( - enabled_if=version_utils.onnxruntime_older_than("1.19"), dtypes=(torch.float16,), matcher=lambda sample: sample.kwargs.get("accumulate") is True, reason="fixme: ORT only supports float32 when accumulate is True: MLFloat16 data type is not supported with ScatterND when reduction is 'add'", @@ -982,19 +980,7 @@ def _where_input_wrangler( ), TorchLibOpInfo("mH", core_ops.aten_mH), TorchLibOpInfo("mH", core_ops.aten_mH_complex, complex=True), - TorchLibOpInfo("min_dim", core_ops.aten_min_dim) - .xfail( - variant_name="reduction_with_dim", - dtypes=(torch.int64,), - reason="fixme: ORT did not implement Min for int64. https://github.com/microsoft/onnxruntime/issues/16654", - ) - .xfail( - variant_name="reduction_with_dim", - reason="fixme: ORT Graph attribute inferencing failed https://github.com/onnx/onnx/issues/4986", - test_class_name="TestOutputConsistencyFullGraph", - enabled_if=not _flags.EXPERIMENTAL_PREFER_TRACING, - ) - .xfail( + TorchLibOpInfo("min_dim", core_ops.aten_min_dim).xfail( matcher=lambda sample: len(sample.args) == 0 or (len(sample.args) > 0 and not isinstance(sample.args[0], int)), reason="this ATen overload only support one tensor as input and another int as args", @@ -1460,7 +1446,7 @@ def _where_input_wrangler( ), TorchLibOpInfo("stack", core_ops.aten_stack), TorchLibOpInfo("stack", core_ops.aten_stack_complex, complex=True), - TorchLibOpInfo("sub", core_ops.aten_sub), + TorchLibOpInfo("sub", core_ops.aten_sub, tolerance={torch.float16: (2e-3, 1e-3)}), TorchLibOpInfo("sub", core_ops.aten_sub_complex, complex=True), # TorchLibOpInfo("sym_size", core_ops.aten_sym_size), # no test case in OPS_DB TorchLibOpInfo( @@ -1481,10 +1467,15 @@ def _where_input_wrangler( or not sample.input.shape, reason="fixme: Logic not implemented for size 0 inputs in op.Reshape", ), - TorchLibOpInfo("topk", core_ops.aten_topk).xfail( + TorchLibOpInfo("topk", core_ops.aten_topk) + .xfail( dtypes=(torch.int64, torch.int32), enabled_if=not ops_test_common.IS_WINDOWS, reason="fixme: result mismatch. https://github.com/microsoft/onnxscript/issues/853", + ) + .skip( + dtypes=(torch.float16,), + reason="fixme: result mismatch. https://github.com/microsoft/onnxscript/issues/853", ), TorchLibOpInfo("tril", core_ops.aten_tril).xfail( dtypes=(torch.int32,), @@ -1611,11 +1602,13 @@ def _where_input_wrangler( TorchLibOpInfo( "grid_sampler_2d", core_ops.aten_grid_sampler_2d, - ).skip( + ) + .skip( # Torch implemented this using the cubic convolution algorithm with alhpa=-0.75, might be different than ORT matcher=lambda sample: sample.args[1] == 2, reason="fixme: 'bicubic' mode in ORT implemented differently with Torch", - ).skip( + ) + .skip( dtypes=(torch.float16,), reason="fixme: Accuracy is not high enough", ), @@ -1632,7 +1625,7 @@ def _where_input_wrangler( "nn.functional.grid_sample", core_ops.aten_grid_sampler, input_wrangler=_grid_sample_input_wrangler, - tolerance={torch.float16: (9e-3, 2e-3)}, + tolerance={torch.float16: (8e-2, 2e-3)}, ).skip( # Torch implemented this using the cubic convolution algorithm with alhpa=-0.75, might be different than ORT matcher=lambda sample: sample.kwargs.get("mode") == "bicubic" From 82eecf3f8c6fdece1ebfe4828217f21fd138602c Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 19:25:08 -0800 Subject: [PATCH 27/36] topk --- onnxscript/function_libs/torch_lib/ops/core.py | 7 +------ tests/function_libs/torch_lib/ops_test_data.py | 4 ++++ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/onnxscript/function_libs/torch_lib/ops/core.py b/onnxscript/function_libs/torch_lib/ops/core.py index 63eaeb46c..7ff0fdcc3 100644 --- a/onnxscript/function_libs/torch_lib/ops/core.py +++ b/onnxscript/function_libs/torch_lib/ops/core.py @@ -8278,14 +8278,9 @@ def aten_topk( ) -> Tuple[TReal, INT64]: """topk(Tensor self, int k, int dim=-1, bool largest=True, bool sorted=True) -> (Tensor values, Tensor indices)""" - self_is_scalar = IsScalar(self) - if self_is_scalar: - self = op.Unsqueeze(self, [0]) + # We do not handle scalar inputs for topk k = op.Constant(value_ints=[k]) values, indices = op.TopK(self, k, axis=dim, largest=largest, sorted=sorted) - if self_is_scalar: - values = op.Squeeze(values, [0]) - indices = op.Squeeze(indices, [0]) return values, indices diff --git a/tests/function_libs/torch_lib/ops_test_data.py b/tests/function_libs/torch_lib/ops_test_data.py index 8a1e096bc..2144596f8 100644 --- a/tests/function_libs/torch_lib/ops_test_data.py +++ b/tests/function_libs/torch_lib/ops_test_data.py @@ -1476,6 +1476,10 @@ def _where_input_wrangler( .skip( dtypes=(torch.float16,), reason="fixme: result mismatch. https://github.com/microsoft/onnxscript/issues/853", + ) + .skip( + matcher=lambda sample: len(sample.input.shape) == 0 or sample.input.numel() == 0, + reason="scalar inputs or empty inputs are not handled" ), TorchLibOpInfo("tril", core_ops.aten_tril).xfail( dtypes=(torch.int32,), From db51048ca5080646f1f2985e826c1b2bceb9b038 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 19:35:22 -0800 Subject: [PATCH 28/36] topk --- onnxscript/function_libs/torch_lib/ops/core.py | 3 +-- tests/function_libs/torch_lib/ops_test_data.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/onnxscript/function_libs/torch_lib/ops/core.py b/onnxscript/function_libs/torch_lib/ops/core.py index 85f7fa140..584c178d5 100644 --- a/onnxscript/function_libs/torch_lib/ops/core.py +++ b/onnxscript/function_libs/torch_lib/ops/core.py @@ -8279,8 +8279,7 @@ def aten_topk( """topk(Tensor self, int k, int dim=-1, bool largest=True, bool sorted=True) -> (Tensor values, Tensor indices)""" # We do not handle scalar inputs for topk - k = op.Constant(value_ints=[k]) - values, indices = op.TopK(self, k, axis=dim, largest=largest, sorted=sorted) + values, indices = op.TopK(self, [k], axis=dim, largest=largest, sorted=sorted) return values, indices diff --git a/tests/function_libs/torch_lib/ops_test_data.py b/tests/function_libs/torch_lib/ops_test_data.py index 2144596f8..70bcf7d33 100644 --- a/tests/function_libs/torch_lib/ops_test_data.py +++ b/tests/function_libs/torch_lib/ops_test_data.py @@ -1479,7 +1479,7 @@ def _where_input_wrangler( ) .skip( matcher=lambda sample: len(sample.input.shape) == 0 or sample.input.numel() == 0, - reason="scalar inputs or empty inputs are not handled" + reason="scalar inputs or empty inputs are not handled", ), TorchLibOpInfo("tril", core_ops.aten_tril).xfail( dtypes=(torch.int32,), From 9724ad9161a2a639f6f2568d2d5cdb66940181ab Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 19:39:40 -0800 Subject: [PATCH 29/36] norm --- tests/function_libs/torch_lib/ops_test_data.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/function_libs/torch_lib/ops_test_data.py b/tests/function_libs/torch_lib/ops_test_data.py index 70bcf7d33..07164d594 100644 --- a/tests/function_libs/torch_lib/ops_test_data.py +++ b/tests/function_libs/torch_lib/ops_test_data.py @@ -772,6 +772,12 @@ def _where_input_wrangler( dtypes=(torch.float16,), # Numbers match sometimes but not other times reason="fixme: off-by-one. https://github.com/microsoft/onnxscript/issues/990", + ) + .skip( + variant_name="floor_rounding", + dtypes=(torch.float16,), + test_class_name="TestOutputConsistencyEager", + reason="fixme: off-by-one and inverted inf. https://github.com/microsoft/onnxscript/issues/989", ), TorchLibOpInfo("div_mode_int", core_ops.aten_div_mode_int).skip( variant_name="no_rounding_mode", @@ -1760,7 +1766,7 @@ def _where_input_wrangler( .xfail( dtypes=(torch.float32,), matcher=lambda sample: len(sample.input.shape) == 1, - enabled_if=ops_test_common.IS_MACOS and version_utils.onnxruntime_older_than("1.18"), + enabled_if=ops_test_common.IS_MACOS, reason="fixme: result mismatch. https://github.com/microsoft/onnxruntime/issues/20676", ) .skip( From 61b9f18fdad6eda29379822ad214429ac469911b Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 19:41:29 -0800 Subject: [PATCH 30/36] ort --- noxfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noxfile.py b/noxfile.py index 343507544..ba28476ef 100644 --- a/noxfile.py +++ b/noxfile.py @@ -31,7 +31,7 @@ "ml-dtypes", ) ONNX = "onnx==1.17" -ONNX_RUNTIME = "onnxruntime==1.19.2" +ONNX_RUNTIME = "onnxruntime==1.20.1" PYTORCH = "torch==2.3.1" TORCHVISON = "torchvision==0.18.1" TRANSFORMERS = "transformers==4.37.2" From eea9fad5b6c02ccbbf6014bd6a61cf42466415d0 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 19:48:46 -0800 Subject: [PATCH 31/36] test --- .github/workflows/main.yaml | 17 +++++------------ pyproject.toml | 1 + 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 9c9a4cf19..910c282fc 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -26,27 +26,23 @@ jobs: matrix: os: [ubuntu-latest, windows-latest, macos-latest] name: - - py312-torch-nightly + - py312 - py311 - py311-torch-nightly - py311-onnx-weekly - py311-ort-nightly - py311-experimental-torchlib-tracing - py310 - - py39 include: + - name: py312 + python-version: "3.12" + nox-tag: test build - name: py311 python-version: "3.11" - nox-tag: test build + nox-tag: test - name: py310 python-version: "3.10" nox-tag: test - - name: py39 - python-version: "3.9" - nox-tag: test - - name: py312-torch-nightly - python-version: "3.12" - nox-tag: test-torch-nightly - name: py311-torch-nightly python-version: "3.11" nox-tag: test-torch-nightly @@ -56,9 +52,6 @@ jobs: - name: py311-ort-nightly python-version: "3.11" nox-tag: test-ort-nightly - - name: py311-experimental-torchlib-tracing - python-version: "3.11" - nox-tag: test-experimental-torchlib-tracing runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 diff --git a/pyproject.toml b/pyproject.toml index e96c2ddc3..4771d85b9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ classifiers = [ "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", "License :: OSI Approved :: MIT License", ] dependencies = ["numpy", "onnx>=1.16", "typing_extensions", "ml_dtypes", "packaging"] From a29db041ba8f3f6a77f1b115a873110da181d3bb Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 19:58:16 -0800 Subject: [PATCH 32/36] dort --- .../tools/transformers_models/llama_test.py | 28 ---------------- .../tools/transformers_models/phi3_test.py | 32 ------------------- 2 files changed, 60 deletions(-) diff --git a/onnxscript/tools/transformers_models/llama_test.py b/onnxscript/tools/transformers_models/llama_test.py index ea4844476..7f8d42050 100644 --- a/onnxscript/tools/transformers_models/llama_test.py +++ b/onnxscript/tools/transformers_models/llama_test.py @@ -2,7 +2,6 @@ # Licensed under the MIT License. # pylint: disable=not-callable -import copy import sys import unittest @@ -111,33 +110,6 @@ def test_llama_export_cuda(self): results = sess.run(None, feeds) np.testing.assert_allclose(expected[0].detach().cpu().numpy(), results[0], atol=1e-5) - @unittest.skipIf(sys.platform == "win32", reason="not supported yet on Windows") - @unittest.skipIf(not has_transformers(), reason="transformers is missing") - @unittest.skipIf(torch_older_than("2.4"), reason="fails to export") - @ignore_warnings(UserWarning) - def test_llama_dort_static(self): - model, input_tensors_many, _ = ( - onnxscript.tools.transformers_models.llama.get_llama_model() - ) - input_tensors = input_tensors_many[0] - expected = model(*input_tensors) - - local_aot_ort = onnxscript.tools.training_helper.make_aot_ort(dynamic=False) - - compiled_model = torch.compile( - copy.deepcopy(model), - backend=local_aot_ort, - dynamic=False, - fullgraph=True, - ) - - results = compiled_model(*input_tensors) - torch.testing.assert_close(expected[0], results[0], atol=1e-5, rtol=1e-5) - - expected_gradients = onnxscript.tools.training_helper.train_loop(model, *input_tensors) - gradients = onnxscript.tools.training_helper.train_loop(compiled_model, *input_tensors) - torch.testing.assert_close(expected_gradients[0], gradients[0], atol=1.0e-5, rtol=1e-5) - if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/onnxscript/tools/transformers_models/phi3_test.py b/onnxscript/tools/transformers_models/phi3_test.py index d9adcfd86..ac03f487d 100644 --- a/onnxscript/tools/transformers_models/phi3_test.py +++ b/onnxscript/tools/transformers_models/phi3_test.py @@ -2,7 +2,6 @@ # Licensed under the MIT License. # pylint: disable=not-callable -import copy import sys import unittest @@ -110,37 +109,6 @@ def test_phi3_export_cuda(self): results = sess.run(None, feeds) np.testing.assert_allclose(expected[0].detach().cpu().numpy(), results[0], atol=1e-5) - @unittest.skipIf(sys.platform == "win32", reason="not supported yet on Windows") - @unittest.skipIf(not has_transformers(), reason="transformers is missing") - @unittest.skipIf(not has_phi3(), reason="transformers is not recent enough") - @unittest.skipIf( - True, - reason="You are not running the flash-attention implementation, expect numerical differences.", - ) - @ignore_warnings(UserWarning) - def test_phi3_dort_static(self): - model, input_tensors_many, _ = ( - onnxscript.tools.transformers_models.phi3.get_phi3_model() - ) - input_tensors = input_tensors_many[0] - expected = model(*input_tensors) - - local_aot_ort = onnxscript.tools.training_helper.make_aot_ort(dynamic=False) - - compiled_model = torch.compile( - copy.deepcopy(model), - backend=local_aot_ort, - dynamic=False, - fullgraph=True, - ) - - results = compiled_model(*input_tensors) - torch.testing.assert_close(expected[0], results[0], atol=1e-5, rtol=1e-5) - - expected_gradients = onnxscript.tools.training_helper.train_loop(model, *input_tensors) - gradients = onnxscript.tools.training_helper.train_loop(compiled_model, *input_tensors) - torch.testing.assert_close(expected_gradients[0], gradients[0], atol=1e-5, rtol=1e-5) - if __name__ == "__main__": unittest.main(verbosity=2) From 26b4973f3f4d7689422a17d0cc95b092c94825b9 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 19:59:20 -0800 Subject: [PATCH 33/36] torch --- noxfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noxfile.py b/noxfile.py index ba28476ef..966779d44 100644 --- a/noxfile.py +++ b/noxfile.py @@ -32,7 +32,7 @@ ) ONNX = "onnx==1.17" ONNX_RUNTIME = "onnxruntime==1.20.1" -PYTORCH = "torch==2.3.1" +PYTORCH = "torch==2.4.1" TORCHVISON = "torchvision==0.18.1" TRANSFORMERS = "transformers==4.37.2" ONNX_RUNTIME_NIGHTLY_DEPENDENCIES = ( From 0967efb4462c37927747b187d1b54903975ce991 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 20:01:09 -0800 Subject: [PATCH 34/36] vision --- noxfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noxfile.py b/noxfile.py index 966779d44..f0e24f642 100644 --- a/noxfile.py +++ b/noxfile.py @@ -33,7 +33,7 @@ ONNX = "onnx==1.17" ONNX_RUNTIME = "onnxruntime==1.20.1" PYTORCH = "torch==2.4.1" -TORCHVISON = "torchvision==0.18.1" +TORCHVISON = "torchvision==0.19.1" TRANSFORMERS = "transformers==4.37.2" ONNX_RUNTIME_NIGHTLY_DEPENDENCIES = ( "flatbuffers", From 24bdc3fcc8c38fa663f8528d6a9a97e95bb7c50b Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 20:10:09 -0800 Subject: [PATCH 35/36] exp --- .github/workflows/main.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 910c282fc..9613b78d9 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -31,7 +31,6 @@ jobs: - py311-torch-nightly - py311-onnx-weekly - py311-ort-nightly - - py311-experimental-torchlib-tracing - py310 include: - name: py312 From 45d0ce52a5cf8ebdc0efe3c2a05ef4acfaf42a25 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Fri, 3 Jan 2025 21:26:55 -0800 Subject: [PATCH 36/36] q --- .../torch_lib/quantization_test.py | 54 ------------------- 1 file changed, 54 deletions(-) delete mode 100644 tests/function_libs/torch_lib/quantization_test.py diff --git a/tests/function_libs/torch_lib/quantization_test.py b/tests/function_libs/torch_lib/quantization_test.py deleted file mode 100644 index 7ec04ee77..000000000 --- a/tests/function_libs/torch_lib/quantization_test.py +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -"""Test quantized model export.""" - -from __future__ import annotations - -import unittest - -import onnx -import torch -import torch._export as torch_export -from torch.ao.quantization import quantize_pt2e -from torch.ao.quantization.quantizer import xnnpack_quantizer - -from onnxscript._internal import version_utils - - -class QuantizedModelExportTest(unittest.TestCase): - @unittest.skipIf( - version_utils.torch_older_than("2.4"), - "Dynamo exporter fails at the modularization step.", - ) - def test_simple_quantized_model(self): - class TestModel(torch.nn.Module): - def __init__(self): - super().__init__() - self.linear = torch.nn.Linear(5, 10) - - def forward(self, x): - return self.linear(x) - - example_inputs = (torch.randn(1, 5),) - model = TestModel().eval() - - # Step 1. program capture - pt2e_torch_model = torch_export.capture_pre_autograd_graph(model, example_inputs) - - # Step 2. quantization - quantizer = xnnpack_quantizer.XNNPACKQuantizer().set_global( - xnnpack_quantizer.get_symmetric_quantization_config() - ) - pt2e_torch_model = quantize_pt2e.prepare_pt2e(pt2e_torch_model, quantizer) - - # Run the prepared model with sample input data to ensure that internal observers are populated with correct values - pt2e_torch_model(*example_inputs) - - # Convert the prepared model to a quantized model - pt2e_torch_model = quantize_pt2e.convert_pt2e(pt2e_torch_model, fold_quantize=False) - program = torch.onnx.dynamo_export(pt2e_torch_model, *example_inputs) - onnx.checker.check_model(program.model_proto, full_check=True) - - -if __name__ == "__main__": - unittest.main()