Skip to content

Commit 1800e56

Browse files
committed
[C API] add in C API for symbolic (#3)
1 parent f3fdd9d commit 1800e56

File tree

8 files changed

+544
-22
lines changed

8 files changed

+544
-22
lines changed

nnvm/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export CFLAGS = -std=c++11 -Wall -O3 -msse2 -Wno-unknown-pragmas -funroll-loop
55
# specify tensor path
66
.PHONY: clean all test lint doc
77

8-
all: lib/libnngraph.so lib/libnngraph.a cli_test
8+
all: lib/libnnvm.so lib/libnnvm.a cli_test
99

1010
SRC = $(wildcard src/*.cc src/*/*.cc example/*.cc)
1111
ALL_OBJ = $(patsubst src/%.cc, build/%.o, $(SRC))
@@ -20,19 +20,19 @@ build/%.o: src/%.cc
2020
$(CXX) $(CFLAGS) -MM -MT build/$*.o $< >build/$*.d
2121
$(CXX) -c $(CFLAGS) -c $< -o $@
2222

23-
lib/libnngraph.so: $(ALL_DEP)
23+
lib/libnnvm.so: $(ALL_DEP)
2424
@mkdir -p $(@D)
2525
$(CXX) $(CFLAGS) -shared -o $@ $(filter %.o %.a, $^) $(LDFLAGS)
2626

27-
lib/libnngraph.a: $(ALL_DEP)
27+
lib/libnnvm.a: $(ALL_DEP)
2828
@mkdir -p $(@D)
2929
ar crv $@ $(filter %.o, $?)
3030

3131
cli_test: $(ALL_DEP) build/test_main.o
3232
$(CXX) $(CFLAGS) -o $@ $(filter %.o %.a, $^) $(LDFLAGS)
3333

3434
lint:
35-
python2 dmlc-core/scripts/lint.py nngraph cpp include src
35+
python2 dmlc-core/scripts/lint.py nnvm cpp include src
3636

3737
doc:
3838
doxygen docs/Doxyfile

nnvm/include/nnvm/c_api.h

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/*!
2+
* Copyright (c) 2016 by Contributors
3+
* \file c_api.h
4+
* \brief C API of NNVM symbolic construction and pass.
5+
* Enables construction and transformation of Graph
6+
* in any other host languages.
7+
*/
8+
#ifndef NNVM_C_API_H_
9+
#define NNVM_C_API_H_
10+
11+
#ifdef __cplusplus
12+
#define NNVM_EXTERN_C extern "C"
13+
#endif
14+
15+
/*! \brief NNVM_DLL prefix for windows */
16+
#ifdef _WIN32
17+
#ifdef NNVM_EXPORTS
18+
#define NNVM_DLL NNVM_EXTERN_C __declspec(dllexport)
19+
#else
20+
#define NNVM_DLL NNVM_EXTERN_C __declspec(dllimport)
21+
#endif
22+
#else
23+
#define NNVM_DLL NNVM_EXTERN_C
24+
#endif
25+
26+
/*! \brief manually define unsigned int */
27+
typedef unsigned int nn_uint;
28+
29+
/*! \brief handle to a function that takes param and creates symbol */
30+
typedef void *AtomicSymbolCreator;
31+
/*! \brief handle to a symbol that can be bind as operator */
32+
typedef void *SymbolHandle;
33+
/*! \brief handle to a AtomicSymbol */
34+
typedef void *AtomicSymbolHandle;
35+
36+
/*!
37+
* \brief return str message of the last error
38+
* all function in this file will return 0 when success
39+
* and -1 when an error occured,
40+
* NNGetLastError can be called to retrieve the error
41+
*
42+
* this function is threadsafe and can be called by different thread
43+
* \return error info
44+
*/
45+
NNVM_DLL const char *NNGetLastError();
46+
47+
/*!
48+
* \brief list all the available AtomicSymbolEntry
49+
* \param out_size the size of returned array
50+
* \param out_array the output AtomicSymbolCreator array
51+
* \return 0 when success, -1 when failure happens
52+
*/
53+
NNVM_DLL int NNSymbolListAtomicSymbolCreators(nn_uint *out_size,
54+
AtomicSymbolCreator **out_array);
55+
/*!
56+
* \brief Get the detailed information about atomic symbol.
57+
* \param creator the AtomicSymbolCreator.
58+
* \param name The returned name of the creator.
59+
* \param description The returned description of the symbol.
60+
* \param num_doc_args Number of arguments that contain documents.
61+
* \param arg_names Name of the arguments of doc args
62+
* \param arg_type_infos Type informations about the arguments.
63+
* \param arg_descriptions Description information about the arguments.
64+
* \param return_type Return type of the function, if any.
65+
* \return 0 when success, -1 when failure happens
66+
*/
67+
NNVM_DLL int NNSymbolGetAtomicSymbolInfo(AtomicSymbolCreator creator,
68+
const char **name,
69+
const char **description,
70+
nn_uint *num_doc_args,
71+
const char ***arg_names,
72+
const char ***arg_type_infos,
73+
const char ***arg_descriptions,
74+
const char **return_type = NULL);
75+
/*!
76+
* \brief Create an AtomicSymbol functor.
77+
* \param creator the AtomicSymbolCreator
78+
* \param num_param the number of parameters
79+
* \param keys the keys to the params
80+
* \param vals the vals of the params
81+
* \param out pointer to the created symbol handle
82+
* \return 0 when success, -1 when failure happens
83+
*/
84+
NNVM_DLL int NNSymbolCreateAtomicSymbol(AtomicSymbolCreator creator,
85+
nn_uint num_param,
86+
const char **keys,
87+
const char **vals,
88+
SymbolHandle *out);
89+
/*!
90+
* \brief Create a Variable Symbol.
91+
* \param name name of the variable
92+
* \param out pointer to the created symbol handle
93+
* \return 0 when success, -1 when failure happens
94+
*/
95+
NNVM_DLL int NNSymbolCreateVariable(const char *name, SymbolHandle *out);
96+
/*!
97+
* \brief Create a Symbol by grouping list of symbols together
98+
* \param num_symbols number of symbols to be grouped
99+
* \param symbols array of symbol handles
100+
* \param out pointer to the created symbol handle
101+
* \return 0 when success, -1 when failure happens
102+
*/
103+
NNVM_DLL int NNSymbolCreateGroup(nn_uint num_symbols,
104+
SymbolHandle *symbols,
105+
SymbolHandle *out);
106+
/*!
107+
* \brief Free the symbol handle.
108+
* \param symbol the symbol
109+
* \return 0 when success, -1 when failure happens
110+
*/
111+
NNVM_DLL int NNSymbolFree(SymbolHandle symbol);
112+
/*!
113+
* \brief Copy the symbol to another handle
114+
* \param symbol the source symbol
115+
* \param out used to hold the result of copy
116+
* \return 0 when success, -1 when failure happens
117+
*/
118+
NNVM_DLL int NNSymbolCopy(SymbolHandle symbol, SymbolHandle *out);
119+
/*!
120+
* \brief Print the content of symbol, used for debug.
121+
* \param symbol the symbol
122+
* \param out_str pointer to hold the output string of the printing.
123+
* \return 0 when success, -1 when failure happens
124+
*/
125+
NNVM_DLL int NNSymbolPrint(SymbolHandle symbol, const char **out_str);
126+
127+
/*!
128+
* \brief Set string attribute from symbol.
129+
* NOTE: Setting attribute to a symbol can affect the semantics(mutable/immutable) of symbolic graph.
130+
*
131+
* Safe recommendaton: use immutable graph
132+
* - Only allow set attributes during creation of new symbol as optional parameter
133+
*
134+
* Mutable graph (be careful about the semantics):
135+
* - Allow set attr at any point.
136+
* - Mutating an attribute of some common node of two graphs can cause confusion from user.
137+
*
138+
* \param symbol the source symbol
139+
* \param num_param Number of parameters to set.
140+
* \param keys The keys of the attribute
141+
* \param values The value to be set
142+
* \return 0 when success, -1 when failure happens
143+
*/
144+
NNVM_DLL int NNSymbolSetAttrs(SymbolHandle symbol,
145+
nn_uint num_param,
146+
const char** keys,
147+
const char** values);
148+
/*!
149+
* \brief Get all attributes from symbol, including all descendents.
150+
* \param symbol the source symbol
151+
* \param recursive_option 0 for recursive, 1 for shallow.
152+
* \param out_size The number of output attributes
153+
* \param out 2*out_size strings representing key value pairs.
154+
* \return 0 when success, -1 when failure happens
155+
*/
156+
NNVM_DLL int NNSymbolListAttrs(SymbolHandle symbol,
157+
int recursive_option,
158+
nn_uint *out_size,
159+
const char*** out);
160+
/*!
161+
* \brief List arguments in the symbol.
162+
* \param symbol the symbol
163+
* \param out_size output size
164+
* \param out_str_array pointer to hold the output string array
165+
* \return 0 when success, -1 when failure happens
166+
*/
167+
NNVM_DLL int NNSymbolListArguments(SymbolHandle symbol,
168+
nn_uint *out_size,
169+
const char ***out_str_array);
170+
/*!
171+
* \brief List returns in the symbol.
172+
* \param symbol the symbol
173+
* \param out_size output size
174+
* \param out_str_array pointer to hold the output string array
175+
* \return 0 when success, -1 when failure happens
176+
*/
177+
NNVM_DLL int NNSymbolListOutputs(SymbolHandle symbol,
178+
nn_uint *out_size,
179+
const char ***out_str_array);
180+
/*!
181+
* \brief Get a symbol that contains all the internals.
182+
* \param symbol The symbol
183+
* \param out The output symbol whose outputs are all the internals.
184+
* \return 0 when success, -1 when failure happens
185+
*/
186+
NNVM_DLL int NNSymbolGetInternals(SymbolHandle symbol,
187+
SymbolHandle *out);
188+
/*!
189+
* \brief Get index-th outputs of the symbol.
190+
* \param symbol The symbol
191+
* \param index the Index of the output.
192+
* \param out The output symbol whose outputs are the index-th symbol.
193+
* \return 0 when success, -1 when failure happens
194+
*/
195+
NNVM_DLL int NNSymbolGetOutput(SymbolHandle symbol,
196+
nn_uint index,
197+
SymbolHandle *out);
198+
199+
/*!
200+
* \brief Compose the symbol on other symbols.
201+
*
202+
* This function will change the sym hanlde.
203+
* To achieve function apply behavior, copy the symbol first
204+
* before apply.
205+
*
206+
* \param sym the symbol to apply
207+
* \param name the name of symbol
208+
* \param num_args number of arguments
209+
* \param keys the key of keyword args (optional)
210+
* \param args arguments to sym
211+
* \return 0 when success, -1 when failure happens
212+
*/
213+
NNVM_DLL int NNSymbolCompose(SymbolHandle sym,
214+
const char* name,
215+
nn_uint num_args,
216+
const char** keys,
217+
SymbolHandle* args);
218+
219+
#endif // NNVM_C_API_H_

nnvm/include/nnvm/op.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ class Op {
7575
public:
7676
/*! \brief name of the operator */
7777
std::string name;
78-
/*! \brief detailed description of the operator */
78+
/*!
79+
* \brief detailed description of the operator
80+
* This can be used to generate docstring automatically for the operator.
81+
*/
7982
std::string description;
8083
/*!
8184
* \brief number of inputs to the operator,
@@ -339,7 +342,7 @@ inline Op& Op::set_num_inputs(uint32_t n) { // NOLINT(*)
339342
return *this;
340343
}
341344

342-
inline Op& Op::set_num_inputs(uint32_t (*fn)(const NodeAttrs&)) { // NOLINT(*)
345+
inline Op& Op::set_num_inputs(uint32_t (*fn)(const NodeAttrs& attr)) { // NOLINT(*)
343346
this->get_num_inputs = fn;
344347
return *this;
345348
}
@@ -349,7 +352,7 @@ inline Op& Op::set_num_outputs(uint32_t n) { // NOLINT(*)
349352
return *this;
350353
}
351354

352-
inline Op& Op::set_num_outputs(uint32_t (*fn)(const NodeAttrs&)) { // NOLINT(*)
355+
inline Op& Op::set_num_outputs(uint32_t (*fn)(const NodeAttrs& attr)) { // NOLINT(*)
353356
this->get_num_outputs = fn;
354357
return *this;
355358
}

nnvm/include/nnvm/symbolic.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class Symbol {
2626
/*! \brief option passed to ListAttr */
2727
enum ListAttrOption {
2828
/*! \brief recursively list all attributes */
29-
kRecursive,
29+
kRecursive = 0,
3030
/*! \brief only list attributes in current node */
31-
kShallow
31+
kShallow = 1
3232
};
3333

3434
/*! \brief output entries contained in the symbol */
@@ -69,7 +69,7 @@ class Symbol {
6969
*
7070
* The rest of the symbols will remain the same name.
7171
*
72-
* \param positional arguments
72+
* \param args positional arguments
7373
* \param kwargs keyword arguments for the symbol
7474
* \param name name of returned symbol.
7575
*/
@@ -108,8 +108,7 @@ class Symbol {
108108
*
109109
* This function mutate the node's symbol and is not recommended.
110110
*
111-
* \param key the key of the attribute
112-
* \param value the value of the attribute.
111+
* \param attrs The attributes to set.
113112
*/
114113
void SetAttrs(const std::vector<std::pair<std::string, std::string> >& attrs);
115114
/*!
@@ -119,16 +118,15 @@ class Symbol {
119118
* The name of symbol will be pre-pended to each key.
120119
* \return The created attribute.
121120
*/
122-
std::unordered_map<std::string, std::string> ListAttr(ListAttrOption option) const;
121+
std::unordered_map<std::string, std::string> ListAttrs(ListAttrOption option) const;
123122
/*!
124123
* \brief create symbolic functor(AtomicSymbol) by given operator and attributes.
125-
* \param op_name The name of the operator.
124+
* \param op The operator.
126125
* \param attrs The additional attributes.
127-
*
128126
* \return Symbol that can be used to call compose further.
129127
*/
130-
static Symbol CreateFunctor(const std::string& op_name,
131-
const std::unordered_map<std::string, std::string>& attrs);
128+
static Symbol CreateFunctor(const Op* op,
129+
std::unordered_map<std::string, std::string>&& attrs);
132130
/*!
133131
* \brief create variable symbol node
134132
* \param name name of the variable

nnvm/src/c_api/c_api_common.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*!
2+
* Copyright (c) 2016 by Contributors
3+
* \file c_api_error.h
4+
* \brief Common fields of all C APIs
5+
*/
6+
#ifndef NNVM_C_API_C_API_COMMON_H_
7+
#define NNVM_C_API_C_API_COMMON_H_
8+
9+
#include <dmlc/base.h>
10+
#include <dmlc/logging.h>
11+
#include <dmlc/thread_local.h>
12+
#include <nnvm/c_api.h>
13+
#include <vector>
14+
#include <string>
15+
16+
/*! \brief macro to guard beginning and end section of all functions */
17+
#define API_BEGIN() try {
18+
/*! \brief every function starts with API_BEGIN();
19+
and finishes with API_END() or API_END_HANDLE_ERROR */
20+
#define API_END() } catch(dmlc::Error &_except_) { return NNAPIHandleException(_except_); } return 0; // NOLINT(*)
21+
/*!
22+
* \brief every function starts with API_BEGIN();
23+
* and finishes with API_END() or API_END_HANDLE_ERROR
24+
* The finally clause contains procedure to cleanup states when an error happens.
25+
*/
26+
#define API_END_HANDLE_ERROR(Finalize) } catch(dmlc::Error &_except_) { Finalize; return NNAPIHandleException(_except_); } return 0; // NOLINT(*)
27+
28+
29+
/*! \brief entry to to easily hold returning information */
30+
struct NNAPIThreadLocalEntry {
31+
/*! \brief result holder for returning string */
32+
std::string ret_str;
33+
/*! \brief result holder for returning strings */
34+
std::vector<std::string> ret_vec_str;
35+
/*! \brief result holder for returning string pointers */
36+
std::vector<const char *> ret_vec_charp;
37+
/*! \brief result holder for returning handles */
38+
std::vector<void *> ret_handles;
39+
};
40+
41+
/*! \brief Thread local store that can be used to hold return values. */
42+
typedef dmlc::ThreadLocalStore<NNAPIThreadLocalEntry> NNAPIThreadLocalStore;
43+
44+
/*!
45+
* \brief Set the last error message needed by C API
46+
* \param msg The error message to set.
47+
*/
48+
void NNAPISetLastError(const char* msg);
49+
/*!
50+
* \brief handle exception throwed out
51+
* \param e the exception
52+
* \return the return value of API after exception is handled
53+
*/
54+
inline int NNAPIHandleException(const dmlc::Error &e) {
55+
NNAPISetLastError(e.what());
56+
return -1;
57+
}
58+
59+
#endif // NNVM_C_API_C_API_COMMON_H_

0 commit comments

Comments
 (0)