Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions bitsandbytes/autograd/_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ def backward(ctx: torch.autograd.function.FunctionCtx, grad_output: torch.Tensor

if req_gradBias:
# compute grad_bias first before changing grad_output dtype
grad_bias = grad_output.sum(0, dtype=ctx.dtype_bias)
grad_bias = grad_output.reshape(-1, grad_output.shape[-1]).sum(0, dtype=ctx.dtype_bias)

# Cast grad_output to fp16
if len(grad_output.shape) == 3:
if len(grad_output.shape) != 2:
grad_output = grad_output.reshape(-1, grad_output.shape[-1]).contiguous()

if req_gradB:
Expand Down Expand Up @@ -278,14 +278,15 @@ def backward(ctx, grad_output):
grad_A = grad_B = grad_bias = None
if req_gradBias:
# compute grad_bias first before changing grad_output dtype
grad_bias = grad_output.sum(0, dtype=ctx.dtype_bias)
grad_bias = grad_output.reshape(-1, grad_output.shape[-1]).sum(0, dtype=ctx.dtype_bias)

# Cast grad_output to fp16
if len(grad_output.shape) == 3:
if len(grad_output.shape) != 2:
grad_output = grad_output.reshape(-1, grad_output.shape[-1]).contiguous()

if req_gradB:
grad_B = torch.matmul(A.t(), grad_output).t()
A_2d = A.reshape(-1, A.shape[-1])
grad_B = torch.matmul(A_2d.t(), grad_output).t()

if req_gradA:
if state.CB is not None:
Expand Down Expand Up @@ -374,7 +375,7 @@ def backward(ctx, grad_output):

if req_gradBias:
# compute grad_bias first before changing grad_output dtype
grad_bias = grad_output.sum(0, dtype=ctx.dtype_bias)
grad_bias = grad_output.reshape(-1, grad_output.shape[-1]).sum(0, dtype=ctx.dtype_bias)

# not supported by PyTorch. TODO: create work-around
# if req_gradB: grad_B = torch.matmul(grad_output.t(), A)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_linear4bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,14 @@ def test_fsdp_state_dict_save_4bit():
f"stdout: {result.stdout}\n"
f"stderr: {result.stderr}"
)


@pytest.mark.parametrize("shape", [(32,), (4, 32), (2, 3, 32), (2, 2, 3, 32)])
@pytest.mark.parametrize("device", get_available_devices())
def test_linear4bit_bias_gradient_input_ranks(shape, device):
layer = bnb.nn.LinearNF4(32, 64, bias=True).to(device)
x = torch.randn(shape, device=device, requires_grad=True)
out = layer(x)
out.sum().backward()
assert layer.bias.grad is not None
assert layer.bias.grad.shape == (64,)
22 changes: 22 additions & 0 deletions tests/test_linear8bitlt.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,25 @@ def test_linear8bitlt_device_movement(device):

# Accelerator outputs should match both times.
torch.testing.assert_close(out_accelerator_2, out_accelerator, rtol=1e-8, atol=1e-8)


@pytest.mark.parametrize("shape", [(32,), (4, 32), (2, 3, 32), (2, 2, 3, 32)])
@pytest.mark.parametrize("device", get_available_devices())
def test_linear8bitlt_bias_gradient_input_ranks(shape, device):
layer = bnb.nn.Linear8bitLt(32, 64, bias=True, has_fp16_weights=False).to(device)
x = torch.randn(shape, device=device, requires_grad=True)
out = layer(x)
out.sum().backward()
assert layer.bias.grad is not None
assert layer.bias.grad.shape == (64,)


@pytest.mark.parametrize("shape", [(32,), (2, 3, 32), (2, 2, 2, 32)])
@pytest.mark.parametrize("device", get_available_devices())
def test_linear8bitlt_fp16_weights_backward_non_2d_ranks(shape, device):
layer = bnb.nn.Linear8bitLt(32, 64, bias=False, has_fp16_weights=True).to(device)
x = torch.randn(shape, device=device, requires_grad=True)
out = layer(x)
out.sum().backward()
assert layer.weight.grad is not None
assert layer.weight.grad.shape == (64, 32)