-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathunified.py
41 lines (33 loc) · 1.11 KB
/
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
37
38
39
40
41
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD 3-Clause license found in the
# LICENSE file in the root directory of this source tree.
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