|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 |
| - "# Intel® Low Precision Optimization Tool (LPOT) Sample for Tensorflow" |
| 7 | + "# Intel® Neural Compressor Sample for Tensorflow" |
8 | 8 | ]
|
9 | 9 | },
|
10 | 10 | {
|
|
13 | 13 | "source": [
|
14 | 14 | "## Agenda\n",
|
15 | 15 | "- Train a CNN Model Based on Keras\n",
|
16 |
| - "- Quantize Keras Model by LPOT\n", |
| 16 | + "- Quantize Keras Model by Intel® Neural Compressor\n", |
17 | 17 | "- Compare Quantized Model"
|
18 | 18 | ]
|
19 | 19 | },
|
20 | 20 | {
|
21 | 21 | "cell_type": "markdown",
|
22 | 22 | "metadata": {},
|
23 | 23 | "source": [
|
24 |
| - "### LPOT Release and Sample \n", |
| 24 | + "### Intel® Neural Compressor Release and Sample \n", |
25 | 25 | "\n",
|
26 |
| - "This sample code is always updated for the LPOT release in latest oneAPI release.\n", |
| 26 | + "This sample code is always updated for the Intel® Neural Compressor release in latest oneAPI release.\n", |
27 | 27 | "\n",
|
28 | 28 | "If you want to get the sample code for old oneAPI release, please checkout the old sample code release by git tag.\n",
|
29 | 29 | "\n",
|
|
51 | 51 | "source": [
|
52 | 52 | "Import python packages and check version.\n",
|
53 | 53 | "\n",
|
54 |
| - "Make sure the Tensorflow is **2.2** or newer, LPOT is **not 1.2** and matplotlib are installed.\n", |
| 54 | + "Make sure the Tensorflow is **2.2** or newer, Intel® Neural Compressor is **not 1.2** and matplotlib are installed.\n", |
55 | 55 | "\n",
|
56 |
| - "Note, LPOT has an old name **ilit**. Following script supports to old package name **ilit**." |
| 56 | + "Note, Intel® Neural Compressor has an old names: **lpot**, **ilit**. Following script supports to old package names." |
57 | 57 | ]
|
58 | 58 | },
|
59 | 59 | {
|
|
64 | 64 | "source": [
|
65 | 65 | "import tensorflow as tf\n",
|
66 | 66 | "print(\"Tensorflow version {}\".format(tf.__version__))\n",
|
| 67 | + "tf.compat.v1.enable_eager_execution()\n", |
67 | 68 | "\n",
|
68 | 69 | "try:\n",
|
69 |
| - " import lpot\n", |
70 |
| - " print(\"LPOT version {}\".format(lpot.__version__)) \n", |
| 70 | + " import neural_compressor as inc\n", |
| 71 | + " print(\"neural_compressor version {}\".format(inc.__version__)) \n", |
71 | 72 | "except:\n",
|
72 |
| - " import ilit as lpot\n", |
73 |
| - " print(\"iLiT version {}\".format(lpot.__version__)) \n", |
| 73 | + " try:\n", |
| 74 | + " import lpot as inc\n", |
| 75 | + " print(\"LPOT version {}\".format(inc.__version__)) \n", |
| 76 | + " except:\n", |
| 77 | + " import ilit as inc\n", |
| 78 | + " print(\"iLiT version {}\".format(inc.__version__)) \n", |
74 | 79 | "\n",
|
75 | 80 | "import matplotlib.pyplot as plt\n",
|
76 | 81 | "import numpy as np"
|
|
87 | 92 | "cell_type": "markdown",
|
88 | 93 | "metadata": {},
|
89 | 94 | "source": [
|
90 |
| - "Intel Optimized TensorFlow 2.5.0 requires to set environment variable **TF_ENABLE_MKL_NATIVE_FORMAT=0** before running LPOT quantize Fp32 model or deploying the quantized model." |
| 95 | + "Intel Optimized TensorFlow 2.5.0 and later require to set environment variable **TF_ENABLE_MKL_NATIVE_FORMAT=0** before running Intel® Neural Compressor quantize Fp32 model or deploying the quantized model." |
91 | 96 | ]
|
92 | 97 | },
|
93 | 98 | {
|
|
219 | 224 | "cell_type": "markdown",
|
220 | 225 | "metadata": {},
|
221 | 226 | "source": [
|
222 |
| - "## Quantize FP32 Model by LPOT\n", |
| 227 | + "## Quantize FP32 Model by Intel® Neural Compressor\n", |
223 | 228 | "\n",
|
224 |
| - "LPOT supports to quantize the model with a validation dataset for tuning.\n", |
| 229 | + "Intel® Neural Compressor supports to quantize the model with a validation dataset for tuning.\n", |
225 | 230 | "Finally, it returns an frezon quantized model based on int8.\n",
|
226 | 231 | "\n",
|
227 |
| - "We prepare a python script \"**LPOT_quantize_model.py**\" to call LPOT to finish the all quantization job.\n", |
| 232 | + "We prepare a python script \"**inc_quantize_model.py**\" to call Intel® Neural Compressor to finish the all quantization job.\n", |
228 | 233 | "Following code sample is used to explain the code.\n",
|
229 | 234 | "\n",
|
230 | 235 | "### Define Dataloader\n",
|
|
286 | 291 | "source": [
|
287 | 292 | "### Define Yaml File\n",
|
288 | 293 | "\n",
|
289 |
| - "We define alexnet.yaml to save the necessary parameters for LPOT.\n", |
| 294 | + "We define alexnet.yaml to save the necessary parameters for Intel® Neural Compressor.\n", |
290 | 295 | "In this case, we only need to change the input/output according to the fp32 model.\n",
|
291 | 296 | "\n",
|
292 | 297 | "In this case, the input node name is '**x**'.\n",
|
|
322 | 327 | "\n",
|
323 | 328 | "def auto_tune(input_graph_path, yaml_config, batch_size): \n",
|
324 | 329 | " fp32_graph = alexnet.load_pb(input_graph_path)\n",
|
325 |
| - " quan = lpot.Quantization(yaml_config)\n", |
| 330 | + " quan = inc.Quantization(yaml_config)\n", |
326 | 331 | " dataloader = Dataloader(batch_size)\n",
|
327 | 332 | " assert(dataloader)\n",
|
328 | 333 | " q_model = quan(\n",
|
|
350 | 355 | "source": [
|
351 | 356 | "### Call Function to Quantize the Model\n",
|
352 | 357 | "\n",
|
353 |
| - "Show the code in \"**lpot_quantize_model.py**\"." |
| 358 | + "Show the code in \"**inc_quantize_model.py**\"." |
354 | 359 | ]
|
355 | 360 | },
|
356 | 361 | {
|
357 | 362 | "cell_type": "code",
|
358 | 363 | "execution_count": null,
|
359 |
| - "metadata": {}, |
| 364 | + "metadata": { |
| 365 | + "scrolled": true |
| 366 | + }, |
360 | 367 | "outputs": [],
|
361 | 368 | "source": [
|
362 |
| - "!cat lpot_quantize_model.py" |
| 369 | + "!cat inc_quantize_model.py" |
363 | 370 | ]
|
364 | 371 | },
|
365 | 372 | {
|
366 | 373 | "cell_type": "markdown",
|
367 | 374 | "metadata": {},
|
368 | 375 | "source": [
|
369 |
| - "We will execute the \"**lpot_quantize_model.py**\" to show the whole process of quantizing a model." |
| 376 | + "We will execute the \"**inc_quantize_model.py**\" to show the whole process of quantizing a model." |
370 | 377 | ]
|
371 | 378 | },
|
372 | 379 | {
|
|
377 | 384 | },
|
378 | 385 | "outputs": [],
|
379 | 386 | "source": [
|
380 |
| - "!python lpot_quantize_model.py" |
| 387 | + "!python inc_quantize_model.py" |
381 | 388 | ]
|
382 | 389 | },
|
383 | 390 | {
|
|
393 | 400 | "source": [
|
394 | 401 | "## Compare Quantized Model\n",
|
395 | 402 | "\n",
|
396 |
| - "We prepare a script **profiling_lpot.py** to test the performance of PB model.\n", |
| 403 | + "We prepare a script **profiling_inc.py** to test the performance of PB model.\n", |
397 | 404 | "\n",
|
398 | 405 | "There is no correct performance data if run the code by jupyter notebook. So we run the script as process.\n",
|
399 | 406 | "\n",
|
400 |
| - "Let learn **profiling_lpot.py**. " |
| 407 | + "Let learn **profiling_inc.py**. " |
401 | 408 | ]
|
402 | 409 | },
|
403 | 410 | {
|
|
406 | 413 | "metadata": {},
|
407 | 414 | "outputs": [],
|
408 | 415 | "source": [
|
409 |
| - "!cat profiling_lpot.py" |
| 416 | + "!cat profiling_inc.py" |
410 | 417 | ]
|
411 | 418 | },
|
412 | 419 | {
|
413 | 420 | "cell_type": "markdown",
|
414 | 421 | "metadata": {},
|
415 | 422 | "source": [
|
416 |
| - "Execute the **profiling_lpot.py** with FP32 model file:" |
| 423 | + "Execute the **profiling_inc.py** with FP32 model file:" |
417 | 424 | ]
|
418 | 425 | },
|
419 | 426 | {
|
420 | 427 | "cell_type": "code",
|
421 | 428 | "execution_count": null,
|
422 |
| - "metadata": {}, |
| 429 | + "metadata": { |
| 430 | + "scrolled": true |
| 431 | + }, |
423 | 432 | "outputs": [],
|
424 | 433 | "source": [
|
425 |
| - "!python profiling_lpot.py --input-graph=./fp32_frezon.pb --omp-num-threads=4 --num-inter-threads=1 --num-intra-threads=4 --index=32" |
| 434 | + "!python profiling_inc.py --input-graph=./fp32_frezon.pb --omp-num-threads=4 --num-inter-threads=1 --num-intra-threads=4 --index=32" |
426 | 435 | ]
|
427 | 436 | },
|
428 | 437 | {
|
429 | 438 | "cell_type": "markdown",
|
430 | 439 | "metadata": {},
|
431 | 440 | "source": [
|
432 |
| - "Execute the **profiling_lpot.py** with int8 model file:" |
| 441 | + "Execute the **profiling_inc.py** with int8 model file:" |
433 | 442 | ]
|
434 | 443 | },
|
435 | 444 | {
|
436 | 445 | "cell_type": "code",
|
437 | 446 | "execution_count": null,
|
438 |
| - "metadata": {}, |
| 447 | + "metadata": { |
| 448 | + "scrolled": true |
| 449 | + }, |
439 | 450 | "outputs": [],
|
440 | 451 | "source": [
|
441 |
| - "!python profiling_lpot.py --input-graph=./alexnet_int8_model.pb --omp-num-threads=4 --num-inter-threads=1 --num-intra-threads=4 --index=8" |
| 452 | + "!python profiling_inc.py --input-graph=./alexnet_int8_model.pb --omp-num-threads=4 --num-inter-threads=1 --num-intra-threads=4 --index=8" |
442 | 453 | ]
|
443 | 454 | },
|
444 | 455 | {
|
445 | 456 | "cell_type": "code",
|
446 | 457 | "execution_count": null,
|
447 |
| - "metadata": {}, |
| 458 | + "metadata": { |
| 459 | + "scrolled": true |
| 460 | + }, |
448 | 461 | "outputs": [],
|
449 | 462 | "source": [
|
450 | 463 | "!cat 32.json\n",
|
|
570 | 583 | "- FP32 to INT8.\n",
|
571 | 584 | "- Intel® Deep Learning Boost speed up INT8 if your CPU is the Second Generation Intel® Xeon® Scalable Processors which supports it."
|
572 | 585 | ]
|
573 |
| - }, |
574 |
| - { |
575 |
| - "cell_type": "code", |
576 |
| - "execution_count": null, |
577 |
| - "metadata": {}, |
578 |
| - "outputs": [], |
579 |
| - "source": [] |
580 | 586 | }
|
581 | 587 | ],
|
582 | 588 | "metadata": {
|
|
595 | 601 | "name": "python",
|
596 | 602 | "nbconvert_exporter": "python",
|
597 | 603 | "pygments_lexer": "ipython3",
|
598 |
| - "version": "3.7.11" |
| 604 | + "version": "3.9.7" |
599 | 605 | }
|
600 | 606 | },
|
601 | 607 | "nbformat": 4,
|
|
0 commit comments