tico 0.1.0.dev250908__py3-none-any.whl → 0.1.0.dev250909__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 CHANGED
@@ -29,7 +29,7 @@ __all__ = [
29
29
  ]
30
30
 
31
31
  # THIS LINE IS AUTOMATICALLY GENERATED BY setup.py
32
- __version__ = "0.1.0.dev250908"
32
+ __version__ = "0.1.0.dev250909"
33
33
 
34
34
  MINIMUM_SUPPORTED_VERSION = "2.5.0"
35
35
  SECURE_TORCH_VERSION = "2.6.0"
@@ -0,0 +1,165 @@
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
+ # -----------------------------------------------------------------------------
16
+ # This file includes modifications based on fairseq
17
+ # (https://github.com/facebookresearch/fairseq), originally licensed under
18
+ # the MIT License. See the LICENSE file in the fairseq repository for details.
19
+ # -----------------------------------------------------------------------------
20
+
21
+ from typing import Optional
22
+
23
+ import torch.nn as nn
24
+ from torch import Tensor
25
+
26
+ from tico.experimental.quantization.ptq.quant_config import QuantConfig
27
+ from tico.experimental.quantization.ptq.wrappers.fairseq.quant_mha import (
28
+ QuantFairseqMultiheadAttention,
29
+ )
30
+ from tico.experimental.quantization.ptq.wrappers.ptq_wrapper import PTQWrapper
31
+ from tico.experimental.quantization.ptq.wrappers.quant_module_base import (
32
+ QuantModuleBase,
33
+ )
34
+ from tico.experimental.quantization.ptq.wrappers.registry import try_register
35
+
36
+
37
+ @try_register("fairseq.modules.transformer_layer.TransformerEncoderLayerBase")
38
+ class QuantFairseqEncoderLayer(QuantModuleBase):
39
+ """
40
+ Quant-aware drop-in replacement for Fairseq TransformerEncoderLayerBase.
41
+
42
+ Design notes (inference-friendly):
43
+ - All training-time logic (dropout, activation-dropout) is removed.
44
+ - I/O shape follows Fairseq convention: [T, B, C].
45
+ - `return_fc` behavior is preserved (returns (x, fc_result) if enabled).
46
+ """
47
+
48
+ def __init__(
49
+ self,
50
+ fp_layer: nn.Module,
51
+ *,
52
+ qcfg: Optional[QuantConfig] = None,
53
+ fp_name: Optional[str] = None,
54
+ ):
55
+ super().__init__(qcfg, fp_name=fp_name)
56
+
57
+ # --- copy meta / config flags from FP layer (read-only) -------------
58
+ assert hasattr(fp_layer, "embed_dim")
59
+ assert hasattr(fp_layer, "normalize_before")
60
+ self.embed_dim: int = int(fp_layer.embed_dim) # type: ignore[arg-type]
61
+ self.normalize_before: bool = bool(fp_layer.normalize_before)
62
+ self.return_fc: bool = bool(getattr(fp_layer, "return_fc", False))
63
+
64
+ # --- PTQ-wrapped submodules ----------------------------------------
65
+ attn_cfg = qcfg.child("self_attn") if qcfg else None
66
+ fc1_cfg = qcfg.child("fc1") if qcfg else None
67
+ fc2_cfg = qcfg.child("fc2") if qcfg else None
68
+ attn_ln_cfg = qcfg.child("self_attn_layer_norm") if qcfg else None
69
+ final_ln_cfg = qcfg.child("final_layer_norm") if qcfg else None
70
+
71
+ assert hasattr(fp_layer, "self_attn") and isinstance(
72
+ fp_layer.self_attn, nn.Module
73
+ )
74
+ assert hasattr(fp_layer, "fc1") and isinstance(fp_layer.fc1, nn.Module)
75
+ assert hasattr(fp_layer, "fc2") and isinstance(fp_layer.fc2, nn.Module)
76
+
77
+ self.self_attn = QuantFairseqMultiheadAttention(
78
+ fp_layer.self_attn, qcfg=attn_cfg, fp_name=f"{fp_name}.self_attn"
79
+ )
80
+ self.fc1 = PTQWrapper(fp_layer.fc1, qcfg=fc1_cfg, fp_name=f"{fp_name}.fc1")
81
+ self.fc2 = PTQWrapper(fp_layer.fc2, qcfg=fc2_cfg, fp_name=f"{fp_name}.fc2")
82
+
83
+ # LayerNorms
84
+ assert hasattr(fp_layer, "self_attn_layer_norm") and isinstance(
85
+ fp_layer.self_attn_layer_norm, nn.Module
86
+ )
87
+ assert hasattr(fp_layer, "final_layer_norm") and isinstance(
88
+ fp_layer.final_layer_norm, nn.Module
89
+ )
90
+ self.self_attn_layer_norm = PTQWrapper(
91
+ fp_layer.self_attn_layer_norm,
92
+ qcfg=attn_ln_cfg,
93
+ fp_name=f"{fp_name}.self_attn_layer_norm",
94
+ )
95
+ self.final_layer_norm = PTQWrapper(
96
+ fp_layer.final_layer_norm,
97
+ qcfg=final_ln_cfg,
98
+ fp_name=f"{fp_name}.final_layer_norm",
99
+ )
100
+
101
+ # Activation function
102
+ self.activation_fn = fp_layer.activation_fn # type: ignore[operator] # e.g., GELU/ReLU
103
+ self.obs_activation_fn = self._make_obs("activation_fn")
104
+
105
+ # ----------------------------------------------------------------------
106
+ def forward(
107
+ self,
108
+ x: Tensor, # [T,B,C]
109
+ encoder_padding_mask: Optional[Tensor],
110
+ attn_mask: Optional[Tensor] = None, # [T,S] boolean/byte or additive float
111
+ ):
112
+ """
113
+ Returns:
114
+ x' of shape [T, B, C] (or (x', fc_result) when return_fc=True)
115
+ """
116
+ # ---- Self-Attention block (pre-/post-norm kept as in FP layer) ----
117
+ residual = x
118
+ if self.normalize_before:
119
+ x = self.self_attn_layer_norm(x)
120
+
121
+ # Fairseq MHA expects [T,B,C]; our wrapped module keeps the same API
122
+ attn_out, _ = self.self_attn(
123
+ query=x,
124
+ key=x,
125
+ value=x,
126
+ key_padding_mask=encoder_padding_mask, # additive float [B,S] or None
127
+ need_weights=False,
128
+ attn_mask=attn_mask, # additive float [T,S] or None
129
+ )
130
+ x = residual + attn_out
131
+
132
+ if not self.normalize_before:
133
+ x = self.self_attn_layer_norm(x)
134
+
135
+ # ---- FFN block (no dropout/activation-dropout) --------------------
136
+ residual = x
137
+ if self.normalize_before:
138
+ x = self.final_layer_norm(x)
139
+
140
+ x = self.fc1(x) # Linear
141
+ x = self.activation_fn(x) # type: ignore[operator]
142
+ x = self._fq(x, self.obs_activation_fn)
143
+ x = self.fc2(x) # Linear
144
+
145
+ fc_result = x # keep before residual for optional return
146
+
147
+ x = residual + x
148
+ if not self.normalize_before:
149
+ x = self.final_layer_norm(x)
150
+
151
+ if self.return_fc:
152
+ return x, fc_result
153
+ return x
154
+
155
+ def _all_observers(self):
156
+ yield from (self.obs_activation_fn,)
157
+ for m in (
158
+ self.self_attn,
159
+ self.fc1,
160
+ self.fc2,
161
+ self.self_attn_layer_norm,
162
+ self.final_layer_norm,
163
+ ):
164
+ if isinstance(m, QuantModuleBase):
165
+ yield from m._all_observers()
@@ -33,6 +33,7 @@ _CORE_MODULES = (
33
33
  "tico.experimental.quantization.ptq.wrappers.llama.quant_decoder_layer",
34
34
  "tico.experimental.quantization.ptq.wrappers.llama.quant_mlp",
35
35
  # fairseq
36
+ "tico.experimental.quantization.ptq.wrappers.fairseq.quant_encoder_layer",
36
37
  "tico.experimental.quantization.ptq.wrappers.fairseq.quant_mha",
37
38
  # add future core wrappers here
38
39
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tico
3
- Version: 0.1.0.dev250908
3
+ Version: 0.1.0.dev250909
4
4
  Summary: Convert exported Torch module to circle
5
5
  Home-page: UNKNOWN
6
6
  License: UNKNOWN
@@ -1,4 +1,4 @@
1
- tico/__init__.py,sha256=KNsLIqvVA_qyf9VOIt_PmHk0L_QUjJqnldhE8_X4n6I,1883
1
+ tico/__init__.py,sha256=c5spmq5DrUrTLuWPal98sdmfFYPjuRym0xcEnK9Am_U,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
@@ -84,8 +84,9 @@ tico/experimental/quantization/ptq/wrappers/__init__.py,sha256=IO6FP_xYbGy0dW0HL
84
84
  tico/experimental/quantization/ptq/wrappers/ptq_wrapper.py,sha256=F9sK_DiRaXiGNHULcwIbs5EUtHz6ZJ7N4r5CWTTfhsM,2442
85
85
  tico/experimental/quantization/ptq/wrappers/quant_elementwise.py,sha256=LhEoobfvto6zKrBOKL4gmxfFFc31jHzyQV_zfps-iQM,3604
86
86
  tico/experimental/quantization/ptq/wrappers/quant_module_base.py,sha256=vkcDos_knGSS29rIZuEIWkAJLHrENbGz8nCH2-iara8,5969
87
- tico/experimental/quantization/ptq/wrappers/registry.py,sha256=cblE6dpLlK1lh61Xpqxcr2Vwi9XuqdrV5Y7TPSjpETQ,4940
87
+ tico/experimental/quantization/ptq/wrappers/registry.py,sha256=bTd1fZGCXkL4iaduKUXjWVpRXfvOaJGeurxwKJBVu6I,5019
88
88
  tico/experimental/quantization/ptq/wrappers/fairseq/__init__.py,sha256=Mc8FLd9DusyB_IT1vk1OYrRkngOYnYd05IvtA9ORVQc,160
89
+ tico/experimental/quantization/ptq/wrappers/fairseq/quant_encoder_layer.py,sha256=aGr80Ku75j2H-UZ0elEa0mOQEyaAs2YJ4WJCN0lonn0,6412
89
90
  tico/experimental/quantization/ptq/wrappers/fairseq/quant_mha.py,sha256=HsigmOLeacLXc46QNeFqwQ0DwKQhNrtWTKEtLJoqXoc,15562
90
91
  tico/experimental/quantization/ptq/wrappers/llama/__init__.py,sha256=IO6FP_xYbGy0dW0HL26GXD3ouxARaxCK7bz9dn4blPQ,26
91
92
  tico/experimental/quantization/ptq/wrappers/llama/quant_attn.py,sha256=-K1COLHIHfJZhQu-RE6KfJIkaL7S6yR4iUj48QkjMTw,8652
@@ -249,9 +250,9 @@ tico/utils/mx/__init__.py,sha256=IO6FP_xYbGy0dW0HL26GXD3ouxARaxCK7bz9dn4blPQ,26
249
250
  tico/utils/mx/elemwise_ops.py,sha256=V6glyAHsVR1joqpsgnNytatCD_ew92xNWZ19UFDoMTA,10281
250
251
  tico/utils/mx/formats.py,sha256=uzNWyu-1onUlwQfX5cZ6fZSUfHMRqorper7_T1k3jfk,3404
251
252
  tico/utils/mx/mx_ops.py,sha256=RcfUTYVi-wilGB2sC35OeARdwDqnixv7dG5iyZ-fQT8,8555
252
- tico-0.1.0.dev250908.dist-info/LICENSE,sha256=kp4JLII7bzRhPb0CPD5XTDZMh22BQ7h3k3B7t8TiSbw,12644
253
- tico-0.1.0.dev250908.dist-info/METADATA,sha256=SBFNzxkvYrsWshqSGGkmtVoHtDk1Rp9wVNfhdIRsFdg,8450
254
- tico-0.1.0.dev250908.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
255
- tico-0.1.0.dev250908.dist-info/entry_points.txt,sha256=kBKYSS_IYrSXmUYevmmepqIVPScq5vF8ulQRu3I_Zf0,59
256
- tico-0.1.0.dev250908.dist-info/top_level.txt,sha256=oqs7UPoNSKZEwqsX8B-KAWdQwfAa7i60pbxW_Jk7P3w,5
257
- tico-0.1.0.dev250908.dist-info/RECORD,,
253
+ tico-0.1.0.dev250909.dist-info/LICENSE,sha256=kp4JLII7bzRhPb0CPD5XTDZMh22BQ7h3k3B7t8TiSbw,12644
254
+ tico-0.1.0.dev250909.dist-info/METADATA,sha256=udUCPXehe7bxZ27PQKpcxub4Q6VSoaoEmckvpdx5FPo,8450
255
+ tico-0.1.0.dev250909.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
256
+ tico-0.1.0.dev250909.dist-info/entry_points.txt,sha256=kBKYSS_IYrSXmUYevmmepqIVPScq5vF8ulQRu3I_Zf0,59
257
+ tico-0.1.0.dev250909.dist-info/top_level.txt,sha256=oqs7UPoNSKZEwqsX8B-KAWdQwfAa7i60pbxW_Jk7P3w,5
258
+ tico-0.1.0.dev250909.dist-info/RECORD,,