|
| 1 | +/*! |
| 2 | + * Copyright (c) 2016 by Contributors |
| 3 | + * \file pass_functions.h |
| 4 | + * \brief Pass functions that simply redirect the calls to ApplyPass |
| 5 | + * |
| 6 | + * This file serves as documentation on how to use functions implemented in "src/pass". |
| 7 | + * It is totally optional to add these functions when you add a new pass, since |
| 8 | + * ApplyPass can be directly called. |
| 9 | + */ |
| 10 | +#ifndef NNVM_PASS_FUNCTIONS_H_ |
| 11 | +#define NNVM_PASS_FUNCTIONS_H_ |
| 12 | + |
| 13 | +#include <string> |
| 14 | +#include <memory> |
| 15 | +#include "./base.h" |
| 16 | +#include "./pass.h" |
| 17 | +#include "./graph_attr_types.h" |
| 18 | + |
| 19 | +namespace nnvm { |
| 20 | +namespace pass { |
| 21 | + |
| 22 | +/*! |
| 23 | + * \brief Load a graph from JSON string, redirects to "LoadJSON" pass. |
| 24 | + * \param json_str The json string. |
| 25 | + * \return Loaded graph. |
| 26 | + */ |
| 27 | +inline Graph LoadJSON(const std::string& json_str) { |
| 28 | + Graph ret; |
| 29 | + ret.attrs["json"] = std::make_shared<any>(json_str); |
| 30 | + return ApplyPass(ret, {"LoadJSON"}); |
| 31 | +} |
| 32 | + |
| 33 | +/*! |
| 34 | + * \brief Save a graph to json, redirects to "SaveJSON" pass. |
| 35 | + * \param graph The to be saved. |
| 36 | + * \return The json string. |
| 37 | + */ |
| 38 | +inline std::string SaveJSON(Graph graph) { |
| 39 | + Graph ret = ApplyPass(std::move(graph), {"SaveJSON"}); |
| 40 | + return ret.GetAttr<std::string>("json"); |
| 41 | +} |
| 42 | + |
| 43 | +/*! |
| 44 | + * \brief Add control flow dependencies between nodes |
| 45 | + * To correctly order mutation and read to resolve |
| 46 | + * write after read problem and read after write problems. |
| 47 | + * \param src source graph |
| 48 | + * \return A graph that added control flow dependencies. |
| 49 | + */ |
| 50 | +inline Graph OrderMutation(Graph src) { |
| 51 | + return ApplyPass(std::move(src), {"OrderMutation"}); |
| 52 | +} |
| 53 | + |
| 54 | +/*! |
| 55 | + * \brief Infer shapes in the graph given the information. |
| 56 | + * \param graph source graph |
| 57 | + * \param shape_args The shapes of aruguments to the graph. |
| 58 | + * \param shape_attr_key The key to the node attribute that can indicate shape. |
| 59 | + * \return A graph with new attribute "shape" containing inferred shape of each NodeEntry. |
| 60 | + * The index of ShapeVector is given by graph.indexed_graph().entry_id |
| 61 | + */ |
| 62 | +inline Graph InferShape(Graph graph, |
| 63 | + ShapeVector shape_args = {}, |
| 64 | + std::string shape_attr_key = "") { |
| 65 | + if (shape_args.size() != 0) { |
| 66 | + graph.attrs["shape_args"] = std::make_shared<any>(std::move(shape_args)); |
| 67 | + } |
| 68 | + if (shape_attr_key.length() != 0) { |
| 69 | + graph.attrs["shape_attr_key"] = std::make_shared<any>(std::move(shape_attr_key)); |
| 70 | + } |
| 71 | + return ApplyPass(std::move(graph), {"InferShape"}); |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace pass |
| 75 | +} // namespace nnvm |
| 76 | +#endif // NNVM_PASS_FUNCTIONS_H_ |
0 commit comments