-
Notifications
You must be signed in to change notification settings - Fork 309
[JAX] Add support for per-layer config custimization and composable configs #2481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bkowalskiINTEL
wants to merge
23
commits into
main
Choose a base branch
from
dev/bkowalsk/jax_composable_configs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
aed7004
add include/exclude filtering and path-based get_model_info
bkowalskiINTEL 09c989c
refactor algorithms to use per-layer config from configs_mapping
bkowalskiINTEL 3b1aa05
support ComposableConfig and centralize model wrapping
bkowalskiINTEL 24ba943
handle ComposableConfig serialization and per-layer deserialization
bkowalskiINTEL bc8d9a6
add ViT config filtering integration test
bkowalskiINTEL 6b1f2b1
update gemma example for composable config API
bkowalskiINTEL 8835c1c
simplify deserialization to use class-based layer matching
bkowalskiINTEL 306286e
Fix configs merging
bkowalskiINTEL 0f17bea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c3cdd3f
Align dynamic algo to static algo and fix white list bug
bkowalskiINTEL f8b4ad3
Merge branch 'master' into dev/bkowalsk/jax_composable_configs
bkowalskiINTEL c8e0698
Remove debug stuff commited by accident
bkowalskiINTEL 56176ef
Apply suggestion from @bkowalskiINTEL
bkowalskiINTEL 690bb51
Apply suggestions from code review
bkowalskiINTEL eb4c879
Apply suggestion from @bkowalskiINTEL
bkowalskiINTEL c45031b
Remove redundant op support checks in static/dynamic algorithms
bkowalskiINTEL f4119e7
Modify docstring for static_quantize
bkowalskiINTEL 5301022
Comments cleanup
bkowalskiINTEL c79ef1e
Restore gemma example
bkowalskiINTEL f9bac3f
Fix comments
bkowalskiINTEL d768b7b
Fix comments
bkowalskiINTEL 368d95e
Apply suggestion from code review
bkowalskiINTEL 185cdde
Fix and refactor keras and keras_hub layers registrations
bkowalskiINTEL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,10 +21,6 @@ | |
| from neural_compressor.common.base_config import BaseConfig | ||
| from neural_compressor.common.utils import DYNAMIC_QUANT | ||
| from neural_compressor.jax.quantization.layers_dynamic import dynamic_quant_mapping | ||
| from neural_compressor.jax.quantization.saving import ( | ||
| WRAPPER_MAPPING, | ||
| KerasQuantizedModelBackboneWrapper, | ||
| ) | ||
| from neural_compressor.jax.utils import register_algo | ||
| from neural_compressor.jax.utils.utility import dtype_mapping, iterate_over_layers | ||
|
|
||
|
|
@@ -48,36 +44,31 @@ def dynamic_quantize( | |
| **kwargs (Any): Additional keyword arguments (unused). | ||
|
|
||
| Returns: | ||
| keras.Model: The quantized model wrapped for inference. | ||
| keras.Model: The quantized model. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previous comment seems to be more accurate
bkowalskiINTEL marked this conversation as resolved.
Outdated
|
||
| """ | ||
| for _, value in configs_mapping.items(): | ||
| config = value | ||
| break | ||
| weight_dtype = dtype_mapping[config.weight_dtype] | ||
| activation_dtype = dtype_mapping[config.activation_dtype] | ||
| # Build set of layer paths that this algorithm should process | ||
| layer_configs = {op_name: cfg for (op_name, _op_type), cfg in configs_mapping.items() if cfg.name == DYNAMIC_QUANT} | ||
|
bkowalskiINTEL marked this conversation as resolved.
Outdated
|
||
|
|
||
| # TODO serialization/deserialisation doesn't work for Gemma3CausalLM model | ||
| # Need to further investigation. | ||
| # Instead of copying model we can mark model parameter as mutable. | ||
| # config = serialization_lib.serialize_keras_object(model) | ||
| # qmodel = serialization_lib.deserialize_keras_object( | ||
| # config, custom_objects={model.__class__.__name__: model.__class__} | ||
| # ) | ||
| # qmodel.set_weights(model.get_weights()) | ||
| qmodel = model | ||
| operations = [ | ||
| lambda layer: dynamic_quant_mapping[layer.__class__].prepare( | ||
| layer, weight_dtype, activation_dtype, quant_config.const_scale, quant_config.const_weight | ||
| ), | ||
| lambda layer: layer.add_variables(), | ||
| lambda layer: layer.post_quantization_cleanup(), | ||
| ] | ||
| iterate_over_layers(qmodel, operations, filter_function=lambda c: c in dynamic_quant_mapping) | ||
|
|
||
| if hasattr(qmodel, "backbone"): | ||
| qmodel._tracker.unlock() | ||
| qmodel.backbone = KerasQuantizedModelBackboneWrapper(qmodel.backbone, quant_config) | ||
| qmodel._tracker.lock() | ||
| def _filter(layer_class): | ||
| return layer_class in dynamic_quant_mapping | ||
|
|
||
| wrapper_cls = WRAPPER_MAPPING[qmodel.__class__] | ||
| return wrapper_cls(qmodel, quant_config) | ||
| def _apply_operations(layer): | ||
| layer_id = layer.path if layer.path else layer.name | ||
| if layer_id not in layer_configs: | ||
| return | ||
| config = layer_configs[layer_id] | ||
| weight_dtype = dtype_mapping[config.weight_dtype] | ||
| activation_dtype = dtype_mapping[config.activation_dtype] | ||
| dynamic_quant_mapping[layer.__class__].prepare( | ||
| layer, weight_dtype, activation_dtype, config.const_scale, config.const_weight | ||
| ) | ||
| layer.add_variables() | ||
| layer.post_quantization_cleanup() | ||
|
|
||
| for layer in qmodel._flatten_layers(): | ||
| if _filter(layer.__class__): | ||
| _apply_operations(layer) | ||
|
|
||
| return qmodel | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,18 +17,12 @@ | |
| from typing import Any, Callable, Optional, OrderedDict, Union | ||
|
|
||
| import keras | ||
| from keras_hub.src.models.causal_lm import CausalLM | ||
|
|
||
| from neural_compressor.common.base_config import BaseConfig | ||
| from neural_compressor.common.utils import STATIC_QUANT | ||
| from neural_compressor.jax.quantization.layers_static import static_quant_mapping | ||
| from neural_compressor.jax.quantization.saving import ( | ||
| WRAPPER_MAPPING, | ||
| KerasQuantizedModelBackboneWrapper, | ||
| ) | ||
| from neural_compressor.jax.utils import register_algo | ||
| from neural_compressor.jax.utils.utility import ( | ||
| causal_lm_make_replace_generate_function, | ||
| dtype_mapping, | ||
| iterate_over_layers, | ||
| ) | ||
|
|
@@ -51,51 +45,37 @@ def static_quantize( | |
| calib_function (Optional[Callable]): Calibration function used to collect activation statistics. | ||
|
|
||
| Returns: | ||
| keras.Model: The quantized model wrapped for inference. | ||
| keras.Model: The quantized model. | ||
| """ | ||
| for _, value in configs_mapping.items(): | ||
| config = value | ||
| break | ||
| weight_dtype = dtype_mapping[config.weight_dtype] | ||
| activation_dtype = dtype_mapping[config.activation_dtype] | ||
| # Build set of layer paths that this algorithm should process | ||
| layer_configs = {op_name: cfg for (op_name, _op_type), cfg in configs_mapping.items() if cfg.name == STATIC_QUANT} | ||
|
bkowalskiINTEL marked this conversation as resolved.
Outdated
bkowalskiINTEL marked this conversation as resolved.
Outdated
|
||
|
|
||
| # TODO serialization/deserialization doesn't work for Gemma3CausalLM model | ||
| # Need to further investigation. | ||
| # Instead of copying model we can mark model parameter as mutable. | ||
| # config = serialization_lib.serialize_keras_object(model) | ||
| # qmodel = serialization_lib.deserialize_keras_object( | ||
| # config, custom_objects={model.__class__.__name__: model.__class__} | ||
| # ) | ||
| # qmodel.set_weights(model.get_weights()) | ||
| qmodel = model | ||
|
|
||
| if isinstance(qmodel, CausalLM): | ||
| causal_lm_make_replace_generate_function(qmodel) | ||
|
|
||
| operations = [ | ||
| lambda layer: static_quant_mapping[layer.__class__].prepare( | ||
| layer, weight_dtype, activation_dtype, quant_config.const_scale, quant_config.const_weight | ||
| ), | ||
| lambda layer: layer.add_observers(), | ||
| ] | ||
| iterate_over_layers(qmodel, operations, filter_function=lambda c: c in static_quant_mapping) | ||
| # Phase 1: Prepare layers and add observers | ||
| for layer in qmodel._flatten_layers(): | ||
| if layer.__class__ not in static_quant_mapping: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we can filter if class could be quantied in earlier stage |
||
| continue | ||
| layer_id = layer.path if layer.path else layer.name | ||
| if layer_id not in layer_configs: | ||
| continue | ||
| config = layer_configs[layer_id] | ||
| weight_dtype = dtype_mapping[config.weight_dtype] | ||
| activation_dtype = dtype_mapping[config.activation_dtype] | ||
| static_quant_mapping[layer.__class__].prepare( | ||
| layer, weight_dtype, activation_dtype, config.const_scale, config.const_weight | ||
| ) | ||
| layer.add_observers() | ||
|
|
||
| # Phase 2: Run calibration on original model with observers | ||
| calib_function(qmodel) | ||
|
|
||
|
|
||
| # Phase 3: Convert observed layers to quantized form | ||
| operations = [ | ||
| lambda layer: layer.add_variables(), | ||
| lambda layer: layer.convert(), | ||
| lambda layer: layer.post_quantization_cleanup(), | ||
| ] | ||
| iterate_over_layers(qmodel, operations, filter_function=lambda c: c in static_quant_mapping.values()) | ||
|
|
||
| if isinstance(qmodel, CausalLM): | ||
| causal_lm_make_replace_generate_function(qmodel, revert=True) | ||
|
|
||
| if hasattr(qmodel, "backbone"): | ||
| qmodel._tracker.unlock() | ||
| qmodel.backbone = KerasQuantizedModelBackboneWrapper(qmodel.backbone, quant_config) | ||
| qmodel._tracker.lock() | ||
|
|
||
| wrapper_cls = WRAPPER_MAPPING[qmodel.__class__] | ||
|
|
||
| return wrapper_cls(qmodel, quant_config) | ||
| return qmodel | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.