bitsandbytes icon indicating copy to clipboard operation
bitsandbytes copied to clipboard

Output 0 of MatMul8bitLtBackward is a view and is being modified inplace.

Open richardwth opened this issue 2 years ago • 9 comments

I would like to report a bug using MatMul8bitLt.

Bug description

When I used the following three in my code:

  • flash_attn_varlen_func from flash_attn (v2.0.8, Github Link),
  • MatMul8bit from bitsandbytes (v0.41.1),
  • Linear8bitLt from PEFT (v0.3.0dev0, Github Link),

I came across the following error: RuntimeError: Output 0 of MatMul8bitLtBackward is a view and is being modified inplace. This view was created inside a custom Function (or because an input was returned as-is) and the autograd logic to handle view+inplace would override the custom backward associated with the custom Function, leading to incorrect gradients. This behavior is forbidden. You can fix this by cloning the output of the custom Function.

How to fix it

The error can be fixed** by modifying Line 440 and 441 of bitsandbytes/autograd/_functions.py:

clone_func = torch.clone if len(output_shape) == 3 else lambda x: x
return clone_func(output.view(output_shape))

to

if len(output_shape) == 3:
    return output.view(output_shape).clone()
else:
    return output

Source of bug

This error happens because:

  1. flash_attn_varlen_func requires the q,k,v inputs of self-attention to be 2D of shape (total_tokens, hidden_size);
  2. in case of 2D inputs, output_shape is 2 and torch.clone is not used;
  3. in Linear8bitLt.forward, the result from bnb.nn.Linear8bitLt is modified inplace: result += output;
  4. for some reason, the autograd logic does not allow this.

richardwth avatar Aug 27 '23 12:08 richardwth

I got this error too when I tried to fine tune a 8bit model with peft

luvwinnie avatar Oct 05 '23 04:10 luvwinnie

me too!

harper-carroll avatar Dec 12 '23 22:12 harper-carroll

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

github-actions[bot] avatar Jan 06 '24 15:01 github-actions[bot]

same here

Muhtasham avatar Jan 11 '24 08:01 Muhtasham

I get the same error.

zimmer-m avatar Jan 17 '24 17:01 zimmer-m

Same error.

lavanyanemani96 avatar Jan 20 '24 07:01 lavanyanemani96

Will try to go down the rabbit hole to fix it

Muhtasham avatar Jan 20 '24 21:01 Muhtasham

The same error occurred for me.

michalszc avatar Oct 13 '24 21:10 michalszc

The same error occurred for me.

I provided a fix. Hope it helps.

richardwth avatar Oct 14 '24 18:10 richardwth

Hi all,

Does this issue remain with v0.45.0 and the latest PEFT? Happy to revisit it if so!

matthewdouglas avatar Jan 22 '25 22:01 matthewdouglas