1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/core/framework/node_def.pb.h"
17#include "tensorflow/tools/graph_transforms/transform_utils.h"
18
19namespace tensorflow {
20namespace graph_transforms {
21
22// Sets the device field of ops in the graph.
23Status SetDevice(const GraphDef& input_graph_def,
24 const TransformFuncContext& context,
25 GraphDef* output_graph_def) {
26 string new_device;
27 TF_RETURN_IF_ERROR(context.GetOneStringParameter("device", "", &new_device));
28 bool if_default;
29 TF_RETURN_IF_ERROR(
30 context.GetOneBoolParameter("if_default", false, &if_default));
31
32 output_graph_def->Clear();
33 for (const NodeDef& node : input_graph_def.node()) {
34 NodeDef* new_node = output_graph_def->mutable_node()->Add();
35 *new_node = node;
36 if (!if_default || (node.device().empty())) {
37 new_node->set_device(new_device);
38 }
39 }
40
41 return OkStatus();
42}
43
44REGISTER_GRAPH_TRANSFORM("set_device", SetDevice);
45
46} // namespace graph_transforms
47} // namespace tensorflow
48