tico 0.1.0.dev250819__py3-none-any.whl → 0.1.0.dev250820__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tico/__init__.py +1 -1
- tico/experimental/quantization/ptq/wrappers/nn/__init__.py +5 -0
- tico/experimental/quantization/ptq/wrappers/nn/quant_linear.py +66 -0
- tico/experimental/quantization/ptq/wrappers/registry.py +112 -0
- {tico-0.1.0.dev250819.dist-info → tico-0.1.0.dev250820.dist-info}/METADATA +1 -1
- {tico-0.1.0.dev250819.dist-info → tico-0.1.0.dev250820.dist-info}/RECORD +10 -7
- {tico-0.1.0.dev250819.dist-info → tico-0.1.0.dev250820.dist-info}/LICENSE +0 -0
- {tico-0.1.0.dev250819.dist-info → tico-0.1.0.dev250820.dist-info}/WHEEL +0 -0
- {tico-0.1.0.dev250819.dist-info → tico-0.1.0.dev250820.dist-info}/entry_points.txt +0 -0
- {tico-0.1.0.dev250819.dist-info → tico-0.1.0.dev250820.dist-info}/top_level.txt +0 -0
tico/__init__.py
CHANGED
@@ -0,0 +1,66 @@
|
|
1
|
+
# Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
from typing import Optional
|
16
|
+
|
17
|
+
import torch.nn as nn
|
18
|
+
import torch.nn.functional as F
|
19
|
+
|
20
|
+
from tico.experimental.quantization.ptq.mode import Mode
|
21
|
+
from tico.experimental.quantization.ptq.qscheme import QScheme
|
22
|
+
from tico.experimental.quantization.ptq.quant_config import QuantConfig
|
23
|
+
from tico.experimental.quantization.ptq.wrappers.quant_module_base import (
|
24
|
+
QuantModuleBase,
|
25
|
+
)
|
26
|
+
from tico.experimental.quantization.ptq.wrappers.registry import register
|
27
|
+
|
28
|
+
|
29
|
+
@register(nn.Linear)
|
30
|
+
class QuantLinear(QuantModuleBase):
|
31
|
+
"""Per-channel weight fake-quant, eager-output activation fake-quant."""
|
32
|
+
|
33
|
+
def __init__(
|
34
|
+
self,
|
35
|
+
fp: nn.Linear,
|
36
|
+
*,
|
37
|
+
qcfg: Optional[QuantConfig] = None,
|
38
|
+
fp_name: Optional[str] = None
|
39
|
+
):
|
40
|
+
super().__init__(qcfg, fp_name=fp_name)
|
41
|
+
self.weight_obs = self._make_obs(
|
42
|
+
"weight", qscheme=QScheme.PER_CHANNEL_ASYMM, channel_axis=0
|
43
|
+
)
|
44
|
+
self.act_in_obs = self._make_obs("act_in")
|
45
|
+
self.act_out_obs = self._make_obs("act_out")
|
46
|
+
self.module = fp
|
47
|
+
|
48
|
+
def enable_calibration(self) -> None:
|
49
|
+
super().enable_calibration()
|
50
|
+
# immediately capture the fixed weight range
|
51
|
+
self.weight_obs.collect(self.module.weight)
|
52
|
+
|
53
|
+
def forward(self, x):
|
54
|
+
x_q = self._fq(x, self.act_in_obs)
|
55
|
+
|
56
|
+
w = self.module.weight
|
57
|
+
if self._mode is Mode.QUANT:
|
58
|
+
w = self.weight_obs.fake_quant(w)
|
59
|
+
b = self.module.bias
|
60
|
+
|
61
|
+
out = F.linear(x_q, w, b)
|
62
|
+
|
63
|
+
return self._fq(out, self.act_out_obs)
|
64
|
+
|
65
|
+
def _all_observers(self):
|
66
|
+
return (self.weight_obs, self.act_in_obs, self.act_out_obs)
|
@@ -0,0 +1,112 @@
|
|
1
|
+
# Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
import importlib
|
16
|
+
from typing import Callable, Dict, Type
|
17
|
+
|
18
|
+
import torch.nn as nn
|
19
|
+
|
20
|
+
from tico.experimental.quantization.ptq.wrappers.quant_module_base import (
|
21
|
+
QuantModuleBase,
|
22
|
+
)
|
23
|
+
|
24
|
+
_WRAPPERS: Dict[Type[nn.Module], Type[QuantModuleBase]] = {}
|
25
|
+
_IMPORT_ONCE = False
|
26
|
+
_CORE_MODULES = (
|
27
|
+
"tico.experimental.quantization.ptq.wrappers.nn.quant_linear",
|
28
|
+
# add future core wrappers here
|
29
|
+
)
|
30
|
+
|
31
|
+
|
32
|
+
def _lazy_init():
|
33
|
+
"""
|
34
|
+
Deferred one-shot import of "core wrapper modules".
|
35
|
+
|
36
|
+
Why not import everything when the program first starts?
|
37
|
+
--------------------------------------------------
|
38
|
+
* **Avoid circular-import hell**
|
39
|
+
Core wrappers often import `PTQWrapper`, which in turn calls
|
40
|
+
`registry.lookup()`. Importing those files eagerly here would create a
|
41
|
+
cycle (`registry → wrapper → registry`). Delaying the import until the
|
42
|
+
*first* `lookup()` call lets Python finish constructing the registry
|
43
|
+
module before any wrapper files are touched.
|
44
|
+
|
45
|
+
* **Cold-start speed**
|
46
|
+
Most user code never wraps layers explicitly; they only hit
|
47
|
+
`PTQWrapper` if they are doing quantization. Deferring half-a-dozen
|
48
|
+
heavyweight `import torch …` files until they are really needed
|
49
|
+
reduces library start-up latency in the common path.
|
50
|
+
|
51
|
+
* **Optional dependencies**
|
52
|
+
Core wrappers listed in `_CORE_MODULES` are chosen to be dependency-free
|
53
|
+
(pure PyTorch). Anything that needs `transformers`, `torchvision`,
|
54
|
+
etc. uses the `@try_register()` decorator inside its own module. Those
|
55
|
+
optional modules are *not* imported here, so users without the extra
|
56
|
+
packages still get a clean import.
|
57
|
+
|
58
|
+
Implementation notes
|
59
|
+
--------------------
|
60
|
+
* `_IMPORT_ONCE` guard ensures we execute the import loop only once,
|
61
|
+
even if `lookup()` is called from multiple threads.
|
62
|
+
* Each path in `_CORE_MODULES` is a "fully-qualified module string"
|
63
|
+
(e.g. "ptq.wrappers.linear_quant"). Importing the module runs all
|
64
|
+
its `@register(nn.Layer)` decorators, populating `_WRAPPERS`.
|
65
|
+
* After the first call the function becomes a cheap constant-time no-op.
|
66
|
+
"""
|
67
|
+
global _IMPORT_ONCE
|
68
|
+
if _IMPORT_ONCE:
|
69
|
+
return
|
70
|
+
for mod in _CORE_MODULES:
|
71
|
+
__import__(mod) # triggers decorators
|
72
|
+
_IMPORT_ONCE = True
|
73
|
+
|
74
|
+
|
75
|
+
# ───────────────────────────── decorator for always-present classes
|
76
|
+
def register(
|
77
|
+
fp_cls: Type[nn.Module],
|
78
|
+
) -> Callable[[Type[QuantModuleBase]], Type[QuantModuleBase]]:
|
79
|
+
def _decorator(quant_cls: Type[QuantModuleBase]):
|
80
|
+
_WRAPPERS[fp_cls] = quant_cls
|
81
|
+
return quant_cls
|
82
|
+
|
83
|
+
return _decorator
|
84
|
+
|
85
|
+
|
86
|
+
# ───────────────────────────── conditional decorator
|
87
|
+
def try_register(path: str) -> Callable[[Type[QuantModuleBase]], Type[QuantModuleBase]]:
|
88
|
+
"""
|
89
|
+
@try_register("transformers.models.llama.modeling_llama.LlamaMLP")
|
90
|
+
|
91
|
+
• If import succeeds → behave like `@register`
|
92
|
+
• If module/class not found → become a NO-OP
|
93
|
+
"""
|
94
|
+
|
95
|
+
def _decorator(quant_cls: Type[QuantModuleBase]):
|
96
|
+
module_name, _, cls_name = path.rpartition(".")
|
97
|
+
try:
|
98
|
+
mod = importlib.import_module(module_name)
|
99
|
+
fp_cls = getattr(mod, cls_name)
|
100
|
+
_WRAPPERS[fp_cls] = quant_cls
|
101
|
+
except (ModuleNotFoundError, AttributeError):
|
102
|
+
# transformers not installed or class renamed – silently skip
|
103
|
+
pass
|
104
|
+
return quant_cls
|
105
|
+
|
106
|
+
return _decorator
|
107
|
+
|
108
|
+
|
109
|
+
# ───────────────────────────── lookup
|
110
|
+
def lookup(fp_cls: Type[nn.Module]) -> Type[QuantModuleBase] | None:
|
111
|
+
_lazy_init()
|
112
|
+
return _WRAPPERS.get(fp_cls)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
tico/__init__.py,sha256=
|
1
|
+
tico/__init__.py,sha256=I2l2YkiyVNNCBv8PmWOlk2OxIDiMcpubxCENlSiOh8E,1883
|
2
2
|
tico/pt2_to_circle.py,sha256=gu3MD4Iqc0zMZcCZ2IT8oGbyj21CTSbT3Rgd9s2B_9A,2767
|
3
3
|
tico/config/__init__.py,sha256=xZzCXjZ84qE-CsBi-dfaL05bqpQ3stKKfTXhnrJRyVs,142
|
4
4
|
tico/config/base.py,sha256=q5xMqGxTUZs4mFqt5c7i_y9U00fYgdMGl9nUqIVMlCo,1248
|
@@ -72,6 +72,9 @@ tico/experimental/quantization/ptq/utils/__init__.py,sha256=PL9IZgiWoMtsXVljeOy7
|
|
72
72
|
tico/experimental/quantization/ptq/utils/reduce_utils.py,sha256=3kWawLB91EcvvHlCrNqqfZF7tpgr22htBSA049mKw_4,973
|
73
73
|
tico/experimental/quantization/ptq/wrappers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
74
74
|
tico/experimental/quantization/ptq/wrappers/quant_module_base.py,sha256=6RK4bn9G1pzFmkIdBdFf7liBOpb-b7rpthgD83AgkbQ,5256
|
75
|
+
tico/experimental/quantization/ptq/wrappers/registry.py,sha256=exXl2wNNzVgC2P9gMjpF_-PqIBgYERGruzh0u1Pril0,4367
|
76
|
+
tico/experimental/quantization/ptq/wrappers/nn/__init__.py,sha256=q4A9BiGlsa8ZdGV3y0SDiSkzkdVugsK2iz2daiJqBCY,118
|
77
|
+
tico/experimental/quantization/ptq/wrappers/nn/quant_linear.py,sha256=xW-VEPB7RJoslS3xLVCdhIuMjppknvpkZleRGK4JFVQ,2240
|
75
78
|
tico/interpreter/__init__.py,sha256=IO6FP_xYbGy0dW0HL26GXD3ouxARaxCK7bz9dn4blPQ,26
|
76
79
|
tico/interpreter/infer.py,sha256=1ZFe3DVMR2mlwBosoedqoL0-CGN_01CKLgMgxuw62KA,4861
|
77
80
|
tico/interpreter/interpreter.py,sha256=tGbluCbrehTCqBu8mtGDNzby_ieJ2ry8_RH_eC0CQxk,3828
|
@@ -226,9 +229,9 @@ tico/utils/mx/__init__.py,sha256=IO6FP_xYbGy0dW0HL26GXD3ouxARaxCK7bz9dn4blPQ,26
|
|
226
229
|
tico/utils/mx/elemwise_ops.py,sha256=V6glyAHsVR1joqpsgnNytatCD_ew92xNWZ19UFDoMTA,10281
|
227
230
|
tico/utils/mx/formats.py,sha256=uzNWyu-1onUlwQfX5cZ6fZSUfHMRqorper7_T1k3jfk,3404
|
228
231
|
tico/utils/mx/mx_ops.py,sha256=RcfUTYVi-wilGB2sC35OeARdwDqnixv7dG5iyZ-fQT8,8555
|
229
|
-
tico-0.1.0.
|
230
|
-
tico-0.1.0.
|
231
|
-
tico-0.1.0.
|
232
|
-
tico-0.1.0.
|
233
|
-
tico-0.1.0.
|
234
|
-
tico-0.1.0.
|
232
|
+
tico-0.1.0.dev250820.dist-info/LICENSE,sha256=kp4JLII7bzRhPb0CPD5XTDZMh22BQ7h3k3B7t8TiSbw,12644
|
233
|
+
tico-0.1.0.dev250820.dist-info/METADATA,sha256=SF_FvVX_JdbjpMAN2-VJGo7zsYU2_nEPXCHap296J3Y,8450
|
234
|
+
tico-0.1.0.dev250820.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
|
235
|
+
tico-0.1.0.dev250820.dist-info/entry_points.txt,sha256=kBKYSS_IYrSXmUYevmmepqIVPScq5vF8ulQRu3I_Zf0,59
|
236
|
+
tico-0.1.0.dev250820.dist-info/top_level.txt,sha256=oqs7UPoNSKZEwqsX8B-KAWdQwfAa7i60pbxW_Jk7P3w,5
|
237
|
+
tico-0.1.0.dev250820.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|