Skip to main content

Latent-MoE

Diagram

Implementation of LatentMoEToward Optimal Accuracy per FLOP and Parameter in Mixture of Experts (Elango et al., NVIDIA 2026) — in Pytorch. A single-file, dependency-light layer you can drop in place of a standard MoE FFN.

The idea is simple. A standard MoE routes and computes its experts in the model hidden dimension d. LatentMoE first projects each token down into a smaller latent dimension l = d / alpha with a shared down-projection, runs all routed experts inside that latent space, then projects back up to d. Because dispatch traffic and expert weights now live in l rather than d, both all-to-all communication volume and per-expert weight-loading memory drop by a factor of alpha.

Those savings are reinvested by scaling the number of experts N' = alpha * N, exponentially expanding the space of expert combinations. Two flavors:

  • l-MoE_eff — keep top-k K fixed → match baseline accuracy at lower inference cost.
  • l-MoE_acc — scale top-k K' = alpha * K → match baseline cost while improving accuracy (recommended, Pareto-optimal).

The router and shared experts continue to operate in the original dimension d, since they are not the memory/communication bottleneck.

Install

uv pip install latent-moe

Usage

import torch
from latent_moe import LatentMoE, LatentMoEConfig

config = LatentMoEConfig(
    d = 2048,          # model hidden dim
    m = 1408,          # expert intermediate width
    n_experts = 64,    # base routed experts (N)
    top_k = 6,         # base active experts per token (K)
    alpha = 4,         # latent compression factor (l = d / alpha)
    n_shared = 2,      # always-on shared experts
    variant = "acc",   # "acc" (iso-cost, higher accuracy) or "eff" (cheaper)
)

layer = LatentMoE(config)

x = torch.randn(2, 128, config.d)  # (batch, seq, d)
y = layer(x)                       # (batch, seq, d)

assert y.shape == x.shape

Inspect the asymptotic cost quantities from Table 1 of the paper:

for k, v in layer.cost_summary().items():
    print(f"{k}: {v:,.2f}")

Transformer Usage

import torch
from latent_moe.transformer import TransformerConfig, MoETransformer

torch.manual_seed(0)

cfg = TransformerConfig(
    vocab_size=1000,
    d_model=256,
    n_layers=4,
    n_heads=8,
    n_kv_heads=2,  # GQA: 4 query heads share each KV head
    max_seq_len=128,
    d_ff=256,
    n_experts=8,
    top_k=2,
    alpha=2,
    n_shared=1,
)
model = MoETransformer(cfg)
print(f"params        : {model.num_params():,}")
print(f"primary device: {model.primary_device}")

idx = torch.randint(0, cfg.vocab_size, (2, 64))
targets = torch.randint(0, cfg.vocab_size, (2, 64))

logits, loss = model(idx, targets)
print(f"logits shape  : {tuple(logits.shape)}")
print(f"loss          : {loss.item():.4f}")
print(f"moe aux+z loss: {model.aux_loss.item():.4f}")

loss.backward()
print("backward OK")

out = model.generate(idx[:, :8], max_new_tokens=16, top_k=20)
print(f"generated     : {tuple(out.shape)}")

Citations

@article{elango2026latentmoe,
    title   = {LatentMoE: Toward Optimal Accuracy per FLOP and Parameter in Mixture of Experts},
    author  = {Elango and others},
    journal = {arXiv preprint arXiv:2601.18089},
    year    = {2026},
}

License

This project is licensed under the Apache License Version 2.0

Download files

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

Source Distribution

latent_moe-0.0.3.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

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

latent_moe-0.0.3-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file latent_moe-0.0.3.tar.gz.

File metadata

  • Download URL: latent_moe-0.0.3.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Darwin/24.6.0

File hashes

Hashes for latent_moe-0.0.3.tar.gz
Algorithm Hash digest
SHA256 3b33c9fb26eaa3e3abe6e356bd8c013645be35e458970cf9095753292f8e5c97
MD5 14d4843e0e07de0327226dbb9ce72dbe
BLAKE2b-256 0f043b2f5d43550db9f115c5ec9c7ad18b8f8edf4417b8c8c6adb4321555cd67

See more details on using hashes here.

File details

Details for the file latent_moe-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: latent_moe-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Darwin/24.6.0

File hashes

Hashes for latent_moe-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7dcc4ca7d89cadedb40a26933a6354e51a8d510d787f654e674682678ca3837a
MD5 35a28101a8b8bc56cc65bd1894221cbf
BLAKE2b-256 ef30836683d1663bdd4a04c818899be029b4f73ace1731b81d4f00134abe3b21

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 Sentry Error logging StatusPage Status page