Skip to navigation

nemo_automodel.components.models.ergo.flops

View as MarkdownOpen in Claude

Useful model FLOPs for Ergo training.

Module Contents

Functions

NameDescription
model_flopsCount useful GEMM FLOPs for one optimizer step of full-parameter Ergo training.

API

nemo_automodel.components.models.ergo.flops.model_flops(
gbs: int = 1,
seq_len: int | None = None
) -> float

Count useful GEMM FLOPs for one optimizer step of full-parameter Ergo training.

A multiply-accumulate (MAC) is two FLOPs. Every counted product is trainable, so it counts the forward pass, the input gradient and the weight gradient (six FLOPs per MAC); attention counts the same three passes over its two operands. Per layer and token:

  • The fused query/key/value projection, hidden * (heads + 2 * kv_heads) * head_dim, and the output projection, heads * head_dim * hidden.
  • Attention: heads * 2 * head_dim per permitted query-key pair (QK^T and PV; grouped keys and values are repeated to every query head). Every global_attention_every_n_layers-th layer permits the S * (S + 1) / 2 causal pairs. The other layers use a window that includes the query itself, so the query at one-based position p attends min(p, window) keys.
  • A leading dense block: the up and down projections, hidden * first_dense_intermediate_size each.
  • An MoE block: the router, hidden * experts; top-k routed experts of 2 * hidden * moe_intermediate_size (up and down, no gate projection); and the shared expert, 2 * hidden * shared_intermediate_size.

The LM head adds hidden * vocab per token; the embedding lookup has no GEMM.

Counted work is the algorithmic model FLOPs (MFU), independent of the backend: a dense attention kernel also executes the masked pairs. Elementwise operations (RMS norms, RoPE, ReLU squared, the residual blends, softmax, top-k), activation recomputation, padding, optimizer updates and communication are excluded.

Each sequence is one document without padding. Packed-document FLOPs need the individual document lengths and cannot be inferred from a packed row length.

Parameters:

config
ErgoConfig

Effective Ergo configuration.

gbs
intDefaults to 1

Number of full-length sequences in the global optimizer step.

seq_len
int | NoneDefaults to None

Tokens in each sequence; defaults to max_position_embeddings.

Returns: float

Useful model FLOPs over all devices for one optimizer step.

Raises:

  • ValueError: gbs or seq_len is not positive.