Skip to main content

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.

PyPI version Downloads Tests Coverage Python License

๐Ÿ“š Docs ยท ๐Ÿ”ฅ PyTorch ยท โšก JAX/Flax ยท ๐ŸŸจ Keras ยท ๐ŸŸง TF ยท ๐ŸŽ MLX ยท ๐Ÿงฎ Theory ยท ๐Ÿ”„ Migrate ยท ๐Ÿ“„ Paper


Contents


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])

โ†’ Full PyTorch guide

โšก 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)

โ†’ Full Flax NNX guide

๐ŸŸจ 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)

โ†’ Full TensorFlow guide

โšก 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)

โ†’ Full Flax Linen guide


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


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


Download files

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

Source Distribution

nmn-0.3.0.tar.gz (653.7 kB view details)

Uploaded Source

Built Distribution

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

nmn-0.3.0-py3-none-any.whl (250.4 kB view details)

Uploaded Python 3

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

Hashes for nmn-0.3.0.tar.gz
Algorithm Hash digest
SHA256 30fe71d4f31dee2821c9805f2984c7d0f904956897a65fa76f6ab48abd09a1cb
MD5 2fbaca897b233b27146dd559f82126e4
BLAKE2b-256 8daa24f03b6456510791004eb1e66ceefcf8def3a3276ef918263a90465ac9e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for nmn-0.3.0.tar.gz:

Publisher: publish.yml on azettaai/nmn

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

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

Hashes for nmn-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0864fa41685995e3bfea0acaceef72db04437ef735c57f3e1d52dc609afd4439
MD5 a95ad5d7bbcf8f5cec39e442d89531b0
BLAKE2b-256 64295574c4cf03b5a258586a737f3fee1d3e705eff5183e4c7c4edb2ca731518

See more details on using hashes here.

Provenance

The following attestation bundles were made for nmn-0.3.0-py3-none-any.whl:

Publisher: publish.yml on azettaai/nmn

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