Skip to main content

MecanTensor: Universal Hardware-Accelerated Tensor Engine with LGC, SSD Infusion, FluxBits, QSBits, MidBits, HAL, and HLAS

Project description

MecanTensor v2.1

Universal Hardware-Accelerated Tensor Engine for ASI Training

MecanTensor is a from-scratch tensor computation library with a C++ backend and Python interface, featuring three novel sub-bit quantization engines that push beyond the Shannon limit, alongside integrated memory-optimized training components.

Installation

pip install -e .

Quick Start

import mecantensor as mt
import numpy as np

# Discover all hardware
mt.hal.print_devices()

# Matrix multiplication (routes through C++ HLAS engine)
A = np.random.randn(512, 1024).astype(np.float32)
B = np.random.randn(1024, 256).astype(np.float32)
C = mt.ops.matmul(A, B)

# Save / Load in .mt format
mt.io.save(C, "output.mt")
loaded = mt.io.load("output.mt")

1. Logical Gradient Compressor (LGC)

LGC is a cache-bounded optimizer that leverages 8-bit gradient quantization, 1-bit sign momentum, and uint8 curvature tracking to reduce training memory overhead.

import mecantensor as mt
import numpy as np

# Define parameters (must have requires_grad=True)
class Parameter:
    def __init__(self, data):
        self.data = np.array(data, dtype=np.float32)
        self.grad = None
        self.requires_grad = True

params = [Parameter(np.random.randn(1024, 1024))]

# Instantiate LGC
# For models > 1B parameters, set total_model_params and provide a cache_dir to enable SSD sharding
optimizer = mt.lgc.LogicalGradientCompressor(
    params,
    lr=1e-3,
    total_model_params=50e9,       # Simulated 50B parameter model
    cache_dir="./optimizer_cache"  # Enable SSD infusion for offloaded states
)

# Training loop step
params[0].grad = np.random.randn(1024, 1024).astype(np.float32)
optimizer.step()

# Clean up states when done
optimizer.cleanup()

2. SSD Infusion Engine

The Infusion Engine performs selective tensor offload to SSD for models that exceed available RAM, employing memory-mapped files and circular LRU eviction to respect a hard budget ceiling.

import mecantensor as mt
import numpy as np

# Initialize the SSD Infusion Engine
engine = mt.infusion.SSDInfusionEngine(
    cache_directory="./infusion_cache",
    total_model_params=50e9  # Sets budget ceiling (e.g., 10 GB limit for 50B params)
)

# Offload a tensor to disk
tensor_data = np.random.randn(2048, 2048).astype(np.float32)
engine.offload_tensor("layer_12_weights", tensor_data)

# Prefetch and retrieve the tensor back via zero-copy memory-map
engine.hint_prefetch("layer_12_weights")
weights = engine.retrieve_tensor("layer_12_weights")

# Clean up all active memory maps and temporary cache files
engine.cleanup()

Subsystems

Module Description Precision
mt.hal Hardware Abstraction Layer — probes CPU, GPU, NPU, FPGA
mt.hlas Hardware Linear Algebra Subroutines (sgemm, add) FP32
mt.ops Core math (matmul, add, bitlinear, flash attention) FP32
mt.fluxbits Bloom Tensor Engine — AND + POPCOUNT, no FP multiply 0.45-bit
mt.qsbits Binary XNOR+POPCOUNT Engine — 64 synapses/clock 1-bit
mt.midbits Block-Palette LUT Engine — 16-element blocks 0.75-bit
mt.io .mt paged serialization (save/load/stream)
mt.tensor Low-level C++ tensor bridge via ctypes
mt.lgc Logical Gradient Compressor (LGC) optimizer 8-bit Grad, 1-bit Mom
mt.infusion SSD Infusion Engine (selective tensor offloader)

Architecture

Python Layer (mecantensor/)
    ├── hal.py        → src/hal/discovery.py (OpenCL/Vulkan/CUDA probe)
    ├── hlas.py       → _native.py → mecantensor_40.dll (C++ HLAS engine)
    ├── ops.py        → _native.py → mecantensor_40.dll (matmul, add, etc)
    ├── fluxbits.py   → Python fallback + C++ (when compiled)
    ├── qsbits.py     → Python fallback + C++ (when compiled)
    ├── midbits.py    → Python fallback + C++ (when compiled)
    ├── io.py         → _native.py → mecantensor_40.dll (.mt format)
    ├── tensor.py     → _native.py → mecantensor_40.dll (raw tensor handle)
    ├── lgc/          → Logical Gradient Compressor optimizer subpackage
    └── infusion/     → SSD Infusion Engine subpackage

C++ Layer (src/)
    ├── hal/          → Hardware discovery & backend registry
    ├── hlas/         → CPU/GPU/NPU/Hybrid linear algebra engines
    ├── fluxbits/     → Bloom tensor compiler & AVX2 kernel
    ├── qsbits/       → XNOR + POPCOUNT binary engine
    ├── midbits/      → 0.75-bit LUT encoder & decoder
    ├── ops/          → math, attention, bitlinear, conv, TSI
    ├── io/           → .mt paged serialization
    └── autograd/     → Tape-based automatic differentiation

Running Tests

python test_package.py

License

Proprietary — MecanLabs

Project details


Download files

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

Source Distribution

mecantensor-2.1.0.tar.gz (249.1 kB view details)

Uploaded Source

Built Distribution

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

mecantensor-2.1.0-py3-none-any.whl (258.5 kB view details)

Uploaded Python 3

File details

Details for the file mecantensor-2.1.0.tar.gz.

File metadata

  • Download URL: mecantensor-2.1.0.tar.gz
  • Upload date:
  • Size: 249.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for mecantensor-2.1.0.tar.gz
Algorithm Hash digest
SHA256 085fe4ba0e00c010a60a5aa0b148c94dbc6461e11ed3ee010ec33f8e897ec88d
MD5 8fd3c8834d51829cd71121206e47fd30
BLAKE2b-256 e47cb2243e2894c3885558ba3d611832583b9f168eb1f5a37c01612321462d9a

See more details on using hashes here.

File details

Details for the file mecantensor-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: mecantensor-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 258.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for mecantensor-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54118f4e15d733a24c5e310d02afe94ce2147a17970b4d455faa95a5a743c29e
MD5 a09458a7c18e91ecb7f7e6514e66dffb
BLAKE2b-256 c3352f56474dbb1e46d909ff0ca5a2c5250bcae6c1200a971a37349a1a248705

See more details on using hashes here.

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