forked from ccf-ai-infra/GPUCodeForces
46 lines
1.1 KiB
Python
46 lines
1.1 KiB
Python
import torch
|
|
import torch.nn as nn
|
|
import torch.nn.functional as F
|
|
|
|
# --- Hyperparameters ---
|
|
N, D = 16, 512 # Batch Size, Vector Length
|
|
DIM = 1 # 默认在 D 维度上进行 Softmin
|
|
|
|
|
|
class Softmin(nn.Module):
|
|
|
|
def __init__(self, dim=-1):
|
|
super().__init__()
|
|
self.dim = dim
|
|
|
|
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
|
# 确保输入是 float 类型
|
|
if input.dtype != torch.float32:
|
|
input = input.float()
|
|
|
|
# 核心:对负输入进行 Softmax
|
|
return torch.softmax(-input, dim=self.dim)
|
|
|
|
|
|
class Model(nn.Module):
|
|
def __init__(self, dim=DIM):
|
|
super().__init__()
|
|
self.op = Softmin(dim=dim)
|
|
|
|
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
|
return self.op(input)
|
|
|
|
|
|
# --- 辅助函数 ---
|
|
|
|
def get_inputs():
|
|
"""返回用于前向传播的随机输入张量。"""
|
|
torch.manual_seed(42)
|
|
# 2D 张量 (N, D)
|
|
x = torch.randn(N, D, dtype=torch.float32) * 5.0
|
|
return [x]
|
|
|
|
|
|
def get_init_inputs():
|
|
"""返回用于初始化模型的参数。"""
|
|
return [DIM] |