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
72 changes: 26 additions & 46 deletions onnxscript/_framework_apis/torch_2_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@
import pathlib
from typing import Callable

import onnx

from onnxscript import ir, optimizer
from onnxscript.function_libs.torch_lib import registration
from onnxscript.ir import _external_data

# Internal flag. Will go away.
_TORCH_ONNX_SAVE_EXTERNAL_DATA_WITH_IR = (
os.getenv("TORCH_ONNX_OFFLOAD_EXTERNAL_DATA_WITH_IR") != "0"
)


@dataclasses.dataclass(frozen=True)
class _OnnxFunctionMeta:
Expand Down Expand Up @@ -83,45 +76,32 @@
"""Save the model with external data. The model is unchanged after saving."""

# TODO(#1835): Decide if we want to externalize large attributes as well
if _TORCH_ONNX_SAVE_EXTERNAL_DATA_WITH_IR:
initializer_values = tuple(model.graph.initializers.values())
tensors = [v.const_value for v in initializer_values]
for tensor in tensors:
if tensor is None:
raise ValueError(
"The model contains uninitialized initializer values. "
"Please make sure all initializer values are initialized."
)
destination_path = pathlib.Path(model_path)
base_dir = destination_path.parent
data_path = f"{destination_path.name}.data"

external_tensors = _external_data.convert_tensors_to_external(
tensors, # type: ignore[arg-type]
base_dir,
data_path,
)

# Replace the initializer values with external tensors and save the model
for initializer, external_tensor in zip(initializer_values, external_tensors):
initializer.const_value = external_tensor
ir.save(model, model_path)

# Restore the original initializer values so the model is unchanged
for initializer, tensor in zip(initializer_values, tensors):
initializer.const_value = tensor

else:
destination_path = pathlib.Path(model_path)
# Create the directory if it does not exist
data_path = f"{destination_path.name}.data"
proto = ir.serde.serialize_model(model)
onnx.save_model(
proto,
model_path,
save_as_external_data=True,
location=data_path,
)
initializer_values = tuple(model.graph.initializers.values())

Check warning on line 79 in onnxscript/_framework_apis/torch_2_5.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/_framework_apis/torch_2_5.py#L79

Added line #L79 was not covered by tests
tensors = [v.const_value for v in initializer_values]
for tensor in tensors:
if tensor is None:
raise ValueError(

Check warning on line 83 in onnxscript/_framework_apis/torch_2_5.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/_framework_apis/torch_2_5.py#L83

Added line #L83 was not covered by tests
"The model contains uninitialized initializer values. "
"Please make sure all initializer values are initialized."
)
destination_path = pathlib.Path(model_path)
base_dir = destination_path.parent
data_path = f"{destination_path.name}.data"

Check warning on line 89 in onnxscript/_framework_apis/torch_2_5.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/_framework_apis/torch_2_5.py#L87-L89

Added lines #L87 - L89 were not covered by tests

external_tensors = _external_data.convert_tensors_to_external(

Check warning on line 91 in onnxscript/_framework_apis/torch_2_5.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/_framework_apis/torch_2_5.py#L91

Added line #L91 was not covered by tests
tensors, # type: ignore[arg-type]
base_dir,
data_path,
)

# Replace the initializer values with external tensors and save the model
for initializer, external_tensor in zip(initializer_values, external_tensors):
initializer.const_value = external_tensor
ir.save(model, model_path)

Check warning on line 100 in onnxscript/_framework_apis/torch_2_5.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/_framework_apis/torch_2_5.py#L99-L100

Added lines #L99 - L100 were not covered by tests

# Restore the original initializer values so the model is unchanged
for initializer, tensor in zip(initializer_values, tensors):
initializer.const_value = tensor

Check warning on line 104 in onnxscript/_framework_apis/torch_2_5.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/_framework_apis/torch_2_5.py#L104

Added line #L104 was not covered by tests


def get_torchlib_ops() -> list[_OnnxFunctionMeta]:
Expand Down
26 changes: 26 additions & 0 deletions onnxscript/_framework_apis/torch_2_6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""Stable APIs for PyTorch 2.6."""

from __future__ import annotations

__all__ = [
"check_model",
"convert_version",
"get_torchlib_ops",
"optimize",
"save_model_with_external_data",
]
from onnxscript import ir, optimizer
from onnxscript._framework_apis.torch_2_5 import (
check_model,
convert_version,
get_torchlib_ops,
save_model_with_external_data,
)


def optimize(model: ir.Model) -> ir.Model:
"""Optimize the model."""
optimizer.optimize_ir(model)
return model

Check warning on line 26 in onnxscript/_framework_apis/torch_2_6.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/_framework_apis/torch_2_6.py#L25-L26

Added lines #L25 - L26 were not covered by tests
Loading