TensorPlay
latest (dev)
Copy
View Markdown

Latest development documentation · Updated 2026-09-22

Source code for tensorplay._tensor

import builtins as _builtins

builtins_int = _builtins.int
builtins_complex = _builtins.complex

from collections import OrderedDict

from . import _C

# Alias Tensor to _C.TensorBase so that objects created by C++ (like via tp.tensor())
# are instances of this class.
Tensor = _C.TensorBase

def ndimension(self) -> int:
    """
    Alias for dim()
    """
    return self.dim()

def long(self):
    return self.to(_C.int64)

def float(self):
    return self.to(_C.float32)

def int(self):
    return self.to(_C.int32)

def double(self):
    return self.to(_C.float64)

def cuda(self, device=None, non_blocking=False):
    """
    Returns a copy of this object in CUDA memory.
    If this object is already in CUDA memory and on the correct device, then no copy is performed and the original object is returned.
    """
    if device is None:
        device_idx = _C._cuda.current_device() if _C._cuda.is_available() else 0
    elif isinstance(device, int):
        device_idx = device
    elif isinstance(device, str):
        parsed = _C.Device(device)
        device_idx = parsed.index
        if device_idx < 0:
            device_idx = _C._cuda.current_device() if _C._cuda.is_available() else 0
    elif isinstance(device, _C.Device):
        if not device.is_cuda():
            raise ValueError(f"Expected a CUDA device, got {device}")
        device_idx = device.index
        if device_idx < 0:
            device_idx = _C._cuda.current_device() if _C._cuda.is_available() else 0
    else:
        raise TypeError(f"Invalid CUDA device: {device!r}")
    
    return self.to(_C.Device(_C.DeviceType.CUDA, device_idx), non_blocking=non_blocking)

def cpu(self):
    """
    Returns a copy of this object in CPU memory.
    If this object is already in CPU memory, then no copy is performed and the original object is returned.
    """
    return self.to(_C.Device(_C.DeviceType.CPU))

def t(self):
    """
    Returns the transpose of the tensor.
    Aliased to transpose(0, 1) to ensure correct autograd behavior (TransposeBackward).
    """
    ndim = self.dim()
    if ndim > 2:
        raise RuntimeError(f"t() expects a tensor with <= 2 dimensions, but self is {ndim}D")
    if ndim < 2:
        return self
    return self.transpose(0, 1)

def type(self, dtype=None, non_blocking=False, **kwargs):
    """
    Returns the type if dtype is not provided, else casts this object to the specified type.
    """
    if dtype is None and not kwargs:
        device_str = ""
        if self.is_cuda:
            device_str = "cuda."
        
        dtype_map = {
            _C.float32: "FloatTensor",
            _C.float64: "DoubleTensor",
            _C.float16: "HalfTensor",
            _C.bfloat16: "BFloat16Tensor",
            _C.int32: "IntTensor",
            _C.int64: "LongTensor",
            _C.int16: "ShortTensor",
            _C.int8: "CharTensor",
            _C.uint8: "ByteTensor",
            _C.uint16: "UInt16Tensor",
            _C.uint32: "UInt32Tensor",
            _C.uint64: "UInt64Tensor",
            _C.complex32: "ComplexHalfTensor",
            _C.complex64: "ComplexFloatTensor",
            _C.complex128: "ComplexDoubleTensor",
            _C.bcomplex32: "BComplex32Tensor",
            _C.bool: "BoolTensor",
        }
        
        dt = self.dtype
        if dt in dtype_map:
            return f"tensorplay.{device_str}{dtype_map[dt]}"
        return f"tensorplay.{device_str}Tensor"
    
    return self.to(dtype, non_blocking=non_blocking, **kwargs)


Tensor.ndimension = ndimension
Tensor.long = long
Tensor.float = float
Tensor.int = int
Tensor.double = double
Tensor.cuda = cuda
Tensor.cpu = cpu
Tensor.t = t
Tensor.type = type




