STT-tensorflow/tensorflow/cc/ops/const_op.cc
Skye Wanderman-Milne 477d49c9ea C++ API: run shape inference as nodes are constructed
Here's an example of the new generated code:

AddN::AddN(const ::tensorflow::Scope& scope, ::tensorflow::InputList inputs) {
  if (!scope.ok()) return;
  auto _inputs = ::tensorflow::ops::AsNodeOutList(scope, inputs);
  if (!scope.ok()) return;
  ::tensorflow::Node* ret;
  const auto unique_name = scope.GetUniqueNameForOp("AddN");
  auto builder = ::tensorflow::NodeBuilder(unique_name, "AddN")
                     .Input(_inputs)
  ;
  scope.UpdateBuilder(&builder);
  scope.UpdateStatus(builder.Finalize(scope.graph(), &ret));
  if (!scope.ok()) return;
  scope.UpdateStatus(scope.DoShapeInference(ret));
  this->sum = Output(ret, 0);
}

Enabling shape inference unfortunately broke many tests. I fixed some of them, but for others I introduced a Scope::DisabledShapeInferenceScope() static method that returns a scope that doesn't perform shape inference. Eventually we should fix the tests that use this and remove it.

PiperOrigin-RevId: 165378429
2017-08-15 16:50:32 -07:00

76 lines
2.4 KiB
C++

/* Copyright 2016 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/cc/ops/const_op.h"
#include "tensorflow/core/framework/types.h"
namespace tensorflow {
namespace ops {
Output Const(const Scope& scope, const Input::Initializer& val) {
if (!scope.ok()) return Output();
if (!val.status.ok()) {
scope.UpdateStatus(val.status);
return Output();
}
Node* ret;
Graph* graph = scope.graph();
const string unique_name = scope.GetUniqueNameForOp("Const");
auto builder = NodeBuilder(unique_name, "Const")
.Attr("value", val.tensor)
.Attr("dtype", val.tensor.dtype());
scope.UpdateBuilder(&builder);
scope.UpdateStatus(builder.Finalize(graph, &ret));
if (!scope.ok()) return Output();
scope.UpdateStatus(scope.DoShapeInference(ret));
if (!scope.ok()) return Output();
return Output(ret);
}
NodeBuilder::NodeOut AsNodeOut(const Scope& scope, const Input& inp) {
if (!inp.status().ok()) {
scope.UpdateStatus(inp.status());
return NodeBuilder::NodeOut(inp.node(), inp.index());
}
if (inp.node()) {
return NodeBuilder::NodeOut(inp.node(), inp.index());
}
if (!inp.node_name().empty()) {
return NodeBuilder::NodeOut(inp.node_name(), inp.index(), inp.data_type());
}
auto transformed = Input{
Const(scope.NewSubScope("Const"), Input::Initializer(inp.tensor()))};
return NodeBuilder::NodeOut{transformed.node(), transformed.index()};
}
std::vector<NodeBuilder::NodeOut> AsNodeOutList(const Scope& scope,
const InputList& inp) {
std::vector<NodeBuilder::NodeOut> out;
for (const auto& i : inp) {
const auto node_out = AsNodeOut(scope, i);
if (!scope.ok()) {
return {};
}
out.push_back(node_out);
}
return out;
}
} // namespace ops
} // namespace tensorflow