Skip to content

Intel® TensorFlow* Model Zoo Inference With FP32 Int8 readme update #1469

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

Merged
merged 15 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
},
{
"cell_type": "markdown",
"id": "5eea6ae7",
"metadata": {},
"source": [
"The training times for the 3 cases are printed out and shown in the figure above. Using BF16 should show significant reduction in training time. However, there is little to no change using AVX512 with BF16 and AMX with BF16 because the amount of computations required for one batch is too small with this dataset. "
Expand Down Expand Up @@ -348,15 +349,16 @@
"id": "b6ea2aeb",
"metadata": {},
"source": [
"This figure shows the relative performance speedup of AMX compared to FP32 and BF16 with AVX512. The expected behavior is that AMX with BF16 should have about a 1.5X improvement over FP32 and about the same performance as BF16 with AVX512. To see more performance improvement between AVX-512 BF16 and AMX BF16, increase the amount of required computations in one batch. This can be done by increasing the batch size with CIFAR10 or using another dataset. "
"This figure shows the relative performance speedup of BF16 with AMX compared to BF16 with AVX512 and FP32."
]
},
{
"cell_type": "markdown",
"id": "0da073a6",
"id": "7bf01080",
"metadata": {},
"source": [
"This code sample shows how to enable and disable AMX during runtime, as well as the performance improvements using AMX BF16 for training the ResNet50 model. There will be additional significant performance improvements if AMX INT8 is used in inference, which is covered in a related oneAPI sample."
"## Conclusion\n",
"This code sample shows how to enable and disable AMX during runtime, as well as the performance improvements using AMX BF16 for training on the ResNet50 model. Performance will vary based on your hardware and software versions. To see more performance improvement between AVX-512 BF16 and AMX BF16, increase the amount of required computations in one batch. This can be done by increasing the batch size with CIFAR10 or using another dataset. For even more speedup, consider using the Intel® Extension for PyTorch* [Launch Script](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/performance_tuning/launch_script.html). "
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,9 @@ If you receive an error message, troubleshoot the problem using the **Diagnostic

## Example Output

If successful, the sample displays `[CODE_SAMPLE_COMPLETED_SUCCESSFULLY]`. Additionally, the sample generates performance and analysis diagrams for comparison.
If successful, the sample displays `[CODE_SAMPLE_COMPLETED_SUCCESSFULLY]`. Additionally, the sample will print out the runtimes and charts of performance relative to the FP32 model without any optimizations as the baseline.

The following image shows approximate performance speed increases using AMX BF16 with auto-mixed precision during training. To see more performance improvement between AVX-512 BF16 and AMX BF16, increase the amount of required computations in one batch. This can be done by increasing the batch size with CIFAR10 or using another dataset.

![comparison images](assets/amx_relative_speedup.png)
The performance speedups using AMX BF16 are approximate on ResNet50. Performance will vary based on your hardware and software versions. To see more performance improvement between AVX-512 BF16 and AMX BF16, increase the amount of required computations in one batch. This can be done by increasing the batch size with CIFAR10 or using another dataset. For even more speedup, consider using the Intel® Extension for PyTorch* [Launch Script](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/performance_tuning/launch_script.html).

## License

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,122 +1,166 @@
# `Intel® Model Zoo` Sample
This code example provides a sample code to run ResNet50 inference on Intel's pretrained FP32 and Int8 model
# `Intel® TensorFlow* Model Zoo Inference With FP32 Int8` Sample

The `Intel® TensorFlow* Model Zoo Inference With FP32 Int8` sample demonstrates how to run ResNet50 inference on pretrained FP32 and Int8 models included in the Model Zoo for Intel® Architecture.

| Area | Description
|:--- |:---
| What you will learn | How to perform TensorFlow* ResNet50 inference on synthetic data using FP32 and Int8 pre-trained models.
| Time to complete | 30 minutes
| Category | Code Optimization

## Purpose
- Demonstrate the AI workloads and deep learning models Intel has optimized and validated to run on Intel hardware
- Show how to efficiently execute, train, and deploy Intel-optimized models
- Make it easy to get started running Intel-optimized models on Intel hardware in the cloud or on bare metal

***DISCLAIMER: These scripts are not intended for benchmarking Intel platforms.
For any performance and/or benchmarking information on specific Intel platforms, visit [https://www.intel.ai/blog](https://www.intel.ai/blog).***
The sample intends to help you understand some key concepts:

## Key implementation details
The example uses Intel's pretrained model published as part of [Intel Model Zoo](https://github.com/IntelAI/models). The example also illustrates how to utilize TensorFlow and MKL run time settings to maximize CPU performance on ResNet50 workload.
- What AI workloads and deep learning models Intel has optimized and validated to run on Intel hardware.
- How to train and deploy Intel-optimized models.
- How to start running Intel-optimized models on Intel hardware in the cloud or on bare metal.

## License
Code samples are licensed under the MIT license. See
[License.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/License.txt) for details.
> **Disclaimer**: The sample and supplied scripts are not intended for benchmarking Intel platforms. For any performance and/or benchmarking information on specific Intel platforms, visit [https://www.intel.ai/blog](https://www.intel.ai/blog).

Third party program Licenses can be found here: [third-party-programs.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/third-party-programs.txt)
## Prerequisites

## Running Samples on the Intel® DevCloud
If you are running this sample on the DevCloud, skip the Pre-requirements and go to the [Activate Conda Environment](#activate-conda) section.
| Optimized for | Description
|:--- |:---
| OS | Ubuntu* 20.04 or higher
| Hardware | Intel® Core™ Gen10 Processor <br> Intel® Xeon® Scalable Performance processors
| Software | Intel® AI Analytics Toolkit (AI Kit)

## Pre-requirements (Local or Remote Host Installation)
### For Local Development Environments

TensorFlow* is ready for use once you finish the Intel® AI Analytics Toolkit (AI Kit) installation and have run the post installation script.
You will need to download and install the following toolkits, tools, and components to use the sample.

You can refer to the oneAPI [main page](https://software.intel.com/en-us/oneapi) for toolkit installation and the Toolkit [Intel&reg; AI Analytics Toolkit Get Started Guide for Linux](https://software.intel.com/en-us/get-started-with-intel-oneapi-linux-get-started-with-the-intel-ai-analytics-toolkit) for post-installation steps and scripts.
- **Intel® AI Analytics Toolkit (AI Kit)**

## Activate conda environment With Root Access<a name="activate-conda"></a>
You can get the AI Kit from [Intel® oneAPI Toolkits](https://www.intel.com/content/www/us/en/developer/tools/oneapi/toolkits.html#analytics-kit). <br> See [*Get Started with the Intel® AI Analytics Toolkit for Linux**](https://www.intel.com/content/www/us/en/develop/documentation/get-started-with-ai-linux) for AI Kit installation information and post-installation steps and scripts.

Navigate the Linux shell to your oneapi installation path, typically `/opt/intel/oneapi`. Activate the conda environment with the following command:
TensorFlow* or Pytorch* are ready for use once you finish installing and configuring the Intel® AI Analytics Toolkit (AI Kit).

#### Linux
```
conda activate tensorflow
```
### For Intel® DevCloud

The necessary tools and components are already installed in the environment. You do not need to install additional components. See [Intel® DevCloud for oneAPI](https://devcloud.intel.com/oneapi/get_started/) for information.

## Activate conda environment Without Root Access (Optional)
## Key Implementation Details

By default, the Intel® AI Analytics Toolkit is installed in the `/opt/intel/oneapi` folder, which requires root privileges to manage it. If you would like to bypass using root access to manage your conda environment, then you can clone your desired conda environment using the following command:
The example uses some pretrained models published as part of the [Model Zoo for Intel® Architecture](https://github.com/IntelAI/models). The example also illustrates how to utilize TensorFlow* and Intel® Math Kernel Library (Intel® MKL) runtime settings to maximize CPU performance on ResNet50 workload.

#### Linux
```
conda create --name user_tensorflow --clone tensorflow
```
## Set Environment Variables

Then activate your conda environment with the following command:
When working with the command-line interface (CLI), you should configure the oneAPI toolkits using environment variables. Set up your CLI environment by sourcing the `setvars` script every time you open a new terminal window. This practice ensures that your compiler, libraries, and tools are ready for development.

```
conda activate user_tensorflow
```
## Run the `Intel® TensorFlow* Model Zoo Inference With FP32 Int8` Sample

## Navigate to Intel Model Zoo
### On Linux*

Navigate to the Intel Model Zoo source directory. It's located in your oneapi installation path, typically `/opt/intel/oneapi/modelzoo`.
You can view the available Model Zoo release versions for the Intel® AI Analytics Toolkit:
```
ls /opt/intel/oneapi/modelzoo
1.8.0 latest
```
Then browse to the preferred [Intel Model Zoo](https://github.com/IntelAI/models/tree/master/benchmarks) release version location to run inference for ResNet50 or another supported topology.
```
cd /opt/intel/oneapi/modelzoo/latest
```
> **Note**: If you have not already done so, set up your CLI
> environment by sourcing the `setvars` script in the root of your oneAPI installation.
>
> Linux*:
> - For system wide installations: `. /opt/intel/oneapi/setvars.sh`
> - For private installations: ` . ~/intel/oneapi/setvars.sh`
> - For non-POSIX shells, like csh, use the following command: `bash -c 'source <install-dir>/setvars.sh ; exec csh'`
>
> For more information on configuring environment variables, see *[Use the setvars Script with Linux* or macOS*](https://www.intel.com/content/www/us/en/develop/documentation/oneapi-programming-guide/top/oneapi-development-environment-setup/use-the-setvars-script-with-linux-or-macos.html)*.

## Install Jupyter Notebook*
#### Activate Conda with Root Access

By default, the AI Kit is installed in the `/opt/intel/oneapi` folder and requires root privileges to manage it. However, if you activated another environment, you can return with the following command.
```
conda install jupyter nb_conda_kernels
conda activate tensorflow
```

## How to Build and Run
1. Go to the code example location.<br>
2. If you have GUI support, enter the command `jupyter notebook`. <br>
or<br>
a. If you do not have GUI support, open a remote shell and enter command `jupyter notebook --no-browser --port=8888`.<br>
b. Open the command prompt where you have GUI support, and forward the port from host to client.<br>
c. Enter `ssh -N -f -L localhost:8888:localhost:8888 <userid@hostname>`<br>
d. Copy-paste the URL address from the host into your local browser to open the jupyter console.<br>
3. Go to `ResNet50_Inference.ipynb` and run each cell to create synthetic data and run int8 inference.

---
**NOTE**
#### Activate Conda without Root Access (Optional)

In the jupyter page, be sure to select the correct kernel. In this example, select 'Kernel' -> 'Change kernel' -> Python [conda env:tensorflow].
You can choose to activate Conda environment without root access. To bypass root access to manage your Conda environment, clone and activate your desired Conda environment using the following commands similar to the following.

---
```
conda create --name user_tensorflow --clone tensorflow
conda activate user_tensorflow
```

### **Request a Compute Node**
In order to run on the DevCloud, you need to request a compute node using node properties such as: `gpu`, `xeon`, `fpga_compile`, `fpga_runtime` and others. For more information about the node properties, execute the `pbsnodes` command.
This node information must be provided when submitting a job to run your sample in batch mode using the qsub command. When you see the qsub command in the Run section of the [Hello World instructions](https://devcloud.intel.com/oneapi/get_started/aiAnalyticsToolkitSamples/), change the command to fit the node you are using. Nodes which are in bold indicate they are compatible with this sample:
#### Navigate to Model Zoo

<!---Mark each compatible Node in BOLD-->
| Node | Command |
| ----------------- | ------------------------------------------------------- |
| GPU | qsub -l nodes=1:gpu:ppn=2 -d . hello-world.sh |
| CPU | qsub -l nodes=1:xeon:ppn=2 -d . hello-world.sh |
| FPGA Compile Time | qsub -l nodes=1:fpga\_compile:ppn=2 -d . hello-world.sh |
| FPGA Runtime | qsub -l nodes=1:fpga\_runtime:ppn=2 -d . hello-world.sh |
Navigate to the Model Zoo for Intel® Architecture source directory. By default, it is in your installation path, like `/opt/intel/oneapi/modelzoo`.

1. View the available Model Zoo release versions for the AI Kit:
```
ls /opt/intel/oneapi/modelzoo
2.11.0 latest
```
2. Navigate to the [Model Zoo Scripts](https://github.com/IntelAI/models/tree/v2.11.0/benchmarks) GitHub repo to determine the preferred released version to run inference for ResNet50 or another supported topology.
```
cd /opt/intel/oneapi/modelzoo/latest
```

### Troubleshooting
If an error occurs, troubleshoot the problem using the Diagnostics Utility for Intel® oneAPI Toolkits.
[Learn more](https://software.intel.com/content/www/us/en/develop/documentation/diagnostic-utility-user-guide/top.html)
#### Install Jupyter Notebook

### Using Visual Studio Code* (Optional)
```
conda install jupyter nb_conda_kernels
```

You can use Visual Studio Code (VS Code) extensions to set your environment, create launch configurations,
and browse and download samples.
#### Open Jupyter Notebook

1. Change to the sample directory.
2. Launch Jupyter Notebook.
```
jupyter notebook
```
> **Note**: If you do not have GUI support, you must open a remote shell and launch the Notebook a different way.
> 1. Enter a command similar to the following:
> ```
> jupyter notebook --no-browser --port=8888`
> ```
>2. Open the command prompt where you have GUI support, and forward the port from host to client.
>3. Enter a command similar to the following:
> ```
> ssh -N -f -L localhost:8888:localhost:8888 <userid@hostname>
> ```
>4. Copy and paste the URL address from the host into your local browser.

3. Locate and select the Notebook.
```
ResNet50_Inference.ipynb
```
4. Change the kernel to **Python [conda env:tensorflow]**.
5. Click the **Run** button to move through the cells in sequence.

### Run the Sample on Intel® DevCloud (Optional)

1. If you do not already have an account, request an Intel® DevCloud account at [*Create an Intel® DevCloud Account*](https://intelsoftwaresites.secure.force.com/DevCloud/oneapi).
2. On a Linux* system, open a terminal.
3. SSH into Intel® DevCloud.
```
ssh DevCloud
```
> **Note**: You can find information about configuring your Linux system and connecting to Intel DevCloud at Intel® DevCloud for oneAPI [Get Started](https://devcloud.intel.com/oneapi/get_started).

4. You can specify a CPU node using a single line script.
```
qsub -I -l nodes=1:xeon:ppn=2 -d .
```

- `-I` (upper case I) requests an interactive session.
- `-l nodes=1:xeon:ppn=2` (lower case L) assigns one full GPU node.
- `-d .` makes the current folder as the working directory for the task.

|Available Nodes |Command Options
|:--- |:---
|GPU |`qsub -l nodes=1:gpu:ppn=2 -d .`
|CPU |`qsub -l nodes=1:xeon:ppn=2 -d .`

5. Activate conda.
` $ conda activate`
6. Follow the instructions to open the URL with the token in your browser.
7. Locate and select the Notebook.
```
ResNet50_Inference.ipynb
````
8. Change the kernel to **Python [conda env:tensorflow]**.
9. Run every cell in the Notebook in sequence.

The basic steps to build and run a sample using VS Code include:
- Download a sample using the extension **Code Sample Browser for Intel oneAPI Toolkits**.
- Configure the oneAPI environment with the extension **Environment Configurator for Intel oneAPI Toolkits**.
- Open a Terminal in VS Code (**Terminal>New Terminal**).
- Run the sample in the VS Code terminal using the instructions below.
- (Linux only) Debug your GPU application with GDB for Intel® oneAPI toolkits using the Generate Launch Configurations extension.
## License

To learn more about the extensions, see
[Using Visual Studio Code with Intel® oneAPI Toolkits](https://software.intel.com/content/www/us/en/develop/documentation/using-vs-code-with-intel-oneapi/top.html).
Code samples are licensed under the MIT license. See
[License.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/License.txt) for details.

After learning how to use the extensions for Intel oneAPI Toolkits, return to this readme for instructions on how to build and run a sample.
Third party program Licenses can be found here: [third-party-programs.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/third-party-programs.txt).