Your first tensor
After installation, save this as first_tensor.py in
the source checkout:
import tensorcx as cx
device = cx.best_device()x = cx.ones((1_000_000,), dtype=cx.float32, device=device)y = cx.ones((1_000_000,), dtype=cx.float32, device=device)
z = x + yprint(z.cpu().numpy()[:5])# [2. 2. 2. 2. 2.]Run it in the installed environment:
uv run --no-sync python first_tensor.pyExpected output:
[2. 2. 2. 2. 2.]What happens
Section titled “What happens”best_device() selects an available accelerator, falling back to CPU. Each
ones call allocates a contiguous tensor on that device. Addition executes
synchronously and returns a new tensor. .cpu().numpy() copies the result to
the CPU as needed and returns a NumPy array.
For a reproducible CPU baseline, change device = cx.best_device() to
device = "cpu". Constructors without a device argument also default to CPU.
Device selection is explicit
Section titled “Device selection is explicit”Use cx.devices() to list available backend names and cx.is_available("cuda")
or cx.is_available("metal") before selecting an accelerator. Use
tensor.to("cuda") or tensor.to("metal") to transfer a tensor. Only device
index 0 is currently supported.
Binary operations accept broadcast-compatible shapes with matching dtypes and
devices. Use astype to convert dtype explicitly; operations never move inputs
between devices. Read tensor operations
for the next steps.