Spaces:
Sleeping
Sleeping
Create core/reevaluator.py
Browse files- core/reevaluator.py +32 -0
core/reevaluator.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn.functional as F
|
| 3 |
+
from .quantizer import fake_quantize
|
| 4 |
+
|
| 5 |
+
def reevaluate(model, tokenizer, texts, policy, threshold=0.01):
|
| 6 |
+
|
| 7 |
+
inputs = tokenizer(texts, return_tensors="pt", padding=True)
|
| 8 |
+
|
| 9 |
+
with torch.no_grad():
|
| 10 |
+
baseline = model(**inputs).logits
|
| 11 |
+
|
| 12 |
+
original = {}
|
| 13 |
+
|
| 14 |
+
for name, module in model.named_modules():
|
| 15 |
+
if name in policy and policy[name] != "FP16":
|
| 16 |
+
bits = 4 if policy[name] == "INT4" else 8
|
| 17 |
+
original[name] = {}
|
| 18 |
+
for pname, param in module.named_parameters():
|
| 19 |
+
original[name][pname] = param.data.clone()
|
| 20 |
+
param.data = fake_quantize(param.data, bits)
|
| 21 |
+
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
mixed = model(**inputs).logits
|
| 24 |
+
|
| 25 |
+
for name, module in model.named_modules():
|
| 26 |
+
if name in original:
|
| 27 |
+
for pname, param in module.named_parameters():
|
| 28 |
+
param.data = original[name][pname]
|
| 29 |
+
|
| 30 |
+
degradation = F.mse_loss(baseline, mixed).item()
|
| 31 |
+
|
| 32 |
+
return policy, degradation
|