This is a helper for creating a Node and adding it to a Graph. Internally, it uses a NodeDefBuilder to automatically set attrs that can be inferred from the inputs, and use default values (where they exist) for unspecified attrs. Example usage: Node* node; Status status = NodeBuilder(node_name, op_name) .Input(...) .Attr(...) .Finalize(&graph, &node); if (!status.ok()) return status; // Use node
| 40 | // if (!status.ok()) return status; |
| 41 | // // Use node here. |
| 42 | class NodeBuilder { |
| 43 | public: |
| 44 | // For specifying the output of a Node to provide to one of the Input() |
| 45 | // functions below. It supports both regular inputs (where you are |
| 46 | // connecting to an existing Node*), and inputs from outside the graph |
| 47 | // (or haven't been added to the graph yet, like back edges, where |
| 48 | // you don't have a Node*). Both types can be mixed, e.g. in an |
| 49 | // ArraySlice. |
| 50 | struct NodeOut { |
| 51 | // For referencing an existing Node. |
| 52 | NodeOut(Node* n, int32 i = 0); |
| 53 | NodeOut(OutputTensor t); |
| 54 | |
| 55 | // For referencing Nodes not in the graph being built. It is |
| 56 | // useful when preparing a graph for ExtendSession or creating a |
| 57 | // back edge to a node that hasn't been added to the graph yet, |
| 58 | // but will be. |
| 59 | NodeOut(StringPiece name, int32 i, DataType t); |
| 60 | |
| 61 | // Default constructor for std::vector<NodeOut>. |
| 62 | NodeOut(); |
| 63 | |
| 64 | Node* node; |
| 65 | // error is set to true if: |
| 66 | // * the NodeOut was default constructed and never overwritten, |
| 67 | // * a nullptr Node* was passed to the NodeOut constructor, or |
| 68 | // * an out-of-range index was passed to the NodeOut constructor. |
| 69 | bool error; |
| 70 | string name; |
| 71 | int32 index; |
| 72 | DataType dt; |
| 73 | }; |
| 74 | |
| 75 | // Specify the name and the Op (either via an OpDef or the name of |
| 76 | // the Op plus a registry) for the Node. Other fields are |
| 77 | // specified by calling the methods below. |
| 78 | // REQUIRES: The OpDef must satisfy ValidateOpDef(). |
| 79 | NodeBuilder(StringPiece name, StringPiece op_name, |
| 80 | const OpRegistryInterface* op_registry = OpRegistry::Global(), |
| 81 | const NodeDebugInfo* debug = nullptr); |
| 82 | NodeBuilder(StringPiece name, const OpDef* op_def); |
| 83 | |
| 84 | // Create a NodeBuilder from an existing NodeDefBuilder. |
| 85 | NodeBuilder(const NodeDefBuilder& def_builder); |
| 86 | |
| 87 | // You must call one Input() function per input_arg in the Op, |
| 88 | // *and in the same order as the input_args appear in the OpDef.* |
| 89 | |
| 90 | // For inputs that take a single tensor. |
| 91 | NodeBuilder& Input(Node* src_node, int src_index = 0); |
| 92 | NodeBuilder& Input(NodeOut src); |
| 93 | |
| 94 | // For inputs that take a list of tensors. |
| 95 | NodeBuilder& Input(gtl::ArraySlice<NodeOut> src_list); |
| 96 | |
| 97 | // Require that this node run after src_node(s). |
| 98 | NodeBuilder& ControlInput(Node* src_node); |
| 99 | NodeBuilder& ControlInputs(gtl::ArraySlice<Node*> src_nodes); |