MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / parse_groups

Function parse_groups

monai/networks/trt_compiler.py:255–302  ·  view source on GitHub ↗

Implements parsing of 'output_lists' arg of trt_compile(). Args: ret: plain list of Tensors output_lists: list of output group sizes: to form some Lists/Tuples out of 'ret' List, this will be a list of group dimensions, like [[], [5], [-1]] for returning Te

(
    ret: list[torch.Tensor], output_lists: list[list[int]]
)

Source from the content-addressed store, hash-verified

253
254
255def parse_groups(
256 ret: list[torch.Tensor], output_lists: list[list[int]]
257) -> tuple[torch.Tensor | list[torch.Tensor], ...]:
258 """
259 Implements parsing of 'output_lists' arg of trt_compile().
260
261 Args:
262 ret: plain list of Tensors
263
264 output_lists: list of output group sizes: to form some Lists/Tuples out of 'ret' List, this will be a list
265 of group dimensions, like [[], [5], [-1]] for returning Tensor, list of 5 items and dynamic list.
266 Format: [[group_n] | [], ...]
267 [] or group_n == 0 : next output from ret is a scalar
268 group_n > 0 : next output from ret is a list of group_n length
269 group_n == -1: next output is a dynamic list. This entry can be at any
270 position in output_lists, but can appear only once.
271 Returns:
272 Tuple of Union[torch.Tensor, List[torch.Tensor]], according to the grouping in output_lists
273
274 """
275 groups: tuple[torch.Tensor | list[torch.Tensor], ...] = ()
276 cur = 0
277 for idx in range(len(output_lists)):
278 gl = output_lists[idx]
279 assert len(gl) == 0 or len(gl) == 1
280 if len(gl) == 0 or gl[0] == 0:
281 groups = (*groups, ret[cur])
282 cur = cur + 1
283 elif gl[0] > 0:
284 groups = (*groups, ret[cur : cur + gl[0]])
285 cur = cur + gl[0]
286 elif gl[0] == -1:
287 rev_groups: tuple[torch.Tensor | list[torch.Tensor], ...] = ()
288 rcur = len(ret)
289 for rl in range(len(output_lists) - 1, idx, -1):
290 rgl = output_lists[rl]
291 assert len(rgl) == 0 or len(rgl) == 1
292 if len(rgl) == 0 or rgl[0] == 0:
293 rcur = rcur - 1
294 rev_groups = (*rev_groups, ret[rcur])
295 elif rgl[0] > 0:
296 rcur = rcur - rgl[0]
297 rev_groups = (*rev_groups, ret[rcur : rcur + rgl[0]])
298 else:
299 raise ValueError("Two -1 lists in output")
300 groups = (*groups, ret[cur:rcur], *rev_groups[::-1])
301 break
302 return groups
303
304
305class TrtCompiler:

Callers 1

forwardMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…