test_basic_casts.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import unittest
  2. import functools as ft
  3. import itertools as it
  4. from apex import amp
  5. import torch
  6. from torch import nn
  7. import torch.nn.functional as F
  8. from utils import common_init, HALF, FLOAT,\
  9. ALWAYS_HALF, ALWAYS_FLOAT, MATCH_INPUT
  10. def run_layer_test(test_case, fns, expected, input_shape, test_backward=True):
  11. for fn, typ in it.product(fns, expected.keys()):
  12. x = torch.randn(input_shape, dtype=typ).requires_grad_()
  13. y = fn(x)
  14. test_case.assertEqual(y.type(), expected[typ])
  15. if test_backward:
  16. y.float().sum().backward()
  17. test_case.assertEqual(x.grad.type(), MATCH_INPUT[typ])
  18. class TestBasicCasts(unittest.TestCase):
  19. def setUp(self):
  20. self.handle = amp.init(enabled=True)
  21. common_init(self)
  22. def tearDown(self):
  23. self.handle._deactivate()
  24. def test_linear_is_half(self):
  25. m = nn.Linear(self.h, self.h)
  26. f = ft.partial(F.linear, weight=m.weight, bias=m.bias)
  27. run_layer_test(self, [m, f], ALWAYS_HALF, (self.b, self.h))
  28. def test_conv2d_is_half(self):
  29. m = nn.Conv2d(self.c, self.c, self.k)
  30. f = ft.partial(F.conv2d, weight=m.weight, bias=m.bias)
  31. run_layer_test(self, [m, f], ALWAYS_HALF, (self.b, self.c, self.h, self.h))
  32. def test_softmax_is_float(self):
  33. m = nn.Softmax(dim=1)
  34. f = ft.partial(F.softmax, dim=1)
  35. run_layer_test(self, [m, f], ALWAYS_FLOAT, (self.b, self.h))
  36. def test_group_norm_is_float(self):
  37. m = nn.GroupNorm(num_groups=4, num_channels=self.c)
  38. run_layer_test(self, [m], ALWAYS_FLOAT, (self.b, self.c, self.h, self.h))
  39. def test_mse_loss_is_float(self):
  40. shape = (self.b, self.h)
  41. target = torch.randn(shape)
  42. mod = nn.MSELoss()
  43. m = lambda x: mod(x, target)
  44. f = ft.partial(F.mse_loss, target=target)
  45. run_layer_test(self, [m], ALWAYS_FLOAT, shape)
  46. def test_relu_is_match(self):
  47. run_layer_test(self, [nn.ReLU(), F.relu], MATCH_INPUT, (self.b, self.h))
  48. def test_batch_norm_is_match(self):
  49. m = nn.BatchNorm2d(num_features=self.c)
  50. f = ft.partial(F.batch_norm, running_mean=m.running_mean, running_var=m.running_var,
  51. weight=m.weight, bias=m.bias, training=True)
  52. run_layer_test(self, [m], MATCH_INPUT, (self.b, self.c, self.h, self.h))
  53. # Test forward-only for BN inference
  54. m.eval()
  55. f = ft.partial(F.batch_norm, running_mean=m.running_mean, running_var=m.running_var,
  56. weight=m.weight, bias=m.bias, training=False)
  57. run_layer_test(self, [m, f], MATCH_INPUT, (self.b, self.c, self.h, self.h),
  58. test_backward=False)
  59. class TestBannedMethods(unittest.TestCase):
  60. def setUp(self):
  61. self.handle = amp.init(enabled=True)
  62. common_init(self)
  63. def tearDown(self):
  64. self.handle._deactivate()
  65. def bce_common(self, assertion):
  66. shape = (self.b, self.h)
  67. target = torch.rand(shape)
  68. mod = nn.BCELoss()
  69. m = lambda x: mod(x, target)
  70. f = ft.partial(F.binary_cross_entropy, target=target)
  71. for fn in [m, f]:
  72. x = torch.rand(shape, dtype=torch.half)
  73. assertion(fn, x)
  74. def test_bce_raises_by_default(self):
  75. assertion = lambda fn, x: self.assertRaises(NotImplementedError, fn, x)
  76. self.bce_common(assertion)
  77. def test_bce_is_float_with_allow_banned(self):
  78. self.handle._deactivate()
  79. self.handle = amp.init(enabled=True, allow_banned=True)
  80. assertion = lambda fn, x: self.assertEqual(fn(x).type(), FLOAT)
  81. self.bce_common(assertion)
  82. class TestTensorCasts(unittest.TestCase):
  83. def setUp(self):
  84. self.handle = amp.init(enabled=True)
  85. common_init(self)
  86. def tearDown(self):
  87. self.handle._deactivate()
  88. def test_matmul_method_is_half(self):
  89. other = torch.randn(self.h, self.h)
  90. lhs = lambda x: x.matmul(other)
  91. rhs = lambda x: other.matmul(x)
  92. run_layer_test(self, [lhs, rhs], ALWAYS_HALF, (self.h, self.h))
  93. def test_matmul_op_is_half(self):
  94. other = torch.randn(self.h, self.h)
  95. lhs = lambda x: x @ other
  96. rhs = lambda x: other @ x
  97. run_layer_test(self, [lhs, rhs], ALWAYS_HALF, (self.h, self.h))
  98. def test_pow_method_is_float(self):
  99. fn = lambda x: x.pow(2.)
  100. run_layer_test(self, [fn], ALWAYS_FLOAT, (self.b, self.h))
  101. def test_pow_op_is_float(self):
  102. fn = lambda x: x ** 2.
  103. run_layer_test(self, [fn], ALWAYS_FLOAT, (self.b, self.h))
  104. def test_cpu_is_float(self):
  105. fn = lambda x: x.cpu()
  106. always_cpu_float = {torch.float: 'torch.FloatTensor',
  107. torch.half: 'torch.FloatTensor'}
  108. run_layer_test(self, [fn], always_cpu_float, (self.b, self.h))
  109. def test_sum_is_float(self):
  110. fn = lambda x: x.sum()
  111. run_layer_test(self, [fn], ALWAYS_FLOAT, (self.b, self.h))
  112. # TODO: maybe more tests on disabled casting?
  113. if __name__ == '__main__':
  114. unittest.main()