Change tuple() to () for slightly improved performance in trt_compiler.py#8749
Change tuple() to () for slightly improved performance in trt_compiler.py#8749benediktjohannes wants to merge 2 commits intoProject-MONAI:devfrom
Conversation
…r.py Signed-off-by: Benedikt Johannes <benedikt.johannes.hofer@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 📝 WalkthroughWalkthroughThe PR modifies Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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. Withngroups this is O(n²) total allocations. Same problem applies torev_groupsat 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>
Description
See also #8747 and #8748
Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.