Skip to content

Commit 495ff2b

Browse files
authored
Merge pull request #2457 from oneapi-src/release/2024.2_AITools
2024.2 AI Tools second set of updates
2 parents 9d520b3 + a81f9dc commit 495ff2b

File tree

18 files changed

+675
-509
lines changed

18 files changed

+675
-509
lines changed

AI-and-Analytics/Features-and-Functionality/IntelPython_GPU_numba-dpex_Genetic_Algorithm/IntelPython_GPU_numba-dpex_Genetic_Algorithm.ipynb

+12-9
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@
348348
"\n",
349349
"The only par that differs form the standard implementation is the evaluation function.\n",
350350
"\n",
351-
"The most important part is to specify the global index of the computation. This is the current index of the computed chromosomes. This serves as a loop function across all chromosomes."
351+
"The most important part is to specify the index of the computation. This is the current index of the computed chromosomes. This serves as a loop function across all chromosomes."
352352
]
353353
},
354354
{
@@ -365,10 +365,11 @@
365365
"outputs": [],
366366
"source": [
367367
"import numba_dpex\n",
368+
"from numba_dpex import kernel_api\n",
368369
"\n",
369370
"@numba_dpex.kernel\n",
370-
"def eval_genomes_sycl_kernel(chromosomes, fitnesses, chrom_length):\n",
371-
" pos = numba_dpex.get_global_id(0)\n",
371+
"def eval_genomes_sycl_kernel(item: kernel_api.Item, chromosomes, fitnesses, chrom_length):\n",
372+
" pos = item.get_id(0)\n",
372373
" num_loops = 3000\n",
373374
" for i in range(num_loops):\n",
374375
" fitnesses[pos] += chromosomes[pos*chrom_length + 1]\n",
@@ -409,8 +410,9 @@
409410
" chromosomes_flat = chromosomes.flatten()\n",
410411
" chromosomes_flat_dpctl = dpnp.asarray(chromosomes_flat, device=\"gpu\")\n",
411412
" fitnesses_dpctl = dpnp.asarray(fitnesses, device=\"gpu\")\n",
412-
"\n",
413-
" eval_genomes_sycl_kernel[numba_dpex.Range(pop_size)](chromosomes_flat_dpctl, fitnesses_dpctl, chrom_size)\n",
413+
" \n",
414+
" exec_range = kernel_api.Range(pop_size)\n",
415+
" numba_dpex.call_kernel(eval_genomes_sycl_kernel, exec_range, chromosomes_flat_dpctl, fitnesses_dpctl, chrom_size)\n",
414416
" fitnesses = dpnp.asnumpy(fitnesses_dpctl)\n",
415417
" chromosomes = next_generation(chromosomes, fitnesses)\n",
416418
" fitnesses = np.zeros(pop_size, dtype=np.float32)\n",
@@ -544,7 +546,7 @@
544546
"\n",
545547
"The evaluate created generation we are calculating the full distance of the given path (chromosome). In this example, the lower the fitness value is, the better the chromosome. That's different from the general GA that we implemented.\n",
546548
"\n",
547-
"As in this example we are also using numba-dpex, we are using a global index like before."
549+
"As in this example we are also using numba-dpex, we are using an index like before."
548550
]
549551
},
550552
{
@@ -554,8 +556,8 @@
554556
"outputs": [],
555557
"source": [
556558
"@numba_dpex.kernel\n",
557-
"def eval_genomes_plain_TSP_SYCL(chromosomes, fitnesses, distances, pop_length):\n",
558-
" pos = numba_dpex.get_global_id(0)\n",
559+
"def eval_genomes_plain_TSP_SYCL(item: kernel_api.Item, chromosomes, fitnesses, distances, pop_length):\n",
560+
" pos = item.get_id(1)\n",
559561
" for j in range(pop_length-1):\n",
560562
" fitnesses[pos] += distances[int(chromosomes[pos, j]), int(chromosomes[pos, j+1])]\n"
561563
]
@@ -708,7 +710,8 @@
708710
" chromosomes_flat_dpctl = dpnp.asarray(chromosomes, device=\"gpu\")\n",
709711
" fitnesses_dpctl = dpnp.asarray(fitnesses.copy(), device=\"gpu\")\n",
710712
"\n",
711-
" eval_genomes_plain_TSP_SYCL[numba_dpex.Range(pop_size)](chromosomes_flat_dpctl, fitnesses_dpctl, distances_dpctl, pop_size)\n",
713+
" exec_range = kernel_api.Range(pop_size)\n",
714+
" numba_dpex.call_kernel(eval_genomes_plain_TSP_SYCL, exec_range, chromosomes_flat_dpctl, fitnesses_dpctl, distances_dpctl, pop_size)\n",
712715
" fitnesses = dpnp.asnumpy(fitnesses_dpctl)\n",
713716
" chromosomes = next_generation_TSP(chromosomes, fitnesses)\n",
714717
" fitnesses = np.zeros(pop_size, dtype=np.float32)\n",

AI-and-Analytics/Features-and-Functionality/IntelPython_GPU_numba-dpex_Genetic_Algorithm/IntelPython_GPU_numba-dpex_Genetic_Algorithm.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,17 @@ def next_generation(chromosomes, fitnesses):
260260
#
261261
# The only par that differs form the standard implementation is the evaluation function.
262262
#
263-
# The most important part is to specify the global index of the computation. This is the current index of the computed chromosomes. This serves as a loop function across all chromosomes.
263+
# The most important part is to specify the index of the computation. This is the current index of the computed chromosomes. This serves as a loop function across all chromosomes.
264264

265265
# In[ ]:
266266

267267

268268
import numba_dpex
269+
from numba_dpex import kernel_api
269270

270271
@numba_dpex.kernel
271-
def eval_genomes_sycl_kernel(chromosomes, fitnesses, chrom_length):
272-
pos = numba_dpex.get_global_id(0)
272+
def eval_genomes_sycl_kernel(item: kernel_api.Item, chromosomes, fitnesses, chrom_length):
273+
pos = item.get_id(0)
273274
num_loops = 3000
274275
for i in range(num_loops):
275276
fitnesses[pos] += chromosomes[pos*chrom_length + 1]
@@ -300,7 +301,8 @@ def eval_genomes_sycl_kernel(chromosomes, fitnesses, chrom_length):
300301
chromosomes_flat_dpctl = dpnp.asarray(chromosomes_flat, device="gpu")
301302
fitnesses_dpctl = dpnp.asarray(fitnesses, device="gpu")
302303

303-
eval_genomes_sycl_kernel[numba_dpex.Range(pop_size)](chromosomes_flat_dpctl, fitnesses_dpctl, chrom_size)
304+
exec_range = kernel_api.Range(pop_size)
305+
numba_dpex.call_kernel(eval_genomes_sycl_kernel, exec_range, chromosomes_flat_dpctl, fitnesses_dpctl, chrom_size)
304306
fitnesses = dpnp.asnumpy(fitnesses_dpctl)
305307
chromosomes = next_generation(chromosomes, fitnesses)
306308
fitnesses = np.zeros(pop_size, dtype=np.float32)
@@ -398,14 +400,14 @@ def eval_genomes_sycl_kernel(chromosomes, fitnesses, chrom_length):
398400
#
399401
# The evaluate created generation we are calculating the full distance of the given path (chromosome). In this example, the lower the fitness value is, the better the chromosome. That's different from the general GA that we implemented.
400402
#
401-
# As in this example we are also using numba-dpex, we are using a global index like before.
403+
# As in this example we are also using numba-dpex, we are using an index like before.
402404

403405
# In[ ]:
404406

405407

406408
@numba_dpex.kernel
407-
def eval_genomes_plain_TSP_SYCL(chromosomes, fitnesses, distances, pop_length):
408-
pos = numba_dpex.get_global_id(0)
409+
def eval_genomes_plain_TSP_SYCL(item: kernel_api.Item, chromosomes, fitnesses, distances, pop_length):
410+
pos = item.get_id(1)
409411
for j in range(pop_length-1):
410412
fitnesses[pos] += distances[int(chromosomes[pos, j]), int(chromosomes[pos, j+1])]
411413

@@ -526,7 +528,8 @@ def next_generation_TSP(chromosomes, fitnesses):
526528
chromosomes_flat_dpctl = dpnp.asarray(chromosomes, device="gpu")
527529
fitnesses_dpctl = dpnp.asarray(fitnesses.copy(), device="gpu")
528530

529-
eval_genomes_plain_TSP_SYCL[numba_dpex.Range(pop_size)](chromosomes_flat_dpctl, fitnesses_dpctl, distances_dpctl, pop_size)
531+
exec_range = kernel_api.Range(pop_size)
532+
numba_dpex.call_kernel(eval_genomes_plain_TSP_SYCL, exec_range, chromosomes_flat_dpctl, fitnesses_dpctl, distances_dpctl, pop_size)
530533
fitnesses = dpnp.asnumpy(fitnesses_dpctl)
531534
chromosomes = next_generation_TSP(chromosomes, fitnesses)
532535
fitnesses = np.zeros(pop_size, dtype=np.float32)

AI-and-Analytics/Features-and-Functionality/IntelTensorFlow_Enabling_Auto_Mixed_Precision_for_TransferLearning/README.md

+65-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The `Enable Auto-Mixed Precision for Transfer Learning with TensorFlow*` sample guides you through the process of enabling auto-mixed precision to use low-precision datatypes, like bfloat16, for transfer learning with TensorFlow* (TF).
44

5-
The sample demonstrates the end-to-end pipeline tasks typically performed in a deep learning use-case: training (and retraining), inference optimization, and serving the model with TensorFlow Serving.
5+
The sample demonstrates the tasks typically performed in a deep learning use-case: training (and retraining), and inference optimization. The sample also includes tips and boilerplate code for serving the model with TensorFlow Serving.
66

77
| Area | Description
88
|:--- |:---
@@ -37,10 +37,6 @@ You will need to download and install the following toolkits, tools, and compone
3737

3838
Install using PIP: `$pip install notebook`. <br> Alternatively, see [*Installing Jupyter*](https://jupyter.org/install) for detailed installation instructions.
3939

40-
- **TensorFlow Serving**
41-
42-
See *TensorFlow Serving* [*Installation*](https://www.tensorflow.org/tfx/serving/setup) for detailed installation options.
43-
4440
- **Other dependencies**
4541

4642
Install using PIP and the `requirements.txt` file supplied with the sample: `$pip install -r requirements.txt --no-deps`. <br> The `requirements.txt` file contains the necessary dependencies to run the Notebook.
@@ -112,6 +108,70 @@ You will see diagrams comparing performance and analysis. This includes performa
112108
113109
For performance analysis, you will see histograms showing different Tensorflow* operations in the analyzed pre-trained model pb file.
114110
111+
## Serve the model with TensorFlow Serving
112+
113+
### Installation
114+
See *TensorFlow Serving* [*Installation*](https://www.tensorflow.org/tfx/serving/setup) for detailed installation options.
115+
116+
### Example Code
117+
118+
Create a copy of the optimized model in a well-defined directory hierarchy with a version number "1".
119+
120+
```
121+
!mkdir serving
122+
!cp -r models/my_optimized_model serving/1
123+
```
124+
125+
```
126+
os.environ["MODEL_DIR"] = os.getcwd() + "/serving"
127+
```
128+
129+
This is where we start running TensorFlow Serving and load our model. After it loads we can start making inference requests using REST. There are some important parameters:
130+
- **rest_api_port**: The port that you'll use for REST requests.
131+
- **model_name**: You'll use this in the URL of REST requests. It can be anything.
132+
- **model_base_path**: This is the path to the directory where you've saved your model.
133+
134+
```
135+
%%bash --bg
136+
nohup tensorflow_model_server --rest_api_port=8501 --model_name=rn50 --model_base_path=${MODEL_DIR} > server.log 2>&1
137+
```
138+
139+
#### Prepare the testing data for prediction
140+
141+
```
142+
for image_batch, labels_batch in val_ds:
143+
print(image_batch.shape)
144+
print(labels_batch.shape)
145+
break
146+
test_data, test_labels = image_batch.numpy(), labels_batch.numpy()
147+
```
148+
149+
#### Make REST requests
150+
151+
Now let's create the JSON object for a batch of three inference requests and we'll send a predict request as a POST to our server's REST endpoint, and pass it three examples.
152+
153+
```
154+
import json
155+
import matplotlib.pyplot as plt
156+
157+
def show(idx, title):
158+
plt.figure()
159+
plt.imshow(test_data[idx])
160+
plt.axis('off')
161+
plt.title('\n\n{}'.format(title), fontdict={'size': 16})
162+
163+
data = json.dumps({"signature_name": "serving_default", "instances": test_data[0:3].tolist()})
164+
print('Data: {} ... {}'.format(data[:50], data[len(data)-52:]))
165+
166+
headers = {"content-type": "application/json"}
167+
json_response = requests.post('http://localhost:8501/v1/models/rn50:predict', data=data, headers=headers)
168+
predictions = json.loads(json_response.text)['predictions']
169+
170+
for i in range(0,3):
171+
show(i, 'The model thought this was a {} (class {}), and it was actually a {} (class {})'.format(
172+
class_names[np.argmax(predictions[i])], np.argmax(predictions[i]), class_names[test_labels[i]], test_labels[i]))
173+
```
174+
115175
## License
116176
117177
Code samples are licensed under the MIT license. See

0 commit comments

Comments
 (0)