Skip to main content

CoFrGeNet-FNG (unofficial PyTorch implementation)

English | 简体中文

Unofficial PyTorch implementation of CoFrGeNet-FNG — the Non-Expanding Gated Continued Fraction feed-forward layer from IBM Research's paper Non-Expanding Gated Continued Fraction Architecture for Feedforward Layers in Language Models (NeurIPS 2026) — plus classic FFN baselines and a fused Triton kernel for the continued-fraction ladders.

Note: This is an independent, from-the-paper implementation. The authors' official repository, IBM/CoFrNets, had not published code at the time of writing.

What is CoFrGeNet-FNG?

A drop-in replacement for the FFN sublayer of transformer (and SSM/MoE) blocks with the same (…, p) -> (…, p) API, but ~4x fewer parameters:

  1. Non-expanding gated projection: x ∈ R^p → z ∈ R^p (expansion factor α = 1), computed as z = a ⊙ silu(b) where [a, b] is a split of one linear map — 2p² parameters.

  2. L continued-fraction ladders of depth d: each ladder has d+1 learned coefficient vectors, a_k = w_kᵀz, and evaluates the rational function

    f~(a_1..a_d) = 1/(a_1 + 1/(a_2 + ... + 1/a_d))
    

    via the continuant recursion (paper eqs. 3–6), needing only one division per ladder instead of d.

  3. log/exp division (paper eq. 8): the remaining division is rewritten as sgn·sgn·exp(log|num| − log max(|den|, ε)), which stabilizes training of deep ladders (d > 1) without the incremental training schedule required by the original CoFrGeNet.

  4. A linear projection R^L → R^p.

Parameter count: 2p² + L·p·(d+1) + L·p vs. 2αp² for a vanilla FFN or 3αp² for SwiGLU (α ≥ 2).

Install

# from PyPI
pip install cofrgenet-torch             # torch is the only hard dependency
pip install "cofrgenet-torch[triton]"   # optional: fused Triton fwd+bwd (GPU)

# or from source (this repo)
pip install -e .

Optional, for the fused Triton ladder kernels:

# GPU
pip install triton
# CPU (experimental triton-cpu backend, pick your platform/python)
pip install "https://github.com/triton-lang/triton-cpu/releases/download/v3.8.0/triton-3.8.0%2Bcpu-cp312-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl"

Note: install the triton-cpu wheel last. torch pins its own triton build, so a later pip install that touches torch's dependencies may replace the +cpu wheel with mainline triton (which has no CPU backend).

Quickstart

import torch
from cofrgenet_fng import CoFrGeNetFNG

ffn = CoFrGeNetFNG(p=768, n_ladders=5, depth=5)   # same API as an FFN
x = torch.randn(2, 128, 768)
y = ffn(x)                                        # (2, 128, 768)

sum(t.numel() for t in ffn.parameters())          # 1,208,862  (vs 4.7M vanilla FFN)

Examples

Run from the repo root.

fng_vs_ffn.py — FNG vs. FFN baselines

Verifies the continuant math, then compares parameters, speed, and learning on a teacher-student task. Measured on an 8-core CPU (torch 2.9.1):

module                             params   vs FFN    fwd+bwd
VanillaFFN (alpha=4)            4,722,432    1.00x   259.90 ms
SwiGLUFFN (alpha=8/3)           4,723,456    1.00x   234.54 ms
CoFrGeNetFNG (L=5, d=5)         1,208,862    0.26x    69.60 ms
CoFrGeNetFNG (L=8, d=8)         1,243,464    0.26x    74.09 ms

On the toy fitting task the FNG block uses 3x fewer parameters but converges slower than a full-size FFN — consistent with the paper, whose parity/better results are at language-model pre-training scale, not tiny regression tasks.

fng_triton.py — fused Triton ladder kernel

The ladder is a serial loop of tiny elementwise ops in eager PyTorch; the kernel fuses both forward and backward into pointwise passes (forward: continuant recursion + one log/exp division; backward: analytic adjoint, also division-free, exposed via ladder_apply / CoFrGeNetFNGTriton). Measured with the triton-cpu backend (forward; numbers on a shared CPU vary run to run; the ladder-only speedup is the consistent part — at whole-block level the p→2p GEMM dominates and the difference is within noise):