def unfold(self, dimension, size, step):
    """Returns a view of the original tensor which contains all slices of
    size :attr:`size` from :attr:`self` in the dimension :attr:`dimension`,

    view semantics, including 0-d inputs and error messages).
    """
    return _C.unfold(self, dimension, size, step)


Tensor.unfold = unfold


def register_hook(self, hook):
    """

    The hook is called every time a gradient with respect to this tensor is
    computed. It may modify the gradient by returning a replacement Tensor;
    returning ``None`` leaves the gradient unchanged. Hooks compose in
    registration order.

    Returns a :class:`~tensorplay.utils.hooks.RemovableHandle` whose
    ``remove()`` method (or context-manager form) unregisters the hook.
    """
    from tensorplay.utils.hooks import RemovableHandle

    if not self.requires_grad:
        raise RuntimeError(
            "cannot register a hook on a tensor that doesn't require gradient"
        )

    hooks_dict = OrderedDict()

    def _apply_hooks(grad):
        for h in list(hooks_dict.values()):
            result = h(grad)
            if result is not None:
                grad = result
        return grad

    if self.is_leaf or self.grad_fn is None:
        node = self._accumulate_grad_node
        if node is None:
            raise RuntimeError(
                "cannot register a hook on a tensor whose AccumulateGrad node "
                "has not been created yet; run a backward pass first"
            )
        pos = 0
    else:
        node = self.grad_fn
        pos = self._output_nr

    def pre_hook(grads):
        grad = grads[pos]
        new_grad = _apply_hooks(grad)
        if new_grad is grad:
            return grads
        out = list(grads)
        out[pos] = new_grad
        return out

    node.add_pre_hook(pre_hook)

    handle = RemovableHandle(hooks_dict)
    hooks_dict[handle.id] = hook
    return handle


Tensor.register_hook = register_hook


def register_post_accumulate_grad_hook(self, hook):
    """

    The hook runs after the gradient has been accumulated into ``self.grad``.
    It receives the tensor (the parameter) and its return value is ignored;
    unlike :meth:`register_hook` it cannot replace the gradient, but it may
    modify ``self.grad`` in place. Only leaf tensors that require grad and
    are used in the autograd graph support this hook.

    Returns a :class:`~tensorplay.utils.hooks.RemovableHandle`.
    """
    from tensorplay.utils.hooks import RemovableHandle

    if not self.requires_grad:
        raise RuntimeError(
            "cannot register a hook on a tensor that doesn't require gradient"
        )
    if not self.is_leaf:
        raise RuntimeError(
            "Registering a hook on a tensor that is not a leaf will "
            "cause an error"
        )

    hooks_dict = OrderedDict()

    def post_hook(_inputs, outputs):
        for h in list(hooks_dict.values()):
            h(self)
        return outputs

    node = self._accumulate_grad_node
    if node is None:
        raise RuntimeError(
            "cannot register a hook on a tensor whose AccumulateGrad node "
            "has not been created yet; run a backward pass first"
        )
    node.add_post_hook(post_hook)

    handle = RemovableHandle(hooks_dict)
    hooks_dict[handle.id] = hook
    return handle


Tensor.register_post_accumulate_grad_hook = register_post_accumulate_grad_hook


# ---------------------------------------------------------------------------
# The C++ binding takes a single sequence; normalize the variadic form here.
# ---------------------------------------------------------------------------
_orig_permute = Tensor.permute


def _permute(self, *dims):
    if len(dims) == 1 and isinstance(dims[0], (list, tuple)):
        dims = dims[0]
    return _orig_permute(self, list(dims))


Tensor.permute = _permute


# ---------------------------------------------------------------------------
# generated TensorMethods binding also names the parameter `size`, so
# t.expand(size=[2, 3]) is valid there; keep that surface here.
# ---------------------------------------------------------------------------
_orig_expand = Tensor.expand


