Skip to content

Commit 47c231e

Browse files
committed
add a draft
1 parent 60c17d7 commit 47c231e

File tree

7 files changed

+534
-0
lines changed

7 files changed

+534
-0
lines changed

AI-and-Analytics/Getting-Started-Samples/IntelJAX_GettingStarted/.gitkeep

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright Intel Corporation
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# `TensorFlow* Getting Started` Sample
2+
3+
The `TensorFlow* Getting Started` sample demonstrates how to train a TensorFlow* model and run inference on Intel® hardware.
4+
| Property | Description
5+
|:--- |:---
6+
| Category | Get Start Sample
7+
| What you will learn | How to start using TensorFlow* on Intel® hardware.
8+
| Time to complete | 10 minutes
9+
10+
## Purpose
11+
12+
TensorFlow* is a widely-used machine learning framework in the deep learning arena, demanding efficient computational resource utilization. To take full advantage of Intel® architecture and to extract maximum performance, the TensorFlow* framework has been optimized using Intel® oneDNN primitives. This sample demonstrates how to train an example neural network and shows how Intel-optimized TensorFlow* enables Intel® oneDNN calls by default. Intel-optimized TensorFlow* is available as part of the Intel® AI Tools.
13+
14+
This sample code shows how to get started with TensorFlow*. It implements an example neural network with one convolution layer and one ReLU layer. You can build and train a TensorFlow* neural network using a simple Python code. Also, by controlling the build-in environment variable, this sample attempts to demonstrate explicitly how Intel® oneDNN Primitives are called and shows their performance during the neural network training.
15+
16+
## Prerequisites
17+
18+
| Optimized for | Description
19+
|:--- |:---
20+
| OS | Ubuntu* 22.0.4 and newer
21+
| Hardware | Intel® Xeon® Scalable processor family
22+
| Software | TensorFlow
23+
24+
> **Note**: AI and Analytics samples are validated on AI Tools Offline Installer. For the full list of validated platforms refer to [Platform Validation](https://github.com/oneapi-src/oneAPI-samples/tree/master?tab=readme-ov-file#platform-validation).
25+
26+
## Key Implementation Details
27+
28+
The sample includes one python file: TensorFlow_HelloWorld.py. it implements a simple neural network's training and inference
29+
- The training data is generated by `np.random`.
30+
- The neural network with one convolution layer and one ReLU layer is created by `tf.nn.conv2d` and `tf.nn.relu`.
31+
- The TF session is initialized by `tf.global_variables_initializer`.
32+
- The train is implemented via the below for-loop:
33+
```python
34+
for epoch in range(0, EPOCHNUM):
35+
for step in range(0, BS_TRAIN):
36+
x_batch = x_data[step*N:(step+1)*N, :, :, :]
37+
y_batch = y_data[step*N:(step+1)*N, :, :, :]
38+
s.run(train, feed_dict={x: x_batch, y: y_batch})
39+
```
40+
In order to show the harware information, you must export the environment variable `export ONEDNN_VERBOSE=1` to display the deep learning primitives trace during execution.
41+
>**Note**: For convenience, code line os.environ["ONEDNN_VERBOSE"] = "1" has been added in the body of the script as an alternative method to setting this variable.
42+
43+
Runtime settings for `ONEDNN_VERBOSE`, `KMP_AFFINITY`, and `Inter/Intra-op` Threads are set within the script. You can read more about these settings in this dedicated document: *[Maximize TensorFlow* Performance on CPU: Considerations and Recommendations for Inference Workloads](https://software.intel.com/en-us/articles/maximize-tensorflow-performance-on-cpu-considerations-and-recommendations-for-inference)*.
44+
45+
### Run the Sample on Intel® GPUs
46+
The sample code is CPU based, but you can run it using Intel® Extension for TensorFlow* with Intel® Data Center GPU Flex Series. If you are using the Intel GPU, refer to *[Intel GPU Software Installation Guide](https://intel.github.io/intel-extension-for-tensorflow/latest/docs/install/install_for_gpu.html)*. The sample should be able to run on GPU **without any code changes**.
47+
48+
For details, refer to the *[Quick Example on Intel CPU and GPU](https://intel.github.io/intel-extension-for-tensorflow/latest/examples/quick_example.html)* topic of the *Intel® Extension for TensorFlow** documentation.
49+
50+
## Environment Setup
51+
52+
You will need to download and install the following toolkits, tools, and components to use the sample.
53+
54+
**1. Get Intel® AI Tools**
55+
56+
Required AI Tools: 'Intel® Extension for TensorFlow* - CPU'
57+
<br>If you have not already, select and install these Tools via [AI Tools Selector](https://www.intel.com/content/www/us/en/developer/tools/oneapi/ai-tools-selector.html). AI and Analytics samples are validated on AI Tools Offline Installer. It is recommended to select Offline Installer option in AI Tools Selector.<br>
58+
please see the [supported versions](https://www.intel.com/content/www/us/en/developer/tools/oneapi/ai-tools-selector.html).
59+
60+
>**Note**: If Docker option is chosen in AI Tools Selector, refer to [Working with Preset Containers](https://github.com/intel/ai-containers/tree/main/preset) to learn how to run the docker and samples.
61+
62+
**2. (Offline Installer) Activate the AI Tools bundle base environment**
63+
64+
If the default path is used during the installation of AI Tools:
65+
```
66+
source $HOME/intel/oneapi/intelpython/bin/activate
67+
```
68+
If a non-default path is used:
69+
```
70+
source <custom_path>/bin/activate
71+
```
72+
73+
**3. (Offline Installer) Activate relevant Conda environment**
74+
75+
For the system with Intel CPU:
76+
```
77+
conda activate tensorflow
78+
```
79+
For the system with Intel GPU:
80+
```
81+
conda activate tensorflow-gpu
82+
```
83+
**4. Clone the GitHub repository**
84+
```
85+
git clone https://github.com/oneapi-src/oneAPI-samples.git
86+
cd oneAPI-samples/AI-and-Analytics/Getting-Started-Samples/IntelTensorFlow_GettingStarted
87+
```
88+
## Run the Sample
89+
90+
>**Note**: Before running the sample, make sure Environment Setup is completed.
91+
Go to the section which corresponds to the installation method chosen in [AI Tools Selector](https://www.intel.com/content/www/us/en/developer/tools/oneapi/ai-tools-selector.html) to see relevant instructions:
92+
* [AI Tools Offline Installer (Validated)/Conda/PIP](#ai-tools-offline-installer-validatedcondapip)
93+
* [Docker](#docker)
94+
### AI Tools Offline Installer (Validated)/Conda/PIP
95+
```
96+
python TensorFlow_HelloWorld.py
97+
```
98+
### Docker
99+
AI Tools Docker images already have Get Started samples pre-installed. Refer to [Working with Preset Containers](https://github.com/intel/ai-containers/tree/main/preset) to learn how to run the docker and samples.
100+
## Example Output
101+
1. With the initial run, you should see results similar to the following:
102+
103+
```
104+
0 0.4147554
105+
1 0.3561021
106+
2 0.33979267
107+
3 0.33283564
108+
4 0.32920069
109+
[CODE_SAMPLE_COMPLETED_SUCCESSFULLY]
110+
```
111+
2. Export `ONEDNN_VERBOSE` as 1 in the command line. The oneDNN run-time verbose trace should look similar to the following:
112+
```
113+
export ONEDNN_VERBOSE=1
114+
Windows: set ONEDNN_VERBOSE=1
115+
```
116+
>**Note**: The historical environment variables include `DNNL_VERBOSE` and `MKLDNN_VERBOSE`.
117+
118+
3. Run the sample again. You should see verbose results similar to the following:
119+
```
120+
2024-03-12 16:01:59.784340: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:117] Plugin optimizer for device_type CPU is enabled.
121+
onednn_verbose,info,oneDNN v3.2.0 (commit 8f2a00d86546e44501c61c38817138619febbb10)
122+
onednn_verbose,info,cpu,runtime:OpenMP,nthr:24
123+
onednn_verbose,info,cpu,isa:Intel AVX2 with Intel DL Boost
124+
onednn_verbose,info,gpu,runtime:none
125+
onednn_verbose,info,prim_template:operation,engine,primitive,implementation,prop_kind,memory_descriptors,attributes,auxiliary,problem_desc,exec_time
126+
onednn_verbose,exec,cpu,reorder,jit:uni,undef,src_f32::blocked:cdba::f0 dst_f32:p:blocked:Acdb16a::f0,,,10x4x3x3,0.00195312
127+
onednn_verbose,exec,cpu,convolution,brgconv:avx2,forward_training,src_f32::blocked:acdb::f0 wei_f32:ap:blocked:Acdb16a::f0 bia_f32::blocked:a::f0
128+
dst_f32::blocked:acdb::f0,attr-scratchpad:user attr-post-ops:eltwise_relu ,alg:convolution_direct,mb4_ic4oc10_ih128oh128kh3sh1dh0ph1_iw128ow128kw3sw1dw0pw1,1.19702
129+
onednn_verbose,exec,cpu,eltwise,jit:avx2,backward_data,data_f32::blocked:abcd::f0 diff_f32::blocked:abcd::f0,attr-scratchpad:user ,alg:eltwise_relu alpha:0
130+
beta:0,4x128x128x10,0.112061
131+
onednn_verbose,exec,cpu,convolution,jit:avx2,backward_weights,src_f32::blocked:acdb::f0 wei_f32:ap:blocked:ABcd8b8a::f0 bia_undef::undef:::
132+
dst_f32::blocked:acdb::f0,attr-scratchpad:user ,alg:convolution_direct,mb4_ic4oc10_ih128oh128kh3sh1dh0ph1_iw128ow128kw3sw1dw0pw1,0.358887
133+
...
134+
135+
>**Note**: See the *[oneAPI Deep Neural Network Library Developer Guide and Reference](https://oneapi-src.github.io/oneDNN/dev_guide_verbose.html)* for more details on the verbose log.
136+
137+
4. Troubleshooting
138+
139+
If you receive an error message, troubleshoot the problem using the **Diagnostics Utility for Intel® oneAPI Toolkits**. The diagnostic utility provides configuration and system checks to help find missing dependencies, permissions errors, and other issues. See the *[Diagnostics Utility for Intel® oneAPI Toolkits User Guide](https://www.intel.com/content/www/us/en/develop/documentation/diagnostic-utility-user-guide/top.html)* for more information on using the utility.
140+
or ask support from https://github.com/intel/intel-extension-for-tensorflow
141+
142+
## Related Samples
143+
144+
* [Intel Extension For TensorFlow Getting Started Sample](https://github.com/oneapi-src/oneAPI-samples/blob/development/AI-and-Analytics/Getting-Started-Samples/Intel_Extension_For_TensorFlow_GettingStarted/README.md)
145+
146+
## License
147+
148+
Code samples are licensed under the MIT license. See
149+
[License.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/License.txt)
150+
for details.
151+
152+
Third party program Licenses can be found here:
153+
[third-party-programs.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/third-party-programs.txt)
154+
155+
*Other names and brands may be claimed as the property of others. [Trademarks](https://www.intel.com/content/www/us/en/legal/trademarks.html)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
'''
5+
==============================================================
6+
Copyright © 2019 Intel Corporation
7+
8+
SPDX-License-Identifier: MIT
9+
==============================================================
10+
'''
11+
import tensorflow.compat.v1 as tf
12+
import numpy as np
13+
import sys
14+
15+
16+
tf.disable_v2_behavior()
17+
18+
19+
'''
20+
Environment settings:
21+
Set ONEDNN_VERBOSE=1 to show oneDNN run time verbose
22+
Set KMP_AFFINITY=verbose to show OpenMP thread information
23+
'''
24+
#import os; os.environ["ONEDNN_VERBOSE"] = "1"
25+
#import os; os.environ["KMP_AFFINITY"] = "granularity=fine,compact,1,0"
26+
'''
27+
Sanity Check: once Intel-optimized TensorFlow is installed, Intel oneDNN optimizations are present by default.
28+
'''
29+
#TODO for TF2.0
30+
#print("Intel oneDNN optimizations are present : ", tf.pywrap_tensorflow.IsMklEnabled())
31+
32+
'''
33+
learning_rate = 0.1
34+
batch_size : Batch size
35+
BS_TRAIN: Number of Batch size for training data
36+
EPOCHNUM: Number of epoch for training
37+
'''
38+
learning_rate = 0.1
39+
batch_size = 4
40+
BS_TRAIN = 10
41+
EPOCHNUM = 5
42+
43+
'''
44+
Perform training and inference in main function
45+
'''
46+
def main():
47+
'''Define input/output data size'''
48+
N, C_in, C_out, H, W = batch_size, 4, 10, 128, 128
49+
50+
''' Create random array to hold inputs and outputs '''
51+
x_data = np.float32(np.random.random([N*BS_TRAIN, H, W, C_in]))
52+
y_data = np.float32(np.random.random([N*BS_TRAIN, H, W, C_out]))
53+
54+
'''
55+
Create l layer conv2d + relu neural network.
56+
given an input tensor of shape [batch, in_height, in_width, in_channels] and a filter / kernel tensor of shape [filter_height, filter_width, in_channels, out_channels], output is [batch, in_height/strides, in_width/striders, out_channels]
57+
'''
58+
filter = tf.Variable(tf.random.uniform([3,3,C_in, C_out],-1.0,1.0))
59+
bias = tf.Variable(tf.constant(0.1, shape=[C_out]))
60+
61+
x = tf.placeholder("float", [N, H, W, C_in])
62+
y = tf.placeholder("float", [N, H, W, C_out])
63+
64+
y_con2d = tf.nn.conv2d(x, filter, strides=[1,1,1,1], data_format='NHWC', padding='SAME')
65+
y_pred = tf.nn.relu(y_con2d + bias)
66+
67+
'''Define the loss function and optimizser'''
68+
loss = tf.reduce_mean(tf.square(y-y_pred))
69+
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
70+
train = optimizer.minimize(loss)
71+
72+
'''Initialize the session and set intra/inter op threads'''
73+
init = tf.global_variables_initializer()
74+
config = tf.ConfigProto()
75+
config.intra_op_parallelism_threads = 8 # Set to number of physical cores
76+
config.inter_op_parallelism_threads = 1 # Set to number of sockets
77+
tf.Session(config=config)
78+
s = tf.Session()
79+
s.run(init)
80+
81+
'''start train step'''
82+
for epoch in range(0, EPOCHNUM):
83+
for step in range(0, BS_TRAIN):
84+
x_batch = x_data[step*N:(step+1)*N, :, :, :]
85+
y_batch = y_data[step*N:(step+1)*N, :, :, :]
86+
s.run(train, feed_dict={x: x_batch, y: y_batch})
87+
'''Compute and print loss. We pass Tensors containing the predicted and true values of y, and the loss function returns a Tensor containing the loss.'''
88+
print(epoch, s.run(loss,feed_dict={x: x_batch, y: y_batch}))
89+
90+
if __name__ == '__main__':
91+
main()
92+
print("Tensorflow HelloWorld Done!")
93+
print("[CODE_SAMPLE_COMPLETED_SUCCESFULLY]")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source /opt/intel/oneapi/setvars.sh
2+
source activate tensorflow
3+
python TensorFlow_HelloWorld.py
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"guid": "111213A0-C930-45B4-820F-02472BABBF34",
3+
"name": "Intel® Optimization for TensorFlow* Getting Started",
4+
"categories": ["Toolkit/oneAPI AI And Analytics/Getting Started"],
5+
"description": "This sample illustrates how to train a TensorFlow model and run inference with oneMKL and oneDNN.",
6+
"builder": ["cli"],
7+
"languages": [{
8+
"python": {}
9+
}],
10+
"os": ["linux"],
11+
"targetDevice": ["CPU"],
12+
"ciTests": {
13+
"linux": [{
14+
"id": "tensorflow hello world",
15+
"steps": [
16+
"source /intel/oneapi/intelpython/bin/activate",
17+
"conda activate tensorflow",
18+
"python TensorFlow_HelloWorld.py"
19+
]
20+
}]
21+
},
22+
"expertise": "Getting Started"
23+
}

0 commit comments

Comments
 (0)