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

Function transpose

src/transformers/src/transformers/utils/generic.py:610–628  ·  view source on GitHub ↗

Framework-agnostic version of `numpy.transpose` that will work on torch/TensorFlow/Jax tensors as well as NumPy arrays.

(array, axes=None)

Source from the content-addressed store, hash-verified

608
609
610def transpose(array, axes=None):
611 """
612 Framework-agnostic version of `numpy.transpose` that will work on torch/TensorFlow/Jax tensors as well as NumPy
613 arrays.
614 """
615 if is_numpy_array(array):
616 return np.transpose(array, axes=axes)
617 elif is_torch_tensor(array):
618 return array.T if axes is None else array.permute(*axes)
619 elif is_tf_tensor(array):
620 import tensorflow as tf
621
622 return tf.transpose(array, perm=axes)
623 elif is_jax_tensor(array):
624 import jax.numpy as jnp
625
626 return jnp.transpose(array, axes=axes)
627 else:
628 raise ValueError(f"Type not supported for transpose: {type(array)}.")
629
630
631def reshape(array, newshape):

Callers 4

test_transpose_numpyMethod · 0.90
test_transpose_torchMethod · 0.90
test_transpose_tfMethod · 0.90
test_transpose_flaxMethod · 0.90

Calls 4

is_numpy_arrayFunction · 0.85
is_torch_tensorFunction · 0.85
is_tf_tensorFunction · 0.85
is_jax_tensorFunction · 0.85

Tested by 4

test_transpose_numpyMethod · 0.72
test_transpose_torchMethod · 0.72
test_transpose_tfMethod · 0.72
test_transpose_flaxMethod · 0.72