def _expand(self, *args, size=None, implicit=False):
    if size is not None:
        if args:
            raise TypeError("expand() got multiple values for argument 'size'")
        size = size
    else:
        size = args
    if len(size) == 1:
        s0 = size[0]
        if isinstance(s0, (list, tuple)) or hasattr(s0, "__iter__"):
            size = tuple(s0)
    if implicit:
        return _orig_expand(self, list(size), implicit=True)
    return _orig_expand(self, list(size))


Tensor.expand = _expand


# ---------------------------------------------------------------------------
# __bool__ lives in the C extension (nb_bool slot on TensorBase): empty ->
# RuntimeError "no values is ambiguous", one element -> value != 0, more ->


def _norm_new_size(size):
    if not size:
        return None
    if len(size) == 1 and hasattr(size[0], "__iter__") and \
            not isinstance(size[0], _C.TensorBase):
        return [builtins_int(x) for x in size[0]]
    return [builtins_int(x) for x in size]


def _new_size(size, kwargs, name):
    if "size" in kwargs:
        if size:
            raise TypeError(f"{name}() got multiple values for argument 'size'")
        size = (kwargs.pop("size"),)
    if kwargs:
        unexpected = next(iter(kwargs))
        raise TypeError(
            f"{name}() got an unexpected keyword argument '{unexpected}'")
    return _norm_new_size(size)


def _flag(out, requires_grad):
    return out.requires_grad_(True) if requires_grad else out


_orig_new_empty = Tensor.new_empty
_orig_new_zeros = Tensor.new_zeros
_orig_new_ones = Tensor.new_ones
_orig_new_full = Tensor.new_full


def _new_zeros(self, *size, dtype=None, device=None, requires_grad=False,
               layout=None, pin_memory=None, **kwargs):
    shape = _new_size(size, kwargs, "new_zeros")
    if shape is None:
        raise TypeError("new_zeros() missing required argument: 'size'")
    out = _orig_new_zeros(self, shape, dtype=dtype, layout=layout,
                          device=device, pin_memory=pin_memory)
    return _flag(out, requires_grad)


def _new_ones(self, *size, dtype=None, device=None, requires_grad=False,
              layout=None, pin_memory=None, **kwargs):
    shape = _new_size(size, kwargs, "new_ones")
    if shape is None:
        raise TypeError("new_ones() missing required argument: 'size'")
    out = _orig_new_ones(self, shape, dtype=dtype, layout=layout,
                         device=device, pin_memory=pin_memory)
    return _flag(out, requires_grad)


def _new_full(self, size, fill_value, *, dtype=None, device=None,
              requires_grad=False, layout=None, pin_memory=None):
    out = _orig_new_full(self, size, fill_value, dtype=dtype, layout=layout,
                         device=device, pin_memory=pin_memory)
    return _flag(out, requires_grad)


def _new_empty(self, size, *, dtype=None, device=None, requires_grad=False,
               layout=None, pin_memory=None):
    if isinstance(size, builtins_int):
        size = [size]
    shape = _norm_new_size((size,))
    out = _orig_new_empty(self, shape, dtype=dtype, layout=layout,
                          device=device, pin_memory=pin_memory)
    return _flag(out, requires_grad)


def _new_tensor(self, data, *, dtype=None, device=None, requires_grad=False):
    out = _C.tensor(data, dtype=dtype or self.dtype,
                    device=device or self.device)
    return out.requires_grad_(requires_grad) if requires_grad else out


Tensor.new_zeros = _new_zeros
Tensor.new_ones = _new_ones
Tensor.new_full = _new_full
Tensor.new_empty = _new_empty
Tensor.new_tensor = _new_tensor


# ---------------------------------------------------------------------------
# as generated bindings; add the rest of the family.
# ---------------------------------------------------------------------------
_DTYPE_SHORTCUTS = {
    "bool": "bool",
    "byte": "uint8",
    "char": "int8",
    "short": "int16",
    "half": "float16",
    "bfloat16": "bfloat16",
    "cfloat": "complex64",
    "cdouble": "complex128",
}


