MCPcopy Index your code
hub / github.com/modelscope/modelscope / numpify_tensor_nested

Function numpify_tensor_nested

modelscope/utils/regress_test_utils.py:463–490  ·  view source on GitHub ↗
(tensors, reduction=None, clip_value=10000)

Source from the content-addressed store, hash-verified

461
462
463def numpify_tensor_nested(tensors, reduction=None, clip_value=10000):
464 try:
465 from modelscope.outputs import ModelOutputBase
466 except ImportError:
467 ModelOutputBase = dict
468 "Numpify `tensors` (even if it's a nested list/tuple of tensors)."
469 if isinstance(tensors, (Mapping, ModelOutputBase)):
470 return OrderedDict({
471 k: numpify_tensor_nested(t, reduction, clip_value)
472 for k, t in tensors.items()
473 })
474 if isinstance(tensors, list):
475 return list(
476 numpify_tensor_nested(t, reduction, clip_value) for t in tensors)
477 if isinstance(tensors, tuple):
478 return tuple(
479 numpify_tensor_nested(t, reduction, clip_value) for t in tensors)
480 if isinstance(tensors, torch.Tensor):
481 t: np.ndarray = tensors.cpu().numpy()
482 if clip_value is not None:
483 t = np.where(t > clip_value, clip_value, t)
484 t = np.where(t < -clip_value, -clip_value, t)
485 if reduction == 'sum':
486 return t.sum(dtype=float)
487 elif reduction == 'mean':
488 return t.mean(dtype=float)
489 return t
490 return tensors
491
492
493def detach_tensor_nested(tensors):

Callers 9

_validate_onnx_modelMethod · 0.90
_validate_onnx_modelMethod · 0.90
test_save_pretrainedMethod · 0.90
_validate_onnx_modelMethod · 0.90
monitor_module_trainMethod · 0.85
_forwardFunction · 0.85
_stepFunction · 0.85
compare_io_and_printFunction · 0.85

Calls 3

itemsMethod · 0.45
whereMethod · 0.45
meanMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…