tico 0.1.0.dev250825__py3-none-any.whl → 0.1.0.dev250826__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.dev250825"
32
+ __version__ = "0.1.0.dev250826"
33
33
 
34
34
  MINIMUM_SUPPORTED_VERSION = "2.5.0"
35
35
  SECURE_TORCH_VERSION = "2.6.0"
@@ -0,0 +1 @@
1
+ # DO NOT REMOVE THIS FILE
@@ -0,0 +1,106 @@
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
+ # POST-TRAINING QUANTIZATION EXAMPLE — Simple Linear Model
17
+ # -----------------------------------------------------------------------------
18
+ # This demo shows a minimal PTQ flow for a toy model:
19
+ # 1. Define a simple model with a single Linear layer.
20
+ # 2. Replace the FP32 Linear with a QuantLinear wrapper.
21
+ # 3. Run a short calibration pass to collect activation statistics.
22
+ # 4. Freeze scales / zero-points and switch to INT-simulation mode.
23
+ # 5. Compare INT vs FP32 outputs with a mean-absolute-diff check.
24
+ # 6. Export the quantized model to a Circle format.
25
+ # =============================================================================
26
+
27
+ import pathlib
28
+
29
+ import torch
30
+ import torch.nn as nn
31
+
32
+ from tico.experimental.quantization.evaluation.metric import compute_peir
33
+ from tico.experimental.quantization.evaluation.utils import plot_two_outputs
34
+
35
+ from tico.experimental.quantization.ptq.mode import Mode
36
+ from tico.experimental.quantization.ptq.wrappers.nn.quant_linear import QuantLinear
37
+ from tico.utils.utils import SuppressWarning
38
+
39
+ # -------------------------------------------------------------------------
40
+ # 0. Define a toy model (1 Linear layer only)
41
+ # -------------------------------------------------------------------------
42
+ class TinyLinearModel(nn.Module):
43
+ """A minimal model: single Linear layer."""
44
+
45
+ def __init__(self, in_features=16, out_features=8):
46
+ super().__init__()
47
+ self.fc = nn.Linear(in_features, out_features, bias=False)
48
+
49
+ def forward(self, x):
50
+ return self.fc(x)
51
+
52
+
53
+ # Instantiate FP32 model
54
+ model = TinyLinearModel()
55
+ model.eval()
56
+
57
+ # Keep FP32 reference for diff check
58
+ fp32_layer = model.fc
59
+
60
+ # -------------------------------------------------------------------------
61
+ # 1. Replace the Linear with QuantLinear wrapper
62
+ # -------------------------------------------------------------------------
63
+ model.fc = QuantLinear(fp32_layer) # type: ignore[assignment]
64
+ # model.fc = PTQWrapper(fp32_layer) (Wrapping helper class)
65
+ qlayer = model.fc # alias for brevity
66
+
67
+ # -------------------------------------------------------------------------
68
+ # 2. Single-pass calibration (collect activation ranges)
69
+ # -------------------------------------------------------------------------
70
+ assert isinstance(qlayer, QuantLinear)
71
+ with torch.no_grad():
72
+ qlayer.enable_calibration()
73
+ for _ in range(16): # small toy batch
74
+ x = torch.randn(4, 16) # (batch=4, features=16)
75
+ _ = model(x)
76
+ qlayer.freeze_qparams() # lock scales & zero-points
77
+
78
+ assert qlayer._mode is Mode.QUANT, "Quantization mode should be active now."
79
+
80
+ # -------------------------------------------------------------------------
81
+ # 3. Quick INT-sim vs FP32 sanity check
82
+ # -------------------------------------------------------------------------
83
+ x = torch.randn(2, 16)
84
+ with torch.no_grad():
85
+ int8_out = model(x)
86
+ fp32_out = fp32_layer(x)
87
+
88
+ print("┌───────────── Quantization Error Summary ─────────────")
89
+ print(f"│ Mean |diff|: {(int8_out - fp32_out).abs().mean().item():.6f}")
90
+ print(f"│ PEIR : {compute_peir(fp32_out, int8_out) * 100:.6f} %")
91
+ print("└──────────────────────────────────────────────────────")
92
+ print(plot_two_outputs(fp32_out, int8_out))
93
+
94
+ # -------------------------------------------------------------------------
95
+ # 4. Export the calibrated model to Circle
96
+ # -------------------------------------------------------------------------
97
+ import tico
98
+
99
+ save_path = pathlib.Path("tiny_linear.q.circle")
100
+ example_input = torch.randn(1, 16)
101
+
102
+ with SuppressWarning(UserWarning, ".*"):
103
+ cm = tico.convert(model, (example_input,)) # forward(x) only
104
+ cm.save(save_path)
105
+
106
+ print(f"Quantized Circle model saved to {save_path.resolve()}")
@@ -48,7 +48,24 @@ class PTQWrapper(QuantModuleBase):
48
48
  return self.wrapped(*args, **kwargs)
