aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/grappler/optimizers/data/function_utils.cc
blob: 311df15bc2728a57a66e58cbe3217d3cf03e44dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/core/grappler/optimizers/data/function_utils.h"
#include "tensorflow/core/grappler/optimizers/data/graph_utils.h"

#include "tensorflow/core/framework/device_base.h"
#include "tensorflow/core/framework/op_def.pb.h"
#include "tensorflow/core/lib/strings/scanner.h"
#include "tensorflow/core/util/ptr_util.h"

namespace tensorflow {
namespace grappler {
namespace function_utils {

FunctionDefTensorDesc::FunctionDefTensorDesc(const string& node_name,
                                             const string& output, int position)
    : node_name(node_name), node_output(output), position(position) {
  full_str = strings::StrCat(node_name, ":", node_output, ":", position);
}

FunctionDefTensorDesc::FunctionDefTensorDesc(const string& input) {
  // Parses node_name:node_output:position string into its components.
  full_str = input;
  StringPiece capture;
  StringPiece remaining;

  // Parse "node_name"
  if (strings::Scanner(input)
          .One(strings::Scanner::LETTER_DIGIT_DOT_UNDERSCORE)
          .Any(strings::Scanner::LETTER_DIGIT_DASH_DOT_SLASH_UNDERSCORE)
          .GetResult(&remaining, &capture)) {
    node_name = string(capture.data(), capture.size());
  }

  // Parse "node_output" if it exists
  if (strings::Scanner(remaining)
          .OneLiteral(":")
          .RestartCapture()
          .One(strings::Scanner::LETTER)
          .Any(strings::Scanner::LETTER_DIGIT_UNDERSCORE)
          .GetResult(&remaining, &capture)) {
    node_output = string(capture.data(), capture.size());
  }

  // Parse "position" if it exists
  if (strings::Scanner(remaining)
          .OneLiteral(":")
          .RestartCapture()
          .Many(strings::Scanner::DIGIT)
          .GetResult(nullptr, &capture)) {
    CHECK(strings::safe_strto32(capture, &position));
  }
}

// TODO(rachelim): Create a utility class similar to MutableGraphView for
// FunctionDefs, and use that to manipulate functions. It'll be more
// performant if we kept mappings of nodes->inputs/outputs, so that we don't
// have to search over all nodes each time.
// Note that we're not using GrapplerFunctionItem because it doesn't cover
// some of our desired uses (eg changing the outputs of a function), and the
// FunctionDef -> GraphDef conversion isn't really necessary in this case.
void ReplaceReferences(const string& from, const string& to,
                       FunctionDef* func) {
  for (NodeDef& n : *func->mutable_node_def()) {
    std::replace(n.mutable_input()->begin(), n.mutable_input()->end(), from,
                 to);
  }

  for (auto& p : *func->mutable_ret()) {
    if (p.second == from) {
      p.second = to;
    }
  }
}

void AddFunctionOutputWithUniqueName(StringPiece prefix,
                                     StringPiece output_tensor_name,
                                     FunctionDef* function, DataType dt) {
  string name = string(prefix);
  int id = function->signature().output_arg_size();
  while (ContainsFunctionOutputWithName(name, *function)) {
    name = strings::StrCat(prefix, "/_", id);
    ++id;
  }
  auto* output = function->mutable_signature()->mutable_output_arg()->Add();
  output->set_name(name);
  output->set_type(dt);

  (*function->mutable_ret())[name] = string(output_tensor_name);
}

NodeDef* AddNode(StringPiece name, StringPiece op,
                 const std::vector<string>& inputs,
                 const std::vector<std::pair<string, AttrValue>>& attributes,
                 FunctionDef* fd) {
  NodeDef* node = fd->add_node_def();
  if (!name.empty()) {
    node->set_name(string(name));
  } else {
    SetUniqueFunctionNodeName(op, fd, node);
  }
  node->set_op(string(op));
  for (const string& input : inputs) {
    node->add_input(input);
  }
  for (auto attr : attributes) {
    (*node->mutable_attr())[attr.first] = attr.second;
  }
  return node;
}

bool ContainsFunctionNodeWithName(StringPiece name,
                                  const FunctionDef& function) {
  return FindFunctionNodeWithName(name, function) != -1;
}

bool ContainsFunctionNodeWithOp(StringPiece op, const FunctionDef& function) {
  return FindFunctionNodeWithOp(op, function) != -1;
}

bool ContainsFunctionOutputWithName(StringPiece name,
                                    const FunctionDef& function) {
  return FindFunctionOutputWithName(name, function) != -1;
}

int FindFunctionInputWithName(StringPiece name, const FunctionDef& function) {
  return graph_utils::GetFirstElementIndexWithPredicate(
      [&name](const OpDef_ArgDef& arg) { return arg.name() == name; },
      function.signature().input_arg());
}

int FindFunctionOutputWithName(StringPiece name, const FunctionDef& function) {
  return graph_utils::GetFirstElementIndexWithPredicate(
      [&name](const OpDef_ArgDef& arg) { return arg.name() == name; },
      function.signature().output_arg());
}

int FindFunctionNodeWithName(StringPiece name, const FunctionDef& function) {
  return graph_utils::GetFirstElementIndexWithPredicate(
      [&name](const NodeDef& node) { return node.name() == name; },
      function.node_def());
}

int FindFunctionNodeWithOp(StringPiece op, const FunctionDef& function) {
  return graph_utils::GetFirstElementIndexWithPredicate(
      [&op](const NodeDef& node) { return node.op() == op; },
      function.node_def());
}

void SetUniqueFunctionNodeName(StringPiece prefix, FunctionDef* function,
                               NodeDef* node) {
  string name = string(prefix);
  int id = function->node_def_size();
  while (ContainsFunctionNodeWithName(name, *function)) {
    name = strings::StrCat(prefix, "/_", id);
    ++id;
  }
  node->set_name(std::move(name));
}

}  // end namespace function_utils
}  // end namespace grappler
}  // end namespace tensorflow