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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from apex.normalization import FusedLayerNorm
|
| 3 |
+
from apex.normalization import FusedRMSNorm
|
| 4 |
+
from apex.normalization import MixedFusedLayerNorm
|
| 5 |
+
from apex.normalization import MixedFusedRMSNorm
|
| 6 |
+
|
| 7 |
+
from torch.testing._internal import common_utils
|
| 8 |
+
from torch.testing._internal.common_device_type import instantiate_device_type_tests
|
| 9 |
+
|
| 10 |
+
from itertools import product
|
| 11 |
+
|
| 12 |
+
def _prep_inputs(batch_size, normalized_shape, dtype):
|
| 13 |
+
shape = (batch_size, *normalized_shape)
|
| 14 |
+
fused = torch.randn(shape).cuda().requires_grad_(True)
|
| 15 |
+
with torch.no_grad():
|
| 16 |
+
native = fused.clone().to(dtype).requires_grad_(True)
|
| 17 |
+
return native, fused
|
| 18 |
+
|
| 19 |
+
autocast_dtypes = (torch.half, torch.bfloat16) if torch.cuda.is_bf16_supported() else (torch.half,)
|
| 20 |
+
|
| 21 |
+
class TestFusedLayerNorm(common_utils.TestCase):
|
| 22 |
+
|
| 23 |
+
def _test_fused_layer_norm(
|
| 24 |
+
self, batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient,
|
| 25 |
+
fwd_thresholds=dict(rtol=None, atol=None), bwd_thresholds=dict(rtol=None, atol=None)
|
| 26 |
+
):
|
| 27 |
+
|
| 28 |
+
normalized_shape = [32, 16]
|
| 29 |
+
|
| 30 |
+
if not mixed_fused:
|
| 31 |
+
module_cpu_ = FusedLayerNorm(
|
| 32 |
+
normalized_shape=normalized_shape, elementwise_affine=elementwise_affine, memory_efficient=memory_efficient
|
| 33 |
+
).cpu()
|
| 34 |
+
module_cuda_ = FusedLayerNorm(
|
| 35 |
+
normalized_shape=normalized_shape, elementwise_affine=elementwise_affine, memory_efficient=memory_efficient
|
| 36 |
+
).to(device="cuda", dtype=dtype)
|
| 37 |
+
else:
|
| 38 |
+
assert elementwise_affine
|
| 39 |
+
module_cpu_ = MixedFusedLayerNorm(
|
| 40 |
+
normalized_shape=normalized_shape, memory_efficient=memory_efficient
|
| 41 |
+
).cpu()
|
| 42 |
+
module_cuda_ = MixedFusedLayerNorm(
|
| 43 |
+
normalized_shape=normalized_shape, memory_efficient=memory_efficient
|
| 44 |
+
).to(device="cuda", dtype=dtype)
|
| 45 |
+
|
| 46 |
+
torch.cuda.manual_seed(42)
|
| 47 |
+
if contiguous:
|
| 48 |
+
input_shape = [batch_size] + normalized_shape
|
| 49 |
+
input_ = torch.randn(input_shape, device="cpu").requires_grad_(True)
|
| 50 |
+
input_cuda_ = input_.to(device="cuda", dtype=dtype).detach().requires_grad_(True)
|
| 51 |
+
self.assertTrue(input_.is_contiguous())
|
| 52 |
+
self.assertTrue(input_cuda_.is_contiguous())
|
| 53 |
+
else:
|
| 54 |
+
input_shape = [batch_size] + normalized_shape
|
| 55 |
+
input_shape = [batch_size * 3] + [normalized_shape[0] * 5, normalized_shape[1] * 3]
|
| 56 |
+
input_src_ = torch.randn(input_shape, device="cpu")
|
| 57 |
+
input_ = input_src_[::3, ::5, ::3].detach().requires_grad_(True)
|
| 58 |
+
input_cuda_ = input_src_.to(device="cuda", dtype=dtype)[::3, ::5, ::3].detach().requires_grad_(True)
|
| 59 |
+
# make sure that tensors are NOT contiguous.
|
| 60 |
+
self.assertFalse(input_.is_contiguous())
|
| 61 |
+
self.assertFalse(input_cuda_.is_contiguous())
|
| 62 |
+
out_cpu_ = module_cpu_(input_)
|
| 63 |
+
gO = torch.rand_like(out_cpu_)
|
| 64 |
+
out_cpu_.backward(gO)
|
| 65 |
+
out_cuda_ = module_cuda_(input_cuda_)
|
| 66 |
+
|
| 67 |
+
gO = gO.to(device="cuda", dtype=dtype)
|
| 68 |
+
out_cuda_.backward(gO)
|
| 69 |
+
self.assertFalse(out_cpu_.is_cuda)
|
| 70 |
+
self.assertTrue(out_cuda_.is_cuda)
|
| 71 |
+
torch.testing.assert_close(
|
| 72 |
+
out_cpu_.to(device="cuda", dtype=dtype), out_cuda_, **fwd_thresholds)
|
| 73 |
+
torch.testing.assert_close(
|
| 74 |
+
input_.grad.to(device="cuda", dtype=dtype), input_cuda_.grad, **bwd_thresholds)
|
| 75 |
+
|
| 76 |
+
def _test_fused_rms_norm(
|
| 77 |
+
self, batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient,
|
| 78 |
+
fwd_thresholds=dict(rtol=None, atol=None), bwd_thresholds=dict(rtol=None, atol=None)
|
| 79 |
+
):
|
| 80 |
+
|
| 81 |
+
normalized_shape = [32, 16]
|
| 82 |
+
|
| 83 |
+
if not mixed_fused:
|
| 84 |
+
module_cpu_ = FusedRMSNorm(
|
| 85 |
+
normalized_shape=normalized_shape, elementwise_affine=elementwise_affine, memory_efficient=memory_efficient
|
| 86 |
+
).cpu()
|
| 87 |
+
module_cuda_ = FusedRMSNorm(
|
| 88 |
+
normalized_shape=normalized_shape, elementwise_affine=elementwise_affine, memory_efficient=memory_efficient
|
| 89 |
+
).to(device="cuda", dtype=dtype)
|
| 90 |
+
else:
|
| 91 |
+
assert elementwise_affine
|
| 92 |
+
module_cpu_ = MixedFusedRMSNorm(
|
| 93 |
+
normalized_shape=normalized_shape).cpu()
|
| 94 |
+
module_cuda_ = MixedFusedRMSNorm(
|
| 95 |
+
normalized_shape=normalized_shape).to(device="cuda", dtype=dtype)
|
| 96 |
+
|
| 97 |
+
torch.cuda.manual_seed(42)
|
| 98 |
+
if contiguous:
|
| 99 |
+
input_shape = [batch_size] + normalized_shape
|
| 100 |
+
input_ = torch.randn(input_shape, device="cpu").requires_grad_(True)
|
| 101 |
+
input_cuda_ = input_.to(device="cuda", dtype=dtype).detach().requires_grad_(True)
|
| 102 |
+
self.assertTrue(input_.is_contiguous())
|
| 103 |
+
self.assertTrue(input_cuda_.is_contiguous())
|
| 104 |
+
else:
|
| 105 |
+
input_shape = [batch_size] + normalized_shape
|
| 106 |
+
input_shape = [batch_size * 3] + [normalized_shape[0] * 5, normalized_shape[1] * 3]
|
| 107 |
+
input_src_ = torch.randn(input_shape, device="cpu")
|
| 108 |
+
input_ = input_src_[::3, ::5, ::3].detach().requires_grad_(True)
|
| 109 |
+
input_cuda_ = input_src_.to(device="cuda", dtype=dtype)[::3, ::5, ::3].detach().requires_grad_(True)
|
| 110 |
+
# make sure that tensors are NOT contiguous.
|
| 111 |
+
self.assertFalse(input_.is_contiguous())
|
| 112 |
+
self.assertFalse(input_cuda_.is_contiguous())
|
| 113 |
+
out_cpu_ = module_cpu_(input_)
|
| 114 |
+
gO = torch.rand_like(out_cpu_)
|
| 115 |
+
out_cpu_.backward(gO)
|
| 116 |
+
out_cuda_ = module_cuda_(input_cuda_)
|
| 117 |
+
|
| 118 |
+
torch.testing.assert_close(
|
| 119 |
+
out_cpu_.to(device="cuda", dtype=dtype), out_cuda_.clone().detach(), **fwd_thresholds)
|
| 120 |
+
gO = gO.to(device="cuda", dtype=dtype)
|
| 121 |
+
out_cuda_.backward(gO)
|
| 122 |
+
self.assertFalse(out_cpu_.is_cuda)
|
| 123 |
+
self.assertTrue(out_cuda_.is_cuda)
|
| 124 |
+
torch.testing.assert_close(
|
| 125 |
+
input_.grad.to(device="cuda", dtype=dtype), input_cuda_.grad, **bwd_thresholds)
|
| 126 |
+
if elementwise_affine:
|
| 127 |
+
torch.testing.assert_close(module_cpu_.weight.grad.to(device="cuda", dtype=dtype),
|
| 128 |
+
module_cuda_.weight.grad, **bwd_thresholds)
|
| 129 |
+
|
| 130 |
+
# layer norm tests
|
| 131 |
+
@common_utils.parametrize(
|
| 132 |
+
"batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient",
|
| 133 |
+
list(product((16, 65536), (True, False), (False,), (False,), (torch.float,), (True, False)))
|
| 134 |
+
)
|
| 135 |
+
def test_layer_norm_regular(self, batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient):
|
| 136 |
+
self._test_fused_layer_norm(batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient)
|
| 137 |
+
|
| 138 |
+
@common_utils.parametrize(
|
| 139 |
+
"batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient",
|
| 140 |
+
list(product((16, 65536), (True, False), (True,), (False,), (torch.float,), (True, False)))
|
| 141 |
+
)
|
| 142 |
+
def test_layer_norm_elemwise(self, batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient):
|
| 143 |
+
self._test_fused_layer_norm(batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient)
|
| 144 |
+
|
| 145 |
+
@common_utils.parametrize(
|
| 146 |
+
"batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient",
|
| 147 |
+
list(product((16, 65536), (True, False), (True,), (True,), (torch.float,), (True, False)))
|
| 148 |
+
)
|
| 149 |
+
def test_layer_norm_mixed(self, batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient):
|
| 150 |
+
self._test_fused_layer_norm(batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient)
|
| 151 |
+
|
| 152 |
+
@common_utils.parametrize(
|
| 153 |
+
"batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient",
|
| 154 |
+
list(product((16,), (True, False), (True,), (False,), (torch.half,), (True, False)))
|
| 155 |
+
)
|
| 156 |
+
def test_layer_norm_half(self, batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient):
|
| 157 |
+
self._test_fused_layer_norm(batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient,
|
| 158 |
+
fwd_thresholds=dict(rtol=1e-3, atol=1e-3), bwd_thresholds=dict(rtol=1e-3, atol=1e-3))
|
| 159 |
+
|
| 160 |
+
@common_utils.parametrize(
|
| 161 |
+
"batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient",
|
| 162 |
+
list(product((16,), (True, False), (True,), (False,), (torch.bfloat16,), (True, False)))
|
| 163 |
+
)
|
| 164 |
+
def test_layer_norm_bfloat16(self, batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient):
|
| 165 |
+
self._test_fused_layer_norm(batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient,
|
| 166 |
+
fwd_thresholds=dict(rtol=1.6e-2, atol=3e-4), bwd_thresholds=dict(rtol=1.6e-2, atol=3e-3))
|
| 167 |
+
|
| 168 |
+
# rms norm tests
|
| 169 |
+
@common_utils.parametrize(
|
| 170 |
+
"batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient",
|
| 171 |
+
list(product((16, 65536), (True, False), (False,), (False,), (torch.float,), (True, False)))
|
| 172 |
+
)
|
| 173 |
+
def test_rms_norm_regular(self, batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient):
|
| 174 |
+
self._test_fused_rms_norm(batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient)
|
| 175 |
+
|
| 176 |
+
@common_utils.parametrize(
|
| 177 |
+
"batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient",
|
| 178 |
+
list(product((16, 65536), (True, False), (True,), (False,), (torch.float,), (True, False)))
|
| 179 |
+
)
|
| 180 |
+
def test_rms_norm_elemwise(self, batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient):
|
| 181 |
+
self._test_fused_rms_norm(batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient,
|
| 182 |
+
bwd_thresholds=dict(rtol=2e-3, atol=2e-4))
|
| 183 |
+
|
| 184 |
+
@common_utils.parametrize(
|
| 185 |
+
"batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient",
|
| 186 |
+
list(product((16, 65536), (True, False), (True,), (True,), (torch.float,), (True, False)))
|
| 187 |
+
)
|
| 188 |
+
def test_rms_norm_mixed(self, batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient):
|
| 189 |
+
self._test_fused_rms_norm(batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient,
|
| 190 |
+
bwd_thresholds=dict(rtol=2e-3, atol=2e-4))
|
| 191 |
+
|
| 192 |
+
@common_utils.parametrize(
|
| 193 |
+
"batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient",
|
| 194 |
+
list(product((16,), (True, False), (True,), (False,), (torch.half,), (True, False)))
|
| 195 |
+
)
|
| 196 |
+
def test_rms_norm_half(self, batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient):
|
| 197 |
+
self._test_fused_rms_norm(batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient,
|
| 198 |
+
bwd_thresholds = dict(rtol=1.6e-2, atol=3e-3))
|
| 199 |
+
|
| 200 |
+
@common_utils.parametrize(
|
| 201 |
+
"batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient",
|
| 202 |
+
list(product((16,), (True, False), (True,), (False,), (torch.bfloat16,), (True, False)))
|
| 203 |
+
)
|
| 204 |
+
def test_rms_norm_bfloat16(self, batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient):
|
| 205 |
+
self._test_fused_rms_norm(batch_size, contiguous, elementwise_affine, mixed_fused, dtype, memory_efficient,
|
| 206 |
+
fwd_thresholds=dict(rtol=1.6e-2, atol=3e-4), bwd_thresholds=dict(rtol=1.6e-2, atol=3e-2))
|
| 207 |
+
|
| 208 |
+
@common_utils.parametrize(
|
| 209 |
+
"dtype, elementwise_affine, memory_efficient",
|
| 210 |
+
list(product(autocast_dtypes, (True, False), (True, False)))
|
| 211 |
+
)
|
| 212 |
+
def test_autocast_fused_layer_norm(self, dtype, elementwise_affine, memory_efficient):
|
| 213 |
+
bf16_fwd_thresholds = dict(rtol=1.6e-2, atol=3e-4)
|
| 214 |
+
bf16_bwd_thresholds = dict(rtol=1.6e-2, atol=3e-3)
|
| 215 |
+
batch_size = 16
|
| 216 |
+
normalized_shape = [32, 16]
|
| 217 |
+
native = torch.nn.LayerNorm(
|
| 218 |
+
normalized_shape=normalized_shape, elementwise_affine=elementwise_affine
|
| 219 |
+
).to(device="cuda", dtype=dtype)
|
| 220 |
+
fused = FusedLayerNorm(
|
| 221 |
+
normalized_shape=normalized_shape, elementwise_affine=elementwise_affine, memory_efficient=memory_efficient
|
| 222 |
+
).cuda()
|
| 223 |
+
native_x, fused_x = _prep_inputs(batch_size, normalized_shape, dtype)
|
| 224 |
+
|
| 225 |
+
expected = native(native_x)
|
| 226 |
+
with torch.amp.autocast('cuda', dtype=dtype):
|
| 227 |
+
actual = fused(fused_x)
|
| 228 |
+
tols = {'rtol': None, 'atol': None} if dtype == torch.half else bf16_fwd_thresholds
|
| 229 |
+
# original tests used torch.testing.assert_allclose, which disables dtype checking by default.
|
| 230 |
+
# link to issue here: https://github.com/pytorch/pytorch/issues/61844
|
| 231 |
+
torch.testing.assert_close(actual, expected, **tols, check_dtype=False)
|
| 232 |
+
|
| 233 |
+
g_native = torch.rand_like(expected)
|
| 234 |
+
with torch.no_grad():
|
| 235 |
+
g_fused = g_native.clone()
|
| 236 |
+
expected.backward(g_native)
|
| 237 |
+
actual.backward(g_fused)
|
| 238 |
+
|
| 239 |
+
if dtype != torch.half:
|
| 240 |
+
tols = bf16_bwd_thresholds
|
| 241 |
+
elif memory_efficient:
|
| 242 |
+
tols = {'rtol': 1e-3, 'atol': 1e-4}
|
| 243 |
+
else:
|
| 244 |
+
tols = {'rtol': None, 'atol': None}
|
| 245 |
+
torch.testing.assert_close(native_x.grad, fused_x.grad, **tols, check_dtype=False)
|
| 246 |
+
@common_utils.parametrize(
|
| 247 |
+
"dtype, elementwise_affine, memory_efficient",
|
| 248 |
+
list(product(autocast_dtypes, (True, False), (True, False)))
|
| 249 |
+
)
|
| 250 |
+
def test_autocast_fused_rms_norm(self, dtype, elementwise_affine, memory_efficient):
|
| 251 |
+
bf16_fwd_thresholds = dict(rtol=1.6e-2, atol=3e-4)
|
| 252 |
+
bf16_bwd_thresholds = dict(rtol=1.6e-2, atol=3e-3)
|
| 253 |
+
batch_size = 16
|
| 254 |
+
normalized_shape = [32, 16]
|
| 255 |
+
native = FusedRMSNorm(
|
| 256 |
+
normalized_shape=normalized_shape, elementwise_affine=elementwise_affine, memory_efficient=memory_efficient,
|
| 257 |
+
).to(dtype=dtype)
|
| 258 |
+
fused = FusedRMSNorm(
|
| 259 |
+
normalized_shape=normalized_shape, elementwise_affine=elementwise_affine, memory_efficient=memory_efficient,
|
| 260 |
+
).cuda()
|
| 261 |
+
native_x, fused_x = _prep_inputs(batch_size, normalized_shape, dtype)
|
| 262 |
+
|
| 263 |
+
expected = native(native_x.cpu())
|
| 264 |
+
with torch.amp.autocast('cuda', dtype=dtype):
|
| 265 |
+
actual = fused(fused_x)
|
| 266 |
+
tols = {'rtol': None, 'atol': None} if dtype == torch.half else bf16_fwd_thresholds
|
| 267 |
+
torch.testing.assert_close(actual, expected.detach().clone().cuda(), **tols, check_dtype=False)
|
| 268 |
+
|
| 269 |
+
g_native = torch.rand_like(expected)
|
| 270 |
+
with torch.no_grad():
|
| 271 |
+
g_fused = g_native.detach().clone().cuda()
|
| 272 |
+
expected.backward(g_native)
|
| 273 |
+
actual.backward(g_fused)
|
| 274 |
+
|
| 275 |
+
tols = {'rtol': 1e-3, 'atol': 1e-3} if dtype == torch.half else bf16_bwd_thresholds
|
| 276 |
+
torch.testing.assert_close(native_x.grad.cuda(), fused_x.grad, **tols, check_dtype=False)
|
| 277 |
+
|
| 278 |
+
def _verify_export(self, fused, fused_x):
|
| 279 |
+
# check that export() is working
|
| 280 |
+
import io
|
| 281 |
+
f = io.BytesIO()
|
| 282 |
+
torch.onnx.export(fused, (fused_x,), f,
|
| 283 |
+
input_names=['x_in'],
|
| 284 |
+
opset_version=18,
|
| 285 |
+
)
|
| 286 |
+
# Load the ONNX model
|
| 287 |
+
import onnx
|
| 288 |
+
model_onnx = onnx.load_from_string(f.getvalue())
|
| 289 |
+
# Get string representation
|
| 290 |
+
onnx_str = onnx.helper.printable_graph(model_onnx.graph)
|
| 291 |
+
|
| 292 |
+
assert 'x_in' in onnx_str
|
| 293 |
+
assert 'ReduceMean' in onnx_str or 'LayerNormalization' in onnx_str
|
| 294 |
+
|
| 295 |
+
def test_rms_export(self):
|
| 296 |
+
batch_size = 16
|
| 297 |
+
normalized_shape = [32, 16]
|
| 298 |
+
fused = FusedRMSNorm(
|
| 299 |
+
normalized_shape=normalized_shape, elementwise_affine=True
|
| 300 |
+
).cuda()
|
| 301 |
+
fused_m = MixedFusedRMSNorm(
|
| 302 |
+
normalized_shape=normalized_shape
|
| 303 |
+
).cuda()
|
| 304 |
+
native_x, fused_x = _prep_inputs(batch_size, normalized_shape, torch.float32)
|
| 305 |
+
self._verify_export(fused, fused_x)
|
| 306 |
+
self._verify_export(fused_m, fused_x)
|
| 307 |
+
|
| 308 |
+
def test_layer_norm_export(self):
|
| 309 |
+
batch_size = 16
|
| 310 |
+
normalized_shape = [32, 16]
|
| 311 |
+
fused = FusedLayerNorm(
|
| 312 |
+
normalized_shape=normalized_shape, elementwise_affine=True
|
| 313 |
+
).cuda()
|
| 314 |
+
fused_m = MixedFusedLayerNorm(
|
| 315 |
+
normalized_shape=normalized_shape
|
| 316 |
+
).cuda()
|
| 317 |
+
native_x, fused_x = _prep_inputs(batch_size, normalized_shape, torch.float32)
|
| 318 |
+
self._verify_export(fused, fused_x)
|
| 319 |
+
self._verify_export(fused_m, fused_x)
|
| 320 |
+
|
| 321 |
+
@common_utils.parametrize("elementwise_affine", (True, False))
|
| 322 |
+
def test_compile_fused_layer_norm(self, elementwise_affine):
|
| 323 |
+
batch_size = 16
|
| 324 |
+
normalized_shape = [32, 16]
|
| 325 |
+
eager_mod = FusedLayerNorm(
|
| 326 |
+
normalized_shape=normalized_shape, elementwise_affine=elementwise_affine
|
| 327 |
+
).cuda()
|
| 328 |
+
compiled_mod = torch.compile(fullgraph=True)(eager_mod)
|
| 329 |
+
input_shape = [batch_size] + normalized_shape
|
| 330 |
+
eager_x = torch.randn(input_shape, device="cuda").requires_grad_(True)
|
| 331 |
+
compiled_x = eager_x.detach().clone().requires_grad_(True)
|
| 332 |
+
|
| 333 |
+
expected = eager_mod(eager_x)
|
| 334 |
+
actual = compiled_mod(compiled_x)
|
| 335 |
+
torch.testing.assert_close(actual, expected.detach())
|
| 336 |
+
|
| 337 |
+
g_eager = torch.rand_like(expected)
|
| 338 |
+
with torch.no_grad():
|
| 339 |
+
g_compiled = g_eager.detach().clone()
|
| 340 |
+
expected.backward(g_eager)
|
| 341 |
+
actual.backward(g_compiled)
|
| 342 |
+
|
| 343 |
+
torch.testing.assert_close(eager_x.grad, compiled_x.grad)
|
| 344 |
+
|
| 345 |
+
@common_utils.parametrize("elementwise_affine", (True, False))
|
| 346 |
+
def test_compile_fused_rms_norm(self, elementwise_affine):
|
| 347 |
+
batch_size = 16
|
| 348 |
+
normalized_shape = [32, 16]
|
| 349 |
+
eager_mod = FusedRMSNorm(
|
| 350 |
+
normalized_shape=normalized_shape, elementwise_affine=elementwise_affine
|
| 351 |
+
).cuda()
|
| 352 |
+
compiled_mod = torch.compile(fullgraph=True)(eager_mod)
|
| 353 |
+
input_shape = [batch_size] + normalized_shape
|
| 354 |
+
eager_x = torch.randn(input_shape, device="cuda").requires_grad_(True)
|
| 355 |
+
compiled_x = eager_x.detach().clone().requires_grad_(True)
|
| 356 |
+
|
| 357 |
+
expected = eager_mod(eager_x)
|
| 358 |
+
actual = compiled_mod(compiled_x)
|
| 359 |
+
torch.testing.assert_close(actual, expected.detach())
|
| 360 |
+
|
| 361 |
+
g_eager = torch.rand_like(expected)
|
| 362 |
+
with torch.no_grad():
|
| 363 |
+
g_compiled = g_eager.detach().clone()
|
| 364 |
+
expected.backward(g_eager)
|
| 365 |
+
actual.backward(g_compiled)
|
| 366 |
+
|
| 367 |
+
torch.testing.assert_close(eager_x.grad, compiled_x.grad)
|
| 368 |
+
|
| 369 |
+
instantiate_device_type_tests(TestFusedLayerNorm, globals(), only_for=("cuda",))
|
| 370 |
+
if __name__ == "__main__":
|
| 371 |
+
common_utils.run_tests()
|