49
49
 
50
50
  def _all_observers(self):
51
- yield from self.wrapped._all_observers()
51
+ """
52
+ PTQWrapper itself owns NO observers (transparent node).
53
+ Returning an empty iterator prevents double-processing when parents
54
+ traverse the tree and then recurse into `self.wrapped`.
55
+ """
56
+ return () # no local observers
57
+
58
+ def named_observers(self):
59
+ """
60
+ Proxy to the wrapped module so debugging tools can still enumerate observers.
61
+ """
62
+ yield from self.wrapped.named_observers()
63
+
64
+ def get_observer(self, name: str):
65
+ """
66
+ Proxy to the wrapped module for direct lookup by name.
67
+ """
68
+ return self.wrapped.get_observer(name)
52
69
 
53
70
  def extra_repr(self) -> str:
54
71
  return self.wrapped.extra_repr()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tico
3
- Version: 0.1.0.dev250825
3
+ Version: 0.1.0.dev250826
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=qQ6AhQGEdqTIol9a3pZd8krL41W7mwvr0HoA1tPCeqU,1883
1
+ tico/__init__.py,sha256=M4dQ4CTD_7xsO5DjUx76t4A5o1Q_2NqGlMe0fjkGDxQ,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
@@ -61,6 +61,8 @@ tico/experimental/quantization/ptq/dtypes.py,sha256=xfCBtq6mQmUYRwsoFgII6gvRl1ra
61
61
  tico/experimental/quantization/ptq/mode.py,sha256=lT-T8vIv8YWcwrjT7xXVhOw1g7aoAdh_3PWB-ptPKaI,1052
62
62
  tico/experimental/quantization/ptq/qscheme.py,sha256=uwhv7bCxOOXB3I-IKlRyr_u4eXOq48uIqGy4TLDqGxY,1301
63
63
  tico/experimental/quantization/ptq/quant_config.py,sha256=nm7570Y1X2mOT_8s27ilWid04otor6cVTi9GwgAEaKc,4300
64
+ tico/experimental/quantization/ptq/examples/__init__.py,sha256=IO6FP_xYbGy0dW0HL26GXD3ouxARaxCK7bz9dn4blPQ,26
65
+ tico/experimental/quantization/ptq/examples/quantize_linear.py,sha256=8zq-ZJDYgam0xQ-PbC6Xb1I7W1mv0Wi-b--IP2wwXtw,4539
64
66
  tico/experimental/quantization/ptq/observers/__init__.py,sha256=WF2MvL9M_jl-B1FqcY9zic34NOCRp17HkRYv-TMxMr4,613
65
67
  tico/experimental/quantization/ptq/observers/affine_base.py,sha256=e2Eba64nrxKQyE4F_WJ7WTSsk3xe6bkdGUKaoLFWGFw,4638
66
68
  tico/experimental/quantization/ptq/observers/base.py,sha256=Wons1MzpqK1mfcy-ppl-B2Dum0edXg2dWW2Lw3V18tw,3280
