forked from nimashoghi/smart-quantization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfp8.py
31 lines (25 loc) · 910 Bytes
/
fp8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from argparse import ArgumentParser, Namespace
import torch
from smart_compress.compress.base import CompressionAlgorithmBase
from smart_compress.util.pytorch.quantization import (
add_float_quantize_args,
float_quantize,
)
class FP8(CompressionAlgorithmBase):
@staticmethod
def add_argparse_args(parent_parser: ArgumentParser):
parser = ArgumentParser(
parents=[
add_float_quantize_args(
CompressionAlgorithmBase.add_argparse_args(parent_parser)
)
],
add_help=False,
)
return parser
def __init__(self, hparams: Namespace):
super().__init__(hparams)
@torch.no_grad()
def __call__(self, tensor: torch.Tensor, tag: str = None, **_):
self.log_ratio(tag, tensor.numel(), 32, 8)
return float_quantize(tensor, exp=5, man=2, hparams=self.hparams)