Span Groups
Span groups are the NeMo Lens mechanism for controlling trace granularity at runtime without code changes. Every instrumentation site tags itself with a group name; at startup, only the enabled groups actually emit spans.
Why Use Span Groups
A training job can use different levels of tracing detail for different environments:
- Minimize production overhead. Enable the
defaultpreset to emit only coarse spans (such asjob,checkpoint, andevaluate) for the lowest performance cost. - Add iteration boundaries. Enable the
per_steppreset in staging to add per-iteration boundaries for a moderate performance cost. - Diagnose performance hangs. Enable the
allpreset during active debugging to instrument every site, including per-microbatch and per-layer spans, which carries the highest performance cost.
The primary goal is to toggle granularity through a single env var without code changes, relying on a fast gating check when disabled.
Base Span Groups
The SpanGroup class ships with eight groups covering typical training workflows:
Coarse-Grained Groups in the default Preset
Medium-Grained Groups in the per_step Preset
Presets
Presets bundle groups by use case:
Presets are specific to each subclass; for example, MegatronSpanGroup.ALL_GROUPS contains more groups than SpanGroup.ALL_GROUPS.
Specification String
The config.span_groups field accepts a comma-separated specification string that mixes preset keywords and individual group names:
Resolution occurs once at setup_telemetry through config.resolved_span_groups. The resulting frozenset is registered with the state module and consulted at every instrumentation site.
Unknown keywords raise ValueError at resolution time with a list of valid options.
State Machinery
Enabled groups live in a module-level frozenset in nemo.lens.state:
The read path (is_span_group_enabled) is lock-free and safe to call from any thread. The write path (set_enabled_span_groups) is lock-protected and typically called once by setup_telemetry.
set_enabled_span_groups is also a top-level public export (from nemo.lens import set_enabled_span_groups), allowing you to override the active groups at runtime without reaching into nemo.lens.state.
Extend with Library-Specific Subclasses
Subclass SpanGroup to add domain-specific groups. For example, Megatron uses the following extension:
Pass this subclass to from_env:
The config.resolved_span_groups field now resolves against MegatronSpanGroup.ALL_GROUPS and its custom _PRESETS.
Design Notes
- Configure groups at runtime. Groups are runtime knobs rather than compile-time settings. Toggling an env var and restarting your application constitutes the entire configuration workflow, requiring no code changes.
- Ensure orthogonal controls. Groups operate independently of rank sampling (
NEMO_LENS_SAMPLER_ENABLEDandNEMO_LENS_EXPORT_SAMPLE_RATE) and export strategy (NEMO_LENS_EXPORT_STRATEGY). You can combine these settings to enableper_stepgroups, sample 10% of ranks, and export from only a single rank. - Filter at multiple granularities. Groups act as coarse-grained filters. For fine-grained control, such as tracing only iterations where loss exceeds a specific threshold, add a runtime check inside your instrumented code, as
is_span_group_enabledis only one signal.