@@ -71,7 +73,7 @@ tico/experimental/quantization/ptq/observers/mx.py,sha256=aP4qmBgeiRIYZJksShN5gs
71
73
  tico/experimental/quantization/ptq/utils/__init__.py,sha256=PL9IZgiWoMtsXVljeOy7KymmLVP238SXEFRLXYK72WQ,126
72
74
  tico/experimental/quantization/ptq/utils/reduce_utils.py,sha256=3kWawLB91EcvvHlCrNqqfZF7tpgr22htBSA049mKw_4,973
73
75
  tico/experimental/quantization/ptq/wrappers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
74
- tico/experimental/quantization/ptq/wrappers/ptq_wrapper.py,sha256=KRw_VvFJYvd2OBj4K1sYEXxUwZk9QghMw3NsgjKIAGk,1857
76
+ tico/experimental/quantization/ptq/wrappers/ptq_wrapper.py,sha256=F9sK_DiRaXiGNHULcwIbs5EUtHz6ZJ7N4r5CWTTfhsM,2442
75
77
  tico/experimental/quantization/ptq/wrappers/quant_elementwise.py,sha256=LhEoobfvto6zKrBOKL4gmxfFFc31jHzyQV_zfps-iQM,3604
76
78
  tico/experimental/quantization/ptq/wrappers/quant_module_base.py,sha256=vkcDos_knGSS29rIZuEIWkAJLHrENbGz8nCH2-iara8,5969
77
79
  tico/experimental/quantization/ptq/wrappers/registry.py,sha256=562nKSlp9qF-w4-aQeJbx2V_wMGE2FRrjIKUfRwC4Mg,4571
@@ -233,9 +235,9 @@ tico/utils/mx/__init__.py,sha256=IO6FP_xYbGy0dW0HL26GXD3ouxARaxCK7bz9dn4blPQ,26
233
235
  tico/utils/mx/elemwise_ops.py,sha256=V6glyAHsVR1joqpsgnNytatCD_ew92xNWZ19UFDoMTA,10281
234
236
  tico/utils/mx/formats.py,sha256=uzNWyu-1onUlwQfX5cZ6fZSUfHMRqorper7_T1k3jfk,3404
235
237
  tico/utils/mx/mx_ops.py,sha256=RcfUTYVi-wilGB2sC35OeARdwDqnixv7dG5iyZ-fQT8,8555
236
- tico-0.1.0.dev250825.dist-info/LICENSE,sha256=kp4JLII7bzRhPb0CPD5XTDZMh22BQ7h3k3B7t8TiSbw,12644
237
- tico-0.1.0.dev250825.dist-info/METADATA,sha256=7wBkNIwJG_prscPdY7Rn_Muit4OuPN29Q8C_isHlEdI,8450
238
- tico-0.1.0.dev250825.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
239
- tico-0.1.0.dev250825.dist-info/entry_points.txt,sha256=kBKYSS_IYrSXmUYevmmepqIVPScq5vF8ulQRu3I_Zf0,59
240
- tico-0.1.0.dev250825.dist-info/top_level.txt,sha256=oqs7UPoNSKZEwqsX8B-KAWdQwfAa7i60pbxW_Jk7P3w,5
241
- tico-0.1.0.dev250825.dist-info/RECORD,,
238
+ tico-0.1.0.dev250826.dist-info/LICENSE,sha256=kp4JLII7bzRhPb0CPD5XTDZMh22BQ7h3k3B7t8TiSbw,12644
239
+ tico-0.1.0.dev250826.dist-info/METADATA,sha256=QhtUiHj_YT4ZxsClOx4OaP24kuaLUsLT83x3yl1gRDY,8450
240
+ tico-0.1.0.dev250826.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
241
+ tico-0.1.0.dev250826.dist-info/entry_points.txt,sha256=kBKYSS_IYrSXmUYevmmepqIVPScq5vF8ulQRu3I_Zf0,59
242
+ tico-0.1.0.dev250826.dist-info/top_level.txt,sha256=oqs7UPoNSKZEwqsX8B-KAWdQwfAa7i60pbxW_Jk7P3w,5
243
+ tico-0.1.0.dev250826.dist-info/RECORD,,