You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Several examples below configure specific module types or module names. To determine these for your model, see [How to get names + types](../quantization/config.md#how-to-get-names--types-for-modules-and-ops). Since palettization only supports eager execution mode, only the eager mode guidance in that section is relevant.
119
+
Several examples below configure specific module types or module names. To determine these for your model, use {class}`~coreai_opt.inspection.ModelInspector` with `execution_mode="eager"` — see [Inspecting Model Structure](../utils/model_inspection.md). Palettization supports eager mode only.
120
120
121
121
### Apply 4-bit palettization globally, 8-bit to linear layers
Copy file name to clipboardExpand all lines: docs/src/quantization/config.md
+7-35Lines changed: 7 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -184,7 +184,7 @@ The defaults are:
184
184
185
185
In [Quantization Overview](overview.md) we saw how to use the default `W_INT8_A_INT8` config. [Config classes and their defaults](#config-classes-and-their-defaults) described the default settings in `QuantizerConfig()`, `ModuleQuantizerConfig()`, and `OpQuantizerConfig()`. Let us now see how to configure quantization when non-default settings are desired.
186
186
187
-
Several examples below configure specific module names, module types, op names, or op types. To determine these for your model, see [How to get names + types for modules and ops](#how-to-get-names-types-for-modules-and-ops) (eager mode) or [Inspecting Model Structure](../utils/model_inspection.md) (graph mode).
187
+
Several examples below configure specific module names, module types, op names, or op types. To determine these for your model, see [Inspecting Model Structure](../utils/model_inspection.md).
188
188
189
189
### Example: `W_MXFP4_A_FP8` applied to all supported ops
190
190
@@ -857,7 +857,7 @@ classDiagram
857
857
858
858
## How to get names + types for modules and ops
859
859
860
-
**Graph mode** (for `module_name_configs`, `module_type_configs`, `op_name_config`, `op_type_config`): use {class}`~coreai_opt.inspection.ModelInspector` to discover module names, module types, op names, and op types.
860
+
Use {class}`~coreai_opt.inspection.ModelInspector` to discover module names, module types, op names, and op types for both graph and eager execution modes.
861
861
862
862
```python
863
863
import torch
@@ -866,40 +866,12 @@ from coreai_opt.inspection import ModelInspector
866
866
867
867
model = nn.Sequential(nn.Linear(10, 20), nn.ReLU(), nn.Linear(20, 5))
# Use execution_mode="eager" for eager mode inspection.
870
+
model,
871
+
example_inputs=(torch.randn(1, 10),),
872
+
execution_mode="graph",
870
873
)
871
874
print(inspector.format_summary())
872
875
```
873
876
874
-
See [Inspecting Model Structure](../utils/model_inspection.md) for full usage and examples.
875
-
876
-
**Eager mode**: module names (for `module_name_configs`) can be obtained by inspecting `model.named_modules()`. This includes all modules in the model (nested and leaf). The names align with the structure of modules defined in code.
877
-
878
-
Op names can be constructed by referring to the parent module and the op in it. Example:
879
-
880
-
```python
881
-
class TwoAddModule(torch.nn.Module):
882
-
def forward(self, x):
883
-
a = x + x
884
-
b = a + a
885
-
return b
886
-
887
-
888
-
class Model(torch.nn.Module):
889
-
def __init__(self):
890
-
super().__init__()
891
-
self.submodule_a = TwoAddModule()
892
-
self.submodule_b = TwoAddModule()
893
-
894
-
def forward(self, x):
895
-
x = self.submodule_a(x)
896
-
x = self.submodule_b(x)
897
-
return x
898
-
899
-
900
-
# the names of ops in the model will be :
901
-
# - submodule_a.add
902
-
# - submodule_a.add_1
903
-
# - submodule_b.add
904
-
# - submodule_b.add_1
905
-
```
877
+
See [Inspecting Model Structure](../utils/model_inspection.md) for full usage, examples, and a comparison of graph and eager mode op naming.
`coreai-opt` configs reference module names, module types, op names, and op types to target specific parts of a model. Before writing a config, you need to know exactly which strings your model exposes. {class}`~coreai_opt.inspection.ModelInspector` discovers these automatically and provides query methods corresponding to each config key type (`op_type_config`, `op_name_config`, `module_name_configs`, `module_type_configs`).
4
4
5
-
:::{note}
6
-
`ModelInspector` currently supports **graph execution mode only**. Eager mode support is planned. For eager mode op naming, see [How to get names + types](../quantization/config.md#how-to-get-names--types-for-modules-and-ops).
7
-
:::
5
+
## Execution Modes
6
+
7
+
`ModelInspector` supports two execution modes, selected via the `execution_mode` argument:
8
+
9
+
-**Graph mode** (`execution_mode="graph"`): Exports the model with `torch.export` and walks the resulting FX graph. Op names are global identifiers assigned during export (for example, `"linear"`, `"linear_1"`). The compressor must be `Quantizer` or `None`.
10
+
-**Eager mode** (`execution_mode="eager"`): Intercepts operations during a live forward pass. Op names are module-qualified identifiers that reflect the module hierarchy (for example, `"linear1.linear"`, `"linear2.linear"`). This mode supports both `Quantizer` and `KMeansPalettizer` as the compressor.
11
+
12
+
Choose graph mode when you need exact parity with the exported graph, and eager mode when you want op names that directly match the module hierarchy or need to inspect models not yet exportable.
8
13
9
14
## Basic Usage
10
15
@@ -38,97 +43,123 @@ inspector = ModelInspector(
38
43
compressor=Quantizer,
39
44
)
40
45
41
-
# Print a module-hierarchy tree showing ops, source locations, and connectivity
46
+
# Print a module-hierarchy tree showing ops, connectivity, and source locations
42
47
print(inspector.format_summary())
43
48
```
44
49
45
-
Note the use of `compressor=Quantizer` in the list of arguments to `ModelInspector`. This filters the list of ops captured and displayed by `ModelInspector` to only those operations which are registered for compressibility by `Quantizer`. Omitting this argument allows for all ops to be captured and displayed.
50
+
Pass `colorize=False` to suppress ANSI color codes, for example when writing to a file.
51
+
52
+
Note the use of `compressor=Quantizer`. This filters the captured and displayed ops to those registered as compressible by `Quantizer`. Omit this argument to capture and display all ops.
46
53
47
54
The above code produces output like the following (colors omitted for brevity):
op inputs: {I: producer[N]} — I = op_input_spec index; N = output slot of the producing op
61
+
op states: param_name — model parameter or buffer
62
+
op outputs: {N: [consumers]} — N = output slot index; consumers = ops receiving that output
63
+
untracked_N — input tensor whose producer was not intercepted (e.g. raw attribute or global tensor); still quantizable via op_input_spec
64
+
module inputs: {I: [op[N], ...]} — I = module_input_spec index; op[N] = op and its input slot receiving data from outside; absent keys = non-quantizable
65
+
module outputs: {I: op[N]} — I = module_output_spec index; op[N] = op and its output slot leaving the module; absent keys = non-quantizable
66
+
67
+
(__main__.MyModel)
68
+
module inputs: {0: [linear[0]]}
69
+
module outputs: {0: linear_1[0]}
55
70
├── ■ linear1 (torch.nn.modules.linear.Linear)
56
-
│ module inputs: linear
57
-
│ module outputs: linear
71
+
│ module inputs: {0: [linear[0]]}
72
+
│ module outputs: {0: linear[0]}
58
73
│ └── ◆ linear [linear]
59
-
│ op inputs: x, linear1_weight, linear1_bias
60
-
│ op outputs: relu
61
-
│ filepath: my_model.py:16
62
-
│ code: x = self.linear1(x)
74
+
│ op inputs: {0: x[0]}
75
+
│ op states: weight, bias
76
+
│ op outputs: {0: [relu]}
63
77
├── ■ relu (torch.nn.modules.activation.ReLU)
64
-
│ module inputs: relu
65
-
│ module outputs: relu
78
+
│ module inputs: {0: [relu[0]]}
79
+
│ module outputs: {0: relu[0]}
66
80
└── ■ linear2 (torch.nn.modules.linear.Linear)
67
-
module inputs: linear_1
68
-
module outputs: linear_1
81
+
module inputs: {0: [linear_1[0]]}
82
+
module outputs: {0: linear_1[0]}
69
83
└── ◆ linear_1 [linear]
70
-
op inputs: relu, linear2_weight, linear2_bias
71
-
op outputs: output
72
-
filepath: my_model.py:18
73
-
code: x = self.linear2(x)
84
+
op inputs: {0: relu[0]}
85
+
op states: weight, bias
86
+
op outputs: {0: [output]}
74
87
```
75
88
76
-
The output shows the model's module hierarchy and the ops within each module. Note in particular that since `relu` is not a registered compressible op by`Quantizer`, it does not show up as an operation within the `ReLU` module.
89
+
Note that `relu` does not appear as an operation (`◆`) within the `relu`module, because `ReLU` is not a compressible op in`Quantizer`. It still appears as a module node (`■`) and in connectivity lines such as `op outputs: {0: [relu]}` and `op inputs: {0: relu[0]}`, because the relu tensor passes through and connects the two linear ops.
77
90
78
-
Reading the tree:
91
+
## Reading the Tree
79
92
80
-
-**Module name** and **module type** appear on module lines: `■ module_name (module_type)`. For example, `■ linear1 (torch.nn.modules.linear.Linear)` — `"linear1"` is the module name (usable in `module_name_configs`) and `"torch.nn.modules.linear.Linear"` is the module type (usable in `module_type_configs`).
81
-
-**Op name** and **op type** appear on operation lines: `◆ op_name [op_type]`. For example, `◆ linear_1 [linear]` — `"linear_1"` is the op name (usable in `op_name_config`) and `"linear"` is the op type (usable in `op_type_config`).
82
-
-**Op inputs/outputs** show connectivity between operations.
83
-
-**filepath** and **code** (shown for user-defined modules) show where in your source code the operation originates.
93
+
### Module lines
84
94
85
-
Using these strings directly in a config:
95
+
Module lines use the form `■ module_name (module_type)`. For example, `■ linear1 (torch.nn.modules.linear.Linear)`:
-`"linear1"` is the module name, usable in `module_name_configs`.
98
+
-`"torch.nn.modules.linear.Linear"` is the module type, usable in `module_type_configs`.
98
99
99
-
# Op-level targeting within a ModuleQuantizerConfig
100
-
config = QuantizerConfig(
101
-
global_config=ModuleQuantizerConfig(
102
-
# Target a specific op by name
103
-
op_name_config={
104
-
"linear_1": OpQuantizerConfig(...),
105
-
},
106
-
# Target all ops of a given type
107
-
op_type_config={
108
-
"linear": OpQuantizerConfig(...),
109
-
},
110
-
),
111
-
)
112
-
```
100
+
**Module boundaries** appear indented under the module header:
101
+
102
+
-`module inputs: {I: [op[N], ...]}` — The activations entering this module from outside. `I` is the position in the module's input spec (matching `module_input_spec` in a config), `op` is the name of the first compressible op inside the module that receives data at that position, and `N` is the input slot on that op. A single external input can fan out to multiple ops. Keys absent from this dict correspond to non-quantizable positions (for example, state tensors or unused arguments).
103
+
-`module outputs: {I: op[N]}` — The activations leaving this module. `I` is the position in the module's output spec, `op` is the compressible op producing that output, and `N` is the op's output slot. Absent keys correspond to non-quantizable positions.
104
+
105
+
### Op lines
106
+
107
+
Op lines use the form `◆ op_name [op_type]`. For example, `◆ linear_1 [linear]`:
108
+
109
+
-`"linear_1"` is the op name, usable in `op_name_config`.
110
+
-`"linear"` is the op type, usable in `op_type_config`.
113
111
114
-
Pass `colorize=False` to suppress ANSI color codes (e.g., when writing to a file).
112
+
**Op connectivity** appears indented under the op header:
113
+
114
+
-`op inputs: {I: producer[N]}` — Activation inputs only (parameters and buffers are on a separate line). `I` is the argument position (matching `op_input_spec` in a config), `producer` is the name of the op that produced this tensor, and `N` is the output slot of that producer. For example, `{0: relu[0]}` means argument 0 comes from output slot 0 of the `relu` op.
115
+
-`op states: param_name, ...` — Model parameters and buffers consumed by this op. This line is omitted if the op takes no states.
116
+
-`op outputs: {N: [consumer1, consumer2, ...]}` — `N` is the output slot index, and the list contains the names of all ops consuming that output.
117
+
-`untracked_N` — Appears in place of a producer name when the input tensor's origin was not intercepted (for example, a raw module attribute or global tensor). These tensors are still quantizable via `op_input_spec`.
118
+
-`filepath` and `code` — Source file and line of the call that produced the op, shown as dim text.
119
+
120
+
## Eager Mode
121
+
122
+
To inspect using eager mode, pass `execution_mode="eager"`. The same `MyModel` example above yields:
123
+
124
+
```text
125
+
(__main__.MyModel)
126
+
module inputs: {0: [linear1.linear[0]]}
127
+
module outputs: {0: linear2.linear[0]}
128
+
├── ■ linear1 (torch.nn.modules.linear.Linear)
129
+
│ module inputs: {0: [linear1.linear[0]]}
130
+
│ module outputs: {0: linear1.linear[0]}
131
+
│ └── ◆ linear1.linear [linear]
132
+
│ op inputs: {0: input_0}
133
+
│ op states: weight, bias
134
+
│ op outputs: {0: [relu.relu]}
135
+
│ filepath: my_model.py:16
136
+
├── ■ relu (torch.nn.modules.activation.ReLU)
137
+
│ module inputs: {0: [relu.relu[0]]}
138
+
│ module outputs: {0: relu.relu[0]}
139
+
└── ■ linear2 (torch.nn.modules.linear.Linear)
140
+
module inputs: {0: [linear2.linear[0]]}
141
+
module outputs: {0: linear2.linear[0]}
142
+
└── ◆ linear2.linear [linear]
143
+
op inputs: {0: relu.relu[0]}
144
+
op states: weight, bias
145
+
op outputs: {0: [output_0]}
146
+
filepath: my_model.py:18
147
+
```
115
148
116
149
## Querying Operations by Config Key
117
150
118
-
Once you have reviewed the full summary to see what names and types are present, you can use query methods to check which operations would be matched by a specific name or type pattern. This is useful for verifying your config will target the intended ops before applying compression.
151
+
Once you have reviewed the full summary to see what names and types are present, use the query methods to check which operations a specific pattern matches. This is useful for verifying that a config targets the intended ops before applying compression.
119
152
120
153
Each query method returns a tuple of {class}`~coreai_opt.inspection.OpInfo` objects matching the filter. The method names correspond directly to the config keys they help populate.
121
154
122
-
From the Basic Usage summary, this model exposes:
155
+
From the graph mode summary above, this model exposes:
Op names and module names can be passed as a literal name or as a regex following [Python re syntax](https://docs.python.org/3/library/re.html) for wildcard matching; the pattern is matched against the entire string. The matching methodology is identical to how compression config entries match modules and ops in a model, allowing the user to see exactly which modules or ops would be matched given a particular string.
130
-
131
-
Each query method returns a tuple of {class}`~coreai_opt.inspection.OpInfo` objects matching the filter:
162
+
Op names and module names can be passed as a literal string or as a regex following [Python re syntax](https://docs.python.org/3/library/re.html) for wildcard matching. The pattern is matched against the full string. The matching behavior is identical to how compression config entries match modules and ops, so you can see exactly which ops a given pattern would select.
132
163
133
164
**By op type** — exact-string match against `op_type_config` keys:
) # matches ops in modules "linear1" and "linear2"
185
+
) # matches ops in "linear1" and "linear2"
155
186
```
156
187
157
-
Each returned {class}`~coreai_opt.inspection.OpInfo` provides `op_name`, `op_type`, and `module_stack`(the nesting of modules containing the op):
188
+
Each returned {class}`~coreai_opt.inspection.OpInfo` provides `op_name`, `op_type`, `module_stack`, `inputs`, `outputs`, and `is_state`. The `module_stack`is a tuple of {class}`~coreai_opt.inspection.ModuleContext` entries from outermost to innermost module:
158
189
159
190
```python
160
191
>>>for op in inspector.get_matched_ops_for_op_type("linear"):
@@ -166,6 +197,37 @@ Each returned {class}`~coreai_opt.inspection.OpInfo` provides `op_name`, `op_typ
166
197
module: linear2 (torch.nn.modules.linear.Linear)
167
198
```
168
199
200
+
`OpInfo.inputs` is a tuple of {class}`~coreai_opt.inspection.InputEdge` objects, one per input argument position. Each `InputEdge` carries the producing `OpInfo` and the output slot index (`output_idx`) of that producer. State inputs (parameters, buffers) are interleaved in the tuple at their actual argument positions, and their corresponding `InputEdge` objects have `is_state=True`.
# Op-level targeting within a ModuleQuantizerConfig
217
+
config = QuantizerConfig(
218
+
global_config=ModuleQuantizerConfig(
219
+
# Target a specific op by name
220
+
op_name_config={
221
+
"linear_1": OpQuantizerConfig(...),
222
+
},
223
+
# Target all ops of a given type
224
+
op_type_config={
225
+
"linear": OpQuantizerConfig(...),
226
+
},
227
+
),
228
+
)
229
+
```
230
+
169
231
## Navigating the Module Hierarchy
170
232
171
233
For programmatic access to the inspector's data structures, the {class}`~coreai_opt.inspection.ModelSummary` exposes a {class}`~coreai_opt.inspection.ModuleInfo` tree that mirrors the `nn.Module` hierarchy. These types are publicly exported from `coreai_opt.inspection` for use in custom analysis or tooling.
@@ -180,11 +242,24 @@ linear2: torch.nn.modules.linear.Linear, 1 direct ops
180
242
```
181
243
182
244
```python
183
-
#look up a specific submodule
245
+
#Look up a specific submodule
184
246
linear2_module = root.get_submodule("linear2")
185
247
186
-
#get all ops under this subtree (depth-first)
248
+
#Get all ops under this subtree (depth-first)
187
249
linear2_ops = linear2_module.all_ops()
188
250
```
189
251
190
252
`ModuleInfo` supports the same iteration patterns as `nn.Module`: `children()`, `named_children()`, `modules()`, `named_modules()`, and `get_submodule()`.
253
+
254
+
`ModuleInfo` also exposes the module boundary connectivity described in the tree:
255
+
256
+
-`input_ops` — dict mapping module input spec index to a list of {class}`~coreai_opt.inspection.BoundaryEdge` objects, each holding the op and input slot receiving data from outside the module.
257
+
-`output_ops` — dict mapping module output spec index to a single {class}`~coreai_opt.inspection.BoundaryEdge`, holding the op and output slot whose tensor leaves the module.
258
+
259
+
```python
260
+
# Inspect boundary connectivity for a submodule
261
+
linear1_module = root.get_submodule("linear1")
262
+
for idx, edges in linear1_module.input_ops.items():
0 commit comments