Skip to content

Polygraphy - fix TypeError when converting an engine with polygraphy convert --convert-to trt - #4835

Open
VaggelisGian wants to merge 2 commits into
NVIDIA:mainfrom
VaggelisGian:fix-pgy-convert-engine-bytes
Open

Polygraphy - fix TypeError when converting an engine with polygraphy convert --convert-to trt#4835
VaggelisGian wants to merge 2 commits into
NVIDIA:mainfrom
VaggelisGian:fix-pgy-convert-engine-bytes

Conversation

@VaggelisGian

Copy link
Copy Markdown

What does this PR do?

polygraphy convert model.engine --convert-to trt -o out.plan always crashed with TypeError, because the engine input was loaded inside a with statement in tools/Polygraphy/polygraphy/tools/convert/convert.py. Engine inputs return plain bytes, which are not context managers, so every engine-to-engine conversion died before doing any work. The fix assigns the loaded bytes to a plain variable instead.

While there, this corrects the load_engine_bytes() docstring, which claimed it returns an ICudaEngine; it actually returns serialized bytes.

Testing

New CLI-level regression test tests/tools/test_convert.py::TestConvertToTrt::test_engine_to_engine: converting a saved .engine back through polygraphy convert --convert-to trt must succeed. It fails on unpatched main with the TypeError above and passes after.

Commands and output on this machine:

$ cd tools/Polygraphy
$ python -m pytest "tests/tools/test_convert.py::TestConvertToTrt::test_engine_to_engine" -v

tests\tools\test_convert.py::TestConvertToTrt::test_engine_to_engine[inprocess] PASSED [100%]
======================== 1 passed, 3 warnings in 1.36s ========================

Full current outcome for the two touched suites on this branch:

$ python -m pytest "tests/tools/test_convert.py::TestConvertToTrt" -v
tests\tools\test_convert.py::TestConvertToTrt::test_onnx_to_trt[inprocess] FAILED
tests\tools\test_convert.py::TestConvertToTrt::test_tf_to_onnx_to_trt[inprocess] SKIPPED
tests\tools\test_convert.py::TestConvertToTrt::test_trt_network_config_script_to_engine[inprocess] FAILED
tests\tools\test_convert.py::TestConvertToTrt::test_modify_onnx_outputs[inprocess] PASSED
tests\tools\test_convert.py::TestConvertToTrt::test_engine_to_engine[inprocess] PASSED
2 failed, 9 passed, 1 skipped in 4.80s (combined run with TestConvertToOnnxLikeTrt)

The two failures are pre-existing on unpatched origin/main (verified in a clean worktree of 10d15ae, same 2 failed) and come from TensorRT 11 wheel API drift (ILayer.precision_is_set removal), unrelated to this change. The new test is the one that flips from red to green.

Environment

TensorRT OSS main (10d15ae), polygraphy sources run from the repo.
Python 3.12, tensorrt 11.2.1.2 (pip wheel), polygraphy 0.53.4, onnx 1.17.0,
NVIDIA RTX 5060 Ti, driver 591.86, Windows 11 Pro.

Issue

Per CONTRIBUTING.md, bugfixes start as an issue approved by TensorRT engineers before code review. I could not find an existing issue covering this; happy to file one and link it here if maintainers prefer that flow, or please advise whether this PR can proceed directly.

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 <baggelis100@gmail.com>
@VaggelisGian
VaggelisGian requested a review from a team as a code owner August 24, 2026 10:58
Upstream appends entries for unreleased changes to the existing
undated version section rather than opening a new heading.

Signed-off-by: Vaggelis <baggelis100@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant