Skip to content

Commit 67ade25

Browse files
committed
Refactor KMeansPalettizer, add extendable training strategy capability
1 parent cd95cb2 commit 67ade25

15 files changed

Lines changed: 1284 additions & 472 deletions

changelog.d/60.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add training-aware palettization to `KMeansPalettizer` via a `training_mode()` context manager and `step()` method, with a `PATSchedule` (`pat_schedule` on the module config) controlling when palettization activates during a training loop. `PalettizationSpec` gains a `training_strategy_config` field for selecting pluggable training-time strategies.

src/coreai_opt/config/compression_config.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -379,21 +379,11 @@ def _normalize_none_op_configs(self) -> ModuleCompressionConfig:
379379

380380
return self
381381

382-
def _get_compressor_specific_settings(self) -> dict[str, Any]:
383-
"""
384-
Get compressor-specific settings, excluding base ModuleCompressionConfig fields.
385-
386-
Returns only fields defined by the concrete subclass (e.g.,
387-
enable_fast_kmeans_mode, rounding_precision for palettization), not the base
388-
spec and config fields (op_input_spec, op_output_spec, op_state_spec,
389-
op_type_config, op_name_config, module_input_spec, module_output_spec,
390-
module_state_spec).
391-
392-
This is useful when constructing arguments for compression operations that need
393-
the compression-specific settings but handle the spec fields separately.
382+
def _get_fake_module_kwargs(self) -> dict[str, Any]:
383+
"""Get the settings to forward to the compression simulator module's constructor.
394384
395385
Returns:
396-
Dictionary of compressor-specific field names to their values.
386+
Dictionary of field names to their values.
397387
"""
398388
base_field_names = set(ModuleCompressionConfig.model_fields.keys())
399389
return {k: v for k, v in self.model_dump().items() if k not in base_field_names}

src/coreai_opt/palettization/config/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
KMeansPalettizerConfig,
1010
ModuleKMeansPalettizerConfig,
1111
OpKMeansPalettizerConfig,
12+
PATSchedule,
1213
)
1314

1415
__all__ = [
1516
"KMeansPalettizerConfig",
1617
"ModuleKMeansPalettizerConfig",
1718
"OpKMeansPalettizerConfig",
19+
"PATSchedule",
1820
]

src/coreai_opt/palettization/config/palettization_config.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from typing import TYPE_CHECKING, ClassVar, final
99

10-
from pydantic import PositiveInt, model_validator
10+
from pydantic import BaseModel, ConfigDict, Field, PositiveInt, model_validator
1111

1212
from coreai_opt.config import (
1313
CompressionConfig,
@@ -31,6 +31,29 @@
3131
_PALETTIZATION_SPEC = "palettization_spec"
3232

3333

34+
class PATSchedule(BaseModel):
35+
"""Schedule for enabling palettization-aware training (PAT).
36+
37+
Defines the step threshold at which a module's fake palettization
38+
forward pass becomes active. Used with ``KMeansPalettizer.step()``.
39+
40+
Attributes:
41+
enable_fake_palettize: Step count at which fake palettization is
42+
enabled. Must be >= 0.
43+
44+
Example:
45+
>>> schedule = PATSchedule(enable_fake_palettize=500)
46+
"""
47+
48+
model_config = ConfigDict(frozen=True)
49+
50+
enable_fake_palettize: int = Field(default=0, ge=0)
51+
52+
def _compute_state(self, step_count: int) -> bool:
53+
"""Return whether fake palettization should be active at the given step."""
54+
return step_count >= self.enable_fake_palettize
55+
56+
3457
class OpKMeansPalettizerConfig(WeightOnlyOpValidationMixin, OpCompressionConfig[PalettizationSpec]):
3558
"""
3659
Configuration class for palettization at the operation level.
@@ -140,6 +163,11 @@ class ModuleKMeansPalettizerConfig(
140163
K-means clustering. Higher values preserve more precision but may reduce
141164
speed benefits. Only used when enable_fast_kmeans_mode is True. Default: 4.
142165
166+
pat_schedule (PATSchedule | None): Schedule controlling when this
167+
module's palettization is active during a training_mode() loop.
168+
If None, palettization is active immediately once training_mode()
169+
begins. Default: None.
170+
143171
Example:
144172
>>> config = ModuleKMeansPalettizerConfig() # Uses defaults
145173
>>> # Or with custom settings:
@@ -160,6 +188,7 @@ def __init_subclass__(cls, **kwargs):
160188

161189
enable_fast_kmeans_mode: bool = True
162190
rounding_precision: PositiveInt = 4
191+
pat_schedule: PATSchedule | None = None
163192

164193
# Namespace exposing built-in preset constructors.
165194
presets: ClassVar[_ModuleKMeansPalettizerConfigPresets]
@@ -183,6 +212,12 @@ def validate_fast_kmeans_cluster_dim_constraint(
183212

184213
return self
185214

215+
def _get_fake_module_kwargs(self) -> dict:
216+
"""Exclude palettizer-only fields from the fake-palettize constructor args."""
217+
base = super()._get_fake_module_kwargs()
218+
base.pop("pat_schedule", None)
219+
return base
220+
186221

187222
@final
188223
class KMeansPalettizerConfig(CompressionConfig[ModuleKMeansPalettizerConfig]):

0 commit comments

Comments
 (0)