andylizf's picture
Upload folder using huggingface_hub
5fed0fc verified
import torch
import torch.nn.functional as F
def fused_linear_ce(X: torch.Tensor, W: torch.Tensor, B: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
"""
Baseline fused linear cross entropy implementation using PyTorch.
Args:
X: Input tensor of shape (M, K) - input features (float16)
W: Weight tensor of shape (K, N) - weight matrix (float16)
B: Bias tensor of shape (N,) - bias vector (float32)
targets: Target tensor of shape (M,) - target class indices (int64)
Returns:
Output tensor of shape (M,) - negative log-likelihood loss per sample (float32)
"""
logits = (X @ W).float() + B.float()
return F.cross_entropy(logits, targets, reduction='none')