Skip to navigation

Span Groups

View as MarkdownOpen in Claude

Span granularity in Megatron is controlled by the MEGATRON_OTEL_SPAN_GROUPS env var (or --otel-span-groups CLI flag). The spec accepts preset keywords, individual group names, or a mix.

For the general span-group mechanism see lens: span groups. This page covers Megatron’s extensions and the complete span hierarchy.

Preset keywords

PresetGroups includedRelative cost
defaultjob, checkpoint, evaluate, inferenceLowest — safe for production
per_stepdefault + model_init, load_checkpoint, step, forward_backward, optimizer, communication, data_loadingModerate — use with sampling
alleverything including microbatch, layer, activation_offloadHighest — dev/debug only

MegatronSpanGroup

Defined in megatron/core/telemetry/span_groups.py. Extends lens’s base SpanGroup with Megatron-specific groups:

GroupSpans emittedTypical frequency
jobmegatron.pretrain, megatron.trainonce per job
checkpointmegatron.save_checkpoint, megatron.save_checkpoint.state_dict, megatron.save_checkpoint.io_writeevery checkpoint
evaluatemegatron.evaluate, megatron.evaluate.stepevery eval interval
model_initmegatron.model_initonce at startup
load_checkpointmegatron.load_checkpoint, megatron.load_checkpoint.io_readonce at startup
stepmegatron.train_stepevery iteration
forward_backwardmegatron.forward_backwardevery iteration
optimizermegatron.optimizer_stepevery iteration
microbatchmegatron.microbatch.forward, megatron.microbatch.backwardevery microbatch
layermegatron.layer.forward, megatron.layer.self_attention, megatron.layer.mlpevery layer per microbatch
communicationmegatron.p2p.{recv,send}_{forward,backward}, megatron.grad_sync.{start,finish}, megatron.pp.recv_forward.linkedevery iteration
activation_offloadmegatron.activation.offload, megatron.activation.reloadevery microbatch
data_loading(reserved for future use)every iteration
inference(reserved for the inference server)every inference request

Examples

# Coarse spans only — default
MEGATRON_OTEL_SPAN_GROUPS=default
# Include per-step spans
MEGATRON_OTEL_SPAN_GROUPS=per_step
# Default + microbatch only (skip step/optimizer groups)
MEGATRON_OTEL_SPAN_GROUPS=default,microbatch
# Everything
MEGATRON_OTEL_SPAN_GROUPS=all

Span hierarchy

The full tree of spans Megatron can emit, with the controlling span group shown per span:

megatron.pretrain # job
├── megatron.model_init # model_init
├── megatron.load_checkpoint # load_checkpoint
│ └── megatron.load_checkpoint.io_read # load_checkpoint
└── megatron.train # job
├── megatron.train_step # step
│ ├── megatron.forward_backward # forward_backward
│ │ ├── megatron.microbatch.forward # microbatch (×N)
│ │ │ └── megatron.layer.forward # layer (×L per microbatch)
│ │ │ ├── megatron.layer.self_attention
│ │ │ └── megatron.layer.mlp
│ │ ├── megatron.microbatch.backward # microbatch (×N)
│ │ ├── megatron.pp.recv_forward.linked # communication — link to sender's context (PP > 1)
│ │ ├── megatron.p2p.recv_forward # communication
│ │ ├── megatron.p2p.send_forward # communication
│ │ ├── megatron.p2p.recv_backward # communication
│ │ ├── megatron.p2p.send_backward # communication
│ │ ├── megatron.activation.offload # activation_offload
│ │ └── megatron.activation.reload # activation_offload
│ ├── megatron.grad_sync.start # communication
│ ├── megatron.grad_sync.finish # communication
│ └── megatron.optimizer_step # optimizer
├── megatron.save_checkpoint # checkpoint
│ ├── megatron.save_checkpoint.state_dict # checkpoint
│ └── megatron.save_checkpoint.io_write # checkpoint
└── megatron.evaluate # evaluate
└── megatron.evaluate.step # evaluate (×N)

Span attributes

Key Megatron-specific span attributes:

AttributeTypeSet on
megatron.model_typestrmegatron.pretrain
megatron.train_itersintmegatron.pretrain, megatron.train
megatron.global_batch_sizeintmegatron.pretrain
megatron.iterationintmegatron.train_step, megatron.save_checkpoint
megatron.lossfloatmegatron.train_step
megatron.grad_normfloatmegatron.train_step, megatron.optimizer_step
megatron.num_microbatchesintmegatron.forward_backward
megatron.microbatch_idintmegatron.microbatch.forward
megatron.eval_itersintmegatron.evaluate
megatron.update_successfulboolmegatron.optimizer_step
dl.pipeline_parallel.rankintmegatron.pp.recv_forward.linked
dl.microbatch_idintmegatron.pp.recv_forward.linked (warmup only)

Granularity guidance

Span groupsRelative costRecommendation
Disabled (MEGATRON_OTEL_ENABLED=0)NoneDefault for smoke tests
defaultLowestSafe for all production runs
per_stepModerateUse with OTEL_TRACES_SAMPLER
all (includes microbatch, layer)HighestDevelopment / profiling only

Non-exporting ranks have frozenset() span groups — is_span_group_enabled() returns False everywhere, so no span objects are created at all. The disabled path is a frozenset lookup followed by an immediate return, not a no-op span that still allocates. See lens: architecture.