Skip to main content

High-performance machine learning library with lazy evaluation and automatic kernel fusion

Project description

zyx - Python Bindings

PyPI version License: LGPL 3.0

zyx is a high-performance machine learning library for Python, powered by Rust. It features lazy evaluation, automatic kernel fusion, and multi-backend support (CPU, GPU via OpenCL/CUDA/WebGPU).

Note: Install with pip install zyx-py, then import as import zyx

Installation

pip install zyx-py

Requirements:

  • Python 3.5+
  • Linux x86_64 (more platforms coming soon)

Quick Start

import zyx

# Create tensors
x = zyx.Tensor([1.0, -2.0, 3.0])
y = zyx.Tensor([4.0, 5.0, 6.0])

# Lazy evaluation - operations build a graph
z = x.relu() + y

# Realize to compute the result
z.realize()
print(z.numpy())  # [5.0, 5.0, 9.0]

# Random tensors
a = zyx.Tensor.randn(3, 3)
b = zyx.Tensor.randn(3, 3)
c = a @ b  # Matrix multiplication
c.realize()

Key Features

  • Lazy Evaluation - Operations accumulate until realize(), reducing temporary allocations
  • Kernel Fusion - Multiple operations compile into single optimized GPU kernels
  • Immutable Tensors - No in-place modification errors common in other frameworks
  • Explicit Gradient Tape - Control what's recorded, no no_grad() semantics needed
  • Arbitrary-Order Gradients - Native support for 2nd, 3rd, and higher-order derivatives
  • Cross-Platform - OpenCL (CPU/GPU), WebGPU, CUDA/ROCm backends

Tensor Operations

Creation

import zyx

# From Python lists
t1 = zyx.Tensor([1, 2, 3])

# Random initialization
t2 = zyx.Tensor.randn(100, 100)
t3 = zyx.Tensor.rand((50, 50), dtype=zyx.DType.F32)

# Constant tensors
zeros = zyx.Tensor.zeros(10, 10)
ones = zyx.Tensor.ones(10, 10)
eye = zyx.Tensor.eye(5)

# Full tensor with specific value
filled = zyx.Tensor.full((3, 3), 2.5)

# With numpy arrays
import numpy as np
arr = np.array([[1, 2], [3, 4]])
t = zyx.Tensor(arr)

Math Operations

x = zyx.Tensor.randn(100, 100)

# Unary operations
y = x.relu()
y = x.sigmoid()
y = x.tanh()
y = x.gelu()
y = x.softmax(dim=-1)
y = x.log_softmax(dim=-1)

# Binary operations
a = zyx.Tensor.randn(100, 100)
b = zyx.Tensor.randn(100, 100)
c = a + b
c = a - b
c = a * b
c = a / b
c = a.matmul(b)  # Matrix multiplication

# Reduction operations
mean = x.mean(dim=0)
sum_val = x.sum(dim=1)
max_val = x.max(dim=-1)
min_val = x.min(dim=-1)
argmax = x.argmax(dim=-1)
argmin = x.argmin(dim=-1)

# Shape operations
y = x.reshape(10, 10)
y = x.transpose(0, 1)
y = x.permute(1, 0, 2)
y = x.squeeze()
y = x.unsqueeze(0)

# Realize to get numpy array
result = y.numpy()

Automatic Differentiation

import zyx

# Create a gradient tape
tape = zyx.GradientTape()

# Forward pass
x = zyx.Tensor([1.0, 2.0, 3.0])
w = zyx.Tensor([0.5, 0.5, 0.5])
y = (x * w).sum()

# Compute gradients
grads = tape.gradient(y, [w])
print(grads[0].numpy())  # [1.0, 2.0, 3.0]

# Higher-order gradients
tape2 = zyx.GradientTape()
y = x.relu().sum()
grads = tape2.gradient(y, [x])

Neural Network Modules

import zyx
import zyx.nn as nn

# Linear layer
linear = nn.Linear(in_features=128, out_features=64)

# Convolutional layer
conv = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=(3, 3))

# Normalization layers
ln = nn.LayerNorm(normalized_shape=(128,))
bn = nn.BatchNorm(num_features=64)
gn = nn.GroupNorm(num_groups=8, num_channels=64)
rn = nn.RMSNorm(dim=128)

