Metadata-Version: 2.4
Name: neuratensor
Version: 3.0.4
Summary: Production neuromorphic inference SDK — real SNN-SSM hybrid, CPU/CUDA/MPS
Author-email: "Neuramorphic, Inc." <info@neuramorphic.ai>
License: Apache-2.0
Project-URL: Homepage, https://neuramorphic.ai
Project-URL: Documentation, https://docs.neuramorphic.ai
Project-URL: Repository, https://github.com/neuramorphic/neuratensor
Project-URL: Bug Reports, https://github.com/neuramorphic/neuratensor/issues
Keywords: neuromorphic,snn,ssm,mamba,cuda,inference,deep-learning,ai,edge,jetson
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: OS Independent
Classifier: Environment :: GPU :: NVIDIA CUDA
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: torch>=2.0.0
Requires-Dist: numpy>=1.24.0
Provides-Extra: server
Requires-Dist: fastapi>=0.100.0; extra == "server"
Requires-Dist: uvicorn[standard]>=0.23.0; extra == "server"
Requires-Dist: pydantic>=2.0.0; extra == "server"
Requires-Dist: httpx>=0.24.0; extra == "server"
Provides-Extra: triton
Requires-Dist: triton>=2.1.0; extra == "triton"
Provides-Extra: dev
Requires-Dist: pytest>=7.4.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
Requires-Dist: black>=23.0.0; extra == "dev"
Requires-Dist: ruff>=0.1.0; extra == "dev"
Requires-Dist: build>=1.0.0; extra == "dev"
Requires-Dist: twine>=4.0.0; extra == "dev"

# NeuraTensor

Fast neuromorphic inference for Python. Built around a hybrid SNN-SSM architecture — spiking neurons feeding into a state space model — that runs on anything from a laptop CPU to a datacenter GPU without any CUDA compilation steps.

```bash
pip install neuratensor
```

Requires Python 3.9+ and PyTorch 2.0+. Works on CPU, CUDA, and Apple Silicon (MPS).

---

## Why this exists

Most "neuromorphic" Python packages are either toy implementations or wrappers around C++ runtimes that only run on specific hardware. NeuraTensor is neither. The core is pure PyTorch — leaky integrate-and-fire neurons with real surrogate gradients, a diagonal state space model with ZOH discretization — so it runs wherever PyTorch runs, compiles with `torch.compile`, and produces real gradient flow for fine-tuning.

The spike sparsity is a genuine property of the architecture, not a claim. On typical inputs, ~90% of activations are zero, which matters for edge deployment and energy-constrained environments.

---

## Quickstart

```python
from neuratensor import NeuraTensor, ModelConfig
import torch

# Use a preset
from neuratensor import get_preset, InferenceEngine

engine = InferenceEngine.from_preset("small")   # 64M params, auto device
input_ids = torch.randint(0, 32000, (1, 64))
result = engine.run(input_ids)

print(f"Latency:  {result.latency_ms:.1f} ms")
print(f"Sparsity: {result.metrics['estimated_sparsity_pct']:.1f}%")
```

That's it. No config files, no CUDA extensions to compile, no environment variables to set.

---

## Available presets

| Preset   | Params | d_model | Layers | d_state | Max seq |
|----------|--------|---------|--------|---------|---------|
| `nano`   | ~8M    | 256     | 6      | 16      | 512     |
| `small`  | ~64M   | 512     | 12     | 32      | 1024    |
| `medium` | ~256M  | 1024    | 24     | 64      | 2048    |
| `large`  | ~1B    | 2048    | 32     | 128     | 2048    |

```python
from neuratensor import list_presets
list_presets()
```

---

## Architecture

Each layer is a `FusedSNNSSMBlock`:

```
input → LayerNorm → Linear(2×) → [LIF neurons] → [SSM] → gate → Linear → + residual
```

**LIF (Leaky Integrate-and-Fire):** membrane potential accumulates input, fires a binary spike when it crosses threshold, then resets. Differentiable through a fast-sigmoid surrogate gradient. The membrane decay β is a learnable parameter per neuron.

**SSM (State Space Model):** diagonal S4D-style layer with zero-order-hold discretization. Processes the spike trains as temporal sequences. Maintains a hidden state that can be carried across calls for streaming inference.

Both components are pure PyTorch — no compiled extensions. `torch.compile` works out of the box.

---

## Custom model

```python
from neuratensor import NeuraTensorModel, ModelConfig

config = ModelConfig(
    vocab_size=50000,
    d_model=768,
    n_layers=16,
    d_state=32,
    expand=2,
    dropout=0.1,
)

model = NeuraTensorModel(config)
print(f"{model.count_parameters():,} parameters")

input_ids = torch.randint(0, 50000, (4, 128))
logits, states, metrics = model(input_ids)
# metrics: spike_rate, estimated_sparsity_pct, per_layer_spike_rate
```

---

## Streaming inference

Carry state between calls — useful for real-time or token-by-token generation:

```python
engine = InferenceEngine.from_preset("small")

# Process chunk by chunk, state flows through
for chunk in token_stream:
    result = engine.run(chunk, reset_state=False)
    yield result.logits

# Or use built-in generate
output = engine.generate(prompt_ids, max_new_tokens=128, temperature=0.8)
```

---

## Benchmark

```bash
neuratensor benchmark small --seq-length 64 --iters 100
```

```
================================================================
  NeuraTensor SMALL | Benchmark Results
================================================================
  Device:         cuda
  Dtype:          torch.bfloat16
  Parameters:     64,512,000
  Batch size:     1
  Seq length:     64
----------------------------------------------------------------
  Latency mean:   8.4 ms ± 0.3 ms
  Latency p99:    9.1 ms
  Throughput:     119.0 seq/s
----------------------------------------------------------------
  Spike rate:     0.074
  Sparsity:       92.6%
================================================================
```

---

## Inference server (AWS / Docker)

```bash
pip install 'neuratensor[server]'
neuratensor serve --preset small --port 8080
```

Endpoints: `GET /health`, `GET /info`, `POST /infer`, `POST /generate`, `GET /metrics` (Prometheus format).

Docker:

```bash
docker build -t neuratensor:3.0.1 .
docker run -p 8080:8080 -e NEURATENSOR_PRESET=small neuratensor:3.0.1
```

For ECS/EKS, see [`deploy/ecs_task_definition.json`](deploy/ecs_task_definition.json).

---

## Save and load

```python
engine.save("checkpoint.pt")

from neuratensor import InferenceEngine
engine = InferenceEngine.from_checkpoint("checkpoint.pt")
```

---

## CLI

```bash
neuratensor info              # device + backend info
neuratensor presets           # list all presets with param counts
neuratensor benchmark small   # run benchmark
neuratensor serve --preset medium --port 8080
```

---

## Telemetry

NeuraTensor collects anonymous usage data (SDK version, Python version, OS, preset name, latency) to help prioritize development. No personally identifiable information is collected. Disable at any time:

```bash
export NEURATENSOR_TELEMETRY=0
```

Or in code:

```python
import os
os.environ["NEURATENSOR_TELEMETRY"] = "0"
import neuratensor
```

---

## License

Apache 2.0. See [LICENSE](LICENSE).

Built by [Neuramorphic, Inc.](https://neuramorphic.ai)
