Optimization
Optimization
Optimum Intel can be used to apply popular compression techniques such as quantization, pruning and knowledge distillation.
Post-training optimization
Post-training compression techniques such as dynamic and static quantization can be easily applied on your model using our INCQuantizer. Note that quantization is currently only supported for CPUs (only CPU backends are available), so we will not be utilizing GPUs / CUDA in the following examples.
Dynamic quantization
You can easily add dynamic quantization on your model by using the following command line:
Copied
optimum-cli inc quantize --model distilbert-base-cased-distilled-squad --output quantized_distilbertWhen applying post-training quantization, an accuracy tolerance along with an adapted evaluation function can also be specified in order to find a quantized model meeting the specified constraints. This can be done for both dynamic and static quantization.
Copied
import evaluate
from optimum.intel import INCQuantizer
from datasets import load_dataset
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
from neural_compressor.config import AccuracyCriterion, TuningCriterion, PostTrainingQuantConfig
model_name = "distilbert-base-cased-distilled-squad"
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
eval_dataset = load_dataset("squad", split="validation").select(range(64))
task_evaluator = evaluate.evaluator("question-answering")
qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer)
def eval_fn(model):
qa_pipeline.model = model
metrics = task_evaluator.compute(model_or_pipeline=qa_pipeline, data=eval_dataset, metric="squad")
return metrics["f1"]
# Set the accepted accuracy loss to 5%
accuracy_criterion = AccuracyCriterion(tolerable_loss=0.05)
# Set the maximum number of trials to 10
tuning_criterion = TuningCriterion(max_trials=10)
quantization_config = PostTrainingQuantConfig(
approach="dynamic", accuracy_criterion=accuracy_criterion, tuning_criterion=tuning_criterion
)
quantizer = INCQuantizer.from_pretrained(model, eval_fn=eval_fn)
quantizer.quantize(quantization_config=quantization_config, save_directory="dynamic_quantization")Static quantization
In the same manner we can apply static quantization, for which we also need to generate the calibration dataset in order to perform the calibration step.
Copied
Specify Quantization Recipes
The SmoothQuant methodology is available for post-training quantization. This methodology usually improves the accuracy of the model in comparison to other post-training static quantization methodologies. This is done by migrating the difficulty from activations to weights with a mathematically equivalent transformation.
Copied
Please refer to INC documentation and the list of models quantized with the methodology for more details.
Distributed Acuracy-aware Tuning
One challenge in model quantization is identifying the optimal configuration that balances accuracy and performance. Distributed tuning speeds up this time-consuming process by parallelizing it across multiple nodes, which accelerates the tuning process in linear scaling.
To utilize distributed tuning, please set the quant_level to 1 and run it with mpirun.
Copied
Copied
Please refer to INC documentation and text-classification example for more details.
During training optimization
The INCTrainer class provides an API to train your model while combining different compression techniques such as knowledge distillation, pruning and quantization. The INCTrainer is very similar to the π Transformers Trainer, which can be replaced with minimal changes in your code.
Quantization
To apply quantization during training, you only need to create the appropriate configuration and pass it to the INCTrainer.
Copied
Pruning
In the same manner, pruning can be applied by specifying the pruning configuration detailing the desired pruning process. To know more about the different supported methodologies, you can refer to the Neural Compressor documentation. At the moment, pruning is applied on both the linear and the convolutional layers, and not on other layers such as the embeddings. Itβs important to mention that the pruning sparsity defined in the configuration will be applied on these layers, and thus will not results in the global model sparsity.
Copied
Knowledge distillation
Knowledge distillation can also be applied in the same manner. To know more about the different supported methodologies, you can refer to the Neural Compressor documentation
Copied
Loading a quantized model
To load a quantized model hosted locally or on the π hub, you must instantiate you model using our INCModelForXxx classes.
Copied
You can load many more quantized models hosted on the hub under the Intel organization here.
Inference with Transformers pipeline
The quantized model can then easily be used to run inference with the Transformers pipelines.
Copied
Check out the examples directory for more sophisticated usage.
Last updated