Skip to content

[Observability 4/7] MetricsProcessor — throughput, memory, validation, log scheduling - #2604

Closed
felipemello1 wants to merge 1 commit into
gh/felipemello1/20/basefrom
gh/felipemello1/20/head
Closed

felipemello1 wants to merge 1 commit into
gh/felipemello1/20/basefrom
gh/felipemello1/20/head

Conversation

@felipemello1

@felipemello1 felipemello1 commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

How to review

Prioritize the files under /observability. Check how they are used in the toy example.
No need to nitpick the toy example. Real implementation in trainer are in PRs 6 and 7.

Summary

(Almost) Full MetricsProcessor for the toy trainer. This is the reference implementation that we will mirror for the real Trainer in PRs 6 and 7.

APIs

  • should_log(step) — returns True every N steps, on step 1, or when force_log is set (for validation).
  • reset_training_counters() / reset_val_counters() — reset timing and token counts.
  • record_throughput() — computes tokens/sec/device, TFLOPS, and MFU from tokens since last reset.
  • record_memory() — records GPU peak stats (reserved/active GiB, pct, alloc retries, OOMs).
  • log(step, is_validation) — signals the logging subprocess to aggregate and flush.

Validation support

MetricsProcessor.Config adds console_log_validation_keys. When is_validation=True is passed to log(), the subprocess prints a separate validation console line.

Test plan

Run toy_spmd: python -m torch.distributed.run --nproc_per_node=4 -m torchtitan.experiments.observability.toy_spmd
Run toy_rl: python -m torchtitan.experiments.observability.toy_rl

  • 13 new unit tests for EveryNSteps + logging boundary (80 cumulative)
  • Integration: toy_spmd with log_freq=5 only logs on steps {1, 5, 10, 15, 20}
  • Integration: validation console line appears on eval steps
  • Integration: throughput and memory metrics appear in WandB

Console output (toy_spmd)

With log_freq=5, metrics only appear on steps {1, 5, 10, 15, 20}.
Validation runs on steps 10 and 20 (every 10 steps):

[titan] 2026-03-15 14:50:51,749 - torchtitan.observability.aggregation - INFO - step:  1  training/loss_mean: 3.66  training/grad_norm_max: 0.49  trainer_memory/reserved_gib_max: 0.068  trainer_throughput/tps_mean: 11.86  
[titan] 2026-03-15 14:50:51,876 - torchtitan.observability.aggregation - INFO - step:  5  training/loss_mean: 3.053  training/grad_norm_max: 0.41  trainer_memory/reserved_gib_max: 0.068  trainer_throughput/tps_mean: 2166.4  
[titan] 2026-03-15 14:50:52,470 - torchtitan.observability.aggregation - INFO - step: 10  training/loss_mean: 2.54  training/grad_norm_max: 0.32  trainer_memory/reserved_gib_max: 0.068  trainer_throughput/tps_mean: 2094.1  
[titan] 2026-03-15 14:50:52,470 - torchtitan.observability.aggregation - INFO - validate step: 10  validation/loss_mean: 2.46  validator_memory/reserved_gib_max: 0.068  validator_throughput/tps_mean: 144.6  
[titan] 2026-03-15 14:50:52,622 - torchtitan.observability.aggregation - INFO - step: 15  training/loss_mean: 2.23  training/grad_norm_max: 0.26  trainer_memory/reserved_gib_max: 0.068  trainer_throughput/tps_mean: 2193.8  
[titan] 2026-03-15 14:50:52,782 - torchtitan.observability.aggregation - INFO - step: 20  training/loss_mean: 2.026  training/grad_norm_max: 0.22  trainer_memory/reserved_gib_max: 0.068  trainer_throughput/tps_mean: 2213.7  
[titan] 2026-03-15 14:50:52,782 - torchtitan.observability.aggregation - INFO - validate step: 20  validation/loss_mean: 1.99  validator_memory/reserved_gib_max: 0.068  validator_throughput/tps_mean: 7884.4  
[titan] 2026-03-15 14:50:52,782 - torchtitan.observability.aggregation - INFO - [logging process] shutting down

WandB

toy_spmd: https://wandb.ai/cabernet-team/torchtitan/runs/owcdodsg

Comment on lines +289 to +290
if self.metrics_processor.should_log(self.step):
with record_span("trainer_time/collect_dist_metrics_s"):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

metrics calculation are the same, we just added a gate 'should_log' and indented for 'record_span'

color = Color()
parts = [f"{color.red}step: {step:2}"]
for i, key in enumerate(console_log_metric_keys):
color = Color() if sys.stdout.isatty() else NoColor()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in titan, there was an extra flag to disable colored metrics. I decided to remove the flag and detect if the terminal is capable or displaying colors. Either way, this will never crash. If the terminal can't display colors, it will have some extra formatting, e.g. '&39step: 5', instead of 'step: 5'

# Counter resets
# ----

def reset_training_counters(self) -> None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, now I see the value of OOD -- it can help hold values which simple record_metrics won't give you.


@dataclass(kw_only=True, slots=True)
class Config(Configurable.Config):
log_freq: int = 10

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another benefit of MetricsProcessor object, as it can hold such "states", which we can't do otherwise?

@felipemello1 felipemello1 Mar 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, i think that overall, we do not need the MP. We could put it all in the trainer. The same way that we dont need a "GarbageCollectorClass".

The class just makes the trainer cleaner. Its easier to call MP.record_throughput(), than to add 10 lines to the trainer. It is cleaner to cal lMP.log(), than having the subprocess in the trainer setup.

Let me know if you disagree

record_metric("training/grad_norm_max", MaxMetric(value=grad_norm_val))
record_event({"train.loss": loss_val, "train.grad_norm": grad_norm_val})

self.metrics_processor.record_memory()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will dig further into this, but just from the initial reading, it strikes me as odd that I can magically call record_metric from anywhere but the metrics_processor is its own class that controls things like record_memory and throughout.

is_validation: bool,
logger_backend: BaseLogger,
console_log_metric_keys: list[str],
console_log_validation_keys: list[str],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: when might the metrics you care about be significantly different enough from validation st you have to have another param and gating on is_validation?

@felipemello1 felipemello1 Mar 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not 100% i understand the question, but will try to aswner:

we need "is_validation" to know if you should call something like:

log_to_console(console_log_validation_keys)

another option would be to remove is_validation, and rely on checking the aggregated metrics dict. E.g.

if Any([key in agg_metrics_dict for key in console_log_validation_keys]):
    is_validation=True

if is_validation:
    log_to_console(console_log_validation_keys)

We have both console_log_metric_keys and console_log_validation_keys because those are the 2 independent steps that happen in different frequencies. If they happened together, we could have just a single list of keys.

@@ -0,0 +1,30 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my question might be answered in later PRs, but not sure why this is its own file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i felt a bit uncomfortable about it as well. No even sure if we need this utility.

mfu = 100 * self.num_flops_per_token * tps / self._gpu_peak_flops
record_metric(f"{prefix}_throughput/mfu_pct_mean", MeanMetric(sum=mfu))

def record_memory(self, is_validation: bool = False) -> None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: might opt to make this just a "prefix" since that's the only thing this controls

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought about it, but the issue is that we have to also reset the specific trainer and validation counters. Since we only call 'record_metrics in two sites, i.e. 'train_step' and 'validation_step', making it a boolean was safer / more straight forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants