From de1999ddf1a38cb08abd895159567a577d776c66 Mon Sep 17 00:00:00 2001 From: Vaggelis Date: Sun, 23 Aug 2026 12:21:43 -0400 Subject: [PATCH 1/2] polygraphy: fix convert crash when input is already an engine The trt branch of polygraphy convert consumed load_engine_bytes() through a with statement. That works when the loader produces IHostMemory for a network input, but when the input is already a serialized engine the loader returns plain bytes, which do not support the context manager protocol, so commands like "polygraphy convert model.engine --convert-to trt -o out.plan" always crashed after doing their work. Assign the result directly; nothing between the conversion and the end of run_impl needs the prompt release, and Python reclaims IHostMemory like any other object. Also correct the load_engine_bytes docstring, which claimed it returns ICudaEngine, and cover the engine-to-engine path with a CLI-level test. Test Plan: cd tools/Polygraphy PYTHONPATH=$PWD python -m pytest "tests/tools/test_convert.py::TestConvertToTrt::test_engine_to_engine" fails on main with TypeError: 'bytes' object does not support the context manager protocol, passes after the change PYTHONPATH=$PWD python -m pytest tests/tools/test_convert.py::TestConvertToOnnxLikeTrt 7 passed, unchanged Signed-off-by: Vaggelis --- tools/Polygraphy/CHANGELOG.md | 5 +++++ .../polygraphy/tools/args/backend/trt/loader.py | 4 ++-- .../Polygraphy/polygraphy/tools/convert/convert.py | 10 +++++----- tools/Polygraphy/tests/tools/test_convert.py | 13 +++++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/tools/Polygraphy/CHANGELOG.md b/tools/Polygraphy/CHANGELOG.md index 35c33eaf5..4e110bcfc 100644 --- a/tools/Polygraphy/CHANGELOG.md +++ b/tools/Polygraphy/CHANGELOG.md @@ -2,6 +2,11 @@ Dates are in YYYY-MM-DD format. +## v0.49.28 +### Fixed +- Fixed a bug where `polygraphy convert` would fail when the input model was already a + TensorRT engine, e.g. `polygraphy convert model.engine --convert-to trt -o out.plan`. + ## v0.49.27 ### Added - Added `polygraphy template shard-hints` to generate hints file for `polygraphy multi-device shard`. diff --git a/tools/Polygraphy/polygraphy/tools/args/backend/trt/loader.py b/tools/Polygraphy/polygraphy/tools/args/backend/trt/loader.py index e78b8fa38..87cf7db9a 100644 --- a/tools/Polygraphy/polygraphy/tools/args/backend/trt/loader.py +++ b/tools/Polygraphy/polygraphy/tools/args/backend/trt/loader.py @@ -712,14 +712,14 @@ def add_to_script_impl(self, script, network_name=None): def load_engine_bytes(self, network=None): """ - Loads a TensorRT engine according to arguments provided on the command-line. + Loads serialized TensorRT engine bytes according to arguments provided on the command-line. Args: network (Tuple[trt.Builder, trt.INetworkDefinition, Optional[parser]]): A tuple containing a TensorRT builder, network and optionally parser. Returns: - tensorrt.ICudaEngine: The engine. + bytes: The serialized engine. """ loader = args_util.run_script(self.add_to_script, network) return loader() diff --git a/tools/Polygraphy/polygraphy/tools/convert/convert.py b/tools/Polygraphy/polygraphy/tools/convert/convert.py index 50ba59bb5..6fb7e3bbf 100644 --- a/tools/Polygraphy/polygraphy/tools/convert/convert.py +++ b/tools/Polygraphy/polygraphy/tools/convert/convert.py @@ -101,11 +101,11 @@ def run_impl(self, args): model = self.arg_groups[OnnxLoadArgs].load_onnx() self.arg_groups[OnnxSaveArgs].save_onnx(model, args.output) elif convert_type.is_trt(): - with self.arg_groups[ + serialized_engine = self.arg_groups[ TrtLoadEngineBytesArgs - ].load_engine_bytes() as serialized_engine: - self.arg_groups[TrtSaveEngineBytesArgs].save_engine_bytes( - serialized_engine, args.output - ) + ].load_engine_bytes() + self.arg_groups[TrtSaveEngineBytesArgs].save_engine_bytes( + serialized_engine, args.output + ) else: G_LOGGER.critical(f"Cannot convert to model type: {convert_type}") diff --git a/tools/Polygraphy/tests/tools/test_convert.py b/tools/Polygraphy/tests/tools/test_convert.py index 19f313deb..5739bf8a8 100644 --- a/tools/Polygraphy/tests/tools/test_convert.py +++ b/tools/Polygraphy/tests/tools/test_convert.py @@ -145,6 +145,19 @@ def test_modify_onnx_outputs(self, poly_convert): model = onnx.load(outmodel.name) assert len(model.graph.output) == 2 + def test_engine_to_engine(self, poly_convert): + # Engine inputs yield plain bytes, not an object that supports the + # context manager protocol. + engine_bytes = b"serialized-engine" + with util.NamedTemporaryFile( + "w+b", suffix=".engine" + ) as inmodel, util.NamedTemporaryFile(suffix=".plan") as outmodel: + inmodel.write(engine_bytes) + inmodel.flush() + + poly_convert([inmodel.name, "--model-type=engine", "-o", outmodel.name]) + assert BytesFromPath(outmodel.name)() == engine_bytes + class TestConvertToOnnxLikeTrt: @pytest.mark.parametrize( From a18639fd8a6767fb0394d5355ac125df29640fa8 Mon Sep 17 00:00:00 2001 From: Vaggelis Date: Mon, 24 Aug 2026 07:32:38 -0400 Subject: [PATCH 2/2] polygraphy: move convert changelog entry under v0.49.27 Upstream appends entries for unreleased changes to the existing undated version section rather than opening a new heading. Signed-off-by: Vaggelis --- tools/Polygraphy/CHANGELOG.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tools/Polygraphy/CHANGELOG.md b/tools/Polygraphy/CHANGELOG.md index 4e110bcfc..1ab1b83a2 100644 --- a/tools/Polygraphy/CHANGELOG.md +++ b/tools/Polygraphy/CHANGELOG.md @@ -2,11 +2,6 @@ Dates are in YYYY-MM-DD format. -## v0.49.28 -### Fixed -- Fixed a bug where `polygraphy convert` would fail when the input model was already a - TensorRT engine, e.g. `polygraphy convert model.engine --convert-to trt -o out.plan`. - ## v0.49.27 ### Added - Added `polygraphy template shard-hints` to generate hints file for `polygraphy multi-device shard`. @@ -18,6 +13,8 @@ Dates are in YYYY-MM-DD format. ### Fixed - Fixed issue when `polygraphy multi-device shard` would exceed python recursive depth limit on large models. +- Fixed a bug where `polygraphy convert` would fail when the input model was already a + TensorRT engine, e.g. `polygraphy convert model.engine --convert-to trt -o out.plan`. ## v0.49.26 (2025-07-16)