Skip to content

Commit 7c01c37

Browse files
committed
refactor(coreai_opt): apply internal-import _-alias fixes
- Add the required `_` alias to private-module imports in public modules, drop the redundant `_` alias in private modules, and rename references to match, per code style guide §3.3 - Generated with `python scripts/pre_commit/check_internal_import_aliases.py --fix` - Kept as a separate commit from the hook so a future rebase conflict here can be resolved by reverting `src/` and re-running `--fix`, instead of hand-merging auto-generated edits
1 parent e31ff2a commit 7c01c37

30 files changed

Lines changed: 397 additions & 485 deletions

src/coreai_opt/_utils/insertion/torch_function/modes.py

Lines changed: 52 additions & 108 deletions
Large diffs are not rendered by default.

src/coreai_opt/_utils/insertion/torch_function/state_spec_resolver.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import torch
1414
import torch.nn as nn
1515

16-
from coreai_opt._utils.spec_utils import PartialConstructor as _PartialConstructor
16+
from coreai_opt._utils.spec_utils import PartialConstructor
1717
from coreai_opt._utils.torch_utils import NamedModule
1818
from coreai_opt.config.spec import CompressionSimulatorBase
1919

@@ -129,7 +129,7 @@ def resolve(
129129
func: Callable,
130130
state_tensor: torch.Tensor,
131131
current_module: NamedModule,
132-
components_dict: Mapping[int | str, _PartialConstructor | None],
132+
components_dict: Mapping[int | str, PartialConstructor | None],
133133
) -> None:
134134
"""Resolve and cache the optimizer for ``state_tensor`` at the current call site.
135135
@@ -197,7 +197,7 @@ def _resolve_op_state(
197197
self,
198198
func: Callable,
199199
state_tensor: torch.Tensor,
200-
components_dict: Mapping[int | str, _PartialConstructor | None],
200+
components_dict: Mapping[int | str, PartialConstructor | None],
201201
) -> CompressionSimulatorBase | None:
202202
"""Look up the ``op_state_spec`` optimizer using all local names for ``state_tensor``."""
203203
local_state_names = self.get_all_local_names(state_tensor)

src/coreai_opt/_utils/insertion/torch_function/types.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import torch
1515

16-
from coreai_opt._utils.spec_utils import PartialConstructor as _PartialConstructor
16+
from coreai_opt._utils.spec_utils import PartialConstructor
1717
from coreai_opt.config.spec import CompressionSimulatorBase
1818

1919

@@ -27,15 +27,15 @@ class OpCompressionComponents:
2727
- None if no compression is applied to that component
2828
"""
2929

30-
op_input_components: Mapping[
31-
int | str, _PartialConstructor[CompressionSimulatorBase] | None
32-
] = field(default_factory=dict)
30+
op_input_components: Mapping[int | str, PartialConstructor[CompressionSimulatorBase] | None] = (
31+
field(default_factory=dict)
32+
)
3333

3434
op_output_components: Mapping[
35-
int | str, _PartialConstructor[CompressionSimulatorBase] | None
35+
int | str, PartialConstructor[CompressionSimulatorBase] | None
3636
] = field(default_factory=dict)
3737

