Skip to content

Commit 6a2fa48

Browse files
authored
Merge branch 'main' into dependabot/github_actions/github/codeql-action-4
2 parents 3b41fd9 + 45ba02d commit 6a2fa48

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+3159
-388
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141

4242
steps:
4343
- name: Checkout repository
44-
uses: actions/checkout@v5
44+
uses: actions/checkout@v6
4545

4646
# Initializes the CodeQL tools for scanning.
4747
- name: Initialize CodeQL

.github/workflows/lint.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
pull-requests: write
2121

2222
steps:
23-
- uses: actions/checkout@v5
23+
- uses: actions/checkout@v6
2424
- name: misspell # Check spelling
2525
uses: reviewdog/action-misspell@v1
2626
with:
@@ -43,7 +43,7 @@ jobs:
4343
permissions:
4444
security-events: write
4545
steps:
46-
- uses: actions/checkout@v5
46+
- uses: actions/checkout@v6
4747
- name: Setup Python
4848
uses: actions/setup-python@v6
4949
with:

.github/workflows/main.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
nox-tag: test-onnx-ir-git
5858
runs-on: ${{ matrix.os }}
5959
steps:
60-
- uses: actions/checkout@v5
60+
- uses: actions/checkout@v6
6161
- name: Setup Python ${{ matrix.python-version }}
6262
uses: actions/setup-python@v6
6363
with:
@@ -83,7 +83,7 @@ jobs:
8383
token: ${{ secrets.CODECOV_TOKEN }}
8484
- name: Upload torchlib error reports
8585
if: always()
86-
uses: actions/upload-artifact@v4
86+
uses: actions/upload-artifact@v5
8787
with:
8888
name: Error reports (${{ matrix.name }}-${{ matrix.os }})
8989
path: error_reports
@@ -95,7 +95,7 @@ jobs:
9595
os: [ubuntu-latest, windows-latest]
9696
runs-on: ${{ matrix.os }}
9797
steps:
98-
- uses: actions/checkout@v5
98+
- uses: actions/checkout@v6
9999
- name: Setup Python
100100
uses: actions/setup-python@v6
101101
with:
@@ -119,7 +119,7 @@ jobs:
119119
update_readme:
120120
runs-on: ubuntu-latest
121121
steps:
122-
- uses: actions/checkout@v5
122+
- uses: actions/checkout@v6
123123
- name: Setup Python
124124
uses: actions/setup-python@v6
125125
- name: Update readme

.github/workflows/pages.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ jobs:
2525
runs-on: ubuntu-latest
2626
steps:
2727
- name: Checkout
28-
uses: actions/checkout@v5
28+
uses: actions/checkout@v6
2929
- name: Setup Pages
3030
uses: actions/configure-pages@v4
3131
- name: Setup Python
3232
uses: actions/setup-python@v6
3333
with:
3434
python-version: "3.10"
35-
- uses: actions/checkout@v5
35+
- uses: actions/checkout@v6
3636
- name: Install dependencies
3737
run: |
3838
python -m pip install --upgrade pip setuptools wheel

