LayerNorm
Scenario Description
Normalize tensors via the mean square error. Currently, KuDNN supports the torch.float16 and torch.float32 data types. For other data types, see the open-source branch.
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import torch import torch.nn as nn # Enable KuDNN. torch._C._set_kdnn_enabled(True) # Input data: (batch_size, seq_len, hidden_dim) input = torch.randn(2, 5, 10) # 2 samples, 5 time steps, 10 feature dimensions, fp32 as the default data type # Define LayerNorm (normalizing the last dimension hidden_dim=10). layer_norm = nn.LayerNorm(10) # Or normalized_shape=[5,10] to normalize the last two dimensions. The default data type is fp32. # Forward computation output = layer_norm(input) # Verification: After normalization, the mean value of the last dimension is close to 0, and the variance is close to 1. print("LayerNorm output mean:", output.mean(dim=-1)) # Should be close to 0. print("LayerNorm output variance:", output.var(dim=-1, unbiased=False)) # Should be close to 1. |
Parent topic: Examples