|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# IPEX model for text-generation" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "IPEX model will replace the linears and some ops. Please note that IPEXModel uses a graph mode model to inference to accelerate the generation." |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 2, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "import torch\n", |
| 24 | + "from transformers import AutoTokenizer\n", |
| 25 | + "from optimum.intel.ipex import IPEXModelForCausalLM" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "code", |
| 30 | + "execution_count": 3, |
| 31 | + "metadata": {}, |
| 32 | + "outputs": [ |
| 33 | + { |
| 34 | + "name": "stderr", |
| 35 | + "output_type": "stream", |
| 36 | + "text": [ |
| 37 | + "Framework not specified. Using pt to export the model.\n", |
| 38 | + "Passing the argument `library_name` to `get_supported_tasks_for_model_type` is required, but got library_name=None. Defaulting to `transformers`. An error will be raised in a future version of Optimum if `library_name` is not provided.\n", |
| 39 | + "/home/jiqingfe/frameworks.ai.pytorch.ipex-cpu/intel_extension_for_pytorch/frontend.py:462: UserWarning: Conv BatchNorm folding failed during the optimize process.\n", |
| 40 | + " warnings.warn(\n", |
| 41 | + "/home/jiqingfe/frameworks.ai.pytorch.ipex-cpu/intel_extension_for_pytorch/frontend.py:469: UserWarning: Linear BatchNorm folding failed during the optimize process.\n", |
| 42 | + " warnings.warn(\n", |
| 43 | + "/home/jiqingfe/miniconda3/envs/ipex/lib/python3.10/site-packages/transformers/modeling_utils.py:4193: FutureWarning: `_is_quantized_training_enabled` is going to be deprecated in transformers 4.39.0. Please use `model.hf_quantizer.is_trainable` instead\n", |
| 44 | + " warnings.warn(\n", |
| 45 | + "/home/jiqingfe/miniconda3/envs/ipex/lib/python3.10/site-packages/transformers/models/gpt2/modeling_gpt2.py:801: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n", |
| 46 | + " if batch_size <= 0:\n", |
| 47 | + "Passing the argument `library_name` to `get_supported_tasks_for_model_type` is required, but got library_name=None. Defaulting to `transformers`. An error will be raised in a future version of Optimum if `library_name` is not provided.\n", |
| 48 | + "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n", |
| 49 | + "access to the `model_dtype` attribute is deprecated and will be removed after v1.18.0, please use `_dtype` instead.\n" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "name": "stdout", |
| 54 | + "output_type": "stream", |
| 55 | + "text": [ |
| 56 | + "Answer the following yes/no question by reasoning step-by-step please. Can you write a whole Haiku in a single tweet? Yes, you can.\n", |
| 57 | + "\n", |
| 58 | + "Yes, I can write Haikus in one tweet. I have no idea how to do that, but I'm sure\n" |
| 59 | + ] |
| 60 | + } |
| 61 | + ], |
| 62 | + "source": [ |
| 63 | + "model = IPEXModelForCausalLM.from_pretrained(\"gpt2\", torch_dtype=torch.bfloat16, export=True)\n", |
| 64 | + "tokenizer = AutoTokenizer.from_pretrained(\"gpt2\")\n", |
| 65 | + "input_sentence = [\"Answer the following yes/no question by reasoning step-by-step please. Can you write a whole Haiku in a single tweet?\"]\n", |
| 66 | + "model_inputs = tokenizer(input_sentence, return_tensors=\"pt\")\n", |
| 67 | + "generation_kwargs = dict(max_new_tokens=32, do_sample=False, num_beams=4, num_beam_groups=1, no_repeat_ngram_size=2, use_cache=True)\n", |
| 68 | + "\n", |
| 69 | + "generated_ids = model.generate(**model_inputs, **generation_kwargs)\n", |
| 70 | + "output = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]\n", |
| 71 | + "print(output)" |
| 72 | + ] |
| 73 | + } |
| 74 | + ], |
| 75 | + "metadata": { |
| 76 | + "kernelspec": { |
| 77 | + "display_name": "ipex", |
| 78 | + "language": "python", |
| 79 | + "name": "python3" |
| 80 | + }, |
| 81 | + "language_info": { |
| 82 | + "codemirror_mode": { |
| 83 | + "name": "ipython", |
| 84 | + "version": 3 |
| 85 | + }, |
| 86 | + "file_extension": ".py", |
| 87 | + "mimetype": "text/x-python", |
| 88 | + "name": "python", |
| 89 | + "nbconvert_exporter": "python", |
| 90 | + "pygments_lexer": "ipython3", |
| 91 | + "version": "3.10.13" |
| 92 | + } |
| 93 | + }, |
| 94 | + "nbformat": 4, |
| 95 | + "nbformat_minor": 2 |
| 96 | +} |
0 commit comments