Skip to content

Fixed fold_constants, test_handler switched to onnx #8211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2024
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
5 changes: 3 additions & 2 deletions monai/networks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,10 @@ def convert_to_onnx(
onnx_model = onnx.load(f)

if do_constant_folding and polygraphy_imported:
from polygraphy.backend.onnx.loader import fold_constants
from polygraphy.backend.onnx.loader import fold_constants, save_onnx

fold_constants(onnx_model, size_threshold=constant_size_threshold)
onnx_model = fold_constants(onnx_model, size_threshold=constant_size_threshold)
save_onnx(onnx_model, f)

if verify:
if isinstance(inputs, dict):
Expand Down
10 changes: 7 additions & 3 deletions tests/test_trt_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def tearDown(self):
if current_device != self.gpu_device:
torch.cuda.set_device(self.gpu_device)

@unittest.skipUnless(torch_trt_imported, "torch_tensorrt is required")
# @unittest.skipUnless(torch_trt_imported, "torch_tensorrt is required")
def test_handler(self):
from ignite.engine import Engine

Expand All @@ -74,7 +74,7 @@ def test_handler(self):

with tempfile.TemporaryDirectory() as tempdir:
engine = Engine(lambda e, b: None)
args = {"method": "torch_trt"}
args = {"method": "onnx", "dynamic_batchsize": [1, 4, 8]}
TrtHandler(net1, tempdir + "/trt_handler", args=args).attach(engine)
engine.run([0] * 8, max_epochs=1)
self.assertIsNotNone(net1._trt_compiler)
Expand All @@ -86,7 +86,11 @@ def test_lists(self):
model = ListAdd().cuda()

with torch.no_grad(), tempfile.TemporaryDirectory() as tmpdir:
args = {"output_lists": [[-1], [2], []], "export_args": {"dynamo": False, "verbose": True}}
args = {
"output_lists": [[-1], [2], []],
"export_args": {"dynamo": False, "verbose": True},
"dynamic_batchsize": [1, 4, 8],
}
x = torch.randn(1, 16).to("cuda")
y = torch.randn(1, 16).to("cuda")
z = torch.randn(1, 16).to("cuda")
Expand Down
Loading