(B=16, S=256, L=8, d=8)  N=32,768 ladders:     eager 0.679 ms | triton 0.164 ms (4.13x)
(B=64, S=1024, L=16, d=8) N=1,048,576 ladders: eager 16.262 ms | triton 5.945 ms (2.74x)

Run it with TRITON_DEFAULT_BACKEND=cpu python fng_triton.py (or on a GPU, plain python fng_triton.py).

mnist_train.py — MNIST forward + backward training test

End-to-end training on CPU (8 epochs, batch 256, AdamW lr=5e-4, grad clip 1.0) of the same tiny residual classifier with the three FFN-style blocks swapped. Data is fetched directly from the MNIST IDX mirrors (no torchvision needed).

block        params    test_acc @8   step
ffn          233,738     97.66%     5.0 ms
fng-eager    149,610     93.76%     5.9 ms
fng-triton   149,610     93.23%     5.1 ms

fng-triton uses the fused Triton forward + backward kernels in the ladder and tracks fng-eager to within small floating-point drift. With depth=5, L=16 the FNG block reaches ~93–94% at 0.64x the parameters of the FFN; depth=8 is noticeably harder to optimize at this small scale.

Run it with TRITON_DEFAULT_BACKEND=cpu python mnist_train.py.

Tests

pip install pytest
pytest -v          # triton tests are skipped automatically if unavailable

Repository layout

cofrgenet_fng/        the package
  layers.py           VanillaFFN, SwiGLUFFN, CoFrGeNetFNG, continuant_fraction
  triton_ladder.py    fused Triton fwd+bwd kernels (optional), CoFrGeNetFNGTriton
fng_vs_ffn.py         example: FNG vs. FFN baselines
fng_triton.py         example: Triton kernel correctness + benchmarks
mnist_train.py        example: MNIST forward + backward training test
tests/                pytest suite

Citation

@article{dhurandhar2026cofrgenetfng,
  title   = {Non-Expanding Gated Continued Fraction Architecture for Feedforward Layers in Language Models},
  author  = {Dhurandhar, Amit and Chenthamarakshan, Vijil and Ramamurthy, Karthikeyan Natesan and Wei, Dennis and Nair, Rahul},
  journal = {arXiv:2611.26835},
  year    = {2026}
}

@inproceedings{dhurandhar2026cofrgenet,
  title     = {CoFrGeNet: Continued Fraction Architectures for Language Generation},
  author    = {Dhurandhar, A. and Chenthamarakshan, V. and Wei, D. and Pedapati, T. and Ramamurthy, K. N. and Nair, R.},
  booktitle = {Proceedings of the 43rd International Conference on Machine Learning (ICML)},
  year      = {2026}
}

License

MIT — see LICENSE.

Metadata

Release files for cofrgenet-torch 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cofrgenet-torch 0.2.0
File Size Uploaded Provenance Actions
cofrgenet_torch-0.2.0.tar.gz 14.7 kB Without provenance Details for cofrgenet_torch-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cofrgenet-torch 0.2.0
File Interpreter ABI Platform Actions
cofrgenet_torch-0.2.0-py3-none-any.whl Python 3 none any Details for cofrgenet_torch-0.2.0-py3-none-any.whl

Total release size: 26.0 kB

Release files / cofrgenet_torch-0.2.0.tar.gz

Download URL cofrgenet_torch-0.2.0.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a655e1657ad1fa6b9f9daa4f270c12ff8db4461af25806af8175750934571ad9
BLAKE2b-256 checksum
How to use checksums
6f0ee6b17c30803b7ea5a12aabdae5eeb78ad919c14d7c19ee60ea6376727ad0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / cofrgenet_torch-0.2.0-py3-none-any.whl

Download URL cofrgenet_torch-0.2.0-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c0722e66227c8e3b8bcd31919bf4e78ceaa455a85f57549ca2415d02fd5d2524
BLAKE2b-256 checksum
How to use checksums
fdd904d6c151f9f890e9fe52dd7beda79be4bfde35480624b8f7612d6728d022
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for cofrgenet-torch 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page