# Activation functions
x = zyx.Tensor.randn(10, 128)
y = x.relu()
y = x.gelu()
y = x.silu()

# Multihead attention
attn = nn.MultiheadAttention(embed_dim=512, num_heads=8)
query = zyx.Tensor.randn(32, 128, 512)  # batch, seq, embed
key = zyx.Tensor.randn(32, 128, 512)
value = zyx.Tensor.randn(32, 128, 512)
output, _ = attn(query, key, value)

# Transformer layers
encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8)
transformer = nn.TransformerEncoder(encoder_layer, num_layers=6)

Optimizers

import zyx
import zyx.nn as nn
import zyx.optim as optim

# Create model
model = nn.Linear(10, 5)

# Create optimizer
optimizer = optim.Adam(learning_rate=0.001)
# or: optim.SGD(learning_rate=0.01, momentum=0.9)
# or: optim.AdamW(learning_rate=0.001, weight_decay=0.01)
# or: optim.RMSprop(learning_rate=0.001)

# Training loop
tape = zyx.GradientTape()
x = zyx.Tensor.randn(32, 10)
target = zyx.Tensor.randn(32, 5)

# Forward
pred = model(x)
loss = ((pred - target) ** 2).mean()

# Backward
grads = tape.gradient(loss, model.get_params())
optimizer.update(model, grads)

# Realize all pending computations
zyx.Tensor.realize_all()

Training Example

import zyx
import zyx.nn as nn
import zyx.optim as optim

# Set random seed
zyx.manual_seed(42)

