Home | History | Annotate | Download | only in c
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/c/python_api.h"
     17 
     18 #include "tensorflow/c/c_api_internal.h"
     19 
     20 namespace tensorflow {
     21 
     22 void AddControlInput(TF_Graph* graph, TF_Operation* op, TF_Operation* input) {
     23   mutex_lock l(graph->mu);
     24   graph->graph.AddControlEdge(&input->node, &op->node);
     25   RecordMutation(graph, *op, "adding control input");
     26 }
     27 
     28 void SetAttr(TF_Graph* graph, TF_Operation* op, const char* attr_name,
     29              TF_Buffer* attr_value_proto, TF_Status* status) {
     30   AttrValue attr_val;
     31   if (!attr_val.ParseFromArray(attr_value_proto->data,
     32                                attr_value_proto->length)) {
     33     status->status =
     34         tensorflow::errors::InvalidArgument("Invalid AttrValue proto");
     35     return;
     36   }
     37 
     38   mutex_lock l(graph->mu);
     39   op->node.AddAttr(attr_name, attr_val);
     40   RecordMutation(graph, *op, "setting attribute");
     41 }
     42 
     43 void SetRequestedDevice(TF_Graph* graph, TF_Operation* op, const char* device) {
     44   mutex_lock l(graph->mu);
     45   op->node.set_requested_device(device);
     46   RecordMutation(graph, *op, "setting device");
     47 }
     48 
     49 void UpdateEdge(TF_Graph* graph, TF_Output new_src, TF_Input dst,
     50                 TF_Status* status) {
     51   mutex_lock l(graph->mu);
     52   tensorflow::shape_inference::InferenceContext* ic =
     53       graph->refiner.GetContext(&new_src.oper->node);
     54 
     55   if (ic->num_outputs() <= new_src.index) {
     56     status->status = tensorflow::errors::OutOfRange(
     57         "Cannot update edge. Output index [", new_src.index,
     58         "] is greater than the number of total outputs [", ic->num_outputs(),
     59         "].");
     60     return;
     61   }
     62   tensorflow::shape_inference::ShapeHandle shape = ic->output(new_src.index);
     63 
     64   tensorflow::shape_inference::InferenceContext* ic_dst =
     65       graph->refiner.GetContext(&dst.oper->node);
     66   if (ic_dst->num_inputs() <= dst.index) {
     67     status->status = tensorflow::errors::OutOfRange(
     68         "Cannot update edge. Input index [", dst.index,
     69         "] is greater than the number of total inputs [", ic_dst->num_inputs(),
     70         "].");
     71     return;
     72   }
     73   if (!ic_dst->MergeInput(dst.index, shape)) {
     74     status->status = tensorflow::errors::InvalidArgument(
     75         "Cannot update edge, incompatible shapes: ", ic_dst->DebugString(shape),
     76         " and ", ic_dst->DebugString(ic_dst->input(dst.index)), ".");
     77     return;
     78   }
     79   status->status = graph->graph.UpdateEdge(&new_src.oper->node, new_src.index,
     80                                            &dst.oper->node, dst.index);
     81 
     82   if (status->status.ok()) {
     83     // This modification only updates the destination node for
     84     // the purposes of running this graph in a session. Thus, we don't
     85     // record the source node as being modified.
     86     RecordMutation(graph, *dst.oper, "updating input tensor");
     87   }
     88 }
     89 
     90 void RemoveAllControlInputs(TF_Graph* graph, TF_Operation* op) {
     91   mutex_lock l(graph->mu);
     92   std::vector<const Edge*> control_edges;
     93   for (const Edge* edge : op->node.in_edges()) {
     94     if (!edge->IsControlEdge()) continue;
     95     control_edges.push_back(edge);
     96   }
     97   for (const Edge* edge : control_edges) {
     98     graph->graph.RemoveControlEdge(edge);
     99   }
    100 }
    101 
    102 }  // namespace tensorflow
    103