This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
lib_api.h
1619 lines (1405 loc) · 59.7 KB
/
lib_api.h
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*!
* \file lib_api.h
* \brief APIs to interact with libraries
* This API specifies function prototypes to
* register custom ops, partitioner, and passes
* for library authors
* See example/extension/lib_custom_op/README.md
* See example/extension/lib_subgraph/README.md
* See example/extension/lib_pass/README.md
*/
#ifndef MXNET_LIB_API_H_
#define MXNET_LIB_API_H_
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <iostream>
#include <utility>
#include <stdexcept>
#include <functional>
#include <random>
#include <sstream>
#if defined(__NVCC__)
#include <cuda_runtime.h>
#include <curand_kernel.h>
#endif
/* Make sure to update the version number everytime you make changes */
#define MX_LIBRARY_VERSION 11
/*!
* \brief For loading multiple custom op libraries in Linux, exporting same symbol multiple
* times may lead to undefined behaviour, so we need to set symbol visibility to hidden
* see https://labjack.com/news/simple-cpp-symbol-visibility-demo for details
*/
#if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
#define PRIVATE_SYMBOL
#else
#define PRIVATE_SYMBOL __attribute__((visibility("hidden")))
#endif
/*
* Import from DLPack https://github.com/dmlc/dlpack/blob/master/include/dlpack/dlpack.h
*/
#ifndef DLPACK_VERSION
#ifdef __cplusplus
#define DLPACK_EXTERN_C extern "C"
#else
#define DLPACK_EXTERN_C
#endif
/*! \brief The current version of dlpack */
#define DLPACK_VERSION 020
/*! \brief DLPACK_DLL prefix for windows */
#ifdef _WIN32
#ifdef DLPACK_EXPORTS
#define DLPACK_DLL __declspec(dllexport)
#else
#define DLPACK_DLL __declspec(dllimport)
#endif
#else
#define DLPACK_DLL
#endif
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/*!
* \brief The device type in DLContext.
*/
typedef enum {
/*! \brief CPU device */
kDLCPU = 1,
/*! \brief CUDA GPU device */
kDLGPU = 2,
/*!
* \brief Pinned CUDA GPU device by cudaMallocHost
* \note kDLCPUPinned = kDLCPU | kDLGPU
*/
kDLCPUPinned = 3,
/*! \brief OpenCL devices. */
kDLOpenCL = 4,
/*! \brief Vulkan buffer for next generation graphics. */
kDLVulkan = 7,
/*! \brief Metal for Apple GPU. */
kDLMetal = 8,
/*! \brief Verilog simulator buffer */
kDLVPI = 9,
/*! \brief ROCm GPUs for AMD GPUs */
kDLROCM = 10,
/*!
* \brief Reserved extension device type,
* used for quickly test extension device
* The semantics can differ depending on the implementation.
*/
kDLExtDev = 12,
} DLDeviceType;
/*!
* \brief A Device context for Tensor and operator.
*/
typedef struct {
/*! \brief The device type used in the device. */
DLDeviceType device_type;
/*! \brief The device index */
int device_id;
} DLContext;
/*!
* \brief The type code options DLDataType.
*/
typedef enum {
kDLInt = 0U,
kDLUInt = 1U,
kDLFloat = 2U,
} DLDataTypeCode;
/*!
* \brief The data type the tensor can hold.
*
* Examples
* - float: type_code = 2, bits = 32, lanes=1
* - float4(vectorized 4 float): type_code = 2, bits = 32, lanes=4
* - int8: type_code = 0, bits = 8, lanes=1
*/
typedef struct {
/*!
* \brief Type code of base types.
* We keep it uint8_t instead of DLDataTypeCode for minimal memory
* footprint, but the value should be one of DLDataTypeCode enum values.
* */
uint8_t code;
/*!
* \brief Number of bits, common choices are 8, 16, 32.
*/
uint8_t bits;
/*! \brief Number of lanes in the type, used for vector types. */
uint16_t lanes;
} DLDataType;
/*!
* \brief Plain C Tensor object, does not manage memory.
*/
typedef struct {
/*!
* \brief The opaque data pointer points to the allocated data. This will be
* CUDA device pointer or cl_mem handle in OpenCL. This pointer is always
* aligns to 256 bytes as in CUDA.
*
* For given DLTensor, the size of memory required to store the contents of
* data is calculated as follows:
*
* \code{.c}
* static inline size_t GetDataSize(const DLTensor* t) {
* size_t size = 1;
* for (tvm_index_t i = 0; i < t->ndim; ++i) {
* size *= t->shape[i];
* }
* size *= (t->dtype.bits * t->dtype.lanes + 7) / 8;
* return size;
* }
* \endcode
*/
void* data;
/*! \brief The device context of the tensor */
DLContext ctx;
/*! \brief Number of dimensions */
int ndim;
/*! \brief The data type of the pointer*/
DLDataType dtype;
/*! \brief The shape of the tensor */
int64_t* shape;
/*!
* \brief strides of the tensor (in number of elements, not bytes)
* can be nullptr, indicating tensor is compact and row-majored.
*/
int64_t* strides;
/*! \brief The offset in bytes to the beginning pointer to data */
uint64_t byte_offset;
} DLTensor;
#ifdef __cplusplus
} // DLPACK_EXTERN_C
#endif
#endif
namespace mxnet {
namespace ext {
/* \brief Class to store error messages from extensions to pass to MXNet */
class MXerrorMsgs {
public:
/* \brief get singleton pointer to class */
static MXerrorMsgs* get();
/* \brief add a new error message */
std::stringstream& add(const char* file, int line);
/* \brief return number of error messages */
int size();
/* \brief get error message at index */
const std::string* get(int idx);
private:
/*! \brief constructor */
MXerrorMsgs() {}
/*! \brief destructor */
~MXerrorMsgs() {}
/*! \brief map of entries in registry */
std::vector<std::stringstream> messages;
};
// Add a new error message, example: MX_ERROR_MSG << "my error msg";
#define MX_ERROR_MSG mxnet::ext::MXerrorMsgs::get()->add(__FILE__, __LINE__)
/*!
* \brief Tensor data type, consistent with mshadow data type
*/
enum MXDType {
kFloat32 = 0,
kFloat64 = 1,
kFloat16 = 2,
kUint8 = 3,
kInt32 = 4,
kInt8 = 5,
kInt64 = 6,
kUNSET = 100,
};
/*
* MXTensor storage type.
*/
enum MXStorageType {
// dense
kDefaultStorage = 0,
// row sparse
kRowSparseStorage = 1,
// csr
kCSRStorage = 2,
};
/*!
* \brief Context info passing from MXNet OpContext
* dev_type is string repr of supported context, currently only "cpu" and "gpu"
* dev_id is the device index where the tensor locates
*/
struct MXContext {
MXContext();
explicit MXContext(std::string dev_type_, int dev_id_);
explicit MXContext(const char* dev_type_, int dev_id_);
static MXContext CPU();
static MXContext GPU();
static MXContext CPU(int dev_id);
static MXContext GPU(int dev_id);
std::string dev_type;
int dev_id;
};
enum MXReturnValue {
MX_FAIL = 0,
MX_SUCCESS = 1,
};
// For sparse tensors, read/write the data from NDarray via pointers.
struct MXSparse {
// Pointer to data.
void* data{nullptr};
// length of (non-zero) data.
int64_t data_len;
// To store aux data for sparse.
// For CSR, indices stores the col index of non-zero elements.
// For row sparse, indices store row index of rows which have non-zero elements.
int64_t* indices;
int64_t indices_len;
// For CSR, indptr gives the start and end index of data for each row.
// For row sparse, indptr is not used.
int64_t* indptr = nullptr;
int64_t indptr_len;
void set(void* data_ptr,
const int64_t* dims,
int ndims,
void* idx,
int64_t num_idx,
void* idx_ptr = nullptr,
int64_t num_idx_ptr = 0);
};
/*!
* \brief Tensor data structure used by custom operator
*/
struct MXTensor {
MXTensor();
MXTensor(const MXTensor& oth);
MXTensor(void* data_ptr,
std::vector<int64_t> shape,
MXDType dtype,
size_t vID,
MXContext mx_ctx,
MXStorageType stype = kDefaultStorage);
/*! \brief populate internal tensor fields */
void setTensor(void* dptr,
MXDType type,
const int64_t* dims,
int ndims,
size_t vID,
MXContext mx_ctx,
MXStorageType storage_type);
/*! \brief populate DLTensor fields */
void setDLTensor();
/*! \brief helper function to cast data pointer */
template <typename data_type>
inline data_type* data() {
return reinterpret_cast<data_type*>(data_ptr);
}
/*! \brief helper function to get data size */
int64_t size() const;
/*! \brief helper function to compare two MXTensors */
bool isSame(const MXTensor& oth) const;
// For dense, data_ptr points to 1D flattened tensor data
// For sparse, data_ptr points to MXSparse
void* data_ptr;
// shape is in [2,3,4] format to represent high-dim tensor
std::vector<int64_t> shape;
// type can only be MXDType enum types
MXDType dtype;
// version number updated if the tensor has changed since the last use by custom op
size_t verID;
// context of MXTensor representing which device the tensor data is located
MXContext ctx;
// corresponding DLTensor repr of MXTensor
// easy way to reuse functions taking DLTensor
DLTensor dltensor;
// storage type
MXStorageType stype;
};
/*! \brief resource malloc function to allocate memory inside Forward/Backward functions */
typedef void* (*xpu_malloc_t)(void*, int);
/*! \brief sparse alloc function to allocate memory inside Forward/Backward functions */
typedef void (*sparse_malloc_t)(void*, int, int, int, void**, int64_t**, int64_t**);
/*! \brief resource malloc function to allocate ndarrays for graph passes */
typedef void (*nd_malloc_t)(const void* _ndarray_alloc,
const int64_t* shapes,
int num_shapes,
const char* dev_str,
int dev_id,
int dtype,
const char* name,
int isArg,
void** data);
/*! \brief GPU stream pointer, is void* when not compiled with CUDA */
#if defined(__NVCC__)
typedef cudaStream_t mx_stream_t;
typedef curandStatePhilox4_32_10_t mx_gpu_rand_t;
#else
typedef void* mx_stream_t;
typedef void* mx_gpu_rand_t;
#endif
typedef std::mt19937 mx_cpu_rand_t;
/*! \brief MXNet initialized random states for each device, used for parallelism */
/* Each thread should generate random number unique sequence out of different states */
#define MX_NUM_CPU_RANDOM_STATES 1024
#define MX_NUM_GPU_RANDOM_STATES 32768
/* \brief Class to help allocate new args/aux params in graph passes */
class PassResource {
public:
PassResource(std::unordered_map<std::string, MXTensor>* new_args,
std::unordered_map<std::string, MXTensor>* new_aux,
nd_malloc_t nd_malloc,
const void* nd_alloc);
// allocate new arg param, adds to args map, returns newly allocated tensor
MXTensor* alloc_arg(const std::string& name,
const std::vector<int64_t>& shapes,
const MXContext& ctx,
MXDType dtype) const;
// allocate new aux param, adds to aux map, returns newly allocated tensor
MXTensor* alloc_aux(const std::string& name,
const std::vector<int64_t>& shapes,
const MXContext& ctx,
MXDType dtype) const;
private:
std::unordered_map<std::string, MXTensor>* new_args_;
std::unordered_map<std::string, MXTensor>* new_aux_;
nd_malloc_t nd_malloc_;
const void* nd_alloc_;
};
/*!
* \brief provide resource APIs memory allocation mechanism to Forward/Backward functions
*/
class OpResource {
public:
OpResource(xpu_malloc_t cpu_malloc_fp,
void* cpu_alloc_fp,
xpu_malloc_t gpu_malloc_fp,
void* gpu_alloc_fp,
void* stream,
sparse_malloc_t sparse_malloc_fp,
void* sparse_alloc_fp,
void* rng_cpu_states,
void* rng_gpu_states);
/*! \brief allocate cpu memory controlled by MXNet */
void* alloc_cpu(int size) const;
/*! \brief allocate gpu memory controlled by MXNet */
void* alloc_gpu(int size) const;
/*! \brief return the cuda stream object with correct type */
inline mx_stream_t get_cuda_stream() const {
return static_cast<mx_stream_t>(cuda_stream);
}
/*! \brief allocate sparse memory controlled by MXNet */
void alloc_sparse(MXSparse* sparse, int index, int indices_len, int indptr_len = 0) const;
/*! \brief get pointer to initialized and seeded random number states located on CPU */
/* Access each state by states[id], but this id should be <= MX_NUM_CPU_RANDOM_STATES */
mx_cpu_rand_t* get_cpu_rand_states() const;
/*! \brief get pointer to initialized and seeded random number states located on GPU */
/* Access each state by states[id], but this id should be <= MX_NUM_GPU_RANDOM_STATES */
/* Note that if you are using cpu build, it will return a nullptr */
inline mx_gpu_rand_t* get_gpu_rand_states() const {
return static_cast<mx_gpu_rand_t*>(rand_gpu_states);
}
private:
/*! \brief allocation lambda function */
xpu_malloc_t cpu_malloc, gpu_malloc;
/*! \brief lambda function to return allocated memory handle */
void *cpu_alloc, *gpu_alloc;
/*! \brief cuda stream passed from MXNet */
void* cuda_stream;
/*! \brief sparse allocation lambda function */
sparse_malloc_t sparse_malloc;
/*! \brief lambda function to return allocated sparse memory handle */
void* sparse_alloc;
/*! \brief cpu and gpu rng fully inited and seeded states */
void *rand_cpu_states, *rand_gpu_states;
};
/*! \brief attribute key to help passing serialized subgraph through subgraph op attribute */
#define MX_STR_SUBGRAPH_SYM_JSON "subgraph_sym_json"
/*! \brief dtype attribute key for ops after type propagation */
#define MX_STR_DTYPE "__ext_dtype__"
/*! \brief shape attribute key for ops after shape propagation */
#define MX_STR_SHAPE "__ext_shape__"
/*! \brief extra input attribute key for ops */
#define MX_STR_EXTRA_INPUTS "__ext_extra_inputs__"
/* \brief get shape value from list of shapes string
*
* Examples:
*
* getShapeAt("[[1]]", 0) returns "[1]"
* getShapeAt("[[1],[2,3]]", 1) returns "[2,3]"
*/
std::string getShapeAt(const std::string& shape, unsigned index);
/* \brief get dtype value from list of dtypes string
*
* Examples:
*
* getDtypeAt("[1]", 0) returns "1"
* getDtypeAt("[1,2]", 1) returns "2"
*/
std::string getDtypeAt(const std::string& dtype, unsigned index);
/*!
* \brief Json utility to parse serialized subgraph symbol
*/
/*! \brief Types of JSON objects */
enum JsonType { ERR, STR, NUM, LIST, MAP };
/*! \brief definition of JSON objects */
struct JsonVal {
JsonVal(); // default constructor
// construct a JSON object by type
explicit JsonVal(JsonType t);
// construct a string JSON object
explicit JsonVal(std::string s);
// construct a number JSON object
explicit JsonVal(int n);
// complex constructor
JsonVal(JsonType t, int n, std::string s);
bool operator<(const JsonVal& o) const;
// convert JSON object back to JSON-compatible string
std::string dump() const;
// convert JSON-compatible string to JSON object
static JsonVal parse(const std::string& json);
// parse a string JSON object
static JsonVal parse_string(const std::string& json, unsigned int* idx);
// parse a number JSON object
static JsonVal parse_num(const std::string& json, unsigned int* idx);
// parse a list of JSON objects
static JsonVal parse_list(const std::string& json, unsigned int* idx);
// parse a map of JSON objects
static JsonVal parse_map(const std::string& json, unsigned int* idx);
// generic parse function
static JsonVal parse(const std::string& json, unsigned int* idx);
// debug function to convert data structure to a debugstring
std::string toString() const;
JsonType type;
int num;
std::string str;
std::vector<JsonVal> list;
std::map<JsonVal, JsonVal> map;
};
/*!
* \brief Graph utility to parse serialized subgraph symbol
*/
class Node;
class Graph;
// Representation of an input/output to a node
struct NodeEntry {
Node* node; // other node thats producing/consuming inputs/outputs
int entry; // entry index from other node (ie. output index from producing node)
};
// Representation of a node in the graph
class Node {
public:
Node();
// internally set passResource to enable tensor allocation for graph passes
void _setPassResource(PassResource* res_);
/* \brief allocate an arg tensor for this node */
void alloc_arg(const std::vector<int64_t>& shapes, const MXContext& ctx, MXDType dtype);
/* \brief allocate an aux tensor for this node */
void alloc_aux(const std::vector<int64_t>& shapes, const MXContext& ctx, MXDType dtype);
std::string op; // operator name (ie. Convolution)
std::string name; // unique node name (ie. conv_0 or conv_1)
MXTensor* tensor; // tensor data for input nodes
std::vector<NodeEntry> inputs; // set of inputs to the node
std::vector<NodeEntry> outputs; // set of outputs from the node
std::vector<Graph*> subgraphs; // set of subgraphs within this node
std::unordered_map<std::string, std::string> attrs; // node attributes
private:
PassResource* res;
};
// Representation of the graph
class Graph {
public:
Graph();
/* \brief deleted nodes when deleting the graph */
~Graph();
/* \brief create a graph object from an unparsed string */
static Graph* fromString(const std::string& json);
/* \brief create a graph object from a parsed JSON object */
static Graph* fromJson(JsonVal val);
/* \brief convert graph object back to JSON object */
JsonVal toJson() const;
/* \brief convert graph object to JSON string */
std::string toString() const;
/* \brief visits a node "n" */
void _dfs_util(Node* n,
std::unordered_set<Node*>* to_visit,
std::function<void(Node*)> handler) const;
/* \brief post-order DFS graph traversal */
void DFS(std::function<void(Node*)> handler) const;
/* \brief sort graph nodes in topological order */
std::vector<Node*> topological_sort() const;
/* \brief print out graph details */
void print(int indent = 0) const;
/* \brief add a new node to this graph */
Node* addNode(const std::string& name, const std::string& op);
/* \brief get node at index in graph */
Node* getNode(size_t idx);
/* \brief get const node at index in const graph */
const Node* getNode(size_t idx) const;
/* \brief get attribute on graph */
const JsonVal& getAttr(const std::string& key) const;
/* \brief get number of nodes in the graph */
size_t size() const;
// internally set passResource to enable tensor allocation for graph passes
void _setPassResource(PassResource* res_);
// internally set arg/aux params when available
void _setParams(std::unordered_map<std::string, mxnet::ext::MXTensor>* args,
std::unordered_map<std::string, mxnet::ext::MXTensor>* aux);
std::vector<Node*> inputs;
std::vector<NodeEntry> outputs;
std::map<std::string, JsonVal> attrs;
private:
std::vector<Node*> nodes;
PassResource* res;
};
/* \brief An abstract class for library authors creating custom
* partitioners. Optional, can just implement supportedOps instead
*/
class CustomOpSelector {
public:
/* \brief Select a node to include in subgraph, return true to include node
* nodeID - index of node in graph
*/
virtual bool Select(int nodeID) = 0;
/* \brief Select an input node from current node to include in subgraph
* return true to include node
* nodeID - index of node in graph
* input_nodeID - index of input node in graph
*/
virtual bool SelectInput(int nodeID, int input_nodeID) = 0;
/* \brief Select an output node from current node to include in subgraph
* return true to include node
* nodeID - index of node in graph
* output_nodeID - index of output node in graph
*/
virtual bool SelectOutput(int nodeID, int output_nodeID) = 0;
/* \brief Review nodes to include in subgraph
* return set of candidate nodes to keep in subgraph
* candidates - indices of nodes to include in subgraph
* keep - indices of nodes to keep in subgraph
*/
virtual void Filter(const std::vector<int>& candidates, std::vector<int>* keep) {
keep->insert(keep->end(), candidates.begin(), candidates.end());
}
/* \brief Reset any selector state, called after growing subgraph, before filter
* Called after finished calling SelectInput/SelectOutput and growing subgraph
*/
virtual void Reset() {}
};
/*!
* \brief An abstract class for library authors creating stateful op
* custom library should override Forward and destructor, and has an
* option to implement Backward
*/
class CustomStatefulOp {
public:
CustomStatefulOp();
virtual ~CustomStatefulOp();
template <class A, typename... Ts>
static CustomStatefulOp* create(Ts... args) {
CustomStatefulOp* op = new A(args...);
op->created = true;
return op;
}
bool wasCreated() {
return created;
}
virtual MXReturnValue Forward(std::vector<MXTensor>* inputs,
std::vector<MXTensor>* outputs,
const OpResource& op_res) = 0;
virtual MXReturnValue Backward(std::vector<MXTensor>* inputs,
std::vector<MXTensor>* outputs,
const OpResource& op_res) {
MX_ERROR_MSG << "Error! Operator does not support backward" << std::endl;
return MX_FAIL;
}
bool ignore_warn;
private:
bool created;
};
/*! \brief Custom Operator function templates */
typedef MXReturnValue (*fcomp_t)(const std::unordered_map<std::string, std::string>& attributes,
std::vector<MXTensor>* inputs,
std::vector<MXTensor>* outputs,
const OpResource& res);
typedef MXReturnValue (*parseAttrs_t)(
const std::unordered_map<std::string, std::string>& attributes,
int* num_inputs,
int* num_outputs);
typedef MXReturnValue (*inferType_t)(const std::unordered_map<std::string, std::string>& attributes,
std::vector<int>* in_types,
std::vector<int>* out_types);
typedef MXReturnValue (*inferSType_t)(
const std::unordered_map<std::string, std::string>& attributes,
std::vector<int>* in_storage_types,
std::vector<int>* out_storage_types);
typedef MXReturnValue (*inferShape_t)(
const std::unordered_map<std::string, std::string>& attributes,
std::vector<std::vector<unsigned int> >* in_shapes,
std::vector<std::vector<unsigned int> >* out_shapes);
typedef MXReturnValue (*mutateInputs_t)(
const std::unordered_map<std::string, std::string>& attributes,
std::vector<int>* input_indices);
typedef MXReturnValue (*createOpState_t)(
const std::unordered_map<std::string, std::string>& attributes,
const MXContext& ctx,
const std::vector<std::vector<unsigned int> >& in_shapes,
const std::vector<int> in_types,
CustomStatefulOp**);
/*!
* \brief Class to hold custom operator registration
*/
class CustomOp {
public:
explicit CustomOp(const char* op_name);
CustomOp& setForward(fcomp_t fcomp, const char* ctx);
CustomOp& setBackward(fcomp_t fgrad, const char* ctx);
CustomOp& setParseAttrs(parseAttrs_t func);
CustomOp& setInferType(inferType_t func);
CustomOp& setInferSType(inferSType_t func);
CustomOp& setInferShape(inferShape_t func);
CustomOp& setMutateInputs(mutateInputs_t func);
CustomOp& setCreateOpState(createOpState_t func, const char* ctx);
CustomOp& setIsSubgraphOp();
void mapToVector();
/*! \brief operator name */
const char* name;
/*! \brief operator functions */
parseAttrs_t parse_attrs;
inferType_t infer_type;
inferSType_t infer_storage_type;
inferShape_t infer_shape;
mutateInputs_t mutate_inputs;
bool isSGop;
/*! \brief vector repr of ctx map to be easily loaded from c_api */
std::vector<const char*> forward_ctx_cstr, backward_ctx_cstr, create_op_ctx_cstr;
std::vector<fcomp_t> forward_fp, backward_fp;
std::vector<createOpState_t> create_op_fp;
private:
void raiseDuplicateContextError();
/*! \brief dedup context maps - static string ctx to custom function */
std::unordered_map<const char*, fcomp_t> forward_ctx_map, backward_ctx_map;
std::unordered_map<const char*, createOpState_t> create_op_ctx_map;
};
/*! \brief Custom Pass Create function template */
typedef MXReturnValue (*graphPass_t)(mxnet::ext::Graph* graph,
const std::unordered_map<std::string, std::string>& options);
/*!
* \brief An abstract class for graph passes
*/
class CustomPass {
public:
CustomPass();
explicit CustomPass(const char* pass_name);
CustomPass& setBody(graphPass_t fn);
/*! \brief pass name */
const char* name;
/*! \brief pass function */
graphPass_t pass;
};
/*! \brief Custom Subgraph Create function template */
typedef MXReturnValue (*supportedOps_t)(
const mxnet::ext::Graph* graph,
std::vector<int>* ids,
const std::unordered_map<std::string, std::string>& options);
typedef MXReturnValue (*createSelector_t)(
const mxnet::ext::Graph* graph,
CustomOpSelector** sel_inst,
const std::unordered_map<std::string, std::string>& options);
typedef MXReturnValue (*reviewSubgraph_t)(
const mxnet::ext::Graph* subgraph,
int subgraph_id,
bool* accept,
const std::unordered_map<std::string, std::string>& options,
std::unordered_map<std::string, std::string>* attrs);
/*!
* \brief An abstract class for subgraph property
*/
class CustomPartitioner {
public:
CustomPartitioner();
explicit CustomPartitioner(const char* backend_name);
CustomPartitioner& addStrategy(const char* prop_name, const char* sg_name);
CustomPartitioner& setSupportedOps(const char* prop_name, supportedOps_t fn);
CustomPartitioner& setCreateSelector(const char* prop_name, createSelector_t fn);
CustomPartitioner& setReviewSubgraph(const char* prop_name, reviewSubgraph_t fn);
supportedOps_t getSupportedOps(int stg_id);
createSelector_t getCreateSelector(int stg_id);
reviewSubgraph_t getReviewSubgraph(int stg_id);
/*! \brief partitioner name */
const char* name;
std::map<std::string, supportedOps_t> supported_map;
std::map<std::string, createSelector_t> selector_map;
std::map<std::string, reviewSubgraph_t> review_map;
/*! \brief strategy names */
std::vector<const char*> strategies;
/*! \brief subgraph operator name */
std::vector<const char*> op_names;
};
/*!
* \brief Registry class to registers things (ops, properties)
* Singleton class
*/
template <class T>
class Registry {
public:
/*!
* \brief get singleton pointer to class
* \returns pointer to class
*/
static Registry* get() PRIVATE_SYMBOL {
static Registry inst;
return &inst;
}
/*!
* \brief add a new entry
* \returns new object associated with registered name
*/
T& add(const char* name) {
T* entry = new T(name);
entries.push_back(entry);
return *entry;
}
int size() {
return entries.size();
}
T& get(int idx) {
return *(entries.at(idx));
}
private:
/*! \brief constructor */
Registry() {}
/*! \brief destructor */
~Registry() {}
/*! \brief map of entries in registry */
std::vector<T*> entries;
};
/*!
* \brief Macros to help with string concat
* Annoyingly, the concat_ and concat macros are necessary to
* be able to use __COUNTER__ in an identifier name
*/
#define MX_STR_CONCAT_(__a, __b) __a##__b
#define MX_STR_CONCAT(__a, __b) MX_STR_CONCAT_(__a, __b)
/*! \brief convert a token to a string */
#define MX_STRINGIFY(x) #x
#define MX_TOSTRING(x) MX_STRINGIFY(x)
/*! \brief declare a variable with custom name */
#define MX_REGISTER_NAME_(Name) MXNet##_CustomOp##_##Name
#define MX_REGISTER_DEF_(Name) mxnet::ext::CustomOp MX_REGISTER_NAME_(Name)
#define MX_REGISTER_PROP_NAME_(Name) MXNet##_CustomSubProp##_##Name
#define MX_REGISTER_PROP_DEF_(Name) mxnet::ext::CustomPartitioner MX_REGISTER_PROP_NAME_(Name)
#define MX_REGISTER_PASS_NAME_(Name) MXNet##_CustomPass##_##Name
#define MX_REGISTER_PASS_DEF_(Name) mxnet::ext::CustomPass MX_REGISTER_PASS_NAME_(Name)
/*! \brief assign a var to a value */
#define REGISTER_OP(Name) \
MX_STR_CONCAT(MX_REGISTER_DEF_(Name), __COUNTER__) = \
mxnet::ext::Registry<mxnet::ext::CustomOp>::get()->add(MX_TOSTRING(Name))
#define REGISTER_PARTITIONER(Name) \
MX_STR_CONCAT(MX_REGISTER_PROP_DEF_(Name), __COUNTER__) = \
mxnet::ext::Registry<mxnet::ext::CustomPartitioner>::get()->add(MX_TOSTRING(Name))
#define REGISTER_PASS(Name) \
MX_STR_CONCAT(MX_REGISTER_PASS_DEF_(Name), __COUNTER__) = \
mxnet::ext::Registry<mxnet::ext::CustomPass>::get()->add(MX_TOSTRING(Name))
/* -------------- BELOW ARE CTYPE FUNCTIONS PROTOTYPES --------------- */
/*!
* \brief Following are the C type APIs implemented in the external library
* Each API has a #define string that is used to lookup the function in the library
* Followed by the function declaration
*/
#define MXLIB_OPREGSIZE_STR "_opRegSize"
typedef int (*opRegSize_t)(void);
#define MXLIB_OPREGGET_STR "_opRegGet"
typedef int (*opRegGet_t)(int idx,
const char** name,
int* isSGop,
const char*** forward_ctx,
mxnet::ext::fcomp_t** forward_fp,
int* forward_count,
const char*** backward_ctx,
mxnet::ext::fcomp_t** backward_fp,
int* backward_count,
const char*** create_op_ctx,
mxnet::ext::createOpState_t** create_op_fp,
int* create_op_count,
mxnet::ext::parseAttrs_t* parse,
mxnet::ext::inferType_t* type,
mxnet::ext::inferSType_t* stype,
mxnet::ext::inferShape_t* shape,
mxnet::ext::mutateInputs_t* mutate);