|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +# pyre-strict |
| 9 | + |
| 10 | +from typing import Any, Dict, List, Optional, Type |
| 11 | + |
| 12 | +import torch |
| 13 | +from torch import distributed as dist |
| 14 | + |
| 15 | +from torchrec.metrics.metrics_namespace import MetricName, MetricNamespace, MetricPrefix |
| 16 | +from torchrec.metrics.rec_metric import ( |
| 17 | + MetricComputationReport, |
| 18 | + RecComputeMode, |
| 19 | + RecMetric, |
| 20 | + RecMetricComputation, |
| 21 | + RecMetricException, |
| 22 | + RecTaskInfo, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +class OutputMetricComputation(RecMetricComputation): |
| 27 | + """ |
| 28 | + Metric that logs whatever model outputs are given in kwargs |
| 29 | + TODO - make this generic metric that can be used for any model output tensor |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 33 | + super().__init__(*args, **kwargs) |
| 34 | + self._add_state( |
| 35 | + "latest_imp", |
| 36 | + torch.zeros(self._n_tasks, dtype=torch.double), |
| 37 | + add_window_state=False, |
| 38 | + dist_reduce_fx="sum", |
| 39 | + persistent=False, |
| 40 | + ) |
| 41 | + self._add_state( |
| 42 | + "total_latest_imp", |
| 43 | + torch.zeros(self._n_tasks, dtype=torch.double), |
| 44 | + add_window_state=False, |
| 45 | + dist_reduce_fx="sum", |
| 46 | + persistent=False, |
| 47 | + ) |
| 48 | + |
| 49 | + def update( |
| 50 | + self, |
| 51 | + *, |
| 52 | + predictions: Optional[torch.Tensor], |
| 53 | + labels: torch.Tensor, |
| 54 | + weights: Optional[torch.Tensor], |
| 55 | + **kwargs: Dict[str, Any], |
| 56 | + ) -> None: |
| 57 | + required_list = ["latest_imp", "total_latest_imp"] |
| 58 | + if "required_inputs" not in kwargs or not all( |
| 59 | + item in kwargs["required_inputs"] for item in required_list |
| 60 | + ): |
| 61 | + raise RecMetricException( |
| 62 | + "OutputMetricComputation requires 'latest_imp' and 'total_latest_imp' in kwargs" |
| 63 | + ) |
| 64 | + states = { |
| 65 | + "latest_imp": kwargs["required_inputs"]["latest_imp"] |
| 66 | + .float() |
| 67 | + .mean(dim=-1, dtype=torch.double), |
| 68 | + "total_latest_imp": kwargs["required_inputs"]["total_latest_imp"] |
| 69 | + .float() |
| 70 | + .mean(dim=-1, dtype=torch.double), |
| 71 | + } |
| 72 | + |
| 73 | + for state_name, state_value in states.items(): |
| 74 | + setattr(self, state_name, state_value) |
| 75 | + |
| 76 | + def _compute(self) -> List[MetricComputationReport]: |
| 77 | + return [ |
| 78 | + MetricComputationReport( |
| 79 | + name=MetricName.OUTPUT, |
| 80 | + metric_prefix=MetricPrefix.DEFAULT, |
| 81 | + value=self.latest_imp, |
| 82 | + description="_latest_imp", |
| 83 | + ), |
| 84 | + MetricComputationReport( |
| 85 | + name=MetricName.OUTPUT, |
| 86 | + metric_prefix=MetricPrefix.DEFAULT, |
| 87 | + value=self.total_latest_imp, |
| 88 | + description="_total_latest_imp", |
| 89 | + ), |
| 90 | + ] |
| 91 | + |
| 92 | + |
| 93 | +class OutputMetric(RecMetric): |
| 94 | + _namespace: MetricNamespace = MetricNamespace.OUTPUT |
| 95 | + _computation_class: Type[RecMetricComputation] = OutputMetricComputation |
| 96 | + |
| 97 | + def __init__( |
| 98 | + self, |
| 99 | + world_size: int, |
| 100 | + my_rank: int, |
| 101 | + batch_size: int, |
| 102 | + tasks: List[RecTaskInfo], |
| 103 | + compute_mode: RecComputeMode = RecComputeMode.UNFUSED_TASKS_COMPUTATION, |
| 104 | + window_size: int = 100, |
| 105 | + fused_update_limit: int = 0, |
| 106 | + compute_on_all_ranks: bool = False, |
| 107 | + should_validate_update: bool = False, |
| 108 | + process_group: Optional[dist.ProcessGroup] = None, |
| 109 | + **kwargs: Dict[str, Any], |
| 110 | + ) -> None: |
| 111 | + super().__init__( |
| 112 | + world_size=world_size, |
| 113 | + my_rank=my_rank, |
| 114 | + batch_size=batch_size, |
| 115 | + tasks=tasks, |
| 116 | + compute_mode=compute_mode, |
| 117 | + window_size=window_size, |
| 118 | + fused_update_limit=fused_update_limit, |
| 119 | + compute_on_all_ranks=compute_on_all_ranks, |
| 120 | + should_validate_update=should_validate_update, |
| 121 | + process_group=process_group, |
| 122 | + **kwargs, |
| 123 | + ) |
| 124 | + self._required_inputs.add("latest_imp") |
| 125 | + self._required_inputs.add("total_latest_imp") |
0 commit comments