Skip to content

Commit fac0bd8

Browse files
committed
Roll back to use python script.
- BF16 without Intel® AMX example is executed from a python script. The bf16_noAmx_training_time variables is saved in a txt file. - Use torch.amp.autocast('cpu') instead of torch.cpu.amp.autocast() Signed-off-by: jafraustro <[email protected]>
1 parent 5b8d629 commit fac0bd8

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

AI-and-Analytics/Features-and-Functionality/IntelPyTorch_TrainingOptimizations_AMX_BF16/IntelPyTorch_TrainingOptimizations_AMX_BF16.ipynb

+17-21
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
"## Training the Model\n",
119119
"The function trainModel() will train the Resnet50 model based on the whether Intel® AMX should be enabled, and whether to use FP32 or BF16 data type. The environment variable `ONEDNN_MAX_CPU_ISA` is used to enable or disable Intel® AMX. **Note that this environment variable is only initialized once.** This means to run with Intel® AMX and VNNI, there will need to be separate processes. The best practice is to set this environment variable before running your script. For more information, refer to the [oneDNN documentation on CPU Dispatcher Control](https://www.intel.com/content/www/us/en/develop/documentation/onednn-developer-guide-and-reference/top/performance-profiling-and-inspection/cpu-dispatcher-control.html). \n",
120120
"\n",
121-
"To use BF16 in operations, use the `torch.cpu.amp.autocast()` function to perform forward and backward propagation."
121+
"To use BF16 in operations, use the `torch.amp.autocast('cpu')` function to perform forward and backward propagation."
122122
]
123123
},
124124
{
@@ -128,7 +128,7 @@
128128
"metadata": {},
129129
"outputs": [],
130130
"source": [
131-
"os.environ[\"ONEDNN_MAX_CPU_ISA\"] = \"AVX512_CORE_BF16\""
131+
"os.environ[\"ONEDNN_MAX_CPU_ISA\"] = \"AVX512_CORE_AMX\""
132132
]
133133
},
134134
{
@@ -171,7 +171,7 @@
171171
" for batch_idx, (data, target) in enumerate(train_loader):\n",
172172
" optimizer.zero_grad()\n",
173173
" if \"bf16\" == dataType:\n",
174-
" with torch.cpu.amp.autocast(): # Auto Mixed Precision\n",
174+
" with torch.amp.autocast('cpu'): # Auto Mixed Precision\n",
175175
" # Setting memory_format to torch.channels_last could improve performance with 4D input data. This is optional.\n",
176176
" data = data.to(memory_format=torch.channels_last)\n",
177177
" output = model(data)\n",
@@ -240,8 +240,8 @@
240240
"## Training with FP32 and BF16, including Intel® AMX\n",
241241
"Train the Resnet50 model in three different cases:\n",
242242
"1. FP32 (baseline) \n",
243-
"2. BF16 without Intel® AMX \n",
244-
"3. BF16 with Intel® AMX \n",
243+
"2. BF16 with Intel® AMX\n",
244+
"x. BF16 without Intel® AMX\n",
245245
"\n",
246246
"The training time is recorded."
247247
]
@@ -254,32 +254,33 @@
254254
"outputs": [],
255255
"source": [
256256
"print(\"Training model with FP32\")\n",
257-
"os.environ[\"ONEDNN_MAX_CPU_ISA\"] = \"AVX512_CORE_AMX\"\n",
258257
"fp32_training_time = trainModel(train_loader, modelName=\"fp32\", dataType=\"fp32\")"
259258
]
260259
},
261260
{
262261
"cell_type": "code",
263262
"execution_count": null,
264-
"id": "a9bd6dec",
263+
"id": "3faaf5de",
265264
"metadata": {},
266265
"outputs": [],
267266
"source": [
268-
"print(\"Training model with BF16 with AVX512\")\n",
269-
"os.environ[\"ONEDNN_MAX_CPU_ISA\"] = \"AVX512_CORE_BF16\"\n",
270-
"bf16_avx512_training_time = trainModel(train_loader, modelName=\"bf16_noAmx\", dataType=\"bf16\")"
267+
"print(\"Training model with BF16 with Intel® AMX\")\n",
268+
"bf16_amx_training_time = trainModel(train_loader, modelName=\"bf16_withAmx\", dataType=\"bf16\")"
271269
]
272270
},
273271
{
274272
"cell_type": "code",
275273
"execution_count": null,
276-
"id": "2fdc8a70-509a-4714-8524-084f34e287c3",
274+
"id": "a9bd6dec",
277275
"metadata": {},
278276
"outputs": [],
279277
"source": [
280-
"print(\"Training model with BF16 with Intel® AMX\")\n",
281-
"os.environ[\"ONEDNN_MAX_CPU_ISA\"] = \"AVX512_CORE_AMX\"\n",
282-
"bf16_amx_training_time = trainModel(train_loader, modelName=\"bf16_withAmx\", dataType=\"bf16\")"
278+
"print(\"Training model with BF16 with AVX512\")\n",
279+
"!python pytorch_training_avx512_bf16.py\n",
280+
"\n",
281+
"# Read the variable\n",
282+
"with open('bf16_avx512_training_time.txt', 'r') as f:\n",
283+
" bf16_avx512_training_time = float(f.read().strip())"
283284
]
284285
},
285286
{
@@ -383,9 +384,9 @@
383384
],
384385
"metadata": {
385386
"kernelspec": {
386-
"display_name": "pytorch",
387+
"display_name": "pytorch_test",
387388
"language": "python",
388-
"name": "pytorch"
389+
"name": "pytorch_test"
389390
},
390391
"language_info": {
391392
"codemirror_mode": {
@@ -398,11 +399,6 @@
398399
"nbconvert_exporter": "python",
399400
"pygments_lexer": "ipython3",
400401
"version": "3.11.0"
401-
},
402-
"vscode": {
403-
"interpreter": {
404-
"hash": "ed6ae0d06e7bec0fef5f1fb38f177ceea45508ce95c68ed2f49461dd6a888a39"
405-
}
406402
}
407403
},
408404
"nbformat": 4,

AI-and-Analytics/Features-and-Functionality/IntelPyTorch_TrainingOptimizations_AMX_BF16/pytorch_training_avx512_bf16.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def trainModel(train_loader, modelName="myModel", dataType="fp32"):
5656
for batch_idx, (data, target) in enumerate(train_loader):
5757
optimizer.zero_grad()
5858
if "bf16" == dataType:
59-
with torch.cpu.amp.autocast(): # Auto Mixed Precision
59+
with torch.amp.autocast('cpu'): # Auto Mixed Precision
6060
# Setting memory_format to torch.channels_last could improve performance with 4D input data. This is optional.
6161
data = data.to(memory_format=torch.channels_last)
6262
output = model(data)
@@ -106,7 +106,11 @@ def main():
106106

107107
# Train models and acquire training times
108108
print("Training model with BF16 with AVX512")
109-
bf16_noAmx_training_time = trainModel(train_loader, modelName="bf16_noAmx", dataType="bf16")
109+
bf16_avx512_training_time = trainModel(train_loader, modelName="bf16_noAmx", dataType="bf16")
110+
111+
# Save variable
112+
with open('bf16_noAmx_training_time.txt', 'w') as f:
113+
f.write(str(bf16_avx512_training_time))
110114

111115
if __name__ == '__main__':
112116
main()

0 commit comments

Comments
 (0)