Repository navigation
[Observability 4/7] MetricsProcessor — throughput, memory, validation, log scheduling - #2604
felipemello1 wants to merge 1 commit into
Conversation
…, log scheduling [ghstack-poisoned]
| if self.metrics_processor.should_log(self.step): | ||
| with record_span("trainer_time/collect_dist_metrics_s"): |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
This is another benefit of MetricsProcessor object, as it can hold such "states", which we can't do otherwise?
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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], |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. | |||
There was a problem hiding this comment.
my question might be answered in later PRs, but not sure why this is its own file
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
nit: might opt to make this just a "prefix" since that's the only thing this controls
There was a problem hiding this comment.
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.
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
MetricsProcessorfor 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 whenforce_logis 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.Configaddsconsole_log_validation_keys. Whenis_validation=Trueis passed tolog(), 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_spmdRun toy_rl:
python -m torchtitan.experiments.observability.toy_rlConsole 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):
WandB
toy_spmd: https://wandb.ai/cabernet-team/torchtitan/runs/owcdodsg