Skip to content

Comments

Change tuple() to () for slightly improved performance in trt_compiler.py#8749

Open
benediktjohannes wants to merge 2 commits intoProject-MONAI:devfrom
benediktjohannes:patch-6
Open

Change tuple() to () for slightly improved performance in trt_compiler.py#8749
benediktjohannes wants to merge 2 commits intoProject-MONAI:devfrom
benediktjohannes:patch-6

Conversation

@benediktjohannes
Copy link
Contributor

Description

See also #8747 and #8748

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

…r.py

Signed-off-by: Benedikt Johannes <benedikt.johannes.hofer@gmail.com>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 22, 2026

No actionable comments were generated in the recent review. 🎉


📝 Walkthrough

Walkthrough

The PR modifies parse_groups in monai/networks/trt_compiler.py: empty-group initializations change from tuple() to (), the loop index variable is renamed from l to idx and corresponding output_lists accesses updated, and the reverse-iteration branch uses idx as its bound and initializes rev_groups with (). Type annotations and overall control flow remain unchanged; no functional behavior or public signatures were altered.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is incomplete; it lacks the required details section explaining what was changed and why, only referencing related issues. Add a substantive description explaining the performance rationale and specific changes made to trt_compiler.py before referencing related issues.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately reflects the main change: replacing tuple() with () for performance improvement in trt_compiler.py.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
monai/networks/trt_compiler.py (1)

270-273: O(n²) tuple construction — list accumulation is the fix.

groups = (*groups, item) allocates and copies the entire tuple on every iteration. With n groups this is O(n²) total allocations. Same problem applies to rev_groups at lines 283/286.

♻️ Proposed refactor
-    groups: tuple[torch.Tensor | list[torch.Tensor], ...] = ()
+    groups_list: list[torch.Tensor | list[torch.Tensor]] = []
     cur = 0
     for i in range(len(output_lists)):
         gl = output_lists[i]
         assert len(gl) == 0 or len(gl) == 1
         if len(gl) == 0 or gl[0] == 0:
-            groups = (*groups, ret[cur])
+            groups_list.append(ret[cur])
             cur = cur + 1
         elif gl[0] > 0:
-            groups = (*groups, ret[cur : cur + gl[0]])
+            groups_list.append(ret[cur : cur + gl[0]])
             cur = cur + gl[0]
         elif gl[0] == -1:
-            rev_groups: tuple[torch.Tensor | list[torch.Tensor], ...] = ()
+            rev_list: list[torch.Tensor | list[torch.Tensor]] = []
             rcur = len(ret)
             for rl in range(len(output_lists) - 1, i, -1):
                 rgl = output_lists[rl]
                 assert len(rgl) == 0 or len(rgl) == 1
                 if len(rgl) == 0 or rgl[0] == 0:
                     rcur = rcur - 1
-                    rev_groups = (*rev_groups, ret[rcur])
+                    rev_list.append(ret[rcur])
                 elif rgl[0] > 0:
                     rcur = rcur - rgl[0]
-                    rev_groups = (*rev_groups, ret[rcur : rcur + rgl[0]])
+                    rev_list.append(ret[rcur : rcur + rgl[0]])
                 else:
                     raise ValueError("Two -1 lists in output")
-            groups = (*groups, ret[cur:rcur], *rev_groups[::-1])
+            groups_list.append(ret[cur:rcur])
+            groups_list.extend(rev_list[::-1])
             break
-    return groups
+    return tuple(groups_list)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@monai/networks/trt_compiler.py` around lines 270 - 273, The current code
repeatedly rebuilds tuples with constructs like groups = (*groups, ret[cur]) and
similar for rev_groups, causing O(n²) allocations; change groups and rev_groups
to be built as lists (init as []), use list.append(ret[cur]) for single items
and list.extend(ret[cur:cur+gl[0]]) for slices, advance cur as before, and
finally convert to a tuple only once (groups = tuple(groups), rev_groups =
tuple(rev_groups)) where the original tuple type is required; update any
downstream code expecting tuples accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@monai/networks/trt_compiler.py`:
- Line 266: The loop uses a PEP8-banned single-character variable name `l` in
the statement `for l in range(len(output_lists)):`; rename `l` to a clear,
non-ambiguous identifier (e.g., `i` or `idx`) and update every occurrence within
that loop body accordingly (references to `l` and any indexing like
`output_lists[l]`) to the new name to satisfy PEP8 and avoid confusion.

---

Nitpick comments:
In `@monai/networks/trt_compiler.py`:
- Around line 270-273: The current code repeatedly rebuilds tuples with
constructs like groups = (*groups, ret[cur]) and similar for rev_groups, causing
O(n²) allocations; change groups and rev_groups to be built as lists (init as
[]), use list.append(ret[cur]) for single items and
list.extend(ret[cur:cur+gl[0]]) for slices, advance cur as before, and finally
convert to a tuple only once (groups = tuple(groups), rev_groups =
tuple(rev_groups)) where the original tuple type is required; update any
downstream code expecting tuples accordingly.

Signed-off-by: Benedikt Johannes <benedikt.johannes.hofer@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