MCPcopy Create free account
hub / github.com/boyiwei/alignment-attribution-code / ActLinear

Class ActLinear

main_low_rank_diff.py:18–43  ·  view source on GitHub ↗

drop in replacement of nn.Linear

Source from the content-addressed store, hash-verified

16
17
18class ActLinear(nn.Module):
19 """
20 drop in replacement of nn.Linear
21 """
22
23 def __init__(self, base: nn.Linear):
24 super().__init__()
25 self.base = base
26 self.activation_norms = [] # offload to CPU
27 self.record_activation = True
28
29 def clear_act_buffer(self):
30 self.activation_norms = []
31
32 def forward(self, x):
33 if self.record_activation:
34 if hasattr(self, "mask") and self.mask is not None:
35 x_ = x[self.mask] # num * dim
36 else:
37 x_ = x # bs * seq_len * dim
38 self.activation_norms.append(
39 x_.view(-1, x_.shape[-1]).cpu()
40 ) # offload to CPU.
41
42 out = self.base(x)
43 return out
44
45
46class no_act_recording:

Callers 1

make_ActFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected