abstract.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Copyright The Lightning team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Any, Callable
  15. from torchmetrics.metric import Metric
  16. class WrapperMetric(Metric):
  17. """Abstract base class for wrapper metrics.
  18. Wrapper metrics are characterized by them wrapping another metric, and forwarding all calls to the wrapped metric.
  19. This means that all logic regarding synchronization etc. is handled by the wrapped metric, and the wrapper metric
  20. should not do anything in this regard.
  21. This class therefore overwrites all methods that are related to synchronization, and does nothing in them.
  22. Additionally, the forward method is not implemented by default as custom logic is required for each wrapper metric.
  23. """
  24. def _wrap_update(self, update: Callable) -> Callable:
  25. """Overwrite to do nothing, because the default wrapped functionality is handled by the wrapped metric."""
  26. return update
  27. def _wrap_compute(self, compute: Callable) -> Callable:
  28. """Overwrite to do nothing, because the default wrapped functionality is handled by the wrapped metric."""
  29. return compute
  30. def forward(self, *args: Any, **kwargs: Any) -> Any:
  31. """Overwrite to do nothing, because the default wrapped functionality is handled by the wrapped metric."""
  32. raise NotImplementedError