def _make_dtype_shortcut(attr, dt_name):
    def op(self):
        return self.to(getattr(_C.DType, dt_name))
    op.__name__ = attr
    return op


for _attr, _dt in _DTYPE_SHORTCUTS.items():
    setattr(Tensor, _attr, _make_dtype_shortcut(_attr, _dt))
del _attr, _dt


# ---------------------------------------------------------------------------
# pointwise method forms routed through the top-level composites so the
# ---------------------------------------------------------------------------
def _cf(name):
    from . import _composite_funcs
    return getattr(_composite_funcs, name)


def _as_tensor(x, device=None):
    from . import as_tensor
    return as_tensor(x, device=device)


# floor_divide/remainder/fmod/true_divide are native methods; only the
# reflected forms need a wrapper, since the left operand arrives as a plain
# Python number.  The materialized number joins the right operand's device.
def __rfloordiv__(self, other):
    return _as_tensor(other, device=self.device).floor_divide(self)


def __rmod__(self, other):
    return _as_tensor(other, device=self.device).remainder(self)


Tensor.__rfloordiv__ = __rfloordiv__
Tensor.__rmod__ = __rmod__


def repeat_interleave(self, repeats, dim=None, *, output_size=None):
    return _cf("repeat_interleave")(self, repeats, dim=dim,
                                    output_size=output_size)


Tensor.repeat_interleave = repeat_interleave


# ---------------------------------------------------------------------------
# reduction faces returning plain tuples.  Restore (a) the elementwise binary
# form ``t.max(Tensor)`` and (b) the named-tuple contract with
# ---------------------------------------------------------------------------
_native_tensor_max = Tensor.max
_native_tensor_min = Tensor.min


def max(self, *args, **kwargs):
    if args and hasattr(args[0], "shape"):
        return _C.maximum(self, args[0])
    result = _native_tensor_max(self, *args, **kwargs)
    if ((args and args[0] is not None) or kwargs.get("dim") is not None) \
            and isinstance(result, tuple):
        from ._return_types import max_return_type
        return max_return_type(*result)
    return result


def min(self, *args, **kwargs):
    if args and hasattr(args[0], "shape"):
        return _C.minimum(self, args[0])
    result = _native_tensor_min(self, *args, **kwargs)
    if ((args and args[0] is not None) or kwargs.get("dim") is not None) \
            and isinstance(result, tuple):
        from ._return_types import min_return_type
        return min_return_type(*result)
    return result


Tensor.max = max
Tensor.min = min


def amax(self, dim=None, keepdim=False):
    from . import functional
    # an absent dim means the full reduction, spelled as the empty list
    return functional.amax(self, dim=[] if dim is None else dim, keepdim=keepdim)


def amin(self, dim=None, keepdim=False):
    from . import functional
    return functional.amin(self, dim=[] if dim is None else dim, keepdim=keepdim)


Tensor.amax = amax
Tensor.amin = amin


def count_nonzero(self, dim=None):
    nz = self.ne(0).to(_C.DType.int64)
    return nz.sum() if dim is None else nz.sum(dim=dim)


Tensor.count_nonzero = count_nonzero


def nonzero(self):
    from . import functional
    return functional.nonzero(self)


Tensor.nonzero = nonzero


def unique(self, sorted=True, return_inverse=False, return_counts=False,
           dim=None):
    from . import functional
    return functional.unique(self, sorted, return_inverse, return_counts,
                             dim)


Tensor.unique = unique


def unique_consecutive(self, return_inverse=False, return_counts=False,
                       dim=None):
    values, inverse, counts = _C.unique_consecutive(
        self, return_inverse, return_counts, dim)
    outs = [values]
    if return_inverse:
        outs.append(inverse)
    if return_counts:
        outs.append(counts)
    return outs[0] if len(outs) == 1 else tuple(outs)


Tensor.unique_consecutive = unique_consecutive


