Add brains/sovereign-nano/quantize_cpu.py
Browse files
brains/sovereign-nano/quantize_cpu.py
CHANGED
|
@@ -26,9 +26,15 @@ import torch.nn as nn
|
|
| 26 |
|
| 27 |
|
| 28 |
def quantize_dynamic(model: nn.Module) -> nn.Module:
|
| 29 |
-
"""Quantize Linear + GRU to qint8 (in place). Convs stay fp32.
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
def quantize_dynamic(model: nn.Module) -> nn.Module:
|
| 29 |
+
"""Quantize Linear + GRU to qint8 (in place). Convs stay fp32.
|
| 30 |
+
Uses torch.ao.quantization (the canonical path since 1.13; the old
|
| 31 |
+
torch.quantization alias emits a deprecation warning — pipeline
|
| 32 |
+
review finding 3)."""
|
| 33 |
+
try:
|
| 34 |
+
from torch.ao import quantization as tq
|
| 35 |
+
except ImportError:
|
| 36 |
+
import torch.quantization as tq # torch < 1.13 fallback
|
| 37 |
+
return tq.quantize_dynamic(model, {nn.Linear, nn.GRU}, dtype=torch.qint8)
|
| 38 |
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|