tf
This commit is contained in:
1 parent
eb1b42a5e8
commit
458eb2aef1
1283 files changed
+323373
No files matched your search
@@ -0,0 +1,75 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
#ifndef TENSORFLOW_CC_FRAMEWORK_GRAD_OP_REGISTRY_H_
|
||||
#define TENSORFLOW_CC_FRAMEWORK_GRAD_OP_REGISTRY_H_
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "tensorflow/cc/framework/ops.h"
|
||||
#include "tensorflow/cc/framework/scope.h"
|
||||
|
||||
namespace tensorflow {
|
||||
namespace ops {
|
||||
|
||||
/// GradFunc is the signature for all gradient functions in GradOpRegistry.
|
||||
/// Implementations should add operations to compute the gradient outputs of
|
||||
/// 'op' (returned in 'grad_outputs') using 'scope' and 'grad_inputs'.
|
||||
typedef Status (*GradFunc)(const Scope& scope, const Operation& op,
|
||||
const std::vector<Output>& grad_inputs,
|
||||
std::vector<Output>* grad_outputs);
|
||||
|
||||
/// GradOpRegistry maintains a static registry of gradient functions.
|
||||
/// Gradient functions are indexed in the registry by the forward op name (i.e.
|
||||
/// "MatMul" -> MatMulGrad func).
|
||||
class GradOpRegistry {
|
||||
public:
|
||||
/// Registers 'func' as the gradient function for 'op'.
|
||||
/// Returns true if registration was successful, check fails otherwise.
|
||||
bool Register(const string& op, GradFunc func);
|
||||
|
||||
/// Sets 'func' to the gradient function for 'op' and returns Status OK if
|
||||
/// the gradient function for 'op' exists in the registry.
|
||||
/// Note that 'func' can be null for ops that have registered no-gradient with
|
||||
/// the registry.
|
||||
/// Returns error status otherwise.
|
||||
Status Lookup(const string& op, GradFunc* func) const;
|
||||
|
||||
/// Returns a pointer to the global gradient function registry.
|
||||
static GradOpRegistry* Global();
|
||||
|
||||
private:
|
||||
std::unordered_map<string, GradFunc> registry_;
|
||||
};
|
||||
|
||||
} // namespace ops
|
||||
|
||||
// Macros used to define gradient functions for ops.
|
||||
#define REGISTER_GRADIENT_OP(name, fn) \
|
||||
REGISTER_GRADIENT_OP_UNIQ_HELPER(__COUNTER__, name, fn)
|
||||
|
||||
#define REGISTER_NO_GRADIENT_OP(name) \
|
||||
REGISTER_GRADIENT_OP_UNIQ_HELPER(__COUNTER__, name, nullptr)
|
||||
|
||||
#define REGISTER_GRADIENT_OP_UNIQ_HELPER(ctr, name, fn) \
|
||||
REGISTER_GRADIENT_OP_UNIQ(ctr, name, fn)
|
||||
|
||||
#define REGISTER_GRADIENT_OP_UNIQ(ctr, name, fn) \
|
||||
static bool unused_ret_val_##ctr = \
|
||||
::tensorflow::ops::GradOpRegistry::Global()->Register(name, fn)
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_CC_FRAMEWORK_GRAD_OP_REGISTRY_H_
|
||||
@@ -0,0 +1,63 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
#ifndef TENSORFLOW_CC_FRAMEWORK_GRADIENT_CHECKER_H_
|
||||
#define TENSORFLOW_CC_FRAMEWORK_GRADIENT_CHECKER_H_
|
||||
|
||||
#include "tensorflow/cc/framework/ops.h"
|
||||
#include "tensorflow/cc/framework/scope.h"
|
||||
#include "tensorflow/core/framework/tensor.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
/// Returns in 'max_error' the maximum element-wise error for dy/dx between the
|
||||
/// computed and numeric Jacobian matrices where 'xs' and 'ys' are tensors.
|
||||
/// X_T and Y_T are the c++ types for the x and y tensors, and JAC_T is a
|
||||
/// real-valued type to store the Jacobian derivatives dy/dx.
|
||||
/// This function adds operations to the graph associated with 'scope'.
|
||||
///
|
||||
/// Examples:
|
||||
/// if y = Square(x), where x (and so y) are DT_FLOAT,
|
||||
/// <X_T, Y_T, JAC_T> should be <float, float, float>
|
||||
///
|
||||
/// if y = Square(x), where x (and so y) are DT_DOUBLE,
|
||||
/// <X_T, Y_T, JAC_T> should be <double, double, double>
|
||||
///
|
||||
/// if y = Square(x), where x (and so y) are DT_COMPLEX64,
|
||||
/// <X_T, Y_T, JAC_T> should be <complex64, complex64, float>
|
||||
/// Note that JAC_T is always real-valued, and should be an appropriate
|
||||
/// precision to host the partial derivatives for dy/dx
|
||||
///
|
||||
/// if y = ComplexAbs(x) where x is DT_COMPLEX64 (so y is DT_FLOAT)
|
||||
/// <X_T, Y_T, JAC_T> should be <complex64, float, float>
|
||||
///
|
||||
/// if y = Complex(x, x) where x is DT_FLOAT (so y is DT_COMPLEX64)
|
||||
/// <X_T, Y_T, JAC_T> should be <float, complex64, float>
|
||||
template <typename X_T, typename Y_T, typename JAC_T>
|
||||
Status ComputeGradientError(const Scope& scope, const OutputList& xs,
|
||||
const std::vector<TensorShape>& x_shapes,
|
||||
const OutputList& ys,
|
||||
const std::vector<TensorShape>& y_shapes,
|
||||
JAC_T* max_error);
|
||||
|
||||
/// Overload of ComputeGradientError which takes an initial value for 'x'.
|
||||
template <typename X_T, typename Y_T, typename JAC_T>
|
||||
Status ComputeGradientError(const Scope& scope, const Output& x,
|
||||
const Tensor& x_init_value, const Output& y,
|
||||
const TensorShape& y_shape, JAC_T* max_error);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_CC_FRAMEWORK_GRADIENT_CHECKER_H_
|
||||
@@ -0,0 +1,52 @@
|
||||
/* Copyright 2015 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.
|
||||
==============================================================================*/
|
||||
|
||||
#ifndef TENSORFLOW_CC_FRAMEWORK_GRADIENTS_H_
|
||||
#define TENSORFLOW_CC_FRAMEWORK_GRADIENTS_H_
|
||||
|
||||
#include "tensorflow/cc/framework/ops.h"
|
||||
#include "tensorflow/cc/framework/scope.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
/// NOTE: This API is a work in progress and will likely be changing frequently.
|
||||
///
|
||||
/// Given initial gradients 'grad_inputs' (which represent the symbolic partial
|
||||
/// derivatives of some loss function 'L' w.r.t 'outputs'), adds gradient nodes
|
||||
/// to the graph associated with 'scope', which compute (and return in
|
||||
/// 'grad_outputs') the symbolic partial derivatives of 'L' w.r.t 'inputs'.
|
||||
Status AddSymbolicGradients(const Scope& scope,
|
||||
const std::vector<Output>& outputs,
|
||||
const std::vector<Output>& inputs,
|
||||
const std::vector<Output>& grad_inputs,
|
||||
std::vector<Output>* grad_outputs);
|
||||
|
||||
// Same as above, but uses 'OnesLike' for all shapes in
|
||||
// 'outputs' as grad_inputs.
|
||||
Status AddSymbolicGradients(const Scope& scope,
|
||||
const std::vector<Output>& outputs,
|
||||
const std::vector<Output>& inputs,
|
||||
std::vector<Output>* grad_outputs);
|
||||
|
||||
/// Returns a sentinel Output that represents 'no gradient' (i.e. no gradient
|
||||
/// flows along some graph edge during backpropagation).
|
||||
/// Can be returned in 'grad_outputs' by an invocation of 'AddSymbolicGradients'
|
||||
/// (note that gradient flow through an Output can be stopped through the use of
|
||||
/// the StopGradient node).
|
||||
Output NoGradient();
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_CC_FRAMEWORK_GRADIENTS_H_
|
||||
@@ -0,0 +1,299 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
#ifndef TENSORFLOW_CC_FRAMEWORK_OPS_H_
|
||||
#define TENSORFLOW_CC_FRAMEWORK_OPS_H_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "tensorflow/core/framework/tensor.h"
|
||||
#include "tensorflow/core/framework/tensor.pb.h"
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
#include "tensorflow/core/lib/hash/hash.h"
|
||||
#include "tensorflow/core/lib/strings/strcat.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
/// @defgroup core Core Tensorflow API
|
||||
|
||||
class Output;
|
||||
|
||||
/// @addtogroup core
|
||||
/// @{
|
||||
|
||||
/// Represents a node in the computation graph.
|
||||
class Operation {
|
||||
public:
|
||||
Operation() : node_(nullptr) {}
|
||||
explicit Operation(Node* n);
|
||||
|
||||
int32 num_inputs() const { return node_->num_inputs(); }
|
||||
DataType input_type(int32 o) const { return node_->input_type(o); }
|
||||
Output input(int32 i) const;
|
||||
|
||||
int32 num_outputs() const { return node_->num_outputs(); }
|
||||
DataType output_type(int32 o) const { return node_->output_type(o); }
|
||||
Output output(int32 i) const;
|
||||
|
||||
Node* node() const { return node_; }
|
||||
|
||||
uint64 hash(int32 index) const;
|
||||
|
||||
bool operator==(const Operation& other) const { return node_ == other.node_; }
|
||||
|
||||
private:
|
||||
typedef std::vector<std::pair<Node*, int32>> Inputs;
|
||||
static Inputs GetInputs(Node* node);
|
||||
|
||||
Inputs inputs_;
|
||||
Node* node_;
|
||||
};
|
||||
|
||||
/// Represents a tensor value produced by an Operation.
|
||||
class Output {
|
||||
public:
|
||||
Output() = default;
|
||||
explicit Output(Node* n) : op_(n) {}
|
||||
Output(Node* n, int32 index) : op_(n), index_(index) {}
|
||||
Output(const Operation& op, int32 index) : op_(op), index_(index) {}
|
||||
|
||||
Operation op() const { return op_; }
|
||||
Node* node() const { return op().node(); }
|
||||
int32 index() const { return index_; }
|
||||
DataType type() const { return op_.output_type(index_); }
|
||||
string name() const { return strings::StrCat(node()->name(), ":", index()); }
|
||||
bool operator==(const Output& other) const {
|
||||
return op_ == other.op_ && index_ == other.index_;
|
||||
}
|
||||
|
||||
uint64 hash() const { return op_.hash(index_); }
|
||||
|
||||
private:
|
||||
Operation op_ = Operation(nullptr);
|
||||
int32 index_ = 0;
|
||||
};
|
||||
|
||||
/// Hash class that can be used for e.g. storing Outputs in an unordered_map
|
||||
struct OutputHash {
|
||||
std::size_t operator()(const Output& output) const {
|
||||
return Hash64Combine(std::hash<Node*>()(output.node()),
|
||||
std::hash<int32>()(output.index()));
|
||||
}
|
||||
};
|
||||
|
||||
/// Represents a tensor value that can be used as an operand to an Operation.
|
||||
class Input {
|
||||
public:
|
||||
/// Initializer enables constructing an Input object from various kinds of C++
|
||||
/// constants such as simple primitive constants and nested initializer lists
|
||||
/// representing a multi-dimensional array. Initializer constructors are all
|
||||
/// templates, so the aforementioned kinds of C++ constants can be used to
|
||||
/// construct an Initializer. Initializer stores the value it got constructed
|
||||
/// with in a Tensor object.
|
||||
struct Initializer {
|
||||
/// Construct from a scalar value of an arithmetic type or a type that can
|
||||
/// be converted to a string (eg. a string literal).
|
||||
template <typename T, typename = typename std::enable_if<
|
||||
std::is_arithmetic<T>::value ||
|
||||
std::is_convertible<T, string>::value>::type>
|
||||
Initializer(const T& v) { // NOLINT(runtime/explicit)
|
||||
typedef typename RealType<T>::type RealT;
|
||||
Tensor t(DataTypeToEnum<RealT>::v(), TensorShape());
|
||||
t.flat<T>()(0) = RealT(v);
|
||||
tensor = t;
|
||||
}
|
||||
|
||||
Initializer(const Tensor& t) : tensor(t) {} // NOLINT(runtime/explicit)
|
||||
|
||||
/// Construct from a scalar value and an explicit shape
|
||||
template <typename T, typename = typename std::enable_if<
|
||||
std::is_arithmetic<T>::value ||
|
||||
std::is_convertible<T, string>::value>::type>
|
||||
Initializer(const T& v, const TensorShape& shape) {
|
||||
typedef typename RealType<T>::type RealT;
|
||||
Tensor t(DataTypeToEnum<RealT>::v(), shape);
|
||||
for (int64 i = 0; i < t.NumElements(); ++i) {
|
||||
t.flat<T>()(i) = RealT(v);
|
||||
}
|
||||
tensor = t;
|
||||
}
|
||||
|
||||
/// Construct from a initializer list of scalars (a one-dimensional tensor).
|
||||
template <typename T, typename = typename std::enable_if<
|
||||
std::is_arithmetic<T>::value ||
|
||||
std::is_convertible<T, string>::value>::type>
|
||||
Initializer(
|
||||
const std::initializer_list<T>& v) { // NOLINT(runtime/explicit)
|
||||
typedef typename RealType<T>::type RealT;
|
||||
Tensor t(DataTypeToEnum<RealT>::v(),
|
||||
TensorShape{static_cast<int>(v.size())});
|
||||
std::copy_n(v.begin(), v.size(), t.flat<RealT>().data());
|
||||
tensor = t;
|
||||
}
|
||||
|
||||
/// Construct from a initializer list of scalars and an explicit shape.
|
||||
template <typename T, typename = typename std::enable_if<
|
||||
std::is_arithmetic<T>::value ||
|
||||
std::is_convertible<T, string>::value>::type>
|
||||
Initializer(const std::initializer_list<T>& v, const TensorShape& shape) {
|
||||
typedef typename RealType<T>::type RealT;
|
||||
Tensor t(DataTypeToEnum<RealT>::v(), shape);
|
||||
if (t.NumElements() != static_cast<int64>(v.size())) {
|
||||
status = errors::InvalidArgument(
|
||||
"Cannot construct a tensor with ", t.NumElements(),
|
||||
" from an initializer list with ", v.size(), " elements");
|
||||
return;
|
||||
}
|
||||
std::copy_n(v.begin(), v.size(), t.flat<RealT>().data());
|
||||
tensor = t;
|
||||
}
|
||||
|
||||
/// Construct a multi-dimensional tensor from a nested initializer
|
||||
/// list. Note that C++ syntax allows nesting of arbitrarily typed
|
||||
/// initializer lists, so such invalid initializers cannot be disallowed at
|
||||
/// compile time. This function performs checks to make sure that the nested
|
||||
/// initializer list is indeed a valid multi-dimensional tensor.
|
||||
Initializer(const std::initializer_list<Initializer>& v);
|
||||
|
||||
// START_SKIP_DOXYGEN
|
||||
template <typename T, bool = std::is_convertible<T, string>::value>
|
||||
struct RealType {
|
||||
typedef string type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct RealType<T, false> {
|
||||
typedef T type;
|
||||
};
|
||||
// END_SKIP_DOXYGEN
|
||||
|
||||
TensorProto AsTensorProto() {
|
||||
TensorProto tensor_proto;
|
||||
if (tensor.NumElements() > 1) {
|
||||
tensor.AsProtoTensorContent(&tensor_proto);
|
||||
} else {
|
||||
tensor.AsProtoField(&tensor_proto);
|
||||
}
|
||||
return tensor_proto;
|
||||
}
|
||||
|
||||
Status status;
|
||||
Tensor tensor;
|
||||
};
|
||||
|
||||
/// All of Input's constructors are implicit. Input can be implicitly
|
||||
/// constructed from the following objects :
|
||||
/// * Output: This is so that the output of an Operation can be directly used
|
||||
/// as the input to a op wrapper, which takes Inputs.
|
||||
/// * A scalar, or a multi-dimensional tensor specified as a recursive
|
||||
/// initializer list. This enables directly passing constants as
|
||||
/// inputs to op wrappers.
|
||||
/// * A Tensor object.
|
||||
Input(const Output& o) : output_(o) {} // NOLINT(runtime/explicit)
|
||||
|
||||
template <typename T, typename = typename std::enable_if<
|
||||
std::is_arithmetic<T>::value ||
|
||||
std::is_convertible<T, string>::value>::type>
|
||||
Input(const T& v) // NOLINT(runtime/explicit)
|
||||
: Input(Initializer(v)) {}
|
||||
|
||||
Input(const Initializer& init) // NOLINT(runtime/explicit)
|
||||
: status_(init.status),
|
||||
tensor_(init.tensor) {}
|
||||
|
||||
Input(const Tensor& t) // NOLINT(runtime/explicit)
|
||||
: status_(Status::OK()),
|
||||
tensor_(t) {}
|
||||
|
||||
Input(const std::initializer_list<Initializer>&
|
||||
init) { // NOLINT(runtime/explicit)
|
||||
for (const auto& i : init) {
|
||||
if (!i.status.ok()) {
|
||||
status_ = i.status;
|
||||
return;
|
||||
}
|
||||
}
|
||||
tensor_ = Initializer(init).tensor;
|
||||
}
|
||||
|
||||
/// Constructor specifying a node name, index and datatype. This should only
|
||||
/// be used for specifying a backward edge, needed by control flow.
|
||||
Input(const string& name, int32 i, DataType dt)
|
||||
: node_name_(name), index_(i), data_type_(dt) {}
|
||||
|
||||
Node* node() const { return output_.node(); }
|
||||
string node_name() const { return node_name_; }
|
||||
int32 index() const { return node_name_.empty() ? output_.index() : index_; }
|
||||
DataType data_type() const { return data_type_; }
|
||||
Status status() const { return status_; }
|
||||
const Tensor& tensor() const { return tensor_; }
|
||||
|
||||
private:
|
||||
Status status_;
|
||||
Output output_ = Output(Operation(nullptr), 0);
|
||||
Tensor tensor_;
|
||||
const string node_name_ = "";
|
||||
int32 index_ = 0;
|
||||
DataType data_type_ = DT_INVALID;
|
||||
};
|
||||
|
||||
/// A type for representing the output of ops that produce more than one output,
|
||||
/// or a list of tensors.
|
||||
typedef std::vector<Output> OutputList;
|
||||
|
||||
/// A type for representing the input to ops that require a list of tensors.
|
||||
class InputList {
|
||||
public:
|
||||
/// Implicitly convert a list of outputs to a list of inputs. This is useful
|
||||
/// to write code such as ops::Concat(ops::Split(x, 4)).
|
||||
InputList(const OutputList& out) { // NOLINT(runtime/explicit)
|
||||
for (auto const& x : out) {
|
||||
inputs_.push_back(x);
|
||||
}
|
||||
}
|
||||
|
||||
InputList(
|
||||
const std::initializer_list<Input>& inputs) // NOLINT(runtime/explicit)
|
||||
: inputs_(inputs.begin(), inputs.end()) {}
|
||||
|
||||
InputList(const tensorflow::gtl::ArraySlice<Input>&
|
||||
inputs) // NOLINT(runtime/explicit)
|
||||
: inputs_(inputs.begin(), inputs.end()) {}
|
||||
|
||||
InputList(
|
||||
const std::initializer_list<Output>& out) { // NOLINT(runtime/explicit)
|
||||
for (auto const& x : out) {
|
||||
inputs_.push_back(x);
|
||||
}
|
||||
}
|
||||
|
||||
typename std::vector<Input>::iterator begin() { return inputs_.begin(); }
|
||||
typename std::vector<Input>::iterator end() { return inputs_.end(); }
|
||||
typename std::vector<Input>::const_iterator begin() const {
|
||||
return inputs_.begin();
|
||||
}
|
||||
typename std::vector<Input>::const_iterator end() const {
|
||||
return inputs_.end();
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Input> inputs_;
|
||||
};
|
||||
|
||||
/// @}
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_CC_FRAMEWORK_OPS_H_
|
||||
@@ -0,0 +1,249 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
#ifndef TENSORFLOW_CC_FRAMEWORK_SCOPE_H_
|
||||
#define TENSORFLOW_CC_FRAMEWORK_SCOPE_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/cc/framework/ops.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
#include "tensorflow/core/lib/gtl/array_slice.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
class Graph;
|
||||
class GraphDef;
|
||||
class NodeBuilder;
|
||||
struct CompositeOpScopes;
|
||||
|
||||
/// @addtogroup core
|
||||
/// @{
|
||||
|
||||
/// A `Scope` object represents a set of related TensorFlow ops that have the
|
||||
/// same properties such as a common name prefix.
|
||||
///
|
||||
/// A Scope object is a container for TensorFlow Op properties. Op constructors
|
||||
/// get a Scope object as a mandatory first argument and the constructed op
|
||||
/// acquires the properties in the object.
|
||||
///
|
||||
/// A simple example:
|
||||
///
|
||||
/// using namespace ops;
|
||||
/// Scope root = Scope::NewRootScope();
|
||||
/// auto c1 = Const(root, { {1, 1} });
|
||||
/// auto m = MatMul(root, c1, { {41}, {1} });
|
||||
/// GraphDef gdef;
|
||||
/// Status s = root.ToGraphDef(&gdef);
|
||||
/// if (!s.ok()) { ... }
|
||||
///
|
||||
/// Scope hierarchy:
|
||||
///
|
||||
/// The Scope class provides various With<> functions that create a new scope.
|
||||
/// The new scope typically has one property changed while other properties are
|
||||
/// inherited from the parent scope.
|
||||
/// NewSubScope(name) method appends `name` to the prefix of names for ops
|
||||
/// created within the scope, and WithOpName() changes the suffix which
|
||||
/// otherwise defaults to the type of the op.
|
||||
///
|
||||
/// Name examples:
|
||||
///
|
||||
/// Scope root = Scope::NewRootScope();
|
||||
/// Scope linear = root.NewSubScope("linear");
|
||||
/// // W will be named "linear/W"
|
||||
/// auto W = Variable(linear.WithOpName("W"),
|
||||
/// {2, 2}, DT_FLOAT);
|
||||
/// // b will be named "linear/b"
|
||||
/// auto b = Variable(linear.WithOpName("b"),
|
||||
/// {2}, DT_FLOAT);
|
||||
/// auto x = Const(linear, {...}); // name: "linear/Const"
|
||||
/// auto m = MatMul(linear, x, W); // name: "linear/MatMul"
|
||||
/// auto r = BiasAdd(linear, m, b); // name: "linear/BiasAdd"
|
||||
///
|
||||
/// Scope lifetime:
|
||||
///
|
||||
/// A new scope is created by calling Scope::NewRootScope. This creates some
|
||||
/// resources that are shared by all the child scopes that inherit from this
|
||||
/// scope, directly or transitively. For instance, a new scope creates a new
|
||||
/// Graph object to which operations are added when the new scope or its
|
||||
/// children are used by an Op constructor. The new scope also has a Status
|
||||
/// object which will be used to indicate errors by Op-constructor functions
|
||||
/// called on any child scope. The Op-constructor functions have to check the
|
||||
/// scope's status by calling the ok() method before proceeding to construct the
|
||||
/// op.
|
||||
///
|
||||
/// Thread safety:
|
||||
///
|
||||
/// A `Scope` object is NOT thread-safe. Threads cannot concurrently call
|
||||
/// op-constructor functions on the same `Scope` object.
|
||||
class Scope {
|
||||
public:
|
||||
Scope(const Scope& other);
|
||||
~Scope();
|
||||
Scope& operator=(const Scope& other);
|
||||
|
||||
// The following functions are for users making graphs. They return brand new
|
||||
// scopes, or scopes derived from an existing scope object.
|
||||
|
||||
/// Return a new scope.
|
||||
/// This creates a new graph and all operations constructed in this graph
|
||||
/// should use the returned object as the "root" scope.
|
||||
static Scope NewRootScope();
|
||||
|
||||
/// Return a new scope. Ops created with this scope will have
|
||||
/// `name/child_scope_name` as the prefix. The actual name will be unique
|
||||
/// in the current scope. All other properties are inherited from the current
|
||||
/// scope. If `child_scope_name` is empty, the `/` is elided.
|
||||
Scope NewSubScope(const string& child_scope_name) const;
|
||||
|
||||
/// Return a new scope. All ops created within the returned scope will have
|
||||
/// names of the form `name/op_name[_suffix]`.
|
||||
Scope WithOpName(const string& op_name) const;
|
||||
|
||||
/// Return a new scope. All ops created within the returned scope will have as
|
||||
/// control dependencies the union of operations in the control_deps vector
|
||||
/// and the control dependencies of the current scope.
|
||||
Scope WithControlDependencies(
|
||||
const gtl::ArraySlice<Operation>& control_deps) const;
|
||||
/// Same as above, but convenient to add control dependency on the operation
|
||||
/// producing the control_dep output.
|
||||
Scope WithControlDependencies(const Output& control_dep) const;
|
||||
|
||||
/// Return a new scope. All ops created within the returned scope will have no
|
||||
/// control dependencies on other operations.
|
||||
Scope WithNoControlDependencies() const;
|
||||
|
||||
/// Return a new scope. All ops created within the returned scope will have
|
||||
/// the device field set to 'device'.
|
||||
Scope WithDevice(const string& device) const;
|
||||
|
||||
/// Returns a new scope. All ops created within the returned scope will have
|
||||
/// their assigned device set to `assigned_device`.
|
||||
Scope WithAssignedDevice(const string& assigned_device) const;
|
||||
|
||||
/// Return a new scope. All ops created within the returned scope will be
|
||||
/// co-located on the device where op is placed.
|
||||
/// NOTE: This function is intended to be use internal libraries only for
|
||||
/// controlling placement of ops on to devices. Public use is not encouraged
|
||||
/// because the implementation of device placement is subject to change.
|
||||
Scope ColocateWith(const Operation& op) const;
|
||||
/// Convenience function for above.
|
||||
Scope ColocateWith(const Output& out) const { return ColocateWith(out.op()); }
|
||||
/// Clear all colocation constraints.
|
||||
Scope ClearColocation() const;
|
||||
|
||||
/// Return a new scope. The op-constructor functions taking the returned scope
|
||||
/// as the scope argument will exit as soon as an error is detected, instead
|
||||
/// of setting the status on the scope.
|
||||
Scope ExitOnError() const;
|
||||
|
||||
/// Return a new scope. All ops created with the new scope will have
|
||||
/// kernel_label as the value for their '_kernel' attribute;
|
||||
Scope WithKernelLabel(const string& kernel_label) const;
|
||||
|
||||
// The following functions are for scope object consumers.
|
||||
|
||||
/// Return a unique name, using default_name if an op name has not been
|
||||
/// specified.
|
||||
string GetUniqueNameForOp(const string& default_name) const;
|
||||
|
||||
/// Update the status on this scope.
|
||||
/// Note: The status object is shared between all children of this scope.
|
||||
/// If the resulting status is not Status::OK() and exit_on_error_ is set on
|
||||
/// this scope, this function exits by calling LOG(FATAL).
|
||||
void UpdateStatus(const Status s) const;
|
||||
|
||||
// START_SKIP_DOXYGEN
|
||||
|
||||
/// Update the builder with properties accumulated in this scope. Does not set
|
||||
/// status().
|
||||
// TODO(skyewm): NodeBuilder is not part of public API
|
||||
void UpdateBuilder(NodeBuilder* builder) const;
|
||||
// END_SKIP_DOXYGEN
|
||||
|
||||
CompositeOpScopes GetCompositeOpScopes(const string& composite_op_name) const;
|
||||
|
||||
bool ok() const;
|
||||
|
||||
// TODO(skyewm): Graph is not part of public API
|
||||
Graph* graph() const;
|
||||
|
||||
// TODO(skyewm): Graph is not part of public API
|
||||
std::shared_ptr<Graph> graph_as_shared_ptr() const;
|
||||
|
||||
Status status() const;
|
||||
|
||||
/// If status() is Status::OK(), convert the Graph object stored in this scope
|
||||
/// to a GraphDef proto and return Status::OK(). Otherwise, return the error
|
||||
/// status as is without performing GraphDef conversion.
|
||||
Status ToGraphDef(GraphDef* gdef) const;
|
||||
|
||||
// START_SKIP_DOXYGEN
|
||||
|
||||
/// If status() is Status::OK(), construct a Graph object using the default
|
||||
/// GraphConstructorOptions, and return Status::OK if graph construction was
|
||||
/// successful. Otherwise, return the error status.
|
||||
// TODO(josh11b, keveman): Make this faster; right now it converts
|
||||
// Graph->GraphDef->Graph. This cleans up the graph (e.g. adds
|
||||
// edges from the source and to the sink node, resolves back edges
|
||||
// by name), and makes sure the resulting graph is valid.
|
||||
Status ToGraph(Graph* g) const;
|
||||
|
||||
// Calls AddNode() using this scope's ShapeRefiner. This exists in the public
|
||||
// API to prevent custom op wrappers from needing access to shape_refiner.h or
|
||||
// scope_internal.h.
|
||||
// TODO(skyewm): remove this from public API
|
||||
Status DoShapeInference(Node* node) const;
|
||||
|
||||
// Creates a new root scope that causes all DoShapeInference() calls to return
|
||||
// Status::OK() (on the returned scope and any subscopes). Used for testing.
|
||||
// TODO(skyewm): fix tests that still require this and eventually remove, or
|
||||
// at least remove from public API
|
||||
static Scope DisabledShapeInferenceScope();
|
||||
// END_SKIP_DOXYGEN
|
||||
|
||||
const std::vector<Operation>& control_deps() const;
|
||||
|
||||
// START_SKIP_DOXYGEN
|
||||
class Impl;
|
||||
Impl* impl() { return impl_.get(); }
|
||||
const Impl* impl() const { return impl_.get(); }
|
||||
// END_SKIP_DOXYGEN
|
||||
|
||||
private:
|
||||
friend class InternalScope;
|
||||
std::unique_ptr<Impl> impl_;
|
||||
explicit Scope(Impl*);
|
||||
};
|
||||
|
||||
/// A helper struct to hold the scopes that would be used by a function
|
||||
/// constructing a composite op.
|
||||
struct CompositeOpScopes {
|
||||
/// Scope to be used for creating the local ops (primitive or other composite
|
||||
/// ops).
|
||||
Scope child;
|
||||
/// Scope to be used for creating the last op.
|
||||
Scope last;
|
||||
};
|
||||
|
||||
/// @}
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_CC_FRAMEWORK_SCOPE_H_
|
||||
@@ -0,0 +1,124 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
#ifndef TENSORFLOW_CC_FRAMEWORK_SCOPE_INTERNAL_H_
|
||||
#define TENSORFLOW_CC_FRAMEWORK_SCOPE_INTERNAL_H_
|
||||
|
||||
#include "tensorflow/cc/framework/scope.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
class ShapeRefiner;
|
||||
|
||||
// NewInternalScope returns a new scope which doesn't take ownership of
|
||||
// graph, status, name_map, and refiner.
|
||||
// This is intended to enable the C API (which are used by other language
|
||||
// bindings) to create a Scope and access C++ functionality (i.e. gradients).
|
||||
//
|
||||
// Shape inference is disabled if `refiner` is nullptr.
|
||||
Scope NewInternalScope(Graph* graph, Status* status, ShapeRefiner* refiner);
|
||||
|
||||
class Scope::Impl {
|
||||
public:
|
||||
// A NameMap is used to keep track of suffixes for names used in a scope. A
|
||||
// name that has not been used so far in a scope will get no suffix. Later
|
||||
// uses of the same name will get suffixes _1, _2, _3, etc. Multiple scopes
|
||||
// can share the same NameMap. For instance, a new scope created using
|
||||
// WithControlDependencies() would share the same NameMap with the parent.
|
||||
typedef std::unordered_map<string, int> NameMap;
|
||||
|
||||
Impl(const std::shared_ptr<Graph>& graph,
|
||||
const std::shared_ptr<Status>& status,
|
||||
const std::shared_ptr<NameMap>& name_map,
|
||||
const std::shared_ptr<ShapeRefiner>& refiner);
|
||||
|
||||
const string& name() const { return name_; }
|
||||
const std::vector<Operation>& control_deps() const { return control_deps_; }
|
||||
|
||||
private:
|
||||
friend class Scope;
|
||||
|
||||
// Tag types to choose the constructor to dispatch.
|
||||
struct Tags {
|
||||
enum class ScopeName;
|
||||
enum class OpName;
|
||||
enum class ControlDeps;
|
||||
enum class Device;
|
||||
enum class SingleUseScope;
|
||||
enum class ExitOnError;
|
||||
enum class KernelLabel;
|
||||
enum class Colocate;
|
||||
enum class AssignedDevice;
|
||||
};
|
||||
|
||||
Impl(Graph* graph, Status* status, NameMap* name_map, ShapeRefiner* refiner,
|
||||
bool disable_shape_inference);
|
||||
Impl(const Scope& other, Tags::ScopeName, const string& name,
|
||||
bool copy_names);
|
||||
Impl(const Scope& other, Tags::OpName, const string& name,
|
||||
const string& op_name);
|
||||
Impl(const Scope& other, Tags::ControlDeps,
|
||||
std::vector<Operation> control_deps, bool clear_control_deps);
|
||||
Impl(const Scope& other, Tags::Device, const string& device);
|
||||
Impl(const Scope& other, Tags::SingleUseScope, const string& op_name);
|
||||
Impl(const Scope& other, Tags::ExitOnError);
|
||||
Impl(const Scope& other, Tags::KernelLabel, const string& kernel_label);
|
||||
Impl(const Scope& other, Tags::Colocate, const Operation& colocate_with_op,
|
||||
bool clear_colocations);
|
||||
Impl(const Scope& other, Tags::AssignedDevice, const string& assigned_device);
|
||||
|
||||
std::unordered_set<string> GetColocationConstraints(
|
||||
const Operation& colocate_with_op) const;
|
||||
|
||||
// Helper functions to get a unique names.
|
||||
string GetUniqueName(const string& prefix, bool check_single_use) const;
|
||||
string GetNameForOp(const string& default_name) const;
|
||||
|
||||
bool single_use_scope() const { return scope_used_ != nullptr; }
|
||||
|
||||
// The graph, status, and name maps are shared by all child scopes
|
||||
// created from a single 'root' scope. A root scope is created by calling the
|
||||
// Scope::NewRootScope function, which creates a new graph, a new status and
|
||||
// the name maps.
|
||||
std::shared_ptr<Graph> graph_ = nullptr;
|
||||
std::shared_ptr<Status> status_ = nullptr;
|
||||
std::shared_ptr<NameMap> name_map_ = nullptr;
|
||||
std::shared_ptr<ShapeRefiner> refiner_ = nullptr;
|
||||
|
||||
// If scope_used_ is not nullptr, op_name_ should be empty and
|
||||
// GetUniqueNameForOp can only be called once on this scope. More calls to
|
||||
// GetUniqueNameForOp will cause an error status to be set on this scope.
|
||||
std::shared_ptr<bool> scope_used_ = nullptr;
|
||||
|
||||
const std::vector<Operation> control_deps_;
|
||||
|
||||
// The fully-qualified name of this scope (i.e. includes any parent scope
|
||||
// names).
|
||||
const string name_ = "";
|
||||
const string op_name_ = "";
|
||||
const bool exit_on_error_ = false;
|
||||
const string kernel_label_ = "";
|
||||
const string device_ = "";
|
||||
const string assigned_device_ = "";
|
||||
const std::unordered_set<string> colocation_constraints_;
|
||||
|
||||
// If true, Scope::DoShapeInference() always returns Status:OK().
|
||||
// TODO(skyewm): remove this when possible
|
||||
const bool disable_shape_inference_;
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_CC_FRAMEWORK_SCOPE_INTERNAL_H_
|
||||
@@ -0,0 +1,40 @@
|
||||
/* Copyright 2017 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.
|
||||
==============================================================================*/
|
||||
|
||||
#ifndef TENSORFLOW_CC_FRAMEWORK_WHILE_GRADIENTS_H_
|
||||
#define TENSORFLOW_CC_FRAMEWORK_WHILE_GRADIENTS_H_
|
||||
|
||||
#include "tensorflow/cc/framework/ops.h"
|
||||
#include "tensorflow/cc/framework/scope.h"
|
||||
#include "tensorflow/core/graph/while_context.h"
|
||||
|
||||
// Utility functions for constructing while loop gradients
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Adds the gradient computation for the while loop associated with
|
||||
// `while_ctx`. `grad_inputs` are the partial derivatives w.r.t. the loop
|
||||
// outputs, i.e. the exit nodes. The partial derivatives w.r.t. the loop
|
||||
// inputs, i.e. the input loop vars, are returned in `grad_outputs`.
|
||||
// `grad_inputs` and `grad_outputs` are both in loop-variable order, as defined
|
||||
// by the original inputs to BuildWhileLoop().
|
||||
// TODO(skyewm): maybe comment on NoGradient once it's supported
|
||||
Status AddWhileLoopGradient(WhileContext* while_ctx, const Scope& scope,
|
||||
const std::vector<Output>& grad_inputs,
|
||||
std::vector<Output>* grad_outputs);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_CC_FRAMEWORK_WHILE_GRADIENTS_H_
|
||||
Reference in new issue
Block a user