Markus28 commited on
Commit
871fd36
1 Parent(s): 4164fd6

fix: try to get from_config to work

Browse files
Files changed (2) hide show
  1. modeling_bert.py +1 -1
  2. patched_padding_bert.py +0 -39
modeling_bert.py CHANGED
@@ -336,7 +336,7 @@ class BertPreTrainedModel(nn.Module):
336
  return model
337
 
338
  @classmethod
339
- def from_config(cls, config, *inputs, **kwargs):
340
  model = cls(config, *inputs, **kwargs)
341
  return model
342
 
 
336
  return model
337
 
338
  @classmethod
339
+ def _from_config(cls, config, *inputs, **kwargs):
340
  model = cls(config, *inputs, **kwargs)
341
  return model
342
 
patched_padding_bert.py DELETED
@@ -1,39 +0,0 @@
1
- """Source https://github.com/Dao-AILab/flash-attention/blob/87a1277653fc55cd615f5341255e00c69d5c00a1/flash_attn/bert_padding.py
2
-
3
- We replace the gather in `IndexFirstAxis.forward` with an indexing operation
4
- """
5
- import torch
6
- from einops import rearrange, repeat
7
-
8
- class IndexFirstAxis(torch.autograd.Function):
9
- @staticmethod
10
- def forward(ctx, input, indices, indexing=False):
11
- ctx.save_for_backward(indices)
12
- assert input.ndim >= 2
13
- ctx.first_axis_dim, other_shape = input.shape[0], input.shape[1:]
14
- # second_dim = other_shape.numel()
15
- # TD [2022-03-04] For some reason torch.gather is a bit faster than indexing.
16
- # return input[indices]
17
- #return torch.gather(
18
- # rearrange(input, "b ... -> b (...)"), 0, repeat(indices, "z -> z d", d=second_dim)
19
- #).reshape(-1, *other_shape)
20
- return input[indices]
21
-
22
- @staticmethod
23
- def backward(ctx, grad_output):
24
- (indices,) = ctx.saved_tensors
25
- assert grad_output.ndim >= 2
26
- other_shape = grad_output.shape[1:]
27
- grad_output = rearrange(grad_output, "b ... -> b (...)")
28
- grad_input = torch.zeros(
29
- [ctx.first_axis_dim, grad_output.shape[1]],
30
- device=grad_output.device,
31
- dtype=grad_output.dtype,
32
- )
33
- # TD [2022-03-04] For some reason torch.scatter is a bit faster than indexing.
34
- # grad_input[indices] = grad_output
35
- grad_input.scatter_(0, repeat(indices, "z -> z d", d=grad_output.shape[1]), grad_output)
36
- return grad_input.reshape(ctx.first_axis_dim, *other_shape), None
37
-
38
-
39
- index_first_axis = IndexFirstAxis.apply