|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-strict |
| 8 | + |
| 9 | +import contextlib |
| 10 | +import unittest |
| 11 | +from typing import Iterator |
| 12 | + |
| 13 | +import torch |
| 14 | +from torchtnt.framework._test_utils import ( |
| 15 | + DummyFitUnit, |
| 16 | + DummyPredictUnit, |
| 17 | + DummyTrainUnit, |
| 18 | + generate_random_dataloader, |
| 19 | +) |
| 20 | +from torchtnt.framework.callback import Callback |
| 21 | +from torchtnt.framework.callbacks.tensorfloat32 import EnableTensorFloat32 |
| 22 | +from torchtnt.framework.fit import fit |
| 23 | +from torchtnt.framework.predict import predict |
| 24 | +from torchtnt.framework.state import State |
| 25 | +from torchtnt.framework.train import train |
| 26 | +from torchtnt.framework.unit import TEvalUnit, TPredictUnit, TTrainUnit |
| 27 | + |
| 28 | + |
| 29 | +class _CheckTensorFloat32Enabled(Callback): |
| 30 | + def __init__(self, testcase: unittest.TestCase) -> None: |
| 31 | + self.testcase = testcase |
| 32 | + |
| 33 | + def assert_enabled(self) -> None: |
| 34 | + self.testcase.assertEqual(torch.get_float32_matmul_precision(), "high") |
| 35 | + self.testcase.assertTrue(torch.backends.cudnn.allow_tf32) |
| 36 | + self.testcase.assertTrue(torch.backends.cuda.matmul.allow_tf32) |
| 37 | + |
| 38 | + def on_train_step_start(self, state: State, unit: TTrainUnit) -> None: |
| 39 | + self.assert_enabled() |
| 40 | + |
| 41 | + def on_eval_step_start(self, state: State, unit: TEvalUnit) -> None: |
| 42 | + self.assert_enabled() |
| 43 | + |
| 44 | + def on_predict_step_start(self, state: State, unit: TPredictUnit) -> None: |
| 45 | + self.assert_enabled() |
| 46 | + |
| 47 | + |
| 48 | +class EnableTensorFloat32Test(unittest.TestCase): |
| 49 | + @contextlib.contextmanager |
| 50 | + def check_proper_restore(self) -> Iterator[EnableTensorFloat32]: |
| 51 | + callback = EnableTensorFloat32() |
| 52 | + |
| 53 | + # Disable TensorFloat32 |
| 54 | + torch.set_float32_matmul_precision("highest") |
| 55 | + torch.backends.cudnn.allow_tf32 = False |
| 56 | + torch.backends.cuda.matmul.allow_tf32 = False |
| 57 | + |
| 58 | + yield callback |
| 59 | + |
| 60 | + # Original Values are Restored |
| 61 | + self.assertIsNone(callback.original_cuda_matmul) |
| 62 | + self.assertIsNone(callback.original_cudnn) |
| 63 | + self.assertIsNone(callback.original_float32_matmul_precision) |
| 64 | + |
| 65 | + self.assertEqual(torch.get_float32_matmul_precision(), "highest") |
| 66 | + self.assertFalse(torch.backends.cudnn.allow_tf32) |
| 67 | + self.assertFalse(torch.backends.cuda.matmul.allow_tf32) |
| 68 | + |
| 69 | + def test_tensorfloat32_callback_train(self) -> None: |
| 70 | + input_dim = batch_size = max_epochs = 2 |
| 71 | + dataset_len = 5 |
| 72 | + |
| 73 | + unit = DummyTrainUnit(input_dim=input_dim) |
| 74 | + dataloader = generate_random_dataloader(dataset_len, input_dim, batch_size) |
| 75 | + with self.check_proper_restore() as callback: |
| 76 | + callbacks: list[Callback] = [callback, _CheckTensorFloat32Enabled(self)] |
| 77 | + train(unit, dataloader, max_epochs=max_epochs, callbacks=callbacks) |
| 78 | + |
| 79 | + def test_tensorfloat32_callback_fit(self) -> None: |
| 80 | + input_dim = batch_size = max_epochs = 2 |
| 81 | + dataset_len = 5 |
| 82 | + |
| 83 | + unit = DummyFitUnit(input_dim=input_dim) |
| 84 | + dataloader = generate_random_dataloader(dataset_len, input_dim, batch_size) |
| 85 | + with self.check_proper_restore() as callback: |
| 86 | + callbacks: list[Callback] = [callback, _CheckTensorFloat32Enabled(self)] |
| 87 | + fit( |
| 88 | + unit, |
| 89 | + dataloader, |
| 90 | + dataloader, |
| 91 | + max_epochs=max_epochs, |
| 92 | + callbacks=callbacks, |
| 93 | + ) |
| 94 | + |
| 95 | + def test_tensorfloat32_callback_predict(self) -> None: |
| 96 | + input_dim = batch_size = 2 |
| 97 | + dataset_len = 5 |
| 98 | + |
| 99 | + unit = DummyPredictUnit(input_dim=input_dim) |
| 100 | + dataloader = generate_random_dataloader(dataset_len, input_dim, batch_size) |
| 101 | + with self.check_proper_restore() as callback: |
| 102 | + callbacks: list[Callback] = [callback, _CheckTensorFloat32Enabled(self)] |
| 103 | + predict(unit, dataloader, callbacks=callbacks) |
0 commit comments