forked from oneapi-src/oneDNN
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprimitive_hashing.hpp
637 lines (599 loc) · 25.7 KB
/
primitive_hashing.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*******************************************************************************
* Copyright 2019 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#ifndef PRIMITIVE_HASHING_HPP
#define PRIMITIVE_HASHING_HPP
#include <typeindex>
#include "c_types_map.hpp"
#include "dnnl.h"
#include "type_helpers.hpp"
namespace dnnl {
namespace impl {
namespace primitive_hashing {
struct key_t {
key_t(const primitive_desc_t *pd, int impl_nthr);
bool operator==(const key_t &rhs) const;
dnnl_primitive_kind_t primitive_kind_;
const op_desc_t *op_desc_;
const primitive_attr_t *attr_;
std::type_index impl_id_;
int impl_nthr_;
std::vector<memory_desc_t> mds;
private:
template <typename T>
bool cast_and_compare(const op_desc_t *lhs, const op_desc_t *rhs) const {
return *(reinterpret_cast<const T *>(lhs))
== *(reinterpret_cast<const T *>(rhs));
}
void init_mds(const primitive_desc_t *pd);
};
// The following code is derived from Boost C++ library
// Copyright 2005-2014 Daniel James.
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
template <typename T>
static size_t hash_combine(size_t seed, const T &v) {
return seed ^= std::hash<T> {}(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template <typename T>
static size_t get_array_hash(size_t seed, const T *v, int size) {
for (int i = 0; i < size; i++) {
seed = hash_combine(seed, v[i]);
}
return seed;
}
static inline size_t get_md_hash(const memory_desc_t &md);
// Specialization for an array of mds
template <>
size_t get_array_hash<memory_desc_t>(
size_t seed, const memory_desc_t *v, int size) {
for (int i = 0; i < size; i++) {
seed = hash_combine(seed, get_md_hash(v[i]));
}
return seed;
}
// Combine hash of each primitive_attr_t data member
static inline size_t get_attr_hash(const primitive_attr_t *attr) {
size_t seed = 0;
// scratchpad_mode
seed = hash_combine(seed, static_cast<size_t>(attr->scratchpad_mode_));
// output_scales: mask
seed = hash_combine(seed, attr->output_scales_.mask_);
// output_scales: scales[:]
if (attr->output_scales_.scales_) {
for (int i = 0; i < attr->output_scales_.count_; i++) {
seed = hash_combine(seed, attr->output_scales_.scales_[i]);
}
}
// post_ops: entry[:]
for (int i = 0; i < attr->post_ops_.len_; i++) {
const auto &entry = attr->post_ops_.entry_[i];
switch (entry.kind) {
case primitive_kind::eltwise:
seed = hash_combine(
seed, static_cast<size_t>(entry.eltwise.alg));
seed = hash_combine(seed, entry.eltwise.scale);
seed = hash_combine(seed, entry.eltwise.alpha);
seed = hash_combine(seed, entry.eltwise.beta);
break;
case primitive_kind::sum:
seed = hash_combine(seed, entry.sum.scale);
break;
default: assert(!"unknown post_op");
}
}
// rnn_data_qparams: scale, shift
seed = hash_combine(seed, attr->rnn_data_qparams_.scale_);
seed = hash_combine(seed, attr->rnn_data_qparams_.shift_);
// rnn_weights_qparams: mask
seed = hash_combine(seed, attr->rnn_weights_qparams_.mask_);
// rnn_weights_qparams_: scales[:]
if (attr->rnn_weights_qparams_.scales_) {
for (int i = 0; i < attr->rnn_weights_qparams_.count_; i++) {
seed = hash_combine(seed, attr->rnn_weights_qparams_.scales_[i]);
}
}
// Combined hash for attributes
return seed;
}
// Combine hash of each memory_desc_t data member
static inline size_t get_md_hash(const memory_desc_t &md) {
size_t seed = 0;
seed = get_array_hash(seed, md.dims, DNNL_MAX_NDIMS);
seed = hash_combine(seed, static_cast<size_t>(md.data_type));
seed = get_array_hash(seed, md.padded_dims, DNNL_MAX_NDIMS);
seed = get_array_hash(seed, md.padded_offsets, DNNL_MAX_NDIMS);
seed = hash_combine(seed, md.offset0);
seed = hash_combine(seed, static_cast<size_t>(md.format_kind));
// format desc
switch (md.format_kind) {
case format_kind::undef:
case format_kind::any: break;
case format_kind::blocked:
seed = get_array_hash(
seed, md.format_desc.blocking.strides, DNNL_MAX_NDIMS);
seed = hash_combine(seed, md.format_desc.blocking.inner_nblks);
seed = get_array_hash(
seed, md.format_desc.blocking.inner_blks, DNNL_MAX_NDIMS);
seed = get_array_hash(
seed, md.format_desc.blocking.inner_idxs, DNNL_MAX_NDIMS);
break;
case format_kind::wino:
seed = hash_combine(seed,
static_cast<size_t>(md.format_desc.wino_desc.wino_format));
seed = hash_combine(seed, md.format_desc.wino_desc.r);
seed = hash_combine(seed, md.format_desc.wino_desc.alpha);
seed = hash_combine(seed, md.format_desc.wino_desc.ic);
seed = hash_combine(seed, md.format_desc.wino_desc.oc);
seed = hash_combine(seed, md.format_desc.wino_desc.ic_block);
seed = hash_combine(seed, md.format_desc.wino_desc.oc_block);
seed = hash_combine(seed, md.format_desc.wino_desc.ic2_block);
seed = hash_combine(seed, md.format_desc.wino_desc.oc2_block);
seed = hash_combine(seed, md.format_desc.wino_desc.adj_scale);
seed = hash_combine(seed, md.format_desc.wino_desc.size);
break;
case format_kind::rnn_packed:
seed = hash_combine(seed,
static_cast<size_t>(md.format_desc.rnn_packed_desc.format));
seed = hash_combine(seed, md.format_desc.rnn_packed_desc.n_parts);
seed = hash_combine(seed, md.format_desc.rnn_packed_desc.n);
seed = hash_combine(seed, md.format_desc.rnn_packed_desc.ldb);
seed = get_array_hash(seed, md.format_desc.rnn_packed_desc.parts,
DNNL_RNN_MAX_N_PARTS);
seed = get_array_hash(seed,
md.format_desc.rnn_packed_desc.part_pack_size,
DNNL_RNN_MAX_N_PARTS);
seed = get_array_hash(seed,
md.format_desc.rnn_packed_desc.pack_part,
DNNL_RNN_MAX_N_PARTS);
seed = hash_combine(
seed, md.format_desc.rnn_packed_desc.offset_compensation);
seed = hash_combine(seed, md.format_desc.rnn_packed_desc.size);
break;
default: assert(!"unknown format_kind");
}
if (md.extra.flags != dnnl_memory_extra_flag_none) {
seed = hash_combine(seed, md.extra.flags);
if (md.extra.flags
& (dnnl_memory_extra_flag_compensation_conv_s8s8
| dnnl_memory_extra_flag_gpu_rnn_u8s8_compensation)) {
seed = hash_combine(seed, md.extra.compensation_mask);
}
if (md.extra.flags & dnnl_memory_extra_flag_scale_adjust) {
seed = hash_combine(seed, md.extra.scale_adjust);
}
}
// Combined hash for a memory descriptor
return seed;
}
// Functions that compute hash for different op_descs
template <typename T>
static size_t get_desc_hash(const op_desc_t *op_desc);
template <>
size_t get_desc_hash<concat_desc_t>(const op_desc_t *op_desc) {
const auto *desc = reinterpret_cast<const concat_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
// Memory descriptors
seed = hash_combine(seed, get_md_hash(desc->dst_md));
// N
seed = hash_combine(seed, desc->n);
// Concat dimension
seed = hash_combine(seed, desc->concat_dimension);
// Array of mds
seed = get_array_hash(seed, desc->src_mds.data(), desc->n);
// Combined hash for concat desc
return seed;
}
template <>
size_t get_desc_hash<batch_normalization_desc_t>(const op_desc_t *op_desc) {
const auto *desc
= reinterpret_cast<const batch_normalization_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->prop_kind));
// Memory descriptors
seed = hash_combine(seed, get_md_hash(desc->data_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_data_desc));
seed = hash_combine(seed, get_md_hash(desc->data_scaleshift_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_data_scaleshift_desc));
seed = hash_combine(seed, get_md_hash(desc->stat_desc));
// Epsilon
seed = hash_combine(seed, desc->batch_norm_epsilon);
// Flags
seed = hash_combine(seed, desc->flags);
// Combined hash for batch normalization desc
return seed;
}
template <>
size_t get_desc_hash<binary_desc_t>(const op_desc_t *op_desc) {
const auto *desc = reinterpret_cast<const binary_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->alg_kind));
// Memory descriptors
seed = hash_combine(seed, get_md_hash(desc->src_desc[0]));
seed = hash_combine(seed, get_md_hash(desc->src_desc[1]));
seed = hash_combine(seed, get_md_hash(desc->dst_desc));
// Combined hash for binary op desc
return seed;
}
// (De-)Convolution
template <>
size_t get_desc_hash<convolution_desc_t>(const op_desc_t *op_desc) {
const auto *desc = reinterpret_cast<const convolution_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->prop_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->alg_kind));
// Memory descriptors
seed = hash_combine(seed, get_md_hash(desc->src_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_src_desc));
seed = hash_combine(seed, get_md_hash(desc->weights_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_weights_desc));
seed = hash_combine(seed, get_md_hash(desc->bias_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_bias_desc));
seed = hash_combine(seed, get_md_hash(desc->dst_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_dst_desc));
// Strides, dilates, padding
seed = get_array_hash(seed, desc->strides, DNNL_MAX_NDIMS);
seed = get_array_hash(seed, desc->dilates, DNNL_MAX_NDIMS);
seed = get_array_hash(seed, desc->padding[0], DNNL_MAX_NDIMS);
seed = get_array_hash(seed, desc->padding[1], DNNL_MAX_NDIMS);
// Accumulator type
seed = hash_combine(seed, static_cast<size_t>(desc->accum_data_type));
// Combined hash for (de-)convolution desc
return seed;
}
// Eltwise
template <>
size_t get_desc_hash<eltwise_desc_t>(const op_desc_t *op_desc) {
const auto *desc = reinterpret_cast<const eltwise_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->prop_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->alg_kind));
// Memory descriptors
seed = hash_combine(seed, get_md_hash(desc->data_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_data_desc));
// Alpha, beta
seed = hash_combine(seed, desc->alpha);
seed = hash_combine(seed, desc->beta);
// Combined hash for eltwise desc
return seed;
}
template <>
size_t get_desc_hash<gemm_desc_t>(const op_desc_t *op_desc) {
const auto *desc = reinterpret_cast<const gemm_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
// Trans
seed = hash_combine(seed, static_cast<size_t>(desc->transa));
seed = hash_combine(seed, static_cast<size_t>(desc->transb));
// M, N, K
seed = hash_combine(seed, desc->m);
seed = hash_combine(seed, desc->n);
seed = hash_combine(seed, desc->k);
// LDA, LDB, LDC
seed = hash_combine(seed, desc->lda);
seed = hash_combine(seed, desc->ldb);
seed = hash_combine(seed, desc->ldc);
// Alpha, beta
seed = hash_combine(seed, desc->alpha);
seed = hash_combine(seed, desc->beta);
// a_type, b_type, c_type, acc_type
seed = hash_combine(seed, static_cast<size_t>(desc->a_type));
seed = hash_combine(seed, static_cast<size_t>(desc->b_type));
seed = hash_combine(seed, static_cast<size_t>(desc->c_type));
seed = hash_combine(seed, static_cast<size_t>(desc->acc_type));
// Combined hash for gemm desc
return seed;
}
template <>
size_t get_desc_hash<inner_product_desc_t>(const op_desc_t *op_desc) {
const auto *desc = reinterpret_cast<const inner_product_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->prop_kind));
// Memory descriptors
seed = hash_combine(seed, get_md_hash(desc->src_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_src_desc));
seed = hash_combine(seed, get_md_hash(desc->weights_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_weights_desc));
seed = hash_combine(seed, get_md_hash(desc->bias_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_bias_desc));
seed = hash_combine(seed, get_md_hash(desc->dst_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_dst_desc));
// Accumulator type
seed = hash_combine(seed, static_cast<size_t>(desc->accum_data_type));
// Combined hash for inner_product desc
return seed;
}
// Layer normalization
template <>
size_t get_desc_hash<layer_normalization_desc_t>(const op_desc_t *op_desc) {
const auto *desc
= reinterpret_cast<const layer_normalization_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->prop_kind));
// Memory descriptors
seed = hash_combine(seed, get_md_hash(desc->data_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_data_desc));
seed = hash_combine(seed, get_md_hash(desc->data_scaleshift_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_data_scaleshift_desc));
seed = hash_combine(seed, get_md_hash(desc->stat_desc));
// Epsilon
seed = hash_combine(seed, desc->layer_norm_epsilon);
// Flags
seed = hash_combine(seed, desc->flags);
// Combined hash for layer_normalization desc
return seed;
}
template <>
size_t get_desc_hash<lrn_desc_t>(const op_desc_t *op_desc) {
const auto *desc = reinterpret_cast<const lrn_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->prop_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->alg_kind));
// Memory descriptors
seed = hash_combine(seed, get_md_hash(desc->data_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_data_desc));
// Local size
seed = hash_combine(seed, desc->local_size);
// Alpha, beta
seed = hash_combine(seed, desc->lrn_alpha);
seed = hash_combine(seed, desc->lrn_beta);
// k
seed = hash_combine(seed, desc->lrn_k);
// Combined hash for lrn desc
return seed;
}
template <>
size_t get_desc_hash<pooling_desc_t>(const op_desc_t *op_desc) {
const auto *desc = reinterpret_cast<const pooling_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->prop_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->alg_kind));
// Memory descriptors
seed = hash_combine(seed, get_md_hash(desc->src_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_src_desc));
seed = hash_combine(seed, get_md_hash(desc->dst_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_dst_desc));
// Strides, dilates, padding
seed = get_array_hash(seed, desc->strides, DNNL_MAX_NDIMS);
seed = get_array_hash(seed, desc->kernel, DNNL_MAX_NDIMS);
seed = get_array_hash(seed, desc->padding[0], DNNL_MAX_NDIMS);
seed = get_array_hash(seed, desc->padding[1], DNNL_MAX_NDIMS);
// Accumulator type
seed = hash_combine(seed, static_cast<size_t>(desc->accum_data_type));
// Combined hash for pooling desc
return seed;
}
template <>
size_t get_desc_hash<reorder_desc_t>(const op_desc_t *op_desc) {
const auto *desc = reinterpret_cast<const reorder_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
// Memory descriptors
seed = hash_combine(seed, get_md_hash(desc->src_md));
seed = hash_combine(seed, get_md_hash(desc->dst_md));
// Kinds of source and destination engines
seed = hash_combine(seed, static_cast<size_t>(desc->src_engine_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->dst_engine_kind));
// Combined hash for reorder desc
return seed;
}
template <>
size_t get_desc_hash<rnn_desc_t>(const op_desc_t *op_desc) {
const auto *desc = reinterpret_cast<const rnn_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->prop_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->cell_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->direction));
// Memory descriptors
seed = hash_combine(seed, get_md_hash(desc->src_layer_desc));
seed = hash_combine(seed, get_md_hash(desc->src_iter_desc));
seed = hash_combine(seed, get_md_hash(desc->src_iter_c_desc));
seed = hash_combine(seed, get_md_hash(desc->weights_layer_desc));
seed = hash_combine(seed, get_md_hash(desc->weights_iter_desc));
seed = hash_combine(seed, get_md_hash(desc->bias_desc));
seed = hash_combine(seed, get_md_hash(desc->dst_layer_desc));
seed = hash_combine(seed, get_md_hash(desc->dst_iter_desc));
seed = hash_combine(seed, get_md_hash(desc->dst_iter_c_desc));
seed = hash_combine(seed, get_md_hash(desc->placeholder_desc));
seed = hash_combine(seed, get_md_hash(desc->placeholder2_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_src_layer_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_src_iter_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_src_iter_c_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_weights_layer_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_weights_iter_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_bias_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_dst_layer_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_dst_iter_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_dst_iter_c_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_placeholder_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_placeholder2_desc));
// Flags
seed = hash_combine(seed, desc->flags);
// Activation kind
seed = hash_combine(seed, static_cast<size_t>(desc->activation_kind));
// Alpha, beta
seed = hash_combine(seed, desc->alpha);
seed = hash_combine(seed, desc->beta);
// Combined hash for rnn desc
return seed;
}
// Shuffle
template <>
size_t get_desc_hash<shuffle_desc_t>(const op_desc_t *op_desc) {
const auto *desc = reinterpret_cast<const shuffle_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->prop_kind));
// Memory descriptors
seed = hash_combine(seed, get_md_hash(desc->data_desc));
// Axis
seed = hash_combine(seed, desc->axis);
// Groupe size
seed = hash_combine(seed, desc->group_size);
// Combined hash for shuffle desc
return seed;
}
template <>
size_t get_desc_hash<softmax_desc_t>(const op_desc_t *op_desc) {
const auto *desc = reinterpret_cast<const softmax_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
seed = hash_combine(seed, static_cast<size_t>(desc->prop_kind));
// Memory descriptors
seed = hash_combine(seed, get_md_hash(desc->data_desc));
seed = hash_combine(seed, get_md_hash(desc->diff_desc));
// Axis
seed = hash_combine(seed, desc->softmax_axis);
// Combined hash for softmax desc
return seed;
}
template <>
size_t get_desc_hash<sum_desc_t>(const op_desc_t *op_desc) {
const auto *desc = reinterpret_cast<const sum_desc_t *>(op_desc);
size_t seed = 0;
// Kinds
seed = hash_combine(seed, static_cast<size_t>(desc->primitive_kind));
// Memory descriptors
seed = hash_combine(seed, get_md_hash(desc->dst_md));
// N
seed = hash_combine(seed, desc->n);
// Scales
if (!desc->scales.empty()) {
seed = get_array_hash(seed, desc->scales.data(), desc->n);
}
// Array of mds
seed = get_array_hash(seed, desc->src_mds.data(), desc->n);
// Combined hash for sum desc
return seed;
}
} // namespace primitive_hashing
} // namespace impl
} // namespace dnnl
// inject a specialization of std::hash for key_t in std namespace
namespace std {
template <>
struct hash<dnnl::impl::primitive_hashing::key_t> {
using argument_type = dnnl::impl::primitive_hashing::key_t;
using result_type = std::size_t;
result_type operator()(const argument_type &key) const {
using namespace dnnl::impl;
using namespace dnnl::impl::primitive_hashing;
size_t seed = 0;
// Compute hash for primitive_kind_, attr_, impl_id_ and impl_nthr_
seed = hash_combine(seed,
hash_combine(0, static_cast<size_t>(key.primitive_kind_)));
seed = hash_combine(seed, get_attr_hash(key.attr_));
seed = hash_combine(seed, hash_combine(0, key.impl_id_));
seed = hash_combine(seed, hash_combine(0, key.impl_nthr_));
// Combine hash for op_desc with the computed hash
switch (key.primitive_kind_) {
case primitive_kind::batch_normalization:
seed = hash_combine(seed,
get_desc_hash<batch_normalization_desc_t>(
key.op_desc_));
break;
case primitive_kind::binary:
seed = hash_combine(
seed, get_desc_hash<binary_desc_t>(key.op_desc_));
break;
case primitive_kind::concat:
seed = hash_combine(
seed, get_desc_hash<concat_desc_t>(key.op_desc_));
break;
case primitive_kind::convolution:
seed = hash_combine(
seed, get_desc_hash<convolution_desc_t>(key.op_desc_));
break;
case primitive_kind::deconvolution:
seed = hash_combine(seed,
get_desc_hash<deconvolution_desc_t>(key.op_desc_));
break;
case primitive_kind::eltwise:
seed = hash_combine(
seed, get_desc_hash<eltwise_desc_t>(key.op_desc_));
break;
case primitive_kind::gemm:
seed = hash_combine(
seed, get_desc_hash<gemm_desc_t>(key.op_desc_));
break;
case primitive_kind::inner_product:
seed = hash_combine(seed,
get_desc_hash<inner_product_desc_t>(key.op_desc_));
break;
case primitive_kind::layer_normalization:
seed = hash_combine(seed,
get_desc_hash<layer_normalization_desc_t>(
key.op_desc_));
break;
case primitive_kind::lrn:
seed = hash_combine(
seed, get_desc_hash<lrn_desc_t>(key.op_desc_));
break;
case primitive_kind::pooling:
seed = hash_combine(
seed, get_desc_hash<pooling_desc_t>(key.op_desc_));
break;
case primitive_kind::reorder:
seed = hash_combine(
seed, get_desc_hash<reorder_desc_t>(key.op_desc_));
break;
case primitive_kind::rnn:
seed = hash_combine(
seed, get_desc_hash<rnn_desc_t>(key.op_desc_));
break;
case primitive_kind::shuffle:
seed = hash_combine(
seed, get_desc_hash<shuffle_desc_t>(key.op_desc_));
break;
case primitive_kind::softmax:
seed = hash_combine(
seed, get_desc_hash<softmax_desc_t>(key.op_desc_));
break;
case primitive_kind::sum:
seed = hash_combine(
seed, get_desc_hash<sum_desc_t>(key.op_desc_));
break;
default: assert(!"unknown primitive_kind");
}
seed = get_array_hash(seed, key.mds.data(), (int)key.mds.size());
return seed;
}
};
} // namespace std
#endif