.lintrunner.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ include_patterns = [
3939
exclude_patterns = [
4040
'tests/**', # Skip linting test files for speed
4141
# FIXME: Fix typing annotations in these files
42+
'examples/custom_op_expansion.py',
4243
'onnxscript/converter_test.py',
4344
'onnxscript/converter.py',
4445
'onnxscript/evaluator_test.py',

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.4
1+
0.5.7

examples/custom_op_expansion.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
# ruff: noqa
4+
5+
"""A utility and an example showing how onnxscript functions can be used to define function expansions
6+
and be used with the inliner to replace calls to the custom function with an expanded subgraph.
7+
This is useful to perform certain classes of graph surgery easily.
8+
"""
9+
10+
import onnx
11+
12+
import onnxscript
13+
import onnxscript.utils.replace as replace
14+
15+
script = onnxscript.script
16+
FLOAT = onnxscript.FLOAT
17+
op = onnxscript.values.opset22
18+
local = onnxscript.values.Opset("local", 1)
19+
20+
21+
# Example Model: Actual models can come from ModelBuilder or Exporter or any other source.
22+
# Models can contain calls to custom operations (from a custom domain like 'local' here or
23+
# even "com.microsoft" etc.)
24+
@script()
25+
def model_script(X: FLOAT["N"], Y: FLOAT["N"]) -> FLOAT["N"]:
26+
DoubleX = op.Add(X, X)
27+
YSquare = op.Mul(Y, Y)
28+
# Example call to a custom operation
29+
Temp1 = local.CustomOp1(DoubleX, YSquare)
30+
# Another call to a custom operation with an attribute
31+
Temp2 = local.CustomOp2(Temp1, alp=0.9)
32+
return Temp2
33+
34+
35+
# Define expansions for custom operations as onnxscript functions
36+
@script(opset=local)
37+
def CustomOp1(X: FLOAT["N"], Y: FLOAT["N"]) -> FLOAT["N"]:
38+
Temp1 = op.Sub(X, Y)
39+
return op.Div(Temp1, X)
40+
41+
42+
@script(opset=local)
43+
def CustomOp2(X: FLOAT["N"], alp: float) -> FLOAT["N"]:
44+
Temp2 = op.Elu(X, alpha=alp)
45+
return op.Mul(Temp2, Temp2)
46+
47+
48+
# Now, we can replace the custom operations in the model with their expansions:
49+
50+
functions = [CustomOp1.to_function_proto(), CustomOp2.to_function_proto()]
51+
52+
model = model_script.to_model_proto()
53+
54+
print("Original Model with custom operations:")
55+
print(onnx.printer.to_text(model))
56+
57+
58+
updated_model = replace.replace_functions(model, functions)
59+
60+
print("\nUpdated Model after replacing custom operations with their expansions:")
61+
print(onnx.printer.to_text(updated_model))

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"packaging",
4242
"protobuf",
4343
)
44-
ONNX_IR = "onnx_ir==0.1.10"
44+
ONNX_IR = "onnx_ir==0.1.12"
4545
ONNX_IR_MAIN = "git+https://github.com/onnx/ir-py.git@main#egg=onnx_ir"
4646

4747

onnxscript/_framework_apis/torch_2_5.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
]
1414

1515
import dataclasses
16+
import importlib.util
1617
import os
1718
import pathlib
1819
from typing import Callable
@@ -63,7 +64,9 @@ def check_model(model: ir.Model) -> None:
6364
del model # Unused yet
6465

6566

66-
def save_model_with_external_data(model: ir.Model, model_path: str | os.PathLike) -> None:
67+
def save_model_with_external_data(
68+
model: ir.Model, model_path: str | os.PathLike, verbose: bool = False
69+
) -> None:
6770
"""Save the model with external data. The model is unchanged after saving."""
6871

6972
# TODO(#1835): Decide if we want to externalize large attributes as well
@@ -78,7 +81,31 @@ def save_model_with_external_data(model: ir.Model, model_path: str | os.PathLike
7881
destination_path = pathlib.Path(model_path)
7982
data_path = f"{destination_path.name}.data"
8083

81-
ir.save(model, model_path, external_data=data_path)
84+
# Show a progress bar if verbose is True and tqdm is installed
85+
use_tqdm = verbose and importlib.util.find_spec("tqdm") is not None
86+
87+
if use_tqdm:
88+
import tqdm # pylint: disable=import-outside-toplevel
89+
90+
with tqdm.tqdm() as pbar:
91+
total_set = False
92+
93+
def callback(
94+
tensor: ir.TensorProtocol, metadata: ir.external_data.CallbackInfo
95+
) -> None:
96+
nonlocal total_set
97+
if not total_set:
98+
pbar.total = metadata.total
99+
total_set = True
100+
101+
pbar.update()
102+
pbar.set_description(
103+
f"Saving {tensor.name} ({tensor.dtype.short_name()}, {tensor.shape}) at offset {metadata.offset}"
104+
)
105+
106+
ir.save(model, model_path, external_data=data_path, callback=callback)
107+
else:
108+
ir.save(model, model_path, external_data=data_path)
82109

83110

84111
def get_torchlib_ops() -> list[_OnnxFunctionMeta]:

0 commit comments

Comments
 (0)