forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_api.h
2008 lines (1929 loc) · 79.2 KB
/
c_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 c_api.h
* \brief C API of mxnet
*/
#ifndef MXNET_C_API_H_
#define MXNET_C_API_H_
/*! \brief Inhibit C++ name-mangling for MXNet functions. */
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/*! \brief Keep the default value in C++ */
#ifdef __cplusplus
#define DEFAULT(x) = x
#else
#define DEFAULT(x)
#endif // __cplusplus
#include <stdint.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
/*! \brief MXNET_DLL prefix for windows */
#ifdef _WIN32
#ifdef MXNET_EXPORTS
#define MXNET_DLL __declspec(dllexport)
#else
#define MXNET_DLL __declspec(dllimport)
#endif
#else
#define MXNET_DLL
#endif
/*! \brief manually define unsigned int */
typedef unsigned int mx_uint;
/*! \brief manually define float */
typedef float mx_float;
// all the handles are simply void *
// will be casted internally to specific pointers types
// these typedefs are mainly used for readablity reasons
/*! \brief handle to NDArray */
typedef void *NDArrayHandle;
/*! \brief handle to a mxnet narray function that changes NDArray */
typedef const void *FunctionHandle;
/*! \brief handle to a function that takes param and creates symbol */
typedef void *AtomicSymbolCreator;
/*! \brief handle to cached operator */
typedef void *CachedOpHandle;
/*! \brief handle to a symbol that can be bind as operator */
typedef void *SymbolHandle;
/*! \brief handle to a AtomicSymbol */
typedef void *AtomicSymbolHandle;
/*! \brief handle to an Executor */
typedef void *ExecutorHandle;
/*! \brief handle a dataiter creator */
typedef void *DataIterCreator;
/*! \brief handle to a DataIterator */
typedef void *DataIterHandle;
/*! \brief handle to KVStore */
typedef void *KVStoreHandle;
/*! \brief handle to RecordIO */
typedef void *RecordIOHandle;
/*! \brief handle to MXRtc*/
typedef void *RtcHandle;
/*! \brief handle to rtc cuda module*/
typedef void *CudaModuleHandle;
/*! \brief handle to rtc cuda kernel*/
typedef void *CudaKernelHandle;
typedef void (*ExecutorMonitorCallback)(const char*,
NDArrayHandle,
void *);
struct NativeOpInfo {
void (*forward)(int, float**, int*, unsigned**, int*, void*);
void (*backward)(int, float**, int*, unsigned**, int*, void*);
void (*infer_shape)(int, int*, unsigned**, void*);
void (*list_outputs)(char***, void*);
void (*list_arguments)(char***, void*);
// all functions also pass a payload void* pointer
void* p_forward;
void* p_backward;
void* p_infer_shape;
void* p_list_outputs;
void* p_list_arguments;
};
struct NDArrayOpInfo {
bool (*forward)(int, void**, int*, void*);
bool (*backward)(int, void**, int*, void*);
bool (*infer_shape)(int, int*, unsigned**, void*);
bool (*list_outputs)(char***, void*);
bool (*list_arguments)(char***, void*);
bool (*declare_backward_dependency)(const int*, const int*, const int*,
int*, int**, void*);
// all functions also pass a payload void* pointer
void* p_forward;
void* p_backward;
void* p_infer_shape;
void* p_list_outputs;
void* p_list_arguments;
void* p_declare_backward_dependency;
};
typedef int (*MXGenericCallback)(void);
struct MXCallbackList {
int num_callbacks;
int (**callbacks)(void);
void **contexts;
};
enum CustomOpCallbacks {
kCustomOpDelete,
kCustomOpForward,
kCustomOpBackward
};
enum CustomOpPropCallbacks {
kCustomOpPropDelete,
kCustomOpPropListArguments,
kCustomOpPropListOutputs,
kCustomOpPropListAuxiliaryStates,
kCustomOpPropInferShape,
kCustomOpPropDeclareBackwardDependency,
kCustomOpPropCreateOperator,
kCustomOpPropInferType
};
typedef int (*CustomOpFBFunc)(int /*size*/, void** /*ptrs*/, int* /*tags*/,
const int* /*reqs*/, const int /*is_train*/,
void* /*state*/);
typedef int (*CustomOpDelFunc)(void* /*state*/);
typedef int (*CustomOpListFunc)(char*** /*args*/, void* /*state*/);
typedef int (*CustomOpInferShapeFunc)(int /*num_input*/, int* /*ndims*/,
unsigned** /*shapes*/, void* /*state*/);
typedef int (*CustomOpInferTypeFunc)(int /*num_input*/, int* /*types*/, void* /*state*/);
typedef int (*CustomOpBwdDepFunc)(const int* /*out_grad*/, const int* /*in_data*/,
const int* /*out_data*/, int* /*num_deps*/,
int** /*rdeps*/, void* /*state*/);
typedef int (*CustomOpCreateFunc)(const char* /*ctx*/, int /*num_inputs*/,
unsigned** /*shapes*/, const int* /*ndims*/,
const int* /*dtypes*/, struct MXCallbackList* /*ret*/,
void* /*state*/);
typedef int (*CustomOpPropCreator)(const char* /*op_type*/, const int /*num_kwargs*/,
const char** /*keys*/, const char** /*values*/,
struct MXCallbackList* /*ret*/);
enum CustomFunctionCallbacks {
kCustomFunctionBackward,
kCustomFunctionDelete
};
typedef int (*CustomFunctionBwdFunc)(int /*num_ograds*/, int /*num_igrads*/, void** /*ptrs*/,
const int* /*reqs*/, const int /*is_train*/,
void* /*state*/);
typedef int (*CustomFunctionDelFunc)(void* /*state*/);
/*!
* \brief return str message of the last error
* all function in this file will return 0 when success
* and -1 when an error occured,
* MXGetLastError can be called to retrieve the error
*
* this function is threadsafe and can be called by different thread
* \return error info
*/
MXNET_DLL const char *MXGetLastError();
//-------------------------------------
// Part 0: Global State setups
//-------------------------------------
/*!
* \brief Seed the global random number generators in mxnet.
* \param seed the random number seed.
* \return 0 when success, -1 when failure happens.
*/
MXNET_DLL int MXRandomSeed(int seed);
/*!
* \brief Notify the engine about a shutdown,
* This can help engine to print less messages into display.
*
* User do not have to call this function.
* \return 0 when success, -1 when failure happens.
*/
MXNET_DLL int MXNotifyShutdown();
/*!
* \brief Set up configuration of profiler
* \param mode indicate the working mode of profiler,
* record anly symbolic operator when mode == 0,
* record all operator when mode == 1
* \param filename where to save trace file
* \return 0 when success, -1 when failure happens.
*/
MXNET_DLL int MXSetProfilerConfig(int mode, const char* filename);
/*!
* \brief Set up state of profiler
* \param state indicate the working state of profiler,
* profiler not running when state == 0,
* profiler running when state == 1
* \return 0 when success, -1 when failure happens.
*/
MXNET_DLL int MXSetProfilerState(int state);
/*! \brief Save profile and stop profiler */
MXNET_DLL int MXDumpProfile();
/*! \brief Set the number of OMP threads to use */
MXNET_DLL int MXSetNumOMPThreads(int thread_num);
/*!
* \brief get the MXNet library version as an integer
* \param pointer to the integer holding the version number
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXGetVersion(int *out);
//-------------------------------------
// Part 1: NDArray creation and deletion
//-------------------------------------
/*!
* \brief create a NDArray handle that is not initialized
* can be used to pass in as mutate variables
* to hold the result of NDArray
* \param out the returning handle
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayCreateNone(NDArrayHandle *out);
/*!
* \brief create a NDArray with specified shape
* \param shape the pointer to the shape
* \param ndim the dimension of the shape
* \param dev_type device type, specify device we want to take
* \param dev_id the device id of the specific device
* \param delay_alloc whether to delay allocation until
* the narray is first mutated
* \param out the returning handle
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayCreate(const mx_uint *shape,
mx_uint ndim,
int dev_type,
int dev_id,
int delay_alloc,
NDArrayHandle *out);
/*!
* \brief create a NDArray with specified shape and data type
* \param shape the pointer to the shape
* \param ndim the dimension of the shape
* \param dev_type device type, specify device we want to take
* \param dev_id the device id of the specific device
* \param delay_alloc whether to delay allocation until
* the narray is first mutated
* \param dtype data type of created array
* \param out the returning handle
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayCreateEx(const mx_uint *shape,
mx_uint ndim,
int dev_type,
int dev_id,
int delay_alloc,
int dtype,
NDArrayHandle *out);
/*!
* \brief create an empty sparse NDArray with specified shape and data type
* \param storage_type the storage type of the ndarray
* \param shape the pointer to the shape
* \param ndim the dimension of the shape
* \param dev_type device type, specify device we want to take
* \param dev_id the device id of the specific device
* \param delay_alloc whether to delay allocation until
* the narray is first mutated
* \param dtype data type of created array
* \param num_aux the number of aux data to support this ndarray
* \param aux_type data type of the aux data for the created array
* \param aux_ndims the dimension of the shapes of aux data
* \param aux_shape the shapes of aux data
* \param out the returning handle
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayCreateSparseEx(int storage_type,
const mx_uint *shape,
mx_uint ndim,
int dev_type,
int dev_id,
int delay_alloc,
int dtype,
mx_uint num_aux,
int *aux_type,
mx_uint *aux_ndims,
const mx_uint *aux_shape,
NDArrayHandle *out);
/*!
* \brief create a NDArray handle that is loaded from raw bytes.
* \param buf the head of the raw bytes
* \param size size of the raw bytes
* \param out the returning handle
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayLoadFromRawBytes(const void *buf,
size_t size,
NDArrayHandle *out);
/*!
* \brief save the NDArray into raw bytes.
* \param handle the NDArray handle
* \param out_size size of the raw bytes
* \param out_buf the head of returning memory bytes.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArraySaveRawBytes(NDArrayHandle handle,
size_t *out_size,
const char **out_buf);
/*!
* \brief Save list of narray into the file.
* \param fname name of the file.
* \param num_args number of arguments to save.
* \param args the array of NDArrayHandles to be saved.
* \param keys the name of the NDArray, optional, can be NULL
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArraySave(const char* fname,
mx_uint num_args,
NDArrayHandle* args,
const char** keys);
/*!
* \brief Load list of narray from the file.
* \param fname name of the file.
* \param out_size number of narray loaded.
* \param out_arr head of the returning narray handles.
* \param out_name_size size of output name arrray.
* \param out_names the names of returning NDArrays, can be NULL
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayLoad(const char* fname,
mx_uint *out_size,
NDArrayHandle** out_arr,
mx_uint *out_name_size,
const char*** out_names);
/*!
* \brief Perform a synchronize copy from a continugous CPU memory region.
*
* This function will call WaitToWrite before the copy is performed.
* This is useful to copy data from existing memory region that are
* not wrapped by NDArray(thus dependency not being tracked).
*
* \param handle the NDArray handle
* \param data the data source to copy from.
* \param size the memory size we want to copy from.
*/
MXNET_DLL int MXNDArraySyncCopyFromCPU(NDArrayHandle handle,
const void *data,
size_t size);
/*!
* \brief Perform a synchronize copyto a continugous CPU memory region.
*
* This function will call WaitToRead before the copy is performed.
* This is useful to copy data from existing memory region that are
* not wrapped by NDArray(thus dependency not being tracked).
*
* \param handle the NDArray handle
* \param data the data source to copy into.
* \param size the memory size we want to copy into.
*/
MXNET_DLL int MXNDArraySyncCopyToCPU(NDArrayHandle handle,
void *data,
size_t size);
/*!
* \brief Copy src.data() to dst.data() if i = -1, else dst.aux_data(i) if i >= 0
* This function blocks. Do not use it in performance critical code.
* \param handle_dst handle of a dst ndarray whose data/aux_data has been allocated
* \param handle_src handle of a src ndarray which has default storage type
* \param i dst data blob indicator
*/
MXNET_DLL int MXNDArraySyncCopyFromNDArray(NDArrayHandle handle_dst,
const NDArrayHandle handle_src,
const int i);
/*!
* \brief check whether the NDArray format is valid
* \param full_check if `True`, rigorous check, O(N) operations
* Otherwise basic check, O(1) operations
*/
MXNET_DLL int MXNDArraySyncCheckFormat(NDArrayHandle handle, const bool full_check);
/*!
* \brief Wait until all the pending writes with respect NDArray are finished.
* Always call this before read data out synchronizely.
* \param handle the NDArray handle
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayWaitToRead(NDArrayHandle handle);
/*!
* \brief Wait until all the pending read/write with respect NDArray are finished.
* Always call this before write data into NDArray synchronizely.
* \param handle the NDArray handle
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayWaitToWrite(NDArrayHandle handle);
/*!
* \brief wait until all delayed operations in
* the system is completed
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayWaitAll();
/*!
* \brief free the narray handle
* \param handle the handle to be freed
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayFree(NDArrayHandle handle);
/*!
* \brief Slice the NDArray along axis 0.
* \param handle the handle to the NDArray
* \param slice_begin The beginning index of slice
* \param slice_end The ending index of slice
* \param out The NDArrayHandle of sliced NDArray
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArraySlice(NDArrayHandle handle,
mx_uint slice_begin,
mx_uint slice_end,
NDArrayHandle *out);
/*!
* \brief Index the NDArray along axis 0.
* \param handle the handle to the NDArray
* \param idx the index
* \param out The NDArrayHandle of output NDArray
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayAt(NDArrayHandle handle,
mx_uint idx,
NDArrayHandle *out);
/*!
* \brief get the storage type of the array
*/
MXNET_DLL int MXNDArrayGetStorageType(NDArrayHandle handle,
int *out_storage_type);
/*!
* \brief Reshape the NDArray.
* \param handle the handle to the narray
* \param ndim number of dimensions of new shape
* \param dims new shape
* \param out the NDArrayHandle of reshaped NDArray
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayReshape(NDArrayHandle handle,
int ndim,
int *dims,
NDArrayHandle *out);
/*!
* \brief get the shape of the array
* \param handle the handle to the narray
* \param out_dim the output dimension
* \param out_pdata pointer holder to get data pointer of the shape
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayGetShape(NDArrayHandle handle,
mx_uint *out_dim,
const mx_uint **out_pdata);
/*!
* \brief get the content of the data in NDArray
* \param handle the handle to the ndarray
* \param out_pdata pointer holder to get pointer of data
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayGetData(NDArrayHandle handle,
void **out_pdata);
/*!
* \brief get the type of the data in NDArray
* \param handle the handle to the narray
* \param out_dtype pointer holder to get type of data
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayGetDType(NDArrayHandle handle,
int *out_dtype);
/*!
* \brief get the type of the ith aux data in NDArray
* \param handle the handle to the narray
* \param i the index of the aux data
* \param out_type pointer holder to get type of aux data
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayGetAuxType(NDArrayHandle handle,
mx_uint i,
int *out_type);
/*!
* \brief Get a deep copy of the ith aux data blob
* in the form of an NDArray of default storage type.
* This function blocks. Do not use it in performance critical code.
*/
MXNET_DLL int MXNDArrayGetAuxNDArray(NDArrayHandle handle,
mx_uint i,
NDArrayHandle *out);
/*!
* \brief Get a deep copy of the data blob
* in the form of an NDArray of default storage type.
* This function blocks. Do not use it in performance critical code.
*/
MXNET_DLL int MXNDArrayGetDataNDArray(NDArrayHandle handle,
NDArrayHandle *out);
/*!
* \brief get the context of the NDArray
* \param handle the handle to the narray
* \param out_dev_type the output device type
* \param out_dev_id the output device id
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayGetContext(NDArrayHandle handle,
int *out_dev_type,
int *out_dev_id);
/*!
* \brief return gradient buffer attached to this NDArray
* \param handle NDArray handle
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayGetGrad(NDArrayHandle handle, NDArrayHandle *out);
/*!
* \brief detach and ndarray from computation graph by clearing entry_
* \param handle NDArray handle
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayDetach(NDArrayHandle handle, NDArrayHandle *out);
/*!
* \brief set the flag for gradient array state.
* \param handle NDArray handle
* \param state the new state.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArraySetGradState(NDArrayHandle handle, int state);
/*!
* \brief set the flag for gradient array state.
* \param handle NDArray handle
* \param state the new state.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXNDArrayGetGradState(NDArrayHandle handle, int *out);
//--------------------------------
// Part 2: functions on NDArray
//--------------------------------
/*!
* \brief list all the available functions handles
* most user can use it to list all the needed functions
* \param out_size the size of returned array
* \param out_array the output function array
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXListFunctions(mx_uint *out_size,
FunctionHandle **out_array);
/*!
* \brief get the function handle by name
* \param name the name of the function
* \param out the corresponding function handle
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXGetFunction(const char *name,
FunctionHandle *out);
/*!
* \brief Get the information of the function handle.
* \param fun The function handle.
* \param name The returned name of the function.
* \param description The returned description of the function.
* \param num_args Number of arguments.
* \param arg_names Name of the arguments.
* \param arg_type_infos Type information about the arguments.
* \param arg_descriptions Description information about the arguments.
* \param return_type Return type of the function.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXFuncGetInfo(FunctionHandle fun,
const char **name,
const char **description,
mx_uint *num_args,
const char ***arg_names,
const char ***arg_type_infos,
const char ***arg_descriptions,
const char **return_type DEFAULT(NULL));
/*!
* \brief get the argument requirements of the function
* \param fun input function handle
* \param num_use_vars how many NDArrays to be passed in as used_vars
* \param num_scalars scalar variable is needed
* \param num_mutate_vars how many NDArrays to be passed in as mutate_vars
* \param type_mask the type mask of this function
* \return 0 when success, -1 when failure happens
* \sa MXFuncInvoke
*/
MXNET_DLL int MXFuncDescribe(FunctionHandle fun,
mx_uint *num_use_vars,
mx_uint *num_scalars,
mx_uint *num_mutate_vars,
int *type_mask);
/*!
* \brief invoke a function, the array size of passed in arguments
* must match the values in the
* \param fun the function
* \param use_vars the normal arguments passed to function
* \param scalar_args the scalar qarguments
* \param mutate_vars the mutate arguments
* \return 0 when success, -1 when failure happens
* \sa MXFuncDescribeArgs
*/
MXNET_DLL int MXFuncInvoke(FunctionHandle fun,
NDArrayHandle *use_vars,
mx_float *scalar_args,
NDArrayHandle *mutate_vars);
/*!
* \brief invoke a function, the array size of passed in arguments
* must match the values in the
* \param fun the function
* \param use_vars the normal arguments passed to function
* \param scalar_args the scalar qarguments
* \param mutate_vars the mutate arguments
* \param num_params number of keyword parameters
* \param param_keys keys for keyword parameters
* \param param_vals values for keyword parameters
* \return 0 when success, -1 when failure happens
* \sa MXFuncDescribeArgs
*/
MXNET_DLL int MXFuncInvokeEx(FunctionHandle fun,
NDArrayHandle *use_vars,
mx_float *scalar_args,
NDArrayHandle *mutate_vars,
int num_params,
char **param_keys,
char **param_vals);
/*!
* \brief invoke a nnvm op and imperative function
* \param creator the op
* \param num_inputs number of input NDArrays
* \param inputs input NDArrays
* \param num_outputs number of output NDArrays
* \param outputs output NDArrays
* \param num_params number of keyword parameters
* \param param_keys keys for keyword parameters
* \param param_vals values for keyword parameters
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXImperativeInvoke(AtomicSymbolCreator creator,
int num_inputs,
NDArrayHandle *inputs,
int *num_outputs,
NDArrayHandle **outputs,
int num_params,
const char **param_keys,
const char **param_vals);
/*!
* \brief invoke a nnvm op and imperative function
* \param creator the op
* \param num_inputs number of input NDArrays
* \param inputs input NDArrays
* \param num_outputs number of output NDArrays
* \param outputs output NDArrays
* \param num_params number of keyword parameters
* \param param_keys keys for keyword parameters
* \param param_vals values for keyword parameters
* \param out_stypes output ndarrays' stypes
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXImperativeInvokeEx(AtomicSymbolCreator creator,
int num_inputs,
NDArrayHandle *inputs,
int *num_outputs,
NDArrayHandle **outputs,
int num_params,
const char **param_keys,
const char **param_vals,
const int **out_stypes);
/*!
* \brief set whether to record operator for autograd
* \param is_recording 1 when recording, 0 when not recording.
* \param prev returns the previous status before this set.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXAutogradSetIsRecording(int is_recording, int* prev);
/*!
* \brief set whether to record operator for autograd
* \param is_training 1 when training, 0 when testing
* \param prev returns the previous status before this set.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXAutogradSetIsTraining(int is_training, int* prev);
/*!
* \brief get whether autograd recording is on
* \param curr returns the current status.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXAutogradIsRecording(bool* curr);
/*!
* \brief get whether training mode is on
* \param curr returns the current status.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXAutogradIsTraining(bool* curr);
/*!
* \brief mark NDArrays as variables to compute gradient for autograd
* \param num_var number of variable NDArrays
* \param var_handles variable NDArrays
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXAutogradMarkVariables(mx_uint num_var,
NDArrayHandle *var_handles,
mx_uint *reqs_array,
NDArrayHandle *grad_handles);
/*!
* \brief compute the gradient of outputs w.r.t variabels
* \param num_output number of output NDArray
* \param output_handles output NDArrays
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXAutogradComputeGradient(mx_uint num_output,
NDArrayHandle* output_handles);
/*!
* \brief compute the gradient of outputs w.r.t variabels
* \param num_output number of output NDArray
* \param output_handles output NDArrays
* \param ograd_handles head gradient for NDArrays
* \param retain_graph whether to keep the graph after backward
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXAutogradBackward(mx_uint num_output,
NDArrayHandle* output_handles,
NDArrayHandle* ograd_handles,
int retain_graph);
/*!
* \brief compute the gradient of outputs w.r.t variabels
* \param num_output number of output NDArray
* \param output_handles output NDArrays
* \param ograd_handles head gradient for NDArrays
* \param num_variables number of variables
* \param
* \param retain_graph whether to keep the graph after backward
* \param is_train whether to do backward for training or inference
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXAutogradBackwardEx(mx_uint num_output,
NDArrayHandle *output_handles,
NDArrayHandle *ograd_handles,
mx_uint num_variables,
NDArrayHandle *var_handles,
int retain_graph,
int create_graph,
int is_train,
NDArrayHandle **grad_handles,
int **grad_stypes);
/*
* \brief get the graph constructed by autograd.
* \param handle ndarray handle
* \param out output symbol handle
*/
MXNET_DLL int MXAutogradGetSymbol(NDArrayHandle handle, SymbolHandle *out);
/*!
* \brief create cached operator
*/
MXNET_DLL int MXCreateCachedOp(SymbolHandle handle,
CachedOpHandle *out);
/*!
* \brief free cached operator
*/
MXNET_DLL int MXFreeCachedOp(CachedOpHandle handle);
/*!
* \brief invoke cached operator
*/
MXNET_DLL int MXInvokeCachedOp(CachedOpHandle handle,
int num_inputs,
NDArrayHandle *inputs,
int *num_outputs,
NDArrayHandle **outputs);
/*!
* \brief invoke a cached op
* \param handle the handle to the cached op
* \param num_inputs number of input NDArrays
* \param inputs input NDArrays
* \param num_outputs number of output NDArrays
* \param outputs output NDArrays
* \param out_stypes output ndarrays' stypes
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXInvokeCachedOpEx(CachedOpHandle handle,
int num_inputs,
NDArrayHandle *inputs,
int *num_outputs,
NDArrayHandle **outputs,
const int** out_stypes);
/*!
* \brief invoke cached operator
*/
MXNET_DLL int MXInvokeCachedOp(CachedOpHandle handle,
int num_inputs,
NDArrayHandle *inputs,
int *num_outputs,
NDArrayHandle **outputs);
//--------------------------------------------
// Part 3: symbolic configuration generation
//--------------------------------------------
/*!
* \brief list all the available operator names, include entries.
* \param out_size the size of returned array
* \param out_array the output operator name array.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXListAllOpNames(mx_uint *out_size,
const char ***out_array);
/*!
* \brief list all the available AtomicSymbolEntry
* \param out_size the size of returned array
* \param out_array the output AtomicSymbolCreator array
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolListAtomicSymbolCreators(mx_uint *out_size,
AtomicSymbolCreator **out_array);
/*!
* \brief Get the name of an atomic symbol.
* \param creator the AtomicSymbolCreator.
* \param name The returned name of the creator.
*/
MXNET_DLL int MXSymbolGetAtomicSymbolName(AtomicSymbolCreator creator,
const char **name);
/*!
* \brief Get the detailed information about atomic symbol.
* \param creator the AtomicSymbolCreator.
* \param name The returned name of the creator.
* \param description The returned description of the symbol.
* \param num_args Number of arguments.
* \param arg_names Name of the arguments.
* \param arg_type_infos Type informations about the arguments.
* \param arg_descriptions Description information about the arguments.
* \param key_var_num_args The keyword argument for specifying variable number of arguments.
* When this parameter has non-zero length, the function allows variable number
* of positional arguments, and will need the caller to pass it in in
* MXSymbolCreateAtomicSymbol,
* With key = key_var_num_args, and value = number of positional arguments.
* \param return_type Return type of the function, can be Symbol or Symbol[]
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolGetAtomicSymbolInfo(AtomicSymbolCreator creator,
const char **name,
const char **description,
mx_uint *num_args,
const char ***arg_names,
const char ***arg_type_infos,
const char ***arg_descriptions,
const char **key_var_num_args,
const char **return_type DEFAULT(NULL));
/*!
* \brief Create an AtomicSymbol.
* \param creator the AtomicSymbolCreator
* \param num_param the number of parameters
* \param keys the keys to the params
* \param vals the vals of the params
* \param out pointer to the created symbol handle
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolCreateAtomicSymbol(AtomicSymbolCreator creator,
mx_uint num_param,
const char **keys,
const char **vals,
SymbolHandle *out);
/*!
* \brief Create a Variable Symbol.
* \param name name of the variable
* \param out pointer to the created symbol handle
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolCreateVariable(const char *name, SymbolHandle *out);
/*!
* \brief Create a Symbol by grouping list of symbols together
* \param num_symbols number of symbols to be grouped
* \param symbols array of symbol handles
* \param out pointer to the created symbol handle
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolCreateGroup(mx_uint num_symbols,
SymbolHandle *symbols,
SymbolHandle *out);
/*!
* \brief Load a symbol from a json file.
* \param fname the file name.
* \param out the output symbol.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolCreateFromFile(const char *fname, SymbolHandle *out);
/*!
* \brief Load a symbol from a json string.
* \param json the json string.
* \param out the output symbol.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolCreateFromJSON(const char *json, SymbolHandle *out);
/*!
* \brief Save a symbol into a json file.
* \param symbol the input symbol.
* \param fname the file name.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolSaveToFile(SymbolHandle symbol, const char *fname);
/*!
* \brief Save a symbol into a json string
* \param symbol the input symbol.
* \param out_json output json string.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolSaveToJSON(SymbolHandle symbol, const char **out_json);
/*!
* \brief Free the symbol handle.
* \param symbol the symbol
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolFree(SymbolHandle symbol);
/*!
* \brief Copy the symbol to another handle
* \param symbol the source symbol
* \param out used to hold the result of copy
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolCopy(SymbolHandle symbol, SymbolHandle *out);
/*!
* \brief Print the content of symbol, used for debug.
* \param symbol the symbol
* \param out_str pointer to hold the output string of the printing.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolPrint(SymbolHandle symbol, const char **out_str);
/*!
* \brief Get string name from symbol
* \param symbol the source symbol
* \param out The result name.
* \param success Whether the result is contained in out.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolGetName(SymbolHandle symbol,
const char** out,
int *success);
/*!
* \brief Get string attribute from symbol
* \param symbol the source symbol
* \param key The key of the symbol.
* \param out The result attribute, can be NULL if the attribute do not exist.
* \param success Whether the result is contained in out.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolGetAttr(SymbolHandle symbol,
const char* key,
const char** out,
int *success);
/*!
* \brief Set string attribute from symbol.
* NOTE: Setting attribute to a symbol can affect the semantics(mutable/immutable) of symbolic graph.
*
* Safe recommendaton: use immutable graph
* - Only allow set attributes during creation of new symbol as optional parameter
*
* Mutable graph (be careful about the semantics):
* - Allow set attr at any point.
* - Mutating an attribute of some common node of two graphs can cause confusion from user.
*
* \param symbol the source symbol
* \param key The key of the symbol.
* \param value The value to be saved.
* \return 0 when success, -1 when failure happens
*/
MXNET_DLL int MXSymbolSetAttr(SymbolHandle symbol,
const char* key,
const char* value);
/*!
* \brief Get all attributes from symbol, including all descendents.