| 255 | } |
| 256 | |
| 257 | Status SetProducer(NodeId producer, ValueId value) final { |
| 258 | ValueDef* v; |
| 259 | RETURN_IF_ERROR(LookupValue(value, &v)); |
| 260 | Value<TensorT>* value_ptr = v->value.get(); |
| 261 | NodeDef* n; |
| 262 | RETURN_IF_ERROR(LookupNode(producer, &n)); |
| 263 | Node* node_ptr = n->node.get(); |
| 264 | |
| 265 | // check if this value has the same producer already |
| 266 | if (node_ptr == v->producer) { |
| 267 | return InvalidArgumentError("Node is already a producer of the value"); |
| 268 | } |
| 269 | |
| 270 | // Check if the node is a consumer of this value. |
| 271 | if (std::find(n->inputs.begin(), n->inputs.end(), value_ptr) != |
| 272 | n->inputs.end()) { |
| 273 | return InvalidArgumentError("Node is a consumer of the value"); |
| 274 | } |
| 275 | // TODO(akulik): detect circular dependency? |
| 276 | |
| 277 | if (v->producer != nullptr) { |
| 278 | // value is no longer produced by it's previous producer. |
| 279 | Erase(&nodes_[v->producer->id].outputs, value_ptr); |
| 280 | } |
| 281 | v->producer = node_ptr; |
| 282 | n->outputs.push_back(value_ptr); |
| 283 | return OkStatus(); |
| 284 | } |
| 285 | |
| 286 | Status RemoveProducer(ValueId value) final { |
| 287 | ValueDef* v; |