def searchsorted(self, sorted_sequence, *, out_int32=False, right=False,
                 side=None, sorter=None, out=None):
    """Insertion positions of `self` values in `sorted_sequence`."""
    from . import functional
    return functional.searchsorted(sorted_sequence, self, out_int32=out_int32,
                                   right=right, side=side, sorter=sorter,
                                   out=out)


Tensor.searchsorted = searchsorted


def bucketize(self, boundaries, *, out_int32=False, right=False, out=None):
    """Bucket indices of `self` against a sorted 1-D `boundaries` tensor."""
    from . import functional
    return functional.bucketize(self, boundaries, out_int32=out_int32,
                                right=right, out=out)


Tensor.bucketize = bucketize


def topk(self, k, dim=None, largest=True, sorted=True):
    ndim = self.dim()
    if ndim == 0:
        raise RuntimeError("topk(): expected a tensor with at least one dimension")
    d = ndim - 1 if dim is None else builtins_int(dim)
    if d < 0:
        d += ndim
    if d < 0 or d >= ndim:
        raise IndexError(f"topk(): dimension {dim} out of range for {ndim}-D tensor")
    k = builtins_int(k)
    n = self.size(d)
    if k < 0 or k > n:
        raise RuntimeError(
            f"selected index k: {k} out of range for dimension {d} "
            f"of size {n}")
    return _C.topk(self, k, d, bool(largest), bool(sorted), 0)


Tensor.topk = topk


# ---------------------------------------------------------------------------
# operator dunders: floor-div/mod reflect + bitwise family. Bitwise ops
# dispatch to the module functions (integers and bool; shifts take the
# bit width modulo through the unsigned domain in the kernel).
# ---------------------------------------------------------------------------
def __ifloordiv__(self, other):
    res = self.floor_divide(other)
    with _C_no_grad():
        self.copy_(res)
    return self


def __imod__(self, other):
    res = self.remainder(other)
    with _C_no_grad():
        self.copy_(res)
    return self


def _C_no_grad():
    from .autograd import no_grad
    return no_grad()


_BITWISE_NAMES = {
    "and": "bitwise_and",
    "or": "bitwise_or",
    "xor": "bitwise_xor",
    "lshift": "bitwise_left_shift",
    "rshift": "bitwise_right_shift",
}


def _bitwise_fn(name, reflect=False):
    def op(self, other):
        import tensorplay
        fn = getattr(tensorplay, _BITWISE_NAMES[name])
        if reflect:
            return fn(_as_tensor(other), self)
        return fn(self, other)

    return op


def _bitwise_inplace(name):
    def op(self, other):
        res = _bitwise_fn(name)(self, other)
        with _C_no_grad():
            self.copy_(res)
        return self

    return op


def __invert__(self):
    import tensorplay
    return tensorplay.bitwise_not(self)


def __pos__(self):
    return self


def __abs__(self):
    return self.abs()


Tensor.__floordiv__ = lambda self, other: self.floor_divide(other)
Tensor.__mod__ = lambda self, other: self.remainder(other)
Tensor.__ifloordiv__ = __ifloordiv__
Tensor.__imod__ = __imod__
Tensor.__and__ = _bitwise_fn("and")
Tensor.__rand__ = _bitwise_fn("and", reflect=True)
Tensor.__iand__ = _bitwise_inplace("and")
Tensor.__or__ = _bitwise_fn("or")
Tensor.__ror__ = _bitwise_fn("or", reflect=True)
Tensor.__ior__ = _bitwise_inplace("or")
Tensor.__xor__ = _bitwise_fn("xor")
Tensor.__rxor__ = _bitwise_fn("xor", reflect=True)
Tensor.__ixor__ = _bitwise_inplace("xor")
Tensor.__invert__ = __invert__
Tensor.__lshift__ = _bitwise_fn("lshift")
Tensor.__rlshift__ = _bitwise_fn("lshift", reflect=True)
Tensor.__ilshift__ = _bitwise_inplace("lshift")
Tensor.__rshift__ = _bitwise_fn("rshift")
Tensor.__rrshift__ = _bitwise_fn("rshift", reflect=True)
Tensor.__irshift__ = _bitwise_inplace("rshift")
Tensor.__pos__ = __pos__
Tensor.__abs__ = __abs__


