-
Notifications
You must be signed in to change notification settings - Fork 75
feat: add compilation algorithms #443
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
minettekaum
wants to merge
5
commits into
main
Choose a base branch
from
feat/compilation-algorithms
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.
+654
−3
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| # Copyright 2025 - Pruna AI GmbH. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import re | ||
| from pathlib import Path | ||
| from typing import Any, Dict | ||
|
|
||
| import torch | ||
| from ConfigSpace import OrdinalHyperparameter | ||
|
|
||
| from pruna.algorithms.base.pruna_base import PrunaAlgorithmBase | ||
| from pruna.algorithms.base.tags import AlgorithmTag | ||
| from pruna.config.smash_config import SmashConfigPrefixWrapper | ||
| from pruna.engine.save import SAVE_FUNCTIONS | ||
| from pruna.logging.logger import pruna_logger | ||
|
|
||
|
|
||
| class IPEXLLM(PrunaAlgorithmBase): | ||
| """ | ||
| Implement IPEX LLM compilation using the intel library. | ||
|
|
||
| This compiler leverages advanced graph optimizations, quantization, and kernel fusion techniques to accelerate | ||
| PyTorch-based LLM inference on Intel CPUs. | ||
|
|
||
| Note: After compilation, the model supports sequence lengths that are either ≤ 32, or even numbers. | ||
| """ | ||
|
|
||
| algorithm_name: str = "ipex_llm" | ||
| group_tags: list[AlgorithmTag] = [AlgorithmTag.COMPILER] | ||
| references: dict[str, str] = {"Github": "https://github.com/intel/intel-extension-for-pytorch"} | ||
| tokenizer_required: bool = False | ||
| processor_required: bool = False | ||
| dataset_required: bool = False | ||
| save_fn = SAVE_FUNCTIONS.save_before_apply | ||
| runs_on: list[str] = ["cpu"] | ||
| compatible_before: list[str] = ["half"] | ||
| required_install = ( | ||
| "``pip install pruna[intel]`` " | ||
| "``--extra-index-url https://pytorch-extension.intel.com/release-whl/stable/cpu/cn/``" | ||
| ) | ||
|
|
||
| def get_hyperparameters(self) -> list: | ||
| """ | ||
| Get the hyperparameters for IPEX LLM compilation. | ||
|
|
||
| Returns | ||
| ------- | ||
| list | ||
| The hyperparameters. | ||
| """ | ||
| return [ | ||
| OrdinalHyperparameter( | ||
| "weight_bits", | ||
| sequence=[8, 4], | ||
| default_value=8, | ||
| meta=dict(desc="The number of bits to use for weight quantization."), | ||
| ), | ||
| ] | ||
|
|
||
| def model_check_fn(self, model: Any) -> bool: | ||
| """ | ||
| Check if the model is compatible with IPEX LLM compilation. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| model : Any | ||
| The model to check. | ||
|
|
||
| Returns | ||
| ------- | ||
| bool | ||
| Whether the model is compatible with IPEX LLM compilation. | ||
| """ | ||
| imported_modules = self.import_algorithm_packages() | ||
| # Find the installation path of ipex | ||
| ipex_path = Path(imported_modules["ipex"].__file__).parent # type: ignore[attr-defined] | ||
| # Try to find the models.py file | ||
| transformers_path = ipex_path / "transformers" | ||
| # Find the full path of models.py if it exists | ||
| models_path = transformers_path / "models" / "reference" / "models.py" | ||
| if models_path.exists(): | ||
| # Read the function names from the file | ||
| with open(models_path, "r") as f: | ||
| content = f.read() | ||
| # Simple regex to find function definitions | ||
| funcs = [f for f in re.findall(r"def\s+([A-Z][a-zA-Z0-9_]*)\s*\(", content) if f.endswith("_forward")] | ||
| compatible_list = [name.replace("_forward", "") for name in funcs] | ||
| return model.__class__.__name__ in compatible_list | ||
| else: | ||
| pruna_logger.warning("IPEX models.py file not found. Please check if IPEX is installed correctly.") | ||
| return False | ||
|
|
||
| def _apply(self, model: Any, smash_config: SmashConfigPrefixWrapper) -> Any: | ||
| """ | ||
| Compile the model with IPEX LLM. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| model : Any | ||
| The model to compile. | ||
| smash_config : SmashConfigPrefixWrapper | ||
| The configuration to use for compilation. | ||
|
|
||
| Returns | ||
| ------- | ||
| Any | ||
| The compiled model. | ||
| """ | ||
| imported_modules = self.import_algorithm_packages() | ||
| ipex = imported_modules["ipex"] | ||
| woq_weight_dtype = imported_modules["WoqWeightDtype"] | ||
|
|
||
| weight_dtype = woq_weight_dtype.INT8 if smash_config["weight_bits"] == 8 else woq_weight_dtype.INT4 | ||
|
|
||
| lowp_mode = ipex.quantization.WoqLowpMode.INT8 # type: ignore[attr-defined] | ||
|
|
||
| qconfig = ipex.quantization.get_weight_only_quant_qconfig_mapping(weight_dtype=weight_dtype, lowp_mode=lowp_mode) # type: ignore[attr-defined] | ||
|
|
||
| model = ipex.llm.optimize( # type: ignore[attr-defined] | ||
| model.eval(), | ||
| dtype=getattr(torch, "float32"), | ||
| quantization_config=qconfig, | ||
| low_precision_checkpoint=None, | ||
| deployment_mode=True, | ||
| inplace=True, | ||
| ) | ||
|
|
||
| return model | ||
|
|
||
| def import_algorithm_packages(self) -> Dict[str, Any]: | ||
| """ | ||
| Import the algorithm packages. | ||
|
|
||
| Returns | ||
| ------- | ||
| Dict[str, Any] | ||
| The algorithm packages. | ||
| """ | ||
| # Import necessary modules here to avoid unnecessary imports and ensure they're available when needed | ||
| import intel_extension_for_pytorch as ipex # type: ignore[import-untyped] | ||
| from intel_extension_for_pytorch.quantization import WoqWeightDtype # type: ignore[import-untyped] | ||
|
|
||
| return dict( | ||
| ipex=ipex, | ||
| WoqWeightDtype=WoqWeightDtype, | ||
| ) | ||
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.