Skip to main content

ferrotensor (Python)

Python bindings for ferrotensor, a pure-Rust ML framework: PyTorch-style tensors, autograd, nn layers and optimizers that run on the CPU or on a GPU through wgpu (Metal, Vulkan, DX12) with hand-written WGSL kernels. No CUDA, no NumPy, no other runtime dependencies.

Install

Wheels are built for macOS arm64 and Linux x86_64 (manylinux), as abi3 wheels that work on CPython 3.10 and newer:

pip install ferrotensor

On other platforms pip builds from the source distribution, which needs a Rust toolchain. To build from a checkout of the repository:

pip install ./ferrotensor-py

For development, maturin develop installs an editable build into the active virtual environment:

cd ferrotensor-py
pip install maturin pytest
maturin develop
pytest

Quickstart

import ferrotensor as ft
import ferrotensor.nn.functional as F
from ferrotensor import nn, optim

# Tensors and autograd
x = ft.tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True)
y = (x @ x).relu().sum()
y.backward()
print(x.grad)  # tensor([[7.0, 11.0], [9.0, 13.0]])

# A model, a loss and an optimizer: learn XOR
ft.manual_seed(0)  # fixes the initial weights, so the output below repeats
model = nn.Sequential(nn.Linear(2, 16), nn.Tanh(), nn.Linear(16, 2))
opt = optim.AdamW(model.parameters(), lr=0.05, weight_decay=0.0)
inputs = ft.tensor([[0, 0], [0, 1], [1, 0], [1, 1]])
targets = [0, 1, 1, 0]
for step in range(200):
    opt.zero_grad()
    loss = F.cross_entropy(model(inputs), targets)
    loss.backward()
    opt.step()
print(loss.item() < 0.05)  # True
print(model(inputs).argmax(dim=1).tolist())  # [0.0, 1.0, 1.0, 0.0]

# Save and load parameters (safetensors)
model.save("xor.safetensors")
model.load("xor.safetensors")

examples/mlp.py trains a small MLP on two spirals with a warmup + cosine learning-rate schedule, then saves and reloads it:

python examples/mlp.py
python examples/mlp.py --device gpu

Devices

Tensors and layers take a device= argument: a ft.Device, "cpu", "gpu" or "best" (the GPU when there is one). The default is the CPU.

import ferrotensor as ft

print(ft.Device.gpu_available())
dev = ft.Device("best")
a = ft.randn(256, 256, device=dev)
b = a.to("cpu")

ft.Device("gpu") raises RuntimeError when wgpu finds no adapter. Tensors on different devices cannot be mixed in one op (ValueError); move them with .to(device).

What is covered

  • Tensor: creation (tensor, zeros, ones, full, randn, rand, arange, linspace, eye, *_like), operators + - * / ** @ and comparisons with broadcasting, int/slice indexing, reductions (sum, mean, max, min, var, argmax, argmin), shape ops (reshape, view, flatten, squeeze, unsqueeze, permute, transpose, T, expand, narrow, chunk, split, index_select), cat, stack, where, element-wise math and activations, softmax, log_softmax.
  • Autograd: requires_grad, backward(), .grad, detach(), no_grad (context manager or decorator).
  • nn: Module, Sequential, ModuleList, Linear, Embedding, LayerNorm, RMSNorm, Dropout, MultiHeadAttention, ReLU, LeakyReLU, GELU, SiLU, Sigmoid, Tanh, Softmax, Flatten, Identity, state_dict() / load_state_dict().
  • nn.functional: mse_loss, l1_loss, smooth_l1_loss, cross_entropy (class indices, with negative indices ignored, or probabilities), nll_loss, binary_cross_entropy(_with_logits), layer_norm, rms_norm, linear, dropout, scaled_dot_product_attention, one_hot, accuracy, cosine_similarity.
  • optim: SGD (momentum, Nesterov, weight decay), AdamW, clip_grad_norm, clip_grad_value, and the schedules ConstantLR, StepLR, CosineAnnealingLR, WarmupCosineLR, WarmupLinearLR.
  • save / load: dicts of tensors as safetensors (loads F32, F16, BF16 and F64 as float32).

Bad arguments raise ValueError, IndexError or TypeError with a message naming the shapes involved; the Rust code never panics across the boundary.

Differences from PyTorch

  • Every tensor is float32. Index tensors (argmax, class targets) hold integral float values.
  • nn.Linear.weight has shape [in_features, out_features]: the layer computes x @ weight + bias.
  • Tensor.max(dim) / min(dim) return values only; use argmax(dim) for indices. var() is the population variance (divides by n).
  • == compares identity (so tensors stay hashable); use a.eq(b) for an element-wise comparison.
  • Schedulers set optimizer.lr for step 0 when created; call scheduler.step() after each optimizer.step().

Tests

pytest                              # CPU
FERROTENSOR_RUN_GPU_TESTS=1 pytest  # also GPU parity tests

FERROTENSOR_GPU_TESTS=1 still works as the older name. CI runs the GPU tests on software Vulkan (Mesa lavapipe).

License

MIT or Apache-2.0, at your option.

Metadata

Release files for ferrotensor 0.7.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ferrotensor 0.7.3
File Size Uploaded Provenance Actions
ferrotensor-0.7.3.tar.gz 178.8 kB With provenance Details for ferrotensor-0.7.3.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for ferrotensor 0.7.3
File Interpreter ABI Platform Actions
ferrotensor-0.7.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details for ferrotensor-0.7.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
ferrotensor-0.7.3-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details for ferrotensor-0.7.3-cp310-abi3-macosx_11_0_arm64.whl

Total release size: 5.9 MB

Release files / ferrotensor-0.7.3.tar.gz

Download URL ferrotensor-0.7.3.tar.gz
Size 178.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e5d6b9bcff3c42d091a0b4694c203cc8dfc05360df077a6a36597dd1f59413d8
BLAKE2b-256 checksum
How to use checksums
efa1ed70bc39dfcd304553324261a29454bbec9a722f2127c73bd75946436467
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

Release files / ferrotensor-0.7.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL ferrotensor-0.7.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 3.2 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
2088944b1cfa0149e76600fc76a8a85cad8ca89d3d69bc11853d3d195991dcb1
BLAKE2b-256 checksum
How to use checksums
e17ca136ac67467c640b28677688c72c9ceee33724f613c2675981ff981458bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

Release files / ferrotensor-0.7.3-cp310-abi3-macosx_11_0_arm64.whl

Download URL ferrotensor-0.7.3-cp310-abi3-macosx_11_0_arm64.whl
Size 2.5 MB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c2cb635d0434e7745dfdf1c4b37600c2ff9a09ec60ff46d0272f56750ebc6c0e
BLAKE2b-256 checksum
How to use checksums
b0fae397278d33dc74b6bca9de5c6d628d0ea9e16a3198decfae92099f41ecec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

Security for ferrotensor 0.7.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 3 files in this release have been uploaded with provenance metadata.

Select from 3 files

Attestation for ferrotensor-0.7.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page