def __complex__(self):
    return builtins_complex(self.item())


def __index__(self):
    return builtins_int(self.item())


Tensor.__complex__ = __complex__
Tensor.__index__ = __index__


# ---------------------------------------------------------------------------
# (cpu/cuda/to/pin_memory/record_stream) is bound natively; these are the
# remaining queries and aliases.
# ---------------------------------------------------------------------------
def _is_cpu(self):
    return str(self.device.type) == "cpu"


def _is_cuda(self):
    return str(self.device.type) == "cuda"


def _is_meta(self):
    return str(self.device.type) == "meta"


Tensor.is_cpu = property(_is_cpu)
Tensor.is_cuda = property(_is_cuda)
Tensor.is_meta = property(_is_meta)
del _is_cpu, _is_cuda


def xpu(self, device=None):
    if device is None:
        return self.to(_C.Device(_C.DeviceType.XPU))
    if builtins_int(device) == device and not isinstance(device, str):
        return self.to(_C.Device(_C.DeviceType.XPU, device))
    parsed = _C.Device(device)
    return self.to(parsed)


Tensor.xpu = xpu


# ---------------------------------------------------------------------------
# Element-count alias, equality predicate, and the complex/istft/sparse
# conversion method faces routed through the package functions.
# ---------------------------------------------------------------------------
def nelement(self) -> builtins_int:
    """Alias of numel()."""
    return self.numel()


def equal(self, other) -> _builtins.bool:
    """True if two tensors have the same size and elements, False otherwise."""
    from .functional import equal as _equal
    return _equal(self, other)


def istft(self, *args, **kwargs):
    from .functional import istft as _istft
    return _istft(self, *args, **kwargs)


def to_sparse_coo(self):
    """Convert a tensor to :ref:`coordinate format <sparse-coo-docs>`."""
    return self.to_sparse()


def module_load(self, other, assign=False):
    """Defines how ``other`` is remapped before being swapped with ``self``
    when a state dictionary is loaded into the owning module.

    Returns a new object that is neither ``self`` nor ``other``: the default
    is ``self.copy_(other).detach()`` unless ``assign`` selects the
    detached source directly.
    """
    if assign:
        return other.detach()
    return self.copy_(other).detach()


Tensor.nelement = nelement
Tensor.equal = equal
Tensor.istft = istft
Tensor.to_sparse_coo = to_sparse_coo
Tensor.module_load = module_load
del nelement, equal, istft, to_sparse_coo, module_load


# ---------------------------------------------------------------------------
# Dunder faces: legacy division spellings, sequence protocols, numpy
# interop, deepcopy, and the CUDA array view.
# ---------------------------------------------------------------------------
Tensor.__div__ = Tensor.__truediv__
Tensor.__rdiv__ = Tensor.__rtruediv__
Tensor.__idiv__ = Tensor.__itruediv__
Tensor.__long__ = Tensor.long
Tensor.__nonzero__ = Tensor.__bool__


def __reversed__(self):
    return self.flip(0)


def __contains__(self, element) -> _builtins.bool:
    """Check if `element` is present in tensor.

    Args:
        element (Tensor or scalar): element to be checked
            for presence in current tensor"
    """
    if isinstance(element, (Tensor, builtins_int, _builtins.float, _builtins.complex, _builtins.bool)):
        return bool((element == self).any().item())
    raise RuntimeError(
        f"Tensor.__contains__ only supports Tensor or scalar, but you passed in a {_builtins.type(element)}."
    )


def __rmatmul__(self, other):
    return _as_tensor(other, device=self.device).matmul(self)


def __deepcopy__(self, memo):
    if not self.is_leaf:
        raise RuntimeError(
            "Only Tensors created explicitly by the user "
            "(graph leaves) support the deepcopy protocol at the moment.  "
            "If you were attempting to deepcopy a module, this may be because "
            "of a tensorplay.nn.utils.weight_norm usage"
        )
    if id(self) in memo:
        return memo[id(self)]
    from .autograd import no_grad as _no_grad
    with _no_grad():
        new_tensor = self.clone()
        if self.is_conj():
            new_tensor = new_tensor.conj_physical()
        if self.is_neg():
            new_tensor = new_tensor.neg()
    if self.requires_grad:
        new_tensor.requires_grad_()
    if self.grad is not None:
        new_tensor.grad = self.grad.__deepcopy__(memo)
    memo[id(self)] = new_tensor
    return new_tensor


def __delitem__(self, key):
    raise TypeError("Tensor does not support deleting items")


def __array_wrap__(self, array):
    if array.dtype == _builtins.bool:
        # Workaround, tensorplay has no built-in bool tensor
        array = array.astype("uint8")
    return _C.from_numpy(array)


Tensor.__reversed__ = __reversed__
Tensor.__contains__ = __contains__
Tensor.__rmatmul__ = __rmatmul__
Tensor.__deepcopy__ = __deepcopy__
Tensor.__delitem__ = __delitem__
Tensor.__array_wrap__ = __array_wrap__
del __reversed__, __contains__, __rmatmul__, __deepcopy__, __delitem__, __array_wrap__

# Prefer tensor ops over numpy ones when numpy probes for a protocol winner.
Tensor.__array_priority__ = 1000

# CUDA devices are little-endian and tensors are stored in native byte
# order. 1-byte entries are endian-agnostic.
_DTYPE_TO_TYPESTR = {
    _C.complex64: "<c8",
    _C.complex128: "<c16",
    _C.bfloat16: "<V2",
    _C.float16: "<f2",
    _C.float32: "<f4",
    _C.float64: "<f8",
    _C.uint8: "|u1",
    _C.int8: "|i1",
    _C.uint16: "<u2",
    _C.int16: "<i2",
    _C.uint32: "<u4",
    _C.int32: "<i4",
    _C.uint64: "<u8",
    _C.int64: "<i8",
    _C.bool: "|b1",
}


def _cuda_array_interface(self):
    """Array view description for cuda tensors.

    See:
    https://numba.pydata.org/numba-doc/dev/cuda/cuda_array_interface.html
    """
    # raise AttributeError for unsupported tensors, so that
    # hasattr(cpu_tensor, "__cuda_array_interface__") is False.
    if not self.is_cuda:
        raise AttributeError(
            f"Can't get __cuda_array_interface__ on non-CUDA tensor type: {self.type()} "
            "If CUDA data is required use tensor.cuda() to copy tensor to device memory."
        )

    if self.is_sparse:
        raise AttributeError(
            f"Can't get __cuda_array_interface__ on sparse type: {self.type()} "
            "Use Tensor.to_dense() to convert to a dense tensor first."
        )

    # RuntimeError, matching tensor.__array__() behavior.
    if self.requires_grad:
        raise RuntimeError(
            "Can't get __cuda_array_interface__ on Variable that requires grad. "
            "If gradients aren't required, use var.detach() to get Variable that doesn't require grad."
        )

    typestr = _DTYPE_TO_TYPESTR[self.dtype]
    itemsize = self.element_size()
    shape = tuple(self.shape)
    if self.is_contiguous():
        # __cuda_array_interface__ v2 requires the strides to be omitted
        # (either not set or set to None) for C-contiguous arrays.
        strides = None
    else:
        strides = tuple(s * itemsize for s in self.stride())
    data_ptr = self.data_ptr() if self.numel() > 0 else 0
    data = (data_ptr, False)  # read-only is false

    return dict(typestr=typestr, shape=shape, strides=strides, data=data, version=2)


Tensor.__cuda_array_interface__ = property(_cuda_array_interface)
Ask DeepWiki