38-
op_state_components: Mapping[str, _PartialConstructor[CompressionSimulatorBase] | None] = field(
38+
op_state_components: Mapping[str, PartialConstructor[CompressionSimulatorBase] | None] = field(
3939
default_factory=dict
4040
)
4141

@@ -74,15 +74,15 @@ class ModuleCompressionComponents:
7474
or name to an OpCompressionComponents class.
7575
"""
7676

77-
weight: Mapping[str, _PartialConstructor[CompressionSimulatorBase] | None] = field(
77+
weight: Mapping[str, PartialConstructor[CompressionSimulatorBase] | None] = field(
7878
default_factory=dict
7979
)
8080

81-
input_activation: Mapping[int | str, _PartialConstructor[CompressionSimulatorBase] | None] = (
81+
input_activation: Mapping[int | str, PartialConstructor[CompressionSimulatorBase] | None] = (
8282
field(default_factory=dict)
8383
)
8484

85-
output_activation: Mapping[int | str, _PartialConstructor[CompressionSimulatorBase] | None] = (
85+
output_activation: Mapping[int | str, PartialConstructor[CompressionSimulatorBase] | None] = (
8686
field(default_factory=dict)
8787
)
8888

@@ -91,14 +91,14 @@ class ModuleCompressionComponents:
9191
op_name_components: Mapping[str, OpCompressionComponents] = field(default_factory=dict)
9292

9393
module_input_components: Mapping[
94-
int | str, _PartialConstructor[CompressionSimulatorBase] | None
94+
int | str, PartialConstructor[CompressionSimulatorBase] | None
9595
] = field(default_factory=dict)
9696

9797
module_output_components: Mapping[
98-
int | str, _PartialConstructor[CompressionSimulatorBase] | None
98+
int | str, PartialConstructor[CompressionSimulatorBase] | None
9999
] = field(default_factory=dict)
100100

101-
module_state_components: Mapping[str, _PartialConstructor[CompressionSimulatorBase] | None] = (
101+
module_state_components: Mapping[str, PartialConstructor[CompressionSimulatorBase] | None] = (
102102
field(default_factory=dict)
103103
)
104104

src/coreai_opt/_utils/insertion/torch_function/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from torch.fx.operator_schemas import create_type_hint, normalize_function
1717

1818
from coreai_opt._utils.config_utils import get_last_matching_spec
19-
from coreai_opt._utils.spec_utils import PartialConstructor as _PartialConstructor
19+
from coreai_opt._utils.spec_utils import PartialConstructor
2020
from coreai_opt.config.spec import CompressionSimulatorBase
2121

2222
logger = logging.getLogger(__name__)
@@ -130,7 +130,7 @@ def any_tensor_optimizable(args: list[Any], kwargs: dict[str, Any]) -> bool:
130130
def get_optimizer_from_components_dict(
131131
func: Callable,
132132
tensor_identifiers: int | str | list[int | str],
133-
components_dict: Mapping[int | str, _PartialConstructor | None],
133+
components_dict: Mapping[int | str, PartialConstructor | None],
134134
) -> tuple[CompressionSimulatorBase | None, bool]:
135135
"""Return the appropriate optimizer from ``components_dict``.
136136

src/coreai_opt/_utils/torch_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import torch
1818
import torch.nn.utils.parametrize as P
1919

20-
from coreai_opt._utils.version_utils import version_ge as _version_ge
20+
from coreai_opt._utils.version_utils import version_ge
2121

2222
logger = logging.getLogger(__name__)
2323

@@ -424,7 +424,7 @@ def export_model(
424424
exported_program = torch.export.export(
425425
model, example_inputs, dynamic_shapes=dynamic_shapes
426426
)
427-
if _version_ge(torch, "2.9"):
427+
if version_ge(torch, "2.9"):
428428
exported_model = exported_program.module(check_guards=False)
429429
else:
430430
exported_model = exported_program.module()

src/coreai_opt/config/spec/compression_simulator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import torch
1111
import torch.nn as nn
1212

13-
from coreai_opt._utils.registry_utils import ClassRegistryMixin
13+
from coreai_opt._utils.registry_utils import ClassRegistryMixin as _ClassRegistryMixin
1414

1515

16-
class CompressionSimulatorBase(ClassRegistryMixin, nn.Module):
16+
class CompressionSimulatorBase(_ClassRegistryMixin, nn.Module):
1717
"""
1818
Abstract base class for compression simulators.
1919

src/coreai_opt/coreai_utils/passes/weight_palettization.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
import numpy as np
1515

1616
from coreai_opt.coreai_utils._coreai_imports import (
17-
AIProgram,
18-
DenseResourceElementsAttr,
19-
F16Type,
20-
F32Type,
21-
InsertionPoint,
22-
IntegerType,
23-
RankedTensorType,
24-
WalkResult,
17+
AIProgram as _AIProgram,
18+
DenseResourceElementsAttr as _DenseResourceElementsAttr,
19+
F16Type as _F16Type,
20+
F32Type as _F32Type,
21+
InsertionPoint as _InsertionPoint,
22+
IntegerType as _IntegerType,
23+
RankedTensorType as _RankedTensorType,
24+
WalkResult as _WalkResult,
2525
_get_constant_value_as_np_array,
26-
compression_types,
27-
coreai,
26+
compression_types as _compression_types,
27+
coreai as _coreai,
2828
)
2929
from coreai_opt.coreai_utils._utils.graph_utils import (
3030
_apply_compression_transform,
@@ -61,7 +61,7 @@
6161

6262

6363
def palettize_weights(
64-
coreai_program: AIProgram,
64+
coreai_program: _AIProgram,
6565
lut_dtype: DType | None,
6666
n_bits: int = 4,
6767
granularity: CompressionGranularity = CompressionGranularity.PER_TENSOR,
@@ -73,7 +73,7 @@ def palettize_weights(
7373
enable_fast_kmeans_mode: bool = True,
7474
rounding_precision: int = 4,
7575
in_place: bool = False,
76-
) -> AIProgram:
76+
) -> _AIProgram:
7777
"""Palettize weights in a Core AI AIProgram (MLIR<CoreAI> IR) by using Core AI ops.
7878
7979
Walks through the IR and palettizes each coreai.constant op that needs to be
@@ -178,7 +178,7 @@ def replace_weight_with_compression_op(
178178
fused zero_point = lut_zero_point * per_channel_scale.
179179
"""
180180
if not _should_compress_op(op, weight_num_threshold, _OPS_WEIGHT_NEED_COMPRESSION):
181-
return WalkResult.ADVANCE
181+
return _WalkResult.ADVANCE
182182

183183
const_weight: Any = op
184184
weight = _get_constant_value_as_np_array(const_weight)
@@ -194,7 +194,7 @@ def replace_weight_with_compression_op(
194194
"The `cluster_dim` is invalid for %s. Skipped this op.",
195195
const_weight.name,
196196
)
197-
return WalkResult.ADVANCE
197+
return _WalkResult.ADVANCE
198198

199199
if enable_per_channel_scale:
200200
# Normalize by per channel scales before doing palettization.
@@ -222,7 +222,7 @@ def replace_weight_with_compression_op(
222222
"Cannot perform palettization on %s. Skipped this op.",
223223
const_weight.name,
224224
)
225-
return WalkResult.ADVANCE
225+
return _WalkResult.ADVANCE
226226
except ImportError:
227227
raise
228228
except Exception as e:
@@ -232,12 +232,12 @@ def replace_weight_with_compression_op(
232232
const_weight.name,
233233
e,
234234
)
235-
return WalkResult.ADVANCE
235+
return _WalkResult.ADVANCE
236236

237-
with const_weight.context, const_weight.location, InsertionPoint(const_weight):
237+
with const_weight.context, const_weight.location, _InsertionPoint(const_weight):
238238
indices = _create_constant_value_from_np_array(
239239
lut_params.indices, # same shape as weight tensor (for many cases)
240-
IntegerType.get_unsigned(n_bits),
240+
_IntegerType.get_unsigned(n_bits),
241241
)
242242

243243
vector_axis = _create_constant_value_from_np_array(
@@ -246,11 +246,11 @@ def replace_weight_with_compression_op(
246246
if lut_params.vector_axis is not None
247247
else np.int16(0)
248248
),
249-
IntegerType.get_signed(16),
249+
_IntegerType.get_signed(16),
250250
)
251251

252252
weight_float_mlir_type = (
253-
F32Type.get() if original_weight_type == np.float32 else F16Type.get()
253+
_F32Type.get() if original_weight_type == np.float32 else _F16Type.get()
254254
)
255255

256256
if lut_dtype is not None:
@@ -259,12 +259,12 @@ def replace_weight_with_compression_op(
259259
weight_element_type = cast("Any", const_weight.result.type).element_type
260260

261261
if lut_dtype.is_int():
262-
lut_dtype_builtin = compression_types.string_to_builtin(lut_dtype)
262+
lut_dtype_builtin = _compression_types.string_to_builtin(lut_dtype)
263263
ref_mlir_type = _get_string_to_mlir_type()[lut_dtype]
264264
quantized_mlir_type = (
265-
IntegerType.get_signed(ref_mlir_type.width)
265+
_IntegerType.get_signed(ref_mlir_type.width)
266266
if ref_mlir_type.is_signed
267-
else IntegerType.get_unsigned(ref_mlir_type.width)
267+
else _IntegerType.get_unsigned(ref_mlir_type.width)
268268
)
269269

270270
quant_params = _compute_qparams_by_dtype(
@@ -278,7 +278,7 @@ def replace_weight_with_compression_op(
278278
"Failed to compute quantization parameters for %s. Skipped this op.",
279279
const_weight.name,
280280
)
281-
return WalkResult.ADVANCE
281+
return _WalkResult.ADVANCE
282282

283283
quantized_lut_data, lut_scale, lut_zero_point = quant_params
284284
if lut_zero_point is None:
@@ -335,7 +335,7 @@ def replace_weight_with_compression_op(
335335
"Failed to compute quantization parameters for %s. Skipped this op.",
336336
const_weight.name,
337337
)
338-
return WalkResult.ADVANCE
338+
return _WalkResult.ADVANCE
339339

340340
quantized_lut_data, lut_scale, _ = quant_params
341341

@@ -348,15 +348,15 @@ def replace_weight_with_compression_op(
348348
dtype=lut_scale.dtype,
349349
)
350350

351-
tensor_type = RankedTensorType.get(
351+
tensor_type = _RankedTensorType.get(
352352
list(quantized_lut_data.shape), fp8_mlir_type
353353
)
354-
lut_quantized_attr = DenseResourceElementsAttr.get_from_buffer(
354+
lut_quantized_attr = _DenseResourceElementsAttr.get_from_buffer(
355355
quantized_lut_data,
356356
"dense_resource",
357357
tensor_type,
358358
)
359-
lut_quantized = cast("Any", coreai.ConstantOp(value=lut_quantized_attr).result)
359+
lut_quantized = cast("Any", _coreai.ConstantOp(value=lut_quantized_attr).result)
360360

361361
lut_scale_const = _create_constant_value_from_np_array(
362362
lut_scale_reshaped,
@@ -368,23 +368,23 @@ def replace_weight_with_compression_op(
368368
weight_element_type,
369369
)
370370

371-
compressed_weight_quantized = coreai.lut_to_dense(
371+
compressed_weight_quantized = _coreai.lut_to_dense(
372372
indices=indices,
373373
lut=lut_quantized,
374374
axis=vector_axis,
375375
)
376376
# Cast lut_to_dense output from quantized type to float so all
377377
# blockwise_shift_scale operands share the same element type.
378-
cast_type = RankedTensorType.get(
378+
cast_type = _RankedTensorType.get(
379379
cast("Any", compressed_weight_quantized.type).shape,
380380
weight_float_mlir_type,
381381
)
382382
compressed_weight_float = cast(
383383
"Any",
384-
coreai.CastOp(cast_type, compressed_weight_quantized).result,
384+
_coreai.CastOp(cast_type, compressed_weight_quantized).result,
385385
)
386386
lut_t = cast("Any", lut_scale_const.type)
387-
compressed_weight = coreai.blockwise_shift_scale(
387+
compressed_weight = _coreai.blockwise_shift_scale(
388388
data=compressed_weight_float,
389389
scale=lut_scale_const,
390390
offset1=lut_zero_point_const,
@@ -397,7 +397,7 @@ def replace_weight_with_compression_op(
397397
# (e.g. coreai.transpose) see the same type contract.
398398
compressed_weight = cast(
399399
"Any",
400-
coreai.CastOp(
400+
_coreai.CastOp(
401401
cast("Any", const_weight.result.type),
402402
compressed_weight,
403403
).result,
@@ -409,7 +409,7 @@ def replace_weight_with_compression_op(
409409
cast("Any", const_weight.result.type).element_type,
410410
)
411411

412-
compressed_weight = coreai.lut_to_dense(
412+
compressed_weight = _coreai.lut_to_dense(
413413
indices=indices,
414414
lut=lut,
415415
axis=vector_axis,
@@ -426,15 +426,15 @@ def replace_weight_with_compression_op(
426426
)
427427
compressed_weight_float = cast(
428428
"Any",
429-
coreai.CastOp(
430-
RankedTensorType.get(
429+
_coreai.CastOp(
430+
_RankedTensorType.get(
431431
cast("Any", compressed_weight.type).shape,
432432
weight_float_mlir_type,
433433
),
434434
compressed_weight,
435435
).result,
436436
)
437-
compressed_weight = coreai.blockwise_shift_scale(
437+
compressed_weight = _coreai.blockwise_shift_scale(
438438
data=compressed_weight_float,
439439
scale=scale,
440440
offset1=zero_point,
@@ -445,15 +445,15 @@ def replace_weight_with_compression_op(
445445
)
446446
compressed_weight = cast(
447447
"Any",
448-
coreai.CastOp(
448+
_coreai.CastOp(
449449
cast("Any", const_weight.result.type),
450450
compressed_weight,
451451
).result,
452452
)
453453

454454
const_weight.result.replace_all_uses_with(compressed_weight)
455455

456-
return WalkResult.ADVANCE
456+
return _WalkResult.ADVANCE
457457

458458
return _apply_compression_transform(
459459
coreai_program,

0 commit comments

Comments
 (0)