GPUCodeForces/S1/Ljy123_#1/layernorm_torchcode.py

23 lines
713 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import torch
import torch.nn as nn
class Model(nn.Module):
def __init__(self, normalized_shape, eps=1e-5):
super(Model, self).__init__()
self.normalized_shape = normalized_shape
self.eps = eps
# 使用PyTorch内置的LayerNorm这是标准实现
self.layer_norm = nn.LayerNorm(normalized_shape, eps=eps)
def forward(self, x):
# 使用PyTorch内置LayerNorm这是标准的实现方式
return self.layer_norm(x)
def get_inputs():
# 使用更大的输入尺寸以获得更好的性能对比
# 增加batch size和hidden size模拟真实场景
return [torch.randn(16, 16384)]
def get_init_inputs():
return [16384]