Skip to content

Commit 1b712a5

Browse files
tqchensergei-mironov
authored andcommitted
Update Symbol and C API (apache#22)
* Update tuple to be compatible with mshadow * Move set error message to C API * simplify with using * updates to shape inference * Add unnamed namespace to the implementations * [SYMBOL] Enable inference of Auxiliary data, rename list_arguments to list_inputs
1 parent 75fd5d9 commit 1b712a5

File tree

19 files changed

+346
-105
lines changed

19 files changed

+346
-105
lines changed

nnvm/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
# NNVM: Build deep learning system by parts
22

3-
NNVM is not a deep learning library. It is a modular, decentralized and lightweight library to
4-
help build deep learning libraries efficiently.
3+
NNVM is not a deep learning library. It is a modular, decentralized and lightweight part to
4+
help build deep learning libraries.
55

66
## What is it
77

88
While most deep learning systems offer end to end solutions,
99
it is interesting to ask if we can actually assemble a deep learning system by parts.
1010
The goal is to enable hackers can customize optimizations, target platforms and set of operators they care about.
1111
We believe that the decentralized modular system is an interesting direction.
12+
1213
The hope is that effective parts can be assembled together just like you assemble your own desktops.
1314
So the customized deep learning solution can be minimax, minimum in terms of dependencies,
1415
while maxiziming the users' need.
1516

16-
NNVM offers one such part, it provides a generic to do generic
17-
computation graph optimization such as memory reduction, device allocation,
18-
operator fusion while being agnostic to the operator
19-
interface defintion and how operators are executed.
17+
NNVM offers one such part, it provides a generic way to do
18+
computation graph optimization such as memory reduction, device allocation and more
19+
while being agnostic to the operator interface defintion and how operators are executed.
2020
NNVM is inspired by LLVM, aiming to be an intermediate representation library
2121
for neural nets and computation graphs generation and optimizations.
2222

nnvm/include/nnvm/base.h

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,13 @@
1616
namespace nnvm {
1717

1818
/*! \brief any type */
19-
using any = dmlc::any;
19+
using dmlc::any;
2020

21-
/*!
22-
* \brief array_veiw type
23-
* \tparam ValueType The value content of array view.
24-
*/
25-
template<typename ValueType>
26-
using array_view = dmlc::array_view<ValueType>;
27-
28-
/*!
29-
* \brief get reference of type T stored in src.
30-
* \param src The source container
31-
* \return the reference to the type.
32-
* \tparam T The type to be fetched.
33-
*/
34-
template<typename T>
35-
inline T& get(any& src) { // NOLINT(*)
36-
return dmlc::get<T>(src);
37-
}
38-
39-
/*!
40-
* \brief get const reference of type T stored in src.
41-
* \param src The source container
42-
* \return the reference to the type.
43-
* \tparam T The type to be fetched.
44-
*/
21+
/*! \brief array_veiw type */
22+
using dmlc::array_view;
4523

46-
template<typename T>
47-
inline const T& get(const any& src) {
48-
return dmlc::get<T>(src);
49-
}
24+
/*!\brief getter function of any type */
25+
using dmlc::get;
5026

5127
} // namespace nnvm
5228

nnvm/include/nnvm/c_api.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ typedef void *SymbolHandle;
3535
/*! \brief handle to Graph */
3636
typedef void *GraphHandle;
3737

38+
/*!
39+
* \brief Set the last error message needed by C API
40+
* \param msg The error message to set.
41+
*/
42+
NNVM_DLL void NNAPISetLastError(const char* msg);
43+
3844
/*!
3945
* \brief return str message of the last error
4046
* all function in this file will return 0 when success
@@ -171,25 +177,30 @@ NNVM_DLL int NNSymbolListAttrs(SymbolHandle symbol,
171177
nn_uint *out_size,
172178
const char*** out);
173179
/*!
174-
* \brief List arguments in the symbol.
180+
* \brief List inputs in the symbol.
175181
* \param symbol the symbol
182+
* \param option The option to list the inputs
183+
* option=0 means list all arguments.
184+
* option=1 means list arguments that are readed only by the graph.
185+
* option=2 means list arguments that are mutated by the graph.
176186
* \param out_size output size
177187
* \param out_str_array pointer to hold the output string array
178188
* \return 0 when success, -1 when failure happens
179189
*/
180-
NNVM_DLL int NNSymbolListArguments(SymbolHandle symbol,
181-
nn_uint *out_size,
182-
const char ***out_str_array);
190+
NNVM_DLL int NNSymbolListInputNames(SymbolHandle symbol,
191+
int option,
192+
nn_uint *out_size,
193+
const char ***out_str_array);
183194
/*!
184-
* \brief List returns in the symbol.
195+
* \brief List returns names in the symbol.
185196
* \param symbol the symbol
186197
* \param out_size output size
187198
* \param out_str_array pointer to hold the output string array
188199
* \return 0 when success, -1 when failure happens
189200
*/
190-
NNVM_DLL int NNSymbolListOutputs(SymbolHandle symbol,
191-
nn_uint *out_size,
192-
const char ***out_str_array);
201+
NNVM_DLL int NNSymbolListOutputNames(SymbolHandle symbol,
202+
nn_uint *out_size,
203+
const char ***out_str_array);
193204
/*!
194205
* \brief Get a symbol that contains all the internals.
195206
* \param symbol The symbol

nnvm/include/nnvm/op.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ template<typename ValueType>
289289
inline const OpMap<ValueType>& Op::GetAttr(const std::string& key) {
290290
const any* ref = GetAttrMap(key);
291291
if (ref == nullptr) {
292+
// update the attribute map of the key by creating new empty OpMap
292293
UpdateAttrMap(key, [key](any* pmap) {
294+
// use callback so it is in lockscope
293295
if (pmap->empty()) {
294296
OpMap<ValueType> pm;
295297
pm.attr_name_ = key;
@@ -304,7 +306,9 @@ inline const OpMap<ValueType>& Op::GetAttr(const std::string& key) {
304306
template<typename ValueType>
305307
inline Op& Op::attr( // NOLINT(*)
306308
const std::string& attr_name, const ValueType& value) {
309+
// update the attribute map of the key by creating new empty if needed.
307310
UpdateAttrMap(attr_name, [this, attr_name, value](any* pmap) {
311+
// the callback is in lockscope so is threadsafe.
308312
if (pmap->empty()) {
309313
OpMap<ValueType> pm;
310314
pm.attr_name_ = attr_name;

nnvm/include/nnvm/pass_functions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ inline Graph InferType(Graph graph,
8383
DTypeVector type_args = {},
8484
std::string type_attr_key = "") {
8585
if (type_args.size() != 0) {
86-
graph.attrs["type_args"] = std::make_shared<any>(std::move(type_args));
86+
graph.attrs["dtype_args"] = std::make_shared<any>(std::move(type_args));
8787
}
8888
if (type_attr_key.length() != 0) {
89-
graph.attrs["type_attr_key"] = std::make_shared<any>(std::move(type_attr_key));
89+
graph.attrs["dtype_attr_key"] = std::make_shared<any>(std::move(type_attr_key));
9090
}
9191
return ApplyPass(std::move(graph), {"InferType"});
9292
}

nnvm/include/nnvm/symbolic.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ class Symbol {
3030
/*! \brief only list attributes in current node */
3131
kShallow = 1
3232
};
33+
/*! \brief option passed to ListInputNames */
34+
enum ListInputOption {
35+
/*! \brief list all the arguments */
36+
kAll = 0,
37+
/*! \brief list only read only arguments */
38+
kReadOnlyArgs = 1,
39+
/*!
40+
* \brief List auxiliary states that can be mutated by the graph.
41+
* This excludes the ReadOnly arguments
42+
*/
43+
kAuxiliaryStates = 2
44+
};
3345

3446
/*! \brief output entries contained in the symbol */
3547
std::vector<NodeEntry> outputs;
@@ -51,18 +63,20 @@ class Symbol {
5163
*/
5264
Symbol operator[] (size_t index) const;
5365
/*!
54-
* \brief List the arguments names.
66+
* \brief List the input names.
67+
* \param option The options to list the arguments.
5568
*
5669
* The position of the returned list also corresponds to calling position in operator()
5770
* \return the arguments list of this symbol, they can be either named or unnamed (empty string).
71+
* \sa ListInputOption
5872
*/
59-
std::vector<std::string> ListArguments() const;
73+
std::vector<std::string> ListInputNames(ListInputOption option) const;
6074
/*!
6175
* \brief List the names of outputs for this symbol.
6276
* For normal operators, it is usually symbol node name + "_output"
6377
* \return get the descriptions of outputs for this symbol.
6478
*/
65-
std::vector<std::string> ListOutputs() const;
79+
std::vector<std::string> ListOutputNames() const;
6680
/*!
6781
* \brief Compose the symbol with arguments, this changes the current symbol.
6882
* The kwargs passed in can be in-complete,

0 commit comments

Comments
 (0)