| 491 | |
| 492 | |
| 493 | def 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 | |
| 512 | def hack_forward(module: nn.Module, |