forked from ccf-ai-infra/GPUCodeForces
47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
import torch
|
|
import torch.nn as nn
|
|
|
|
class Model(nn.Module):
|
|
"""
|
|
Simple model that performs a GroupNorm operation.
|
|
"""
|
|
def __init__(self, num_channels, num_groups=8, eps=1e-5, affine=True):
|
|
super(Model, self).__init__()
|
|
self.num_channels = num_channels
|
|
self.num_groups = num_groups
|
|
self.eps = eps
|
|
self.affine = affine
|
|
|
|
# 创建GroupNorm层
|
|
self.group_norm = nn.GroupNorm(
|
|
num_groups=num_groups,
|
|
num_channels=num_channels,
|
|
eps=eps,
|
|
affine=affine
|
|
)
|
|
|
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
"""
|
|
Applies GroupNorm to the input tensor.
|
|
|
|
Args:
|
|
x (torch.Tensor): Input tensor of shape [batch_size, num_channels, height, width]
|
|
|
|
Returns:
|
|
torch.Tensor: Output tensor after group normalization, same shape as input.
|
|
"""
|
|
return self.group_norm(x)
|
|
|
|
batch_size = 32
|
|
num_channels = 64
|
|
height = 32
|
|
width = 32
|
|
num_groups = 8
|
|
|
|
def get_inputs():
|
|
x = torch.randn(batch_size, num_channels, height, width)
|
|
return [x]
|
|
|
|
def get_init_inputs():
|
|
return [num_channels, num_groups] # GroupNorm needs num_channels and num_groups
|