# Create model
class SimpleNet:
    def __init__(self):
        self.fc1 = nn.Linear(784, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = self.fc1(x).relu()
        x = self.fc2(x)
        return x

    def get_params(self):
        return self.fc1.get_params() + self.fc2.get_params()

    def set_params(self, params):
        self.fc1.set_params(params[:2])
        self.fc2.set_params(params[2:])

model = SimpleNet()
optimizer = optim.Adam(learning_rate=0.001)

# Training loop
for epoch in range(10):
    tape = zyx.GradientTape()

    # Dummy data
    x = zyx.Tensor.randn(64, 784)
    y = zyx.Tensor.randint(0, 10, (64,))

    # Forward pass
    logits = model.forward(x)
    loss = logits.cross_entropy(y)

    # Backward pass
    grads = tape.gradient(loss, model.get_params())
    optimizer.update(model, grads)

    # Compute accuracy
    pred = logits.argmax(dim=-1)
    acc = (pred == y).float().mean()

    zyx.Tensor.realize_all()
    print(f"Epoch {epoch}, Loss: {loss.numpy():.4f}, Acc: {acc.numpy():.4f}")

Data Types

import zyx

# Supported dtypes
zyx.DType.F32    # 32-bit float
zyx.DType.F64    # 64-bit float
zyx.DType.F16    # 16-bit float
zyx.DType.BF16   # BFloat16
zyx.DType.I8     # 8-bit int
zyx.DType.I16    # 16-bit int
zyx.DType.I32    # 32-bit int
zyx.DType.I64    # 64-bit int
zyx.DType.U8     # 8-bit uint
zyx.DType.U16    # 16-bit uint
zyx.DType.U32    # 32-bit uint
zyx.DType.U64    # 64-bit uint
zyx.DType.Bool   # Boolean

# Create tensor with specific dtype
t = zyx.Tensor([1, 2, 3], dtype=zyx.DType.F32)

Device Support

zyx automatically selects the best available backend:

  • OpenCL - CPU and GPU support via POCL or vendor drivers
  • WebGPU - Modern cross-platform GPU API
  • CUDA/ROCm - NVIDIA and AMD GPU support

To check or set the runtime backend, use environment variables:

export ZYX_BACKEND=opencl    # Use OpenCL backend
export ZYX_BACKEND=wgpu      # Use WebGPU backend
export ZYX_BACKEND=cuda      # Use CUDA backend

Debug Options

Enable debug output with the ZYX_DEBUG environment variable:

ZYX_DEBUG=1    # Print hardware devices
ZYX_DEBUG=2    # Print performance info
ZYX_DEBUG=4    # Print kernel scheduling
ZYX_DEBUG=8    # Print kernel IR
ZYX_DEBUG=16   # Print native assembly

Combine flags: ZYX_DEBUG=18 (2 + 16) for perf + assembly output.

Why zyx?

Feature zyx PyTorch
Gradient recording Explicit GradientTape Implicit, requires no_grad()
Tensor mutability Immutable (no in-place errors) Mutable (can cause back-prop failures)
Higher-order gradients Arbitrary order natively Supported but more complex
Kernel fusion Automatic via lazy graph Manual or via torch.compile
Disk I/O Lazy loading parallel to compute Typically blocking

License

LGPL-3.0-only - see LICENSE file for details.

Links

Status

Experimental - API is stabilizing, performance under active optimization. Not production-ready yet.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

zyx_py-0.15.5-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

zyx_py-0.15.5-cp314-cp314-macosx_11_0_arm64.whl (3.4 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

zyx_py-0.15.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

zyx_py-0.15.5-cp313-cp313-macosx_11_0_arm64.whl (3.4 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

zyx_py-0.15.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

zyx_py-0.15.5-cp312-cp312-macosx_11_0_arm64.whl (3.4 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

zyx_py-0.15.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

zyx_py-0.15.5-cp311-cp311-macosx_11_0_arm64.whl (3.4 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

zyx_py-0.15.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

zyx_py-0.15.5-cp310-cp310-macosx_11_0_arm64.whl (3.4 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

zyx_py-0.15.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

zyx_py-0.15.5-cp39-cp39-macosx_11_0_arm64.whl (3.4 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

zyx_py-0.15.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

zyx_py-0.15.5-cp38-cp38-macosx_11_0_arm64.whl (3.4 MB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

File details

Details for the file zyx_py-0.15.5-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zyx_py-0.15.5-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a9103f0d33ba6c0dae3cb10dbe2b4b26bd238e815c721a76d3d4cd7e6b2d3fb9
MD5 21f16e1c9312ca46e5aeea45450ccdc5
BLAKE2b-256 684c37e89338562e051bc1046edad88089e7efa937d3910ecd79a83ef5bf5878

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyx_py-0.15.5-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-wheels.yml on zk4x/zyx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zyx_py-0.15.5-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zyx_py-0.15.5-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d07bd7c1005f0d302869a5ed551c2039d10940113d45bcc07f568ab89e462a90
MD5 55940ff4a0ad7ef7e2695bc6141e0e2b
BLAKE2b-256 5cc7189afb398c6fd56fdb3a783b81bad1e3555724c70cb4f8d5b117d62db5d4

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyx_py-0.15.5-cp314-cp314-macosx_11_0_arm64.whl:

Publisher: build-wheels.yml on zk4x/zyx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zyx_py-0.15.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zyx_py-0.15.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 766888fa0da9fedf64e71cd85cec3e475df955ed43bbe41faae3d89ee3c36ed1
MD5 cd297edd0547538cd579a7ee38cf8a86
BLAKE2b-256 7bc55db23bb1aecd1e440edf9f86778d0eee36f68d080d59c161f2fa700cc9c3

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyx_py-0.15.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-wheels.yml on zk4x/zyx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zyx_py-0.15.5-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zyx_py-0.15.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d7ac4e256750e133c4f330c02f347fbad7d03ce91ca181cb1c154da245799f86
MD5 42eedd6b4bb6c3f4bdaa60d8f0b89a54
BLAKE2b-256 bca5a5cdab9727c304b7be73cd8bfd907f199138f61ee0d172897013fd82b271

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyx_py-0.15.5-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: build-wheels.yml on zk4x/zyx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zyx_py-0.15.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zyx_py-0.15.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ae844ad016e48d2ef163fba0576561044fb586a3382bd8cf3f24f8cf5c2f6a10
MD5 8d4ce4bb02422b015923c90686c79218
BLAKE2b-256 9216b09e3f29e7424b023a6aa98c2b47df1495da9cc42381d15961032af99cca

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyx_py-0.15.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-wheels.yml on zk4x/zyx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zyx_py-0.15.5-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zyx_py-0.15.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7bdeea21f060cbff49d8a8a431b67ce47e2509cb1f16d8562f38116b5234ee72
MD5 322c677ad5ea6ad2922bed1240572c6c
BLAKE2b-256 767daf037c0d8a06b34158afa2999e481d41cd8bffcefa8677030a893dbfd8ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyx_py-0.15.5-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: build-wheels.yml on zk4x/zyx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zyx_py-0.15.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zyx_py-0.15.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 88ec33ae70dfc160cf037f636d8984b1c791584809e84f0bb03c7bdb8a9ed298
MD5 b0c54db585706c33255df69fe7bfd77e
BLAKE2b-256 35373d4aac15390e23e66f91f7ab7572e11c1b48a5c8ae5febb3ac01b4b5bd3f

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyx_py-0.15.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-wheels.yml on zk4x/zyx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zyx_py-0.15.5-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zyx_py-0.15.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bafec934137cbee40c1fdf678fbbb21b9720f04a022c4f8e0b3348d1ff3faa09
MD5 4dad88e48644ebd825c411fb06bafb73
BLAKE2b-256 00ae9d142a1b857c4bdc6f8e164f304d8cb9e0bec6bdcab1f8f153eab1359516

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyx_py-0.15.5-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: build-wheels.yml on zk4x/zyx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zyx_py-0.15.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zyx_py-0.15.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8883bc0e2461f4913ff742fbd4b627019cd3f6144ff6717eeea58000f5b1add0
MD5 fa1058343b3fc663c2fdaae61956c97b
BLAKE2b-256 bc8b196c38614b2c03c2efd5cae3e8de1aff30f9bb01e67ae57877bd434f4788

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyx_py-0.15.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-wheels.yml on zk4x/zyx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zyx_py-0.15.5-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zyx_py-0.15.5-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6883c3d0c85cda2030eef521b750b07aa47a16ab50aeab40bada75cbfc876266
MD5 457dd4a54393ffdd923b464111bdc6b6
BLAKE2b-256 eeccc1a3d9e0e7456046ed568b7b4907258b28be3534eece45944271a386e5fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyx_py-0.15.5-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: build-wheels.yml on zk4x/zyx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zyx_py-0.15.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zyx_py-0.15.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 feec87c9ac34978e4f5cfb6c07bbe5f48ba74d729e069b60436982603087d575
MD5 c068188a53c53e507877804cb64c5a3d
BLAKE2b-256 b7b540e4f84e3ed02bdd445a06613cfb520ac9ba15a555d3066aa41299105924

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyx_py-0.15.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-wheels.yml on zk4x/zyx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zyx_py-0.15.5-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zyx_py-0.15.5-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1d64783b7dd6e941c83ce27b6449ee91eba6cecffa77c463c49059808845c662
MD5 434ff8784e39f8bd355c3f06d03d8b6a
BLAKE2b-256 fb50efa8072cbc2caf1e79465cc3dc871c106e5f9192ede9f44c77499ee0783b

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyx_py-0.15.5-cp39-cp39-macosx_11_0_arm64.whl:

Publisher: build-wheels.yml on zk4x/zyx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zyx_py-0.15.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zyx_py-0.15.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8c13ad400d4cd0ee933e2362bb82eeb509097d65fe369be683f854329b54fc20
MD5 a46d158b2f244744a144be2c0f5677af
BLAKE2b-256 c5c6c43387133d7eefd2234cddceeb128a2ccef1f26d54396585c0771c307403

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyx_py-0.15.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-wheels.yml on zk4x/zyx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zyx_py-0.15.5-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zyx_py-0.15.5-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c47d0267f44c5a9091ffe286256cb3b5cff5db56475a3c83a1b2f25b9cff9f89
MD5 f7d3e1421e44828838d6520e07d9e6b8
BLAKE2b-256 ab7b921812097dc3f047e8bb7e57458c42a587d8f99f538ba6b50e581a3ba547

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyx_py-0.15.5-cp38-cp38-macosx_11_0_arm64.whl:

Publisher: build-wheels.yml on zk4x/zyx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page