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

Method fasterprune

lib/sparsegpt.py:43–124  ·  view source on GitHub ↗
(self, sparsity, prune_n=0, prune_m=0, blocksize=128, percdamp=0.01)

Source from the content-addressed store, hash-verified

41 self.H += inp.matmul(inp.t())
42
43 def fasterprune(self, sparsity, prune_n=0, prune_m=0, blocksize=128, percdamp=0.01):
44 W = self.layer.weight.data.clone()
45 if isinstance(self.layer, nn.Conv2d):
46 W = W.flatten(1)
47 if isinstance(self.layer, transformers.Conv1D):
48 W = W.t()
49 W = W.float()
50
51 tick = time.time()
52
53 H = self.H
54 del self.H
55 dead = torch.diag(H) == 0
56 H[dead, dead] = 1
57 W[:, dead] = 0
58
59 Losses = torch.zeros(self.rows, device=self.dev)
60
61 damp = percdamp * torch.mean(torch.diag(H))
62 diag = torch.arange(self.columns, device=self.dev)
63 H[diag, diag] += damp
64 H = torch.linalg.cholesky(H)
65 H = torch.cholesky_inverse(H)
66 H = torch.linalg.cholesky(H, upper=True)
67 Hinv = H
68
69 mask = None
70
71 for i1 in range(0, self.columns, blocksize):
72 i2 = min(i1 + blocksize, self.columns)
73 count = i2 - i1
74
75 W1 = W[:, i1:i2].clone()
76 Q1 = torch.zeros_like(W1)
77 Err1 = torch.zeros_like(W1)
78 Losses1 = torch.zeros_like(W1)
79 Hinv1 = Hinv[i1:i2, i1:i2]
80
81 if prune_n == 0:
82 if mask is not None:
83 mask1 = mask[:, i1:i2]
84 else:
85 tmp = W1**2 / (torch.diag(Hinv1).reshape((1, -1))) ** 2
86 thresh = torch.sort(tmp.flatten())[0][int(tmp.numel() * sparsity)]
87 mask1 = tmp <= thresh
88 else:
89 mask1 = torch.zeros_like(W1) == 1
90
91 for i in range(count):
92 w = W1[:, i]
93 d = Hinv1[i, i]
94
95 if prune_n != 0 and i % prune_m == 0:
96 tmp = (
97 W1[:, i : (i + prune_m)] ** 2
98 / (torch.diag(Hinv1)[i : (i + prune_m)].reshape((1, -1))) ** 2
99 )
100 mask1.scatter_(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected