fn

tensor

Tensor
tensor(data: object, dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False)
source

Construct a new Tensor from Python data, a NumPy array, or another Tensor.

Always allocates a fresh storage and copies the source bytes into Lucid-owned memory. This is the canonical entry point for creating tensors from heterogeneous Python inputs: scalars (int / float / bool), nested lists, NumPy ndarray\s, and existing Lucid Tensor\s. Dtype is inferred from the source unless dtype is given; device defaults to the global default (typically "cpu") unless overridden.

Parameters

dataobject
Source data. Accepted forms:
  • Python scalar (int, float, bool) — produces a 0-d tensor.
  • Nested list / tuple — recursively converted; element type must be uniform.
  • numpy.ndarray — bridge boundary (see lucid._factories.converters); the data is copied regardless of the source array's contiguity.
  • Existing Tensor — copied to a new buffer (use as_tensor to avoid the copy when dtype/device match).
dtypedtype | str | None
Target element type. None (default) infers from data: integers → int64, floats → float32, complex → complex64.
devicedevice | str | None
Target device ("cpu" or "metal"). None uses lucid.get_default_device.
requires_gradbool
Whether the resulting tensor should record autograd operations. Defaults to False.

Returns

Tensor

A freshly-allocated Lucid tensor.

Notes

This factory is one of the six "bridge" entry points in the H4 rule — the only places where external libraries (NumPy here) may legitimately cross into Lucid's compute path. Outside the bridges, Lucid composites must use engine primitives directly.

For zero-copy conversion when the source is already an ndarray on CPU and shares dtype, prefer as_tensor.

Examples

>>> import lucid
>>> lucid.tensor([1.0, 2.0, 3.0])
Tensor([1., 2., 3.])
>>> lucid.tensor([[1, 2], [3, 4]], dtype=lucid.float32)
Tensor([[1., 2.],
        [3., 4.]])
>>> import numpy as np
>>> lucid.tensor(np.arange(6).reshape(2, 3))
Tensor([[0, 1, 2],
        [3, 4, 5]])