-
Notifications
You must be signed in to change notification settings - Fork 177
/
unified.py
36 lines (28 loc) · 925 Bytes
/
unified.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
32
33
34
35
36
from abc import ABC, abstractmethod
from typing import Any
import torch
"""
The vast majority of quantization algorithms follow one of two patterns
1. Single quantize call to create a quantized model with quantized state_dict
2. Flow that needs calibration or training
This file defines the API for both patterns
"""
# API 1, single quantize call to create a quantized model with quantized state_dict
class Quantizer(ABC):
@abstractmethod
def quantize(
self, model: torch.nn.Module, *args: Any, **kwargs: Any
) -> torch.nn.Module:
pass
# API 2, flow that needs calibration or training
class TwoStepQuantizer:
@abstractmethod
def prepare(
self, model: torch.nn.Module, *args: Any, **kwargs: Any
) -> torch.nn.Module:
pass
@abstractmethod
def convert(
self, model: torch.nn.Module, *args: Any, **kwargs: Any
) -> torch.nn.Module:
pass