An OpTestBuilder is a graph builder class that takes as input an operator to test, its inputs and attributes, and builds a graph that executes the operator.
| 94 | // test, its inputs and attributes, and builds a graph that executes the |
| 95 | // operator. |
| 96 | class OpTestBuilder { |
| 97 | public: |
| 98 | explicit OpTestBuilder(const string& op_name); |
| 99 | |
| 100 | // Adds an input 'tensor' as a Placeholder node. |
| 101 | OpTestBuilder& Input(const Tensor& tensor); |
| 102 | |
| 103 | // Adds a random input tensor with 'type' as a Placeholder node. |
| 104 | // If 'dims' is not provided, RandomDims() is used. |
| 105 | OpTestBuilder& RandomInput(DataType type); |
| 106 | OpTestBuilder& RandomInput(DataType type, std::vector<int64> dims); |
| 107 | |
| 108 | // As RandomInput but the values are unique. |
| 109 | OpTestBuilder& RandomUniqueInput(DataType type, std::vector<int64> dims); |
| 110 | |
| 111 | // Sets an attribute. |
| 112 | template <class T> |
| 113 | OpTestBuilder& Attr(absl::string_view attr_name, T&& value); |
| 114 | |
| 115 | // Overload needed to allow {...} expressions for value. |
| 116 | template <class T> |
| 117 | OpTestBuilder& Attr(absl::string_view attr_name, |
| 118 | std::initializer_list<T> value); |
| 119 | |
| 120 | // Adds nodes that executes the operator under test on 'device' to 'graphdef'. |
| 121 | // If 'use_jit' is true, marks the operator under test to be compiled by XLA. |
| 122 | // The graph will consist of one Placeholder node per input, the operator |
| 123 | // itself, and one Identity node per output. If 'test_node_def' is not null, |
| 124 | // sets it to the NodeDef of the operator under test. Fills 'inputs' and |
| 125 | // 'outputs' with the names of the input placeholder nodes and the output |
| 126 | // identity nodes, respectively. |
| 127 | Status BuildGraph(const string& name_prefix, const string& device, |
| 128 | bool use_jit, GraphDef* graphdef, NodeDef** test_node_def, |
| 129 | std::vector<string>* inputs, |
| 130 | std::vector<string>* outputs) const; |
| 131 | |
| 132 | struct InputDescription { |
| 133 | Tensor tensor; |
| 134 | |
| 135 | DataType type = DT_INVALID; |
| 136 | bool has_dims = false; |
| 137 | bool needs_unique_values = false; |
| 138 | std::vector<int64> dims; |
| 139 | }; |
| 140 | |
| 141 | const std::vector<InputDescription>& inputs() const { return inputs_; } |
| 142 | |
| 143 | private: |
| 144 | NodeDef node_def_; |
| 145 | std::vector<InputDescription> inputs_; |
| 146 | }; |
| 147 | |
| 148 | OpTestBuilder::OpTestBuilder(const string& op_name) { |
| 149 | node_def_.set_op(op_name); |