Neural-Matter Network (NMN) - Advanced neural network layers with attention mechanisms
Project description
NMN โ Neural Matter Networks
Activation-free neural layers that learn non-linearity through geometric operations.
One library. Six frameworks. Numerically equivalent.
๐ Docs ยท ๐ฅ PyTorch ยท โก JAX/Flax ยท ๐จ Keras ยท ๐ง TF ยท ๐ MLX ยท ๐งฎ Theory ยท ๐ Migrate ยท ๐ Paper
Contents
- What is NMN?
- Install
- 60-second tour
- Choose your framework
- Layer reference
- The math, in one minute
- Examples
- Testing
- Project status
- Contributing
- Citation
- License
What is NMN?
NMN is a drop-in replacement for Linear + activation and Conv + activation blocks. The non-linearity is built into the layer via a geometric ratio โ no ReLU, no Sigmoid, no GELU.
# Before
y = relu(linear(x)) # dot product โ activation
# After
y = YatNMN(in_features=128, out_features=64)(x) # geometric, intrinsically non-linear
Why care?
| Standard neuron | Yat neuron |
|---|---|
| Measures correlation between w and x | Balances correlation AND proximity |
| Requires an external activation for non-linearity | Non-linearity is intrinsic |
| Fires for distant-but-aligned vectors (spurious) | Penalizes distance โ cleaner, prototype-like features |
NMN ships across PyTorch, Flax NNX, Flax Linen, Keras 3, TensorFlow, and MLX (Apple Silicon) with numerically equivalent outputs (< 1e-6 max-abs error in fp32, verified by an integration parity matrix). Pick the framework you like; switch later without retraining math.
Install
pip install nmn # the Yat layers, no framework deps
pip install "nmn[torch]" # + PyTorch
pip install "nmn[nnx]" # + Flax NNX (JAX)
pip install "nmn[linen]" # + Flax Linen (JAX)
pip install "nmn[keras]" # + Keras 3 / TensorFlow
pip install "nmn[tf]" # + TensorFlow
pip install "nmn[mlx]" # + MLX (Apple Silicon only)
pip install "nmn[all]" # everything except MLX (Linux/Windows safe)
Requirements: Python โฅ 3.10 (โฅ 3.11 if you want JAX/Flax).
GPU/TPU note: install the GPU/TPU build of your framework first (see PyTorch or JAX install pages), then
pip install nmn.
60-second tour
The same MLP in every framework. Pick one, copy, run.
๐ฅ PyTorch
import torch, torch.nn as nn
from nmn.torch import YatNMN
model = nn.Sequential(
nn.Flatten(),
YatNMN(in_features=784, out_features=256),
YatNMN(in_features=256, out_features=128),
nn.Linear(128, 10), # keep logits linear
)
x = torch.randn(32, 1, 28, 28)
print(model(x).shape) # torch.Size([32, 10])
โก Flax NNX (JAX)
import jax.numpy as jnp
from flax import nnx
from nmn.nnx import YatNMN
class MLP(nnx.Module):
def __init__(self, rngs):
self.fc1 = YatNMN(in_features=784, out_features=256, rngs=rngs)
self.fc2 = YatNMN(in_features=256, out_features=128, rngs=rngs)
self.out = nnx.Linear(128, 10, rngs=rngs)
def __call__(self, x):
x = x.reshape((x.shape[0], -1))
return self.out(self.fc2(self.fc1(x)))
model = MLP(rngs=nnx.Rngs(0))
print(model(jnp.ones((32, 28, 28, 1))).shape) # (32, 10)
๐จ Keras 3
import keras
from nmn.keras import YatNMN
model = keras.Sequential([
keras.layers.Input((28, 28)),
keras.layers.Flatten(),
YatNMN(features=256),
YatNMN(features=128),
keras.layers.Dense(10),
])
print(model(keras.ops.ones((32, 28, 28))).shape) # (32, 10)
โ Full Keras guide
๐ง TensorFlow
import tensorflow as tf
from nmn.tf import YatNMN
model = tf.keras.Sequential([
tf.keras.layers.Flatten(),
YatNMN(features=256),
YatNMN(features=128),
tf.keras.layers.Dense(10),
])
print(model(tf.zeros((32, 28, 28))).shape) # (32, 10)
โก Flax Linen (JAX, legacy API)
import jax, jax.numpy as jnp
import flax.linen as nn
from nmn.linen import YatNMN
class MLP(nn.Module):
@nn.compact
def __call__(self, x):
x = x.reshape((x.shape[0], -1))
x = YatNMN(features=256)(x)
x = YatNMN(features=128)(x)
return nn.Dense(10)(x)
model = MLP()
params = model.init(jax.random.PRNGKey(0), jnp.ones((1, 28, 28, 1)))
print(model.apply(params, jnp.ones((32, 28, 28, 1))).shape) # (32, 10)
Choose your framework
All five backends expose the same operations with framework-idiomatic naming. They are numerically equivalent (verified in tests/integration/).
| Framework | Pick it whenโฆ | Guide |
|---|---|---|
| PyTorch | You want the most ergonomic Python API and broad GPU support. | docs/guides/pytorch.md |
| Flax NNX | You want JAX speed with Pythonic state. Recommended JAX entry point. | docs/guides/flax-nnx.md |
| Flax Linen | You're maintaining a legacy Linen codebase. | docs/guides/flax-linen.md |
| Keras 3 | You want one API that runs on JAX, TF, or PyTorch backends. | docs/guides/keras.md |
| TensorFlow | You need TF-specific deployment (SavedModel, TFLite, Serving). | docs/guides/tensorflow.md |
Layer reference
All layers are available across all 5 frameworks with verified parity.
| Operation | PyTorch | TF / Keras | Flax NNX | Flax Linen |
|---|---|---|---|---|
| Dense | YatNMN |
YatNMN |
YatNMN |
YatNMN |
| Conv 1D / 2D / 3D | YatConv{1,2,3}D |
YatConv{1,2,3}D |
YatConv |
YatConv{1,2,3}D |
| ConvTranspose 1D / 2D / 3D | YatConvTranspose{1,2,3}D |
YatConvTranspose{1,2,3}D |
YatConvTranspose |
YatConvTranspose{1,2,3}D |
| Multi-Head Attention | MultiHeadYatAttention |
MultiHeadYatAttention |
MultiHeadAttention |
MultiHeadAttention |
| Embedding | YatEmbed |
YatEmbed |
Embed |
YatEmbed |
| Squashers | softermax, softer_sigmoid, soft_tanh |
same | same | same |
Flax NNX exclusives:
| Variant | What it does | Complexity |
|---|---|---|
RotaryYatAttention |
Yat attention + RoPE | O(nยฒ) |
MultiHeadAttention(use_performer=True) |
Spherical YAT-Performer (FAVOR+ features) | O(n) |
| Pallas fused yat-attention kernel | Flash-attention-style fused TPU/GPU kernel | O(nยฒ) mem-efficient |
Cross-framework consistency
Framework Pair โ Max Error โ Status
โโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโผโโโโโโโโ
PyTorch โ TensorFlow โ < 1e-6 โ โ
PyTorch โ Keras โ < 1e-6 โ โ
PyTorch โ Flax NNX โ < 1e-6 โ โ
PyTorch โ Flax Linen โ < 1e-6 โ โ
TensorFlow โ Keras โ < 1e-7 โ โ
Flax NNX โ Flax Linen โ < 1e-7 โ โ
Run yourself: pytest tests/integration/test_cross_framework_consistency.py -v.
The math, in one minute
A Yat neuron is a ratio of similarity to distance, with the bias absorbed into the (squared) inner product:
$$ \mathrm{โต}(\mathbf{w}, \mathbf{x}, b) = \frac{\bigl(\langle \mathbf{w}, \mathbf{x} \rangle + b\bigr)^2}{\lVert \mathbf{w} - \mathbf{x} \rVert^2 + \varepsilon} $$
Maximum response requires w and x to be both aligned AND close. That's the geometric prior that lets you drop the activation function. The bias b shifts the affine score inside the polynomial (biased polynomial kernel) โ not added to the output after the ratio.
For convolutions, the same identity applies per patch:
$$ \mathrm{โต}^*(\mathbf{W}, \mathbf{X}, b) = \frac{\bigl(\sum_{i,j} w_{ij} x_{ij} + b\bigr)^2}{\sum_{i,j} (w_{ij} - x_{ij})^2 + \varepsilon} $$
ฮต (epsilon, default 1e-5) prevents division by zero; bump it to 1e-3 for fp16/bf16. Some layers also expose a learnable alpha scalar (set use_alpha=True, or constant_alpha=True to fix ฮฑ = โ2).
๐ Deeper dive: docs/architecture.md โ geometric reading, ฮต tuning, where (not) to use NMN, mental model.
Examples
Runnable scripts live in-tree, organized per framework:
| Script | What it does |
|---|---|
src/nmn/torch/examples/quick_example.py |
Yat layers in PyTorch (weight norm, ฮฑ, โฆ) |
src/nmn/torch/examples/vision/resnet_training.py |
ResNet training on PyTorch |
src/nmn/nnx/examples/vision/aether_resnet50_tpu.py |
ResNet-50 on TPU with Flax NNX |
src/nmn/nnx/examples/language/m3za.py |
MiniBERT pre-training (uses fused attention) |
src/nmn/nnx/examples/language/m3za_perf.py |
Performance evaluation |
For copy-pasteable snippets across all frameworks (CNN, transformer, attention, embeddings, custom squashers), see EXAMPLES.md.
Testing
pip install "nmn[test]"
pytest tests/ # everything
pytest tests/test_torch/ # one framework
pytest tests/integration/ # cross-framework parity
pytest tests/ -m "not slow" # skip slow tests
pytest tests/ --cov=nmn --cov-report=html # coverage report
CI matrix: Linux ร Python {3.10, 3.11, 3.12} for all frameworks, plus macOS-3.11 (PyTorch + Keras/TF) and Windows-3.11 (PyTorch). See .github/workflows/test.yml.
Project status
| Area | Status |
|---|---|
| Core layers across 5 frameworks | โ Production-ready, on PyPI |
| Cross-framework consistency tests | โ Verified < 1e-6 in fp32 |
| Documentation | โ Per-platform guides, architecture, migration |
| ONNX export | ๐ง Should work (standard ops) โ not yet covered in CI (TODO.md) |
| INT8 quantization | ๐ง Not yet implemented (TODO.md) |
| Auto-generated API reference | ๐ง Planned (Sphinx / mkdocstrings) โ see TODO.md |
Latest changes: CHANGELOG.md.
Contributing
We welcome contributions of all sizes โ from typo fixes to new framework backends. See CONTRIBUTING.md for development setup, test commands, and the "add a new layer" workflow.
Quick start:
git clone https://github.com/azettaai/nmn.git
cd nmn
pip install -e ".[dev,torch]" # or ".[dev,nnx]", etc.
pytest tests/test_torch/ -v
Found a bug? โ open an issue. Security issue? โ see SECURITY.md for private disclosure.
Citation
@software{nmn2024,
author = {Bouhsine, Taha},
title = {NMN: Neural Matter Networks},
year = {2024},
url = {https://github.com/azettaai/nmn}
}
@article{bouhsine2024dl2,
author = {Bouhsine, Taha},
title = {Deep Learning 2.0: Artificial Neurons that Matter --- Reject Correlation, Embrace Orthogonality},
year = {2024}
}
Community
- ๐ฌ Discussions โ GitHub Discussions
- ๐ Issues โ GitHub Issues
- ๐ Company โ azetta.ai
- ๐ง Contact โ taha@azetta.ai
License
AGPL-3.0 โ free for personal, academic, and commercial use with attribution. If you modify and deploy on a network, you must share the source.
For alternative licensing, contact taha@azetta.ai.
Acknowledgments
This project was originally developed under the mlnomadpy organization and is now maintained by Azetta.ai. Thanks to everyone who has contributed code, feedback, and ideas.
Built with โค๏ธ by Azetta.ai ยท Originally created by ML Nomad
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nmn-0.3.0.tar.gz.
File metadata
- Download URL: nmn-0.3.0.tar.gz
- Upload date:
- Size: 653.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30fe71d4f31dee2821c9805f2984c7d0f904956897a65fa76f6ab48abd09a1cb
|
|
| MD5 |
2fbaca897b233b27146dd559f82126e4
|
|
| BLAKE2b-256 |
8daa24f03b6456510791004eb1e66ceefcf8def3a3276ef918263a90465ac9e1
|
Provenance
The following attestation bundles were made for nmn-0.3.0.tar.gz:
Publisher:
publish.yml on azettaai/nmn
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nmn-0.3.0.tar.gz -
Subject digest:
30fe71d4f31dee2821c9805f2984c7d0f904956897a65fa76f6ab48abd09a1cb - Sigstore transparency entry: 1661283994
- Sigstore integration time:
-
Permalink:
azettaai/nmn@cd6edf6a721e32957352e4abfc17b3315eb42eef -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/azettaai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@cd6edf6a721e32957352e4abfc17b3315eb42eef -
Trigger Event:
push
-
Statement type:
File details
Details for the file nmn-0.3.0-py3-none-any.whl.
File metadata
- Download URL: nmn-0.3.0-py3-none-any.whl
- Upload date:
- Size: 250.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0864fa41685995e3bfea0acaceef72db04437ef735c57f3e1d52dc609afd4439
|
|
| MD5 |
a95ad5d7bbcf8f5cec39e442d89531b0
|
|
| BLAKE2b-256 |
64295574c4cf03b5a258586a737f3fee1d3e705eff5183e4c7c4edb2ca731518
|
Provenance
The following attestation bundles were made for nmn-0.3.0-py3-none-any.whl:
Publisher:
publish.yml on azettaai/nmn
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nmn-0.3.0-py3-none-any.whl -
Subject digest:
0864fa41685995e3bfea0acaceef72db04437ef735c57f3e1d52dc609afd4439 - Sigstore transparency entry: 1661284158
- Sigstore integration time:
-
Permalink:
azettaai/nmn@cd6edf6a721e32957352e4abfc17b3315eb42eef -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/azettaai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@cd6edf6a721e32957352e4abfc17b3315eb42eef -
Trigger Event:
push
-
Statement type: