Skip to content

Metrics computation on pytorch MPS backend fails, needs float32 instead of float64 #3326

@blackplane

Description

@blackplane

🐛 Bug description

Running metrics via evaluator.run(dataloader) on MacOS fails, because the pytorch MPS backend doesn't support the float64 type that the result is cast into. source code link

Suggestion: Cast to float32 instead. Although it is correct that int64 cannot cast to float32 without a loss of precision and float64 is technically the correct choice, pragmatically I'd think that this loss won't matter in practice as y_pred represents class label and 2^32 are already more than anyone will need.

File "/Users/user/Library/Caches/pypoetry/virtualenvs/columbo-HtoL9iYn-py3.12/lib/python3.12/site-packages/ignite/engine/engine.py", line 889, in run
return self._internal_run()
^^^^^^^^^^^^^^^^^^^^
File "/Users/user/Library/Caches/pypoetry/virtualenvs/columbo-HtoL9iYn-py3.12/lib/python3.12/site-packages/ignite/engine/engine.py", line 932, in _internal_run
return next(self._internal_run_generator)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/Library/Caches/pypoetry/virtualenvs/columbo-HtoL9iYn-py3.12/lib/python3.12/site-packages/ignite/engine/engine.py", line 990, in _internal_run_as_gen
self._handle_exception(e)
File "/Users/user/Library/Caches/pypoetry/virtualenvs/columbo-HtoL9iYn-py3.12/lib/python3.12/site-packages/ignite/engine/engine.py", line 644, in _handle_exception
raise e
File "/Users/user/Library/Caches/pypoetry/virtualenvs/columbo-HtoL9iYn-py3.12/lib/python3.12/site-packages/ignite/engine/engine.py", line 956, in _internal_run_as_gen
epoch_time_taken += yield from self._run_once_on_dataset_as_gen()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/Library/Caches/pypoetry/virtualenvs/columbo-HtoL9iYn-py3.12/lib/python3.12/site-packages/ignite/engine/engine.py", line 1096, in _run_once_on_dataset_as_gen
self._handle_exception(e)
File "/Users/user/Library/Caches/pypoetry/virtualenvs/columbo-HtoL9iYn-py3.12/lib/python3.12/site-packages/ignite/engine/engine.py", line 644, in _handle_exception
raise e
File "/Users/user/Library/Caches/pypoetry/virtualenvs/columbo-HtoL9iYn-py3.12/lib/python3.12/site-packages/ignite/engine/engine.py", line 1078, in _run_once_on_dataset_as_gen
self._fire_event(Events.ITERATION_COMPLETED)
File "/Users/user/Library/Caches/pypoetry/virtualenvs/columbo-HtoL9iYn-py3.12/lib/python3.12/site-packages/ignite/engine/engine.py", line 431, in _fire_event
func(*first, *(event_args + others), **kwargs)
File "/Users/user/Library/Caches/pypoetry/virtualenvs/columbo-HtoL9iYn-py3.12/lib/python3.12/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/Library/Caches/pypoetry/virtualenvs/columbo-HtoL9iYn-py3.12/lib/python3.12/site-packages/ignite/metrics/metric.py", line 469, in iteration_completed
self.update(output)
File "/Users/user/Library/Caches/pypoetry/virtualenvs/columbo-HtoL9iYn-py3.12/lib/python3.12/site-packages/ignite/metrics/metric.py", line 864, in wrapper
func(self, *args, **kwargs)
File "/Users/user/Library/Caches/pypoetry/virtualenvs/columbo-HtoL9iYn-py3.12/lib/python3.12/site-packages/ignite/metrics/recall.py", line 227, in update
_, y, correct = self._prepare_output(output)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/Library/Caches/pypoetry/virtualenvs/columbo-HtoL9iYn-py3.12/lib/python3.12/site-packages/ignite/metrics/precision.py", line 84, in _prepare_output
y_pred = y_pred.to(dtype=torch.float64, device=self._device)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Cannot convert a MPS Tensor to float64 dtype as the MPS framework doesn't support float64. Please use float32 instead.

Environment

  • PyTorch Version (e.g., 1.4): 2.6.0
  • Ignite Version (e.g., 0.3.0): 0.5.1
  • OS (e.g., Linux): MacOS
  • How you installed Ignite (conda, pip, source): pip
  • Python version: 3.12.8
  • Any other relevant information:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions