forked from dmlc/nnvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
240 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,8 @@ | |
*.exe | ||
*.out | ||
*.app | ||
build | ||
lib | ||
*~ | ||
test | ||
dmlc-core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "dmlc-core"] | ||
path = dmlc-core | ||
url = https://github.com/dmlc/dmlc-core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export LDFLAGS= -pthread -lm | ||
export CFLAGS= -std=c++11 -Wall -O3 -msse2 -Wno-unknown-pragmas -funroll-loops\ | ||
-Iinclude -Idmlc-core/include -fPIC | ||
|
||
# specify tensor path | ||
.PHONY: clean all test | ||
|
||
|
||
all: lib/libnngraph.so test | ||
|
||
SRC = $(wildcard src/*.cc src/*/*.cc) | ||
ALL_OBJ = $(patsubst src/%.cc, build/%.o, $(SRC)) $(PLUGIN_OBJS) | ||
ALL_DEP = $(ALL_OBJ) | ||
|
||
build/%.o: src/%.cc | ||
@mkdir -p $(@D) | ||
$(CXX) $(CFLAGS) -MM -MT build/$*.o $< >build/$*.d | ||
$(CXX) -c $(CFLAGS) -c $< -o $@ | ||
|
||
lib/libnngraph.so: $(ALL_DEP) | ||
@mkdir -p $(@D) | ||
$(CXX) $(CFLAGS) -shared -o $@ $(filter %.o %.a, $^) $(LDFLAGS) | ||
|
||
test: $(ALL_DEP) | ||
$(CXX) $(CFLAGS) -o $@ $(filter %.o %.a, $^) $(LDFLAGS) | ||
|
||
clean: | ||
$(RM) -rf build lib bin *~ */*~ */*/*~ */*/*/*~ */*.o */*/*.o */*/*/*.o xgboost | ||
|
||
-include build/*.d | ||
-include build/*/*.d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/*! | ||
* Copyright (c) 2016 by Contributors | ||
* \file attr_frame.h | ||
* \brief Attribute frame data structure for properties in the graph. | ||
* This data structure is inspired by data_frame for general. | ||
*/ | ||
#include "./base.h" | ||
|
||
namespace nngraph { | ||
|
||
|
||
struct AttrFrame { | ||
std::unique_ptr<std::unordered_map<std::string, any> > info; | ||
}; | ||
|
||
} // namespace nngraph |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/*! | ||
* Copyright (c) 2016 by Contributors | ||
* \file base.h | ||
* \brief Configuation of nngraph as well as basic data structure. | ||
*/ | ||
#ifndef NNGRAPH_BASE_H_ | ||
#define NNGRAPH_BASE_H_ | ||
|
||
#include <dmlc/base.h> | ||
#include <dmlc/any.h> | ||
#include <dmlc/logging.h> | ||
|
||
namespace nngraph { | ||
|
||
/*! \brief any type */ | ||
using any = dmlc::any; | ||
|
||
/*! | ||
* \brief get reference of type T stored in src. | ||
* \param src The source container | ||
* \return the reference to the type. | ||
* \tparam T The type to be fetched. | ||
*/ | ||
template<typename T> | ||
inline T& get(any& src) { // NOLINT(*) | ||
return dmlc::get<T>(src); | ||
} | ||
|
||
/*! | ||
* \brief get const reference of type T stored in src. | ||
* \param src The source container | ||
* \return the reference to the type. | ||
* \tparam T The type to be fetched. | ||
*/ | ||
|
||
template<typename T> | ||
inline const T& get(const any& src) { | ||
return dmlc::get<T>(src); | ||
} | ||
|
||
} // namespace nngraph | ||
#endif // NNGRAPH_BASE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/*! | ||
* Copyright (c) 2016 by Contributors | ||
* \file graph.h | ||
* \brief Configuation of nngraph as well as basic data structure. | ||
*/ | ||
#ifndef NNGRAPH_GRAPH_H_ | ||
#define NNGRAPH_GRAPH_H_ | ||
|
||
#include <vector> | ||
#include "./node.h" | ||
#include "./attr_frame.h" | ||
|
||
namespace nngraph { | ||
/*! | ||
* \brief Symbolic computation graph. | ||
*/ | ||
class Graph { | ||
public: | ||
/*! | ||
* \brief get the index th element from the returned tuple. | ||
* \param index index of multi output | ||
* \return the symbol corresponds to the indexed element. | ||
*/ | ||
Graph operator[] (size_t index) const; | ||
/*! | ||
* \brief get number of outputs of this symbol | ||
* \return number of outputs | ||
*/ | ||
inline size_t outputs_size() const { | ||
return outputs_.size(); | ||
} | ||
|
||
private: | ||
/*! \brief outputs of the graph. */ | ||
std::vector<NodeEntry> outputs_; | ||
/*! \brief additional internal attribute */ | ||
AttrFrame attr_frame_; | ||
}; | ||
|
||
} // namespace nngraph | ||
|
||
#endif // NNGRAPH_GRAPH_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/*! | ||
* Copyright (c) 2016 by Contributors | ||
* \file base.h | ||
* \brief Configuation of nngraph as well as basic data structure. | ||
*/ | ||
#ifndef NNGRAPH_NODE_H_ | ||
#define NNGRAPH_NODE_H_ | ||
|
||
#include <memory> | ||
#include <unordered_map> | ||
#include "./op_prop.h" | ||
|
||
namespace nngraph { | ||
// Forward declare node. | ||
struct Node; | ||
|
||
/*! \brief an entry that represents output data from a node */ | ||
struct NodeEntry { | ||
/*! \brief the source node of this data */ | ||
std::shared_ptr<Node> node; | ||
/*! \brief index of output from the source. */ | ||
uint32_t index; | ||
}; | ||
|
||
/*! | ||
* \brief Node represents an operation in a computation graph. | ||
*/ | ||
struct Node { | ||
/*! \brief name of the node */ | ||
std::string name; | ||
/*! \brief the operator this node is pointing at */ | ||
const OpProperty *op; | ||
/*! \brief inputs to this node */ | ||
std::vector<NodeEntry> inputs; | ||
/*! | ||
* \brief additional attributes about the node, | ||
* Use pointer to save space, as attr can be accessed in a slow way, | ||
* not every node will have attributes. | ||
*/ | ||
std::unordered_map<std::string, std::string> attr; | ||
|
||
~Node() { | ||
if (inputs.size() != 0) { | ||
// explicit deletion via DFS | ||
// this is used to avoid stackoverflow caused by chain of deletions | ||
std::vector<Node*> stack{this}; | ||
std::vector<std::shared_ptr<Node> > to_delete; | ||
while (!stack.empty()) { | ||
Node* n = stack.back(); | ||
stack.pop_back(); | ||
for (NodeEntry& e: n->inputs) { | ||
if (e.node.unique()) { | ||
stack.push_back(e.node.get()); | ||
to_delete.emplace_back(std::move(e.node)); | ||
} else { | ||
e.node.reset(); | ||
} | ||
} | ||
n->inputs.clear(); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
} // namespace nngraph | ||
|
||
#endif // NNGRAPH_NODE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/*! | ||
* Copyright (c) 2016 by Contributors | ||
* \file op_prop.h | ||
* \brief Data structure about property of operators | ||
*/ | ||
#ifndef NNGRAPH_OP_PROP_H_ | ||
#define NNGRAPH_OP_PROP_H_ | ||
|
||
namespace nngraph { | ||
|
||
/*! | ||
* \brief operator specific data structure | ||
*/ | ||
struct OpProperty { | ||
/*! \brief name of the operator */ | ||
std::string name; | ||
/*! \brief number of inputs to the operator */ | ||
int num_inputs; | ||
/*! \brief number of outputs to the operator */ | ||
int num_outputs; | ||
}; | ||
|
||
|
||
} // namespace nngraph | ||
|
||
#endif // NNGRAPH_OP_PROP_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include <nngraph/graph.h> | ||
|
||
int main() { | ||
nngraph::any a = 1; | ||
LOG(INFO) << nngraph::get<int>(a); | ||
return 0; | ||
} |