MCPcopy Create free account
hub / github.com/ModalityDance/Omni-R1 / to_numpy

Function to_numpy

src/transformers/src/transformers/utils/generic.py:284–307  ·  view source on GitHub ↗

Convert a TensorFlow tensor, PyTorch tensor, Numpy array or python list to a Numpy array.

(obj)

Source from the content-addressed store, hash-verified

282
283
284def to_numpy(obj):
285 """
286 Convert a TensorFlow tensor, PyTorch tensor, Numpy array or python list to a Numpy array.
287 """
288
289 framework_to_numpy = {
290 "pt": lambda obj: obj.detach().cpu().numpy(),
291 "tf": lambda obj: obj.numpy(),
292 "jax": lambda obj: np.asarray(obj),
293 "np": lambda obj: obj,
294 }
295
296 if isinstance(obj, (dict, UserDict)):
297 return {k: to_numpy(v) for k, v in obj.items()}
298 elif isinstance(obj, (list, tuple)):
299 return np.array(obj)
300
301 # This gives us a smart order to test the frameworks with the corresponding tests.
302 framework_to_test_func = _get_frameworks_and_test_func(obj)
303 for framework, test_func in framework_to_test_func.items():
304 if test_func(obj):
305 return framework_to_numpy[framework](obj)
306
307 return obj
308
309
310class ModelOutput(OrderedDict):

Callers 5

to_numpy_arrayFunction · 0.85
padMethod · 0.85
_decode_audioMethod · 0.85
_decode_audioMethod · 0.85
batch_decodeMethod · 0.85

Calls 3

detachMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected