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: 2 additions & 1 deletion CHANGELOGS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Change Logs
0.6.1
+++++

* :pr:`115`, :pr:`116`, :pr:`117`, :pr:`118`: first steps for TorchOnnxEvaluator
* :pr:`115`, :pr:`116`, :pr:`117`, :pr:`118`, :pr:`119`:
first steps for TorchOnnxEvaluator
* :pr:`114`: extends the list of known rewritings
* :pr:`113`: fixes a couple of issues with ModelBuilder

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import onnx.helper as oh
import onnx.numpy_helper as onh
import torch
from onnx_diagnostic.ext_test_case import ExtTestCase
from onnx_diagnostic.ext_test_case import ExtTestCase, ignore_warnings
from onnx_diagnostic.helpers.onnx_helper import from_array_extended
from onnx_diagnostic.reference import ExtendedReferenceEvaluator, TorchOnnxEvaluator
from onnx_diagnostic.reference.torch_evaluator import get_kernels

Expand Down Expand Up @@ -97,11 +98,13 @@ def test_op_binary_cmp(self):
[
oh.make_node("Neg", ["X"], ["nx"]),
oh.make_node("Reciprocal", ["nx"], ["rnx"]),
oh.make_node("Equal", ["X", "Y"], ["ae"]),
oh.make_node("Greater", ["X", "rnx"], ["a"]),
oh.make_node("GreaterOrEqual", ["X", "Y"], ["b"]),
oh.make_node("Less", ["X", "Y"], ["c"]),
oh.make_node("LessOrEqual", ["X", "Y"], ["d"]),
oh.make_node("And", ["a", "b"], ["ab"]),
oh.make_node("And", ["ae", "a"], ["aa"]),
oh.make_node("And", ["aa", "b"], ["ab"]),
oh.make_node("Or", ["c", "d"], ["cd"]),
oh.make_node("Not", ["cd"], ["ncd"]),
oh.make_node("And", ["ab", "ncd"], ["Z"]),
Expand Down Expand Up @@ -559,6 +562,297 @@ def test_op_layer_normalization_big_eps(self):
atol=1e-4,
)

