> This page is for version 0.7.0 · 26.10.
> For other versions, use one of these documentation indexes:
> - Nightly (default): https://docs.nvidia.com/nemo/automodel/nightly/llms.txt
> - Latest: https://docs.nvidia.com/nemo/automodel/latest/llms.txt
> - 0.7.0 · 26.10: https://docs.nvidia.com/nemo/automodel/v0.7/llms.txt
> - 0.5.0 · 26.06: https://docs.nvidia.com/nemo/automodel/v0.5/llms.txt
> - 0.4.0 · 26.04: https://docs.nvidia.com/nemo/automodel/v0.4/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/automodel/llms.txt.

# nemo_automodel.components.models.ergo.flops

Useful model FLOPs for Ergo training.

## Module Contents

### Functions

| Name                                                                      | Description                                                                     |
| ------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| [`model_flops`](#nemo_automodel-components-models-ergo-flops-model_flops) | Count useful GEMM FLOPs for one optimizer step of full-parameter Ergo training. |

### API

```python
nemo_automodel.components.models.ergo.flops.model_flops(
    config: nemo_automodel.components.models.ergo.config.ErgoConfig,
    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`** `int` — default: 1

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

---

**`seq_len`** `int | None` — default: 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.