Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_mixed_precision_cpu.py 2.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  1. import unittest
  2. import tempfile
  3. from super_gradients import Trainer
  4. from super_gradients.training.dataloaders.dataloaders import classification_test_dataloader
  5. from super_gradients.training.metrics import Accuracy, Top5
  6. from super_gradients.training.models import ResNet18
  7. from super_gradients.training.utils.distributed_training_utils import setup_device
  8. class TestMixedPrecisionDisabled(unittest.TestCase):
  9. def test_mixed_precision_automatically_changed_with_warning(self):
  10. setup_device(device="cpu")
  11. with tempfile.TemporaryDirectory() as temp_dir:
  12. trainer = Trainer("test_mixed_precision_automatically_changed_with_warning", ckpt_root_dir=temp_dir)
  13. net = ResNet18(num_classes=5, arch_params={})
  14. train_params = {
  15. "max_epochs": 2,
  16. "lr_updates": [1],
  17. "lr_decay_factor": 0.1,
  18. "lr_mode": "StepLRScheduler",
  19. "lr_warmup_epochs": 0,
  20. "initial_lr": 0.1,
  21. "loss": "CrossEntropyLoss",
  22. "criterion_params": {"ignore_index": 0},
  23. "train_metrics_list": [Accuracy(), Top5()],
  24. "valid_metrics_list": [Accuracy(), Top5()],
  25. "metric_to_watch": "Accuracy",
  26. "greater_metric_to_watch_is_better": True,
  27. "mixed_precision": True, # This is not supported for CPU, so we expect a warning to be raised AND the code to run
  28. }
  29. import warnings
  30. with warnings.catch_warnings(record=True) as w:
  31. # Trigger a filter to always make warnings visible
  32. warnings.simplefilter("always")
  33. trainer.train(
  34. model=net,
  35. training_params=train_params,
  36. train_loader=classification_test_dataloader(batch_size=10),
  37. valid_loader=classification_test_dataloader(batch_size=10),
  38. )
  39. # Check if the desired warning is in the list of warnings
  40. self.assertTrue(any("Mixed precision training is not supported on CPU" in str(warn.message) for warn in w))
  41. if __name__ == "__main__":
  42. unittest.main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...