MCPcopy Create free account
hub / github.com/modelscope/modelscope / detach_tensor_nested

Function detach_tensor_nested

modelscope/utils/regress_test_utils.py:493–509  ·  view source on GitHub ↗
(tensors)

Source from the content-addressed store, hash-verified

491
492
493def detach_tensor_nested(tensors):
494 try:
495 from modelscope.outputs import ModelOutputBase
496 except ImportError:
497 ModelOutputBase = dict
498 "Detach `tensors` (even if it's a nested list/tuple of tensors)."
499 if isinstance(tensors, (Mapping, ModelOutputBase)):
500 return OrderedDict(
501 {k: detach_tensor_nested(t)
502 for k, t in tensors.items()})
503 if isinstance(tensors, list):
504 return list(detach_tensor_nested(t) for t in tensors)
505 if isinstance(tensors, tuple):
506 return tuple(detach_tensor_nested(t) for t in tensors)
507 if isinstance(tensors, torch.Tensor):
508 return tensors.detach()
509 return tensors
510
511
512def hack_forward(module: nn.Module,

Callers 2

_forwardFunction · 0.85
_stepFunction · 0.85

Calls 2

detachMethod · 0.80
itemsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…