nemo_automodel.components.models.ergo.layers
nemo_automodel.components.models.ergo.layers
Differentiable Ergo layers built on AutoModel attention and expert engines.
The mathematical contract was inspected in the supplied Ergo SGLang release, revision bd598c4f927288ea5c2bb9e84359f51d9c0b5aaf, python/sglang/srt/models/ergo.py (Apache-2.0). This implementation is independent; inference-only coefficient caching and kernels are not reused. Numerical operation order is also validated against the release’s python/sglang/test/ergo_naive_reference.py at that revision.
Module Contents
Classes
Functions
Data
API
Bases: Module
GQA with Q/K norm, local interleaved RoPE and every-fourth global NoPE.
Rotate adjacent pairs using the source’s CPU-rounded frequency table.
Parameters:
Queries or keys [batch, sequence, heads, channels] or [tokens, heads, channels], on the same device as positions.
Integer IDs [batch, sequence] or [tokens].
Returns: torch.Tensor
Rotated queries or keys with the input shape, dtype and device.
Attend local queries to global keys, preserving document boundaries.
Parameters:
Branch states [batch, local_sequence, hidden], or native THD [tokens, hidden]. SDPA CP shards contiguous sequence intervals.
RoPE positions matching the local token axes.
Document IDs [batch, local_sequence], zero for padding. Without CP, also accepts a boolean allow-mask [batch, 1, sequence, sequence].
Backend-specific packed attention metadata.
Returns: torch.Tensor
Attention output with x’s shape and dtype. Differentiable CP key
Bind a stage-local CP group through the shared MoE attention contract.
Bases: Module
One FP32 residual block with explicitly rounded branch computation.
Select window-compatible TE transport in the shared MoE parallelizer.
Expose the checkpoint-named FFN to shared EP/FSDP discovery.
Expose the checkpoint-named attention to shared CP discovery.
Return FP32 residual states with unchanged token shape and ownership.
Parameters:
FP32 residual states [batch, sequence, hidden] (the CP-local sequence under context parallelism), or [tokens, hidden] for native THD inputs.
Integer RoPE positions with the leading shape of residual.
Optional document IDs [batch, sequence], zero for padding, or a boolean allow-mask
[batch, 1, sequence, sequence]; see ErgoAttention.forward.
Optional boolean padding flags with the leading shape of residual.
Backend-specific packed attention metadata, passed to the attention.
Returns: torch.Tensor
FP32 residual states with the shape of residual.
Preserve the caller’s compilation budget on checkpoint worker threads.
Parameters:
Existing forward/recompute context factory, if present.
Returns: Callable[[], tuple[AbstractContextManager, AbstractContextManager]] | None
Composed contexts retaining the caller’s Dynamo limits, following
Bases: ErgoRMSNorm
Q/K and shared-expert normalization in the incoming compute dtype.
Normalize expert outputs with FP32 weights inside weighted RMSNorm.
Parameters:
Tensor of shape […, hidden], with arbitrary leading dimensions.
Returns: torch.Tensor
Tensor with the same shape, dtype and device as x.
Bases: Module
Non-gated ReLU-squared FFN using the selected linear backend.
Return […, hidden] after BF16/FP32 projection and ReLU² rounding.
Accept the stage’s context-parallel mesh; the dense FFN has no router that needs it.
Bases: Module
Per-expert FP32 scale, sharded on the same expert axis as the MLP.
The grouped GEMM expert paths call it through per_expert_output with the row counts of the expert
segments, which say which expert produced each row.
Keep compiled normalization scratch operations outside SAC caching.
Expert slicing and distributed operations stay outside this pure tensor core. As in Muse Spark, autograd and enclosing checkpoint recomputation remain active while selective-checkpoint dispatch accounting is suspended.
Parameters:
Rows of one expert, shape [rows, hidden], in the compute dtype.
That expert’s FP32 scale, shape [hidden].
Returns: torch.Tensor
Normalized rows of shape [rows, hidden] in the dtype of values.
Apply each expert’s weighted RMSNorm in the incoming compute dtype.
Parameters:
Tensor of shape [routes, hidden], grouped in local expert order.
Integer tensor of shape [local_experts], with segment lengths.
Returns: torch.Tensor
Tensor of shape [routes, hidden], preserving input dtype and device.
Raises:
ValueError: If the expert path did not passtokens_per_expert.
Bases: MoE
Common routed experts plus independently normalized shared ReLU² FFN.
Routing is the shared Gate in FP32: experts are chosen by the corrected sigmoid scores and weighted by the
uncorrected scores, normalised and scaled (score_func="sigmoid_with_bias" with norm_topk_prob).
Keep the output norm with the experts when LoRA replaces the expert module, and freeze it.
Normalize routes before weighted reduction; add normalized shared rows.
Parameters:
Branch input of shape […, hidden], e.g. [batch, sequence, hidden], in the compute dtype.
Optional boolean padding flags with the leading shape of x; padded tokens are not
routed.
Returns: torch.Tensor
Tensor with the shape and dtype of x: the routed experts’ weighted sum plus the normalized shared
Bind the stage’s context-parallel mesh, which the router passes to the shared gate.
Bases: Module
Learned FP32 RMSNorm; callers explicitly own any branch-output cast.
Normalize […, hidden] in FP32 and return FP32 on the input device.
Bases: Module
Learned variance-preserving residual blend with stable live gradients.
Blend matching […, hidden] branch/residual tensors in FP32.
Match the source operation order while its exponential is normal. Clamp unselected branches and factor the extreme positive tail to avoid sqrt(0) derivatives without detaching learned coefficients.
Parameters:
Tensor of shape […, hidden], with arbitrary leading dimensions.
Tensor with the same shape as branch.
Returns: torch.Tensor
FP32 blended tensor of shape […, hidden].
Bases: Embedding
FP32 token table [vocab, hidden].
The shared FSDP flows shard the input embedding as a unit of its own and hand over the bare module, so the table names itself an FP32 owner with a parameter-free child. The child adds no state-dict entry.
Cache pure compiled math, without retaining parameters or activations.
Normalize [routes, hidden] with an FP32 [hidden] scale and retain input dtype.