1414import numpy as np
1515
1616from 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)
2929from coreai_opt .coreai_utils ._utils .graph_utils import (
3030 _apply_compression_transform ,
6161
6262
6363def 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