refactor: align linear backward dtype with weight
- cast gradients and inputs to weight.dtype instead of hardcoded bf16 - single code path covers bf16 and fp32 models, no branch needed - gradient dtype now matches the leaf parameter dtype exactly
This commit is contained in:
@@ -35,31 +35,32 @@ def _linear_cuda_impl(x: torch.Tensor, w: torch.Tensor, bias=None):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _linear_backward_cuda_impl(input, grad_output, weight, output_mask):
|
def _linear_backward_cuda_impl(input_tensor, grad_output, weight, output_mask):
|
||||||
# VariableType wraps aten::linear; its backward runs aten::linear_backward
|
# VariableType wraps aten::linear; its backward runs aten::linear_backward
|
||||||
# with schema (self, grad_output, weight, mask). Implement the bf16
|
# with schema (self, grad_output, weight, mask). weight is the leaf
|
||||||
# gradient math directly (no redispatch), supporting [..., K] inputs:
|
# parameter, so its dtype is the model-precision baseline; cast everything
|
||||||
# dX = g @ W, dW = g^T @ X, dB = sum(g, dim=0)
|
# to it (bf16 model -> bf16 GEMMs, fp32 model -> fp32, no branch):
|
||||||
g = grad_output.to(torch.bfloat16)
|
# grad_input = g @ W, grad_weight = g^T @ X, grad_bias = sum(g, dim=0)
|
||||||
g2d = g.reshape(-1, weight.size(0))
|
compute_dtype = weight.dtype
|
||||||
x2d = input.reshape(-1, input.size(-1)).to(torch.bfloat16)
|
grad = grad_output.to(compute_dtype)
|
||||||
dX = (
|
grad_2d = grad.reshape(-1, weight.size(0))
|
||||||
torch.mm(g2d, weight)
|
input_2d = input_tensor.reshape(-1, input_tensor.size(-1)).to(compute_dtype)
|
||||||
|
grad_input = (
|
||||||
|
torch.mm(grad_2d, weight)
|
||||||
if output_mask[0]
|
if output_mask[0]
|
||||||
else torch.empty(0, device=input.device, dtype=input.dtype)
|
else torch.empty(0, device=input_tensor.device, dtype=input_tensor.dtype)
|
||||||
)
|
)
|
||||||
dX = dX.reshape_as(input)
|
grad_weight = (
|
||||||
dW = (
|
torch.mm(grad_2d.t(), input_2d)
|
||||||
torch.mm(g2d.t(), x2d)
|
|
||||||
if output_mask[1]
|
if output_mask[1]
|
||||||
else torch.empty(0, device=input.device, dtype=input.dtype)
|
else torch.empty(0, device=input_tensor.device, dtype=input_tensor.dtype)
|
||||||
)
|
)
|
||||||
dB = (
|
grad_bias = (
|
||||||
g.sum(dim=0)
|
grad.sum(dim=0)
|
||||||
if output_mask[2]
|
if output_mask[2]
|
||||||
else torch.empty(0, device=input.device, dtype=input.dtype)
|
else torch.empty(0, device=input_tensor.device, dtype=input_tensor.dtype)
|
||||||
)
|
)
|
||||||
return dX, dW, dB
|
return grad_input.reshape_as(input_tensor), grad_weight, grad_bias
|
||||||
|
|
||||||
|
|
||||||
_lib = Library("aten", "IMPL", "CUDA")
|
_lib = Library("aten", "IMPL", "CUDA")
|
||||||
|
|||||||
Reference in New Issue
Block a user