Skip to content

Commit

Permalink
Add graph documentations (#788)
Browse files Browse the repository at this point in the history
* add API docs for expression_graph.h
* change API docs to doxygen-readable format
* add API docs for node_initializers
* update doxygen configure file
* add hyperlinks and remove layers section from graph documentation
* fixing typos and links on graph doc
  • Loading branch information
qianqianzhu authored Feb 28, 2021
1 parent f88ded2 commit 2a9c0bb
Show file tree
Hide file tree
Showing 18 changed files with 940 additions and 159 deletions.
2 changes: 1 addition & 1 deletion Doxyfile.in
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ SHORT_NAMES = NO
# description.)
# The default value is: NO.

JAVADOC_AUTOBRIEF = NO
JAVADOC_AUTOBRIEF = YES

# If the QT_AUTOBRIEF tag is set to YES then doxygen will interpret the first
# line (until the first dot) of a Qt-style comment as the brief description. If
Expand Down
405 changes: 404 additions & 1 deletion doc/graph.md

Large diffs are not rendered by default.

Binary file added doc/images/example1_dot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/images/example1_dot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/images/example1_dot3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/images/example2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/images/graph_example1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/common/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ IPtr<T> INew(Ptr<T> p) {
return IPtr<T>(p);
}

/// enum class DeviceType: defines which device is used for computation
enum class DeviceType : size_t { gpu = 0, cpu = 1 };

struct DeviceId {
Expand Down
8 changes: 8 additions & 0 deletions src/common/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ struct Slice // Python-like slice/index descriptor
};
typedef std::vector<Slice> Slices;

/**
* Shape class mainly defines the shape or dimensionality of the node.
* Basically, Shape is a wrapper of a std::vector. Its size is the number of
* dimension. E.g., shape={2,3} means 2D matrix with dim[0]=2 and dim[1]=3.
* WHen the index is negative, the real index is size() + index.
* It implements most common functions demanded by operations, e.g., resize(),
* slice(), and broadcast().
*/
struct Shape {
private:
std::vector<int> shape_;
Expand Down
61 changes: 31 additions & 30 deletions src/common/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ do { \
default: ABORT("Unknown type {}", type); \
} \
} while(0)

/// namespace marian
namespace marian {

// small struct to enable templating based on types use for packing
Expand Down Expand Up @@ -247,36 +247,37 @@ constexpr inline size_t operator+(size_t val, TypeClass typeClass) {
}

// @TODO: rename to ElementType when things become stable, so it's easier to review
/// enum class Type: stores all supported data type in Marian
enum class Type : size_t {
int8 = TypeClass::signed_type + 1u,
int16 = TypeClass::signed_type + 2u,
int32 = TypeClass::signed_type + 4u,
int64 = TypeClass::signed_type + 8u,

uint8 = TypeClass::unsigned_type + 1u,
uint16 = TypeClass::unsigned_type + 2u,
uint32 = TypeClass::unsigned_type + 4u,
uint64 = TypeClass::unsigned_type + 8u,

float16 = TypeClass::float_type + 2u,
float32 = TypeClass::float_type + 4u,
float64 = TypeClass::float_type + 8u,

packed16 = TypeClass::packed_type + 2u, // special type for FBGEMM, not meant to be used anywhere else, not meant to be accessed invidually. Internal actual type (uint16) is meaningless.
packed8avx2 = TypeClass::packed_type + 1u + TypeClass::avx2_type, // special type for FBGEMM with AVX2, not meant to be used anywhere else, not meant to be accessed invidually. Internal actual type (uint8) is meaningless.
packed8avx512 = TypeClass::packed_type + 1u + TypeClass::avx512_type, // special type for FBGEMM with AVX512, not meant to be used anywhere else, not meant to be accessed invidually. Internal actual type (uint8) is meaningless.

intgemm8 = TypeClass::intgemm_type + 1u, // Int8 quantized (not packed) matrices for intgemm
intgemm16 = TypeClass::intgemm_type + 2u, // Int16 quantized (not packed) matrices for intgemm

intgemm8ssse3 = TypeClass::intgemm_type + 1u + TypeClass::ssse3_type, // Int8 quantized and packed (ssse3) matrices for intgemm
intgemm8avx2 = TypeClass::intgemm_type + 1u + TypeClass::avx2_type, // Int8 quantized and packed (avx2) matrices for intgemm
intgemm8avx512 = TypeClass::intgemm_type + 1u + TypeClass::avx512_type, // Int8 quantized and packed (avx512) matrices for intgemm
intgemm8avx512vnni = TypeClass::intgemm_type + 1u + TypeClass::avx512_type + 4096u, // Int8 quantized and packed (avx512) matrices for intgemm. VNNI algorithm

intgemm16sse2 = TypeClass::intgemm_type + 2u + TypeClass::sse2_type, // Int16 quantized and packed (sse2) matrices for intgemm
intgemm16avx2 = TypeClass::intgemm_type + 2u + TypeClass::avx2_type, // Int16 quantized and packed (avx2) matrices for intgemm
intgemm16avx512 = TypeClass::intgemm_type + 2u + TypeClass::avx512_type, // Int16 quantized and packed (avx512) matrices for intgemm
int8 = TypeClass::signed_type + 1u, ///< int8 type
int16 = TypeClass::signed_type + 2u, ///< int16 type
int32 = TypeClass::signed_type + 4u, ///< int32 type
int64 = TypeClass::signed_type + 8u, ///< int64 type

uint8 = TypeClass::unsigned_type + 1u, ///< uint8 type
uint16 = TypeClass::unsigned_type + 2u, ///< uint16 type
uint32 = TypeClass::unsigned_type + 4u, ///< uint32 type
uint64 = TypeClass::unsigned_type + 8u, ///< uint64 type

float16 = TypeClass::float_type + 2u, ///< float16 type
float32 = TypeClass::float_type + 4u, ///< float32 type
float64 = TypeClass::float_type + 8u, ///< float64 type

packed16 = TypeClass::packed_type + 2u, ///< special type for FBGEMM, not meant to be used anywhere else, not meant to be accessed invidually. Internal actual type (uint16) is meaningless.
packed8avx2 = TypeClass::packed_type + 1u + TypeClass::avx2_type, ///< special type for FBGEMM with AVX2, not meant to be used anywhere else, not meant to be accessed invidually. Internal actual type (uint8) is meaningless.
packed8avx512 = TypeClass::packed_type + 1u + TypeClass::avx512_type, ///< special type for FBGEMM with AVX512, not meant to be used anywhere else, not meant to be accessed invidually. Internal actual type (uint8) is meaningless.

intgemm8 = TypeClass::intgemm_type + 1u, ///< Int8 quantized (not packed) matrices for intgemm
intgemm16 = TypeClass::intgemm_type + 2u, ///< Int16 quantized (not packed) matrices for intgemm
intgemm8ssse3 = TypeClass::intgemm_type + 1u + TypeClass::ssse3_type, ///< Int8 quantized and packed (ssse3) matrices for intgemm
intgemm8avx2 = TypeClass::intgemm_type + 1u + TypeClass::avx2_type, ///< Int8 quantized and packed (avx2) matrices for intgemm
intgemm8avx512 = TypeClass::intgemm_type + 1u + TypeClass::avx512_type, ///< Int8 quantized and packed (avx512) matrices for intgemm
intgemm8avx512vnni = TypeClass::intgemm_type + 1u + TypeClass::avx512_type + 4096u, ///< Int8 quantized and packed (avx512) matrices for intgemm. VNNI algorithm

intgemm16sse2 = TypeClass::intgemm_type + 2u + TypeClass::sse2_type, ///< Int16 quantized and packed (sse2) matrices for intgemm
intgemm16avx2 = TypeClass::intgemm_type + 2u + TypeClass::avx2_type, ///< Int16 quantized and packed (avx2) matrices for intgemm
intgemm16avx512 = TypeClass::intgemm_type + 2u + TypeClass::avx512_type, ///< Int16 quantized and packed (avx512) matrices for intgemm
};

static inline size_t operator&(TypeClass typeClass, Type type) {
Expand Down
16 changes: 13 additions & 3 deletions src/graph/expression_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Expr ExpressionGraph::add(Expr node) {
} else {
node->setId(count_++);

// record in foward graph
// record in forward graph
nodesForward_.push_back(node);

// record in backward graph if training, and keep track of roots
Expand Down Expand Up @@ -143,6 +143,11 @@ void ExpressionGraph::forward(std::list<Expr>& forwardTape, bool finalPass) {
if(inferenceOnly_)
v->children().clear();

// If checkpointing is disabled, keep the memory for forward signals for all nodes.
// If checkpointing is enabled:
// (a) In the forward pass before the backward pass, free the memory for the nodes in the subtape to save memory.
// (b) In the forward calls during the backward pass, keep the memory in the current subtape to accelerate
// gradient computation.
if(checkpointing_ && !finalPass) {
auto subtape = v->getSubtape();
if(subtape) {
Expand Down Expand Up @@ -171,12 +176,14 @@ void ExpressionGraph::backward(bool reset, float clipValue) {
ABORT("Aborting");
}

// allocates memory and initialises gradients for parameters
for(auto kvParams : paramsByElementType_) {
kvParams.second->allocateBackward();
if(reset)
kvParams.second->set_zero_adjoint();
}

// for top nodes: allocates memory and initialise gradients to 1
for(auto&& v : topNodes_)
v->init_dependent();

Expand All @@ -186,13 +193,16 @@ void ExpressionGraph::backward(bool reset, float clipValue) {

bool firstNaN = true;
while(!nodesBackward_.empty()) {
auto v = nodesBackward_.back();
nodesBackward_.pop_back();
auto v = nodesBackward_.back(); // return the last element
nodesBackward_.pop_back(); // remove the last element

// for non-top nodes: allocates memory and initialises gradients to 0
for(auto&& child : v->children())
if(child->trainable() && child->type() != "param")
child->set_zero_adjoint();

// if using gradient checkpointing,
// recompute the forward pass from checkpoint to the root
if(checkpointing_ && v->getSubtape()) {
forward(*v->getSubtape(), /*finalPass=*/true);
}
Expand Down
Loading

0 comments on commit 2a9c0bb

Please sign in to comment.