Repository: Lightning-AI/pytorch-lightning

Issue:
reduce test warnings
## What does this PR do?
Try to ignore all these warnings...
```
tests/models/test_cpu.py::test_early_stopping_cpu_model | 318s
-- | --
1700 | /drone/src/pytorch_lightning/utilities/distributed.py:23: RuntimeWarning: 2.5854711532592773 is supposed to be a torch.Tensor. Saving checkpoint may not work correctly. HINT: check the value of val_loss in your validation loop | 318s
1701 | warnings.warn(*args, **kwargs)
```
but it would be better to fix them instead

## PR review    
Anyone in the community is free to review the PR once the tests have passed.     
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.

## Did you have fun?
Make sure you had fun coding 🙃



Discussion:
# [Codecov](https://codecov.io/gh/PyTorchLightning/pytorch-lightning/pull/2000?src=pr&el=h1) Report
> Merging [#2000](https://codecov.io/gh/PyTorchLightning/pytorch-lightning/pull/2000?src=pr&el=desc) into [master](https://codecov.io/gh/PyTorchLightning/pytorch-lightning/commit/ceecf1cea92dc2d8c29b1364237ac9467abf2f9b&el=desc) will **not change** coverage.
> The diff coverage is `n/a`.


```diff
@@          Coverage Diff           @@
##           master   #2000   +/-   ##
======================================
  Coverage      88%     88%           
======================================
  Files          74      74           
  Lines        4654    4654           
======================================
  Hits         4076    4076           
  Misses        578     578           
```

> but it would be better to fix them instead

I agree, I don't think we should hide the warnings as it will decrease the motivation to actually fix the problem.
Let's fix the actual problems instead of hiding warning. 

Resolving patch:
diff --git a/pytorch_lightning/callbacks/model_checkpoint.py b/pytorch_lightning/callbacks/model_checkpoint.py
--- a/pytorch_lightning/callbacks/model_checkpoint.py
+++ b/pytorch_lightning/callbacks/model_checkpoint.py
@@ -176,8 +176,8 @@ def check_monitor_top_k(self, current):
 
         if not isinstance(current, torch.Tensor):
             rank_zero_warn(
-                f'{current} is supposed to be a torch.Tensor. Saving checkpoint may not work correctly. '
-                f'HINT: check the value of {self.monitor} in your validation loop', RuntimeWarning
+                f'{current} is supposed to be a `torch.Tensor`. Saving checkpoint may not work correctly.'
+                f' HINT: check the value of {self.monitor} in your validation loop', RuntimeWarning
             )
             current = torch.tensor(current)
 
@@ -259,9 +259,11 @@ def on_validation_end(self, trainer, pl_module):
 
             if not isinstance(current, torch.Tensor):
                 rank_zero_warn(
-                    f'The metric you returned {current} must be a Torch.Tensor instance, checkpoint not saved '
-                    f'HINT: what is the value of {self.monitor} in validation_epoch_end()?', RuntimeWarning
+                    f'The metric you returned {current} must be a `torch.Tensor` instance, checkpoint not saved'
+                    f' HINT: what is the value of {self.monitor} in validation_epoch_end()?', RuntimeWarning
                 )
+                if current is not None:
+                    current = torch.tensor(current)
 
             if current is None:
                 rank_zero_warn(


Test patch:
diff --git a/tests/base/model_test_epoch_ends.py b/tests/base/model_test_epoch_ends.py
--- a/tests/base/model_test_epoch_ends.py
+++ b/tests/base/model_test_epoch_ends.py
@@ -34,7 +34,7 @@ def test_epoch_end(self, outputs):
         test_loss_mean /= len(outputs)
         test_acc_mean /= len(outputs)
 
-        metrics_dict = {'test_loss': test_loss_mean.item(), 'test_acc': test_acc_mean.item()}
+        metrics_dict = {'test_loss': test_loss_mean, 'test_acc': test_acc_mean}
         result = {'progress_bar': metrics_dict, 'log': metrics_dict}
         return result
 
@@ -70,6 +70,6 @@ def test_epoch_end__multiple_dataloaders(self, outputs):
         test_loss_mean /= i
         test_acc_mean /= i
 
-        tqdm_dict = {'test_loss': test_loss_mean.item(), 'test_acc': test_acc_mean.item()}
+        tqdm_dict = {'test_loss': test_loss_mean, 'test_acc': test_acc_mean}
         result = {'progress_bar': tqdm_dict}
         return result
diff --git a/tests/base/model_valid_epoch_ends.py b/tests/base/model_valid_epoch_ends.py
--- a/tests/base/model_valid_epoch_ends.py
+++ b/tests/base/model_valid_epoch_ends.py
@@ -24,7 +24,7 @@ def _mean(res, key):
         val_loss_mean = _mean(outputs, 'val_loss')
         val_acc_mean = _mean(outputs, 'val_acc')
 
-        metrics_dict = {'val_loss': val_loss_mean.item(), 'val_acc': val_acc_mean.item()}
+        metrics_dict = {'val_loss': val_loss_mean, 'val_acc': val_acc_mean}
         results = {'progress_bar': metrics_dict, 'log': metrics_dict}
         return results
 
diff --git a/tests/callbacks/test_callbacks.py b/tests/callbacks/test_callbacks.py
--- a/tests/callbacks/test_callbacks.py
+++ b/tests/callbacks/test_callbacks.py
@@ -17,8 +17,7 @@ class CurrentModel(EvalModelTemplate):
         def validation_epoch_end(self, outputs):
             losses = [8, 4, 2, 3, 4, 5, 8, 10]
             val_loss = losses[self.current_epoch]
-            val_loss = torch.tensor(val_loss)
-            return {'val_loss': val_loss}
+            return {'val_loss': torch.tensor(val_loss)}
 
     model = CurrentModel()
 
diff --git a/tests/test_deprecated.py b/tests/test_deprecated.py
--- a/tests/test_deprecated.py
+++ b/tests/test_deprecated.py
@@ -2,6 +2,7 @@
 import sys
 
 import pytest
+import torch
 
 from pytorch_lightning import Trainer
 from tests.base import EvalModelTemplate
@@ -58,16 +59,16 @@ def val_dataloader(self):
         return self.dataloader(train=False)
 
     def validation_step(self, batch, batch_idx, *args, **kwargs):
-        return {'val_loss': 0.6}
+        return {'val_loss': torch.tensor(0.6)}
 
     def validation_end(self, outputs):
-        return {'val_loss': 0.6}
+        return {'val_loss': torch.tensor(0.6)}
 
     def test_dataloader(self):
         return self.dataloader(train=False)
 
     def test_end(self, outputs):
-        return {'test_loss': 0.6}
+        return {'test_loss': torch.tensor(0.6)}
 
 
 class ModelVer0_7(EvalModelTemplate):
@@ -77,16 +78,16 @@ def val_dataloader(self):
         return self.dataloader(train=False)
 
     def validation_step(self, batch, batch_idx, *args, **kwargs):
-        return {'val_loss': 0.7}
+        return {'val_loss': torch.tensor(0.7)}
 
     def validation_end(self, outputs):
-        return {'val_loss': 0.7}
+        return {'val_loss': torch.tensor(0.7)}
 
     def test_dataloader(self):
         return self.dataloader(train=False)
 
     def test_end(self, outputs):
-        return {'test_loss': 0.7}
+        return {'test_loss': torch.tensor(0.7)}
 
 
 def test_tbd_remove_in_v1_0_0_model_hooks():
@@ -97,23 +98,23 @@ def test_tbd_remove_in_v1_0_0_model_hooks():
     with pytest.deprecated_call(match='v1.0'):
         trainer = Trainer(logger=False)
         trainer.test(model)
-    assert trainer.callback_metrics == {'test_loss': 0.6}
+    assert trainer.callback_metrics == {'test_loss': torch.tensor(0.6)}
 
     with pytest.deprecated_call(match='v1.0'):
         trainer = Trainer(logger=False)
         # TODO: why `dataloder` is required if it is not used
         result = trainer._evaluate(model, dataloaders=[[None]], max_batches=1)
-    assert result == {'val_loss': 0.6}
+    assert result == {'val_loss': torch.tensor(0.6)}
 
     model = ModelVer0_7(hparams)
 
     with pytest.deprecated_call(match='v1.0'):
         trainer = Trainer(logger=False)
         trainer.test(model)
-    assert trainer.callback_metrics == {'test_loss': 0.7}
+    assert trainer.callback_metrics == {'test_loss': torch.tensor(0.7)}
 
     with pytest.deprecated_call(match='v1.0'):
         trainer = Trainer(logger=False)
         # TODO: why `dataloder` is required if it is not used
         result = trainer._evaluate(model, dataloaders=[[None]], max_batches=1)
-    assert result == {'val_loss': 0.7}
+    assert result == {'val_loss': torch.tensor(0.7)}
diff --git a/tests/trainer/test_trainer.py b/tests/trainer/test_trainer.py
--- a/tests/trainer/test_trainer.py
+++ b/tests/trainer/test_trainer.py
@@ -272,7 +272,7 @@ def mock_save_function(filepath, *args):
     # emulate callback's calls during the training
     for i, loss in enumerate(losses):
         trainer.current_epoch = i
-        trainer.callback_metrics = {'val_loss': loss}
+        trainer.callback_metrics = {'val_loss': torch.tensor(loss)}
         checkpoint_callback.on_validation_end(trainer, trainer.get_model())
 
     file_lists = set(os.listdir(tmpdir))