This commit is contained in:
Kevin Darby committed 2019-01-02 17:04:10 -05:00
1 parent eb1b42a5e8
commit 458eb2aef1
1283 files changed
+323373

No files matched your search

@@ -0,0 +1,120 @@
/* 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_CORE_GRAPH_ALGORITHM_H_
#define TENSORFLOW_CORE_GRAPH_ALGORITHM_H_
#include <functional>
#include <unordered_set>
#include <vector>
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/lib/gtl/array_slice.h"
namespace tensorflow {
// Comparator for two nodes. This is used in order to get a stable ording.
using NodeComparator = std::function<bool(const Node*, const Node*)>;
using EdgeFilter = std::function<bool(const Edge&)>;
// Compares two node based on their ids.
struct NodeComparatorID {
bool operator()(const Node* n1, const Node* n2) const {
return n1->id() < n2->id();
}
};
// Compare two nodes based on their names.
struct NodeComparatorName {
bool operator()(const Node* n1, const Node* n2) const {
return n1->name() < n2->name();
}
};
// Perform a depth-first-search on g starting at the source node.
// If enter is not empty, calls enter(n) before visiting any children of n.
// If leave is not empty, calls leave(n) after visiting all children of n.
// If stable_comparator is set, a stable ordering of visit is achieved by
// sorting a node's neighbors first before visiting them.
// If edge_filter is set then ignores edges for which edge_filter returns false.
extern void DFS(const Graph& g, const std::function<void(Node*)>& enter,
const std::function<void(Node*)>& leave,
const NodeComparator& stable_comparator = {},
const EdgeFilter& edge_filter = {});
// Perform a reverse depth-first-search on g starting at the sink node.
// If enter is not empty, calls enter(n) before visiting any parents of n.
// If leave is not empty, calls leave(n) after visiting all parents of n.
// If stable_comparator is set, a stable ordering of visit is achieved by
// sorting a node's neighbors first before visiting them.
extern void ReverseDFS(const Graph& g, const std::function<void(Node*)>& enter,
const std::function<void(Node*)>& leave,
const NodeComparator& stable_comparator = {});
// Perform a reverse depth-first-search on g starting at the 'start' nodes.
// If enter is not empty, calls enter(n) before visiting any parents of n.
// If leave is not empty, calls leave(n) after visiting all parents of n.
// If stable_comparator is set, a stable ordering of visit is achieved by
// sorting a node's neighbors first before visiting them.
extern void ReverseDFSFrom(const Graph& g, gtl::ArraySlice<Node*> start,
const std::function<void(Node*)>& enter,
const std::function<void(Node*)>& leave,
const NodeComparator& stable_comparator = {});
extern void ReverseDFSFrom(const Graph& g, gtl::ArraySlice<const Node*> start,
const std::function<void(const Node*)>& enter,
const std::function<void(const Node*)>& leave,
const NodeComparator& stable_comparator = {});
// Stores in *order the post-order numbering of all nodes
// in graph found via a depth first search starting at the source node.
//
// Note that this is equivalent to reverse topological sorting when the
// graph does not have cycles.
//
// If stable_comparator is set, a stable ordering of visit is achieved by
// sorting a node's neighbors first before visiting them.
//
// If edge_filter is set then ignores edges for which edge_filter returns false.
//
// REQUIRES: order is not NULL.
void GetPostOrder(const Graph& g, std::vector<Node*>* order,
const NodeComparator& stable_comparator = {},
const EdgeFilter& edge_filter = {});
// Stores in *order the reverse post-order numbering of all nodes
// If stable_comparator is set, a stable ordering of visit is achieved by
// sorting a node's neighbors first before visiting them.
//
// If edge_filter is set then ignores edges for which edge_filter returns false.
void GetReversePostOrder(const Graph& g, std::vector<Node*>* order,
const NodeComparator& stable_comparator = {},
const EdgeFilter& edge_filter = {});
// Prune nodes in "g" that are not in some path from the source node
// to any node in 'nodes'. Returns true if changes were made to the graph.
// Does not fix up source and sink edges.
bool PruneForReverseReachability(Graph* g,
std::unordered_set<const Node*> nodes);
// Connect all nodes with no incoming edges to source.
// Connect all nodes with no outgoing edges to sink.
//
// Returns true if and only if 'g' is mutated.
bool FixupSourceAndSinkEdges(Graph* g);
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_ALGORITHM_H_
@@ -0,0 +1,29 @@
/* 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_CORE_GRAPH_COLORS_H_
#define TENSORFLOW_CORE_GRAPH_COLORS_H_
namespace tensorflow {
// Return a color drawn from a palette to represent an entity
// identified by "i". The return value has the form "#RRGGBB" Note
// that the palette has a limited set of colors and therefore colors
// will be reused eventually.
const char* ColorFor(int dindex);
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_COLORS_H_
@@ -0,0 +1,51 @@
/* 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_CORE_GRAPH_CONTROL_FLOW_H_
#define TENSORFLOW_CORE_GRAPH_CONTROL_FLOW_H_
#include <vector>
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/lib/core/status.h"
namespace tensorflow {
// Control flow info for a graph node.
struct ControlFlowInfo {
const Node* frame = nullptr; // frame of a node
const Node* parent_frame = nullptr; // parent frame of a node
string frame_name; // frame name of a node
};
// Clear and populate `info` with each node's frame and the level it belongs to.
// We check the well-formedness of the graph:
// 1) All inputs to a node must come from the same frame and have the same
// "static" iteration level.
// 2) Each frame has at most one LoopCond node.
// 3) Each frame has a single parent frame.
// If `unreachable_nodes` is set, return names of nodes unreachable from the
// source node. We cannot build ControlFlowInfo for such nodes. They might be
// pruned later.
//
// NOTE(yuanbyu): For now, we require all sends/recvs have iteration level 0.
// This essentially means there can't be multiple serial Nexts in an iteration,
// which all sane front-ends should satisfy.
Status BuildControlFlowInfo(const Graph* g, std::vector<ControlFlowInfo>* info,
std::vector<string>* unreachable_nodes = nullptr);
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_CONTROL_FLOW_H_
@@ -0,0 +1,232 @@
/* 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_CORE_GRAPH_COSTMODEL_H_
#define TENSORFLOW_CORE_GRAPH_COSTMODEL_H_
#include <unordered_map>
#include <vector>
#include "tensorflow/core/framework/cost_graph.pb.h"
#include "tensorflow/core/framework/step_stats.pb.h"
#include "tensorflow/core/framework/tensor_shape.pb.h"
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/graph/types.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/gtl/array_slice.h"
#include "tensorflow/core/platform/macros.h"
#include "tensorflow/core/platform/protobuf.h"
namespace tensorflow {
typedef std::unordered_map<StringPiece, int32, StringPieceHasher>
NodeNameToCostIdMap;
class StepStats;
// CostModel keeps track of the following runtime statistics for nodes
// of a single Graph:
// * The total number of times a node has executed.
// * The accumulated execution time (in microseconds) of a node.
// * The accumulated size (in bytes) of each node's output.
//
// This class is NOT thread-safe.
class CostModel {
public:
// If "global" is true, maintains costs based on Node::cost_id, otherwise
// maintains costs based on Node::id.
explicit CostModel(bool is_global) : is_global_(is_global) {
unknown_shape_.set_unknown_rank(true);
}
// Assigns min_count_ as a function of the median count for a Node.
// This value is then used for suppressing the time/size costs of
// infrequent operations.
// NOTE(tucker): Maybe this should move to a subclass of CostModel.
void SuppressInfrequent();
bool is_global() const { return is_global_; }
inline int Id(const Node* n) const {
if (is_global_) {
return n->cost_id();
} else {
return n->id();
}
}
// Initializes cost model for 'g'.
void InitFromGraph(const Graph& g);
// Merges costs from cm.
// REQUIRES: is_global_ is true for this and for "cm"
void MergeFromGlobal(const CostModel& cm);
// Merges costs from "cm", which has been computed relative to "g".
// REQUIRES: is_global_ is true for this, and false for "cm".
void MergeFromLocal(const Graph& g, const CostModel& cm);
void MergeFromStats(const NodeNameToCostIdMap& map, const StepStats& ss);
// Sets the number of outputs of "node".
void SetNumOutputs(const Node* node, int num_outputs);
// Records that "node" has executed "num_count" more times.
void RecordCount(const Node* node, int num_count);
// Returns how many times "node" has been executed.
int32 TotalCount(const Node* node) const;
// Records that "output_slot" of "node" has produced tensors of
// aggregated "bytes".
void RecordSize(const Node* node, int output_slot, Bytes bytes);
// Returns total bytes of tensors produced by "node"s output slot.
Bytes TotalBytes(const Node* node, int output_slot) const;
// Returns a prediction for the size of the tensor at the
// output_slot produced by one execution of "node".
Bytes SizeEstimate(const Node* node, int output_slot) const;
// Records that Executions of "node" have taken "time" microseconds.
void RecordTime(const Node* node, Microseconds time);
// Returns the total execution time for "node".
Microseconds TotalTime(const Node* node) const;
// Returns a prediction for one execution of "node".
Microseconds TimeEstimate(const Node* node) const;
// Check that an estimate is available for every OP node in graph.
void CheckInitialized(const Graph& graph) const;
// Records the maximum size in bytes and optionally the corresponding shape of
// the tensor generated by "output_slot" of "node". If
void RecordMaxMemorySize(const Node* node, int output_slot, Bytes bytes,
const TensorShapeProto& tensor_shape,
const DataType& dtype);
// Returns the maximum size in bytes of the tensor generated by "output_slot"
// of "node".
Bytes MaxMemorySize(const Node* node, int output_slot) const;
// Returns the shape corresponding to the largest memory size of the tensor
// generated by "output_slot" of "node".
const TensorShapeProto& MaxMemoryShape(const Node* node,
int output_slot) const;
// Returns the shape corresponding to the largest memory size of the tensor
// generated by "output_slot" of "node".
DataType MaxMemoryType(const Node* node, int output_slot) const;
// Returns the size in bytes of temporary memory consumed by "node".
Bytes TempMemorySize(const Node* node) const;
// Returns the size of persistent memory allocated by "node".
Bytes PersistentMemorySize(const Node* node) const;
// Records memory stats such as temp momory and persistent memory.
void RecordMemoryStats(const Node* node, const MemoryStats& memory_stats);
// Records the maximum execution time (in microseconds) of "node".
void RecordMaxExecutionTime(const Node* node, Microseconds time);
// Returns the maximum execution time (in microseconds) of "node".
Microseconds MaxExecutionTime(const Node* node) const;
// Record the unique id of the tensor generated by "output_slot" of "node".
// Any other tensor sharing the same id will be an alias, i.e. it will share
// the same underlying memory storage area.
void RecordAllocationId(const Node* node, int output_slot, int64 alloc_id);
// Return the unique id of the tensor generated by "output_slot" of "node".
int64 AllocationId(const Node* node, int output_slot) const;
bool IsPersistentTensor(const Node* node, int64 alloc_id) const;
// Helper routines to encapsulate static estimation heuristics
// Compute an estimate of the time to copy "b" bytes over the network,
// given a fixed cost of "network_latency_millis" milliseconds and
// an estimated bandwidth of "estimated_gbps" gigabits per second (note that
// this value is in gigabits, not gigabytes).
static Microseconds CopyTimeEstimate(Bytes b, double network_latency_millis,
double estimated_gbps);
static Microseconds ComputationTimeEstimate(int64 mathops);
// Add this CostModel into the CostGraphDef.
void AddToCostGraphDef(const Graph* graph, CostGraphDef* cost_graph) const;
// Write the contents of the CostModel to the INFO log.
void WriteSummaryToLog() const;
// Increment the times that the cost model is updated.
void IncrementUpdateTimes();
// Get the times that the cost model is updated.
int32 GetUpdateTimes() const;
private:
static Bytes MinTensorMemoryUsage(const TensorShapeProto& tensor_shape,
const DataType& dtype);
const bool is_global_;
// Resizes vectors so that they are large enough for "id" and id's outputs.
void Ensure(int id, int num_outputs);
// Nodes and Edges whose count is < this value
// get type/byte estimates of 0.
int32 min_count_ = 0;
// The number of times the cost model is updated.
int32 update_times_ = 0;
// Number of times each Node has been executed.
std::vector<int32> count_;
// Cumulative execution time.
std::vector<Microseconds> time_;
// Cumulative Bytes output on each channel.
std::vector<gtl::InlinedVector<Bytes, 2>> slot_bytes_;
// Maximum execution time
std::vector<Microseconds> max_exec_time_;
// Maximum memory usage
struct MemUsage {
MemUsage() : temp_memory_size(0), persistent_memory_size(0) {}
// TODO(yuefengz): temp_memory_size is not being used, remove it.
Bytes temp_memory_size;
Bytes persistent_memory_size;
gtl::InlinedVector<Bytes, 2> output_port_mem;
gtl::InlinedVector<TensorShapeProto, 2> output_port_shape;
gtl::InlinedVector<DataType, 2> output_port_type;
};
std::vector<MemUsage> max_mem_usage_;
std::vector<gtl::InlinedVector<int64, 2>> output_port_alloc_ids_;
std::set<int64> persistent_alloc_ids_;
std::map<string, std::set<int64>> persistent_alloc_ids_by_devices_;
TensorShapeProto unknown_shape_;
TF_DISALLOW_COPY_AND_ASSIGN(CostModel);
};
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_COSTMODEL_H_
@@ -0,0 +1,41 @@
/* 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_CORE_GRAPH_DEFAULT_DEVICE_H_
#define TENSORFLOW_CORE_GRAPH_DEFAULT_DEVICE_H_
#include <string>
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/node_def.pb.h"
namespace tensorflow {
namespace graph {
// Sets the default device for all nodes in graph_def to "device",
// only if not already set.
inline void SetDefaultDevice(const string& device, GraphDef* graph_def) {
for (int i = 0; i < graph_def->node_size(); ++i) {
auto node = graph_def->mutable_node(i);
if (node->device().empty()) {
node->set_device(device);
}
}
}
} // namespace graph
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_DEFAULT_DEVICE_H_
@@ -0,0 +1,232 @@
/* 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_GRAPH_EDGESET_H_
#define TENSORFLOW_GRAPH_EDGESET_H_
#include <stddef.h>
#include <set>
#include "tensorflow/core/platform/macros.h"
#include "tensorflow/core/platform/types.h"
#include "tensorflow/core/platform/logging.h"
namespace tensorflow {
class Edge;
// An unordered set of edges. Uses very little memory for small sets.
// Unlike std::set, EdgeSet does NOT allow mutations during iteration.
class EdgeSet {
public:
EdgeSet();
~EdgeSet();
typedef const Edge* key_type;
typedef const Edge* value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
class const_iterator;
typedef const_iterator iterator;
bool empty() const;
size_type size() const;
void clear();
std::pair<iterator, bool> insert(value_type value);
size_type erase(key_type key);
// Caller is not allowed to mutate the EdgeSet while iterating.
const_iterator begin() const;
const_iterator end() const;
private:
// Up to kInline elements are stored directly in ptrs_ (nullptr means none).
// If ptrs_[0] == this then ptrs_[1] points to a set<const Edge*>.
static const int kInline = 4; // Must be >= 2.
const void* ptrs_[kInline];
std::set<const Edge*>* get_set() const {
if (ptrs_[0] == this) {
return static_cast<std::set<const Edge*>*>(const_cast<void*>(ptrs_[1]));
} else {
return nullptr;
}
}
// To detect mutations while iterating.
#ifdef NDEBUG
void RegisterMutation() {}
#else
uint32 mutations_ = 0;
void RegisterMutation() { mutations_++; }
#endif
TF_DISALLOW_COPY_AND_ASSIGN(EdgeSet);
};
class EdgeSet::const_iterator {
public:
typedef typename EdgeSet::value_type value_type;
typedef const typename EdgeSet::value_type& reference;
typedef const typename EdgeSet::value_type* pointer;
typedef typename EdgeSet::difference_type difference_type;
typedef std::forward_iterator_tag iterator_category;
const_iterator() {}
const_iterator& operator++();
const_iterator operator++(int /*unused*/);
const value_type* operator->() const;
value_type operator*() const;
bool operator==(const const_iterator& other) const;
bool operator!=(const const_iterator& other) const {
return !(*this == other);
}
private:
friend class EdgeSet;
void const* const* array_iter_ = nullptr;
typename std::set<const Edge*>::const_iterator tree_iter_;
#ifdef NDEBUG
inline void Init(const EdgeSet* e) {}
inline void CheckNoMutations() const {}
#else
inline void Init(const EdgeSet* e) {
owner_ = e;
init_mutations_ = e->mutations_;
}
inline void CheckNoMutations() const {
CHECK_EQ(init_mutations_, owner_->mutations_);
}
const EdgeSet* owner_ = nullptr;
uint32 init_mutations_ = 0;
#endif
};
inline EdgeSet::EdgeSet() {
for (int i = 0; i < kInline; i++) {
ptrs_[i] = nullptr;
}
}
inline EdgeSet::~EdgeSet() { delete get_set(); }
inline bool EdgeSet::empty() const { return size() == 0; }
inline EdgeSet::size_type EdgeSet::size() const {
auto s = get_set();
if (s) {
return s->size();
} else {
size_t result = 0;
for (int i = 0; i < kInline; i++) {
if (ptrs_[i]) result++;
}
return result;
}
}
inline void EdgeSet::clear() {
RegisterMutation();
delete get_set();
for (int i = 0; i < kInline; i++) {
ptrs_[i] = nullptr;
}
}
inline EdgeSet::const_iterator EdgeSet::begin() const {
const_iterator ci;
ci.Init(this);
auto s = get_set();
if (s) {
ci.tree_iter_ = s->begin();
} else {
ci.array_iter_ = &ptrs_[0];
}
return ci;
}
inline EdgeSet::const_iterator EdgeSet::end() const {
const_iterator ci;
ci.Init(this);
auto s = get_set();
if (s) {
ci.tree_iter_ = s->end();
} else {
ci.array_iter_ = &ptrs_[size()];
}
return ci;
}
inline EdgeSet::const_iterator& EdgeSet::const_iterator::operator++() {
CheckNoMutations();
if (array_iter_ != nullptr) {
++array_iter_;
} else {
++tree_iter_;
}
return *this;
}
inline EdgeSet::const_iterator EdgeSet::const_iterator::operator++(
int /*unused*/) {
CheckNoMutations();
const_iterator tmp = *this;
operator++();
return tmp;
}
// gcc's set and multiset always use const_iterator since it will otherwise
// allow modification of keys.
inline const EdgeSet::const_iterator::value_type* EdgeSet::const_iterator::
operator->() const {
CheckNoMutations();
if (array_iter_ != nullptr) {
return reinterpret_cast<const value_type*>(array_iter_);
} else {
return tree_iter_.operator->();
}
}
// gcc's set and multiset always use const_iterator since it will otherwise
// allow modification of keys.
inline EdgeSet::const_iterator::value_type EdgeSet::const_iterator::operator*()
const {
CheckNoMutations();
if (array_iter_ != nullptr) {
return static_cast<value_type>(*array_iter_);
} else {
return *tree_iter_;
}
}
inline bool EdgeSet::const_iterator::operator==(
const const_iterator& other) const {
DCHECK((array_iter_ == nullptr) == (other.array_iter_ == nullptr))
<< "Iterators being compared must be from same set that has not "
<< "been modified since the iterator was constructed";
CheckNoMutations();
if (array_iter_ != nullptr) {
return array_iter_ == other.array_iter_;
} else {
return other.array_iter_ == nullptr && tree_iter_ == other.tree_iter_;
}
}
} // namespace tensorflow
#endif // TENSORFLOW_GRAPH_EDGESET_H_
@@ -0,0 +1,58 @@
/* 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_CORE_GRAPH_GRADIENTS_H_
#define TENSORFLOW_CORE_GRAPH_GRADIENTS_H_
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/gtl/array_slice.h"
namespace tensorflow {
// Represents the output of 'node' at 'index'.
struct NodeOut {
Node* node;
int index;
// Returns the string name that represents the output of this node.
string name() const;
// Returns the data type of the output of this node.
DataType dtype() const;
};
// NOTE: This API is a work in progress and will likely be changing frequently.
//
// Given initial gradient-node outputs 'y_grad_node_outputs' (which compute the
// symbolic partial derivatives of some loss function 'L' w.r.t the node outputs
// 'y_node_outputs'), adds gradient nodes to 'graph' that compute the symbolic
// partial derivatives of 'L' w.r.t the node outputs 'x_node_outputs'.
//
// REQUIRES: Each node in 'x_node_outputs' to be unique, and so to have a single
// output (this restriction will be removed in a subsequent change).
// TODO(andydavis) Add symbolic gradient support for general graphs (the current
// implementation only supports gradients for functions). In particular,
// the nodes in 'x_nodes' are currently restricted to have one output.
Status AddSymbolicGradients(gtl::ArraySlice<NodeOut> y_node_outputs,
gtl::ArraySlice<NodeOut> x_node_outputs,
gtl::ArraySlice<NodeOut> y_grad_node_outputs,
std::vector<NodeOut>* x_grad_node_outputs,
Graph* graph);
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_GRADIENTS_H_
+852
View File
@@ -0,0 +1,852 @@
/* 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.
==============================================================================*/
// A Graph describes a set of computations that are to be
// performed, as well as the dependencies between those
// computations. The basic model is a DAG (directed acyclic graph) with
// * internal nodes representing computational operations to be performed;
// * edges represent dependencies, indicating the target may only be
// executed once the source has completed; and
// * predefined "source" (start) and "sink" (finish) nodes -- the source
// should be the only node that doesn't depend on anything, and the sink
// should be the only node that nothing depends on.
//
// Note: Node ids are intended to be relatively dense in the
// 0..max_id range, but there may be gaps since ids won't be reused.
//
// Note: Some dependencies between operations are due to one operation
// consuming the output of another. In fact operations can produce
// multiple outputs and consume multiple inputs, and some
// optimizations will care about which specific outputs are connected
// to which specific inputs. We therefore represent data dependency
// between output O of layer A and input I of layer B using
// "input index" and "output index" labels per edge.
#ifndef TENSORFLOW_CORE_GRAPH_GRAPH_H_
#define TENSORFLOW_CORE_GRAPH_GRAPH_H_
#include <functional>
#include <string>
#include <vector>
#include "tensorflow/core/framework/function.h"
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/graph/edgeset.h"
#include "tensorflow/core/lib/core/arena.h"
#include "tensorflow/core/lib/core/refcount.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/gtl/iterator_range.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/macros.h"
#include "tensorflow/core/platform/types.h"
namespace tensorflow {
class Edge;
class EdgeSetTest;
class Graph;
class GraphDef;
class Node;
class VersionDef;
class WhileContext;
class NeighborIter; // Declared below
class NodeIter; // Declared below
class NodeProperties; // Defined in .cc
class Node {
public:
string DebugString() const;
int id() const { return id_; }
int cost_id() const { return cost_id_; }
const string& name() const;
const string& type_string() const;
// def() provides the NodeDef the user supplied, but the specifics
// of this Node may have changed due to placement, optimization, etc.
// In particular:
// * def().name() will match name();
// * def().op() will match type_string() and op_def().name();
// * def().input() is not reliable, use "in_edges()" below instead;
// * def().device() is the "user's requested device" and may not match
// the actual assigned device, see assigned_device_name() below;
// * def().attr() is authoritative.
// TODO(irving): Replace with NodeInfo.
const NodeDef& def() const;
const OpDef& op_def() const;
// input and output types
int32 num_inputs() const;
DataType input_type(int32 i) const;
const DataTypeVector& input_types() const;
int32 num_outputs() const;
DataType output_type(int32 o) const;
const DataTypeVector& output_types() const;
// The device requested by the user. For the actual assigned device,
// use assigned_device_name() below.
const string& requested_device() const;
// This changes the user requested device but not necessarily the device that
// on which the operation will run.
void set_requested_device(const string& device);
// This gives the device the runtime has assigned this node to. If
// you want the device the user requested, use def().device() instead.
// TODO(josh11b): Validate that the assigned_device, if not empty:
// fully specifies a device, and satisfies def().device().
// TODO(josh11b): Move assigned_device_name outside of Node into a
// NodeId->DeviceName map.
const string& assigned_device_name() const;
void set_assigned_device_name(const string& device_name);
bool has_assigned_device_name() const {
return assigned_device_name_index_ > 0;
}
int assigned_device_name_index() const { return assigned_device_name_index_; }
void set_assigned_device_name_index(int index);
// Read only access to attributes
AttrSlice attrs() const;
// Inputs requested by the NodeDef. For the actual inputs, use in_edges.
const protobuf::RepeatedPtrField<string>& requested_inputs() const;
// Get the neighboring nodes via edges either in or out of this node. This
// includes control edges.
gtl::iterator_range<NeighborIter> in_nodes() const;
gtl::iterator_range<NeighborIter> out_nodes() const;
const EdgeSet& in_edges() const { return in_edges_; }
const EdgeSet& out_edges() const { return out_edges_; }
// Node type helpers.
bool IsSource() const { return id() == 0; }
bool IsSink() const { return id() == 1; }
// Anything other than the special Source & Sink nodes.
bool IsOp() const { return id() > 1; }
// Node class helpers
bool IsSwitch() const { return class_ == NC_SWITCH; }
bool IsMerge() const { return class_ == NC_MERGE; }
bool IsEnter() const { return class_ == NC_ENTER; }
bool IsExit() const { return class_ == NC_EXIT; }
bool IsNextIteration() const { return class_ == NC_NEXT_ITERATION; }
bool IsLoopCond() const { return class_ == NC_LOOP_COND; }
bool IsControlTrigger() const { return class_ == NC_CONTROL_TRIGGER; }
bool IsSend() const { return class_ == NC_SEND || class_ == NC_HOST_SEND; }
bool IsRecv() const { return class_ == NC_RECV || class_ == NC_HOST_RECV; }
bool IsConstant() const { return class_ == NC_CONSTANT; }
bool IsVariable() const { return class_ == NC_VARIABLE; }
bool IsIdentity() const { return class_ == NC_IDENTITY; }
bool IsGetSessionHandle() const { return class_ == NC_GET_SESSION_HANDLE; }
bool IsGetSessionTensor() const { return class_ == NC_GET_SESSION_TENSOR; }
bool IsDeleteSessionTensor() const {
return class_ == NC_DELETE_SESSION_TENSOR;
}
bool IsControlFlow() const {
return (class_ != NC_OTHER) && // Fast path
(IsSwitch() || IsMerge() || IsEnter() || IsExit() ||
IsNextIteration());
}
bool IsHostSend() const { return class_ == NC_HOST_SEND; }
bool IsHostRecv() const { return class_ == NC_HOST_RECV; }
bool IsScopedAllocator() const { return class_ == NC_SCOPED_ALLOCATOR; }
bool IsCollective() const { return class_ == NC_COLLECTIVE; }
bool IsMetadata() const { return class_ == NC_METADATA; }
template <typename T>
void AddAttr(const string& name, const T& val) {
SetAttrValue(val, AddAttrHelper(name));
}
void ClearAttr(const string& name);
// Returns into '*e' the edge connecting to the 'idx' input of this Node.
Status input_edge(int idx, const Edge** e) const;
// Returns into '*edges' the input data edges of this Node, indexed by input
// number. Does not return control edges.
Status input_edges(std::vector<const Edge*>* edges) const;
// Returns into '*n' the node that has an output connected to the
// 'idx' input of this Node.
Status input_node(int idx, const Node** n) const;
Status input_node(int idx, Node** n) const;
WhileContext* while_ctx() const { return while_ctx_; }
void set_while_ctx(WhileContext* while_ctx) {
DCHECK(IsExit());
DCHECK(while_ctx_ == nullptr);
while_ctx_ = while_ctx;
}
private:
friend class Graph;
Node();
NodeProperties* properties() const { return props_.get(); }
void Initialize(int id, int cost_id, std::shared_ptr<NodeProperties> props);
// Releases memory from props_, in addition to restoring *this to its
// uninitialized state.
void Clear();
// Make a copy of the Node's props_ if props_ is shared with
// other nodes. This must be called before mutating properties,
// e.g. in AddAttr.
void MaybeCopyOnWrite();
AttrValue* AddAttrHelper(const string& name);
// A set of mutually exclusive classes for different kinds of nodes,
// class_ is initialized in the Node::Initialize routine based on the
// node's type_string().
enum NodeClass {
NC_UNINITIALIZED,
NC_SWITCH,
NC_MERGE,
NC_ENTER,
NC_EXIT,
NC_NEXT_ITERATION,
NC_LOOP_COND,
NC_CONTROL_TRIGGER,
NC_SEND,
NC_HOST_SEND,
NC_RECV,
NC_HOST_RECV,
NC_CONSTANT,
NC_VARIABLE,
NC_IDENTITY,
NC_GET_SESSION_HANDLE,
NC_GET_SESSION_TENSOR,
NC_DELETE_SESSION_TENSOR,
NC_METADATA,
NC_SCOPED_ALLOCATOR,
NC_COLLECTIVE,
NC_OTHER // Not a special kind of node
};
static const std::unordered_map<string, NodeClass>& kNodeClassTable;
static NodeClass GetNodeClassForOp(const string& ts);
int id_; // -1 until Initialize() is called
int cost_id_; // -1 if there is no corresponding cost accounting node
NodeClass class_;
EdgeSet in_edges_;
EdgeSet out_edges_;
// NOTE(skyewm): inheriting from core::RefCounted may have a slight
// performance benefit over using shared_ptr, at the cost of manual ref
// counting
std::shared_ptr<NodeProperties> props_;
// Index within Graph::device_names_ of the name of device assigned
// to perform this computation.
int assigned_device_name_index_;
// A back-pointer to the Graph that owns this node. Currently, this exists
// solely to allow Node::[set_]assigned_device_name() to work. However, if all
// callers of Node::[set_]assigned_device_name() are modified to use the
// equivalent methods defined directly on Graph, then we can remove this
// field and reclaim that memory.
Graph* graph_;
// Set if this is an exit node of a while loop with an associated
// WhileContext. Otherwise null. (This is only set for exit nodes because
// they're the first nodes of a loop encountered while creating the gradient
// graph. Exit nodes that are part of while loop gradient graphs will not have
// this set.)
WhileContext* while_ctx_;
TF_DISALLOW_COPY_AND_ASSIGN(Node);
};
// Represents an input of a node, i.e., the `index`-th input to `node`.
struct InputTensor {
const Node* node;
int index;
InputTensor(const Node* n, int i) : node(n), index(i) {}
InputTensor() : node(nullptr), index(0) {}
// Returns true if this InputTensor is identical to 'other'. Nodes are
// compared using pointer equality.
bool operator==(const InputTensor& other) const;
// A hash function for InputTensors. Nodes are hashed based on their pointer
// value.
struct Hash {
uint64 operator()(InputTensor const& s) const;
};
};
// Represents an output of a node, i.e., the `index`-th output of `node`. Note
// that a single `OutputTensor` can correspond to multiple `Edge`s if the output
// is consumed by multiple destination nodes.
struct OutputTensor {
const Node* node;
int index;
OutputTensor(const Node* n, int i) : node(n), index(i) {}
OutputTensor() : node(nullptr), index(0) {}
// Returns true if this OutputTensor is identical to 'other'. Nodes are
// compared using pointer equality.
bool operator==(const OutputTensor& other) const;
// A hash function for OutputTensors. Nodes are hashed based on their pointer
// value.
struct Hash {
uint64 operator()(OutputTensor const& s) const;
};
};
class Edge {
public:
Node* src() const { return src_; }
Node* dst() const { return dst_; }
int id() const { return id_; }
// Return the index of the source output that produces the data
// carried by this edge. The special value kControlSlot is used
// for control dependencies.
int src_output() const { return src_output_; }
// Return the index of the destination input that consumes the data
// carried by this edge. The special value kControlSlot is used
// for control dependencies.
int dst_input() const { return dst_input_; }
// Return true iff this is an edge that indicates a control-flow
// (as opposed to a data-flow) dependency.
bool IsControlEdge() const;
string DebugString() const;
private:
Edge() {}
friend class EdgeSetTest;
friend class Graph;
Node* src_;
Node* dst_;
int id_;
int src_output_;
int dst_input_;
};
// Allows for iteration of the edges of a Graph, by iterating the underlying
// Graph.edges_ vector while skipping over null entries.
class GraphEdgesIterable {
private:
const std::vector<Edge*>& edges_;
public:
explicit GraphEdgesIterable(const std::vector<Edge*>& edges)
: edges_(edges) {}
typedef Edge* value_type;
class const_iterator {
private:
// The underlying iterator.
std::vector<value_type>::const_iterator iter_;
// The end of the underlying iterator.
std::vector<value_type>::const_iterator end_;
// Advances iter_ until it reaches a non-null item, or reaches the end.
void apply_filter() {
while (iter_ != end_ && *iter_ == nullptr) {
++iter_;
}
}
public:
const_iterator(std::vector<value_type>::const_iterator iter,
std::vector<value_type>::const_iterator end)
: iter_(iter), end_(end) {
apply_filter();
}
bool operator==(const const_iterator& other) const {
return iter_ == other.iter_;
}
bool operator!=(const const_iterator& other) const {
return iter_ != other.iter_;
}
// This is the prefix increment operator (++x), which is the operator
// used by C++ range iteration (for (x : y) ...). We intentionally do not
// provide a postfix increment operator.
const_iterator& operator++() {
++iter_;
apply_filter();
return *this;
}
value_type operator*() { return *iter_; }
};
const_iterator begin() {
return const_iterator(edges_.begin(), edges_.end());
}
const_iterator end() { return const_iterator(edges_.end(), edges_.end()); }
};
// Thread compatible but not thread safe.
class Graph {
public:
// Constructs a graph with a single SOURCE (always id kSourceId) and a
// single SINK (always id kSinkId) node, and an edge from SOURCE->SINK.
//
// The graph can hold ops found in registry. `registry`s lifetime must be at
// least that of the constructed graph's.
explicit Graph(const OpRegistryInterface* registry);
// Constructs a graph with a single SOURCE (always id kSourceId) and a
// single SINK (always id kSinkId) node, and an edge from SOURCE->SINK.
//
// The graph can hold ops found in `flib_def`. Unlike the constructor taking
// an OpRegistryInterface, this constructor copies the function definitions in
// `flib_def` so its lifetime may be shorter than that of the graph's. The
// OpRegistryInterface backing `flib_def` must still have the lifetime of the
// graph though.
explicit Graph(const FunctionLibraryDefinition& flib_def);
~Graph();
static const int kControlSlot;
// The GraphDef version range of this graph (see graph.proto).
const VersionDef& versions() const;
void set_versions(const VersionDef& versions);
// Adds a new node to this graph, and returns it. Infers the Op and
// input/output types for the node. *this owns the returned instance.
// Returns nullptr and sets *status on error.
Node* AddNode(const NodeDef& node_def, Status* status);
// Copies *node, which may belong to another graph, to a new node,
// which is returned. Does not copy any edges. *this owns the
// returned instance.
Node* CopyNode(const Node* node);
// Removes a node from this graph, including all edges from or to it.
// *node should not be accessed after calling this function.
// REQUIRES: node->IsOp()
void RemoveNode(Node* node);
// Adds an edge that connects the xth output of `source` to the yth input of
// `dest` and returns it. Does not update dest's NodeDef.
const Edge* AddEdge(Node* source, int x, Node* dest, int y);
// Adds a control edge (no data flows along this edge) that connects `source`
// to `dest`. If `dest`s NodeDef is missing the corresponding control input,
// adds the control input.
//
// If such a control edge already exists and `allow_duplicates` is false, no
// edge is added and the function returns nullptr. Otherwise the edge is
// unconditionally created and returned. The NodeDef is not updated if
// `allow_duplicates` is true.
// TODO(skyewm): // TODO(skyewm): allow_duplicates is needed only by
// graph_partition.cc. Figure out if we can do away with it.
const Edge* AddControlEdge(Node* source, Node* dest,
bool allow_duplicates = false);
// Removes edge from the graph. Does not update the destination node's
// NodeDef.
// REQUIRES: The edge must exist.
void RemoveEdge(const Edge* edge);
// Removes control edge `edge` from the graph. Note that this also updates
// the corresponding NodeDef to reflect the change.
// REQUIRES: The control edge must exist.
void RemoveControlEdge(const Edge* e);
// Updates the input to a node. The existing edge to `dst` is removed and an
// edge from `new_src` to `dst` is created. The NodeDef associated with `dst`
// is also updated.
Status UpdateEdge(Node* new_src, int new_src_index, Node* dst, int dst_index);
// Adds the function and gradient definitions in `fdef_lib` to this graph's op
// registry. Ignores duplicate functions, and returns a bad status if an
// imported function differs from an existing function or op with the same
// name.
Status AddFunctionLibrary(const FunctionDefLibrary& fdef_lib);
// The number of live nodes in the graph.
//
// Because nodes can be removed from the graph, num_nodes() is often
// smaller than num_node_ids(). If one needs to create an array of
// nodes indexed by node ids, num_node_ids() should be used as the
// array's size.
int num_nodes() const { return num_nodes_; }
// The number of live nodes in the graph, excluding the Source and Sink nodes.
int num_op_nodes() const {
DCHECK_GE(num_nodes_, 2);
return num_nodes_ - 2;
}
// The number of live edges in the graph.
//
// Because edges can be removed from the graph, num_edges() is often
// smaller than num_edge_ids(). If one needs to create an array of
// edges indexed by edge ids, num_edge_ids() should be used as the
// array's size.
int num_edges() const { return num_edges_; }
// Serialize the nodes starting at `from_node_id` to a GraphDef.
void ToGraphDefSubRange(GraphDef* graph_def, int from_node_id) const;
// Serialize to a GraphDef.
void ToGraphDef(GraphDef* graph_def) const;
// This version can be called from debugger to inspect the graph content.
// Use the previous version outside debug context for efficiency reasons.
//
// Note: We do not expose a DebugString() API, since GraphDef.DebugString() is
// not defined in some TensorFlow builds.
GraphDef ToGraphDefDebug() const;
// Generate new node name with the specified prefix that is unique
// across this graph.
string NewName(StringPiece prefix);
// Access to the list of all nodes. Example usage:
// for (Node* node : graph.nodes()) { ... }
gtl::iterator_range<NodeIter> nodes() const;
// Access to the list of all nodes, excluding the Source and Sink nodes.
gtl::iterator_range<NodeIter> op_nodes() const;
// Returns one more than the maximum id assigned to any node.
int num_node_ids() const { return nodes_.size(); }
// Returns the node associated with an id, or nullptr if no node
// with that id (the node with that id was removed and the id has
// not yet been re-used). *this owns the returned instance.
// REQUIRES: 0 <= id < num_node_ids().
Node* FindNodeId(int id) const { return nodes_[id]; }
// Returns one more than the maximum id assigned to any edge.
int num_edge_ids() const { return edges_.size(); }
// Returns the Edge associated with an id, or nullptr if no edge
// with that id (the node with that id was removed and the id has
// not yet been re-used). *this owns the returned instance.
// REQUIRES: 0 <= id < num_node_ids().
const Edge* FindEdgeId(int id) const { return edges_[id]; }
// Access to the set of all edges. Example usage:
// for (const Edge* e : graph.edges()) { ... }
GraphEdgesIterable edges() const { return GraphEdgesIterable(edges_); }
// The pre-defined nodes.
enum { kSourceId = 0, kSinkId = 1 };
Node* source_node() const { return FindNodeId(kSourceId); }
Node* sink_node() const { return FindNodeId(kSinkId); }
const OpRegistryInterface* op_registry() const { return &ops_; }
const FunctionLibraryDefinition& flib_def() const { return ops_; }
void CheckDeviceNameIndex(int index) {
DCHECK_GE(index, 0);
DCHECK_LT(index, static_cast<int>(device_names_.size()));
}
int InternDeviceName(const string& device_name);
const string& get_assigned_device_name(const Node& node) const {
return device_names_[node.assigned_device_name_index()];
}
void set_assigned_device_name_index(Node* node, int device_name_index) {
CheckDeviceNameIndex(device_name_index);
node->assigned_device_name_index_ = device_name_index;
}
void set_assigned_device_name(Node* node, const string& device_name) {
node->assigned_device_name_index_ = InternDeviceName(device_name);
}
// Returns OK if `node` is non-null and belongs to this graph
Status IsValidNode(const Node* node) const;
// Returns OK if IsValidNode(`node`) and `idx` is less than
// node->num_outputs()
Status IsValidOutputTensor(const Node* node, int idx) const;
// Returns OK if IsValidNode(`node`) and `idx` is less than
// node->num_inputs()
Status IsValidInputTensor(const Node* node, int idx) const;
// Create and return a new WhileContext owned by this graph. This is called
// when a new while loop is created. `frame_name` must be unique among
// WhileContexts in this graph.
Status AddWhileContext(StringPiece frame_name, std::vector<Node*> enter_nodes,
std::vector<Node*> exit_nodes,
OutputTensor cond_output,
std::vector<OutputTensor> body_inputs,
std::vector<OutputTensor> body_outputs,
WhileContext** result);
// TODO(josh11b): uint64 hash() const;
private:
// If cost_node is non-null, then cost accounting (in CostModel)
// will be associated with that node rather than the new one being
// created.
//
// Ownership of the returned Node is not transferred to caller.
Node* AllocateNode(std::shared_ptr<NodeProperties> props,
const Node* cost_node);
void ReleaseNode(Node* node);
// Registry of all known ops, including functions.
FunctionLibraryDefinition ops_;
// GraphDef versions
const std::unique_ptr<VersionDef> versions_;
// Allocator which will give us good locality.
core::Arena arena_;
// Map from node ids to allocated nodes. nodes_[id] may be nullptr if
// the node with that id was removed from the graph.
std::vector<Node*> nodes_;
// Number of nodes alive.
int64 num_nodes_ = 0;
// Map from edge ids to allocated edges. edges_[id] may be nullptr if
// the edge with that id was removed from the graph.
std::vector<Edge*> edges_;
// The number of entries in edges_ that are not nullptr.
int num_edges_ = 0;
// Allocated but free nodes and edges.
std::vector<Node*> free_nodes_;
std::vector<Edge*> free_edges_;
// For generating unique names.
int name_counter_ = 0;
// In most graphs, the number of unique values used for the
// Node::assigned_device_name() property is quite small. If the graph is
// large, then this duplication of values can consume a significant amount of
// memory. Instead, we represent the same information using an interning
// table, which consists of a vector of unique strings (device_names_), as
// well a map (device_names_map_) from unique strings to indices within the
// unique string table.
//
// The InternDeviceName() method handles adding a new entry into the table,
// or locating the index of an existing entry.
//
// The fact that Node::assigned_device_name() is implemented using an
// interning table is intentionally public. This allows algorithms that
// frequently access this field to do so efficiently, especially for the case
// where the assigned_device_name of one Node is copied directly from that
// of another Node.
// A table of the unique assigned device names. Indices do NOT correspond
// to node IDs. Index 0 is always the empty string.
std::vector<string> device_names_;
// Maps unique device names to indices within device_names_[i].
std::unordered_map<string, int> device_names_map_;
// All the while contexts owned by this graph, keyed by frame name,
// corresponding to all the while loops contained in this graph (including
// nested loops). The stored contexts are usually accessed via
// AddWhileContext() or Node::while_ctx(), but this manages the lifetime.
std::map<string, WhileContext> while_ctxs_;
TF_DISALLOW_COPY_AND_ASSIGN(Graph);
};
// TODO(josh11b): We may want to support keeping an index on various
// node/edge attributes in a graph, particularly node names.
// Helper routines
inline bool IsSource(const Node* node) { return node->IsSource(); }
inline bool IsSink(const Node* node) { return node->IsSink(); }
inline bool IsSwitch(const Node* node) { return node->IsSwitch(); }
inline bool IsMerge(const Node* node) { return node->IsMerge(); }
inline bool IsEnter(const Node* node) { return node->IsEnter(); }
inline bool IsExit(const Node* node) { return node->IsExit(); }
inline bool IsNextIteration(const Node* n) { return n->IsNextIteration(); }
inline bool IsLoopCond(const Node* node) { return node->IsLoopCond(); }
inline bool IsControlTrigger(const Node* n) { return n->IsControlTrigger(); }
inline bool IsSend(const Node* node) { return node->IsSend(); }
inline bool IsRecv(const Node* node) { return node->IsRecv(); }
inline bool IsHostSend(const Node* node) { return node->IsHostSend(); }
inline bool IsHostRecv(const Node* node) { return node->IsHostRecv(); }
// True for Nodes that mediate the transfer of values between processes.
inline bool IsTransferNode(const Node* n) { return IsSend(n) || IsRecv(n); }
inline bool IsConstant(const Node* node) { return node->IsConstant(); }
inline bool IsVariable(const Node* node) { return node->IsVariable(); }
inline bool IsIdentity(const Node* node) { return node->IsIdentity(); }
// Returns true iff 'n' is a control flow node.
inline bool IsControlFlow(const Node* n) { return n->IsControlFlow(); }
// Returns true if the node only depends on its input's metadata
// (shape). Specifically, returns true for "Size", "Shape" and "Rank" ops.
inline bool IsMetadata(const Node* n) { return n->IsMetadata(); }
inline bool IsScopedAllocator(const Node* n) { return n->IsScopedAllocator(); }
inline bool IsHostMemoryPreserving(const Node* node) {
return IsIdentity(node) || IsControlFlow(node);
}
// Iterator for stepping through the nodes of a graph.
class NodeIter {
public:
NodeIter(const Graph* graph, int id);
bool operator==(const NodeIter& rhs);
bool operator!=(const NodeIter& rhs);
void operator++();
Node* operator*();
Node* operator->();
private:
// Invariant: id_ == graph_->num_node_ids() || graph_->FindId(id_) != nullptr
const Graph* graph_;
int id_;
};
// Iterator for stepping through the neighbors of a node.
class NeighborIter {
public:
NeighborIter(EdgeSet::const_iterator iter, bool incoming);
bool operator==(const NeighborIter& rhs);
bool operator!=(const NeighborIter& rhs);
void operator++();
Node* operator*();
Node* operator->();
private:
EdgeSet::const_iterator iter_;
bool incoming_;
};
// IMPLEMENTATION DETAILS, PLEASE IGNORE
inline NodeIter::NodeIter(const Graph* graph, int id)
: graph_(graph), id_(id) {}
inline bool NodeIter::operator==(const NodeIter& rhs) {
DCHECK(graph_ == rhs.graph_);
return id_ == rhs.id_;
}
inline bool NodeIter::operator!=(const NodeIter& rhs) {
return !(*this == rhs);
}
inline void NodeIter::operator++() {
while (1) {
DCHECK_LE(id_, graph_->num_node_ids());
++id_;
if (id_ >= graph_->num_node_ids() || graph_->FindNodeId(id_) != nullptr) {
return;
}
}
}
inline Node* NodeIter::operator*() { return graph_->FindNodeId(id_); }
inline Node* NodeIter::operator->() { return graph_->FindNodeId(id_); }
inline NeighborIter::NeighborIter(EdgeSet::const_iterator iter, bool incoming)
: iter_(iter), incoming_(incoming) {}
inline bool NeighborIter::operator==(const NeighborIter& rhs) {
return iter_ == rhs.iter_ && incoming_ == rhs.incoming_;
}
inline bool NeighborIter::operator!=(const NeighborIter& rhs) {
return !(*this == rhs);
}
inline void NeighborIter::operator++() { ++iter_; }
inline Node* NeighborIter::operator*() {
const Edge* e = *iter_;
return incoming_ ? e->src() : e->dst();
}
inline Node* NeighborIter::operator->() {
const Edge* e = *iter_;
return incoming_ ? e->src() : e->dst();
}
inline bool Edge::IsControlEdge() const {
// Note that if either src_output_ or dst_input_ is kControlSlot,
// so is the other one (AddEdge checks this).
return src_output_ == Graph::kControlSlot;
}
inline gtl::iterator_range<NodeIter> Graph::nodes() const {
// Note that NodeId 0 is always valid since we don't let the source
// node be removed from the graph.
return gtl::make_range(NodeIter(this, 0), NodeIter(this, num_node_ids()));
}
inline gtl::iterator_range<NodeIter> Graph::op_nodes() const {
// Note that NodeId 0 is always valid since we don't let the source
// node be removed from the graph.
//
// The current implementation of Graph maintains the invariant that the
// first two nodes are the source and sink nodes, and all other nodes are op
// nodes. This method (op_nodes()) relies on this invariant.
NodeIter begin(this, 0);
NodeIter end(this, num_node_ids());
if (begin != end) {
++begin;
}
if (begin != end) {
++begin;
}
return gtl::make_range(begin, end);
}
inline void Node::set_assigned_device_name_index(int index) {
graph_->CheckDeviceNameIndex(index);
assigned_device_name_index_ = index;
}
inline void Node::set_assigned_device_name(const string& device_name) {
graph_->set_assigned_device_name(this, device_name);
}
inline const string& Node::assigned_device_name() const {
return graph_->get_assigned_device_name(*this);
}
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_GRAPH_H_
@@ -0,0 +1,189 @@
/* 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_CORE_GRAPH_GRAPH_CONSTRUCTOR_H_
#define TENSORFLOW_CORE_GRAPH_GRAPH_CONSTRUCTOR_H_
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/graph/tensor_id.h"
#include "tensorflow/core/lib/core/status.h"
namespace tensorflow {
class ShapeRefiner;
// Construct a Graph *g out of a GraphDef gdef. Returns non-OK on
// error, in which case *g is left in an incomplete state.
//
// *g is expected to be an empty graph (with no more than a source and sink
// nodes) when provided to ConvertGraphDefToGraph. To enhance an existing Graph,
// see ImportGraphDef.
struct GraphConstructorOptions {
GraphConstructorOptions() {}
// If true, allows internal ops in the GraphDef.
bool allow_internal_ops = false;
// If true, the graph def is expected to have fully specified
// devices for all nodes. A node in the resulting graph "g" has the
// device name set accordingly.
//
// TODO(zhifengc): if possible, consider removing this option.
bool expect_device_spec = false;
};
extern Status ConvertGraphDefToGraph(const GraphConstructorOptions& opts,
const GraphDef& gdef, Graph* g);
// Same as ConvertGraphDefToGraph, but takes just nodes. Used by function
// instantiation.
// TODO(irving): This will turn into std::vector<NodeInfoPtr> soon.
extern Status ConvertNodeDefsToGraph(const GraphConstructorOptions& opts,
gtl::ArraySlice<NodeDef> nodes, Graph* g);
// Options for calling ImportGraphDef().
struct ImportGraphDefOptions {
ImportGraphDefOptions()
: uniquify_names(false),
uniquify_prefix(false),
skip_mapped_nodes(false),
validate_shape(true) {}
// Name prefix to use for nodes imported from the GraphDef. For example, if
// prefix="animals" and GraphDef contains a node "bunny" then the node will be
// named "animals/bunny" in *g. Must not be already used as a node name or
// prefix in the graph.
string prefix;
// If true, imported node names will be modified if their name already exists
// in the graph. If false, conflicting names will be treated as an error. Note
// that this option has no effect if `prefix` is specified, since `prefix`
// will guarantee all node names are unique.
bool uniquify_names;
// If true, `prefix` will be modified if it already exists as a node name or
// prefix in the graph. If false, a conflicting prefix will be treated as an
// error. This option has no effect if `prefix` isn't specified.
bool uniquify_prefix;
// Maps tensors in `gdef` to existing tensors in `g`. Inputs in `gdef`
// corresponding to `input_map` keys will be remapped to the nodes in `g`
// corresponding to the values.
//
// Keys should not include `prefix`, i.e., a key ID's name should be the name
// as it originally appears in `gdef`.
//
// If this is non-empty, ImportGraphDef must be called with the shape refiner
// used to create the existing nodes referenced in `input_map`.
// TODO(skyewm): can we remove this requirement? How do we access the original
// shape refiner?
std::map<SafeTensorId, SafeTensorId> input_map;
// If true, nodes that will have all output edges removed because of
// overrides in `input_map` will not be imported.
bool skip_mapped_nodes;
// The names of existing nodes in `g` that the imported graph should have
// control dependencies on.
//
// Note that to avoid creating many redundant control edges, ImportGraphDef()
// won't add control edges to nodes that will inherit the dependencies from
// other nodes in `gdef`.
std::vector<string> control_dependencies;
// Tensors in `gdef` that will be returned via the ImportGraphDefResults
// output parameter of `ImportGraphDef()`. If this list is non-empty, the
// caller must pass a results object to `ImportGraphDef()`. The
// `return_tensors` field will be populated with the imported nodes in `g`.
//
// Entries should not include `prefix`, i.e., each ID's name should be the
// name as it originally appears in `gdef`.
//
// If this contains a tensor that's also being remapped via `input_map`, the
// corresponding existing tensor in `g` will be returned.
std::vector<SafeTensorId> return_tensors;
// The names of nodes in `gdef` that will be returned via the
// ImportGraphDefResults output parameter of `ImportGraphDef()`. If this list
// is non-empty, the caller must pass a results object to
// `ImportGraphDef()`. The `return_nodes` field will be populated with the
// imported nodes in `g`.
//
// Entries should not include `prefix`, i.e., each node's name should be the
// name as it originally appears in `gdef`.
//
// Unlike `return_tensors`, `input_map` has no effect on the nodes
// returned. `return_nodes` must be empty if `skip_mapped_nodes` is true.
// TODO(skyewm): make this work with `skip_mapped_nodes` if there's a need.
std::vector<string> return_nodes;
// If true, checks that all colocation constraints are nodes in the GraphDef.
bool validate_colocation_constraints = true;
// If false skips shape validation.
bool validate_shape;
// TODO(ashankar): Enable handling of GraphDefs produced by newer binaries
// with ops that are not defined in the binary calling ImportGraphDef.
// Similar to the producer_op_list argument to import_graph_def in the
// python API.
};
// Optional results that may be returned by ImportGraphDef.
struct ImportGraphDefResults {
// The requested tensors associated with
// ImportGraphDefOptions::return_tensors. Note that the index may be different
// than the requested index if the returned tensor has been remapped according
// to `input_map`.
typedef int Index;
std::vector<std::pair<Node*, Index>> return_tensors;
// The requested nodes associated with ImportGraphDefOptions::return_nodes.
std::vector<Node*> return_nodes;
// Keys in ImportGraphDefOptions::input_map that don't appear in `gdef` and
// weren't used as an input to any node in `gdef`. These keys are likely due
// to typos, and callers may wish to treat their existence as an error.
std::vector<SafeTensorId> missing_unused_input_map_keys;
};
// Adds the graph in GraphDef `gdef` into an existing Graph `*g`.
//
// On error, returns non-OK and leaves `*g` unmodified.
//
// `refiner` can be null. It should be non-null if the caller
// intends to add additional nodes to the graph after the import. This
// allows the caller to validate shapes of those nodes (since
// ShapeRefiner::AddNode must be called in topological order).
//
// `results` must be non-null if `opts.return_tensors` or `opts.result_nodes` is
// non-empty. It can also be set to fetch the unused input map keys. If it's
// non-null, all the vector fields must be empty.
//
// TODO(ashankar): Push this mechanism and get rid of Session::Extend()
// as a means of enhancing an existing Graph.
extern Status ImportGraphDef(const ImportGraphDefOptions& opts,
const GraphDef& gdef, Graph* g,
ShapeRefiner* refiner,
ImportGraphDefResults* results = nullptr);
// Make a copy of "src" into "*dest".
//
// REQUIRES: "*dest" is a freshly allocated graph without any nodes or edges
// other than the implicit Source/Sink nodes.
extern void CopyGraph(const Graph& src, Graph* dest);
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_GRAPH_CONSTRUCTOR_H_
@@ -0,0 +1,206 @@
/* 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_CORE_GRAPH_GRAPH_DEF_BUILDER_H_
#define TENSORFLOW_CORE_GRAPH_GRAPH_DEF_BUILDER_H_
#include <vector>
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/graph/node_builder.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/gtl/array_slice.h"
namespace tensorflow {
// Given a function like:
// namespace ops {
// Node* Identity(NodeOut input, const GraphDefBuilder::Options& opts) {
// if (opts.HaveError()) return nullptr;
// static const string kOpName = "Identity";
// NodeBuilder node_builder(opts.GetNameForOp(kOpName), kOpName,
// opts.op_registry());
// node_builder.Input(input);
// return opts.FinalizeBuilder(&node_builder);
// }
// } // namespace ops
//
// // Or, alternatively:
// namespace ops {
// Node* Identity(NodeOut input, const GraphDefBuilder::Options& opts) {
// static const string kOpName = "Identity";
// return UnaryOp(kOpName, input, opts);
// }
// } // namespace ops
//
// You call it like:
// GraphDefBuilder b;
// using namespace ::tensorflow::ops; // NOLINT(build/namespaces)
// Node* na = Const(7, b.opts());
// // Note: WithName() returns a copy, opts is unchanged.
// Node* nb = Const(5, b.opts().WithName("control-input"));
// Node* nc = Identity(na, b.opts().WithControlInput(nb));
// GraphDef graph_def;
// Status status = b.ToGraphDef(&graph_def);
// if (!status.ok()) { /* Handle error */ }
//
// In tests you can skip the status handling via:
// GraphDefBuilder b(GraphDefBuilder::kFailImmediately);
// ...
// b.ToGraphDef(&graph_def);
class GraphDefBuilder {
public:
// Options for adding a Node to a Graph.
class Options {
public:
// Sets the Graph (that Nodes will be added to) and the status. The
// status may be set to nullptr, in which case errors cause CHECK
// failures. The graph and status must outlive *this.
Options(Graph* graph, Status* status);
~Options();
// Methods for setting options. These are const methods: they
// return a copy of *this with the option set.
Options WithName(StringPiece name) const;
Options WithDevice(StringPiece device) const;
Options WithControlInput(Node* control_input) const;
Options WithControlInputs(gtl::ArraySlice<Node*> control_inputs) const;
// Override the default value for an optional attr.
template <class T>
Options WithAttr(StringPiece attr_name, T&& value) const {
return Options(*this).WithAttrImpl(attr_name, std::forward<T>(value));
}
// Note: overload needed to allow {...} expressions for value.
template <class T>
Options WithAttr(StringPiece attr_name,
std::initializer_list<T> value) const {
return WithAttr<std::initializer_list<T>>(attr_name, std::move(value));
}
// Methods for using options from a function that creates a Node.
// Returns true if the status associated with *this has an error.
// Use this to skip processing that may depend on prior results.
bool HaveError() const { return status_ != nullptr && !status_->ok(); }
// Returns a string representation of the status associated with *this.
// Returns the string `"OK"` if the status doesn't have any error.
string StatusToString() const { return status_->ToString(); }
// Given the Op type name, return a name for a node of that type.
// Uses the value set in WithName() if that has been called. Otherwise,
// returns a name built out of the Op type name.
string GetNameForOp(StringPiece op) const;
// Sets the device, adds control inputs, adds attrs, and calls Finalize().
// If Finalize returns an error, it is saved and this function returns
// nullptr.
Node* FinalizeBuilder(NodeBuilder* builder) const;
// Updates the associated status, if any, or calls TF_CHECK_OK if none.
void UpdateStatus(const Status& status) const;
// Accessor
const OpRegistryInterface* op_registry() const {
return graph_->op_registry();
}
private:
Options WithNameImpl(StringPiece name);
Options WithDeviceImpl(StringPiece device);
Options WithControlInputImpl(Node* control_input);
Options WithControlInputsImpl(gtl::ArraySlice<Node*> control_inputs);
template <class T>
Options WithAttrImpl(StringPiece name, T&& value) {
attrs_.emplace_back(string(name), AttrValue());
SetAttrValue(std::forward<T>(value), &attrs_.back().second);
return *this;
}
Graph* const graph_;
Status* const status_;
string name_;
string device_;
std::vector<Node*> control_inputs_;
std::vector<std::pair<string, AttrValue>> attrs_;
};
// Start building a new graph.
explicit GraphDefBuilder(
const OpRegistryInterface* op_registry = OpRegistry::Global())
: graph_(op_registry), opts_(&graph_, &status_) {}
// For use in tests, where you want to fail immediately on error instead
// of checking the status at the end.
enum TestFailImmediatelyType { kFailImmediately };
explicit GraphDefBuilder(
TestFailImmediatelyType,
const OpRegistryInterface* op_registry = OpRegistry::Global())
: graph_(op_registry), opts_(&graph_, nullptr) {}
// Gets the Options with the associated Graph and Status.
const Options& opts() const { return opts_; }
// Once all the nodes have been added, call this to get whether it was
// successful, and if so fill *graph_def.
Status ToGraphDef(GraphDef* graph_def) const;
// Adds the function and gradient definitions in `fdef_lib` to this graph's op
// registry. Ignores duplicate functions, and returns a bad status if an
// imported function differs from an existing function or op with the same
// name.
Status AddFunctionLibrary(const FunctionDefLibrary& fdef_lib) {
return graph_.AddFunctionLibrary(fdef_lib);
}
// Returns whether a user-defined function with `name` already exists in the
// graph.
bool HasFunction(const string& name) {
return graph_.flib_def().Find(name) != nullptr;
}
private:
Graph graph_;
Status status_;
Options opts_;
};
namespace ops {
// A NodeOut may either be a regular input or back input. Regular
// inputs are specified via either a Node* or a Node* and an output
// index. Back inputs are specified by a node name, output index, and
// output type.
typedef NodeBuilder::NodeOut NodeOut;
// For adding an Op with no inputs to a GraphDefBuilder.
Node* SourceOp(const string& op_name, const GraphDefBuilder::Options& opts);
// For adding an Op with one input to a GraphDefBuilder.
Node* UnaryOp(const string& op_name, NodeOut input,
const GraphDefBuilder::Options& opts);
// For adding an Op with two inputs to a GraphDefBuilder.
Node* BinaryOp(const string& op_name, NodeOut a, NodeOut b,
const GraphDefBuilder::Options& opts);
} // namespace ops
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_GRAPH_DEF_BUILDER_H_
@@ -0,0 +1,35 @@
/* 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.
==============================================================================*/
#ifndef TENSORFLOW_CORE_GRAPH_GRAPH_DEF_BUILDER_UTIL_H_
#define TENSORFLOW_CORE_GRAPH_GRAPH_DEF_BUILDER_UTIL_H_
#include "tensorflow/core/graph/graph_def_builder.h"
#include "tensorflow/core/lib/core/status.h"
namespace tensorflow {
class Graph;
// Converts the `GraphDef` being built by `builder` to a `Graph` and
// stores it in `*graph`.
// TODO(josh11b): 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 GraphDefBuilderToGraph(const GraphDefBuilder& builder, Graph* graph);
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_GRAPH_DEF_BUILDER_UTIL_H_
@@ -0,0 +1,98 @@
/* 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_CORE_GRAPH_GRAPH_PARTITION_H_
#define TENSORFLOW_CORE_GRAPH_GRAPH_PARTITION_H_
#include <functional>
#include <string>
#include <unordered_map>
#include <vector>
#include "tensorflow/core/framework/function.h"
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/graph/costmodel.h"
#include "tensorflow/core/graph/graph.h"
namespace tensorflow {
struct PartitionOptions {
// A function that returns a location for the execution of a given
// Node.
typedef std::function<string(const Node*)> NodeToLocFunc;
NodeToLocFunc node_to_loc = nullptr;
// A function that returns a unique graph node name with the given
// prefix.
typedef std::function<string(const string&)> NewNameFunc;
NewNameFunc new_name = nullptr;
// A function that returns the incarnation of a device given the
// device's fullname. If not found, GetIncarnationFunc should return
// kIllegalIncarnation.
static const uint64 kIllegalIncarnation = 0;
typedef std::function<uint64(const string&)> GetIncarnationFunc;
GetIncarnationFunc get_incarnation = nullptr;
// If specified, flib_def defines a function library that should be
// partitioned and replicated into each resulting partition graphs.
const FunctionLibraryDefinition* flib_def = nullptr;
// True if all the control flow "code" has already been added. The
// control flow code needs to be added when we still have the entire
// graph before any partitioning. So this flag should be false for
// the first partitioning but true for all subsequent partitioning.
//
// TODO(yuanbyu): We could also make the addition of the control
// flow code incremental based on 'node_to_loc'. This makes the
// communication a broadcast tree, which could be more efficient when
// the number of participating devices is large.
bool control_flow_added = false;
// A function that returns the data type into which the tensor
// should be cast before sent over the wire.
typedef std::function<DataType(const Edge*)> ShouldCastFunc;
ShouldCastFunc should_cast = nullptr;
// Schedule the execution of the recvs based on their start times
// computed by some scheduling algorithm. The recvs are divided into
// epochs based on their start times. A recv is enabled only when
// execution reaches its epoch - N for some predefined N.
bool scheduling_for_recvs = false;
// The start time for each node in the graph computed by some scheduling
// algorithm. If 'need_to_record_start_times' is true, we record them
// in the graph as a node attribute.
bool need_to_record_start_times = false;
std::vector<Microseconds> start_times;
};
// Partition "input" graph into a set of graphs, one per location.
// The location for node n is derived by calling opts.node_to_loc(n).
// New nodes added by Partition use "opts.new_name(old_name)" to
// generate node names.
//
// Stores the partitions in *partitions.
Status Partition(const PartitionOptions& opts, Graph* input,
std::unordered_map<string, GraphDef>* partitions);
// Add control edges to the partitions to control the ordering
// and timing of the recv nodes based on the start times calculated
// using some scheduling algorithm.
Status AddControlEdges(const PartitionOptions& opts,
std::unordered_map<string, GraphDef>* partitions);
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_GRAPH_PARTITION_H_
@@ -0,0 +1,36 @@
/* 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.
==============================================================================*/
// A graph pass that rewrites graph for propagating MKL layout as a tensor
#ifndef TENSORFLOW_CORE_GRAPH_MKL_LAYOUT_PASS_H_
#define TENSORFLOW_CORE_GRAPH_MKL_LAYOUT_PASS_H_
#ifdef INTEL_MKL
#include <sys/types.h>
#include <memory>
#include "tensorflow/core/graph/graph.h"
namespace tensorflow {
// Interface to invoke the pass for unit test
//
// Returns true if and only if 'g' is mutated.
extern bool RunMklLayoutRewritePass(std::unique_ptr<Graph>* g);
} // namespace tensorflow
#endif
#endif // TENSORFLOW_CORE_GRAPH_MKL_LAYOUT_PASS_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.
==============================================================================*/
// An optimization pass that inserts MklToTf conversion nodes in the graph
#ifndef TENSORFLOW_CORE_GRAPH_MKL_TFCONVERSION_PASS_H_
#define TENSORFLOW_CORE_GRAPH_MKL_TFCONVERSION_PASS_H_
#ifdef INTEL_MKL
#include <sys/types.h>
#include <memory>
#include "tensorflow/core/graph/graph.h"
#ifdef _WIN32
typedef unsigned int uint;
#endif
namespace tensorflow {
// Interface to invoke the pass for unit test
//
// Returns true if and only if 'g' is mutated.
extern bool InsertMklToTfConversionNodes(std::unique_ptr<Graph>* g);
} // namespace tensorflow
#endif
#endif // TENSORFLOW_CORE_GRAPH_MKL_TFCONVERSION_PASS_H_
@@ -0,0 +1,167 @@
/* 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_CORE_GRAPH_NODE_BUILDER_H_
#define TENSORFLOW_CORE_GRAPH_NODE_BUILDER_H_
#include <vector>
#include "tensorflow/core/framework/node_def_builder.h"
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_def.pb.h"
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/gtl/array_slice.h"
namespace tensorflow {
// This is a helper for creating a Node and adding it to a Graph.
// Internally, it uses a NodeDefBuilder to automatically set attrs
// that can be inferred from the inputs, and use default values
// (where they exist) for unspecified attrs. Example usage:
//
// Node* node;
// Status status = NodeBuilder(node_name, op_name)
// .Input(...)
// .Attr(...)
// .Finalize(&graph, &node);
// if (!status.ok()) return status;
// // Use node here.
class NodeBuilder {
public:
// For specifying the output of a Node to provide to one of the Input()
// functions below. It supports both regular inputs (where you are
// connecting to an existing Node*), and inputs from outside the graph
// (or haven't been added to the graph yet, like back edges, where
// you don't have a Node*). Both types can be mixed, e.g. in an
// ArraySlice.
struct NodeOut {
// For referencing an existing Node.
NodeOut(Node* n, int32 i = 0);
// For referencing Nodes not in the graph being built. It is
// useful when preparing a graph for ExtendSession or creating a
// back edge to a node that hasn't been added to the graph yet,
// but will be.
NodeOut(StringPiece name, int32 i, DataType t);
// Default constructor for std::vector<NodeOut>.
NodeOut();
Node* node;
// error is set to true if:
// * the NodeOut was default constructed and never overwritten,
// * a nullptr Node* was passed to the NodeOut constructor, or
// * an out-of-range index was passed to the NodeOut constructor.
bool error;
string name;
int32 index;
DataType dt;
};
// Specify the name and the Op (either via an OpDef or the name of
// the Op plus a registry) for the Node. Other fields are
// specified by calling the methods below.
// REQUIRES: The OpDef must satisfy ValidateOpDef().
NodeBuilder(StringPiece name, StringPiece op_name,
const OpRegistryInterface* op_registry = OpRegistry::Global());
NodeBuilder(StringPiece name, const OpDef* op_def);
// Create a NodeBuilder from an existing NodeDefBuilder.
NodeBuilder(const NodeDefBuilder& def_builder);
// You must call one Input() function per input_arg in the Op,
// *and in the same order as the input_args appear in the OpDef.*
// For inputs that take a single tensor.
NodeBuilder& Input(Node* src_node, int src_index = 0);
NodeBuilder& Input(NodeOut src);
// For inputs that take a list of tensors.
NodeBuilder& Input(gtl::ArraySlice<NodeOut> src_list);
// Require that this node run after src_node(s).
NodeBuilder& ControlInput(Node* src_node);
NodeBuilder& ControlInputs(gtl::ArraySlice<Node*> src_nodes);
// Sets the "requested device spec" in the NodeDef (not the
// "assigned device" in the Node).
NodeBuilder& Device(StringPiece device_spec);
// Sets the device name in the "assigned device" field in tensorflow::Node.
NodeBuilder& AssignedDevice(StringPiece device);
// Set the value of an attr. attr_name must match the name of one of
// attrs defined by the Op, and value must have the corresponding type
// (see SetAttrValue() in ../framework/attr_value_util.h for legal
// types for value). Note that attrs will be set automatically if
// they can be determined by the inputs.
template <class T>
NodeBuilder& Attr(StringPiece attr_name, T&& value);
template <class T>
NodeBuilder& Attr(StringPiece attr_name, std::initializer_list<T> value);
// Validates the described node and adds it to *graph, adding edges
// for all (non-back) inputs. If created_node is not nullptr,
// *created_node will be set to the new node (or nullptr on error).
Status Finalize(Graph* graph, Node** created_node) const;
// Accessors for the values set in the constructor.
const string& node_name() const { return def_builder_.node_name(); }
const OpDef& op_def() const { return def_builder_.op_def(); }
private:
static DataType SafeGetOutput(const Node* node, int i, bool* error) {
if (node != nullptr && i >= 0 && i < node->num_outputs()) {
*error = false;
return node->output_type(i);
} else {
*error = true;
return DT_FLOAT;
}
}
// If SafeGetOutput indicates a range error, add it to errors_.
void AddIndexError(const Node* node, int i);
// Set *dt and returns true if i is in range. Combines
// SafeGetOutput() and AddIndexError().
bool GetOutputType(const Node* node, int i, DataType* dt);
NodeDefBuilder def_builder_;
std::vector<NodeOut> inputs_;
std::vector<Node*> control_inputs_;
std::vector<string> errors_;
string assigned_device_;
};
// IMPLEMENTATION -------------------------------------------------------------
template <class T>
NodeBuilder& NodeBuilder::Attr(StringPiece attr_name, T&& value) {
def_builder_.Attr(attr_name, std::forward<T>(value));
return *this;
}
template <class T>
NodeBuilder& NodeBuilder::Attr(StringPiece attr_name,
std::initializer_list<T> value) {
def_builder_.Attr(attr_name, value);
return *this;
}
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_NODE_BUILDER_H_
@@ -0,0 +1,37 @@
/* 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.
==============================================================================*/
// An optimization pass that performs common subexpression elimination.
#ifndef TENSORFLOW_CORE_GRAPH_OPTIMIZER_CSE_H_
#define TENSORFLOW_CORE_GRAPH_OPTIMIZER_CSE_H_
#include <sys/types.h>
#include "tensorflow/core/graph/graph.h"
namespace tensorflow {
// Perform common-subexpression elimination on the graph "*g". If
// "consider_fn" is not nullptr, then only nodes for which
// consider_fn(node) returns true will be considered for combining
// during the common subexpression elimination.
//
// Returns true if and only if 'g' is mutated.
extern bool OptimizeCSE(Graph* g,
const std::function<bool(const Node*)>& consider_fn);
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_OPTIMIZER_CSE_H_
@@ -0,0 +1,56 @@
/* 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_CORE_GRAPH_QUANTIZE_TRAINING_H_
#define TENSORFLOW_CORE_GRAPH_QUANTIZE_TRAINING_H_
#include "tensorflow/core/graph/graph.h"
namespace tensorflow {
// Rewrites graph for quantized training.
// Rewrites the forward pass to include the precision loss with quantization so
// the model can learn to deal with such loss and achieve better accuracy when
// it is quantized later for inference.
// Note that the num_bits should be in [1, 63] and 'g' must be not null.
// quant_op_type specifies which quantization op should be used.
// Current ops supported:
// - QuantizeAndDequantizeV2.
// - FakeQuantWithMinMaxVars.
//
// On success, returns OK.
//
// On failure, returns the error status. Possible errors include:
// - num_bits out of range.
// - g is null.
// - More than 1 unknown ops encountered.
Status DoQuantizeTraining(int32 num_bits, const string& quant_op_type,
Graph* g);
// Converts the input serialized GraphDef and returns a rewritten serialized
// GraphDef for quantized training.
Status DoQuantizeTrainingOnSerializedGraphDef(const string& input_graph,
int32 num_bits,
const string& quant_op_type,
string* result_graph);
// Converts the input GraphDef and returns a rewritten GraphDef for quantized
// training.
Status DoQuantizeTrainingOnGraphDef(const GraphDef& input_graphdef,
int32 num_bits, const string& quant_op_type,
GraphDef* result_graphdef);
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_QUANTIZE_TRAINING_H_
@@ -0,0 +1,165 @@
/* 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_CORE_GRAPH_SUBGRAPH_H_
#define TENSORFLOW_CORE_GRAPH_SUBGRAPH_H_
#include <string>
#include "tensorflow/core/framework/device_attributes.pb.h"
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/graph/node_builder.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/gtl/array_slice.h"
#include "tensorflow/core/protobuf/config.pb.h"
namespace tensorflow {
namespace subgraph {
// Information about a graph rewritten by `RewriteGraphForExecution()`.
struct RewriteGraphMetadata {
// The element type of each tensor fed to this subgraph. The order
// of types corresponds to the order of tensor names in
// `fed_outputs` when calling `RewriteGraphForExecution()`.
DataTypeVector feed_types;
// The element type of each tensor fetched from this subgraph. The
// order of types corresponds to the order of tensor names in
// `fetch_outputs` when calling `RewriteGraphForExecution()`.
DataTypeVector fetch_types;
};
// Describes the action to take on a particular tensor endpoint (described by
// a "<node_name>:<output_index>" pair) when pruning the graph.
//
// The `AddNode()` method must be overridden to describe this action. The method
// will be invoked once during `RewriteGraphForExecution()` with tensor endpoint
// named by `endpoint_name`, and it may either create a single new node, or fail
// with an error if the resulting graph would be invalid.
class PruneRewrite {
public:
// `endpoint_name` and `device_info` must outlive this object.
PruneRewrite(const string* endpoint_name, const DeviceAttributes* device_info)
: endpoint_name_(endpoint_name), device_info_(device_info) {}
virtual ~PruneRewrite() {}
// Creates a new node whose output replaces the given `tensor` in graph `g`.
// The node will be assigned to the device named in `device_info`.
virtual Status AddNode(Graph* g, NodeBuilder::NodeOut tensor,
Node** out_node) = 0;
// Returns the name of the tensor to which this rewrite applies.
const string& endpoint_name() { return *endpoint_name_; }
protected:
// The device on which the new node will be created.
const DeviceAttributes& device_info() { return *device_info_; }
private:
const string* const endpoint_name_; // Not owned.
const DeviceAttributes* const device_info_; // Not owned.
};
// Rewrite the graph structure of "*g" to deal with feeding node
// outputs, fetching node outputs, and only running a subset of the
// graph. "fed_outputs" and "fetch_outputs" are both lists of
// output tensor identifiers in the form of
// "<name>[:<optional_output_index>]", and "target_nodes_str" is a
// lists of target node names in "*g" "g".
//
// In the resulting graph "*g", output edges in "fed_outputs" have
// been redirected to special "_recv" nodes introduced into the graph.
// If these fed nodes are not needed in order to compute the effects
// of the nodes in "target_node_names" and "fetch_outputs", then these may
// be omitted from the graph.
//
// In the resulting graph "*g", additional "_send" nodes are connected
// to every output in "fetch_outputs". These "_send" nodes are set up
// to execute on the device described by device_info.
//
// On success, returns OK, and sets "*g" to a version of "*g"
// that represents the portions of the graph necessary for producing
// the output of all nodes listed in "target_node_names" and fetching the
// specific node outputs specified in "fetch_outputs".
//
// On failure, returns the error status. Possible errors include:
// - fed output "node:output_index" does not exist in "*g"
// - fetch output "node:output_index" does not exist in "*g"
// - target node "node" does not exist in "*g"
Status RewriteGraphForExecution(
Graph* g, const gtl::ArraySlice<string>& fed_outputs,
const gtl::ArraySlice<string>& fetch_outputs,
const gtl::ArraySlice<string>& target_node_names,
const DeviceAttributes& device_info, bool use_function_convention,
RewriteGraphMetadata* out_metadata);
// A more general version of the above function that supports
// customizable rewriting actions for each fed and fetched tensor.
Status RewriteGraphForExecution(
Graph* g, const std::vector<std::unique_ptr<PruneRewrite>>& feed_rewrites,
const std::vector<std::unique_ptr<PruneRewrite>>& fetch_rewrites,
const gtl::ArraySlice<string>& target_node_names,
RewriteGraphMetadata* out_metadata);
/////////////////////////////////////////////////////////
// Custom rewrite actions for fed and fetched tensors. //
/////////////////////////////////////////////////////////
// A rewrite action that adds an _Arg node for a fed tensor.
class ArgFeedRewrite : public PruneRewrite {
public:
ArgFeedRewrite(const string* endpoint_name,
const DeviceAttributes* device_info, int32 arg_index)
: PruneRewrite(endpoint_name, device_info), arg_index_(arg_index) {}
Status AddNode(Graph* g, NodeBuilder::NodeOut feed_tensor,
Node** out_node) override;
private:
const int32 arg_index_;
};
// A rewrite action that adds a client-terminated _Recv node for a fed tensor.
class RecvFeedRewrite : public PruneRewrite {
public:
using PruneRewrite::PruneRewrite;
Status AddNode(Graph* g, NodeBuilder::NodeOut feed_tensor,
Node** out_node) override;
};
// A rewrite action that adds a _Retval node for a fetched tensor.
class RetvalFetchRewrite : public PruneRewrite {
public:
RetvalFetchRewrite(const string* endpoint_name,
const DeviceAttributes* device_info, int32 retval_index)
: PruneRewrite(endpoint_name, device_info), retval_index_(retval_index) {}
Status AddNode(Graph* g, NodeBuilder::NodeOut fetch_tensor,
Node** out_node) override;
private:
const int32 retval_index_;
};
// A rewrite action that adds a client-terminated _Send node for a
// fetched tensor.
class SendFetchRewrite : public PruneRewrite {
public:
using PruneRewrite::PruneRewrite;
Status AddNode(Graph* g, NodeBuilder::NodeOut fetch_tensor,
Node** out_node) override;
};
} // namespace subgraph
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_SUBGRAPH_H_
@@ -0,0 +1,86 @@
/* 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_GRAPH_TENSOR_ID_H_
#define TENSORFLOW_GRAPH_TENSOR_ID_H_
#include <string>
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/hash/hash.h"
#include "tensorflow/core/lib/strings/strcat.h"
namespace tensorflow {
struct SafeTensorId;
// Identifier for a tensor within a step.
// first == operation_name, second == output_index
// Note: does not own backing storage for name.
struct TensorId : public std::pair<StringPiece, int> {
typedef std::pair<StringPiece, int> Base;
// Inherit the set of constructors.
using Base::pair;
// NOTE(skyewm): this is required on some platforms. I'm not sure why the
// using statement above isn't always sufficient.
TensorId() : Base() {}
TensorId(const SafeTensorId& id);
string ToString() const {
if (second == Graph::kControlSlot) return strings::StrCat("^", first);
return strings::StrCat(first, ":", second);
}
struct Hasher {
public:
std::size_t operator()(const TensorId& x) const {
return Hash32(x.first.data(), x.first.size(), x.second);
}
};
};
TensorId ParseTensorName(const string& name);
TensorId ParseTensorName(StringPiece name);
// Same as TensorId, except owns the backing storage for the op name. This makes
// the memory management simpler at the expense of a copy.
struct SafeTensorId : public std::pair<string, int> {
typedef std::pair<string, int> Base;
// NOTE(skyewm): this is required on some platforms. I'm not sure why the
// using "using Base::pair;" isn't always sufficient.
SafeTensorId() : Base() {}
SafeTensorId(const string& str, int idx) : Base(str, idx) {}
SafeTensorId(const TensorId& id);
string ToString() const {
if (second == Graph::kControlSlot) return strings::StrCat("^", first);
return strings::StrCat(first, ":", second);
}
struct Hasher {
public:
std::size_t operator()(const TensorId& x) const {
return Hash32(x.first.data(), x.first.size(), x.second);
}
};
};
} // namespace tensorflow
#endif // TENSORFLOW_GRAPH_TENSOR_ID_H_
@@ -0,0 +1,225 @@
/* 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.
==============================================================================*/
// DEPRECATED: Use the C++ API defined in tensorflow/cc instead.
#ifndef TENSORFLOW_CORE_GRAPH_TESTLIB_H_
#define TENSORFLOW_CORE_GRAPH_TESTLIB_H_
#include <string>
#include <vector>
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/graph/types.h"
#include "tensorflow/core/platform/types.h"
namespace tensorflow {
namespace test {
namespace graph {
// Converts "g" into its corresponding GraphDef "def".
ABSL_DEPRECATED("Call g->ToGraphDef(def) instead.")
void ToGraphDef(Graph* g, GraphDef* def);
// A few helpers to construct a graph.
// Adds a node in "g" producing a constant "tensor".
Node* Constant(Graph* g, const Tensor& tensor);
Node* Constant(Graph* g, const Tensor& tensor, const string& name);
// Adds a node in "g" producing a constant "tensor" on the host.
// The given node which, unlike the regular Constant above, always
// stores its output on the host. This is necessary for use
// in GPU tests where the test Op in question runs on the device
// but requires some arguments to be pinned to the host.
Node* HostConstant(Graph* g, const Tensor& tensor);
Node* HostConstant(Graph* g, const Tensor& tensor, const string& name);
// Adds a variable in "g" of the given "shape" and "dtype".
Node* Var(Graph* g, const DataType dtype, const TensorShape& shape);
Node* Var(Graph* g, const DataType dtype, const TensorShape& shape,
const string& name);
// Adds an assign node in "g" which assigns "val" into "var".
Node* Assign(Graph* g, Node* var, Node* val);
// Adds a send node "g" sending "input" as a named "tensor" from
// "sender" to "receiver".
Node* Send(Graph* g, Node* input, const string& tensor, const string& sender,
const uint64 sender_incarnation, const string& receiver);
// Adds a recv node in "g" receiving a named "tensor" from "sender"
// to "receiver".
Node* Recv(Graph* g, const string& tensor, const string& type,
const string& sender, const uint64 sender_incarnation,
const string& receiver);
// Adds a reduction "node" in "g" doing sum(data, axes). "reduce" is
// a reduction, e.g., Sum, Max, Min, Mean, etc.
Node* Reduce(Graph* g, const string& reduce, Node* data, Node* axes,
bool keep_dims = false);
// Adds a Matmul node in g doing in0.contract(in1).
Node* Matmul(Graph* g, Node* in0, Node* in1, bool transpose_a,
bool transpose_b);
// Adds a Matmul node in g doing in0.contract(in1).
Node* BatchMatmul(Graph* g, Node* in0, Node* in1, bool adj_x, bool adj_y);
// Adds a Quantize node into g that quantize floats into QUINT8. The range of
// the input float tensor is assumed to be [-1, 1].
Node* QuantizeToUINT8(Graph* g, Node* data);
// Adds a unary function "func" "node" in "g" taking "input".
Node* Unary(Graph* g, const string& func, Node* input, int index = 0);
// Adds an identity node in "g" taking "input" and producing an
// identity copy.
Node* Identity(Graph* g, Node* input, int index = 0);
// Adds a binary function "func" node in "g" taking "in0" and "in1".
Node* Binary(Graph* g, const string& func, Node* in0, Node* in1);
// Adds a function "func" node in "g" taking inputs "ins".
Node* Multi(Graph* g, const string& func, gtl::ArraySlice<Node*> ins);
// Adds a binary add node in "g" doing in0 + in1.
Node* Add(Graph* g, Node* in0, Node* in1);
// Reverses <axis> dimensions of <tensor>>
Node* Reverse(Graph* g, Node* tensor, Node* axis);
// Generates random unit uniform distribution of the input shape.
Node* RandomUniform(Graph* g, Node* input, DataType dtype);
// Generates random unit normal distribution of the input shape.
Node* RandomGaussian(Graph* g, Node* input, DataType dtype);
// Generates random gamma distribution with the given shape and alpha[s].
// Output dtype determined by alpha.
Node* RandomGamma(Graph* g, Node* shape, Node* alpha);
// Generates random poisson distribution with the given shape and lam[s].
// Output dtype determined by lam.
Node* RandomPoisson(Graph* g, Node* shape, Node* lam);
// Rolls tensor by an offset of <shift> along the corresponding
// <axis> dimensions.
Node* Roll(Graph* g, Node* input, Node* shift, Node* axis);
// Generates random parameters from the truncated standard normal distribution
// of the nput shape
Node* TruncatedNormal(Graph* g, Node* input, DataType dtype);
// Adds an error node in "g". The node's computation always
// generates an error with the given error message "errmsg".
Node* Error(Graph* g, Node* input, const string& errmsg);
// Adds a node that generates a invalid ref output.
Node* InvalidRefType(Graph* g, DataType out_type, DataType invalid_type);
// Adds a node in "g". Its Compute() sleeps a while and outputs the
// input (i.e., same as identity).
Node* Delay(Graph* g, Node* input, Microseconds delay_micros);
// Adds a no-op "node" in "g", with control inputs from all nodes in
// control_inputs vector.
Node* NoOp(Graph* g, const std::vector<Node*>& control_inputs);
// Adds a Switch node in "g". If "in1" is true, it forwards "in0" to
// output 1. Otherwise, it forwards "in0" to output 0.
Node* Switch(Graph* g, Node* in0, Node* in1);
// Adds an Enter node in "g", which enters a new frame.
Node* Enter(Graph* g, Node* input, const string& frame_name);
// Adds an Exit node in "g", which exits a frame.
Node* Exit(Graph* g, Node* input);
// Adds a Merge node in "g" with two inputs "in0" and "in1".
Node* Merge(Graph* g, Node* in0, Node* in1);
// Adds a Merge node in "g". The first input is "in0", the remaining
// inputs are only given by their names in remaining_in.
Node* Merge(Graph* g, Node* in0, gtl::ArraySlice<string> remaining_in);
// Adds a NextIteration node in "g", which makes its input available
// to the next iteration.
Node* Next(Graph* g, const string& name, Node* input);
// Adds a LoopCond node in "g", representing the "pivot" termination
// condition of a loop.
Node* LoopCond(Graph* g, Node* input);
// Adds a less node in "g", which returns true iff "in0" < "in1".
Node* Less(Graph* g, Node* in0, Node* in1);
// Adds a select node in "g", which outputs either "inx" or "iny"
// depending on the boolean value of "c".
Node* Select(Graph* g, Node* c, Node* inx, Node* iny);
// Casts "in" into data type "dst".
Node* Cast(Graph* g, Node* in, DataType dst);
// Perform gather op on params "in0" with indices "in1" and axis "axis".
Node* Gather(Graph* g, Node* in0, Node* in1, Node* axis);
// Gets a tensor stored in the session state.
Node* GetSessionTensor(Graph* g, Node* in);
// Adds a Concat node in "g". The first input is "concat_dim", the
// dimension to concatenate on, and the tensors to concatenate are
// given in "tensors".
Node* Concat(Graph* g, Node* concat_dim, gtl::ArraySlice<Node*> tensors);
// Adds a ConcatV2 node in "g". The last input is "concat_dim", the
// dimension to concatenate on, and the tensors to concatenate are
// given in "tensors".
Node* ConcatV2(Graph* g, gtl::ArraySlice<Node*> tensors, Node* concat_dim);
// Add a Relu node in "g".
Node* Relu(Graph* g, Node* in);
// Add a Relu6 node in "g".
Node* Relu6(Graph* g, Node* in);
// Add a BiasAdd node in "g".
Node* BiasAdd(Graph* g, Node* value, Node* bias);
// Add a Conv2D node in "g".
Node* Conv2D(Graph* g, Node* in0, Node* in1);
// Add a Diag node in "g".
Node* Diag(Graph* g, Node* in, DataType type);
// Add a DiagPart node in "g".
Node* DiagPart(Graph* g, Node* in, DataType type);
// Add a CheckNumerics node in "g".
Node* CheckNumerics(Graph* g, Node* in, const string& message);
// Add an _Arg node in "g".
Node* Arg(Graph* g, int64 index, DataType type);
// Add a _Retval node in "g".
Node* Retval(Graph* g, int64 index, Node* in);
} // end namespace graph
} // end namespace test
} // end namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_TESTLIB_H_
@@ -0,0 +1,35 @@
/* 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_CORE_GRAPH_TYPES_H_
#define TENSORFLOW_CORE_GRAPH_TYPES_H_
#include "tensorflow/core/lib/gtl/int_type.h"
#include "tensorflow/core/platform/types.h"
namespace tensorflow {
// We model running time in microseconds.
TF_LIB_GTL_DEFINE_INT_TYPE(Microseconds, int64);
// We can also model running time in nanoseconds for more accuracy.
TF_LIB_GTL_DEFINE_INT_TYPE(Nanoseconds, int64);
// We model size in bytes.
TF_LIB_GTL_DEFINE_INT_TYPE(Bytes, int64);
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_TYPES_H_
@@ -0,0 +1,65 @@
/* 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_CORE_GRAPH_VALIDATE_H_
#define TENSORFLOW_CORE_GRAPH_VALIDATE_H_
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/lib/core/status.h"
namespace tensorflow {
namespace graph {
// Returns OK if every NodeDef in `graph_def` is valid with respect to
// its corresponding OpDef (as defined by ValidateNodeDef()) as
// registered in `op_registry`. Also checks for deprecated ops.
//
// REQUIRES:
// * `op_registry` is not nullptr.
// * `graph_def` has default attrs filled in (see AddDefaultAttrsToGraphDef()).
Status ValidateGraphDef(const GraphDef& graph_def,
const OpRegistryInterface& op_registry);
// Like ValidateGraphDef() except it makes a copy of `graph_def` and calls
// AddDefaultAttrsToGraphDef() on the copy, removing that requirement from the
// caller.
Status ValidateGraphDefAgainstOpRegistry(
const GraphDef& graph_def, const OpRegistryInterface& op_registry);
// Like ValidateGraphDefAgainstOpRegistry() except it takes an OpList
// instead of an OpRegistryInterface. Note that the OpList need not
// have descriptions, which can be a big space savings, see
// GetOpListForValidation() below.
Status ValidateGraphDefAgainstOpList(const GraphDef& graph_def,
const OpList& op_list);
// Get an OpList from `*op_registry` with all the descriptions removed.
void GetOpListForValidation(
OpList* op_list, const OpRegistry& op_registry = *OpRegistry::Global());
// Validate that the graph has no cycle except for legal while loop cycles.
// This traverses the specified nodes in topological order to verify there are
// no cycles. Starting with inputless nodes, it visits nodes whose inputs have
// all been visited, and counts the total number of visited nodes. If there is a
// cycle, nodes in the cycle will never be visited, and the visited count will
// be less than the total node count.
Status ValidateGraphHasNoCycle(const Graph& graph);
} // namespace graph
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_VALIDATE_H_
@@ -0,0 +1,76 @@
/* 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_CORE_GRAPH_WHILE_CONTEXT_H_
#define TENSORFLOW_CORE_GRAPH_WHILE_CONTEXT_H_
#include "tensorflow/core/graph/graph.h"
namespace tensorflow {
// Information about a while loop. Every user-defined while loop has an
// associated WhileContext, i.e., there is a WhileContext for every execution
// frame. Created with the while loop and used during gradient
// construction. Note that the gradient graph of while loop contains while loops
// itself, but these do not generate separate WhileContexts.
//
// TODO(skyewm): this is currently insufficient to handle nested loops and
// conditionals (and possibly other requirements). This may change a lot in the
// future to support these features.
//
// TODO(skyewm): de/serialize in MetaGraphDef so imported while loops will be
// differentiable. Figure out backwards compatibility story.
class WhileContext {
public:
WhileContext(StringPiece frame_name, std::vector<Node*> enter_nodes,
std::vector<Node*> exit_nodes, OutputTensor cond_output,
std::vector<OutputTensor> body_inputs,
std::vector<OutputTensor> body_outputs);
const string& frame_name() const { return frame_name_; }
const std::vector<Node*>& enter_nodes() const { return enter_nodes_; }
const std::vector<Node*>& exit_nodes() const { return exit_nodes_; }
const OutputTensor& cond_output() const { return cond_output_; }
const std::vector<OutputTensor>& body_inputs() const { return body_inputs_; }
const std::vector<OutputTensor>& body_outputs() const {
return body_outputs_;
}
private:
// Each user-defined while loop defines a new execution frame, which is
// uniquely identified by its frame name. Frames are used by the executor to
// manage the iterations of a loop. See the FrameState comment in
// core/common_runtime/executor.cc for more details.
const string frame_name_;
// The enter nodes defining the input loop variables to the while loop. This
// vector defines the order of the loop variables.
const std::vector<Node*> enter_nodes_;
// The exit nodes defining the outputs of the while loop. These are in loop
// variable order.
const std::vector<Node*> exit_nodes_;
// The boolean output of the loop predicate.
const OutputTensor cond_output_;
// The inputs and outputs to the loop body.
const std::vector<OutputTensor> body_inputs_;
const std::vector<OutputTensor> body_outputs_;
};
} // namespace tensorflow
#endif // TENSORFLOW_CORE_GRAPH_WHILE_CONTEXT_H_