def test_op_range_float(self):
model = oh.make_model(
oh.make_graph(
[oh.make_node("Range", ["start", "limit", "delta"], ["Z"])],
"dummy",
[
oh.make_tensor_value_info("start", TFLOAT, []),
oh.make_tensor_value_info("limit", TFLOAT, []),
oh.make_tensor_value_info("delta", TFLOAT, []),
],
[oh.make_tensor_value_info("Z", TFLOAT, ["a"])],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
self._finalize_test(
model,
torch.tensor(2.1, dtype=torch.float32),
torch.tensor(5.1, dtype=torch.float32),
torch.tensor(1, dtype=torch.float32),
)

def test_op_range_int64(self):
model = oh.make_model(
oh.make_graph(
[oh.make_node("Range", ["start", "limit", "delta"], ["Z"])],
"dummy",
[
oh.make_tensor_value_info("start", TINT64, []),
oh.make_tensor_value_info("limit", TINT64, []),
oh.make_tensor_value_info("delta", TINT64, []),
],
[oh.make_tensor_value_info("Z", TINT64, ["a"])],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
self._finalize_test(
model,
torch.tensor(2, dtype=torch.int64),
torch.tensor(5, dtype=torch.int64),
torch.tensor(1, dtype=torch.int64),
)

def test_op_range_int64_h2(self):
model = oh.make_model(
oh.make_graph(
[oh.make_node("Range", ["start", "limit", "delta"], ["Z"])],
"dummy",
[
oh.make_tensor_value_info("start", TINT64, []),
oh.make_tensor_value_info("limit", TINT64, []),
oh.make_tensor_value_info("delta", TINT64, []),
],
[oh.make_tensor_value_info("Z", TINT64, ["a"])],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
self._finalize_test(
model,
torch.tensor(2, dtype=torch.int64),
torch.tensor(5, dtype=torch.int64),
torch.tensor(2, dtype=torch.int64),
)

def test_op_expand(self):
model = oh.make_model(
oh.make_graph(
[oh.make_node("Expand", ["X", "shape"], ["Y"])],
"dummy",
[
oh.make_tensor_value_info("X", TFLOAT, ["a", "b", "c", "d"]),
oh.make_tensor_value_info("shape", TINT64, ["f"]),
],
[oh.make_tensor_value_info("Y", TFLOAT, ["aa", "ba", "ca", "da"])],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
self._finalize_test(
model,
torch.rand((1, 5, 6, 7), dtype=torch.float32),
torch.tensor([4, 5, 1, 1], dtype=torch.int64),
)

def test_op_unary(self):
model = oh.make_model(
oh.make_graph(
[
oh.make_node("Cos", ["X"], ["nx"]),
oh.make_node("Sin", ["nx"], ["t"]),
oh.make_node("Exp", ["t"], ["u"]),
oh.make_node("Log", ["u"], ["Z"]),
],
"dummy",
[oh.make_tensor_value_info("X", TFLOAT, ["a", "b"])],
[oh.make_tensor_value_info("Z", TFLOAT, ["a", "b"])],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
onnx.checker.check_model(model)
self._finalize_test(model, torch.abs(torch.rand(3, 4, dtype=torch.float32)), atol=1e-6)

def test_op_pow(self):
model = oh.make_model(
oh.make_graph(
[oh.make_node("Pow", ["X", "Y"], ["Z"])],
"dummy",
[
oh.make_tensor_value_info("X", TFLOAT, ["a", "b"]),
oh.make_tensor_value_info("Y", TFLOAT, ["a", "b"]),
],
[oh.make_tensor_value_info("Z", TFLOAT, ["a", "b"])],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
onnx.checker.check_model(model)
self._finalize_test(
model,
torch.abs(torch.rand(3, 4, 5, dtype=torch.float32)),
torch.abs(torch.rand(3, 4, 5, dtype=torch.float32)),
atol=1e-7,
)

def test_op_pow_op_int(self):
model = oh.make_model(
oh.make_graph(
[oh.make_node("Pow", ["X", "Y"], ["Z"])],
"dummy",
[
oh.make_tensor_value_info("X", TFLOAT, ["a", "b"]),
oh.make_tensor_value_info("Y", TINT64, ["a", "b"]),
],
[oh.make_tensor_value_info("Z", TFLOAT, ["a", "b"])],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
onnx.checker.check_model(model)
self._finalize_test(
model,
torch.rand(3, 4, 5, dtype=torch.float32),
torch.tensor([2], dtype=torch.int64),
atol=1e-7,
)

def test_op_sqrt(self):
model = oh.make_model(
oh.make_graph(
[oh.make_node("Sqrt", ["X"], ["Z"])],
"dummy",
[oh.make_tensor_value_info("X", TFLOAT, ["a", "b"])],
[oh.make_tensor_value_info("Z", TFLOAT, ["a", "b"])],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
onnx.checker.check_model(model)
self._finalize_test(model, torch.abs(torch.rand(3, 4, dtype=torch.float32)), atol=1e-6)

def test_op_sigmoid(self):
model = oh.make_model(
oh.make_graph(
[oh.make_node("Sigmoid", ["X"], ["Z"])],
"dummy",
[oh.make_tensor_value_info("X", TFLOAT, ["a", "b"])],
[oh.make_tensor_value_info("Z", TFLOAT, ["a", "b"])],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
onnx.checker.check_model(model)
self._finalize_test(model, torch.abs(torch.rand(3, 4, dtype=torch.float32)), atol=1e-6)

def test_op_split(self):
model = oh.make_model(
oh.make_graph(
[oh.make_node("Split", ["X"], ["Z1", "Z2"], axis=1, num_outputs=2)],
"dummy",
[oh.make_tensor_value_info("X", TFLOAT, ["a", "b"])],
[
oh.make_tensor_value_info("Z1", TFLOAT, ["a", "b1"]),
oh.make_tensor_value_info("Z2", TFLOAT, ["a", "b2"]),
],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
onnx.checker.check_model(model)
self._finalize_test(model, torch.rand(3, 5, dtype=torch.float32), use_ort=True)
self._finalize_test(model, torch.rand(3, 6, dtype=torch.float32), use_ort=True)

def test_op_split_op_sizes(self):
model = oh.make_model(
oh.make_graph(
[oh.make_node("Split", ["X", "split"], ["Z1", "Z2"], axis=1)],
"dummy",
[
oh.make_tensor_value_info("X", TFLOAT, ["a", "b"]),
oh.make_tensor_value_info("split", TINT64, [2]),
],
[
oh.make_tensor_value_info("Z1", TFLOAT, ["a", "b1"]),
oh.make_tensor_value_info("Z2", TFLOAT, ["a", "b2"]),
],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
onnx.checker.check_model(model)
self._finalize_test(
model,
torch.rand(3, 5, dtype=torch.float32),
torch.tensor([2, 3], dtype=torch.int64),
use_ort=True,
)

def test_op_constant_of_shape(self):
model = oh.make_model(
oh.make_graph(
[
oh.make_node(
"ConstantOfShape",
["shape"],
["Z"],
value=from_array_extended(np.array([2], dtype=np.float16)),
)
],
"dummy",
[oh.make_tensor_value_info("shape", TINT64, ["a"])],
[oh.make_tensor_value_info("Z", onnx.TensorProto.FLOAT16, ["a", "b"])],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
onnx.checker.check_model(model)
self._finalize_test(model, torch.tensor([4, 5], dtype=torch.int64))

def test_op_trilu(self):
model = oh.make_model(
oh.make_graph(
[oh.make_node("Trilu", ["X"], ["Z"])],
"dummy",
[oh.make_tensor_value_info("X", TFLOAT, ["a", "b"])],
[oh.make_tensor_value_info("Z", TFLOAT, ["a", "b"])],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
onnx.checker.check_model(model)
self._finalize_test(model, torch.rand((4, 4), dtype=torch.float32))

def test_op_trilu_1(self):
model = oh.make_model(
oh.make_graph(
[oh.make_node("Trilu", ["X"], ["Z"], upper=0)],
"dummy",
[oh.make_tensor_value_info("X", TFLOAT, ["a", "b"])],
[oh.make_tensor_value_info("Z", TFLOAT, ["a", "b"])],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
onnx.checker.check_model(model)
self._finalize_test(model, torch.rand((4, 4), dtype=torch.float32))

@ignore_warnings(DeprecationWarning)
def test_op_trilu_k(self):
model = oh.make_model(
oh.make_graph(
[oh.make_node("Trilu", ["X", "k"], ["Z"], upper=1)],
"dummy",
[
oh.make_tensor_value_info("X", TFLOAT, ["a", "b"]),
oh.make_tensor_value_info("k", TINT64, []),
],
[oh.make_tensor_value_info("Z", TFLOAT, ["a", "b"])],
),
ir_version=9,
opset_imports=[oh.make_opsetid("", 18)],
)
onnx.checker.check_model(model)
self._finalize_test(
model,
torch.rand((6, 6), dtype=torch.float32),
torch.tensor([2], dtype=torch.int64),
)


if __name__ == "__main__":
unittest.main(verbosity=2)
23 changes: 23 additions & 0 deletions _unittests/ut_torch_models/test_test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,29 @@ def test_validate_model_custom(self):
onnx_filename, output_path, num_attention_heads=2, hidden_size=192, verbose=10
)

@requires_torch("2.7")
@hide_stdout()
@ignore_warnings(FutureWarning)
@requires_experimental()
def test_validate_model_custom_torch(self):
mid = "arnir0/Tiny-LLM"
summary, data = validate_model(
mid,
do_run=True,
verbose=10,
exporter="custom-inline",
dump_folder="dump_test_validate_model_custom_torch",
patch=True,
stop_if_static=2 if pv.Version(torch.__version__) > pv.Version("2.6.1") else 0,
optimization="default",
quiet=False,
runtime="torch",
)
self.assertIsInstance(summary, dict)
self.assertIsInstance(data, dict)
self.assertIn("disc_onnx_ort_run_abs", summary)
self.assertLess(summary["disc_onnx_ort_run_abs"], 1e-4)

def test_filter_inputs(self):
inputs, ds = {"a": 1, "b": 2}, {"a": 20, "b": 30}
ni, nd = filter_inputs(inputs, dynamic_shapes=ds, drop_names=["a"])
Expand Down
7 changes: 7 additions & 0 deletions onnx_diagnostic/_command_lines_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,12 @@ def get_parser_validate() -> ArgumentParser:
action=BooleanOptionalAction,
help="validate the trained model (requires downloading)",
)
parser.add_argument(
"--runtime",
choices=["onnxruntime", "torch", "ref"],
default="onnxruntime",
help="onnx runtime to use, ",
)
parser.add_argument(
"-o",
"--dump-folder",
Expand Down Expand Up @@ -453,6 +459,7 @@ def _cmd_validate(argv: List[Any]):
model_options=args.mop,
subfolder=args.subfolder,
opset=args.opset,
runtime=args.runtime,
)
print("")
print("-- summary --")
Expand Down
6 changes: 5 additions & 1 deletion onnx_diagnostic/helpers/rt_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ def make_feeds(
names = (
[i.name for i in proto.graph.input]
if isinstance(proto, onnx.ModelProto)
else ([i.name for i in proto.get_inputs()] if hasattr(proto, "get_inputs") else proto)
else (
[i.name for i in proto.get_inputs()]
if hasattr(proto, "get_inputs")
else (proto.input_names if hasattr(proto, "input_names") else proto)
)
)
assert (
isinstance(names, list)
Expand Down
Loading
Loading