forked from oneapi-src/oneDNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkldnn.hpp
3337 lines (2971 loc) · 134 KB
/
mkldnn.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright 2016-2018 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 MKLDNN_HPP
#define MKLDNN_HPP
#ifndef DOXYGEN_SHOULD_SKIP_THIS
#include <stdlib.h>
#include <memory>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include "mkldnn.h"
#endif
namespace mkldnn {
/// @addtogroup cpp_api C++ API
/// @{
/// @addtogroup cpp_api_utils Utils
/// @{
/// A class that provides the destructor for an Intel(R) MKL-DNN C handle
template <typename T> class handle_traits {};
/// A class for wrapping an Intel(R) MKL-DNN handle. It is used as the base
/// class for primitive (#mkldnn_primitive_t), engine (#mkldnn_engine_t), and
/// stream (#mkldnn_stream_t) handles. An object of the #mkldnn::handle class
/// can be passed by value. This class enables wrapping:
/// - Newly constructed handles.
/// @n In this case, the constructed handle uses reference counting provided
/// by @p std::shared_ptr with a proper deleter function specified through
/// the @p handle_traits class.
/// - Pre-existing handles returned by the Intel(R) MKL-DNN C API (for
/// example, through #mkldnn_primitive_get_output()).
/// @n In this case, an Intel(R) MKL-DNN C API handle is wrapped without a
/// deleter because it is assumed that the handle wrapper for the original
/// object deletes the handle (this model is similar to @p std::weak_ptr).
template <typename T, typename traits=handle_traits<T>> class handle {
private:
std::shared_ptr<typename std::remove_pointer<T>::type> _data;
handle(const handle &&) = delete;
handle &operator=(const handle &&other) = delete;
protected:
bool operator==(const T other) const { return other == _data.get(); }
bool operator!=(const T other) const { return !(*this == other); }
public:
/// Constructs a C handle wrapper.
/// @param t The C handle to wrap.
/// @param weak A flag to specify whether to construct a weak wrapper.
handle(T t = 0, bool weak = false): _data(0) {
reset(t, weak);
}
handle(const handle &other): _data(other._data) {}
handle &operator=(const handle &other) {
_data = other._data;
return *this;
}
/// Resets the value of a C handle.
/// @param t The new value of the C handle.
/// @param weak A flag to specify whether the wrapper should be weak.
void reset(T t, bool weak = false) {
auto dummy_destructor = [](T) { return decltype(traits::destructor(0))(0); };
_data.reset(t, weak ? dummy_destructor : traits::destructor);
}
/// Returns the value of the underlying C handle.
T get() const { return _data.get(); }
bool operator==(const handle &other) const { return other._data.get() == _data.get(); }
bool operator!=(const handle &other) const { return !(*this == other); }
};
#ifndef DOXYGEN_SHOULD_SKIP_THIS
template <> struct handle_traits<mkldnn_primitive_desc_t> {
static constexpr auto destructor = &mkldnn_primitive_desc_destroy;
};
template <> struct handle_traits<mkldnn_primitive_t> {
static constexpr auto destructor = &mkldnn_primitive_destroy;
};
template <> struct handle_traits<mkldnn_primitive_desc_iterator_t> {
static constexpr auto destructor = &mkldnn_primitive_desc_iterator_destroy;
};
#endif
/// Base class for all computational primitives.
class primitive: public handle<mkldnn_primitive_t> {
friend struct error;
friend struct stream;
friend class primitive_at;
using handle::handle;
public:
/// A proxy to C primitive kind enum
enum class kind {
undefined_primitive = mkldnn_undefined_primitive,
memory = mkldnn_memory,
view = mkldnn_view,
reorder = mkldnn_reorder,
concat = mkldnn_concat,
concat_inplace = mkldnn_concat_inplace,
sum = mkldnn_sum,
convolution = mkldnn_convolution,
deconvolution = mkldnn_deconvolution,
shuffle = mkldnn_shuffle,
eltwise = mkldnn_eltwise,
softmax = mkldnn_softmax,
pooling = mkldnn_pooling,
lrn = mkldnn_lrn,
batch_normalization = mkldnn_batch_normalization,
inner_product = mkldnn_inner_product,
rnn = mkldnn_rnn,
};
/// A wrapper structure to specify a particular output of a primitive.
struct at {
/// The underlying C API structure.
mkldnn_primitive_at_t data;
/// Constructs a wrapper specifying @p aprimitive output with index @p
/// at.
///
/// @param aprimitive The target primitive.
/// @param at The output index.
at(const primitive &aprimitive, size_t at = 0)
: data(mkldnn_primitive_at(aprimitive.get(), at)) {}
/// Returns the specified output.
inline operator primitive() const;
};
/// Returns the descriptor of the underlying C API primitive
inline const_mkldnn_primitive_desc_t get_primitive_desc() const;
// TODO: use the C++ API wrapper structure.
};
inline mkldnn_primitive_kind_t convert_to_c(primitive::kind akind) {
return static_cast<mkldnn_primitive_kind_t>(akind);
}
/// Intel(R) MKL-DNN exception class.
///
/// This class captures the status returned by the failed C API function, error
/// message, and, optionally, handle of the primitive that caused the error.
struct error: public std::exception {
mkldnn_status_t status;
std::string message;
primitive error_primitive;
/// Constructs an error instance.
///
/// @param astatus The error status returned by the C API.
/// @param amessage The error message.
/// @param aerror_primitive (optional) A C handle of the primitive that
/// caused the error.
error(mkldnn_status_t astatus, std::string amessage,
mkldnn_primitive_t aerror_primitive = 0)
: status(astatus)
, message(amessage)
, error_primitive(aerror_primitive, true)
{}
/// A convenience function for wrapping calls to the C API. Checks the
/// return status and throws an #error in case of failure.
///
/// @param status The error status returned by the C API.
/// @param message The error message.
/// @param error_primitive (optional) A C handle of the primitive that
/// caused the error.
static void wrap_c_api(mkldnn_status_t status,
const std::string &message,
mkldnn_primitive_t *error_primitive = 0)
{
if (status != mkldnn_success) {
if (nullptr != error_primitive)
throw error(status, message, *error_primitive);
else
throw error(status, message, nullptr);
}
}
};
inline primitive::at::operator primitive() const {
const_mkldnn_primitive_t output;
error::wrap_c_api(
mkldnn_primitive_get_output(data.primitive,
data.output_index, &output),
"could not get an output primitive");
return primitive(const_cast<mkldnn_primitive_t>(output), true);
}
const_mkldnn_primitive_desc_t primitive::get_primitive_desc() const {
const_mkldnn_primitive_desc_t pd;
error::wrap_c_api(mkldnn_primitive_get_primitive_desc(get(), &pd),
"could not get primitive descriptor by primitive");
return pd;
}
/// @}
/// @addtogroup cpp_api_enums Common data types and enumerations
/// A proxy to @ref c_api_types in @ref c_api.
///
/// @{
enum round_mode {
round_nearest = mkldnn_round_nearest,
round_down = mkldnn_round_down,
};
inline mkldnn_round_mode_t convert_to_c(round_mode mode) {
return static_cast<mkldnn_round_mode_t>(mode);
}
enum padding_kind {
zero = mkldnn_padding_zero
};
inline mkldnn_padding_kind_t convert_to_c(padding_kind kind) {
return static_cast<mkldnn_padding_kind_t>(kind);
}
enum prop_kind {
forward_training = mkldnn_forward_training,
forward_scoring = mkldnn_forward_scoring,
forward_inference = mkldnn_forward_inference,
forward = mkldnn_forward,
backward = mkldnn_backward,
backward_data = mkldnn_backward_data,
backward_weights = mkldnn_backward_weights,
backward_bias = mkldnn_backward_bias
};
inline mkldnn_prop_kind_t convert_to_c(prop_kind kind) {
return static_cast<mkldnn_prop_kind_t>(kind);
}
enum algorithm {
algorithm_undef = mkldnn_alg_kind_undef,
convolution_direct = mkldnn_convolution_direct,
convolution_winograd = mkldnn_convolution_winograd,
deconvolution_direct = mkldnn_deconvolution_direct,
deconvolution_winograd = mkldnn_deconvolution_winograd,
eltwise_relu = mkldnn_eltwise_relu,
eltwise_tanh = mkldnn_eltwise_tanh,
eltwise_elu = mkldnn_eltwise_elu,
eltwise_square = mkldnn_eltwise_square,
eltwise_abs = mkldnn_eltwise_abs,
eltwise_sqrt = mkldnn_eltwise_sqrt,
eltwise_linear = mkldnn_eltwise_linear,
eltwise_bounded_relu = mkldnn_eltwise_bounded_relu,
eltwise_soft_relu = mkldnn_eltwise_soft_relu,
eltwise_logistic = mkldnn_eltwise_logistic,
lrn_across_channels = mkldnn_lrn_across_channels,
lrn_within_channel = mkldnn_lrn_within_channel,
pooling_max = mkldnn_pooling_max,
pooling_avg = mkldnn_pooling_avg,
pooling_avg_include_padding = mkldnn_pooling_avg_include_padding,
pooling_avg_exclude_padding = mkldnn_pooling_avg_exclude_padding,
vanilla_rnn = mkldnn_vanilla_rnn,
vanilla_lstm = mkldnn_vanilla_lstm,
vanilla_gru = mkldnn_vanilla_gru,
gru_linear_before_reset = mkldnn_gru_linear_before_reset
};
inline mkldnn_alg_kind_t convert_to_c(algorithm aalgorithm) {
return static_cast<mkldnn_alg_kind_t>(aalgorithm);
}
enum batch_normalization_flag {
use_global_stats = mkldnn_use_global_stats,
use_scale_shift = mkldnn_use_scaleshift,
fuse_bn_relu = mkldnn_fuse_bn_relu
};
inline mkldnn_batch_normalization_flag_t convert_to_c(
batch_normalization_flag aflag) {
return static_cast<mkldnn_batch_normalization_flag_t>(aflag);
}
enum rnn_direction {
unidirectional_left2right = mkldnn_unidirectional_left2right,
unidirectional_right2left = mkldnn_unidirectional_right2left,
unidirectional = mkldnn_unidirectional,
bidirectional_concat = mkldnn_bidirectional_concat,
bidirectional_sum = mkldnn_bidirectional_sum,
};
inline mkldnn_rnn_direction_t convert_to_c(rnn_direction adir) {
return static_cast<mkldnn_rnn_direction_t>(adir);
}
enum query {
undef = mkldnn_query_undef,
eengine = mkldnn_query_engine,
primitive_kind = mkldnn_query_primitive_kind,
num_of_inputs_s32 = mkldnn_query_num_of_inputs_s32,
num_of_outputs_s32 = mkldnn_query_num_of_outputs_s32,
time_estimate_f64 = mkldnn_query_time_estimate_f64,
memory_consumption_s64 = mkldnn_query_memory_consumption_s64,
impl_info_str = mkldnn_query_impl_info_str,
op_d = mkldnn_query_op_d,
memory_d = mkldnn_query_memory_d,
convolution_d = mkldnn_query_convolution_d,
deconvolution_d = mkldnn_query_deconvolution_d,
shuffle_d = mkldnn_query_shuffle_d,
eltwise_d = mkldnn_query_eltwise_d,
softmax_d = mkldnn_query_softmax_d,
pooling_d = mkldnn_query_pooling_d,
lrn_d = mkldnn_query_lrn_d,
batch_normalization_d = mkldnn_query_batch_normalization_d,
inner_product_d = mkldnn_query_inner_product_d,
rnn_d = mkldnn_query_rnn_d,
input_pd = mkldnn_query_input_pd,
output_pd = mkldnn_query_output_pd,
src_pd = mkldnn_query_src_pd,
diff_src_pd = mkldnn_query_diff_src_pd,
weights_pd = mkldnn_query_weights_pd,
diff_weights_pd = mkldnn_query_diff_weights_pd,
dst_pd = mkldnn_query_dst_pd,
diff_dst_pd = mkldnn_query_diff_dst_pd,
workspace_pd = mkldnn_query_workspace_pd,
};
inline mkldnn_query_t convert_to_c(query aquery) {
return static_cast<mkldnn_query_t>(aquery);
}
/// @}
/// @addtogroup cpp_api_attr Attributes
/// An extension for controlling primitive behavior.
///
/// @sa @ref c_api_attributes in @ref c_api
/// @{
#ifndef DOXYGEN_SHOULD_SKIP_THIS
template <> struct handle_traits<mkldnn_post_ops_t> {
static constexpr auto destructor = &mkldnn_post_ops_destroy;
};
#endif
struct post_ops: public handle<mkldnn_post_ops_t> {
post_ops() {
mkldnn_post_ops_t result;
error::wrap_c_api(mkldnn_post_ops_create(&result),
"could not create post operation sequence");
reset(result);
}
int len() const { return mkldnn_post_ops_len(get()); }
primitive::kind kind(int index) const {
error::wrap_c_api(
index < len() ? mkldnn_success : mkldnn_invalid_arguments,
"post_ops index is out of range");
return static_cast<primitive::kind>(mkldnn_post_ops_get_kind(get(),
index));
}
void append_sum(float scale = 1.) {
error::wrap_c_api(mkldnn_post_ops_append_sum(get(), scale),
"could not append sum");
}
void get_params_sum(int index, float &scale) const {
error::wrap_c_api(mkldnn_post_ops_get_params_sum(get(), index, &scale),
"could not get sum params");
}
void append_eltwise(float scale, algorithm alg, float alpha,
float beta) {
error::wrap_c_api(mkldnn_post_ops_append_eltwise(get(), scale,
convert_to_c(alg), alpha, beta),
"could not append eltwise");
}
void get_params_eltwise(int index, float &scale, algorithm &alg,
float &alpha, float &beta) const {
mkldnn_alg_kind_t c_alg;
error::wrap_c_api(mkldnn_post_ops_get_params_eltwise(get(), index,
&scale, &c_alg, &alpha, &beta),
"could not get eltwise params");
alg = static_cast<algorithm>(c_alg);
}
};
#ifndef DOXYGEN_SHOULD_SKIP_THIS
template <> struct handle_traits<mkldnn_primitive_attr_t> {
static constexpr auto destructor = &mkldnn_primitive_attr_destroy;
};
#endif
struct primitive_attr: public handle<mkldnn_primitive_attr_t> {
primitive_attr() {
mkldnn_primitive_attr_t result;
error::wrap_c_api(mkldnn_primitive_attr_create(&result),
"could not create a primitive attr");
reset(result);
}
round_mode get_int_output_round_mode() const {
mkldnn_round_mode_t result;
error::wrap_c_api(mkldnn_primitive_attr_get_int_output_round_mode(
get(), &result), "could not get int output round mode");
return round_mode(result);
}
void set_int_output_round_mode(round_mode mode) {
error::wrap_c_api(mkldnn_primitive_attr_set_int_output_round_mode(
get(), mkldnn::convert_to_c(mode)),
"could not set int output round mode");
}
void get_output_scales(int &mask, std::vector<float> &scales) const
{
int count, c_mask;
const float *c_scales;
error::wrap_c_api(mkldnn_primitive_attr_get_output_scales(get(),
&count, &c_mask, &c_scales),
"could not get int output scales");
scales.resize(count);
mask = c_mask;
for (int c = 0; c < count; ++c)
scales[c] = c_scales[c];
}
void set_output_scales(int mask, const std::vector<float> &scales)
{
error::wrap_c_api(mkldnn_primitive_attr_set_output_scales(get(),
(int)scales.size(), mask, &scales[0]),
"could not set int output scales");
}
const post_ops get_post_ops() const {
post_ops result;
const_mkldnn_post_ops_t c_result;
error::wrap_c_api(mkldnn_primitive_attr_get_post_ops(get(), &c_result),
"could not get post operation sequence");
result.reset(const_cast<mkldnn_post_ops_t>(c_result), true);
return result;
}
void set_post_ops(post_ops ops) {
error::wrap_c_api(mkldnn_primitive_attr_set_post_ops(get(), ops.get()),
"could not set post operation sequence");
}
};
/// @}
/// @addtogroup cpp_api_engine Engine
/// Engine operations
///
/// @sa @ref c_api_engine in @ref c_api
/// @{
#ifndef DOXYGEN_SHOULD_SKIP_THIS
template <> struct handle_traits<mkldnn_engine_t> {
static constexpr auto destructor = &mkldnn_engine_destroy;
};
#endif
/// An execution engine.
struct engine: public handle<mkldnn_engine_t> {
friend class primitive;
// gcc bug??? using handle::handle;
/// Kinds of engines
enum kind {
/// An unspecified engine
any = mkldnn_any_engine,
/// CPU engine
cpu = mkldnn_cpu,
};
/// Returns the number of engines of a certain kind.
///
/// @param akind The kind of engines to count.
static size_t get_count(kind akind) {
return mkldnn_engine_get_count(convert_to_c(akind));
}
/// Constructs an engine.
///
/// @param akind The kind of engine to construct.
/// @param index The index of the engine. Must be less than the value
/// returned by #get_count() for this particular kind of engine.
engine(kind akind, size_t index) {
mkldnn_engine_t aengine;
error::wrap_c_api(
mkldnn_engine_create(&aengine,
convert_to_c(akind), index),
"could not create an engine");
reset(aengine);
}
explicit engine(const mkldnn_engine_t& aengine)
: handle(aengine, true) {}
engine(const handle<mkldnn_primitive_desc_t> &pd) {
mkldnn_engine_t engine_q;
error::wrap_c_api(
mkldnn_primitive_desc_query(pd.get(),
mkldnn::convert_to_c(eengine), 0, &engine_q),
"could not get engine from primitive_desc");
reset(engine_q, true);
}
template <class primitive_desc>
static engine query(const primitive_desc &pd) {
mkldnn_engine_t engine_q;
error::wrap_c_api(
mkldnn_primitive_desc_query(pd.get(),
mkldnn::convert_to_c(eengine), 0, &engine_q),
"could not get engine from primitive_desc");
return engine(engine_q);
}
private:
static mkldnn_engine_kind_t convert_to_c(kind akind) {
return static_cast<mkldnn_engine_kind_t>(akind);
}
};
/// @}
/// @addtogroup cpp_api_memory_related Memory and memory related operations
/// @{
/// @addtogroup cpp_api_memory Memory
/// A primitive to describe and store data.
///
/// For more information please refer to @ref c_api_memory in @ref c_api
/// @{
/// Memory primitive that describes the data.
struct memory: public primitive {
private:
std::shared_ptr<char> _handle;
public:
typedef std::vector<std::remove_extent<mkldnn_dims_t>::type> dims;
template <typename T> static void validate_dims(std::vector<T> v) {
if (v.size() > TENSOR_MAX_DIMS)
throw error(mkldnn_invalid_arguments,
"invalid dimensions");
}
/// Data type specification. See #mkldnn_data_type_t for a detailed
/// description.
enum data_type {
data_undef = mkldnn_data_type_undef,
f32 = mkldnn_f32,
s32 = mkldnn_s32,
s16 = mkldnn_s16,
s8 = mkldnn_s8,
u8 = mkldnn_u8,
};
/// Memory format specification. See #mkldnn_memory_format_t
/// for a detailed description.
enum format {
format_undef = mkldnn_format_undef,
any = mkldnn_any,
blocked = mkldnn_blocked,
x = mkldnn_x,
nc = mkldnn_nc,
ncw = mkldnn_ncw,
nwc = mkldnn_nwc,
nCw16c = mkldnn_nCw16c,
nchw = mkldnn_nchw,
nhwc = mkldnn_nhwc,
chwn = mkldnn_chwn,
nCw8c = mkldnn_nCw8c,
nChw8c = mkldnn_nChw8c,
nChw16c = mkldnn_nChw16c,
ncdhw = mkldnn_ncdhw,
ndhwc = mkldnn_ndhwc,
nCdhw8c = mkldnn_nCdhw8c,
nCdhw16c = mkldnn_nCdhw16c,
oi = mkldnn_oi,
io = mkldnn_io,
oiw = mkldnn_oiw,
wio = mkldnn_wio,
Owi8o = mkldnn_Owi8o,
OIw8o8i = mkldnn_OIw8o8i,
OIw8i8o = mkldnn_OIw8i8o,
OIw16i16o = mkldnn_OIw16i16o,
OIw16o16i = mkldnn_OIw16o16i,
Oiw16o = mkldnn_Oiw16o,
Owi16o = mkldnn_Owi16o,
OIw8i16o2i = mkldnn_OIw8i16o2i,
OIw8o16i2o = mkldnn_OIw8o16i2o,
IOw16o16i = mkldnn_IOw16o16i,
oihw = mkldnn_oihw,
ihwo = mkldnn_ihwo,
hwio = mkldnn_hwio,
iohw = mkldnn_iohw,
hwio_s8s8 = mkldnn_hwio_s8s8,
dhwio = mkldnn_dhwio,
oidhw = mkldnn_oidhw,
OIdhw8i8o = mkldnn_OIdhw8i8o,
OIdhw8o8i = mkldnn_OIdhw8o8i,
Odhwi8o = mkldnn_Odhwi8o,
OIdhw16i16o = mkldnn_OIdhw16i16o,
OIdhw16o16i = mkldnn_OIdhw16o16i,
Oidhw16o = mkldnn_Oidhw16o,
Odhwi16o = mkldnn_Odhwi16o,
oIhw8i = mkldnn_oIhw8i,
oIhw16i = mkldnn_oIhw16i,
oIdhw8i = mkldnn_oIdhw8i,
oIdhw16i = mkldnn_oIdhw16i,
OIhw8i8o = mkldnn_OIhw8i8o,
OIhw16i16o = mkldnn_OIhw16i16o,
OIhw8o8i = mkldnn_OIhw8o8i,
OIhw16o16i = mkldnn_OIhw16o16i,
IOhw16o16i = mkldnn_IOhw16o16i,
OIhw8i16o2i = mkldnn_OIhw8i16o2i,
OIdhw8i16o2i = mkldnn_OIdhw8i16o2i,
OIhw8o16i2o = mkldnn_OIhw8o16i2o,
OIhw4i16o4i = mkldnn_OIhw4i16o4i,
OIhw4i16o4i_s8s8 = mkldnn_OIhw4i16o4i_s8s8,
Oihw8o = mkldnn_Oihw8o,
Oihw16o = mkldnn_Oihw16o,
Ohwi8o = mkldnn_Ohwi8o,
Ohwi16o = mkldnn_Ohwi16o,
OhIw16o4i = mkldnn_OhIw16o4i,
goiw = mkldnn_goiw,
gOwi8o = mkldnn_gOwi8o,
gOIw8o8i = mkldnn_gOIw8o8i,
gOIw8i8o = mkldnn_gOIw8i8o,
gOIw16i16o = mkldnn_gOIw16i16o,
gOIw16o16i = mkldnn_gOIw16o16i,
gOiw16o = mkldnn_gOiw16o,
gOwi16o = mkldnn_gOwi16o,
gOIw8i16o2i = mkldnn_gOIw8i16o2i,
gIOw16o16i = mkldnn_gIOw16o16i,
gOIw8o16i2o = mkldnn_gOIw8o16i2o,
goihw = mkldnn_goihw,
hwigo = mkldnn_hwigo,
giohw = mkldnn_giohw,
hwigo_s8s8 = mkldnn_hwigo_s8s8,
gOIdhw8i8o = mkldnn_gOIdhw8i8o,
gOIdhw8o8i = mkldnn_gOIdhw8o8i,
gOdhwi8o = mkldnn_gOdhwi8o,
gOIhw8i8o = mkldnn_gOIhw8i8o,
gOIhw16i16o = mkldnn_gOIhw16i16o,
gOIhw8i16o2i = mkldnn_gOIhw8i16o2i,
gOIdhw8i16o2i = mkldnn_gOIdhw8i16o2i,
gOIhw8o16i2o = mkldnn_gOIhw8o16i2o,
gOIhw4i16o4i = mkldnn_gOIhw4i16o4i,
gOIhw4i16o4i_s8s8 = mkldnn_gOIhw4i16o4i_s8s8,
gOihw8o = mkldnn_gOihw8o,
gOihw16o = mkldnn_gOihw16o,
gOhwi8o = mkldnn_gOhwi8o,
gOhwi16o = mkldnn_gOhwi16o,
Goihw8g = mkldnn_Goihw8g,
Goihw16g = mkldnn_Goihw16g,
gOIhw8o8i = mkldnn_gOIhw8o8i,
gOIhw16o16i = mkldnn_gOIhw16o16i,
gIOhw16o16i = mkldnn_gIOhw16o16i,
gOhIw16o4i = mkldnn_gOhIw16o4i,
goidhw = mkldnn_goidhw,
gOIdhw16i16o = mkldnn_gOIdhw16i16o,
gOIdhw16o16i = mkldnn_gOIdhw16o16i,
gOidhw16o = mkldnn_gOidhw16o,
gOdhwi16o = mkldnn_gOdhwi16o,
ntc = mkldnn_ntc,
tnc = mkldnn_tnc,
ldsnc = mkldnn_ldsnc,
ldigo = mkldnn_ldigo,
ldigo_p = mkldnn_ldigo_p,
ldgoi = mkldnn_ldgoi,
ldgoi_p = mkldnn_ldgoi_p,
ldgo = mkldnn_ldgo,
wino_fmt = mkldnn_wino_fmt,
format_last = mkldnn_format_last,
};
/// A memory descriptor.
struct desc {
friend struct memory;
/// The underlying C API data structure.
mkldnn_memory_desc_t data;
/// Constructs a memory descriptor.
///
/// @param adims Data dimensions
/// @param adata_type Data precision/type.
/// @param aformat Data layout format.
desc(dims adims, data_type adata_type,
format aformat) {
validate_dims(adims);
error::wrap_c_api(
mkldnn_memory_desc_init(&data, (int)adims.size(),
adims.size() == 0 ? nullptr : &adims[0],
convert_to_c(adata_type), convert_to_c(aformat)),
"could not initialize a memory descriptor");
}
/// Constructs a memory descriptor from a C API data structure.
///
/// @param adata A C API #mkldnn_memory_desc_t structure.
desc(const mkldnn_memory_desc_t &adata): data(adata) {}
};
/// A memory primitive descriptor.
struct primitive_desc : public handle<mkldnn_primitive_desc_t> {
friend struct memory;
// TODO: make private
primitive_desc() {}
/// Constructs a memory primitive descriptor.
primitive_desc(const desc &adesc, const engine &aengine) {
mkldnn_primitive_desc_t result;
error::wrap_c_api(
mkldnn_memory_primitive_desc_create(&result,
&adesc.data, aengine.get()),
"could not initialize a memory primitive descriptor");
reset(result);
}
/// Returns the memory primitive descriptor.
memory::desc desc() {
auto memory_d = mkldnn_primitive_desc_query_memory_d(get());
return memory::desc(*memory_d); }
/// Returns the number of bytes required to allocate the memory described
/// including the padding area.
size_t get_size() const {
return mkldnn_memory_primitive_desc_get_size(get());
}
bool operator==(const primitive_desc &other) const {
return (0 == mkldnn_memory_primitive_desc_equal(get(),
other.get())) ? false : true;
}
bool operator!=(const primitive_desc &other) const {
return !operator==(other);
}
engine get_engine() { return engine::query(*this); }
};
/// Constructs a memory primitive from a generic primitive.
///
/// @param aprimitive The primitive to treat as memory.
memory(const primitive &aprimitive): primitive(aprimitive) {}
/// Constructs a memory primitive.
///
/// @param adesc Memory primitive descriptor.
memory(const primitive_desc &adesc) {
mkldnn_primitive_t result;
error::wrap_c_api(
mkldnn_primitive_create(&result, adesc.get(), nullptr, nullptr),
"could not create a memory primitive");
reset(result);
auto _malloc = [](size_t size, int alignment) {
void *ptr;
#ifdef _WIN32
ptr = _aligned_malloc(size, alignment);
int rc = ((ptr)? 0 : errno);
#else
int rc = ::posix_memalign(&ptr, alignment, size);
#endif /* _WIN32 */
return (rc == 0) ? (char*)ptr : nullptr;
};
auto _free = [](char* p) {
#ifdef _WIN32
_aligned_free((void*)p);
#else
::free((void*)p);
#endif /* _WIN32 */
};
_handle.reset(_malloc(adesc.get_size(), 4096), _free);
set_data_handle(_handle.get());
}
memory(const primitive_desc &adesc, void *ahandle) {
mkldnn_primitive_t result;
error::wrap_c_api(
mkldnn_primitive_create(&result, adesc.get(), nullptr, nullptr),
"could not create a memory primitive");
reset(result);
set_data_handle(ahandle);
}
/// Returns the descriptor of the memory primitive.
primitive_desc get_primitive_desc() const {
primitive_desc adesc;
const_mkldnn_primitive_desc_t cdesc;
error::wrap_c_api(mkldnn_primitive_get_primitive_desc(get(),
&cdesc),
"could not get primitive descriptor from a memory primitive");
/* FIXME: no const_cast should be here */
adesc.reset(const_cast<mkldnn_primitive_desc_t>(cdesc), true);
return adesc;
}
/// Returns a handle of the data contained in the memory primitive. On
/// the CPU engine, this is a pointer to the allocated memory.
inline void *get_data_handle() const {
void *handle;
error::wrap_c_api(mkldnn_memory_get_data_handle(get(), &handle),
"could not get native handle");
return handle;
}
inline void set_data_handle(void *handle) const {
error::wrap_c_api(mkldnn_memory_set_data_handle(get(), handle),
"could not set native handle");
}
// Must go away or be private:
static mkldnn_data_type_t convert_to_c(data_type adata_type) {
return static_cast<mkldnn_data_type_t>(adata_type);
}
static mkldnn_memory_format_t convert_to_c(format aformat) {
return static_cast<mkldnn_memory_format_t>(aformat);
}
};
inline memory::desc zero_md() {
auto zero = mkldnn_memory_desc_t();
zero.primitive_kind = mkldnn_memory;
return memory::desc(zero);
}
inline memory null_memory(engine eng) {
mkldnn::memory::desc zero = zero_md();
return memory({zero, eng}, nullptr);
}
inline void check_num_parameters(const const_mkldnn_primitive_desc_t
&aprimitive_desc, int n_inputs, int n_outputs,
const std::string &prim_name) {
const int n_inputs_expected = mkldnn_primitive_desc_query_s32(
aprimitive_desc, mkldnn_query_num_of_inputs_s32, 0);
const int n_outputs_expected = mkldnn_primitive_desc_query_s32(
aprimitive_desc, mkldnn_query_num_of_outputs_s32, 0);
if (n_outputs_expected > n_outputs ) {
std::string message = "could not create " + prim_name +
" primitive, not enought output parameters";
throw error(mkldnn_invalid_arguments, message, nullptr);
}
if (n_inputs_expected > n_inputs ) {
std::string message = "could not create " + prim_name +
" primitive, not enought input parameters";
throw error(mkldnn_invalid_arguments, message, nullptr);
}
}
inline bool is_null_memory(const const_mkldnn_primitive_t &aprimitive) {
const_mkldnn_primitive_desc_t aprimitive_pd;
mkldnn_primitive_get_primitive_desc(aprimitive, &aprimitive_pd);
const mkldnn_memory_desc_t *aprimitive_md = mkldnn_primitive_desc_query_memory_d(
aprimitive_pd);
return ((aprimitive_md != nullptr) && (aprimitive_md->ndims == 0));
}
inline bool operator==(mkldnn_data_type_t a, memory::data_type b) {
return a == memory::convert_to_c(b);
}
inline bool operator!=(mkldnn_data_type_t a, memory::data_type b) {
return !(a == b);
}
inline bool operator==(memory::data_type a, mkldnn_data_type_t b) {
return b == a;
}
inline bool operator!=(memory::data_type a, mkldnn_data_type_t b) {
return !(a == b);
}
inline bool operator==(mkldnn_memory_format_t a, memory::format b) {
return a == memory::convert_to_c(b);
}
inline bool operator!=(mkldnn_memory_format_t a, memory::format b) {
return !(a == b);
}
inline bool operator==(memory::format a, mkldnn_memory_format_t b) {
return b == a;
}
inline bool operator!=(memory::format a, mkldnn_memory_format_t b) {
return !(a == b);
}
/// @}
/// @addtogroup cpp_api_reorder Reorder
/// A primitive to copy data between memory formats.
///
/// @sa @ref c_api_reorder in @ref c_api
/// @{
struct reorder : public primitive {
struct primitive_desc : public handle<mkldnn_primitive_desc_t> {
primitive_desc(const memory::primitive_desc &input,
const memory::primitive_desc &output) {
mkldnn_primitive_desc_t result;
error::wrap_c_api(mkldnn_reorder_primitive_desc_create(
&result, input.get(), output.get()),
"could not create a reorder primitive descriptor");
reset(result);
}
primitive_desc(const memory::primitive_desc &input,
const memory::primitive_desc &output,
const primitive_attr &aattr) {
mkldnn_primitive_desc_t result;
error::wrap_c_api(mkldnn_reorder_primitive_desc_create_v2(
&result, input.get(), output.get(), aattr.get()),
"could not create a reorder primitive descriptor");
reset(result);
}
engine get_engine() { return engine::query(*this); }
};
reorder(const primitive_desc &aprimitive_desc,
const primitive::at &input, const memory &output) {
mkldnn_primitive_t result;
mkldnn_primitive_at_t inputs[] = { input.data };
const_mkldnn_primitive_t outputs[] = { output.get() };
error::wrap_c_api(mkldnn_primitive_create(&result,
aprimitive_desc.get(), inputs, outputs),
"could not create a reorder primitive");
reset(result);
}
reorder(const primitive::at &input, const memory &output) {
auto input_mpd = memory(input).get_primitive_desc();
auto output_mpd = output.get_primitive_desc();
auto reorder_d = primitive_desc(input_mpd, output_mpd);
mkldnn_primitive_t result;
mkldnn_primitive_at_t inputs[] = { input.data };
const_mkldnn_primitive_t outputs[] = { output.get() };
error::wrap_c_api(mkldnn_primitive_create(&result,
reorder_d.get(), inputs, outputs),
"could not create a reorder primitive");
reset(result);
}
};
/// @}
/// @addtogroup cpp_api_view View
/// A primitive to view on a memory.
///
/// @sa mkldnn_view_primitive_desc_create in @ref c_api
/// @{
struct view : public primitive {
struct primitive_desc : public handle<mkldnn_primitive_desc_t> {
primitive_desc(const memory::primitive_desc &input, memory::dims dims,
memory::dims offsets) {
mkldnn_primitive_desc_t result;
error::wrap_c_api(mkldnn_view_primitive_desc_create(
&result, input.get(), &dims[0], &offsets[0]),
"could not create a view primitive descriptor");
reset(result);
}
memory::primitive_desc dst_primitive_desc() const {
memory::primitive_desc adesc;