Skip to content

Commit 1adeab5

Browse files
committed
examples: purged tag and dt shortcuts
Some examples define 'tag' and 'df' shortcuts to improve code readability. This detail is misleading when looking at annotated examples code that only shows code snippets.
1 parent 1835a43 commit 1adeab5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+634
-461
lines changed

examples/bnorm_u8_via_binary_postops.cpp

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright 2020-2022 Intel Corporation
2+
* Copyright 2020-2025 Intel Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,9 +46,6 @@
4646

4747
using namespace dnnl;
4848

49-
using tag = memory::format_tag;
50-
using dt = memory::data_type;
51-
5249
void bnorm_u8_via_binary_postops(dnnl::engine::kind engine_kind) {
5350

5451
// Create execution dnnl::engine.
@@ -102,12 +99,18 @@ void bnorm_u8_via_binary_postops(dnnl::engine::kind engine_kind) {
10299
oscale_data.begin(), oscale_data.end(), []() { return 0.5f; });
103100

104101
// Create descriptors.
105-
auto src_md = memory::desc(src_dims, dt::u8, tag::nhwc);
106-
auto mean_md = memory::desc(params_dims, dt::f32, tag::nhwc);
107-
auto variance_md = memory::desc(params_dims, dt::f32, tag::nhwc);
108-
auto scale_md = memory::desc(params_dims, dt::f32, tag::nhwc);
109-
auto shift_md = memory::desc(params_dims, dt::f32, tag::nhwc);
110-
auto oscale_md = memory::desc(params_dims, dt::f32, tag::nhwc);
102+
auto src_md = memory::desc(
103+
src_dims, memory::data_type::u8, memory::format_tag::nhwc);
104+
auto mean_md = memory::desc(
105+
params_dims, memory::data_type::f32, memory::format_tag::nhwc);
106+
auto variance_md = memory::desc(
107+
params_dims, memory::data_type::f32, memory::format_tag::nhwc);
108+
auto scale_md = memory::desc(
109+
params_dims, memory::data_type::f32, memory::format_tag::nhwc);
110+
auto shift_md = memory::desc(
111+
params_dims, memory::data_type::f32, memory::format_tag::nhwc);
112+
auto oscale_md = memory::desc(
113+
params_dims, memory::data_type::f32, memory::format_tag::nhwc);
111114

112115
// Create src memory objects.
113116
auto src_mem = memory(src_md, engine);

examples/cnn_inference_f32.cpp

+121-62
Large diffs are not rendered by default.

examples/cnn_inference_int8.cpp

+27-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright 2018-2024 Intel Corporation
2+
* Copyright 2018-2025 Intel Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,9 +33,6 @@
3333
using namespace dnnl;
3434

3535
void simple_net_int8(engine::kind engine_kind) {
36-
using tag = memory::format_tag;
37-
using dt = memory::data_type;
38-
3936
auto eng = engine(engine_kind, 0);
4037
stream s(eng);
4138

@@ -89,12 +86,18 @@ void simple_net_int8(engine::kind engine_kind) {
8986
/// The user data will be in its original 32-bit floating point format.
9087
/// @snippet cnn_inference_int8.cpp Allocate buffers
9188
//[Allocate buffers]
92-
auto user_src_memory = memory({{conv_src_tz}, dt::f32, tag::nchw}, eng);
89+
auto user_src_memory = memory(
90+
{{conv_src_tz}, memory::data_type::f32, memory::format_tag::nchw},
91+
eng);
9392
write_to_dnnl_memory(user_src.data(), user_src_memory);
9493
auto user_weights_memory
95-
= memory({{conv_weights_tz}, dt::f32, tag::oihw}, eng);
94+
= memory({{conv_weights_tz}, memory::data_type::f32,
95+
memory::format_tag::oihw},
96+
eng);
9697
write_to_dnnl_memory(conv_weights.data(), user_weights_memory);
97-
auto user_bias_memory = memory({{conv_bias_tz}, dt::f32, tag::x}, eng);
98+
auto user_bias_memory = memory(
99+
{{conv_bias_tz}, memory::data_type::f32, memory::format_tag::x},
100+
eng);
98101
write_to_dnnl_memory(conv_bias.data(), user_bias_memory);
99102
//[Allocate buffers]
100103

@@ -112,10 +115,14 @@ void simple_net_int8(engine::kind engine_kind) {
112115
/// > Bias does not support quantization.
113116
/// @snippet cnn_inference_int8.cpp Create convolution memory descriptors
114117
//[Create convolution memory descriptors]
115-
auto conv_src_md = memory::desc({conv_src_tz}, dt::u8, tag::any);
116-
auto conv_bias_md = memory::desc({conv_bias_tz}, dt::f32, tag::any);
117-
auto conv_weights_md = memory::desc({conv_weights_tz}, dt::s8, tag::any);
118-
auto conv_dst_md = memory::desc({conv_dst_tz}, dt::u8, tag::any);
118+
auto conv_src_md = memory::desc(
119+
{conv_src_tz}, memory::data_type::u8, memory::format_tag::any);
120+
auto conv_bias_md = memory::desc(
121+
{conv_bias_tz}, memory::data_type::f32, memory::format_tag::any);
122+
auto conv_weights_md = memory::desc(
123+
{conv_weights_tz}, memory::data_type::s8, memory::format_tag::any);
124+
auto conv_dst_md = memory::desc(
125+
{conv_dst_tz}, memory::data_type::u8, memory::format_tag::any);
119126
//[Create convolution memory descriptors]
120127

121128
/// Configuring int8-specific parameters in an int8 primitive is done
@@ -129,7 +136,8 @@ void simple_net_int8(engine::kind engine_kind) {
129136
conv_attr.set_scales_mask(DNNL_ARG_DST, dst_mask);
130137

131138
// Prepare dst scales
132-
auto dst_scale_md = memory::desc({1}, dt::f32, tag::x);
139+
auto dst_scale_md
140+
= memory::desc({1}, memory::data_type::f32, memory::format_tag::x);
133141
auto dst_scale_memory = memory(dst_scale_md, eng);
134142
write_to_dnnl_memory(dst_scales.data(), dst_scale_memory);
135143
//[Configure scaling]
@@ -194,7 +202,8 @@ void simple_net_int8(engine::kind engine_kind) {
194202
auto conv_src_memory = memory(conv_prim_desc.src_desc(), eng);
195203
primitive_attr src_attr;
196204
src_attr.set_scales_mask(DNNL_ARG_DST, src_mask);
197-
auto src_scale_md = memory::desc({1}, dt::f32, tag::x);
205+
auto src_scale_md
206+
= memory::desc({1}, memory::data_type::f32, memory::format_tag::x);
198207
auto src_scale_memory = memory(src_scale_md, eng);
199208
write_to_dnnl_memory(src_scales.data(), src_scale_memory);
200209
auto src_reorder_pd
@@ -208,7 +217,8 @@ void simple_net_int8(engine::kind engine_kind) {
208217
auto conv_weights_memory = memory(conv_prim_desc.weights_desc(), eng);
209218
primitive_attr weight_attr;
210219
weight_attr.set_scales_mask(DNNL_ARG_DST, weight_mask);
211-
auto wei_scale_md = memory::desc({1}, dt::f32, tag::x);
220+
auto wei_scale_md
221+
= memory::desc({1}, memory::data_type::f32, memory::format_tag::x);
212222
auto wei_scale_memory = memory(wei_scale_md, eng);
213223
write_to_dnnl_memory(weight_scales.data(), wei_scale_memory);
214224
auto weight_reorder_pd
@@ -251,7 +261,9 @@ void simple_net_int8(engine::kind engine_kind) {
251261
/// computation output data.
252262
/// @snippet cnn_inference_int8.cpp Dequantize the result
253263
///[Dequantize the result]
254-
auto user_dst_memory = memory({{conv_dst_tz}, dt::f32, tag::nchw}, eng);
264+
auto user_dst_memory = memory(
265+
{{conv_dst_tz}, memory::data_type::f32, memory::format_tag::nchw},
266+
eng);
255267
write_to_dnnl_memory(user_dst.data(), user_dst_memory);
256268
primitive_attr dst_attr;
257269
dst_attr.set_scales_mask(DNNL_ARG_SRC, dst_mask);

examples/cnn_training_bf16.cpp

+48-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright 2019-2022 Intel Corporation
2+
* Copyright 2019-2025 Intel Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,9 +38,6 @@
3838
using namespace dnnl;
3939

4040
void simple_net(engine::kind engine_kind) {
41-
using tag = memory::format_tag;
42-
using dt = memory::data_type;
43-
4441
auto eng = engine(engine_kind, 0);
4542
stream s(eng);
4643

@@ -79,27 +76,36 @@ void simple_net(engine::kind engine_kind) {
7976
conv_bias[i] = sinf((float)i);
8077

8178
// create memory for user data
82-
auto conv_user_src_memory
83-
= memory({{conv_src_tz}, dt::f32, tag::nchw}, eng);
79+
auto conv_user_src_memory = memory(
80+
{{conv_src_tz}, memory::data_type::f32, memory::format_tag::nchw},
81+
eng);
8482
write_to_dnnl_memory(net_src.data(), conv_user_src_memory);
8583

8684
auto conv_user_weights_memory
87-
= memory({{conv_weights_tz}, dt::f32, tag::oihw}, eng);
85+
= memory({{conv_weights_tz}, memory::data_type::f32,
86+
memory::format_tag::oihw},
87+
eng);
8888
write_to_dnnl_memory(conv_weights.data(), conv_user_weights_memory);
8989

90-
auto conv_user_bias_memory = memory({{conv_bias_tz}, dt::f32, tag::x}, eng);
90+
auto conv_user_bias_memory = memory(
91+
{{conv_bias_tz}, memory::data_type::f32, memory::format_tag::x},
92+
eng);
9193
write_to_dnnl_memory(conv_bias.data(), conv_user_bias_memory);
9294

9395
// create memory descriptors for bfloat16 convolution data w/ no specified
9496
// format tag(`any`)
9597
// tag `any` lets a primitive(convolution in this case)
9698
// chose the memory format preferred for best performance.
97-
auto conv_src_md = memory::desc({conv_src_tz}, dt::bf16, tag::any);
98-
auto conv_weights_md = memory::desc({conv_weights_tz}, dt::bf16, tag::any);
99-
auto conv_dst_md = memory::desc({conv_dst_tz}, dt::bf16, tag::any);
99+
auto conv_src_md = memory::desc(
100+
{conv_src_tz}, memory::data_type::bf16, memory::format_tag::any);
101+
auto conv_weights_md = memory::desc({conv_weights_tz},
102+
memory::data_type::bf16, memory::format_tag::any);
103+
auto conv_dst_md = memory::desc(
104+
{conv_dst_tz}, memory::data_type::bf16, memory::format_tag::any);
100105
// here bias data type is set to bf16.
101106
// additionally, f32 data type is supported for bf16 convolution.
102-
auto conv_bias_md = memory::desc({conv_bias_tz}, dt::bf16, tag::any);
107+
auto conv_bias_md = memory::desc(
108+
{conv_bias_tz}, memory::data_type::bf16, memory::format_tag::any);
103109

104110
// create a convolution primitive descriptor
105111

@@ -225,11 +231,13 @@ void simple_net(engine::kind engine_kind) {
225231
memory::dims pool_padding = {0, 0};
226232

227233
// create memory for pool dst data in user format
228-
auto pool_user_dst_memory
229-
= memory({{pool_dst_tz}, dt::f32, tag::nchw}, eng);
234+
auto pool_user_dst_memory = memory(
235+
{{pool_dst_tz}, memory::data_type::f32, memory::format_tag::nchw},
236+
eng);
230237

231238
// create pool dst memory descriptor in format any for bfloat16 data type
232-
auto pool_dst_md = memory::desc({pool_dst_tz}, dt::bf16, tag::any);
239+
auto pool_dst_md = memory::desc(
240+
{pool_dst_tz}, memory::data_type::bf16, memory::format_tag::any);
233241

234242
// create a pooling primitive descriptor
235243
auto pool_pd = pooling_forward::primitive_desc(eng, prop_kind::forward,
@@ -269,14 +277,17 @@ void simple_net(engine::kind engine_kind) {
269277
net_diff_dst[i] = sinf((float)i);
270278

271279
// create memory for user diff dst data stored in float data type
272-
auto pool_user_diff_dst_memory
273-
= memory({{pool_dst_tz}, dt::f32, tag::nchw}, eng);
280+
auto pool_user_diff_dst_memory = memory(
281+
{{pool_dst_tz}, memory::data_type::f32, memory::format_tag::nchw},
282+
eng);
274283
write_to_dnnl_memory(net_diff_dst.data(), pool_user_diff_dst_memory);
275284

276285
// Backward pooling
277286
// create memory descriptors for pooling
278-
auto pool_diff_src_md = memory::desc({lrn_data_tz}, dt::bf16, tag::any);
279-
auto pool_diff_dst_md = memory::desc({pool_dst_tz}, dt::bf16, tag::any);
287+
auto pool_diff_src_md = memory::desc(
288+
{lrn_data_tz}, memory::data_type::bf16, memory::format_tag::any);
289+
auto pool_diff_dst_md = memory::desc(
290+
{pool_dst_tz}, memory::data_type::bf16, memory::format_tag::any);
280291

281292
// backward primitive descriptor needs to hint forward descriptor
282293
auto pool_bwd_pd = pooling_backward::primitive_desc(eng,
@@ -305,7 +316,8 @@ void simple_net(engine::kind engine_kind) {
305316
{DNNL_ARG_WORKSPACE, pool_workspace_memory}});
306317

307318
// Backward lrn
308-
auto lrn_diff_dst_md = memory::desc({lrn_data_tz}, dt::bf16, tag::any);
319+
auto lrn_diff_dst_md = memory::desc(
320+
{lrn_data_tz}, memory::data_type::bf16, memory::format_tag::any);
309321
const auto &lrn_diff_src_md = lrn_diff_dst_md;
310322

311323
// create backward lrn primitive descriptor
@@ -335,8 +347,10 @@ void simple_net(engine::kind engine_kind) {
335347
{DNNL_ARG_WORKSPACE, lrn_workspace_memory}});
336348

337349
// Backward relu
338-
auto relu_diff_src_md = memory::desc({relu_data_tz}, dt::bf16, tag::any);
339-
auto relu_diff_dst_md = memory::desc({relu_data_tz}, dt::bf16, tag::any);
350+
auto relu_diff_src_md = memory::desc(
351+
{relu_data_tz}, memory::data_type::bf16, memory::format_tag::any);
352+
auto relu_diff_dst_md = memory::desc(
353+
{relu_data_tz}, memory::data_type::bf16, memory::format_tag::any);
340354
auto relu_src_md = conv_pd.dst_desc();
341355

342356
// create backward relu primitive_descriptor
@@ -367,14 +381,20 @@ void simple_net(engine::kind engine_kind) {
367381
// create user format diff weights and diff bias memory for float data type
368382

369383
auto conv_user_diff_weights_memory
370-
= memory({{conv_weights_tz}, dt::f32, tag::nchw}, eng);
371-
auto conv_diff_bias_memory = memory({{conv_bias_tz}, dt::f32, tag::x}, eng);
384+
= memory({{conv_weights_tz}, memory::data_type::f32,
385+
memory::format_tag::nchw},
386+
eng);
387+
auto conv_diff_bias_memory = memory(
388+
{{conv_bias_tz}, memory::data_type::f32, memory::format_tag::x},
389+
eng);
372390

373391
// create memory descriptors for bfloat16 convolution data
374-
auto conv_bwd_src_md = memory::desc({conv_src_tz}, dt::bf16, tag::any);
375-
auto conv_diff_weights_md
376-
= memory::desc({conv_weights_tz}, dt::bf16, tag::any);
377-
auto conv_diff_dst_md = memory::desc({conv_dst_tz}, dt::bf16, tag::any);
392+
auto conv_bwd_src_md = memory::desc(
393+
{conv_src_tz}, memory::data_type::bf16, memory::format_tag::any);
394+
auto conv_diff_weights_md = memory::desc({conv_weights_tz},
395+
memory::data_type::bf16, memory::format_tag::any);
396+
auto conv_diff_dst_md = memory::desc(
397+
{conv_dst_tz}, memory::data_type::bf16, memory::format_tag::any);
378398

379399
// use diff bias provided by the user
380400
auto conv_diff_bias_md = conv_diff_bias_memory.get_desc();

0 commit comments

Comments
 (0)