tf
This commit is contained in:
1 parent
eb1b42a5e8
commit
458eb2aef1
1283 files changed
+323373
No files matched your search
@@ -0,0 +1,33 @@
|
||||
/* 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_COMPILER_JIT_BUILD_XLA_OPS_PASS_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_BUILD_XLA_OPS_PASS_H_
|
||||
|
||||
#include "tensorflow/core/common_runtime/optimization_registry.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Adds _XlaCompile and _XlaRun operations to the TF graph that compiles and
|
||||
// executes (using XLA) TF function calls marked with "_XlaCompiledKernel".
|
||||
class BuildXlaOpsPass : public GraphOptimizationPass {
|
||||
public:
|
||||
Status Run(const GraphOptimizationPassOptions& options) override;
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_BUILD_XLA_OPS_PASS_H_
|
||||
@@ -0,0 +1,68 @@
|
||||
/* 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_COMPILER_JIT_DEADNESS_ANALYSIS_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_DEADNESS_ANALYSIS_H_
|
||||
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// This analyzes a TensorFlow graph to identify nodes which may have partially
|
||||
// dead inputs (i.e. these nodes may have some dead inputs and some alive
|
||||
// inputs).
|
||||
//
|
||||
// For example, the ADD node in the following graph
|
||||
//
|
||||
// V0 PRED0 V1 PRED1
|
||||
// | | | |
|
||||
// v v v v
|
||||
// SWITCH SWITCH
|
||||
// | |
|
||||
// +---+ + ---+
|
||||
// | |
|
||||
// v v
|
||||
// ADD
|
||||
//
|
||||
// can have its inputs independently dead or alive based on the runtime values
|
||||
// of PRED0 and PRED1.
|
||||
//
|
||||
// It is tempting to call this a liveness analysis but I avoided that because
|
||||
// "liveness" already has other connotations.
|
||||
class DeadnessAnalysis {
|
||||
public:
|
||||
// Returns true if `node` may have some live inputs and some dead inputs.
|
||||
//
|
||||
// This is a conservatively correct routine -- if it returns false then `node`
|
||||
// is guaranteed to not have inputs with mismatching liveness, but not the
|
||||
// converse.
|
||||
//
|
||||
// REQUIRES: node is not a Merge operation.
|
||||
virtual bool HasInputsWithMismatchingDeadness(const Node& node) = 0;
|
||||
|
||||
// Prints out the internal state of this instance. For debugging purposes
|
||||
// only.
|
||||
virtual void Print() const = 0;
|
||||
virtual ~DeadnessAnalysis();
|
||||
|
||||
// Run the deadness analysis over `graph` and returns an error or a populated
|
||||
// instance of DeadnessAnalysis in `result`.
|
||||
static Status Run(const Graph& graph,
|
||||
std::unique_ptr<DeadnessAnalysis>* result);
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_DEADNESS_ANALYSIS_H_
|
||||
@@ -0,0 +1,40 @@
|
||||
/* 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_COMPILER_JIT_DEADNESS_ANALYSIS_INTERNAL_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_DEADNESS_ANALYSIS_INTERNAL_H_
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "tensorflow/core/graph/tensor_id.h"
|
||||
|
||||
namespace tensorflow {
|
||||
namespace deadness_analysis_internal {
|
||||
|
||||
// Returns a map describing the predicate each Tensor was mapped to. For
|
||||
// testing purposes only.
|
||||
using PredicateMapTy = absl::flat_hash_map<TensorId, string, TensorId::Hasher>;
|
||||
Status ComputePredicates(const Graph& graph, PredicateMapTy* out_predicate_map);
|
||||
|
||||
// Returns a map describing the predicate each Tensor was mapped to. For
|
||||
// testing purposes only. Makes deadness analysis visit the graph in the order
|
||||
// specified in `reverse_post_order` which must be a valid RPO for the graph
|
||||
// minus NextIteration->Merge edges.
|
||||
Status ComputePredicates(const Graph& graph,
|
||||
absl::Span<Node* const> reverse_post_order,
|
||||
PredicateMapTy* out_predicate_map);
|
||||
} // namespace deadness_analysis_internal
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_DEADNESS_ANALYSIS_INTERNAL_H_
|
||||
@@ -0,0 +1,30 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// Provides definitions needed for use of the TensorFlow XLA
|
||||
// device.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_JIT_DEFS_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_DEFS_H_
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Name of attribute used to tag operators for compilation with XLA
|
||||
extern const char* const kXlaCompileAttr; // "_XlaCompile"
|
||||
extern const char* const kXlaScopeAttr; // "_XlaScope"
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_DEFS_H_
|
||||
@@ -0,0 +1,118 @@
|
||||
/* 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 groups nodes marked with a common
|
||||
// kXlaClusterAttr into functions, and replaces the original nodes by
|
||||
// calls. The calls are annotated with kXlaCompiledKernelAttr.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_JIT_ENCAPSULATE_SUBGRAPHS_PASS_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_ENCAPSULATE_SUBGRAPHS_PASS_H_
|
||||
|
||||
#include "tensorflow/core/common_runtime/optimization_registry.h"
|
||||
#include "tensorflow/core/framework/function.h"
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// A rewriting function to apply to each subgraph during encapsulation.
|
||||
// 'arg_source_tensors' are the tensors corresponding to the arguments in the
|
||||
// original source graph (*not* 'graph').
|
||||
//
|
||||
// 'graph' is the subgraph. The rewriting may renumber the inputs and outputs;
|
||||
// 'input_permutation' is a mapping from old argument numbers to new argument
|
||||
// numbers, whereas 'output_permutation' is the same for outputs. Both
|
||||
// 'input_permutation' and 'output_permutation' are initialized to the identity
|
||||
// permutation. 'nodedef' is the NodeDef for the call to the function under
|
||||
// construction, provided to allow additional attributes to be set.
|
||||
// The rewrite may also change the NodeDef's operator name, and that
|
||||
// name will be used as the name of the generated function.
|
||||
typedef std::function<Status(
|
||||
const std::vector<OutputTensor>& arg_source_tensors,
|
||||
std::unique_ptr<Graph>* graph, std::vector<int>* input_permutation,
|
||||
std::vector<int>* output_permutation, NodeDef* node_def)>
|
||||
RewriteSubgraphFn;
|
||||
|
||||
// Transformation that finds subgraphs whose nodes are marked with
|
||||
// 'group_attribute', splits those subgraphs into functions, and replaces
|
||||
// the originals with function calls.
|
||||
//
|
||||
// 'group_attribute' must be a string valued-attribute that names the new
|
||||
// functions to introduce.
|
||||
//
|
||||
// 'outside_compilation_attribute' must be a string-valued attribute that is
|
||||
// used to tag nodes within a subgraph to be part of an 'outside_compilation'
|
||||
// cluster within the subgraph. A cluster is formed from the set of nodes with
|
||||
// the same value of outside_compilation_subgraph and group_attribute. The nodes
|
||||
// in an outside_compilation cluster are left in the original graph. Edges
|
||||
// crossing from the subgraph to an outside_compilation cluster nested in the
|
||||
// subgraph are lifted into a SendToHost/RecvAtHost pair of nodes, and edges
|
||||
// crossing from an outside_compilation cluster into its enclosing subgraph are
|
||||
// lifted into a SendFromHost/RecvFromHost pair of nodes.
|
||||
//
|
||||
// If 'rewrite_subgraph_fn' is set, it is applied to each subgraph before
|
||||
// function conversion.
|
||||
//
|
||||
// If 'reuse_existing_functions' is set, use an existing function with the
|
||||
// same name, if any.
|
||||
//
|
||||
// TODO(phawkins): currently, some information in control edges
|
||||
// is not preserved. Suppose you have A and B in the main
|
||||
// graph, C and D in a subgraph. B and C have control deps from A, D has control
|
||||
// dep from B. Originally D must run after C, post-transformation this
|
||||
// dependency is lost.
|
||||
Status EncapsulateSubgraphsInFunctions(
|
||||
string group_attribute, string outside_compilation_attribute,
|
||||
const Graph& graph_in, const RewriteSubgraphFn& rewrite_subgraph_fn,
|
||||
bool reuse_existing_functions, std::unique_ptr<Graph>* graph_out,
|
||||
FunctionLibraryDefinition* library);
|
||||
|
||||
// The attribute that marks function calls produced by the encapsulate
|
||||
// subgraphs pass and that should in turn be compiled via XlaLaunch operators.
|
||||
extern const char* const kXlaCompiledKernelAttr;
|
||||
|
||||
// Does `node` have the kXlaCompiledKernelAttr attribute?
|
||||
bool IsXlaCompiledKernel(const Node& node);
|
||||
|
||||
// Functions produced by the EncapsulateSubgraphs pass have their arguments in
|
||||
// the order:
|
||||
// 1) compile-time constant arguments, in host memory,
|
||||
// 2) other arguments, in device memory.
|
||||
// 3) resource variable arguments, in host memory. Note that only the resource
|
||||
// Tensor itself is in host memory; the underlying value may be in device
|
||||
// memory.
|
||||
// The functions are annotated with the following attributes that describe how
|
||||
// many constant and resource arguments there are:
|
||||
|
||||
// Name of the attribute containing the number of constant arguments.
|
||||
extern const char* const kXlaNumConstantArgsAttr;
|
||||
|
||||
// Name of the attribute containing the number of resource variable arguments.
|
||||
extern const char* const kXlaNumResourceArgsAttr;
|
||||
|
||||
// Sorts each node's control inputs by their names. This guarantees that for two
|
||||
// structually equivalent GraphDefs, we get the same traversal ordering on
|
||||
// node's control input fields.
|
||||
// TODO(hpucha): Move the utilities to a more appropriate place.
|
||||
void SortControlInputs(GraphDef* gdef);
|
||||
|
||||
class EncapsulateSubgraphsPass : public GraphOptimizationPass {
|
||||
public:
|
||||
Status Run(const GraphOptimizationPassOptions& options) override;
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_ENCAPSULATE_SUBGRAPHS_PASS_H_
|
||||
@@ -0,0 +1,60 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
// Rewrites computations generated by the xla.compile() Python code into
|
||||
// XlaLaunch nodes.
|
||||
//
|
||||
// xla.compile() does two main things:
|
||||
// a) marks operators that make up an XLA computation with the attribute
|
||||
// _xla_compile_id=XYZ, where XYZ is a unique key.
|
||||
// b) adds XlaClusterOutput nodes to represent outputs of the computation.
|
||||
// These nodes are not marked with the _xla_compile_id attribute.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_JIT_ENCAPSULATE_XLA_COMPUTATIONS_PASS_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_ENCAPSULATE_XLA_COMPUTATIONS_PASS_H_
|
||||
|
||||
#include "tensorflow/core/common_runtime/optimization_registry.h"
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
#include "tensorflow/core/platform/env.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Encapsulates nodes marked with the _xla_compile_id attribute into
|
||||
// XlaLaunch operators.
|
||||
class EncapsulateXlaComputationsPass : public GraphOptimizationPass {
|
||||
public:
|
||||
static const char* const kXlaClusterAttr; // _xla_compile_id
|
||||
|
||||
Status Run(const GraphOptimizationPassOptions& options) override;
|
||||
|
||||
// The following methods are public only for unit tests.
|
||||
|
||||
// This pass has two stages:
|
||||
// a) first, we call EncapsulateSubgraphsPass to encapsulate all nodes
|
||||
// marked with the same _xla_compile_id attribute into functions. These
|
||||
// functions contain the computations to be passed to XlaLaunch. During
|
||||
// encapsulation, we sort the arguments into the order expected by
|
||||
// XlaLaunch.
|
||||
static Status Encapsulate(std::unique_ptr<Graph>* graph,
|
||||
FunctionLibraryDefinition* flib_def);
|
||||
|
||||
// b) we rewrite the function calls generated in phase (a) into XlaLaunch
|
||||
// operators. We also convert the XlaClusterOutput output nodes of the
|
||||
// function call into the outputs of the XlaLaunch operator.
|
||||
static Status BuildXlaLaunchOps(Graph* graph);
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_ENCAPSULATE_XLA_COMPUTATIONS_PASS_H_
|
||||
@@ -0,0 +1,132 @@
|
||||
/* 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_COMPILER_JIT_GRAPHCYCLES_GRAPHCYCLES_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_GRAPHCYCLES_GRAPHCYCLES_H_
|
||||
|
||||
// GraphCycles detects the introduction of a cycle into a directed
|
||||
// graph that is being built up incrementally.
|
||||
//
|
||||
// Nodes are identified by small integers. It is not possible to
|
||||
// record multiple edges with the same (source, destination) pair;
|
||||
// requests to add an edge where one already exists are silently
|
||||
// ignored.
|
||||
//
|
||||
// It is also not possible to introduce a cycle; an attempt to insert
|
||||
// an edge that would introduce a cycle fails and returns false.
|
||||
//
|
||||
// GraphCycles uses no internal locking; calls into it should be
|
||||
// serialized externally.
|
||||
|
||||
// Performance considerations:
|
||||
// Works well on sparse graphs, poorly on dense graphs.
|
||||
// Extra information is maintained incrementally to detect cycles quickly.
|
||||
// InsertEdge() is very fast when the edge already exists, and reasonably fast
|
||||
// otherwise.
|
||||
// FindPath() is linear in the size of the graph.
|
||||
// The current implementation uses O(|V|+|E|) space.
|
||||
|
||||
#include <unordered_set>
|
||||
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// NOTE!!!
|
||||
// For now a copy of this is forked to net/plaque. If you
|
||||
// find a bug or add a feature, please inform the owners of the
|
||||
// net/plaque copy in case it should be integrated.
|
||||
// NOTE!!!
|
||||
class GraphCycles {
|
||||
public:
|
||||
GraphCycles();
|
||||
~GraphCycles();
|
||||
|
||||
// Allocate an unused node id and return it.
|
||||
// The new node has a null pointer for its node data.
|
||||
// All node identifiers passed to other routines in this interface
|
||||
// must have been allocated by NewNode() and not yet deallocated
|
||||
// by RemoveNode().
|
||||
int32 NewNode();
|
||||
|
||||
// Remove "node" from the graph, deleting all edges to and from it.
|
||||
// After this call the identifier "node" it may no longer be used
|
||||
// as an argument to any routine until it has been reallocated with
|
||||
// NewNode().
|
||||
void RemoveNode(int32 node);
|
||||
|
||||
// Attempt to insert an edge from source_node to dest_node. If the
|
||||
// edge would introduce a cycle, return false without making any
|
||||
// changes. Otherwise add the edge and return true.
|
||||
bool InsertEdge(int32 source_node, int32 dest_node);
|
||||
|
||||
// Remove any edge that exists from source_node to dest_node.
|
||||
void RemoveEdge(int32 source_node, int32 dest_node);
|
||||
|
||||
// Return whether there is an edge directly from source_node to dest_node.
|
||||
bool HasEdge(int32 source_node, int32 dest_node) const;
|
||||
|
||||
// Contracts the edge from 'a' to node 'b', merging nodes 'a' and 'b'. 'b' is
|
||||
// removed from the graph, and edges to/from 'b' are replaced with edges
|
||||
// to/from 'a'. If contracting the edge would create a cycle, does nothing
|
||||
// and returns false.
|
||||
bool ContractEdge(int32 a, int32 b);
|
||||
|
||||
// Return true if can contract edge, otherwise return false.
|
||||
bool CanContractEdge(int32 a, int32 b);
|
||||
|
||||
// Return whether dest_node is reachable from source_node
|
||||
// by following edges.
|
||||
bool IsReachable(int32 source_node, int32 dest_node) const;
|
||||
|
||||
// A faster non-thread-safe version of IsReachable.
|
||||
bool IsReachableNonConst(int32 source_node, int32 dest_node);
|
||||
|
||||
// Return or set the node data for a node. This data is unused
|
||||
// by the implementation.
|
||||
void *GetNodeData(int32 node) const;
|
||||
void SetNodeData(int32 node, void *data);
|
||||
|
||||
// Find a path from "source" to "dest". If such a path exists, place the
|
||||
// node IDs of the nodes on the path in the array path[], and return the
|
||||
// number of nodes on the path. If the path is longer than max_path_len
|
||||
// nodes, only the first max_path_len nodes are placed in path[]. The client
|
||||
// should compare the return value with max_path_len" to see when this
|
||||
// occurs. If no path exists, return 0. Any valid path stored in path[]
|
||||
// will start with "source" and end with "dest". There is no guarantee that
|
||||
// the path is the shortest, but no node will appear twice in the path,
|
||||
// except the source and destination node if they are identical; therefore,
|
||||
// the return value is at most one greater than the number of nodes in the
|
||||
// graph.
|
||||
int FindPath(int32 source, int32 dest, int max_path_len, int32 path[]) const;
|
||||
|
||||
// Check internal invariants. Crashes on failure, returns true on success.
|
||||
// Expensive: should only be called from graphcycles_test.cc.
|
||||
bool CheckInvariants() const;
|
||||
|
||||
std::unordered_set<int32> Successors(int32 node);
|
||||
std::unordered_set<int32> Predecessors(int32 node);
|
||||
|
||||
// ----------------------------------------------------
|
||||
struct Rep;
|
||||
|
||||
private:
|
||||
Rep *rep_; // opaque representation
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(GraphCycles);
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
#endif // TENSORFLOW_COMPILER_JIT_GRAPHCYCLES_GRAPHCYCLES_H_
|
||||
@@ -0,0 +1,168 @@
|
||||
/* 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_COMPILER_JIT_KERNELS_XLA_OPS_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_KERNELS_XLA_OPS_H_
|
||||
|
||||
#include "tensorflow/compiler/jit/xla_compilation_cache.h"
|
||||
#include "tensorflow/compiler/jit/xla_device.h"
|
||||
#include "tensorflow/compiler/jit/xla_launch_util.h"
|
||||
#include "tensorflow/core/framework/allocator.h"
|
||||
#include "tensorflow/core/framework/op.h"
|
||||
#include "tensorflow/core/framework/op_kernel.h"
|
||||
#include "tensorflow/core/framework/tensor.h"
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
#include "tensorflow/core/util/stream_executor_util.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Holds some information about the platform on which an
|
||||
// XlaLaunch/_XlaCompile/_XlaRun op must run on.
|
||||
class XlaPlatformInfo {
|
||||
public:
|
||||
XlaPlatformInfo() : device_type_("") {}
|
||||
explicit XlaPlatformInfo(const DeviceType device_type,
|
||||
se::Platform::Id platform_id,
|
||||
const XlaDevice::Metadata* xla_device_metadata,
|
||||
std::unique_ptr<XlaAllocator> xla_allocator,
|
||||
xla::DeviceMemoryAllocator* device_allocator)
|
||||
: device_type_(device_type),
|
||||
platform_id_(platform_id),
|
||||
xla_device_metadata_(xla_device_metadata),
|
||||
xla_allocator_(std::move(xla_allocator)),
|
||||
device_allocator_(device_allocator) {
|
||||
CHECK((device_allocator_ != nullptr) ^ (xla_allocator_.get() != nullptr));
|
||||
}
|
||||
|
||||
XlaPlatformInfo& operator=(XlaPlatformInfo&& other) = default;
|
||||
|
||||
bool UseMultipleStreams() const {
|
||||
return xla_device_metadata_ && xla_device_metadata_->UseMultipleStreams();
|
||||
}
|
||||
|
||||
xla::DeviceMemoryAllocator* allocator() const {
|
||||
return device_allocator_ ? device_allocator_ : xla_allocator_.get();
|
||||
}
|
||||
DeviceType device_type() const { return device_type_; }
|
||||
|
||||
// This is equal to xla_device_metadata()->platform()->id() if
|
||||
// xla_device_metadata() is not nullptr.
|
||||
se::Platform::Id platform_id() const { return platform_id_; }
|
||||
|
||||
// This may be null if the op this XlaPlatformInfo is for was not placed on an
|
||||
// XLA device.
|
||||
const XlaDevice::Metadata* xla_device_metadata() const {
|
||||
return xla_device_metadata_;
|
||||
}
|
||||
bool is_on_xla_device() const { return xla_device_metadata() != nullptr; }
|
||||
|
||||
private:
|
||||
DeviceType device_type_;
|
||||
se::Platform::Id platform_id_;
|
||||
|
||||
// xla_device_metadata_ lives in the tensorflow::DeviceBase in which the
|
||||
// XlaLaunch/_XlaCompile/_XlaRun op is placed and thus does not die before the
|
||||
// XlaLaunch/_XlaCompile/_XlaRun OpKernel.
|
||||
const XlaDevice::Metadata* xla_device_metadata_;
|
||||
|
||||
// If the op associated with this XlaPlatformInfo is placed on an XLA device
|
||||
// then device_allocator_ is the xla::Backend's memory allocator and
|
||||
// xla_allocator_ is null. If the op is placed on a regular CPU or GPU device
|
||||
// then device_allocator_ is null and xla_allocator_ points to an appropriate
|
||||
// XlaAllocator instance.
|
||||
std::unique_ptr<XlaAllocator> xla_allocator_;
|
||||
xla::DeviceMemoryAllocator* device_allocator_;
|
||||
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(XlaPlatformInfo);
|
||||
};
|
||||
|
||||
// XlaLocalLaunchBase is almost the same as XlaLocalLaunchOp.
|
||||
// The only difference is that it does not require arguments to follow
|
||||
// the "constants, then regular args, then resources" order.
|
||||
// It takes vectors of constant and resource arguments explicitly.
|
||||
// It does not have corresponding OpDef because it is never present
|
||||
// in the GraphDef.
|
||||
// Currently, it is used by eager runtime. FunctionLibraryRuntime creates
|
||||
// this kernel when asked to create a kernel for an XLA-compiled function.
|
||||
class XlaLocalLaunchBase : public OpKernel {
|
||||
public:
|
||||
XlaLocalLaunchBase(OpKernelConstruction* ctx,
|
||||
const std::vector<int>& constants,
|
||||
const std::vector<int>& resources,
|
||||
const NameAttrList& function);
|
||||
XlaLocalLaunchBase(const XlaLocalLaunchBase&) = delete;
|
||||
XlaLocalLaunchBase& operator=(const XlaLocalLaunchBase&) = delete;
|
||||
~XlaLocalLaunchBase() override = default;
|
||||
|
||||
void Compute(OpKernelContext* ctx) override;
|
||||
|
||||
protected:
|
||||
// Indexes of compile-time constant inputs
|
||||
std::vector<int> constants_;
|
||||
// Indexes of resource inputs
|
||||
std::vector<int> resources_;
|
||||
|
||||
NameAttrList function_;
|
||||
XlaPlatformInfo platform_info_;
|
||||
};
|
||||
|
||||
// XlaLocalLaunchOp is used to replace a region of the TensorFlow graph
|
||||
// which will be compiled and executed using XLA. The XlaLocalLaunchOp is
|
||||
// responsible for handling interactions with the TensorFlow executor.
|
||||
// Once all inputs are present, and their shapes are known, the op can
|
||||
// use a 'XlaCompilationCache' to compile and execute code which is specific
|
||||
// to the shapes of input Tensors.
|
||||
// XlaLocalLaunchOp uses xla::LocalClient::Compile() and
|
||||
// xla::LocalExecutable::Run(), and passes arguments into/out of XLA in device
|
||||
// memory.
|
||||
class XlaLocalLaunchOp : public XlaLocalLaunchBase {
|
||||
public:
|
||||
explicit XlaLocalLaunchOp(OpKernelConstruction* ctx);
|
||||
~XlaLocalLaunchOp() override;
|
||||
|
||||
private:
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(XlaLocalLaunchOp);
|
||||
};
|
||||
|
||||
class XlaCompileOp : public OpKernel {
|
||||
public:
|
||||
explicit XlaCompileOp(OpKernelConstruction* ctx);
|
||||
|
||||
void Compute(OpKernelContext* ctx) override;
|
||||
|
||||
private:
|
||||
// Indexes of compile-time constant inputs
|
||||
std::vector<int> constants_;
|
||||
// Indexes of resource inputs
|
||||
std::vector<int> resources_;
|
||||
|
||||
NameAttrList function_;
|
||||
|
||||
XlaPlatformInfo platform_info_;
|
||||
};
|
||||
|
||||
class XlaRunOp : public OpKernel {
|
||||
public:
|
||||
explicit XlaRunOp(OpKernelConstruction* ctx);
|
||||
|
||||
void Compute(OpKernelContext* ctx) override;
|
||||
|
||||
private:
|
||||
XlaPlatformInfo platform_info_;
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_KERNELS_XLA_LAUNCH_OP_H_
|
||||
@@ -0,0 +1,68 @@
|
||||
/* 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_COMPILER_JIT_LEGACY_FLAGS_MARK_FOR_COMPILATION_PASS_FLAGS_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_LEGACY_FLAGS_MARK_FOR_COMPILATION_PASS_FLAGS_H_
|
||||
|
||||
// Legacy flags for the XLA bridge's mark_for_compilation_pass module.
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
#include "tensorflow/core/util/command_line_flags.h"
|
||||
|
||||
namespace tensorflow {
|
||||
namespace legacy_flags {
|
||||
|
||||
// Append to *flag_list flag definitions associated with the XLA bridge's
|
||||
// mark_for_compilation_pass module.
|
||||
void AppendMarkForCompilationPassFlags(
|
||||
std::vector<tensorflow::Flag>* flag_list);
|
||||
|
||||
// The values of flags associated with the XLA bridge's
|
||||
// mark_for_compilation_pass module.
|
||||
typedef struct {
|
||||
int32 tf_xla_auto_jit; // Control compilation of operators into XLA
|
||||
// computations on CPU and GPU devices. 0 = use
|
||||
// ConfigProto setting; -1 = off; 1 = on for things
|
||||
// very likely to be improved; 2 = on for everything.
|
||||
// Experimental.
|
||||
int32 tf_xla_min_cluster_size; // Minimum number of operators in an XLA
|
||||
// compilation. Ignored for operators placed
|
||||
// on an XLA device or operators explicitly
|
||||
// marked for compilation.
|
||||
int32 tf_xla_max_cluster_size; // Maximum number of operators in an XLA
|
||||
// compilation.
|
||||
bool tf_xla_clustering_debug; // Dump graphs during XLA compilation.
|
||||
bool tf_xla_cpu_global_jit; // Enables global JIT compilation for CPU
|
||||
// via SessionOptions.
|
||||
int64 tf_xla_clustering_fuel; // "Compiler fuel" for clustering. Only this
|
||||
// many ops will be marked as eligible for
|
||||
// clustering.
|
||||
bool tf_xla_fusion_only; // This flag is effective only when global_jit_level
|
||||
// is set to ON* and overrides its behavior. If
|
||||
// true, enable fusion of element-wise operations
|
||||
// only using XLA.
|
||||
} MarkForCompilationPassFlags;
|
||||
|
||||
// Return a pointer to the MarkForCompilationPassFlags struct;
|
||||
// repeated calls return the same pointer.
|
||||
// This should be called only after Flags::Parse() has returned.
|
||||
MarkForCompilationPassFlags* GetMarkForCompilationPassFlags();
|
||||
|
||||
} // namespace legacy_flags
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_LEGACY_FLAGS_MARK_FOR_COMPILATION_PASS_FLAGS_H_
|
||||
@@ -0,0 +1,47 @@
|
||||
/* 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_COMPILER_JIT_LEGACY_FLAGS_XLA_DEVICE_FLAGS_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_LEGACY_FLAGS_XLA_DEVICE_FLAGS_H_
|
||||
|
||||
// Legacy flags for the XLA bridge's xla_device module.
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
#include "tensorflow/core/util/command_line_flags.h"
|
||||
|
||||
namespace tensorflow {
|
||||
namespace legacy_flags {
|
||||
|
||||
// The values of flags associated with the XLA bridge's
|
||||
// xla_device module.
|
||||
typedef struct {
|
||||
// Switch the CPU device into "on-demand" mode, where instead of
|
||||
// autoclustering ops are compiled one by one just-in-time.
|
||||
// Enabling this mode by a legacy flag is a temporary mechanism. When this
|
||||
// feature is battle-tested, we will switch this to be a session option.
|
||||
bool tf_xla_compile_on_demand;
|
||||
} XlaDeviceFlags;
|
||||
|
||||
// Return a pointer to the XlaDeviceFlags struct;
|
||||
// repeated calls return the same pointer.
|
||||
// This should be called only after Flags::Parse() has returned.
|
||||
XlaDeviceFlags* GetXlaDeviceFlags();
|
||||
|
||||
} // namespace legacy_flags
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_LEGACY_FLAGS_XLA_DEVICE_FLAGS_H_
|
||||
@@ -0,0 +1,57 @@
|
||||
/* 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 passes that marks nodes that are to be compiled with
|
||||
// attribute kXlaClusterAttr. Nodes with the same cluster ID will be compiled
|
||||
// together.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_JIT_MARK_FOR_COMPILATION_PASS_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_MARK_FOR_COMPILATION_PASS_H_
|
||||
|
||||
#include "tensorflow/core/common_runtime/optimization_registry.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// The attribute that marks nodes to be grouped into functions by the
|
||||
// encapsulate subgraphs pass.
|
||||
extern const char* const kXlaClusterAttr;
|
||||
|
||||
// The attribute that marks nodes in a cluster to be placed outside the xla
|
||||
// compilation by the encapsulate subgraphs pass.
|
||||
extern const char* const kXlaOutsideCompilationAttr;
|
||||
|
||||
// Pass that marks a subset of operators in the graph with attribute
|
||||
// _XlaCluster so they are compiled by the EncapsulateSubgraphsPass.
|
||||
class MarkForCompilationPass : public GraphOptimizationPass {
|
||||
public:
|
||||
MarkForCompilationPass() = default;
|
||||
|
||||
Status Run(const GraphOptimizationPassOptions& options) override;
|
||||
|
||||
private:
|
||||
Status RunImpl(const GraphOptimizationPassOptions& options,
|
||||
const std::function<bool(const Node*, const DeviceType&)>&
|
||||
is_compilable_fn = {});
|
||||
|
||||
friend class MarkForCompilationPassTestHelper;
|
||||
};
|
||||
|
||||
// Returns true iff 'ndef' is a call to a function that is compilable. A
|
||||
// function is compilable iff every operator in the function body is
|
||||
// compilable.
|
||||
bool IsCompilable(FunctionLibraryRuntime* flr, const NodeDef& ndef);
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_MARK_FOR_COMPILATION_PASS_H_
|
||||
@@ -0,0 +1,40 @@
|
||||
/* 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_COMPILER_JIT_MARK_FOR_COMPILATION_PASS_TEST_HELPER_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_MARK_FOR_COMPILATION_PASS_TEST_HELPER_H_
|
||||
|
||||
#include "tensorflow/compiler/jit/mark_for_compilation_pass.h"
|
||||
|
||||
namespace tensorflow {
|
||||
class MarkForCompilationPassTestHelper {
|
||||
public:
|
||||
// Runs the MarkForCompilation pass on `graph` after assigning all nodes in
|
||||
// `graph` to the CPU device. To make testing easier, ignores device
|
||||
// registration, _XlaCompile attributes, input deadness and global jit level.
|
||||
static Status MarkForCompilation(std::unique_ptr<Graph>* graph,
|
||||
FunctionLibraryDefinition* flib_def,
|
||||
SessionOptions* session_options);
|
||||
|
||||
// Like `MarkForCompilation` but creates a default SessionOptions.
|
||||
static Status MarkForCompilation(std::unique_ptr<Graph>* graph,
|
||||
FunctionLibraryDefinition* flib_def);
|
||||
|
||||
// Like `MarkForCompilation` but creates `flib_def` from the op registry.
|
||||
static Status MarkForCompilation(std::unique_ptr<Graph>* graph);
|
||||
};
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_MARK_FOR_COMPILATION_PASS_TEST_HELPER_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_COMPILER_JIT_PARTIALLY_DECLUSTER_PASS_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_PARTIALLY_DECLUSTER_PASS_H_
|
||||
|
||||
#include "tensorflow/core/common_runtime/optimization_registry.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Clones or moves nodes from within a cluster to outside the cluster if
|
||||
// profitable. There are two reasons why we do this:
|
||||
//
|
||||
// - Reducing device-to-host copies.
|
||||
// - Reducing the number of XLA recompilations.
|
||||
class PartiallyDeclusterPass : public GraphOptimizationPass {
|
||||
public:
|
||||
Status Run(const GraphOptimizationPassOptions& options) override;
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_PARTIALLY_DECLUSTER_PASS_H_
|
||||
@@ -0,0 +1,73 @@
|
||||
/* 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_COMPILER_JIT_RESOURCE_OPERATION_SAFETY_ANALYSIS_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_RESOURCE_OPERATION_SAFETY_ANALYSIS_H_
|
||||
|
||||
#include "tensorflow/compiler/jit/graphcycles/graphcycles.h"
|
||||
#include "tensorflow/core/framework/function.h"
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
|
||||
namespace tensorflow {
|
||||
// An XLA cluster hoists all resource reads to be beginning of the cluster
|
||||
// execution and all the resource writes to the end. This means it cannot
|
||||
// enforce arbitrary ordering dependencies (via control or data edges) between
|
||||
// resource operations. Since all resource reads happen before all resource
|
||||
// writes, edges constraining resource reads to happen before resource writes
|
||||
// are fine, but all other kinds of edges are problematic. This analysis
|
||||
// returns the set of pairs of resource operations that cannot be put in the
|
||||
// same cluster because XLA cannot respect the dependencies between them in the
|
||||
// TensorFlow program.
|
||||
//
|
||||
// The restrictions are not transitive: it is fine to put A and C in the same
|
||||
// cluster even if the returned set contains (A,B) and (B,C).
|
||||
//
|
||||
// In other words, if these pairs are seen as edges in an undirected graph of
|
||||
// the nodes in `g` then auto-clustering is at least as constrained as the graph
|
||||
// coloring problem on this graph.
|
||||
//
|
||||
//
|
||||
// For instance if we auto-cluster all operations in this TensorFlow graph:
|
||||
//
|
||||
// ReadVariablepOp0 -> ReadVariableOp1
|
||||
// |
|
||||
// v
|
||||
// AssignVariableOp0 -> AssignVariableOp1
|
||||
//
|
||||
// we will lose the ReadVariablepOp0 -> ReadVariableOp1 and the
|
||||
// AssignVariableOp0 -> AssignVariableOp1 dependencies. I.e. it is possible for
|
||||
// XlaLaunchOp to issue ReadVariableOp1 before ReadVariablepOp0 since it reads
|
||||
// all the resource variables when the cluster starts executing without any
|
||||
// particular ordering between them; same holds for the AssignVariableOp0 ->
|
||||
// AssignVariableOp1 edge. The ReadVariableOp1 -> AssignVariableOp0 edge will
|
||||
// be respected by XlaLaunchOp though because all reads happen before all
|
||||
// writes.
|
||||
//
|
||||
//
|
||||
// NB! The result computed by this analysis assumes that we don't auto-cluster
|
||||
// back-edges (i.e. the edges from NextIteration to Merge).
|
||||
//
|
||||
// NB! The result computed by this analysis assumes that we don't auto-cluster
|
||||
// functional control flow nodes containing resource operations.
|
||||
//
|
||||
// If `resource_ops_to_ignore` is set then nodes for which it returns true are
|
||||
// ignored (we pretend these nodes are not resource operations).
|
||||
Status ComputeIncompatibleResourceOperationPairs(
|
||||
const Graph& g, const FunctionLibraryDefinition* flib_def,
|
||||
const std::function<Status(const Node&, bool*)>& resource_ops_to_ignore,
|
||||
std::vector<std::pair<int, int>>* result);
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_RESOURCE_OPERATION_SAFETY_ANALYSIS_H_
|
||||
@@ -0,0 +1,65 @@
|
||||
/* 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_COMPILER_JIT_SHAPE_INFERENCE_HELPERS_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_SHAPE_INFERENCE_HELPERS_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Helper class to temporarily remove, then replace, the back edges in a
|
||||
// graph. Simple algorithms for shape inference don't work with cycles, and this
|
||||
// class can be used to remove cycles before running inference and replace them
|
||||
// after. Correct usage requires exactly one call to Remove(), followed by any
|
||||
// number of calls to RemovedEdges() and at most one call to Replace(). The call
|
||||
// to Replace() is optional if the graph will be discarded without being
|
||||
// executed, e.g., if it is being used purely for a shape inference pass.
|
||||
class BackEdgeHelper {
|
||||
public:
|
||||
struct BackEdge {
|
||||
const Edge* edge;
|
||||
Node* src;
|
||||
int src_output;
|
||||
Node* dst;
|
||||
int dst_input;
|
||||
};
|
||||
|
||||
BackEdgeHelper() = default;
|
||||
// Disallows copy and assign.
|
||||
BackEdgeHelper(const BackEdgeHelper& other) = delete;
|
||||
BackEdgeHelper& operator=(const BackEdgeHelper& other) = delete;
|
||||
|
||||
// Temporarily removes all the back edges in graph.
|
||||
Status Remove(Graph* graph);
|
||||
|
||||
// Gets the list of removed edges.
|
||||
const std::vector<BackEdge>& RemovedEdges() const;
|
||||
|
||||
// Replaces the back edges removed by a prior call to Remove.
|
||||
Status Replace();
|
||||
|
||||
private:
|
||||
Graph* graph_ = nullptr; // not owned
|
||||
std::vector<BackEdge> back_edges_;
|
||||
// Set once Replace has been called.
|
||||
bool replaced_ = false;
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_SHAPE_INFERENCE_HELPERS_H_
|
||||
@@ -0,0 +1,81 @@
|
||||
/* 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_COMPILER_JIT_UNION_FIND_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_UNION_FIND_H_
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Union-Find data structure.
|
||||
// Each cluster has an associated value; when merging clusters we can control
|
||||
// which value becomes the representative of the merged clusters. Values must be
|
||||
// copyable.
|
||||
template <typename T>
|
||||
class UnionFind {
|
||||
public:
|
||||
UnionFind() : rank_(0), size_(1), parent_(nullptr) {}
|
||||
|
||||
// Returns the number of elements in a cluster.
|
||||
int Size() { return FindRoot()->size_; }
|
||||
|
||||
// Merges this cluster with 'other'. This cluster's value becomes
|
||||
// the value of the merged cluster; the value of 'other' is ignored.
|
||||
void Merge(UnionFind* other);
|
||||
|
||||
// Each cluster has an associated value. Retrieves the value associated
|
||||
// with this cluster.
|
||||
T& Get() { return FindRoot()->value_; }
|
||||
|
||||
private:
|
||||
// Finds the root element of the cluster. Performs path compression.
|
||||
UnionFind* FindRoot();
|
||||
|
||||
int rank_;
|
||||
int size_; // Size of the cluster.
|
||||
UnionFind* parent_;
|
||||
T value_;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void UnionFind<T>::Merge(UnionFind* other) {
|
||||
UnionFind<T>* a = FindRoot();
|
||||
UnionFind<T>* b = other->FindRoot();
|
||||
if (a == b) return;
|
||||
if (a->rank_ > b->rank_) {
|
||||
b->parent_ = a;
|
||||
a->size_ += b->size_;
|
||||
return;
|
||||
}
|
||||
|
||||
a->parent_ = b;
|
||||
if (a->rank_ == b->rank_) {
|
||||
b->rank_++;
|
||||
}
|
||||
b->value_ = a->value_;
|
||||
b->size_ += a->size_;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
UnionFind<T>* UnionFind<T>::FindRoot() {
|
||||
if (!parent_) return this;
|
||||
// Path compression: update intermediate nodes to point to the root of the
|
||||
// equivalence class.
|
||||
parent_ = parent_->FindRoot();
|
||||
return parent_;
|
||||
}
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_UNION_FIND_H_
|
||||
@@ -0,0 +1,70 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// Contains utilities for clustering compilable graph nodes via XLA.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_JIT_XLA_CLUSTER_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_XLA_CLUSTER_UTIL_H_
|
||||
|
||||
#include "absl/types/optional.h"
|
||||
#include "tensorflow/compiler/jit/graphcycles/graphcycles.h"
|
||||
#include "tensorflow/core/graph/algorithm.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// The attribute that marks nodes to be grouped into functions by the
|
||||
// encapsulate subgraphs pass.
|
||||
extern const char* const kXlaClusterAttr;
|
||||
|
||||
// The attribute that marks nodes in a cluster to be placed outside the xla
|
||||
// compilation by the encapsulate subgraphs pass.
|
||||
extern const char* const kXlaOutsideCompilationAttr;
|
||||
|
||||
using OrderedNodeSet = std::set<Node*, NodeComparatorID>;
|
||||
|
||||
// Returns the DeviceType corresponding to 'device'.
|
||||
Status DeviceToDeviceType(const string& device, DeviceType* device_type);
|
||||
|
||||
// Returns true if `node` has a ref tensor input that it forwards to its output.
|
||||
bool HasForwardedRefInput(const Node& node);
|
||||
|
||||
// Creates a graph representation to enable cycle detection when clustering.
|
||||
// This representation handles loops in graph by disconnecting each loop from
|
||||
// the enclosing graph.
|
||||
Status CreateCycleDetectionGraph(const Graph* graph, GraphCycles* cycles);
|
||||
|
||||
// Returns the XLA cluster in which `node` is placed if it is in an XLA cluster,
|
||||
// otherwise returns nullopt.
|
||||
absl::optional<absl::string_view> GetXlaClusterForNode(const Node& node);
|
||||
|
||||
// Removes `node_def` its XLA cluster (by clearing its _XlaCluster attribute).
|
||||
void RemoveFromXlaCluster(NodeDef* node_def);
|
||||
|
||||
// Removes `node` its XLA cluster (by clearing its _XlaCluster attribute).
|
||||
void RemoveFromXlaCluster(Node* node);
|
||||
|
||||
// Returns true if `node` has a DT_RESOURCE typed input or output.
|
||||
bool HasResourceInputOrOutput(const Node& node);
|
||||
|
||||
// Adds edges to `cycles` to prevent clustering resource operations that cannot
|
||||
// be legally clustered.
|
||||
Status AdjustCycleDetectionGraphForResourceOps(
|
||||
const Graph* graph, const FunctionLibraryDefinition* flib_def,
|
||||
const std::function<Status(const Node&, bool*)>& resource_ops_to_ignore,
|
||||
GraphCycles* cycles);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_XLA_CLUSTER_UTIL_H_
|
||||
@@ -0,0 +1,176 @@
|
||||
/* 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_COMPILER_JIT_XLA_COMPILATION_CACHE_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_XLA_COMPILATION_CACHE_H_
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "tensorflow/compiler/tf2xla/xla_compiler.h"
|
||||
#include "tensorflow/compiler/tf2xla/xla_context.h"
|
||||
#include "tensorflow/compiler/xla/client/local_client.h"
|
||||
#include "tensorflow/core/common_runtime/device.h"
|
||||
#include "tensorflow/core/common_runtime/device_mgr.h"
|
||||
#include "tensorflow/core/framework/graph.pb.h"
|
||||
#include "tensorflow/core/framework/op_kernel.h"
|
||||
#include "tensorflow/core/lib/core/threadpool.h"
|
||||
#include "tensorflow/core/platform/mutex.h"
|
||||
#include "tensorflow/core/platform/thread_annotations.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Struct that represents a possibly-absent Tensor.
|
||||
struct OptionalTensor {
|
||||
string name; // A descriptive name
|
||||
bool present = false; // Is the tensor present?
|
||||
Tensor value; // If present, what is the Tensor's value?
|
||||
};
|
||||
|
||||
// The XlaCompilationCache class caches the results of the XlaCompiler class,
|
||||
// which converts a Tensorflow graph into a compiled XLA compilation.
|
||||
//
|
||||
// Since XLA computations must have static shapes, the cache generates a new
|
||||
// XLA computation for each new set of input shapes.
|
||||
//
|
||||
// Currently no cache eviction policy is implemented and the cache grows without
|
||||
// bound.
|
||||
class XlaCompilationCache : public ResourceBase {
|
||||
public:
|
||||
XlaCompilationCache(xla::LocalClient* client, DeviceType device_type);
|
||||
~XlaCompilationCache() override;
|
||||
|
||||
// Compiles a function into a XlaCompiler::CompilationResult that can be used
|
||||
// to execute an XLA Computation. Compilation results are cached.
|
||||
// `function` is the name of a Tensorflow function to compile.
|
||||
// `constant_args` is a map of tensorflow argument number to its constant
|
||||
// value.
|
||||
// `variable_args` is a snapshot of the current values of the
|
||||
// resource variable arguments to `function`; uninitialized variables are
|
||||
// represented by an absent OptionalTensor.
|
||||
// The result of compilation is written to `*compilation_result`, which must
|
||||
// be non-null. If `executable` is non-null, also builds an
|
||||
// xla::LocalExecutable and sets `executable` to point to it. The resulting
|
||||
// executable pointer may be null if the computation has no non-constant
|
||||
// outputs.
|
||||
Status Compile(const XlaCompiler::Options& options,
|
||||
const NameAttrList& function,
|
||||
const std::map<int, Tensor>& constant_args,
|
||||
const std::map<int, OptionalTensor>& variable_args,
|
||||
OpKernelContext* ctx,
|
||||
const XlaCompiler::CompilationResult** compilation_result,
|
||||
xla::LocalExecutable** executable,
|
||||
const XlaCompiler::CompileOptions& compile_options);
|
||||
|
||||
// As above, but calls XlaCompiler::CompileSingleOp instead of
|
||||
// XlaCompiler::CompileFunction.
|
||||
Status CompileSingleOp(
|
||||
const XlaCompiler::Options& options,
|
||||
const std::map<int, Tensor>& constant_args,
|
||||
const std::map<int, OptionalTensor>& variable_args, OpKernelContext* ctx,
|
||||
const XlaCompiler::CompilationResult** compilation_result,
|
||||
xla::LocalExecutable** executable,
|
||||
const XlaCompiler::CompileOptions& compile_options);
|
||||
|
||||
xla::LocalClient* client() const { return client_; }
|
||||
const DeviceType& device_type() const { return device_type_; }
|
||||
|
||||
string DebugString() override;
|
||||
|
||||
private:
|
||||
// Common implementation of Compile and CompileSingleOp.
|
||||
Status CompileImpl(const XlaCompiler::Options& options,
|
||||
const NameAttrList& function,
|
||||
const std::map<int, Tensor>& constant_args,
|
||||
const std::map<int, OptionalTensor>& variable_args,
|
||||
OpKernelContext* ctx,
|
||||
const XlaCompiler::CompilationResult** compilation_result,
|
||||
xla::LocalExecutable** executable,
|
||||
const XlaCompiler::CompileOptions& compile_options,
|
||||
bool compile_single_op);
|
||||
|
||||
// Takes `result` which has been compiled from a Tensorflow subgraph to a
|
||||
// XLA computation already, and generates an XLA LocalExecutable `executable`.
|
||||
Status BuildExecutable(const XlaCompiler::Options& options,
|
||||
const XlaCompiler::CompilationResult& result,
|
||||
std::unique_ptr<xla::LocalExecutable>* executable);
|
||||
|
||||
xla::LocalClient* const client_;
|
||||
const DeviceType device_type_;
|
||||
|
||||
// Describes the types, shapes and any compile-time constant arguments
|
||||
// to a kernel. Key that uniquely identifies a compilation output.
|
||||
struct Signature {
|
||||
string name;
|
||||
|
||||
std::vector<std::pair<DataType, TensorShape>> arg_types;
|
||||
|
||||
// List of Tensor values for compile-time constant arguments to the
|
||||
// compilation, ordered by argument number. Tensors must be in host memory.
|
||||
std::vector<Tensor> arg_values;
|
||||
|
||||
bool operator==(const Signature& other) const;
|
||||
|
||||
struct Hash {
|
||||
uint64 operator()(const Signature& signature) const;
|
||||
};
|
||||
};
|
||||
static string SignatureDebugString(const Signature& sig);
|
||||
|
||||
// Builds the signature for a compilation.
|
||||
Status BuildSignature(const NameAttrList& function,
|
||||
const std::map<int, Tensor>& constant_args,
|
||||
const std::map<int, OptionalTensor>& variable_args,
|
||||
OpKernelContext* ctx, Signature* signature);
|
||||
|
||||
// The value associated with a cache entry.
|
||||
struct Entry {
|
||||
mutex mu;
|
||||
|
||||
// Have we tried compiling this entry?
|
||||
bool compiled = false;
|
||||
|
||||
// Did compilation succeed?
|
||||
Status compilation_status GUARDED_BY(mu);
|
||||
|
||||
// Output of the XlaCompiler.
|
||||
XlaCompiler::CompilationResult compilation_result GUARDED_BY(mu);
|
||||
|
||||
// The XLA executable compiled from <computation>. May be null if no
|
||||
// executable has been built.
|
||||
std::unique_ptr<xla::LocalExecutable> executable GUARDED_BY(mu);
|
||||
};
|
||||
|
||||
mutex compile_cache_mu_;
|
||||
absl::flat_hash_map<Signature, std::unique_ptr<Entry>, Signature::Hash> cache_
|
||||
GUARDED_BY(compile_cache_mu_);
|
||||
|
||||
struct CompileStats {
|
||||
// Number of times the cluster has been (re-)compiled.
|
||||
int64 compile_count = 0;
|
||||
|
||||
// Cumulative time spent compiling the cluster.
|
||||
int64 cumulative_compile_time_us = 0;
|
||||
};
|
||||
mutex compile_stats_mu_;
|
||||
|
||||
// Maps cluster names to compilation statistics for said cluster.
|
||||
absl::flat_hash_map<string, CompileStats> compile_stats_
|
||||
GUARDED_BY(compile_stats_mu_);
|
||||
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(XlaCompilationCache);
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_XLA_COMPILATION_CACHE_H_
|
||||
@@ -0,0 +1,53 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// The XlaCompileOnDemandOp is an OpKernel that, when its Compute method is
|
||||
// called, will generate an xla::Computation and run it asynchronously.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_JIT_XLA_COMPILE_ON_DEMAND_OP_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_XLA_COMPILE_ON_DEMAND_OP_H_
|
||||
|
||||
#include "tensorflow/compiler/jit/xla_device.h"
|
||||
#include "tensorflow/compiler/tf2xla/xla_compiler.h"
|
||||
#include "tensorflow/compiler/xla/client/local_client.h"
|
||||
#include "tensorflow/core/framework/tensor.h"
|
||||
#include "tensorflow/core/framework/types.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// An OpKernel that compiles an op to an XLA computation and runs it. Unlike
|
||||
// XlaLaunch this doesn't rely on any rewrites of the graphdef - it will run a
|
||||
// vanilla TensorFlow op as long as the bridge supports it.
|
||||
class XlaCompileOnDemandOp : public OpKernel {
|
||||
public:
|
||||
explicit XlaCompileOnDemandOp(OpKernelConstruction* ctx) : OpKernel(ctx) {}
|
||||
void Compute(OpKernelContext* ctx) override;
|
||||
|
||||
private:
|
||||
XlaCompiler::Argument CreateCompilerArgument(OpKernelContext* ctx, int64 i);
|
||||
bool ShouldArgumentBeConstant(const OpKernel* op_kernel, int64 argument_idx);
|
||||
bool MustArgumentBeConstant(const OpKernel* op_kernel, int64 argument_idx);
|
||||
Status Compile(OpKernelContext* ctx, const XlaDevice::Metadata& metadata,
|
||||
const XlaCompiler::CompilationResult** result,
|
||||
xla::LocalExecutable** executable);
|
||||
Status Run(OpKernelContext* ctx, const XlaDevice::Metadata& metadata,
|
||||
const XlaCompiler::CompilationResult* result,
|
||||
xla::LocalExecutable* executable);
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_XLA_COMPILE_ON_DEMAND_OP_H_
|
||||
@@ -0,0 +1,234 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// The XlaDevice executes a TensorFlow graph using the XLA linear algebra
|
||||
// runtime.
|
||||
//
|
||||
// Operators assigned to an XlaDevice are compiled into XLA computations.
|
||||
// Tensors on an XlaDevice are thin wrappers around XLA ScopedShapedBuffers.
|
||||
//
|
||||
// XlaDevice is instantiated separately for each XLA backend (e.g., CPU or GPU),
|
||||
// under different names (e.g., XLA_CPU or XLA_GPU).
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_JIT_XLA_DEVICE_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_XLA_DEVICE_H_
|
||||
|
||||
#include "tensorflow/compiler/jit/xla_device_context.h"
|
||||
#include "tensorflow/compiler/jit/xla_tensor.h"
|
||||
#include "tensorflow/compiler/tf2xla/xla_compiler.h"
|
||||
#include "tensorflow/compiler/tf2xla/xla_op_registry.h"
|
||||
#include "tensorflow/compiler/xla/client/local_client.h"
|
||||
#include "tensorflow/core/common_runtime/device_factory.h"
|
||||
#include "tensorflow/core/common_runtime/local_device.h"
|
||||
#include "tensorflow/core/framework/allocator.h"
|
||||
#include "tensorflow/core/framework/device_base.h"
|
||||
#include "tensorflow/core/framework/node_def_builder.h"
|
||||
#include "tensorflow/core/framework/op_kernel.h"
|
||||
#include "tensorflow/core/framework/resource_mgr.h"
|
||||
#include "tensorflow/core/framework/tensor.h"
|
||||
#include "tensorflow/core/framework/types.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
#include "tensorflow/core/platform/mutex.h"
|
||||
#include "tensorflow/core/platform/stream_executor_no_cuda.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
class XlaDevice : public LocalDevice {
|
||||
public:
|
||||
// Given a tensor, sets `xla::Shape*` the shape of tensor's representation
|
||||
// on device, fully padded. On error, the contents of `xla::Shape*`
|
||||
// are undefined.
|
||||
typedef std::function<Status(const Tensor&, xla::Shape*)> PaddedShapeFn;
|
||||
|
||||
// Wrapper class to store metadata about the XlaDevice, where it can be
|
||||
// retrieved e.g., when lazily creating the XlaCompilationCache device.
|
||||
class Metadata {
|
||||
public:
|
||||
Metadata(int device_ordinal, se::Platform* platform,
|
||||
const DeviceType& device_type,
|
||||
XlaCompiler::ShapeRepresentationFn shape_representation_fn,
|
||||
PaddedShapeFn padded_shape_fn, bool use_multiple_streams);
|
||||
|
||||
// The index of the device on this host.
|
||||
int device_ordinal() const;
|
||||
|
||||
se::Platform* platform() const;
|
||||
xla::LocalClient* client() const;
|
||||
const DeviceType& jit_device_type() const;
|
||||
const XlaCompiler::ShapeRepresentationFn& shape_representation_fn() const {
|
||||
return shape_representation_fn_;
|
||||
}
|
||||
const PaddedShapeFn& padded_shape_fn() const { return padded_shape_fn_; }
|
||||
|
||||
bool UseMultipleStreams() const { return use_multiple_streams_; }
|
||||
|
||||
private:
|
||||
const int device_ordinal_;
|
||||
const DeviceType device_type_;
|
||||
se::Platform* platform_; // Not owned.
|
||||
XlaCompiler::ShapeRepresentationFn shape_representation_fn_;
|
||||
PaddedShapeFn padded_shape_fn_;
|
||||
const bool use_multiple_streams_;
|
||||
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(Metadata);
|
||||
};
|
||||
|
||||
// Sets `*metadata` to the XlaDevice Metadata in the XLA device used by `ctx`.
|
||||
static Status GetMetadata(OpKernelContext* ctx, const Metadata** metadata);
|
||||
|
||||
// Sets `*metadata` to the XlaDevice Metadata in the XLA device used by `ctx`.
|
||||
static Status GetMetadata(OpKernelConstruction* ctx,
|
||||
const Metadata** metadata);
|
||||
|
||||
// Factory function. 'platform_name' is the name of the XLA platform.
|
||||
// 'device_name' is the name of the Tensorflow device to create.
|
||||
// 'jit_device_name' is the name of the corresponding JIT device.
|
||||
// 'transfer_as_literal' is true if device<->host transfers must be done using
|
||||
// XLA's TransferLiteral{To,From}Device interface. If false, we can use
|
||||
// ThenMemcpy instead.
|
||||
// If 'use_multiple_streams' is true, we create separate streams for
|
||||
// host-to-device and device-to-host communication.
|
||||
// If padded_shape_fn is empty, a default implementation that returns
|
||||
// the on-host shape is used.
|
||||
static Status Create(
|
||||
const string& platform_name, const string& device_name,
|
||||
int device_ordinal, const string& jit_device_name,
|
||||
const SessionOptions& options, const string& name_prefix,
|
||||
const XlaOpRegistry::DeviceRegistration& registration,
|
||||
bool transfer_as_literal, bool use_multiple_streams,
|
||||
const XlaCompiler::ShapeRepresentationFn& shape_representation_fn,
|
||||
const PaddedShapeFn& padded_shape_fn, std::unique_ptr<XlaDevice>* device);
|
||||
|
||||
// Creates a new XLA Device.
|
||||
// If padded_shape_fn is empty, a default implementation that returns
|
||||
// the logical on-device shape without padding is used.
|
||||
XlaDevice(const SessionOptions& options, const DeviceAttributes& attrs,
|
||||
int device_ordinal, const DeviceType& jit_device_name,
|
||||
se::Platform* platform, bool transfer_as_literal,
|
||||
bool use_multiple_streams,
|
||||
const XlaCompiler::ShapeRepresentationFn& shape_representation_fn,
|
||||
const PaddedShapeFn& padded_shape_fn);
|
||||
~XlaDevice() override;
|
||||
|
||||
Allocator* GetAllocator(AllocatorAttributes attr) override
|
||||
LOCKS_EXCLUDED(mu_);
|
||||
void Compute(OpKernel* op_kernel, OpKernelContext* context) override;
|
||||
void ComputeAsync(AsyncOpKernel* op_kernel, OpKernelContext* context,
|
||||
AsyncOpKernel::DoneCallback done) override;
|
||||
Status Sync() override;
|
||||
|
||||
Status FillContextMap(const Graph* graph,
|
||||
DeviceContextMap* device_context_map) override
|
||||
LOCKS_EXCLUDED(mu_);
|
||||
|
||||
Status MakeTensorFromProto(const TensorProto& tensor_proto,
|
||||
const AllocatorAttributes alloc_attrs,
|
||||
Tensor* tensor) override LOCKS_EXCLUDED(mu_);
|
||||
|
||||
const Metadata& metadata() { return xla_metadata_; }
|
||||
|
||||
// Ensures the DeviceContext associated with this XlaDevice is created and
|
||||
// valid (i.e. all streams are ok). If any state is not valid, a new
|
||||
// DeviceContext will be created.
|
||||
//
|
||||
// TODO(b/111859745): The Eager context needs to call this method to recover
|
||||
// from failures.
|
||||
Status EnsureDeviceContextOk() LOCKS_EXCLUDED(mu_);
|
||||
|
||||
// Instructs this XlaDevice to set a GpuDeviceInfo, which holds extra
|
||||
// information for GPU and TPU devices.
|
||||
Status UseGpuDeviceInfo() LOCKS_EXCLUDED(mu_);
|
||||
|
||||
// Instructs this XlaDevice to return 'sync_on_completion' for
|
||||
// RequiresSyncOnCompletion().
|
||||
void SetRequiresSyncOnCompletion(bool sync_on_completion) LOCKS_EXCLUDED(mu_);
|
||||
|
||||
bool RequiresSyncOnCompletion() const override LOCKS_EXCLUDED(mu_);
|
||||
|
||||
private:
|
||||
xla::LocalClient* client() const;
|
||||
Allocator* GetAllocatorLocked(AllocatorAttributes attr)
|
||||
EXCLUSIVE_LOCKS_REQUIRED(mu_);
|
||||
Status EnsureStreamOkLocked(xla::Backend* backend, const string& name,
|
||||
std::shared_ptr<se::Stream>* stream,
|
||||
bool* stream_was_changed)
|
||||
EXCLUSIVE_LOCKS_REQUIRED(mu_);
|
||||
xla::StatusOr<XlaDeviceContext*> GetDeviceContextLocked()
|
||||
EXCLUSIVE_LOCKS_REQUIRED(mu_);
|
||||
|
||||
static Status GetMetadataFromDevice(DeviceBase* device,
|
||||
const XlaDevice::Metadata** metadata);
|
||||
|
||||
mutable mutex mu_;
|
||||
// The metadata of this XlaDevice.
|
||||
const Metadata xla_metadata_;
|
||||
// Which hardware device in the client's platform this XlaDevice controls.
|
||||
const int device_ordinal_;
|
||||
// The name of the device that is used to compile Ops for this XlaDevice.
|
||||
const DeviceType jit_device_name_;
|
||||
// The platform for this device.
|
||||
se::Platform* const platform_; // Not owned.
|
||||
// Memory allocator associated with this device.
|
||||
Allocator* xla_allocator_ GUARDED_BY(mu_) = nullptr; // Not owned.
|
||||
// Stream associated with this device. Operations enqueued on this
|
||||
// stream are executed on the device. Operations include data
|
||||
// copying back and forth between CPU and the device, and
|
||||
// computations enqueued by XLA.
|
||||
std::shared_ptr<se::Stream> stream_ GUARDED_BY(mu_);
|
||||
// If false, only stream_ is valid and all computation and transfers use
|
||||
// stream_. If true, computation is performed by stream_ and transfers are
|
||||
// performed by host_to_device/device_to_host_stream.
|
||||
const bool use_multiple_streams_;
|
||||
// If use_multiple_streams_, host to device transfers are performed using this
|
||||
// stream.
|
||||
std::shared_ptr<se::Stream> host_to_device_stream_ GUARDED_BY(mu_);
|
||||
// If use_multiple_streams_, device to host transfers are performed using this
|
||||
// stream.
|
||||
std::shared_ptr<se::Stream> device_to_host_stream_ GUARDED_BY(mu_);
|
||||
// Must we use XLA's transfer manager for correct host<->device transfers? if
|
||||
// false, we can use ThenMemcpy() instead.
|
||||
const bool transfer_as_literal_;
|
||||
const XlaCompiler::ShapeRepresentationFn shape_representation_fn_;
|
||||
|
||||
// The device context accessed by all users of the XlaDevice, set by calls to
|
||||
// EnsureDeviceContextOk. If gpu_device_info_ is non-null, this pointer is
|
||||
// also filled in to that struct. XlaDeviceContext is a ref-counted object.
|
||||
XlaDeviceContext* device_context_ GUARDED_BY(mu_) = nullptr;
|
||||
|
||||
// Holds extra information for GPU and TPU devices, e.g. the device context.
|
||||
bool use_gpu_device_info_ GUARDED_BY(mu_) = false;
|
||||
std::unique_ptr<GpuDeviceInfo> gpu_device_info_ GUARDED_BY(mu_);
|
||||
|
||||
// Thread pool used for running closures
|
||||
std::unique_ptr<thread::ThreadPool> thread_pool_;
|
||||
|
||||
// True if the device requires XlaDevice::Sync to be called on completion
|
||||
// regardless of status.
|
||||
bool sync_on_completion_ GUARDED_BY(mu_) = false;
|
||||
};
|
||||
|
||||
// Builds OpKernel registrations on 'device' for the JIT operators
|
||||
// registered on 'jit_device'. Returns ownership of a XlaDeviceOpRegistrations
|
||||
// object that encapsulates the kernel registrations.
|
||||
struct XlaDeviceOpRegistrations {
|
||||
std::vector<std::unique_ptr<kernel_factory::OpKernelRegistrar>>
|
||||
op_kernel_registrars;
|
||||
};
|
||||
XlaDeviceOpRegistrations* RegisterXlaDeviceKernels(const char* device,
|
||||
const char* jit_device);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_XLA_DEVICE_H_
|
||||
@@ -0,0 +1,127 @@
|
||||
/* 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_COMPILER_JIT_XLA_DEVICE_CONTEXT_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_XLA_DEVICE_CONTEXT_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "tensorflow/compiler/jit/xla_tensor.h"
|
||||
#include "tensorflow/compiler/tf2xla/xla_compiler.h"
|
||||
#include "tensorflow/compiler/xla/client/global_data.h"
|
||||
#include "tensorflow/compiler/xla/client/local_client.h"
|
||||
#include "tensorflow/core/framework/allocator.h"
|
||||
#include "tensorflow/core/framework/device_base.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// The allocator used for Tensors assigned to the XLA device. The allocator
|
||||
// ignores the alignment and size of the request and always returns a new,
|
||||
// empty, XlaTensor.
|
||||
class XlaDeviceAllocator : public Allocator {
|
||||
public:
|
||||
XlaDeviceAllocator();
|
||||
~XlaDeviceAllocator() override;
|
||||
|
||||
string Name() override;
|
||||
|
||||
void* AllocateRaw(size_t alignment, size_t num_bytes) override;
|
||||
void DeallocateRaw(void* ptr) override;
|
||||
void GetStats(AllocatorStats* stats) override;
|
||||
};
|
||||
|
||||
// Helper class for managing data transfers between host and XLA devices.
|
||||
class XlaTransferManager {
|
||||
public:
|
||||
explicit XlaTransferManager(
|
||||
std::shared_ptr<se::Stream> compute_stream,
|
||||
std::shared_ptr<se::Stream> host_to_device_stream,
|
||||
std::shared_ptr<se::Stream> device_to_host_stream,
|
||||
xla::LocalClient* client, bool transfer_as_literal,
|
||||
XlaCompiler::ShapeRepresentationFn shape_representation_fn,
|
||||
thread::ThreadPool* thread_pool);
|
||||
|
||||
void CopyCPUTensorToDevice(const Tensor* cpu_tensor, Device* device,
|
||||
Tensor* device_tensor, StatusCallback done) const;
|
||||
void CopyDeviceTensorToCPU(const Tensor* device_tensor,
|
||||
absl::string_view tensor_name, Device* device,
|
||||
Tensor* cpu_tensor, StatusCallback done);
|
||||
|
||||
void CopyDeviceTensorToDevice(const Tensor& src_tensor, Tensor* dst_tensor,
|
||||
const StatusCallback& done);
|
||||
|
||||
se::Stream* stream() const { return stream_.get(); }
|
||||
|
||||
private:
|
||||
Status TransferLiteralToDevice(const Tensor& host_tensor,
|
||||
Tensor* device_tensor) const;
|
||||
void TransferLiteralFromDevice(Tensor* host_tensor,
|
||||
const Tensor& device_tensor,
|
||||
const StatusCallback& done) const;
|
||||
bool UseMultipleStreams() const { return stream_ != host_to_device_stream_; }
|
||||
|
||||
// The main compute stream of the device, used to synchronize the transfer
|
||||
// streams if they are set.
|
||||
std::shared_ptr<se::Stream> stream_;
|
||||
// The stream to use for transferring data from host to device. Can be
|
||||
// idential to stream_, but must not be nullptr.
|
||||
std::shared_ptr<se::Stream> host_to_device_stream_;
|
||||
// The stream to use for transferring data from device to host. Can be
|
||||
// idential to stream_, but must not be nullptr.
|
||||
std::shared_ptr<se::Stream> device_to_host_stream_;
|
||||
// For the underlying memory allocator and XLA's TransferManager.
|
||||
xla::LocalClient* client_;
|
||||
// Transfer manager, for marshalling data to and from the device.
|
||||
xla::TransferManager* transfer_manager_;
|
||||
// True if we must use XLA's TransferManager for correct device transfers.
|
||||
const bool transfer_as_literal_;
|
||||
XlaCompiler::ShapeRepresentationFn shape_representation_fn_;
|
||||
|
||||
// Thread pool used for running closures
|
||||
thread::ThreadPool* thread_pool_;
|
||||
};
|
||||
|
||||
// DeviceContext for operators assigned to XlaDevice devices. The
|
||||
// implementation must inherit from DeviceContext but otherwise just
|
||||
// wraps the methods in XlaTransferManager.
|
||||
class XlaDeviceContext : public DeviceContext {
|
||||
public:
|
||||
explicit XlaDeviceContext(
|
||||
std::shared_ptr<se::Stream> compute_stream,
|
||||
std::shared_ptr<se::Stream> host_to_device_stream,
|
||||
std::shared_ptr<se::Stream> device_to_host_stream,
|
||||
xla::LocalClient* client, bool transfer_as_literal,
|
||||
XlaCompiler::ShapeRepresentationFn shape_representation_fn,
|
||||
thread::ThreadPool* thread_pool);
|
||||
|
||||
void CopyCPUTensorToDevice(const Tensor* cpu_tensor, Device* device,
|
||||
Tensor* device_tensor,
|
||||
StatusCallback done) const override;
|
||||
void CopyDeviceTensorToCPU(const Tensor* device_tensor,
|
||||
absl::string_view tensor_name, Device* device,
|
||||
Tensor* cpu_tensor, StatusCallback done) override;
|
||||
void CopyDeviceTensorToDevice(const Tensor& src_tensor, Tensor* dst_tensor,
|
||||
const StatusCallback& done);
|
||||
|
||||
se::Stream* stream() const override { return manager_.stream(); }
|
||||
|
||||
private:
|
||||
XlaTransferManager manager_;
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_XLA_DEVICE_CONTEXT_H_
|
||||
@@ -0,0 +1,264 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// Common kernel registrations for XLA devices.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_JIT_XLA_DEVICE_OPS_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_XLA_DEVICE_OPS_H_
|
||||
|
||||
#include "tensorflow/core/framework/op_kernel.h"
|
||||
#include "tensorflow/core/framework/resource_mgr.h"
|
||||
#include "tensorflow/core/kernels/cast_op.h"
|
||||
#include "tensorflow/core/kernels/constant_op.h"
|
||||
#include "tensorflow/core/kernels/control_flow_ops.h"
|
||||
#include "tensorflow/core/kernels/data/generator_dataset_op.h"
|
||||
#include "tensorflow/core/kernels/data/iterator_ops.h"
|
||||
#include "tensorflow/core/kernels/data/prefetch_dataset_op.h"
|
||||
#include "tensorflow/core/kernels/fifo_queue.h"
|
||||
#include "tensorflow/core/kernels/function_ops.h"
|
||||
#include "tensorflow/core/kernels/identity_n_op.h"
|
||||
#include "tensorflow/core/kernels/identity_op.h"
|
||||
#include "tensorflow/core/kernels/no_op.h"
|
||||
#include "tensorflow/core/kernels/queue_op.h"
|
||||
#include "tensorflow/core/kernels/resource_variable_ops.h"
|
||||
#include "tensorflow/core/kernels/sendrecv_ops.h"
|
||||
#include "tensorflow/core/kernels/shape_ops.h"
|
||||
#include "tensorflow/core/kernels/variable_ops.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Dummy OpKernel, used for kernels assigned to an XLA device that should be
|
||||
// compiled. Should never be called at runtime since such ops should be
|
||||
// rewritten to a XlaLaunch op. If it is called, it means the placer placed an
|
||||
// operator on an XLA device but the compiler did not compile it.
|
||||
class XlaDeviceDummyOp : public OpKernel {
|
||||
public:
|
||||
explicit XlaDeviceDummyOp(OpKernelConstruction* ctx);
|
||||
void Compute(OpKernelContext* ctx) override;
|
||||
};
|
||||
|
||||
class XlaAssignVariableOp : public AsyncOpKernel {
|
||||
public:
|
||||
explicit XlaAssignVariableOp(OpKernelConstruction* c);
|
||||
void ComputeAsync(OpKernelContext* context, DoneCallback done) override;
|
||||
|
||||
private:
|
||||
DataType dtype_;
|
||||
};
|
||||
|
||||
#define REGISTER_XLA_LAUNCH_KERNEL(DEVICE, KERNEL, TYPES) \
|
||||
REGISTER_KERNEL_BUILDER(Name("XlaLaunch") \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("constants") \
|
||||
.HostMemory("resources"), \
|
||||
KERNEL);
|
||||
|
||||
#define REGISTER_XLA_COMPILE_KERNEL(DEVICE, KERNEL, TYPES) \
|
||||
REGISTER_KERNEL_BUILDER(Name("_XlaCompile") \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("constants") \
|
||||
.HostMemory("resources"), \
|
||||
KERNEL);
|
||||
|
||||
#define REGISTER_XLA_RUN_KERNEL(DEVICE, KERNEL, TYPES) \
|
||||
REGISTER_KERNEL_BUILDER(Name("_XlaRun").Device(DEVICE), KERNEL);
|
||||
|
||||
#define REGISTER_XLA_DEVICE_KERNELS(DEVICE, TYPES) \
|
||||
REGISTER_KERNEL_BUILDER(Name("_Send").Device(DEVICE), SendOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("_Recv").Device(DEVICE), RecvOp); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("_HostSend").Device(DEVICE).HostMemory("tensor"), SendOp); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("_HostRecv").Device(DEVICE).HostMemory("tensor"), RecvOp); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("_HostCast").Device(DEVICE).HostMemory("x").HostMemory("y"), \
|
||||
CpuCastOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("NoOp").Device(DEVICE), NoOp); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("Const").Device(DEVICE).TypeConstraint("dtype", TYPES), \
|
||||
ConstantOp); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("Identity").Device(DEVICE).TypeConstraint("T", TYPES), IdentityOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("IdentityN").Device(DEVICE), IdentityNOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("Placeholder").Device(DEVICE), PlaceholderOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("PlaceholderV2").Device(DEVICE), \
|
||||
PlaceholderOp); \
|
||||
\
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("VarHandleOp").Device(DEVICE).HostMemory("resource"), \
|
||||
ResourceHandleOp<Var>); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("_VarHandlesOp").Device(DEVICE).HostMemory("resources"), \
|
||||
ResourceHandlesOp<Var>); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("ReadVariableOp").Device(DEVICE).HostMemory("resource"), \
|
||||
ReadVariableOp); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("_ReadVariablesOp").Device(DEVICE).HostMemory("resources"), \
|
||||
ReadVariablesOp); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("DestroyResourceOp").Device(DEVICE).HostMemory("resource"), \
|
||||
DestroyResourceOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("Shape") \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("output") \
|
||||
.TypeConstraint<int32>("out_type") \
|
||||
.TypeConstraint("T", TYPES), \
|
||||
ShapeOp<int32>); \
|
||||
REGISTER_KERNEL_BUILDER(Name("Shape") \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("output") \
|
||||
.TypeConstraint<int64>("out_type") \
|
||||
.TypeConstraint("T", TYPES), \
|
||||
ShapeOp<int64>); \
|
||||
REGISTER_KERNEL_BUILDER(Name("ShapeN") \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("output") \
|
||||
.TypeConstraint<int32>("out_type") \
|
||||
.TypeConstraint("T", TYPES), \
|
||||
ShapeNOp<int32>); \
|
||||
REGISTER_KERNEL_BUILDER(Name("ShapeN") \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("output") \
|
||||
.TypeConstraint<int64>("out_type") \
|
||||
.TypeConstraint("T", TYPES), \
|
||||
ShapeNOp<int64>); \
|
||||
REGISTER_KERNEL_BUILDER(Name("Size") \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("output") \
|
||||
.TypeConstraint<int32>("out_type") \
|
||||
.TypeConstraint("T", TYPES), \
|
||||
SizeOp<int32>); \
|
||||
REGISTER_KERNEL_BUILDER(Name("Size") \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("output") \
|
||||
.TypeConstraint<int64>("out_type") \
|
||||
.TypeConstraint("T", TYPES), \
|
||||
SizeOp<int64>); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("Rank").Device(DEVICE).HostMemory("output").TypeConstraint("T", \
|
||||
TYPES), \
|
||||
RankOp); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("AssignVariableOp").Device(DEVICE).HostMemory("resource"), \
|
||||
XlaAssignVariableOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("ControlTrigger").Device(DEVICE), \
|
||||
ControlTriggerOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("Switch").Device(DEVICE).HostMemory("pred"), \
|
||||
SwitchOp); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("Merge").Device(DEVICE).HostMemory("value_index"), MergeOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("Enter").Device(DEVICE), EnterOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("Exit").Device(DEVICE), ExitOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("NextIteration").Device(DEVICE), \
|
||||
NextIterationOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("LoopCond") \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("input") \
|
||||
.HostMemory("output"), \
|
||||
LoopCondOp); \
|
||||
\
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("QueueEnqueueV2").Device(DEVICE).HostMemory("handle"), EnqueueOp); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("QueueDequeueV2").Device(DEVICE).HostMemory("handle"), DequeueOp); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("QueueCloseV2").Device(DEVICE).HostMemory("handle"), QueueCloseOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("QueueSizeV2") \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("size") \
|
||||
.HostMemory("handle"), \
|
||||
QueueSizeOp); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("QueueIsClosedV2").Device(DEVICE).HostMemory("handle"), \
|
||||
QueueIsClosedOp); \
|
||||
\
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("FIFOQueueV2").Device(DEVICE).HostMemory("handle"), FIFOQueueOp); \
|
||||
\
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name(kArgOp).Device(DEVICE).HostMemory("output").TypeConstraint("T", \
|
||||
TYPES), \
|
||||
ArgOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name(kArgOp) \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("output") \
|
||||
.TypeConstraint<ResourceHandle>("T"), \
|
||||
ArgOp); \
|
||||
\
|
||||
REGISTER_KERNEL_BUILDER(Name(kRetOp) \
|
||||
.Device(DEVICE) \
|
||||
.TypeConstraint("T", TYPES) \
|
||||
.HostMemory("input"), \
|
||||
RetvalOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name(kRetOp) \
|
||||
.Device(DEVICE) \
|
||||
.TypeConstraint<ResourceHandle>("T") \
|
||||
.HostMemory("input"), \
|
||||
RetvalOp); \
|
||||
\
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("RemoteCall").Device(DEVICE).HostMemory("target"), RemoteCallOp); \
|
||||
\
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("GeneratorDataset").Device(DEVICE).HostMemory("handle"), \
|
||||
data::GeneratorDatasetOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("PrefetchDataset") \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("buffer_size") \
|
||||
.HostMemory("input_dataset") \
|
||||
.HostMemory("handle"), \
|
||||
data::PrefetchDatasetOp); \
|
||||
\
|
||||
REGISTER_KERNEL_BUILDER(Name("IteratorV2").Device(DEVICE), \
|
||||
data::IteratorHandleOp); \
|
||||
REGISTER_KERNEL_BUILDER( \
|
||||
Name("MakeIterator").Device(DEVICE).HostMemory("dataset"), \
|
||||
data::MakeIteratorOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("AnonymousIterator").Device(DEVICE), \
|
||||
data::AnonymousIteratorHandleOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("IteratorGetNext").Device(DEVICE), \
|
||||
data::IteratorGetNextOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("IteratorGetNextSync").Device(DEVICE), \
|
||||
data::IteratorGetNextSyncOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("IteratorToStringHandle") \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("string_handle"), \
|
||||
data::IteratorToStringHandleOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name("IteratorFromStringHandleV2") \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("string_handle"), \
|
||||
data::IteratorFromStringHandleOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name(FunctionLibraryDefinition::kArgOp) \
|
||||
.Device(DEVICE) \
|
||||
.HostMemory("output") \
|
||||
.TypeConstraint<string>("T"), \
|
||||
ArgOp); \
|
||||
REGISTER_KERNEL_BUILDER(Name(FunctionLibraryDefinition::kRetOp) \
|
||||
.Device(DEVICE) \
|
||||
.TypeConstraint<string>("T") \
|
||||
.HostMemory("input"), \
|
||||
RetvalOp);
|
||||
|
||||
// TODO(phawkins): currently we do not register the QueueEnqueueMany,
|
||||
// QueueDequeueMany, or QueueDequeueUpTo kernels because they attempt to read
|
||||
// and write the tensors they access in order to concatenate them into a batch.
|
||||
// We would need either to call out to an XLA computation to perform the
|
||||
// concatenation, or we would need to refactor those kernels so the splitting
|
||||
// or merging is done in a separate operator that can be compiled.
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_XLA_DEVICE_OPS_H_
|
||||
@@ -0,0 +1,181 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// Contains utilities for launching compiled XLA kernels for a KernelContext.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_JIT_XLA_LAUNCH_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_XLA_LAUNCH_UTIL_H_
|
||||
|
||||
#include "tensorflow/compiler/jit/xla_compilation_cache.h"
|
||||
#include "tensorflow/compiler/jit/xla_tensor.h"
|
||||
#include "tensorflow/compiler/tf2xla/xla_compiler.h"
|
||||
#include "tensorflow/compiler/xla/client/local_client.h"
|
||||
#include "tensorflow/compiler/xla/service/device_memory_allocator.h"
|
||||
#include "tensorflow/compiler/xla/service/owning_device_memory.h"
|
||||
#include "tensorflow/core/framework/allocation_description.pb.h"
|
||||
#include "tensorflow/core/framework/tensor.h"
|
||||
#include "tensorflow/core/framework/types.h"
|
||||
#include "tensorflow/core/kernels/variable_ops.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
#include "tensorflow/core/lib/gtl/array_slice.h"
|
||||
|
||||
namespace tensorflow {
|
||||
class XlaAllocator;
|
||||
|
||||
// Takes a snapshot of the values of resource variable arguments, whose
|
||||
// indices are specified in `variables` argument. We snapshot tensors that back
|
||||
// resource variables since concurrent updates may modify the shape, and it is
|
||||
// important that the shapes used for compilation match the true shapes of the
|
||||
// buffers.
|
||||
//
|
||||
// Returns a map of TensorFlow argument index to resource variable. If a
|
||||
// resource variable is not initialized, the corresponding OptionalTensor
|
||||
// will have its `present` field set to false.
|
||||
std::map<int, OptionalTensor> SnapshotResourceVariables(
|
||||
OpKernelContext* ctx, absl::Span<const int> variables);
|
||||
|
||||
// Adapter class that wraps a Tensorflow allocator as an XLA allocator.
|
||||
// Assumes that the Tensorflow allocator permits asynchronous deallocation:
|
||||
// see comment on `AllowsAsynchronousDeallocation()`.
|
||||
class XlaAllocator : public xla::DeviceMemoryAllocator {
|
||||
public:
|
||||
XlaAllocator(const se::Platform* platform, Allocator* wrapped);
|
||||
~XlaAllocator() override;
|
||||
xla::StatusOr<xla::OwningDeviceMemory> Allocate(
|
||||
int device_ordinal, uint64 size, bool retry_on_failure) override;
|
||||
Status Deallocate(int device_ordinal, se::DeviceMemoryBase mem) override;
|
||||
|
||||
// The Tensorflow BFC allocator used on GPU allows host-side deallocation
|
||||
// before GPU execution takes place. Tensorflow uses the ordering of the main
|
||||
// compute stream to enforce a happens-before relationship between a memory
|
||||
// allocation and code that reuses the same memory. If Tensorflow adds
|
||||
// support for multiple GPU streams or allocators with different ordering
|
||||
// requirements, this code may need to change.
|
||||
// (This attribute has no effect on CPU.)
|
||||
bool AllowsAsynchronousDeallocation() const override { return true; }
|
||||
|
||||
private:
|
||||
Allocator* wrapped_;
|
||||
};
|
||||
|
||||
// Helper class to perform the marshalling of TensorFlow inputs and outputs to
|
||||
// ShapedBuffers suitable for passing to an XLA computation.
|
||||
class XlaComputationLaunchContext {
|
||||
public:
|
||||
// Create a new launch context. 'allocate_xla_tensors' is true if allocated
|
||||
// output tensors and variables are always XlaTensors. If false they are
|
||||
// assumed to be "normal" device pointers.
|
||||
// If 'use_multiple_streams' is true, tensors may be defined and used on
|
||||
// multiple streams and so se::Events must be defined and waited for. If
|
||||
// 'use_multiple_streams' is true, 'allocate_xla_tensors' must also be true
|
||||
// because we track inter-stream dependencies through events inside XlaTensor
|
||||
// objects.
|
||||
XlaComputationLaunchContext(xla::LocalClient* client,
|
||||
xla::DeviceMemoryAllocator* xla_allocator,
|
||||
bool allocate_xla_tensors,
|
||||
bool use_multiple_streams);
|
||||
|
||||
// Add all inputs within `ctx` as XLA arguments (returned by arguments()).
|
||||
// `variables` is a map from TensorFlow argument number to resource variable.
|
||||
//
|
||||
// Assumes that the first `missing_ctx_input_prefix` inputs to the kernel are
|
||||
// missing and adjusts input indices accordingly. All elements in kernel's
|
||||
// input_mapping must be greater than or equal to `missing_ctx_input_prefix`
|
||||
// (in other words, no inputs actually required by the kernel can be missing).
|
||||
void PopulateInputs(OpKernelContext* ctx,
|
||||
const XlaCompiler::CompilationResult* kernel,
|
||||
const std::map<int, OptionalTensor>& variables,
|
||||
int missing_ctx_input_prefix);
|
||||
|
||||
// Given the XLA output in `output`, populate all outputs of `ctx`.
|
||||
//
|
||||
// Assumes that the first `missing_ctx_input_prefix` inputs to the kernel are
|
||||
// missing and adjusts input indices accordingly.
|
||||
Status PopulateOutputs(OpKernelContext* ctx,
|
||||
const XlaCompiler::CompilationResult* kernel,
|
||||
xla::ScopedShapedBuffer output,
|
||||
int missing_ctx_input_prefix);
|
||||
|
||||
// Return the argument list. Only valid after PopulateInputs() has been
|
||||
// called.
|
||||
const std::vector<xla::ShapedBuffer*>& arguments() const { return arg_ptrs_; }
|
||||
|
||||
private:
|
||||
xla::LocalClient* client_;
|
||||
xla::DeviceMemoryAllocator* xla_allocator_;
|
||||
bool allocate_xla_tensors_;
|
||||
bool use_multiple_streams_;
|
||||
std::vector<std::unique_ptr<xla::ShapedBuffer>> arg_buffers_;
|
||||
std::vector<xla::ShapedBuffer*> arg_ptrs_;
|
||||
};
|
||||
|
||||
// A simple TensorBuffer implementation that allows us to create Tensors that
|
||||
// take ownership of pre-allocated memory.
|
||||
class XlaTensorBuffer : public TensorBuffer {
|
||||
public:
|
||||
XlaTensorBuffer(const void* ptr, size_t expected_size, size_t actual_size,
|
||||
Allocator* allocator)
|
||||
: expected_size_(expected_size),
|
||||
actual_size_(actual_size),
|
||||
allocator_(allocator) {
|
||||
data_ = const_cast<void*>(ptr);
|
||||
}
|
||||
|
||||
~XlaTensorBuffer() override {
|
||||
if (data_) {
|
||||
allocator_->DeallocateRaw(data_);
|
||||
}
|
||||
}
|
||||
|
||||
void* data() const override { return data_; }
|
||||
size_t size() const override { return expected_size_; }
|
||||
|
||||
TensorBuffer* root_buffer() override { return this; }
|
||||
|
||||
void FillAllocationDescription(AllocationDescription* proto) const override {
|
||||
proto->set_allocated_bytes(actual_size_);
|
||||
}
|
||||
|
||||
static Tensor MakeTensor(DataType dtype, const TensorShape& shape,
|
||||
se::DeviceMemoryBase buffer, Allocator* allocator) {
|
||||
size_t expected_size = shape.num_elements() * DataTypeSize(dtype);
|
||||
auto* tensor_buffer = new XlaTensorBuffer(buffer.opaque(), expected_size,
|
||||
buffer.size(), allocator);
|
||||
Tensor t(dtype, shape, tensor_buffer);
|
||||
tensor_buffer->Unref();
|
||||
return t;
|
||||
}
|
||||
|
||||
private:
|
||||
void* data_;
|
||||
size_t expected_size_;
|
||||
size_t actual_size_;
|
||||
Allocator* allocator_;
|
||||
};
|
||||
|
||||
// Exposed in this header file for microbenchmarking purposes, but this is an
|
||||
// internal implementation detail.
|
||||
namespace internal {
|
||||
// Return the 'index''th subtree of the given ShapedBuffer as a
|
||||
// ScopedShapedBuffer. The returned ScopedShapedBuffer takes ownership of the
|
||||
// subtree, and sets the input's buffer pointers to nullptr for the subtree.
|
||||
xla::ScopedShapedBuffer ExtractSubShapedBuffer(
|
||||
xla::ShapedBuffer* shaped_buffer, int index,
|
||||
xla::DeviceMemoryAllocator* allocator);
|
||||
} // namespace internal
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_XLA_LAUNCH_UTIL_H_
|
||||
@@ -0,0 +1,131 @@
|
||||
/* 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_COMPILER_JIT_XLA_TENSOR_H_
|
||||
#define TENSORFLOW_COMPILER_JIT_XLA_TENSOR_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "absl/memory/memory.h"
|
||||
#include "tensorflow/compiler/xla/client/local_client.h"
|
||||
#include "tensorflow/compiler/xla/service/shaped_buffer.h"
|
||||
#include "tensorflow/core/framework/allocator.h"
|
||||
#include "tensorflow/core/framework/device_base.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
#include "tensorflow/core/platform/mutex.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// The implementation of a Tensor for an XlaDevice. All device tensors are
|
||||
// actually one of these.
|
||||
//
|
||||
// To distinguish between "normal" device tensors and XlaTensors, the raw
|
||||
// pointer data stored in the TensorBuffer is a tagged pointer.
|
||||
class XlaTensor {
|
||||
public:
|
||||
// Downcast from a Tensor to an XlaTensor. Return nullptr if the downcast
|
||||
// fails.
|
||||
static XlaTensor* FromTensor(const Tensor* tensor);
|
||||
|
||||
static bool RefCountIsOne(const Tensor& tensor);
|
||||
|
||||
// Create a DeviceMemoryBase from a Tensor. The Tensor can be an XlaTensor, in
|
||||
// which case the returned value is shaped_buffer()->root_buffer(), or a
|
||||
// normal Tensor in which case the returned value is
|
||||
// {tensor.tensor_data().data(), tensor.tensor_data().size}.
|
||||
static se::DeviceMemoryBase DeviceMemoryFromTensor(const Tensor& tensor);
|
||||
|
||||
// Assign the internal ShapedBuffer to new memory for the given dtype and
|
||||
// shape. If a ShapedBuffer exists already (has_shaped_buffer() == true), it
|
||||
// is replaced and the managed memory deallocated.
|
||||
Status AllocateShapedBuffer(DataType dtype, const TensorShape& shape,
|
||||
xla::LocalClient* client, int device_ordinal);
|
||||
|
||||
// Some Tensors can have complex on-device shapes, including tuple shapes. To
|
||||
// manage the memory for these tensors a ShapedBuffer may be required.
|
||||
|
||||
// Return true if this XlaTensor contains a ShapedBuffer.
|
||||
bool has_shaped_buffer() const { return shaped_buffer_ != nullptr; }
|
||||
// Return the contained ShapedBuffer.
|
||||
// REQUIRES: has_shaped_buffer()
|
||||
const xla::ShapedBuffer& shaped_buffer() const {
|
||||
CHECK(has_shaped_buffer());
|
||||
return *shaped_buffer_;
|
||||
}
|
||||
xla::ShapedBuffer& shaped_buffer() {
|
||||
CHECK(has_shaped_buffer());
|
||||
return *shaped_buffer_;
|
||||
}
|
||||
// Mutates the XlaTensor to set the ShapedBuffer.
|
||||
void set_shaped_buffer(xla::ScopedShapedBuffer shaped_buffer) {
|
||||
shaped_buffer_ =
|
||||
absl::make_unique<xla::ScopedShapedBuffer>(std::move(shaped_buffer));
|
||||
}
|
||||
|
||||
// Some tensors on the device may have known values on the host. We use these
|
||||
// in on-demand mode to avoid re-copying values from the device if we know the
|
||||
// host value already.
|
||||
|
||||
// Return true if this XlaTensor contains a host tensor.
|
||||
bool has_host_tensor() const { return host_tensor_ != nullptr; }
|
||||
// Return the contained host tensor.
|
||||
// REQUIRES: has_host_tensor()
|
||||
const Tensor& host_tensor() const { return *host_tensor_; }
|
||||
// Sets the contained host tensor.
|
||||
void set_host_tensor(const Tensor& tensor) {
|
||||
host_tensor_.reset(new Tensor(tensor));
|
||||
}
|
||||
|
||||
// If the tensor's content is not yet defined on 'stream', and there exists an
|
||||
// se::Event declaring when the tensor's content is defined, return it.
|
||||
// Otherwise, return nullptr. If this function returns nullptr then the
|
||||
// tensor's content can be read on 'stream' without additional
|
||||
// synchronization.
|
||||
se::Event* GetDefinitionEvent(se::Stream* stream);
|
||||
|
||||
// Assert that the tensor's content is defined on 'stream' by the time 'event'
|
||||
// triggers.
|
||||
void SetDefinedOn(se::Stream* stream, std::shared_ptr<se::Event> event);
|
||||
|
||||
// Assert that the tensor's content is defined on 'stream'. This version does
|
||||
// not provide an event, and must be called *after* SetDefinedOn(Stream,
|
||||
// Event). This call can be read as an assertion that the definition event has
|
||||
// been waited on by 'stream', so further calls to GetDefinitionEvent(stream)
|
||||
// do not need to also wait on the event.
|
||||
void SetDefinedOn(se::Stream* stream);
|
||||
|
||||
// Convert from a raw pointer to an XlaTensor, removing the pointer tag.
|
||||
static XlaTensor* FromOpaquePointer(void* ptr);
|
||||
// Convert to a raw pointer from an XlaTensor, adding the pointer tag.
|
||||
static void* ToOpaquePointer(XlaTensor* tensor);
|
||||
|
||||
private:
|
||||
// The optional contained ShapedBuffer.
|
||||
std::unique_ptr<xla::ScopedShapedBuffer> shaped_buffer_;
|
||||
// An optional host tensor value.
|
||||
std::unique_ptr<Tensor> host_tensor_;
|
||||
// An optional event that is triggered when the tensor's content has been
|
||||
// defined. If this event is nullptr, it is assumed that the tensor's content
|
||||
// is always defined.
|
||||
std::shared_ptr<se::Event> definition_event_;
|
||||
// A list of all streams for which the tensor's content is defined for any
|
||||
// newly enqueued command.
|
||||
absl::InlinedVector<se::Stream*, 2> streams_defined_on_ GUARDED_BY(mu_);
|
||||
mutex mu_;
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_JIT_XLA_TENSOR_H_
|
||||
@@ -0,0 +1,108 @@
|
||||
// This file is MACHINE GENERATED! Do not edit.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_TF_XLA_CC_OPS_XLA_JIT_OPS_H_
|
||||
#define TENSORFLOW_COMPILER_TF_XLA_CC_OPS_XLA_JIT_OPS_H_
|
||||
|
||||
// This file is MACHINE GENERATED! Do not edit.
|
||||
|
||||
#include "tensorflow/cc/framework/ops.h"
|
||||
#include "tensorflow/cc/framework/scope.h"
|
||||
#include "tensorflow/core/framework/tensor.h"
|
||||
#include "tensorflow/core/framework/tensor_shape.h"
|
||||
#include "tensorflow/core/framework/types.h"
|
||||
#include "tensorflow/core/lib/gtl/array_slice.h"
|
||||
|
||||
namespace tensorflow {
|
||||
namespace ops {
|
||||
|
||||
/// @defgroup xla_jit_ops Xla Jit Ops
|
||||
/// @{
|
||||
|
||||
/// Operator that connects the output of an XLA computation to other consumer graph nodes.
|
||||
///
|
||||
/// Arguments:
|
||||
/// * scope: A Scope object
|
||||
///
|
||||
/// Returns:
|
||||
/// * `Output`: The outputs tensor.
|
||||
class XlaClusterOutput {
|
||||
public:
|
||||
XlaClusterOutput(const ::tensorflow::Scope& scope, ::tensorflow::Input input);
|
||||
operator ::tensorflow::Output() const { return outputs; }
|
||||
operator ::tensorflow::Input() const { return outputs; }
|
||||
::tensorflow::Node* node() const { return outputs.node(); }
|
||||
|
||||
Operation operation;
|
||||
::tensorflow::Output outputs;
|
||||
};
|
||||
|
||||
/// XLA Launch Op. For use by the XLA JIT only.
|
||||
///
|
||||
/// Arguments:
|
||||
/// * scope: A Scope object
|
||||
///
|
||||
/// Returns:
|
||||
/// * `OutputList`: The results tensor.
|
||||
class XlaLaunch {
|
||||
public:
|
||||
XlaLaunch(const ::tensorflow::Scope& scope, ::tensorflow::InputList constants,
|
||||
::tensorflow::InputList args, ::tensorflow::InputList resources,
|
||||
const DataTypeSlice& Tresults, const NameAttrList& function);
|
||||
::tensorflow::Output operator[](size_t index) const { return results[index]; }
|
||||
|
||||
|
||||
Operation operation;
|
||||
::tensorflow::OutputList results;
|
||||
};
|
||||
|
||||
/// XLA Compile Op. For use by the XLA JIT only.
|
||||
///
|
||||
/// Compiles a TensorFlow function into an XLA LocalExecutable and returns a key
|
||||
/// that _XlaRun can use to look up the LocalExecutable and execute it.
|
||||
///
|
||||
/// Arguments:
|
||||
/// * scope: A Scope object
|
||||
///
|
||||
/// Returns:
|
||||
/// * `Output` key: A key that can be used to look up the local executable compiled by the
|
||||
/// node and associated metadata.
|
||||
/// * `Output` compilation_successful: True iff the compilation was successful. Always true
|
||||
/// for now.
|
||||
class _XlaCompile {
|
||||
public:
|
||||
_XlaCompile(const ::tensorflow::Scope& scope, ::tensorflow::InputList
|
||||
constants, ::tensorflow::InputList args, ::tensorflow::InputList
|
||||
resources, const NameAttrList& function);
|
||||
|
||||
Operation operation;
|
||||
::tensorflow::Output key;
|
||||
::tensorflow::Output compilation_successful;
|
||||
};
|
||||
|
||||
/// XLA Run Op. For use by the XLA JIT only.
|
||||
///
|
||||
/// Executes a TensorFlow function previously compiled into a LocalExecutable by an
|
||||
/// _XlaCompile op.
|
||||
///
|
||||
/// Arguments:
|
||||
/// * scope: A Scope object
|
||||
///
|
||||
/// Returns:
|
||||
/// * `OutputList`: The results tensor.
|
||||
class _XlaRun {
|
||||
public:
|
||||
_XlaRun(const ::tensorflow::Scope& scope, ::tensorflow::InputList args,
|
||||
::tensorflow::Input key, const DataTypeSlice& Tresults);
|
||||
::tensorflow::Output operator[](size_t index) const { return results[index]; }
|
||||
|
||||
|
||||
Operation operation;
|
||||
::tensorflow::OutputList results;
|
||||
};
|
||||
|
||||
/// @}
|
||||
|
||||
} // namespace ops
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF_XLA_CC_OPS_XLA_JIT_OPS_H_
|
||||
@@ -0,0 +1,45 @@
|
||||
/* 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_COMPILER_TF2XLA_CONST_ANALYSIS_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_CONST_ANALYSIS_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Backwards dataflow analysis that finds nodes in a graph that must be
|
||||
// compile-time constants for us to be able to lower the graph to XLA.
|
||||
//
|
||||
// The indices of the arguments to `graph` that must be constant are returned in
|
||||
// `compile_time_const_arg_indices`, if `compile_time_const_arg_indices` is not
|
||||
// null.
|
||||
//
|
||||
// The ids of the nodes in `graph` that must be constant are returned in
|
||||
// `compile_time_const_nodes`, if `compile_time_const_nodes` is not null.
|
||||
//
|
||||
// Only propagate const-ness along edges for which `edge_filter` returns true.
|
||||
Status BackwardsConstAnalysis(const Graph& g,
|
||||
std::vector<bool>* compile_time_const_arg_indices,
|
||||
std::vector<bool>* compile_time_const_nodes,
|
||||
std::function<bool(const Edge&)> edge_filter =
|
||||
[](const Edge& e) { return true; });
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_CONST_ANALYSIS_H_
|
||||
@@ -0,0 +1,165 @@
|
||||
/* 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_COMPILER_TF2XLA_CPU_FUNCTION_RUNTIME_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_CPU_FUNCTION_RUNTIME_H_
|
||||
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace tensorflow {
|
||||
namespace cpu_function_runtime {
|
||||
// Stores information about one buffer used by an XLA:CPU compiled function.
|
||||
// These buffers are used for holding inputs to the computation, outputs from
|
||||
// the computation and as temporary scratch space.
|
||||
class BufferInfo {
|
||||
public:
|
||||
// Creates a BufferInfo from a serialized encoding generated by `Encode`.
|
||||
explicit BufferInfo(std::pair<uint64, uint64> encoding)
|
||||
: entry_param_number_(encoding.second) {
|
||||
Kind kind;
|
||||
uint64 size;
|
||||
Unpack(encoding.first, &kind, &size);
|
||||
kind_ = kind;
|
||||
size_ = size;
|
||||
}
|
||||
|
||||
// Returns true if this buffer stores a constant. These never need to be
|
||||
// allocated by the runtime.
|
||||
bool is_constant() const { return kind() == Kind::kConstant; }
|
||||
|
||||
// Returns true if this buffer stores an entry parameter. These may or may
|
||||
// not need to be allocated by the runtime, depending on
|
||||
// XlaCompiledCpuFunction::AllocMode.
|
||||
bool is_entry_parameter() const { return kind() == Kind::kEntryParameter; }
|
||||
|
||||
// Returns the entry parameter number of this buffer.
|
||||
uint64 entry_parameter_number() const {
|
||||
assert(is_entry_parameter());
|
||||
return entry_param_number_;
|
||||
}
|
||||
|
||||
// Returns true if this buffer is temporary scratch space required by the XLA
|
||||
// computations. These are always allocated by the runtime.
|
||||
bool is_temp_buffer() const { return kind() == Kind::kTempBuffer; }
|
||||
|
||||
// Returns true if this buffer is allocated on the C stack or into registers.
|
||||
// These buffers are never allocated by the runtime.
|
||||
bool is_on_stack_buffer() const { return kind() == Kind::kOnStackBuffer; }
|
||||
|
||||
// Returns the size for this buffer.
|
||||
uint64 size() const { return size_; }
|
||||
|
||||
// Encodes this BufferInfo into two 64 bit integers that can be used to
|
||||
// reconstruct the BufferInfo later using the constructor. We need this
|
||||
// because we use BufferInfo in places where using protocol buffers would
|
||||
// negatively impact binary size.
|
||||
std::pair<uint64, uint64> Encode() const {
|
||||
static_assert(sizeof(*this) == 16, "");
|
||||
uint64 upper = Pack(kind(), size_);
|
||||
uint64 lower = entry_param_number_;
|
||||
return {upper, lower};
|
||||
}
|
||||
|
||||
bool operator==(const BufferInfo& buffer_info) const {
|
||||
if (kind() != buffer_info.kind() || size() != buffer_info.size()) {
|
||||
return false;
|
||||
}
|
||||
return !is_entry_parameter() ||
|
||||
entry_parameter_number() == buffer_info.entry_parameter_number();
|
||||
}
|
||||
|
||||
// Factory methods:
|
||||
|
||||
static BufferInfo MakeTempBuffer(uint64 size) {
|
||||
return BufferInfo(Kind::kTempBuffer, /*size=*/size,
|
||||
/*entry_param_number=*/-1);
|
||||
}
|
||||
static BufferInfo MakeConstant(uint64 size) {
|
||||
return BufferInfo(Kind::kConstant, /*size=*/size,
|
||||
/*entry_param_number=*/-1);
|
||||
}
|
||||
static BufferInfo MakeEntryParameter(uint64 size, uint64 param_number) {
|
||||
return BufferInfo(Kind::kEntryParameter, /*size=*/size,
|
||||
/*entry_param_number=*/param_number);
|
||||
}
|
||||
static BufferInfo MakeOnStackBuffer(uint64 size) {
|
||||
return BufferInfo(Kind::kOnStackBuffer, /*size=*/size,
|
||||
/*entry_param_number=*/-1);
|
||||
}
|
||||
|
||||
private:
|
||||
BufferInfo() = default;
|
||||
|
||||
enum class Kind : unsigned {
|
||||
kConstant,
|
||||
kTempBuffer,
|
||||
kEntryParameter,
|
||||
kOnStackBuffer
|
||||
};
|
||||
|
||||
Kind kind() const { return static_cast<Kind>(kind_); }
|
||||
|
||||
explicit BufferInfo(Kind kind, uint64 size, uint64 entry_param_number)
|
||||
: kind_(kind), size_(size), entry_param_number_(entry_param_number) {}
|
||||
|
||||
static uint64 Pack(Kind kind, uint64 size) {
|
||||
return (static_cast<uint64>(size) << 2) | static_cast<uint64>(kind);
|
||||
}
|
||||
|
||||
static void Unpack(uint64 packed, Kind* kind, uint64* size) {
|
||||
*size = packed >> 2;
|
||||
*kind = static_cast<Kind>((packed << 62) >> 62);
|
||||
}
|
||||
|
||||
Kind kind_ : 2;
|
||||
uint64 size_ : 62;
|
||||
int64 entry_param_number_;
|
||||
};
|
||||
|
||||
// Align to 64-bytes, to mimic tensorflow::Allocator::kAllocatorAlignment.
|
||||
constexpr size_t kAlign = 64;
|
||||
|
||||
// AlignedBufferBytes returns the sum of the size of each buffer in
|
||||
// `buffer_infos`, skipping constants, on-stack buffers and, if
|
||||
// allocate_entry_params is false, entry parameters. There are `n` entries in
|
||||
// `buffer_infos`. Each buffer is aligned to kAlign byte boundaries.
|
||||
size_t AlignedBufferBytes(const BufferInfo* buffer_infos, size_t n,
|
||||
bool allocate_entry_params);
|
||||
|
||||
// MallocContiguousBuffers allocates buffers for use by the entry point
|
||||
// generated by tfcompile. There are `n` entries in `buffer_infos`. If
|
||||
// `annotate_initialized` is set, the allocated memory will be annotated as
|
||||
// having been initialized - this is useful when allocating temporary buffers.
|
||||
// If allocate_entry_params is true then allocates temp buffers and entry
|
||||
// parameters, otherwise allocated only temp buffers. Slots in `bufs`
|
||||
// corresponding to unallocated buffers are set to nullptr.
|
||||
//
|
||||
// A single contiguous block of memory is allocated, and portions of it are
|
||||
// parceled out into `bufs`, which must have space for `n` entries. Returns
|
||||
// the head of the allocated contiguous block, which should be passed to
|
||||
// FreeContiguous when the buffers are no longer in use.
|
||||
void* MallocContiguousBuffers(const BufferInfo* buffer_infos, size_t n,
|
||||
bool allocate_entry_params, void** bufs,
|
||||
bool annotate_initialized);
|
||||
|
||||
// FreeContiguous frees the contiguous block of memory allocated by
|
||||
// MallocContiguousBuffers.
|
||||
void FreeContiguous(void* contiguous);
|
||||
} // namespace cpu_function_runtime
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_CPU_FUNCTION_RUNTIME_H_
|
||||
@@ -0,0 +1,50 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// Helper functions for dumping Graphs, GraphDefs, and FunctionDefs to files for
|
||||
// debugging.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_TF2XLA_DUMP_GRAPH_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_DUMP_GRAPH_H_
|
||||
|
||||
#include "tensorflow/core/framework/function.h"
|
||||
#include "tensorflow/core/framework/graph.pb.h"
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
|
||||
namespace tensorflow {
|
||||
namespace dump_graph {
|
||||
|
||||
// Dumps 'graph_def' to a file, as a GraphDef text proto. Returns the file name
|
||||
// chosen.
|
||||
//
|
||||
// Automatically picks a file name. Prefixes 'name' with the value of the
|
||||
// --tf_dump_graph_prefix flag and suffixes it with ".pbtxt" to form a name.
|
||||
// If a graph has already been dumped by this process with the same name,
|
||||
// suffixes with "_n.pbtxt", where 'n' is a sequence number.
|
||||
string DumpGraphDefToFile(const string& name, GraphDef const& graph_def);
|
||||
|
||||
// Similar to DumpGraphDefToFile, but builds the GraphDef to dump from a 'graph'
|
||||
// and an optional function library 'flib_def'. Returns the file name chosen.
|
||||
string DumpGraphToFile(const string& name, Graph const& graph,
|
||||
const FunctionLibraryDefinition* flib_def = nullptr);
|
||||
|
||||
// Similar to DumpGraphDefToFile, but dumps a function as a FunctionDef text
|
||||
// proto. Returns the file name chosen.
|
||||
string DumpFunctionDefToFile(const string& name, FunctionDef const& fdef);
|
||||
|
||||
} // namespace dump_graph
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_DUMP_GRAPH_H_
|
||||
@@ -0,0 +1,48 @@
|
||||
/* 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_COMPILER_TF2XLA_DUMP_GRAPH_FLAGS_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_DUMP_GRAPH_FLAGS_H_
|
||||
|
||||
// Legacy flags for the XLA bridge's dump_graph module.
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
#include "tensorflow/core/util/command_line_flags.h"
|
||||
|
||||
namespace tensorflow {
|
||||
namespace legacy_flags {
|
||||
|
||||
// Append to *flag_list flag definitions associated with the XLA bridge's
|
||||
// dump_graph module.
|
||||
void AppendDumpGraphFlags(std::vector<tensorflow::Flag>* flag_list);
|
||||
|
||||
// The values of flags associated with the XLA bridge's
|
||||
// dump_graph module.
|
||||
typedef struct {
|
||||
string tf_dump_graph_prefix; // Path prefix to which graphs dumped during
|
||||
// debugging should be written.
|
||||
} DumpGraphFlags;
|
||||
|
||||
// Return a pointer to the DumpGraphFlags struct;
|
||||
// repeated calls return the same pointer.
|
||||
// This should be called only after Flags::Parse() has returned.
|
||||
DumpGraphFlags* GetDumpGraphFlags();
|
||||
|
||||
} // namespace legacy_flags
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_DUMP_GRAPH_FLAGS_H_
|
||||
@@ -0,0 +1,243 @@
|
||||
/* 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_COMPILER_TF2XLA_FUNCTIONALIZE_COND_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_FUNCTIONALIZE_COND_H_
|
||||
|
||||
#include <deque>
|
||||
#include "tensorflow/compiler/xla/status_macros.h"
|
||||
#include "tensorflow/core/framework/function.h"
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Functionalize all the switch-merge nodes of a loop-free graph into If
|
||||
// nodes. That is, attempt to transform every remaining switch and merge nodes
|
||||
// in the graph into If nodes.
|
||||
// Precondition: All while loops have been removed from graph.
|
||||
Status FunctionalizeCond(Graph* graph, FunctionLibraryDefinition* library);
|
||||
|
||||
// Internal functions/classes exposed for testing purposes.
|
||||
namespace functionalize_cond {
|
||||
|
||||
// All nodes are assumed to be either in no branch, then branch, else branch,
|
||||
// or both branches (such as merge nodes).
|
||||
// The code below relies on Else and Then being 0 and 1 (corresponding to the
|
||||
// switch outputs). Both and Neither are arbitrary.
|
||||
enum class BranchType {
|
||||
kElseBranch = 0,
|
||||
kThenBranch = 1,
|
||||
kBoth = 2,
|
||||
kNeither = 3,
|
||||
};
|
||||
|
||||
// StateMap is responsible for mapping from each graph Node to
|
||||
// * a CondState, where each CondState is a map from predicate to branch (i,e.,
|
||||
// what predicates have to hold or not hold).
|
||||
// * a AncestorState, where each AncestorState is a set of switch/merge nodes
|
||||
// that are an ancestor of the node in the graph;
|
||||
// For efficiency, this class interns the CondState (AncestorState), so that
|
||||
// CondState (AncestorState) equality comparisons are simply pointer
|
||||
// comparisons.
|
||||
class StateMap {
|
||||
public:
|
||||
explicit StateMap(Graph* graph);
|
||||
|
||||
// Compare two OutputTensors by (node id, index).
|
||||
struct OutputTensorLess {
|
||||
bool operator()(const OutputTensor& lhs, const OutputTensor& rhs) const;
|
||||
};
|
||||
|
||||
// A node in the graph is executed when multiple conditions hold. Keep track
|
||||
// of the predicates that must hold for a node to execute.
|
||||
using CondState = std::map<OutputTensor, BranchType, OutputTensorLess>;
|
||||
|
||||
// Every unique ID is mapped to a CondState.
|
||||
using CondId = const CondState*;
|
||||
|
||||
// Keep track of which switch/merge node's feed into a node's values.
|
||||
using AncestorState = std::set<Node*>;
|
||||
|
||||
// Every unique ID is mapped to a AncestorState.
|
||||
using AncestorId = const AncestorState*;
|
||||
|
||||
// Returns the CondId for a given node.
|
||||
CondId LookupCondId(const Node* node) const;
|
||||
|
||||
// Returns the unique CondId for CondState.
|
||||
CondId GetCondId(const CondState& state);
|
||||
|
||||
// Resets the CondId for a given node.
|
||||
void ResetCondId(const Node* node, CondId id);
|
||||
|
||||
// Returns the AncestorId for a given node.
|
||||
AncestorId LookupAncestorId(const Node* node) const;
|
||||
|
||||
// Returns the unique AncestorId for CondState.
|
||||
AncestorId GetAncestorId(const AncestorState& state);
|
||||
|
||||
// Resets the AncestorId for a given node.
|
||||
void ResetAncestorId(const Node* node, AncestorId id);
|
||||
|
||||
// Marks `node` as dead.
|
||||
void MarkDead(const Node* node);
|
||||
|
||||
// Determine branch execution of CondState.
|
||||
BranchType FindBranchOf(CondId id, OutputTensor predicate) const;
|
||||
|
||||
// Returns textual representation of node's CondState.
|
||||
string CondStateToString(const Node* node) const;
|
||||
string CondStateToString(CondId id) const;
|
||||
|
||||
// Returns textual representation of node's AncestorState.
|
||||
string AncestorStateToString(const Node* node) const;
|
||||
|
||||
// Returns whether the cond state is the dead state.
|
||||
bool IsDead(CondId id) const;
|
||||
|
||||
// Returns whether the cond state is the empty state.
|
||||
bool IsEmpty(CondId id) const;
|
||||
|
||||
private:
|
||||
// Hash for CondState and AncestorState.
|
||||
struct Hash {
|
||||
size_t operator()(const CondState& map) const;
|
||||
size_t operator()(const AncestorState& map) const;
|
||||
};
|
||||
|
||||
// Set to keep track of unique CondStates.
|
||||
// Pointers to the entries in the unordered set are used as identifiers:
|
||||
// unordered_set guarantees that the pointers remain the same.
|
||||
std::unordered_set<CondState, Hash> condstate_set_;
|
||||
|
||||
// Mapping from Node id to CondId.
|
||||
std::vector<CondId> node_to_condid_map_;
|
||||
|
||||
// Track the CondId for newly inserted nodes. We use a vector to quickly map
|
||||
// from Node id in the original graph to the CondId, but there will be nodes
|
||||
// added to the original graph (such as If nodes) whose CondState needs to be
|
||||
// tracked too.
|
||||
std::unordered_map<int, CondId> added_node_condid_mapping_;
|
||||
|
||||
// AncestorId variants of the CondId members.
|
||||
std::unordered_set<AncestorState, Hash> ancestorstate_set_;
|
||||
std::vector<AncestorId> node_to_ancestorid_map_;
|
||||
std::unordered_map<int, AncestorId> added_node_ancestorid_mapping_;
|
||||
|
||||
// Identifier of the dead flow state. The empty flow state is represented with
|
||||
// a nullptr.
|
||||
CondId dead_id_;
|
||||
};
|
||||
|
||||
// FunctionalizeCond groups all the state used by functionalizing conditionals
|
||||
// of the given graph together.
|
||||
class FunctionalizeCond {
|
||||
public:
|
||||
// Functionalize all the switch-merge nodes of a loop-free graph into If
|
||||
// nodes. That is, attempt to transform every remaining switch and merge nodes
|
||||
// in the graph into If nodes.
|
||||
// Precondition: All while loops have been removed from graph.
|
||||
static Status Functionalize(Graph* graph, FunctionLibraryDefinition* library);
|
||||
|
||||
// Build identity node with the same name as the merge that will be replaced
|
||||
// in case the output is fetched/colocated.
|
||||
Status AddIdentityNode(const Node* replacee, Node* if_node, int port);
|
||||
|
||||
// Add a If node to the graph defined by def that will, amongst other, replace
|
||||
// replacee in the graph.
|
||||
xla::StatusOr<Node*> AddIfNode(const NodeDef& def, const Node* replacee,
|
||||
const OutputTensor& predicate);
|
||||
|
||||
// Propagates the state of a newly inserted node.
|
||||
Status PropagateUpdatedState(const Node* replacee);
|
||||
|
||||
// Dump graph with the CondState annotated.
|
||||
void DumpGraphWithCondState(const string& name);
|
||||
|
||||
private:
|
||||
FunctionalizeCond(Graph* graph, FunctionLibraryDefinition* library);
|
||||
|
||||
// Performs the actual cond functionalization. Iterate over groups of merge
|
||||
// nodes (linked by common predicates & ancestor IDs), from innermost to
|
||||
// outermost, and extract into If nodes.
|
||||
Status FunctionalizeInternal();
|
||||
|
||||
// Returns the forward flow state propagated along edge `e`.
|
||||
// This may modify state_map_.
|
||||
StateMap::CondId StateAlongEdge(const Edge* e);
|
||||
|
||||
// Determines the CondState and AncestorState of all the nodes in the given
|
||||
// vector where the input is expected in reverse topological order.
|
||||
// This populates the state_map_.
|
||||
Status DetermineStates(std::vector<Node*> rev_topo_order);
|
||||
|
||||
// Determine the CondState for a given node using the incomming edges
|
||||
// to the node. Note: it is expected that this node's CondState is only
|
||||
// determined once its input's CondState is.
|
||||
Status DetermineCondState(Node* dst) {
|
||||
if (IsMerge(dst)) return DetermineCondStateMerge(dst);
|
||||
return DetermineCondStateNonMerge(dst);
|
||||
}
|
||||
|
||||
// Helper functions for DetermineCondState.
|
||||
Status DetermineCondStateNonMerge(Node* dst);
|
||||
Status DetermineCondStateMerge(Node* dst);
|
||||
|
||||
// Determines the dst node's CondState by joining the src and dst's CondState
|
||||
// where either the dst node is a merge or not.
|
||||
// These may modify state_map_.
|
||||
xla::StatusOr<StateMap::CondId> JoinCondStatesMerge(Node* merge,
|
||||
StateMap::CondId src,
|
||||
StateMap::CondId dst);
|
||||
xla::StatusOr<StateMap::CondId> JoinCondStatesNonMerge(StateMap::CondId src,
|
||||
StateMap::CondId dst);
|
||||
|
||||
// Determines which switch/merge nodes are ancestors of this node.
|
||||
Status DetermineAncestorState(Node* dst);
|
||||
|
||||
// Checks if a merge node is redundant and if so removes it from the graph.
|
||||
Status RemoveRedundantMerge(Node* node);
|
||||
|
||||
// Checks if a switch node is redundant and if so removes it from the graph.
|
||||
Status RemoveRedundantSwitch(Node* node);
|
||||
|
||||
// Sorts merge nodes (in reverse topological order) in order of increasing
|
||||
// nesting depth.
|
||||
void SortMergeNodes(std::vector<Node*>* merge_order);
|
||||
|
||||
// Deletes all nodes in/consumers reachable from switch/merge nodes that were
|
||||
// extracted.
|
||||
void DeleteReachableAndDeadNodes(const std::vector<int>& switch_ids,
|
||||
const std::vector<Node*>& merge_order);
|
||||
|
||||
// Member used to unique the CondState to a unique CondId (AncestorState to a
|
||||
// unique AncestorId) and keep track of CondState/CondId
|
||||
// (AncestorState/AncestorId) per Node.
|
||||
StateMap state_map_;
|
||||
|
||||
// Mapping from merge nodes to predicate.
|
||||
std::unordered_map<Node*, OutputTensor> merge_to_predicate_;
|
||||
|
||||
FunctionLibraryDefinition* library_;
|
||||
Graph* graph_;
|
||||
|
||||
friend class FunctionalizeCondTest;
|
||||
};
|
||||
|
||||
} // namespace functionalize_cond
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_FUNCTIONALIZE_COND_H_
|
||||
@@ -0,0 +1,46 @@
|
||||
/* 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_COMPILER_TF2XLA_FUNCTIONALIZE_CONTROL_FLOW_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_FUNCTIONALIZE_CONTROL_FLOW_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/status_macros.h"
|
||||
#include "tensorflow/core/common_runtime/optimization_registry.h"
|
||||
#include "tensorflow/core/framework/function.h"
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Transformation that converts tf.while_loop() loops into functional While
|
||||
// operators and tf.cond() conditionals into function If operators, suitable for
|
||||
// XLA compilation. If lookup_library is provided, use it to make the library
|
||||
// for control flow self-contained.
|
||||
Status FunctionalizeControlFlow(Graph* graph,
|
||||
FunctionLibraryDefinition* library);
|
||||
Status FunctionalizeControlFlow(const FunctionLibraryDefinition* lookup_library,
|
||||
Graph* graph,
|
||||
FunctionLibraryDefinition* library);
|
||||
|
||||
// This pass looks at the graph and all associated FunctionDefs, and turns
|
||||
// traditional control flow structure (Switch/Merge/etc.) into functional
|
||||
// control flow structure (If/While).
|
||||
class FunctionalizeControlFlowPass : public GraphOptimizationPass {
|
||||
public:
|
||||
Status Run(const GraphOptimizationPassOptions& options) override;
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_FUNCTIONALIZE_CONTROL_FLOW_H_
|
||||
@@ -0,0 +1,56 @@
|
||||
/* 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_COMPILER_TF2XLA_FUNCTIONALIZE_CONTROL_FLOW_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_FUNCTIONALIZE_CONTROL_FLOW_UTIL_H_
|
||||
|
||||
#include "absl/strings/str_join.h"
|
||||
#include "tensorflow/compiler/xla/status_macros.h"
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
|
||||
// Utility functions shared between functionalize cond and while.
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Check that the graph has no cycle containing the given node.
|
||||
Status CheckNodeNotInCycle(const Node* node, const int num_nodes);
|
||||
|
||||
// Comparison function used for sorting nodes consistently.
|
||||
// a) resource variables are last, and
|
||||
// b) sort lexicographically by name (for deterministic output).
|
||||
struct NodeCmpByNameResourcesLast {
|
||||
bool operator()(const Node* lhs, const Node* rhs) const;
|
||||
};
|
||||
|
||||
// Returns the Node* created from the NodeDef in the Graph.
|
||||
xla::StatusOr<Node*> AddNodeDefToGraph(const NodeDef& node_def, Graph* graph);
|
||||
|
||||
// Build a retval node of given type and index.
|
||||
xla::StatusOr<Node*> BuildRetvalNode(Graph* graph, DataType type, int index);
|
||||
|
||||
// Returns a textual representation of the names of the nodes in the input.
|
||||
template <typename T>
|
||||
string NodesToString(const T& nodes) {
|
||||
return absl::StrCat("{",
|
||||
absl::StrJoin(nodes, ",",
|
||||
[](string* output, const Node* node) {
|
||||
absl::StrAppend(output, node->name());
|
||||
}),
|
||||
"}");
|
||||
}
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_FUNCTIONALIZE_CONTROL_FLOW_UTIL_H_
|
||||
@@ -0,0 +1,32 @@
|
||||
/* 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_COMPILER_TF2XLA_FUNCTIONALIZE_WHILE_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_FUNCTIONALIZE_WHILE_H_
|
||||
|
||||
#include "tensorflow/core/framework/function.h"
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Transformation that converts tf.while_loop() loops into functional While
|
||||
// operators, suitable for XLA compilation. If lookup_library is provided, use
|
||||
// it to make the library for control flow self-contained.
|
||||
Status FunctionalizeWhileLoop(const FunctionLibraryDefinition* lookup_library,
|
||||
Graph* graph, FunctionLibraryDefinition* library);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_FUNCTIONALIZE_WHILE_H_
|
||||
@@ -0,0 +1,96 @@
|
||||
/* 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_COMPILER_TF2XLA_GRAPH_COMPILER_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_GRAPH_COMPILER_H_
|
||||
|
||||
#include "tensorflow/compiler/tf2xla/xla_compilation_device.h"
|
||||
#include "tensorflow/compiler/tf2xla/xla_context.h"
|
||||
#include "tensorflow/compiler/xla/client/local_client.h"
|
||||
#include "tensorflow/core/common_runtime/device.h"
|
||||
#include "tensorflow/core/common_runtime/device_mgr.h"
|
||||
#include "tensorflow/core/common_runtime/function.h"
|
||||
#include "tensorflow/core/framework/function.h"
|
||||
#include "tensorflow/core/framework/op_kernel.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
#include "tensorflow/core/platform/env.h"
|
||||
#include "tensorflow/core/platform/mutex.h"
|
||||
#include "tensorflow/core/platform/notification.h"
|
||||
#include "tensorflow/core/platform/thread_annotations.h"
|
||||
#include "tensorflow/core/public/version.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// GraphCompiler compiles the graph in topological order in the current
|
||||
// thread. It also resolves the nondeterminism in the graph by enforcing a
|
||||
// total order on all inputs to a node. This abstraction helps us create the
|
||||
// same XLA computation given two structurally equivalent TensorFlow graphs.
|
||||
// If a function call is visited during the graph traversal, it is then
|
||||
// compiled through the xla_context into a computation and a `Call` operation
|
||||
// is inserted to call into that computation.
|
||||
//
|
||||
// Note: GraphCompiler was created to remove our dependency to TF Executor in
|
||||
// the history. There are still some todos so that we can completely decouple
|
||||
// from Executor.
|
||||
//
|
||||
// TODO(yunxing): Remove usage of XlaCompilationDevice.
|
||||
//
|
||||
// TODO(yunxing): Remove the hack that wraps XlaExpression within a tensor now
|
||||
// that we don't use TF Executor to pass around a tensor.
|
||||
//
|
||||
// TODO(yunxing): Make XlaOpkernel not a subclass of OpKernel so that it can
|
||||
// handle a XlaExpression directly instead of a Tensor. This may require our own
|
||||
// op registration infrastructure instead of FunctionLibraryRuntime.
|
||||
class GraphCompiler {
|
||||
public:
|
||||
GraphCompiler(XlaCompilationDevice* device, Graph* graph,
|
||||
FunctionLibraryRuntime* flib,
|
||||
ScopedStepContainer* step_container)
|
||||
: device_(device),
|
||||
graph_(graph),
|
||||
flib_(flib),
|
||||
step_container_(step_container) {}
|
||||
|
||||
// Compiles the graph. The results are written in xla_context stored in the
|
||||
// resource_manager of the 'XlaCompilationDevice' that's passed into the
|
||||
// constructor.
|
||||
Status Compile();
|
||||
|
||||
private:
|
||||
// Partially sets params. This partially set params can be reused
|
||||
// across multiple nodes visit.
|
||||
void PartiallySetupParams(OpKernelContext::Params* params);
|
||||
|
||||
// Tests if a node is a functional node. A functional node represents a
|
||||
// defined computation and should be compiled using `compiler_`.
|
||||
bool IsFunctional(Node* n);
|
||||
|
||||
// Compiles a functional node and writes result to OpkernelContext. A
|
||||
// functional node represents a defined computation and should be compiled
|
||||
// using `compiler_`.
|
||||
Status CompileFunctionalNode(Node* n, OpKernelContext* op_context);
|
||||
|
||||
XlaCompilationDevice* device_;
|
||||
Graph* graph_;
|
||||
FunctionLibraryRuntime* flib_;
|
||||
ScopedStepContainer* step_container_;
|
||||
// A buffer to hold tensor inputs to a node, this is reused across the graph
|
||||
// traversal.
|
||||
absl::InlinedVector<TensorValue, 4> tensor_inputs_;
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_GRAPH_COMPILER_H_
|
||||
@@ -0,0 +1,764 @@
|
||||
// Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||
// source: tensorflow/compiler/tf2xla/host_compute_metadata.proto
|
||||
|
||||
#ifndef PROTOBUF_INCLUDED_tensorflow_2fcompiler_2ftf2xla_2fhost_5fcompute_5fmetadata_2eproto
|
||||
#define PROTOBUF_INCLUDED_tensorflow_2fcompiler_2ftf2xla_2fhost_5fcompute_5fmetadata_2eproto
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <google/protobuf/stubs/common.h>
|
||||
|
||||
#if GOOGLE_PROTOBUF_VERSION < 3006000
|
||||
#error This file was generated by a newer version of protoc which is
|
||||
#error incompatible with your Protocol Buffer headers. Please update
|
||||
#error your headers.
|
||||
#endif
|
||||
#if 3006000 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
|
||||
#error This file was generated by an older version of protoc which is
|
||||
#error incompatible with your Protocol Buffer headers. Please
|
||||
#error regenerate this file with a newer version of protoc.
|
||||
#endif
|
||||
|
||||
#include <google/protobuf/io/coded_stream.h>
|
||||
#include <google/protobuf/arena.h>
|
||||
#include <google/protobuf/arenastring.h>
|
||||
#include <google/protobuf/generated_message_table_driven.h>
|
||||
#include <google/protobuf/generated_message_util.h>
|
||||
#include <google/protobuf/inlined_string_field.h>
|
||||
#include <google/protobuf/metadata.h>
|
||||
#include <google/protobuf/message.h>
|
||||
#include <google/protobuf/repeated_field.h> // IWYU pragma: export
|
||||
#include <google/protobuf/extension_set.h> // IWYU pragma: export
|
||||
#include <google/protobuf/unknown_field_set.h>
|
||||
#include "tensorflow/core/framework/tensor_shape.pb.h"
|
||||
#include "tensorflow/core/framework/types.pb.h"
|
||||
// @@protoc_insertion_point(includes)
|
||||
#define PROTOBUF_INTERNAL_EXPORT_protobuf_tensorflow_2fcompiler_2ftf2xla_2fhost_5fcompute_5fmetadata_2eproto
|
||||
|
||||
namespace protobuf_tensorflow_2fcompiler_2ftf2xla_2fhost_5fcompute_5fmetadata_2eproto {
|
||||
// Internal implementation detail -- do not use these members.
|
||||
struct TableStruct {
|
||||
static const ::google::protobuf::internal::ParseTableField entries[];
|
||||
static const ::google::protobuf::internal::AuxillaryParseTableField aux[];
|
||||
static const ::google::protobuf::internal::ParseTable schema[3];
|
||||
static const ::google::protobuf::internal::FieldMetadata field_metadata[];
|
||||
static const ::google::protobuf::internal::SerializationTable serialization_table[];
|
||||
static const ::google::protobuf::uint32 offsets[];
|
||||
};
|
||||
void AddDescriptors();
|
||||
} // namespace protobuf_tensorflow_2fcompiler_2ftf2xla_2fhost_5fcompute_5fmetadata_2eproto
|
||||
namespace tensorflow {
|
||||
namespace tf2xla {
|
||||
class HostComputeMetadata;
|
||||
class HostComputeMetadataDefaultTypeInternal;
|
||||
extern HostComputeMetadataDefaultTypeInternal _HostComputeMetadata_default_instance_;
|
||||
class HostTransferMetadata;
|
||||
class HostTransferMetadataDefaultTypeInternal;
|
||||
extern HostTransferMetadataDefaultTypeInternal _HostTransferMetadata_default_instance_;
|
||||
class TensorMetadata;
|
||||
class TensorMetadataDefaultTypeInternal;
|
||||
extern TensorMetadataDefaultTypeInternal _TensorMetadata_default_instance_;
|
||||
} // namespace tf2xla
|
||||
} // namespace tensorflow
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
template<> ::tensorflow::tf2xla::HostComputeMetadata* Arena::CreateMaybeMessage<::tensorflow::tf2xla::HostComputeMetadata>(Arena*);
|
||||
template<> ::tensorflow::tf2xla::HostTransferMetadata* Arena::CreateMaybeMessage<::tensorflow::tf2xla::HostTransferMetadata>(Arena*);
|
||||
template<> ::tensorflow::tf2xla::TensorMetadata* Arena::CreateMaybeMessage<::tensorflow::tf2xla::TensorMetadata>(Arena*);
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
namespace tensorflow {
|
||||
namespace tf2xla {
|
||||
|
||||
// ===================================================================
|
||||
|
||||
class TensorMetadata : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:tensorflow.tf2xla.TensorMetadata) */ {
|
||||
public:
|
||||
TensorMetadata();
|
||||
virtual ~TensorMetadata();
|
||||
|
||||
TensorMetadata(const TensorMetadata& from);
|
||||
|
||||
inline TensorMetadata& operator=(const TensorMetadata& from) {
|
||||
CopyFrom(from);
|
||||
return *this;
|
||||
}
|
||||
#if LANG_CXX11
|
||||
TensorMetadata(TensorMetadata&& from) noexcept
|
||||
: TensorMetadata() {
|
||||
*this = ::std::move(from);
|
||||
}
|
||||
|
||||
inline TensorMetadata& operator=(TensorMetadata&& from) noexcept {
|
||||
if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) {
|
||||
if (this != &from) InternalSwap(&from);
|
||||
} else {
|
||||
CopyFrom(from);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
inline ::google::protobuf::Arena* GetArena() const final {
|
||||
return GetArenaNoVirtual();
|
||||
}
|
||||
inline void* GetMaybeArenaPointer() const final {
|
||||
return MaybeArenaPtr();
|
||||
}
|
||||
static const ::google::protobuf::Descriptor* descriptor();
|
||||
static const TensorMetadata& default_instance();
|
||||
|
||||
static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY
|
||||
static inline const TensorMetadata* internal_default_instance() {
|
||||
return reinterpret_cast<const TensorMetadata*>(
|
||||
&_TensorMetadata_default_instance_);
|
||||
}
|
||||
static constexpr int kIndexInFileMessages =
|
||||
0;
|
||||
|
||||
void UnsafeArenaSwap(TensorMetadata* other);
|
||||
void Swap(TensorMetadata* other);
|
||||
friend void swap(TensorMetadata& a, TensorMetadata& b) {
|
||||
a.Swap(&b);
|
||||
}
|
||||
|
||||
// implements Message ----------------------------------------------
|
||||
|
||||
inline TensorMetadata* New() const final {
|
||||
return CreateMaybeMessage<TensorMetadata>(NULL);
|
||||
}
|
||||
|
||||
TensorMetadata* New(::google::protobuf::Arena* arena) const final {
|
||||
return CreateMaybeMessage<TensorMetadata>(arena);
|
||||
}
|
||||
void CopyFrom(const ::google::protobuf::Message& from) final;
|
||||
void MergeFrom(const ::google::protobuf::Message& from) final;
|
||||
void CopyFrom(const TensorMetadata& from);
|
||||
void MergeFrom(const TensorMetadata& from);
|
||||
void Clear() final;
|
||||
bool IsInitialized() const final;
|
||||
|
||||
size_t ByteSizeLong() const final;
|
||||
bool MergePartialFromCodedStream(
|
||||
::google::protobuf::io::CodedInputStream* input) final;
|
||||
void SerializeWithCachedSizes(
|
||||
::google::protobuf::io::CodedOutputStream* output) const final;
|
||||
::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray(
|
||||
bool deterministic, ::google::protobuf::uint8* target) const final;
|
||||
int GetCachedSize() const final { return _cached_size_.Get(); }
|
||||
|
||||
private:
|
||||
void SharedCtor();
|
||||
void SharedDtor();
|
||||
void SetCachedSize(int size) const final;
|
||||
void InternalSwap(TensorMetadata* other);
|
||||
protected:
|
||||
explicit TensorMetadata(::google::protobuf::Arena* arena);
|
||||
private:
|
||||
static void ArenaDtor(void* object);
|
||||
inline void RegisterArenaDtor(::google::protobuf::Arena* arena);
|
||||
private:
|
||||
inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
|
||||
return _internal_metadata_.arena();
|
||||
}
|
||||
inline void* MaybeArenaPtr() const {
|
||||
return _internal_metadata_.raw_arena_ptr();
|
||||
}
|
||||
public:
|
||||
|
||||
::google::protobuf::Metadata GetMetadata() const final;
|
||||
|
||||
// nested types ----------------------------------------------------
|
||||
|
||||
// accessors -------------------------------------------------------
|
||||
|
||||
// .tensorflow.TensorShapeProto shape = 2;
|
||||
bool has_shape() const;
|
||||
void clear_shape();
|
||||
static const int kShapeFieldNumber = 2;
|
||||
private:
|
||||
const ::tensorflow::TensorShapeProto& _internal_shape() const;
|
||||
public:
|
||||
const ::tensorflow::TensorShapeProto& shape() const;
|
||||
::tensorflow::TensorShapeProto* release_shape();
|
||||
::tensorflow::TensorShapeProto* mutable_shape();
|
||||
void set_allocated_shape(::tensorflow::TensorShapeProto* shape);
|
||||
void unsafe_arena_set_allocated_shape(
|
||||
::tensorflow::TensorShapeProto* shape);
|
||||
::tensorflow::TensorShapeProto* unsafe_arena_release_shape();
|
||||
|
||||
// .tensorflow.DataType type = 1;
|
||||
void clear_type();
|
||||
static const int kTypeFieldNumber = 1;
|
||||
::tensorflow::DataType type() const;
|
||||
void set_type(::tensorflow::DataType value);
|
||||
|
||||
// @@protoc_insertion_point(class_scope:tensorflow.tf2xla.TensorMetadata)
|
||||
private:
|
||||
|
||||
::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
|
||||
template <typename T> friend class ::google::protobuf::Arena::InternalHelper;
|
||||
typedef void InternalArenaConstructable_;
|
||||
typedef void DestructorSkippable_;
|
||||
::tensorflow::TensorShapeProto* shape_;
|
||||
int type_;
|
||||
mutable ::google::protobuf::internal::CachedSize _cached_size_;
|
||||
friend struct ::protobuf_tensorflow_2fcompiler_2ftf2xla_2fhost_5fcompute_5fmetadata_2eproto::TableStruct;
|
||||
};
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
class HostTransferMetadata : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:tensorflow.tf2xla.HostTransferMetadata) */ {
|
||||
public:
|
||||
HostTransferMetadata();
|
||||
virtual ~HostTransferMetadata();
|
||||
|
||||
HostTransferMetadata(const HostTransferMetadata& from);
|
||||
|
||||
inline HostTransferMetadata& operator=(const HostTransferMetadata& from) {
|
||||
CopyFrom(from);
|
||||
return *this;
|
||||
}
|
||||
#if LANG_CXX11
|
||||
HostTransferMetadata(HostTransferMetadata&& from) noexcept
|
||||
: HostTransferMetadata() {
|
||||
*this = ::std::move(from);
|
||||
}
|
||||
|
||||
inline HostTransferMetadata& operator=(HostTransferMetadata&& from) noexcept {
|
||||
if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) {
|
||||
if (this != &from) InternalSwap(&from);
|
||||
} else {
|
||||
CopyFrom(from);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
inline ::google::protobuf::Arena* GetArena() const final {
|
||||
return GetArenaNoVirtual();
|
||||
}
|
||||
inline void* GetMaybeArenaPointer() const final {
|
||||
return MaybeArenaPtr();
|
||||
}
|
||||
static const ::google::protobuf::Descriptor* descriptor();
|
||||
static const HostTransferMetadata& default_instance();
|
||||
|
||||
static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY
|
||||
static inline const HostTransferMetadata* internal_default_instance() {
|
||||
return reinterpret_cast<const HostTransferMetadata*>(
|
||||
&_HostTransferMetadata_default_instance_);
|
||||
}
|
||||
static constexpr int kIndexInFileMessages =
|
||||
1;
|
||||
|
||||
void UnsafeArenaSwap(HostTransferMetadata* other);
|
||||
void Swap(HostTransferMetadata* other);
|
||||
friend void swap(HostTransferMetadata& a, HostTransferMetadata& b) {
|
||||
a.Swap(&b);
|
||||
}
|
||||
|
||||
// implements Message ----------------------------------------------
|
||||
|
||||
inline HostTransferMetadata* New() const final {
|
||||
return CreateMaybeMessage<HostTransferMetadata>(NULL);
|
||||
}
|
||||
|
||||
HostTransferMetadata* New(::google::protobuf::Arena* arena) const final {
|
||||
return CreateMaybeMessage<HostTransferMetadata>(arena);
|
||||
}
|
||||
void CopyFrom(const ::google::protobuf::Message& from) final;
|
||||
void MergeFrom(const ::google::protobuf::Message& from) final;
|
||||
void CopyFrom(const HostTransferMetadata& from);
|
||||
void MergeFrom(const HostTransferMetadata& from);
|
||||
void Clear() final;
|
||||
bool IsInitialized() const final;
|
||||
|
||||
size_t ByteSizeLong() const final;
|
||||
bool MergePartialFromCodedStream(
|
||||
::google::protobuf::io::CodedInputStream* input) final;
|
||||
void SerializeWithCachedSizes(
|
||||
::google::protobuf::io::CodedOutputStream* output) const final;
|
||||
::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray(
|
||||
bool deterministic, ::google::protobuf::uint8* target) const final;
|
||||
int GetCachedSize() const final { return _cached_size_.Get(); }
|
||||
|
||||
private:
|
||||
void SharedCtor();
|
||||
void SharedDtor();
|
||||
void SetCachedSize(int size) const final;
|
||||
void InternalSwap(HostTransferMetadata* other);
|
||||
protected:
|
||||
explicit HostTransferMetadata(::google::protobuf::Arena* arena);
|
||||
private:
|
||||
static void ArenaDtor(void* object);
|
||||
inline void RegisterArenaDtor(::google::protobuf::Arena* arena);
|
||||
private:
|
||||
inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
|
||||
return _internal_metadata_.arena();
|
||||
}
|
||||
inline void* MaybeArenaPtr() const {
|
||||
return _internal_metadata_.raw_arena_ptr();
|
||||
}
|
||||
public:
|
||||
|
||||
::google::protobuf::Metadata GetMetadata() const final;
|
||||
|
||||
// nested types ----------------------------------------------------
|
||||
|
||||
// accessors -------------------------------------------------------
|
||||
|
||||
// repeated .tensorflow.tf2xla.TensorMetadata metadata = 2;
|
||||
int metadata_size() const;
|
||||
void clear_metadata();
|
||||
static const int kMetadataFieldNumber = 2;
|
||||
::tensorflow::tf2xla::TensorMetadata* mutable_metadata(int index);
|
||||
::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::TensorMetadata >*
|
||||
mutable_metadata();
|
||||
const ::tensorflow::tf2xla::TensorMetadata& metadata(int index) const;
|
||||
::tensorflow::tf2xla::TensorMetadata* add_metadata();
|
||||
const ::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::TensorMetadata >&
|
||||
metadata() const;
|
||||
|
||||
// string key = 1;
|
||||
void clear_key();
|
||||
static const int kKeyFieldNumber = 1;
|
||||
const ::std::string& key() const;
|
||||
void set_key(const ::std::string& value);
|
||||
#if LANG_CXX11
|
||||
void set_key(::std::string&& value);
|
||||
#endif
|
||||
void set_key(const char* value);
|
||||
void set_key(const char* value, size_t size);
|
||||
::std::string* mutable_key();
|
||||
::std::string* release_key();
|
||||
void set_allocated_key(::std::string* key);
|
||||
PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for"
|
||||
" string fields are deprecated and will be removed in a"
|
||||
" future release.")
|
||||
::std::string* unsafe_arena_release_key();
|
||||
PROTOBUF_RUNTIME_DEPRECATED("The unsafe_arena_ accessors for"
|
||||
" string fields are deprecated and will be removed in a"
|
||||
" future release.")
|
||||
void unsafe_arena_set_allocated_key(
|
||||
::std::string* key);
|
||||
|
||||
// @@protoc_insertion_point(class_scope:tensorflow.tf2xla.HostTransferMetadata)
|
||||
private:
|
||||
|
||||
::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
|
||||
template <typename T> friend class ::google::protobuf::Arena::InternalHelper;
|
||||
typedef void InternalArenaConstructable_;
|
||||
typedef void DestructorSkippable_;
|
||||
::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::TensorMetadata > metadata_;
|
||||
::google::protobuf::internal::ArenaStringPtr key_;
|
||||
mutable ::google::protobuf::internal::CachedSize _cached_size_;
|
||||
friend struct ::protobuf_tensorflow_2fcompiler_2ftf2xla_2fhost_5fcompute_5fmetadata_2eproto::TableStruct;
|
||||
};
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
class HostComputeMetadata : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:tensorflow.tf2xla.HostComputeMetadata) */ {
|
||||
public:
|
||||
HostComputeMetadata();
|
||||
virtual ~HostComputeMetadata();
|
||||
|
||||
HostComputeMetadata(const HostComputeMetadata& from);
|
||||
|
||||
inline HostComputeMetadata& operator=(const HostComputeMetadata& from) {
|
||||
CopyFrom(from);
|
||||
return *this;
|
||||
}
|
||||
#if LANG_CXX11
|
||||
HostComputeMetadata(HostComputeMetadata&& from) noexcept
|
||||
: HostComputeMetadata() {
|
||||
*this = ::std::move(from);
|
||||
}
|
||||
|
||||
inline HostComputeMetadata& operator=(HostComputeMetadata&& from) noexcept {
|
||||
if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) {
|
||||
if (this != &from) InternalSwap(&from);
|
||||
} else {
|
||||
CopyFrom(from);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
inline ::google::protobuf::Arena* GetArena() const final {
|
||||
return GetArenaNoVirtual();
|
||||
}
|
||||
inline void* GetMaybeArenaPointer() const final {
|
||||
return MaybeArenaPtr();
|
||||
}
|
||||
static const ::google::protobuf::Descriptor* descriptor();
|
||||
static const HostComputeMetadata& default_instance();
|
||||
|
||||
static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY
|
||||
static inline const HostComputeMetadata* internal_default_instance() {
|
||||
return reinterpret_cast<const HostComputeMetadata*>(
|
||||
&_HostComputeMetadata_default_instance_);
|
||||
}
|
||||
static constexpr int kIndexInFileMessages =
|
||||
2;
|
||||
|
||||
void UnsafeArenaSwap(HostComputeMetadata* other);
|
||||
void Swap(HostComputeMetadata* other);
|
||||
friend void swap(HostComputeMetadata& a, HostComputeMetadata& b) {
|
||||
a.Swap(&b);
|
||||
}
|
||||
|
||||
// implements Message ----------------------------------------------
|
||||
|
||||
inline HostComputeMetadata* New() const final {
|
||||
return CreateMaybeMessage<HostComputeMetadata>(NULL);
|
||||
}
|
||||
|
||||
HostComputeMetadata* New(::google::protobuf::Arena* arena) const final {
|
||||
return CreateMaybeMessage<HostComputeMetadata>(arena);
|
||||
}
|
||||
void CopyFrom(const ::google::protobuf::Message& from) final;
|
||||
void MergeFrom(const ::google::protobuf::Message& from) final;
|
||||
void CopyFrom(const HostComputeMetadata& from);
|
||||
void MergeFrom(const HostComputeMetadata& from);
|
||||
void Clear() final;
|
||||
bool IsInitialized() const final;
|
||||
|
||||
size_t ByteSizeLong() const final;
|
||||
bool MergePartialFromCodedStream(
|
||||
::google::protobuf::io::CodedInputStream* input) final;
|
||||
void SerializeWithCachedSizes(
|
||||
::google::protobuf::io::CodedOutputStream* output) const final;
|
||||
::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray(
|
||||
bool deterministic, ::google::protobuf::uint8* target) const final;
|
||||
int GetCachedSize() const final { return _cached_size_.Get(); }
|
||||
|
||||
private:
|
||||
void SharedCtor();
|
||||
void SharedDtor();
|
||||
void SetCachedSize(int size) const final;
|
||||
void InternalSwap(HostComputeMetadata* other);
|
||||
protected:
|
||||
explicit HostComputeMetadata(::google::protobuf::Arena* arena);
|
||||
private:
|
||||
static void ArenaDtor(void* object);
|
||||
inline void RegisterArenaDtor(::google::protobuf::Arena* arena);
|
||||
private:
|
||||
inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
|
||||
return _internal_metadata_.arena();
|
||||
}
|
||||
inline void* MaybeArenaPtr() const {
|
||||
return _internal_metadata_.raw_arena_ptr();
|
||||
}
|
||||
public:
|
||||
|
||||
::google::protobuf::Metadata GetMetadata() const final;
|
||||
|
||||
// nested types ----------------------------------------------------
|
||||
|
||||
// accessors -------------------------------------------------------
|
||||
|
||||
// repeated .tensorflow.tf2xla.HostTransferMetadata device_to_host = 1;
|
||||
int device_to_host_size() const;
|
||||
void clear_device_to_host();
|
||||
static const int kDeviceToHostFieldNumber = 1;
|
||||
::tensorflow::tf2xla::HostTransferMetadata* mutable_device_to_host(int index);
|
||||
::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::HostTransferMetadata >*
|
||||
mutable_device_to_host();
|
||||
const ::tensorflow::tf2xla::HostTransferMetadata& device_to_host(int index) const;
|
||||
::tensorflow::tf2xla::HostTransferMetadata* add_device_to_host();
|
||||
const ::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::HostTransferMetadata >&
|
||||
device_to_host() const;
|
||||
|
||||
// repeated .tensorflow.tf2xla.HostTransferMetadata host_to_device = 2;
|
||||
int host_to_device_size() const;
|
||||
void clear_host_to_device();
|
||||
static const int kHostToDeviceFieldNumber = 2;
|
||||
::tensorflow::tf2xla::HostTransferMetadata* mutable_host_to_device(int index);
|
||||
::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::HostTransferMetadata >*
|
||||
mutable_host_to_device();
|
||||
const ::tensorflow::tf2xla::HostTransferMetadata& host_to_device(int index) const;
|
||||
::tensorflow::tf2xla::HostTransferMetadata* add_host_to_device();
|
||||
const ::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::HostTransferMetadata >&
|
||||
host_to_device() const;
|
||||
|
||||
// @@protoc_insertion_point(class_scope:tensorflow.tf2xla.HostComputeMetadata)
|
||||
private:
|
||||
|
||||
::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
|
||||
template <typename T> friend class ::google::protobuf::Arena::InternalHelper;
|
||||
typedef void InternalArenaConstructable_;
|
||||
typedef void DestructorSkippable_;
|
||||
::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::HostTransferMetadata > device_to_host_;
|
||||
::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::HostTransferMetadata > host_to_device_;
|
||||
mutable ::google::protobuf::internal::CachedSize _cached_size_;
|
||||
friend struct ::protobuf_tensorflow_2fcompiler_2ftf2xla_2fhost_5fcompute_5fmetadata_2eproto::TableStruct;
|
||||
};
|
||||
// ===================================================================
|
||||
|
||||
|
||||
// ===================================================================
|
||||
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
||||
#endif // __GNUC__
|
||||
// TensorMetadata
|
||||
|
||||
// .tensorflow.DataType type = 1;
|
||||
inline void TensorMetadata::clear_type() {
|
||||
type_ = 0;
|
||||
}
|
||||
inline ::tensorflow::DataType TensorMetadata::type() const {
|
||||
// @@protoc_insertion_point(field_get:tensorflow.tf2xla.TensorMetadata.type)
|
||||
return static_cast< ::tensorflow::DataType >(type_);
|
||||
}
|
||||
inline void TensorMetadata::set_type(::tensorflow::DataType value) {
|
||||
|
||||
type_ = value;
|
||||
// @@protoc_insertion_point(field_set:tensorflow.tf2xla.TensorMetadata.type)
|
||||
}
|
||||
|
||||
// .tensorflow.TensorShapeProto shape = 2;
|
||||
inline bool TensorMetadata::has_shape() const {
|
||||
return this != internal_default_instance() && shape_ != NULL;
|
||||
}
|
||||
inline const ::tensorflow::TensorShapeProto& TensorMetadata::_internal_shape() const {
|
||||
return *shape_;
|
||||
}
|
||||
inline const ::tensorflow::TensorShapeProto& TensorMetadata::shape() const {
|
||||
const ::tensorflow::TensorShapeProto* p = shape_;
|
||||
// @@protoc_insertion_point(field_get:tensorflow.tf2xla.TensorMetadata.shape)
|
||||
return p != NULL ? *p : *reinterpret_cast<const ::tensorflow::TensorShapeProto*>(
|
||||
&::tensorflow::_TensorShapeProto_default_instance_);
|
||||
}
|
||||
inline ::tensorflow::TensorShapeProto* TensorMetadata::release_shape() {
|
||||
// @@protoc_insertion_point(field_release:tensorflow.tf2xla.TensorMetadata.shape)
|
||||
|
||||
::tensorflow::TensorShapeProto* temp = shape_;
|
||||
if (GetArenaNoVirtual() != NULL) {
|
||||
temp = ::google::protobuf::internal::DuplicateIfNonNull(temp);
|
||||
}
|
||||
shape_ = NULL;
|
||||
return temp;
|
||||
}
|
||||
inline ::tensorflow::TensorShapeProto* TensorMetadata::unsafe_arena_release_shape() {
|
||||
// @@protoc_insertion_point(field_unsafe_arena_release:tensorflow.tf2xla.TensorMetadata.shape)
|
||||
|
||||
::tensorflow::TensorShapeProto* temp = shape_;
|
||||
shape_ = NULL;
|
||||
return temp;
|
||||
}
|
||||
inline ::tensorflow::TensorShapeProto* TensorMetadata::mutable_shape() {
|
||||
|
||||
if (shape_ == NULL) {
|
||||
auto* p = CreateMaybeMessage<::tensorflow::TensorShapeProto>(GetArenaNoVirtual());
|
||||
shape_ = p;
|
||||
}
|
||||
// @@protoc_insertion_point(field_mutable:tensorflow.tf2xla.TensorMetadata.shape)
|
||||
return shape_;
|
||||
}
|
||||
inline void TensorMetadata::set_allocated_shape(::tensorflow::TensorShapeProto* shape) {
|
||||
::google::protobuf::Arena* message_arena = GetArenaNoVirtual();
|
||||
if (message_arena == NULL) {
|
||||
delete reinterpret_cast< ::google::protobuf::MessageLite*>(shape_);
|
||||
}
|
||||
if (shape) {
|
||||
::google::protobuf::Arena* submessage_arena =
|
||||
reinterpret_cast<::google::protobuf::MessageLite*>(shape)->GetArena();
|
||||
if (message_arena != submessage_arena) {
|
||||
shape = ::google::protobuf::internal::GetOwnedMessage(
|
||||
message_arena, shape, submessage_arena);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
shape_ = shape;
|
||||
// @@protoc_insertion_point(field_set_allocated:tensorflow.tf2xla.TensorMetadata.shape)
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
// HostTransferMetadata
|
||||
|
||||
// string key = 1;
|
||||
inline void HostTransferMetadata::clear_key() {
|
||||
key_.ClearToEmpty(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual());
|
||||
}
|
||||
inline const ::std::string& HostTransferMetadata::key() const {
|
||||
// @@protoc_insertion_point(field_get:tensorflow.tf2xla.HostTransferMetadata.key)
|
||||
return key_.Get();
|
||||
}
|
||||
inline void HostTransferMetadata::set_key(const ::std::string& value) {
|
||||
|
||||
key_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value, GetArenaNoVirtual());
|
||||
// @@protoc_insertion_point(field_set:tensorflow.tf2xla.HostTransferMetadata.key)
|
||||
}
|
||||
#if LANG_CXX11
|
||||
inline void HostTransferMetadata::set_key(::std::string&& value) {
|
||||
|
||||
key_.Set(
|
||||
&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::move(value), GetArenaNoVirtual());
|
||||
// @@protoc_insertion_point(field_set_rvalue:tensorflow.tf2xla.HostTransferMetadata.key)
|
||||
}
|
||||
#endif
|
||||
inline void HostTransferMetadata::set_key(const char* value) {
|
||||
GOOGLE_DCHECK(value != NULL);
|
||||
|
||||
key_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value),
|
||||
GetArenaNoVirtual());
|
||||
// @@protoc_insertion_point(field_set_char:tensorflow.tf2xla.HostTransferMetadata.key)
|
||||
}
|
||||
inline void HostTransferMetadata::set_key(const char* value,
|
||||
size_t size) {
|
||||
|
||||
key_.Set(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(
|
||||
reinterpret_cast<const char*>(value), size), GetArenaNoVirtual());
|
||||
// @@protoc_insertion_point(field_set_pointer:tensorflow.tf2xla.HostTransferMetadata.key)
|
||||
}
|
||||
inline ::std::string* HostTransferMetadata::mutable_key() {
|
||||
|
||||
// @@protoc_insertion_point(field_mutable:tensorflow.tf2xla.HostTransferMetadata.key)
|
||||
return key_.Mutable(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual());
|
||||
}
|
||||
inline ::std::string* HostTransferMetadata::release_key() {
|
||||
// @@protoc_insertion_point(field_release:tensorflow.tf2xla.HostTransferMetadata.key)
|
||||
|
||||
return key_.Release(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), GetArenaNoVirtual());
|
||||
}
|
||||
inline void HostTransferMetadata::set_allocated_key(::std::string* key) {
|
||||
if (key != NULL) {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
key_.SetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), key,
|
||||
GetArenaNoVirtual());
|
||||
// @@protoc_insertion_point(field_set_allocated:tensorflow.tf2xla.HostTransferMetadata.key)
|
||||
}
|
||||
inline ::std::string* HostTransferMetadata::unsafe_arena_release_key() {
|
||||
// @@protoc_insertion_point(field_unsafe_arena_release:tensorflow.tf2xla.HostTransferMetadata.key)
|
||||
GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);
|
||||
|
||||
return key_.UnsafeArenaRelease(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
|
||||
GetArenaNoVirtual());
|
||||
}
|
||||
inline void HostTransferMetadata::unsafe_arena_set_allocated_key(
|
||||
::std::string* key) {
|
||||
GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);
|
||||
if (key != NULL) {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
key_.UnsafeArenaSetAllocated(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
|
||||
key, GetArenaNoVirtual());
|
||||
// @@protoc_insertion_point(field_unsafe_arena_set_allocated:tensorflow.tf2xla.HostTransferMetadata.key)
|
||||
}
|
||||
|
||||
// repeated .tensorflow.tf2xla.TensorMetadata metadata = 2;
|
||||
inline int HostTransferMetadata::metadata_size() const {
|
||||
return metadata_.size();
|
||||
}
|
||||
inline void HostTransferMetadata::clear_metadata() {
|
||||
metadata_.Clear();
|
||||
}
|
||||
inline ::tensorflow::tf2xla::TensorMetadata* HostTransferMetadata::mutable_metadata(int index) {
|
||||
// @@protoc_insertion_point(field_mutable:tensorflow.tf2xla.HostTransferMetadata.metadata)
|
||||
return metadata_.Mutable(index);
|
||||
}
|
||||
inline ::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::TensorMetadata >*
|
||||
HostTransferMetadata::mutable_metadata() {
|
||||
// @@protoc_insertion_point(field_mutable_list:tensorflow.tf2xla.HostTransferMetadata.metadata)
|
||||
return &metadata_;
|
||||
}
|
||||
inline const ::tensorflow::tf2xla::TensorMetadata& HostTransferMetadata::metadata(int index) const {
|
||||
// @@protoc_insertion_point(field_get:tensorflow.tf2xla.HostTransferMetadata.metadata)
|
||||
return metadata_.Get(index);
|
||||
}
|
||||
inline ::tensorflow::tf2xla::TensorMetadata* HostTransferMetadata::add_metadata() {
|
||||
// @@protoc_insertion_point(field_add:tensorflow.tf2xla.HostTransferMetadata.metadata)
|
||||
return metadata_.Add();
|
||||
}
|
||||
inline const ::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::TensorMetadata >&
|
||||
HostTransferMetadata::metadata() const {
|
||||
// @@protoc_insertion_point(field_list:tensorflow.tf2xla.HostTransferMetadata.metadata)
|
||||
return metadata_;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
// HostComputeMetadata
|
||||
|
||||
// repeated .tensorflow.tf2xla.HostTransferMetadata device_to_host = 1;
|
||||
inline int HostComputeMetadata::device_to_host_size() const {
|
||||
return device_to_host_.size();
|
||||
}
|
||||
inline void HostComputeMetadata::clear_device_to_host() {
|
||||
device_to_host_.Clear();
|
||||
}
|
||||
inline ::tensorflow::tf2xla::HostTransferMetadata* HostComputeMetadata::mutable_device_to_host(int index) {
|
||||
// @@protoc_insertion_point(field_mutable:tensorflow.tf2xla.HostComputeMetadata.device_to_host)
|
||||
return device_to_host_.Mutable(index);
|
||||
}
|
||||
inline ::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::HostTransferMetadata >*
|
||||
HostComputeMetadata::mutable_device_to_host() {
|
||||
// @@protoc_insertion_point(field_mutable_list:tensorflow.tf2xla.HostComputeMetadata.device_to_host)
|
||||
return &device_to_host_;
|
||||
}
|
||||
inline const ::tensorflow::tf2xla::HostTransferMetadata& HostComputeMetadata::device_to_host(int index) const {
|
||||
// @@protoc_insertion_point(field_get:tensorflow.tf2xla.HostComputeMetadata.device_to_host)
|
||||
return device_to_host_.Get(index);
|
||||
}
|
||||
inline ::tensorflow::tf2xla::HostTransferMetadata* HostComputeMetadata::add_device_to_host() {
|
||||
// @@protoc_insertion_point(field_add:tensorflow.tf2xla.HostComputeMetadata.device_to_host)
|
||||
return device_to_host_.Add();
|
||||
}
|
||||
inline const ::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::HostTransferMetadata >&
|
||||
HostComputeMetadata::device_to_host() const {
|
||||
// @@protoc_insertion_point(field_list:tensorflow.tf2xla.HostComputeMetadata.device_to_host)
|
||||
return device_to_host_;
|
||||
}
|
||||
|
||||
// repeated .tensorflow.tf2xla.HostTransferMetadata host_to_device = 2;
|
||||
inline int HostComputeMetadata::host_to_device_size() const {
|
||||
return host_to_device_.size();
|
||||
}
|
||||
inline void HostComputeMetadata::clear_host_to_device() {
|
||||
host_to_device_.Clear();
|
||||
}
|
||||
inline ::tensorflow::tf2xla::HostTransferMetadata* HostComputeMetadata::mutable_host_to_device(int index) {
|
||||
// @@protoc_insertion_point(field_mutable:tensorflow.tf2xla.HostComputeMetadata.host_to_device)
|
||||
return host_to_device_.Mutable(index);
|
||||
}
|
||||
inline ::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::HostTransferMetadata >*
|
||||
HostComputeMetadata::mutable_host_to_device() {
|
||||
// @@protoc_insertion_point(field_mutable_list:tensorflow.tf2xla.HostComputeMetadata.host_to_device)
|
||||
return &host_to_device_;
|
||||
}
|
||||
inline const ::tensorflow::tf2xla::HostTransferMetadata& HostComputeMetadata::host_to_device(int index) const {
|
||||
// @@protoc_insertion_point(field_get:tensorflow.tf2xla.HostComputeMetadata.host_to_device)
|
||||
return host_to_device_.Get(index);
|
||||
}
|
||||
inline ::tensorflow::tf2xla::HostTransferMetadata* HostComputeMetadata::add_host_to_device() {
|
||||
// @@protoc_insertion_point(field_add:tensorflow.tf2xla.HostComputeMetadata.host_to_device)
|
||||
return host_to_device_.Add();
|
||||
}
|
||||
inline const ::google::protobuf::RepeatedPtrField< ::tensorflow::tf2xla::HostTransferMetadata >&
|
||||
HostComputeMetadata::host_to_device() const {
|
||||
// @@protoc_insertion_point(field_list:tensorflow.tf2xla.HostComputeMetadata.host_to_device)
|
||||
return host_to_device_;
|
||||
}
|
||||
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic pop
|
||||
#endif // __GNUC__
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
|
||||
// @@protoc_insertion_point(namespace_scope)
|
||||
|
||||
} // namespace tf2xla
|
||||
} // namespace tensorflow
|
||||
|
||||
// @@protoc_insertion_point(global_scope)
|
||||
|
||||
#endif // PROTOBUF_INCLUDED_tensorflow_2fcompiler_2ftf2xla_2fhost_5fcompute_5fmetadata_2eproto
|
||||
@@ -0,0 +1,69 @@
|
||||
/* 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_COMPILER_TF2XLA_KERNELS_CONV_OP_HELPERS_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_KERNELS_CONV_OP_HELPERS_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/statusor.h"
|
||||
#include "tensorflow/core/framework/op_kernel.h"
|
||||
#include "tensorflow/core/framework/types.h"
|
||||
#include "tensorflow/core/util/padding.h"
|
||||
#include "tensorflow/core/util/tensor_format.h"
|
||||
|
||||
// This header exposes utilities for translating TensorFlow convolution ops into
|
||||
// XLA ops.
|
||||
//
|
||||
// conv_ops.cc contains lowerings for many of these TF convolution ops (e.g.
|
||||
// Conv2D, Conv3DBackpropFilterV2), but you might want to use the utilities in
|
||||
// this header to implement a new and exciting convolution op, for example a
|
||||
// fused TensorFlow op that contains a convolution and other things.
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// ConvOpAttrs contains all of the metadata necessary to specify a TF or XLA
|
||||
// convolution.
|
||||
struct ConvOpAttrs {
|
||||
// Constructs a ConvOpAttrs, reading most of the attributes from `ctx`.
|
||||
static xla::StatusOr<ConvOpAttrs> Create(int num_spatial_dims, bool depthwise,
|
||||
OpKernelConstruction* ctx);
|
||||
|
||||
bool depthwise;
|
||||
int num_spatial_dims;
|
||||
std::vector<int32> dilations;
|
||||
std::vector<int32> strides;
|
||||
Padding padding;
|
||||
TensorFormat data_format;
|
||||
};
|
||||
|
||||
// Creates a new XLA forward or backward convolution with the given inputs and
|
||||
// attributes.
|
||||
xla::StatusOr<xla::XlaOp> MakeXlaForwardConvOp(StringPiece type_string,
|
||||
xla::XlaOp conv_input,
|
||||
xla::XlaOp filter,
|
||||
const ConvOpAttrs& attrs);
|
||||
xla::StatusOr<xla::XlaOp> MakeXlaBackpropInputConvOp(
|
||||
StringPiece type_string, const xla::Shape& input_shape, xla::XlaOp filter,
|
||||
xla::XlaOp out_backprop, const ConvOpAttrs& attrs);
|
||||
xla::StatusOr<xla::XlaOp> MakeXlaBackpropFilterConvOp(
|
||||
StringPiece type_string, xla::XlaOp activations,
|
||||
const xla::Shape& filter_shape, xla::XlaOp gradients,
|
||||
const ConvOpAttrs& attrs);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_KERNELS_CONV_OP_HELPERS_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.
|
||||
==============================================================================*/
|
||||
|
||||
// XLA-specific base classes for Unary and Binary Ops.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_TF2XLA_KERNELS_CWISE_OPS_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_KERNELS_CWISE_OPS_H_
|
||||
|
||||
#include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
|
||||
#include "tensorflow/compiler/xla/client/client_library.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/core/framework/op_kernel.h"
|
||||
#include "tensorflow/core/util/bcast.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Coefficient-wise binary operations. Each binary Op expects two
|
||||
// inputs that can be broadcast to the same shape. The base class
|
||||
// contains pure virtual methods to override: description is a textual
|
||||
// description of the operation; and Computation adds the
|
||||
// implementation of the operation to a xla::XlaBuilder. For most
|
||||
// arithmetic Ops XLA handles the broadcasting automatically given the input
|
||||
// tensors.
|
||||
class XlaBinaryOp : public XlaOpKernel {
|
||||
public:
|
||||
explicit XlaBinaryOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {
|
||||
const DataType lhs = BaseType(input_type(0));
|
||||
const DataType rhs = BaseType(input_type(1));
|
||||
OP_REQUIRES(ctx, lhs == rhs,
|
||||
errors::InvalidArgument("Input types of binary op must match"));
|
||||
}
|
||||
~XlaBinaryOp() override {}
|
||||
|
||||
// Implement the (tensor,tensor)->tensor lambda that should be
|
||||
// applied to the inputs. The desired computation should be added to
|
||||
// 'tc->builder()' and '(lhs,rhs)' are the function's inputs and
|
||||
// (lhs_shape,rhs_shape) are their respective
|
||||
// shapes. 'broadcast_helper' contains metadata about the shapes of
|
||||
// the inputs and the dimensions that need to be broadcast, which
|
||||
// may be useful for Ops that can't use standard XLA automatic
|
||||
// broadcasting. 'extend_dimension' is non-empty if lhs and rhs have
|
||||
// different ranks, and indicates which dimensions of the
|
||||
// higher-rank input should be matched when broadcasting the
|
||||
// lower-rank input. See comment below and the documentation on broadcasting
|
||||
// in the XLA documentation.
|
||||
virtual xla::XlaOp Computation(
|
||||
XlaOpKernelContext* ctx, const xla::XlaOp& lhs,
|
||||
const absl::Span<const int64>& lhs_shape, const xla::XlaOp& rhs,
|
||||
const absl::Span<const int64>& rhs_shape, const BCast& broadcast_helper,
|
||||
const std::vector<int64>& extend_dimensions) = 0;
|
||||
|
||||
void Compile(XlaOpKernelContext* ctx) override;
|
||||
|
||||
// Helper function that performs the broadcasting described by
|
||||
// 'broadcast_helper', yielding arguments 'lhs' and 'rhs' that have the same
|
||||
// shape.
|
||||
static std::pair<xla::XlaOp, xla::XlaOp> Broadcast(
|
||||
xla::XlaBuilder* builder, const xla::XlaOp& lhs, const xla::XlaOp& rhs,
|
||||
const BCast& broadcast_helper);
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_KERNELS_CWISE_OPS_H_
|
||||
@@ -0,0 +1,44 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// Helper methods for XLA Gather Ops.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_TF2XLA_KERNELS_GATHER_OP_HELPERS_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_KERNELS_GATHER_OP_HELPERS_H_
|
||||
|
||||
#include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
|
||||
#include "tensorflow/compiler/xla/client/client_library.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/core/framework/op_kernel.h"
|
||||
#include "tensorflow/core/util/bcast.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Adds to builder an XLA computation that performs a gather on input (of
|
||||
// shape input_shape) keyed on indices (of shape indices_shape).
|
||||
//
|
||||
// index_type must be must be DT_INT32 or DT_INT64.
|
||||
// If `indices_are_nd` is true, the last dimension of `indices` are treated as
|
||||
// a multidimensional index values. Otherwise, `indices` is treated as a tensor
|
||||
// of scalar indices.
|
||||
Status XlaGather(const xla::XlaOp& input, const TensorShape& input_shape,
|
||||
const xla::XlaOp& indices, const TensorShape& indices_shape,
|
||||
int64 axis, bool indices_are_nd, DataType dtype,
|
||||
DataType index_type, xla::XlaBuilder* builder,
|
||||
xla::XlaOp* gather_output);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_KERNELS_GATHER_OP_HELPERS_H_
|
||||
@@ -0,0 +1,61 @@
|
||||
/* 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_COMPILER_TF2XLA_KERNELS_IF_OP_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_KERNELS_IF_OP_H_
|
||||
|
||||
#include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
|
||||
#include "tensorflow/core/framework/attr_value.pb.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// This TensorFlow op provides a functional conditional primitive.
|
||||
//
|
||||
// The outputs of the then/else branches must agree on the number, types, and
|
||||
// shapes of the Tensors carried around the two bodies.
|
||||
//
|
||||
// Computations in then/else bodies may read from and write to resource
|
||||
// variables.
|
||||
// Resource variables may be passed as arguments to the then/else function's
|
||||
// bodies. The XlaCompiler converts resource variable arguments
|
||||
// into parameters to the XLA computation and moves them to the end of the
|
||||
// parameter list, and by using the `return_updated_values_for_all_variables`
|
||||
// we ensure that all variables that appear in the input also appear at the
|
||||
// end of the then/else bodies output. This ensures the then/else bodies output
|
||||
// signatures match.
|
||||
//
|
||||
// It is the user's responsibility to ensure that each non-variable _Arg matches
|
||||
// the corresponding _Retval.
|
||||
class XlaIfOp : public XlaOpKernel {
|
||||
public:
|
||||
explicit XlaIfOp(OpKernelConstruction* ctx);
|
||||
|
||||
void Compile(XlaOpKernelContext* ctx) override;
|
||||
|
||||
private:
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(XlaIfOp);
|
||||
|
||||
NameAttrList then_branch_;
|
||||
NameAttrList else_branch_;
|
||||
DataType cond_type_;
|
||||
DataTypeVector input_types_;
|
||||
DataTypeVector output_types_;
|
||||
bool has_token_input_output_;
|
||||
std::vector<string> token_input_nodes_;
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_KERNELS_IF_OP_H_
|
||||
@@ -0,0 +1,42 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// Declarations of the ArgMax/ArgMin ops using a pure XLA implementation.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_TF2XLA_KERNELS_INDEX_OPS_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_KERNELS_INDEX_OPS_H_
|
||||
|
||||
#include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
|
||||
#include "tensorflow/core/framework/op_kernel.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
class XlaArgMinMaxOp : public XlaOpKernel {
|
||||
public:
|
||||
explicit XlaArgMinMaxOp(OpKernelConstruction* ctx, bool is_min);
|
||||
void Compile(XlaOpKernelContext* ctx) override;
|
||||
|
||||
private:
|
||||
const bool is_min_; // Are we computing ArgMin (true) or ArgMax (false)?
|
||||
};
|
||||
|
||||
class XlaArgMaxOp : public XlaArgMinMaxOp {
|
||||
public:
|
||||
explicit XlaArgMaxOp(OpKernelConstruction* ctx);
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_KERNELS_INDEX_OPS_H_
|
||||
@@ -0,0 +1,72 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// XLA-specific base classes for Reduction Ops.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_TF2XLA_KERNELS_REDUCTION_OPS_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_KERNELS_REDUCTION_OPS_H_
|
||||
|
||||
#include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/core/framework/op_kernel.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Reduction operations. The base class contains pure virtual methods
|
||||
// to override: description is a textual description of the mapped
|
||||
// function; InitialValue constructs the base case for the reduction;
|
||||
// BuildReducer adds the implementation of the reduction lambda to a
|
||||
// xla::XlaBuilder and BuildFinalizer adds the
|
||||
// implementation of the finalizer lambda (if there is one) to a
|
||||
// xla::XlaBuilder.
|
||||
class XlaReductionOp : public XlaOpKernel {
|
||||
public:
|
||||
XlaReductionOp(OpKernelConstruction* ctx, DataType reduction_type);
|
||||
~XlaReductionOp() override {}
|
||||
|
||||
// Return the base case for the reduction.
|
||||
virtual xla::XlaOp InitialValue(xla::XlaBuilder* builder) = 0;
|
||||
|
||||
// Implement the (scalar,scalar)->scalar lambda that should be
|
||||
// applied to each pair of elements to be reduced. The desired
|
||||
// computation should be added to 'builder' and
|
||||
// '(scalar_lhs,scalar_rhs)' are the function's inputs.
|
||||
virtual void BuildReducer(xla::XlaBuilder* builder,
|
||||
const xla::XlaOp& scalar_lhs,
|
||||
const xla::XlaOp& scalar_rhs) = 0;
|
||||
|
||||
// Applies a transformation to the output of the reduction. The desired
|
||||
// computation should be added to 'builder'. Argument 'reduce_output' is the
|
||||
// output of the reduction. 'num_elements_reduced' is the number of elements
|
||||
// that contributed to the reduction. Returns the transformed reduction
|
||||
// output, Defaults to returning 'reduce_output' unchanged.
|
||||
virtual xla::XlaOp BuildFinalizer(xla::XlaBuilder* builder,
|
||||
const xla::XlaOp& reduce_output,
|
||||
int64 num_elements_reduced);
|
||||
|
||||
void Compile(XlaOpKernelContext* ctx) override;
|
||||
|
||||
private:
|
||||
// True if the number of dimensions should be maintained.
|
||||
bool keep_dims_;
|
||||
|
||||
protected:
|
||||
DataType reduction_type_;
|
||||
xla::PrimitiveType xla_reduction_type_;
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_KERNELS_REDUCTION_OPS_H_
|
||||
@@ -0,0 +1,34 @@
|
||||
/* 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_COMPILER_TF2XLA_KERNELS_SHAPE_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_KERNELS_SHAPE_UTIL_H_
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include "tensorflow/core/framework/tensor.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Converts a TensorShape to a constant Tensor.
|
||||
//
|
||||
// The input TensorShape input_shape is used to populate the elements of
|
||||
// shape_constant, which is modified in place.
|
||||
Status TensorShapeToConstant(const TensorShape& input_shape,
|
||||
Tensor* shape_constant);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_KERNELS_SHAPE_UTIL_H_
|
||||
@@ -0,0 +1,67 @@
|
||||
/* 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_COMPILER_TF2XLA_KERNELS_WHILE_OP_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_KERNELS_WHILE_OP_H_
|
||||
|
||||
#include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
|
||||
#include "tensorflow/core/framework/attr_value.pb.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// This TensorFlow op provides a functional iteration primitive.
|
||||
//
|
||||
// The inputs and outputs of the loop body must agree on the number, types, and
|
||||
// shapes of the Tensors carried around the loop body.
|
||||
//
|
||||
// Computations in while loops may read from and write to resource variables.
|
||||
// Resource variables may be passed as arguments to a function's body and
|
||||
// condition functions. The XlaCompiler converts resource variable arguments
|
||||
// into parameters to the XLA computation and moves them to the end of the
|
||||
// parameter list, and by using the `return_updated_values_for_all_variables`
|
||||
// we ensure that all variables that appear in the input also appear at the
|
||||
// end of the body's output. This ensures the loop body's input and output
|
||||
// signatures match.
|
||||
//
|
||||
// It is the user's responsibility to ensure that each non-variable _Arg matches
|
||||
// the corresponding _Retval.
|
||||
//
|
||||
// For example, suppose we have a loop body with arguments:
|
||||
// DT_INT32, DT_RESOURCE (pointing to a DT_BOOL var), DT_FLOAT
|
||||
// and return values
|
||||
// DT_INT32, DT_FLOAT
|
||||
// It is an error for the body to return DT_RESOURCE values.
|
||||
//
|
||||
// The body will be lowered into an XLA computation that takes and returns a
|
||||
// tuple with XLA type (I32, F32, PRED). Note the resource variable appears at
|
||||
// the end of both the loop body's input and output argument lists.
|
||||
class XlaWhileOp : public XlaOpKernel {
|
||||
public:
|
||||
explicit XlaWhileOp(OpKernelConstruction* ctx);
|
||||
|
||||
void Compile(XlaOpKernelContext* ctx) override;
|
||||
|
||||
private:
|
||||
NameAttrList cond_name_attr_;
|
||||
NameAttrList body_name_attr_;
|
||||
bool has_token_input_output_;
|
||||
std::vector<string> token_input_nodes_;
|
||||
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(XlaWhileOp);
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_KERNELS_WHILE_OP_H_
|
||||
@@ -0,0 +1,54 @@
|
||||
/* 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_COMPILER_TF2XLA_LIB_BATCH_DOT_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_LIB_BATCH_DOT_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Multiplies slices of two tensors in batches.
|
||||
|
||||
// Multiplies all slices of `Tensor` `x` and `y` (each slice can be
|
||||
// viewed as an element of a batch), and arranges the individual results
|
||||
// in a single output tensor of the same batch size. Each of the
|
||||
// individual slices can optionally be transposed before multiplication by
|
||||
// setting the `transpose_x` or `transpose_y` flag to `true`. Similarly, each
|
||||
// can be elementwise-complex-conjugated by setting the `conjugate_x` or
|
||||
// `conjugate_y` flag to `true`. To apply a Hermitian adjoint to `x`, set both
|
||||
// `transpose_x` and `conjugate_x` to `true`, and analogously for `y`.
|
||||
//
|
||||
// The input tensors `x` and `y` are 2-D or higher with shape `[..., r_x, c_x]`
|
||||
// and `[..., r_y, c_y]`.
|
||||
//
|
||||
// The output tensor is 2-D or higher with shape `[..., r_o, c_o]`, where:
|
||||
//
|
||||
// r_o = c_x if transpose_x else r_x
|
||||
// c_o = r_y if transpose_y else c_y
|
||||
//
|
||||
// It is computed as:
|
||||
//
|
||||
// output[..., :, :] = matrix(x[..., :, :]) * matrix(y[..., :, :])
|
||||
xla::XlaOp BatchDot(
|
||||
xla::XlaOp x, xla::XlaOp y, bool transpose_x = false,
|
||||
bool transpose_y = false, bool conjugate_x = false,
|
||||
bool conjugate_y = false,
|
||||
xla::PrecisionConfig::Precision precision = xla::PrecisionConfig::DEFAULT);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_LIB_BATCH_DOT_H_
|
||||
@@ -0,0 +1,39 @@
|
||||
/* 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_COMPILER_TF2XLA_LIB_CHOLESKY_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_LIB_CHOLESKY_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Computes the Cholesky decompositions of a batch of symmetric positive
|
||||
// definite matrices.
|
||||
// `a` must be a (batched) square matrix; i.e., it must have rank >= 2 with the
|
||||
// two minor dimensions equal.
|
||||
// The algorithm implements a blocked Cholesky decomposition; `block_size` is
|
||||
// the block size to use.
|
||||
// TODO(phawkins): check for negative values on the diagonal and return an
|
||||
// error, instead of silently yielding NaNs.
|
||||
// TODO(znado): handle the complex Hermitian case
|
||||
xla::XlaOp Cholesky(
|
||||
xla::XlaOp a, int64 block_size = 256,
|
||||
xla::PrecisionConfig::Precision precision = xla::PrecisionConfig::HIGHEST);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_LIB_CHOLESKY_H_
|
||||
@@ -0,0 +1,42 @@
|
||||
/* 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_COMPILER_TF2XLA_LIB_QR_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_LIB_QR_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Computes the QR decompositions of a batch of matrices. That is,
|
||||
// given a (batched) matrix a, computes an orthonormal matrix Q and an
|
||||
// upper-triangular matrix R such that a = QR.
|
||||
// `a` must be a (batched) matrix of size [..., m, n].
|
||||
// The algorithm implements a blocked QR decomposition; `block_size` is
|
||||
// the block size to use.
|
||||
// TODO(phawkins): handle the complex case.
|
||||
struct QRDecompositionResult {
|
||||
xla::XlaOp q;
|
||||
xla::XlaOp r;
|
||||
};
|
||||
|
||||
xla::StatusOr<QRDecompositionResult> QRDecomposition(
|
||||
xla::XlaOp a, bool full_matrices, int64 block_size = 128,
|
||||
xla::PrecisionConfig::Precision precision = xla::PrecisionConfig::HIGHEST);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_LIB_QR_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_COMPILER_TF2XLA_LIB_RANDOM_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_LIB_RANDOM_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/statusor.h"
|
||||
#include "tensorflow/core/framework/types.pb.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Builds an array filled with values sampled from a truncated normal
|
||||
// distribution such that no values are greater than two or less than negative
|
||||
// two.
|
||||
//
|
||||
// The "uniform" parameter must be an array of random numbers distributed in
|
||||
// (0,1).
|
||||
xla::XlaOp TruncatedNormal(xla::XlaOp uniform);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_LIB_RANDOM_H_
|
||||
@@ -0,0 +1,55 @@
|
||||
/* 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_COMPILER_TF2XLA_LIB_SCATTER_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_LIB_SCATTER_H_
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_computation.h"
|
||||
#include "tensorflow/compiler/xla/statusor.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Builds an XLA computation that performs a scatter operation on `buffer`,
|
||||
// returning an updated buffer.
|
||||
// For each i0, i1, ..., sets
|
||||
// buffer[indices[i0, i1, ...], ...] := updates[i0, i1, ...]
|
||||
//
|
||||
// If `indices_are_vectors` is false, then each index in indices is a scalar,
|
||||
// and the shape of `indices` must be a prefix of the shape of updates.
|
||||
// Otherwise, `indices_are_vectors`, then indices are multidimensional and the
|
||||
// minor dimension of `indices` represents a vector of indices.
|
||||
//
|
||||
// If `updates` is a scalar, then it will be broadcasted into the expected shape
|
||||
// of updates.
|
||||
//
|
||||
// If any part of the update region is out-of-bounds, the corresponding update
|
||||
// is discarded.
|
||||
//
|
||||
// If a `combiner` is provided, updates are combined with the existing values in
|
||||
// the buffer using the combiner function. Otherwise, the updates replace the
|
||||
// existing values. The order of updates is implementation-defined.
|
||||
xla::StatusOr<xla::XlaOp> XlaScatter(
|
||||
const xla::XlaOp& buffer, const xla::XlaOp& updates,
|
||||
const xla::XlaOp& indices, bool indices_are_vectors,
|
||||
const std::function<xla::XlaOp(xla::XlaOp, xla::XlaOp, xla::XlaBuilder*)>&
|
||||
combiner,
|
||||
xla::XlaBuilder* builder);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_LIB_SCATTER_H_
|
||||
@@ -0,0 +1,67 @@
|
||||
/* 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_COMPILER_TF2XLA_LIB_TRIANGULAR_SOLVE_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_LIB_TRIANGULAR_SOLVE_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Solves systems of linear equations with lower or upper triangular coefficient
|
||||
// matrices by forward- or back-substitution. Broadcasting along leading
|
||||
// dimensions, this routine solves one of the matrix systems
|
||||
// `op(a) * x = b`, or `x * op(a) = b`,
|
||||
// for the variable `x` given `a` and `b`, where `op(a)` is either
|
||||
// `op(a) = a`, or `op(a) = transpose(a)`, or `op(a) = conj(transpose(a))`.
|
||||
// That is, the innermost matrices in the output satisfy a scalar system
|
||||
// depending on the value of the value of (left_side, transpose_a, conjugate_a)
|
||||
// according to:
|
||||
// (F, F, F) => `output[..., i, k] a[..., k, j] = b[..., i, j]`,
|
||||
// (F, F, T) => `output[..., i, k] a*[..., k, j] = b[..., i, j]`,
|
||||
// (F, T, F) => `output[..., i, k] a[..., j, k] = b[..., i, j]`,
|
||||
// (F, T, T) => `output[..., i, k] a*[..., j, k] = b[..., i, j]`,
|
||||
// (T, F, F) => ` a[..., i, k] output[..., k, j] = b[..., i, j]`,
|
||||
// (T, F, T) => `a*[..., i, k] output[..., k, j] = b[..., i, j]`,
|
||||
// (T, T, F) => ` a[..., i, k] output[..., j, k] = b[..., i, j]`,
|
||||
// (T, T, T) => `a*[..., i, k] output[..., j, k] = b[..., i, j]`,
|
||||
// where * denotes complex conjugation and where the index `k` is summed over.
|
||||
//
|
||||
// `a` is a tensor of shape `[..., M, M]` whose innermost 2 dimensions form
|
||||
// square matrices. If lower is true (false), then the strictly upper (lower)
|
||||
// triangular part of each innermost matrix in `a` is assumed to be zero and is
|
||||
// not accessed.
|
||||
// `b` is a tensor of shape `[..., M, K]` if left_side is true, otherwise a
|
||||
// tensor of shape `[..., K, M]`.
|
||||
// `left_side` is a boolean, indicating whether to solve a system of the form
|
||||
// op(a) * x = b (true) or x * op(a) = b (false).
|
||||
// `lower` is a boolean, indicating whether the argument `a` is lower-triangular
|
||||
// (true) or upper-triangular (false).
|
||||
// `transpose_a` is a boolean indicating whether the matrix `a` is transposed.
|
||||
// `conjugate_a` is a boolean indicating whether the entries of `a` are complex
|
||||
// conjugated (independently of whether they are transposed), so that when both
|
||||
// transpose_a and conjugate_a are true the effect is a Hermitian adjoint.
|
||||
//
|
||||
// Uses a blocked algorithm if `block_size` is > 1; if block_size == 1 then no
|
||||
// blocking is used.
|
||||
xla::XlaOp TriangularSolve(
|
||||
xla::XlaOp a, xla::XlaOp b, bool left_side, bool lower, bool transpose_a,
|
||||
bool conjugate_a, int64 block_size = 128,
|
||||
xla::PrecisionConfig::Precision precision = xla::PrecisionConfig::HIGHEST);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_LIB_TRIANGULAR_SOLVE_H_
|
||||
@@ -0,0 +1,81 @@
|
||||
/* 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_COMPILER_TF2XLA_LIB_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_LIB_UTIL_H_
|
||||
|
||||
#include "absl/types/span.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_computation.h"
|
||||
#include "tensorflow/compiler/xla/statusor.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Returns a floating point scalar constant of 'type' with 'value'.
|
||||
// If 'type' is complex, returns a real value with zero imaginary component.
|
||||
xla::XlaOp FloatLiteral(xla::XlaBuilder* builder, xla::PrimitiveType type,
|
||||
double value);
|
||||
|
||||
// Makes a 1D tensor [0, ..., x, y] from two tensors x and y with zeros
|
||||
// prepended until the array is length n_dims.
|
||||
xla::XlaOp PrependZerosInMajorDims(xla::XlaOp x,
|
||||
absl::Span<const xla::XlaOp> starts);
|
||||
|
||||
// Returns a integer scalar constant of 'type' with 'value'.
|
||||
// If 'type' is complex, returns a real value with zero imaginary component.
|
||||
xla::XlaOp IntegerLiteral(xla::XlaBuilder* builder, xla::PrimitiveType type,
|
||||
int64 value);
|
||||
|
||||
// Builds a vector of zeros of length rank(x) with the last values being
|
||||
// those in `starts`.
|
||||
xla::XlaOp PrependZerosInMajorDims(xla::XlaOp x,
|
||||
absl::Span<const xla::XlaOp> starts);
|
||||
|
||||
// Performs a slice in the minor dimensions of a Tensor.
|
||||
xla::XlaOp SliceInMinorDims(xla::XlaOp x, absl::Span<const int64> start,
|
||||
absl::Span<const int64> end);
|
||||
|
||||
// Returns the concatenation of `xs` and `ys`.
|
||||
std::vector<int64> ConcatVectors(absl::Span<const int64> xs,
|
||||
absl::Span<const int64> ys);
|
||||
|
||||
// Performs a dynamic slice in the minor dimensions of a Tensor.
|
||||
xla::XlaOp DynamicSliceInMinorDims(xla::XlaOp x,
|
||||
absl::Span<const xla::XlaOp> starts,
|
||||
absl::Span<const int64> sizes);
|
||||
|
||||
// Updates a slice of 'x', i.e.,
|
||||
// x[start[0], ..., start[n]] = update
|
||||
xla::XlaOp UpdateSlice(xla::XlaOp x, xla::XlaOp update,
|
||||
absl::Span<const int64> start);
|
||||
|
||||
// Updates a slice of 'x', where 'start' contains a list of minor dimensions:
|
||||
// x[..., start[0], ..., start[n]] = update
|
||||
xla::XlaOp UpdateSliceInMinorDims(xla::XlaOp x, xla::XlaOp update,
|
||||
absl::Span<const int64> start);
|
||||
|
||||
xla::XlaOp DynamicUpdateSliceInMinorDims(xla::XlaOp x, xla::XlaOp update,
|
||||
absl::Span<const xla::XlaOp> starts);
|
||||
|
||||
// Transposes a stack of matrices `x` by swapping the last two dimensions.
|
||||
xla::XlaOp TransposeInMinorDims(xla::XlaOp x);
|
||||
|
||||
// Applies a complex conjugation operation if `a` is complex and `conjugate_a`
|
||||
// is true, otherwise returns its argument.
|
||||
xla::XlaOp MaybeConjugate(xla::XlaOp x, bool conjugate);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_LIB_UTIL_H_
|
||||
@@ -0,0 +1,73 @@
|
||||
/* 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_COMPILER_TF2XLA_LIB_WHILE_LOOP_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_LIB_WHILE_LOOP_H_
|
||||
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/types/span.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_computation.h"
|
||||
#include "tensorflow/compiler/xla/statusor.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Function that builds a loop condition. Takes as input a sequence of input
|
||||
// values, and returns a boolean value representing if the condition succeeds.
|
||||
typedef std::function<xla::StatusOr<xla::XlaOp>(absl::Span<const xla::XlaOp>,
|
||||
xla::XlaBuilder*)>
|
||||
LoopConditionFunction;
|
||||
|
||||
// Function that builds a loop body. Takes as input a sequence of input values
|
||||
// and returns a sequence of output values.
|
||||
typedef std::function<xla::StatusOr<std::vector<xla::XlaOp>>(
|
||||
absl::Span<const xla::XlaOp>, xla::XlaBuilder*)>
|
||||
LoopBodyFunction;
|
||||
|
||||
// Helper function for building an XLA while loop, where the values carried by
|
||||
// the loop are a tuple of values, e.g., (a, b, c):
|
||||
// while(
|
||||
// condition: (a, b, c) -> bool,
|
||||
// body: (a, b, c) -> (a, b, c)
|
||||
// init: (a, b, c)
|
||||
// )
|
||||
// 'name' is a descriptive name for the loop.
|
||||
xla::StatusOr<std::vector<xla::XlaOp>> XlaWhileLoop(
|
||||
const LoopConditionFunction& condition_function,
|
||||
const LoopBodyFunction& body_function,
|
||||
absl::Span<const xla::XlaOp> initial_values, absl::string_view name,
|
||||
xla::XlaBuilder* builder);
|
||||
|
||||
// Builds an XLA loop that repeats a computation `num_iterations` times.
|
||||
//
|
||||
// The body function (ForEachIndexBodyFunction) takes as input a pair of
|
||||
// (current iteration number, loop-carried values), and returns an updated
|
||||
// vector of the loop-carried values.
|
||||
typedef std::function<xla::StatusOr<std::vector<xla::XlaOp>>(
|
||||
xla::XlaOp, absl::Span<const xla::XlaOp>, xla::XlaBuilder*)>
|
||||
ForEachIndexBodyFunction;
|
||||
|
||||
xla::StatusOr<std::vector<xla::XlaOp>> XlaForEachIndex(
|
||||
int64 num_iterations, xla::PrimitiveType num_iterations_type,
|
||||
const ForEachIndexBodyFunction& body_function,
|
||||
absl::Span<const xla::XlaOp> initial_values, absl::string_view name,
|
||||
xla::XlaBuilder* builder);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_LIB_WHILE_LOOP_H_
|
||||
@@ -0,0 +1,67 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// Utilities for working with XLA Literals.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_TF2XLA_LITERAL_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_LITERAL_UTIL_H_
|
||||
|
||||
#include "absl/types/span.h"
|
||||
#include "tensorflow/compiler/xla/literal.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
#include "tensorflow/core/framework/tensor.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Returns a BorrowingLiteral that utilizes the same underlying buffer owned by
|
||||
// 'host_tensor'.
|
||||
Status HostTensorToBorrowingLiteral(const Tensor& host_tensor,
|
||||
xla::BorrowingLiteral* literal);
|
||||
// Returns a MutableBorrowingLiteral that utilizes the same underlying buffer
|
||||
// owned by 'host_tensor', but is mutable via the xla::Literal methods.
|
||||
Status HostTensorToMutableBorrowingLiteral(
|
||||
Tensor* host_tensor, xla::MutableBorrowingLiteral* literal);
|
||||
// Similar as above, except the literal shape is explicitly provided and used
|
||||
// instead of obtaining it from the 'host_tensor'. The provided literal shape
|
||||
// 'xla_shape' must be compatible with the shape of 'host_tensor'.
|
||||
Status HostTensorToMutableBorrowingLiteral(
|
||||
const xla::Shape& xla_shape, Tensor* host_tensor,
|
||||
xla::MutableBorrowingLiteral* literal);
|
||||
|
||||
// Returns a BorrowingLiteral tuple that utilizes the same underlying buffers
|
||||
// owned by 'host_tensors'.
|
||||
Status HostTensorsToBorrowingLiteralTuple(absl::Span<const Tensor> host_tensors,
|
||||
xla::BorrowingLiteral* literal);
|
||||
|
||||
// Copies 'literal' to freshly allocated 'host_tensor', which is allocated of
|
||||
// type <target_type>.
|
||||
// Fails if the literal's primitive type !=
|
||||
// DataTypeToPrimitiveType(target_type). Note that <target_type> is not
|
||||
// derivable from the type of <literal>, because multiple tensorflow types map
|
||||
// to the same XLA type (e.g. INT32 and QINT32 both map to INT32 in
|
||||
// XLA).
|
||||
Status LiteralToHostTensor(const xla::LiteralSlice& literal,
|
||||
DataType target_type, Tensor* host_tensor);
|
||||
|
||||
// Copies the contents of 'literal' to a previously allocated tensor
|
||||
// 'host_tensor'. The tensor and the literal must have the same number of
|
||||
// elements and the same type.
|
||||
Status CopyLiteralToHostTensor(const xla::LiteralSlice& literal,
|
||||
Tensor* host_tensor);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_LITERAL_UTIL_H_
|
||||
@@ -0,0 +1,71 @@
|
||||
/* 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_COMPILER_TF2XLA_RESOURCE_OPERATION_TABLE_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_RESOURCE_OPERATION_TABLE_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "tensorflow/core/platform/logging.h"
|
||||
|
||||
// Exposes information about the resource operations supported by tf2xla in a
|
||||
// structured form.
|
||||
|
||||
namespace tensorflow {
|
||||
enum class XlaResourceOpKind {
|
||||
kRead, // Only reads from resources.
|
||||
kWrite, // Only writes to resources.
|
||||
kReadWrite // Reads from and writes to resources.
|
||||
};
|
||||
|
||||
enum class XlaResourceKind {
|
||||
kVariable, // Operates on resource variables.
|
||||
kStack, // Operates on stacks.
|
||||
kTensorArray // Operates on tensor arrays.
|
||||
};
|
||||
|
||||
class XlaResourceOpInfo {
|
||||
public:
|
||||
explicit XlaResourceOpInfo(XlaResourceOpKind op_kind,
|
||||
XlaResourceKind resource_kind)
|
||||
: op_kind_(op_kind), resource_kind_(resource_kind) {}
|
||||
|
||||
XlaResourceOpKind kind() const { return op_kind_; }
|
||||
XlaResourceKind resource_kind() const { return resource_kind_; }
|
||||
|
||||
static absl::string_view XlaResourceOpKindToString(XlaResourceOpKind op_kind);
|
||||
|
||||
private:
|
||||
XlaResourceOpKind op_kind_;
|
||||
XlaResourceKind resource_kind_;
|
||||
};
|
||||
|
||||
// Returns a XlaResourceOpInfo describing `op` if it is a resource operation
|
||||
// supported by tf2xla, otherwise returns null (i.e. if this returns null then
|
||||
// `op` is either not a resource operation or is unsupported by XLA).
|
||||
const XlaResourceOpInfo* GetResourceOpInfoForOp(absl::string_view op);
|
||||
|
||||
namespace resource_op_table_internal {
|
||||
// NB! Implementation detail exposed for unit testing, do not use.
|
||||
//
|
||||
// Returns the set of resource operations known by this module.
|
||||
std::vector<absl::string_view> GetKnownResourceOps();
|
||||
} // namespace resource_op_table_internal
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_RESOURCE_OPERATION_TABLE_H_
|
||||
@@ -0,0 +1,45 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// Utilities for working with XLA shapes.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_TF2XLA_SHAPE_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_SHAPE_UTIL_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
#include "tensorflow/core/framework/tensor_shape.h"
|
||||
#include "tensorflow/core/framework/types.pb.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Convert an XLA Shape into the equivalent TensorFlow shape. May fail since
|
||||
// not all XLA shapes can be represented as TensorShapes.
|
||||
Status XLAShapeToTensorShape(const xla::Shape& shape,
|
||||
TensorShape* tensor_shape);
|
||||
|
||||
// Convert a TensorShape into the equivalent XLA Shape proto. Unlike Tensorflow,
|
||||
// XLA shapes include the type. Not all `dtype` values can be represented by
|
||||
// XLA, so this conversion may fail.
|
||||
Status TensorShapeToXLAShape(DataType dtype, const TensorShape& tensor_shape,
|
||||
xla::Shape* shape);
|
||||
|
||||
// Converts a TensorShape into the equivalent XLA Shape proto, taking an
|
||||
// xla::PrimitiveType to specify the element type. This never fails.
|
||||
xla::Shape TensorShapeToXLAShape(xla::PrimitiveType type,
|
||||
const TensorShape& tensor_shape);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_SHAPE_UTIL_H_
|
||||
@@ -0,0 +1,50 @@
|
||||
/* 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_COMPILER_TF2XLA_SHARDING_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_SHARDING_UTIL_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "tensorflow/compiler/xla/client/sharding_builder.h"
|
||||
#include "tensorflow/compiler/xla/status_macros.h"
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Parses the op sharding from the 'replicated core' device_name <device_name>.
|
||||
// Returns an error:
|
||||
// - if the device name is invalid.
|
||||
// - the core is parsed and is out of the range [0, num_cores_per_replica).
|
||||
//
|
||||
// Otherwise, returns either:
|
||||
// - explicit_sharding if explicit_sharding.has_value()
|
||||
// - a non-value if there is no assigned core or
|
||||
// - a sharding set as per xla::sharding_builder::AssignDevice.
|
||||
xla::StatusOr<absl::optional<xla::OpSharding>> ParseShardingFromDevice(
|
||||
const string& device_name, int num_cores_per_replica,
|
||||
absl::optional<xla::OpSharding> explicit_sharding = absl::nullopt);
|
||||
|
||||
xla::StatusOr<absl::optional<xla::OpSharding>> ParseShardingFromDevice(
|
||||
const Node& node, int num_cores_per_replica);
|
||||
|
||||
xla::StatusOr<absl::optional<xla::OpSharding>> ParseShardingFromDevice(
|
||||
const NodeDef& node_def, int num_cores_per_replica);
|
||||
|
||||
void SetShardingDeviceAssignmentFromNode(const Node& src, Node* dst);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_SHARDING_UTIL_H_
|
||||
@@ -0,0 +1,47 @@
|
||||
/* 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_COMPILER_TF2XLA_SIDE_EFFECT_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_SIDE_EFFECT_UTIL_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Side-effecting nodes will have this attribute set. Its value is the list of
|
||||
// node names which this node has side-effect dependencies on.
|
||||
//
|
||||
// Nodes like HostCompute, SendToHost, RecvFromHost always have this attribute,
|
||||
// because they always have side-effect.
|
||||
// If and While nodes may or may not have this attribute, depending on whether
|
||||
// their bodies have side-effecting nodes.
|
||||
extern const char kXlaTokenInputNodesAttrName[];
|
||||
|
||||
// This node name is used in kXlaTokenInputNodesAttrName attr to signal that a
|
||||
// node has side-effect dependency on current graph's token input.
|
||||
extern const char kXlaTokenArgNodeName[];
|
||||
|
||||
// Calculates side-effect dependencies for the graph's token output.
|
||||
// Returns a set of node names representing these dependencies.
|
||||
std::set<std::string> CalculateTokenInputsForOutputToken(const Graph& g);
|
||||
|
||||
// Returns whether a graph contains side-effecting nodes.
|
||||
bool HasSideEffectingNodes(const Graph& g);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_SIDE_EFFECT_UTIL_H_
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,149 @@
|
||||
/* 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_COMPILER_TF2XLA_TF2XLA_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_TF2XLA_UTIL_H_
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "tensorflow/compiler/tf2xla/tf2xla.pb.h"
|
||||
#include "tensorflow/compiler/xla/status_macros.h"
|
||||
#include "tensorflow/core/framework/graph.pb.h"
|
||||
#include "tensorflow/core/framework/kernel_def.pb.h"
|
||||
#include "tensorflow/core/framework/op.h"
|
||||
#include "tensorflow/core/graph/graph.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// ValidateConfig returns OK iff config is valid.
|
||||
Status ValidateConfig(const tf2xla::Config& config);
|
||||
|
||||
// Modifies <graph_def> to include placeholders for each fed tensor, and
|
||||
// update references to the fed tensors to refer to the placeholders.
|
||||
// The existing nodes referenced by the feeds are not removed or modified
|
||||
// (except where their input edges are modified by the replacement of other
|
||||
// feeds).
|
||||
Status AddPlaceholdersForFeeds(
|
||||
const tf2xla::Config& config, const OpRegistryInterface* op_registry,
|
||||
std::unordered_map<string, string>* feed_remapping, GraphDef* graph_def);
|
||||
|
||||
// Returns in <out> a copy of <in>, pruned to only include fetches from
|
||||
// <config>.
|
||||
Status PruneGraphDefInto(const tf2xla::Config& config, const GraphDef& in,
|
||||
GraphDef* out);
|
||||
|
||||
// Returns node:port for the given <id>.
|
||||
string TensorIdToString(const tf2xla::TensorId& id);
|
||||
|
||||
// Updates the sharding of <n> based on the sharding of its neighbors.
|
||||
// If <out_edges> is true, outgoing edges from <n> are considered; else incoming
|
||||
// edges are considered.
|
||||
Status SetNodeShardingFromNeighbors(Node* n, bool out_edges);
|
||||
|
||||
// Add an allowed data type to the AttrConstraint with the given name.
|
||||
void AddDtypeToKernalDefConstraint(absl::string_view name, DataType dtype,
|
||||
KernelDef* kdef);
|
||||
|
||||
// Returns the next random seed to use for seeding xla rng.
|
||||
uint32 GetXLARandomSeed();
|
||||
|
||||
// Indicates how a FunctionDef is associated with a graph node (e.g. the node is
|
||||
// a function call, or the node has function attrs).
|
||||
class AssociatedFunctionInfo {
|
||||
public:
|
||||
enum AssociatedFunctionType {
|
||||
kFunctionAttr = 0,
|
||||
kFunctionCallNode = 1,
|
||||
kSymbolicGradient = 2,
|
||||
};
|
||||
|
||||
// The function is an attr of the node.
|
||||
static AssociatedFunctionInfo FunctionAttr(const string& func_name,
|
||||
const AttrValueMap& attrs,
|
||||
const string& attr_name) {
|
||||
return AssociatedFunctionInfo(kFunctionAttr, func_name, attrs, attr_name);
|
||||
}
|
||||
|
||||
// The node is a function call.
|
||||
static AssociatedFunctionInfo FunctionCall(const string& func_name,
|
||||
const AttrValueMap& attrs) {
|
||||
// attr_name will not be used in this case.
|
||||
return AssociatedFunctionInfo(kFunctionCallNode, func_name, attrs,
|
||||
/*attr_name=*/"");
|
||||
}
|
||||
|
||||
// The node is a SymbolicGradient op.
|
||||
static AssociatedFunctionInfo SymbolicGradient(const string& func_name,
|
||||
const AttrValueMap& attrs) {
|
||||
// attr_name will not be used in this case.
|
||||
return AssociatedFunctionInfo(kSymbolicGradient, func_name, attrs,
|
||||
/*attr_name=*/"");
|
||||
}
|
||||
|
||||
AssociatedFunctionType type() const { return type_; }
|
||||
|
||||
const string& func_name() const { return func_name_; }
|
||||
|
||||
const string& attr_name() const { return attr_name_; }
|
||||
|
||||
const AttrValueMap& attrs() const { return attrs_; }
|
||||
|
||||
private:
|
||||
AssociatedFunctionInfo(AssociatedFunctionType type, const string& func_name,
|
||||
const AttrValueMap& attrs, const string& attr_name)
|
||||
: type_(type),
|
||||
func_name_(func_name),
|
||||
attrs_(attrs),
|
||||
attr_name_(attr_name) {}
|
||||
|
||||
// Available for all instances.
|
||||
AssociatedFunctionType type_;
|
||||
string func_name_;
|
||||
AttrValueMap attrs_;
|
||||
|
||||
// Only available if the function is defined in an attr.
|
||||
string attr_name_;
|
||||
};
|
||||
|
||||
// Returns if the NodeDef has associated function.
|
||||
bool HasAssociatedFunction(const NodeDef& node_def,
|
||||
FunctionLibraryRuntime* flr);
|
||||
|
||||
// Gets functions associated with the node. Current cases:
|
||||
// 1. For function call node, its function name;
|
||||
// 2. For SymbolicGradient op, returned func_name will be "SymbolicGradient",
|
||||
// and returned attrs will be this node's attributes;
|
||||
// 3. For nodes like XlaWhile/XlaIf, all their function attributes.
|
||||
std::vector<AssociatedFunctionInfo> GetAssociatedFunctions(
|
||||
const Node& node, FunctionLibraryRuntime* flr);
|
||||
|
||||
// Changes associated functions for the node. Current cases:
|
||||
// 1. For function call node, creates a new node with the new function name and
|
||||
// remove the old node;
|
||||
// 2. For SymbolicGradient op, add or replace GradientDef in
|
||||
// FunctionLibraryDefinition;
|
||||
// 3. For nodes like XlaWhile/XlaIf, modify their function attributes.
|
||||
Status RewriteAssociatedFunction(
|
||||
Graph* graph, Node* node, FunctionLibraryDefinition* fld,
|
||||
const AssociatedFunctionInfo& associated_function,
|
||||
const string& rewritten_function_name);
|
||||
|
||||
// Attribute to mark nodes to be executed on host.
|
||||
extern const char kXlaOutsideCompilationAttrName[];
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_TF2XLA_UTIL_H_
|
||||
@@ -0,0 +1,38 @@
|
||||
/* 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_COMPILER_TF2XLA_TYPE_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_TYPE_UTIL_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
#include "tensorflow/core/framework/types.pb.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Converts a Tensorflow DataType to an XLA PrimitiveType.
|
||||
Status DataTypeToPrimitiveType(DataType data_type, xla::PrimitiveType* type);
|
||||
|
||||
// N.B.: there is intentionally no function to convert an XLA PrimitiveType to
|
||||
// a TensorFlow DataType. The mapping from TF types to XLA types is not
|
||||
// one-to-one: for example, both DT_INT8 and DT_QINT8 map to xla::S8. So the
|
||||
// inverse would not be a well-defined function. If you find that you want the
|
||||
// inverse mapping, then most likely you should be preserving the original
|
||||
// TensorFlow type, rather than trying to convert an XLA type into a TensorFlow
|
||||
// type.
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_TYPE_UTIL_H_
|
||||
@@ -0,0 +1,106 @@
|
||||
/* 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_COMPILER_TF2XLA_XLA_COMPILATION_DEVICE_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_XLA_COMPILATION_DEVICE_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "tensorflow/compiler/tf2xla/xla_resource.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
#include "tensorflow/core/common_runtime/local_device.h"
|
||||
#include "tensorflow/core/framework/device_base.h"
|
||||
#include "tensorflow/core/framework/tensor.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
#include "tensorflow/core/platform/mem.h"
|
||||
#include "tensorflow/core/public/session_options.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Class is defined in xla_compilation_device.cc, reference
|
||||
// included here only so the XlaCompilationDevice allocator_ member can be
|
||||
// declared.
|
||||
class XlaCompilationAllocator;
|
||||
|
||||
// This is a 'dummy' TensorFlow device that is only used to execute a
|
||||
// subgraph of XLA compilation Ops to construct a compiled version
|
||||
// of the subgraph's computation. It has a 'dummy' allocator that
|
||||
// backs each Tensor with metadata indicating the computation the
|
||||
// Tensor represents.
|
||||
//
|
||||
// We deliberately don't register a device factory because we *never*
|
||||
// want placement to put Ops on a compilation device. The device is created
|
||||
// manually, not using a factory.
|
||||
//
|
||||
// XLA compilation is not thread-safe. OpKernels registered on the
|
||||
// XlaCompilationDevice must not use threads or concurrency.
|
||||
class XlaCompilationDevice : public LocalDevice {
|
||||
public:
|
||||
XlaCompilationDevice(const SessionOptions& options, DeviceType type);
|
||||
|
||||
~XlaCompilationDevice() override;
|
||||
|
||||
Allocator* GetAllocator(AllocatorAttributes attr) override;
|
||||
|
||||
void Compute(OpKernel* op_kernel, OpKernelContext* context) override;
|
||||
|
||||
Status Sync() override;
|
||||
|
||||
Status MakeTensorFromProto(const TensorProto& tensor_proto,
|
||||
const AllocatorAttributes alloc_attrs,
|
||||
Tensor* tensor) override;
|
||||
|
||||
private:
|
||||
std::unique_ptr<XlaCompilationAllocator> allocator_;
|
||||
};
|
||||
|
||||
// A XlaExpression wraps an XLA computation. Each Tensor on an
|
||||
// XlaCompilationDevice contains an XlaExpression, and the shape of the Tensor
|
||||
// matches the shape of the subcomputation in the XlaOp. Each
|
||||
// expression is either a constant, or a function of previously-compiled
|
||||
// expressions.
|
||||
class XlaExpression {
|
||||
public:
|
||||
XlaExpression();
|
||||
|
||||
// handle() stores the XLA handle of the computation that the
|
||||
// expression represents.
|
||||
void set_handle(const xla::XlaOp& h);
|
||||
const xla::XlaOp& handle() const { return handle_; }
|
||||
|
||||
void set_constant_value(Tensor value);
|
||||
bool has_constant_value() const { return has_constant_value_; }
|
||||
const Tensor& constant_value() const { return constant_value_; }
|
||||
|
||||
void set_resource(XlaResource* resource) { resource_ = resource; }
|
||||
XlaResource* resource() const { return resource_; }
|
||||
|
||||
private:
|
||||
// The XLA handle of the expression's computation.
|
||||
xla::XlaOp handle_;
|
||||
|
||||
// If this expression is a constant with a known value, 'constant_value' is a
|
||||
// host-memory Tensor containing the value. Used to avoid invoking XLA for
|
||||
// expressions that are trivially constant.
|
||||
bool has_constant_value_ = false;
|
||||
Tensor constant_value_;
|
||||
|
||||
XlaResource* resource_ = nullptr; // Not owned.
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_XLA_COMPILATION_DEVICE_H_
|
||||
@@ -0,0 +1,479 @@
|
||||
/* 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_COMPILER_TF2XLA_XLA_COMPILER_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_XLA_COMPILER_H_
|
||||
|
||||
#include <stack>
|
||||
|
||||
#include "tensorflow/compiler/tf2xla/host_compute_metadata.pb.h"
|
||||
#include "tensorflow/compiler/tf2xla/xla_compilation_device.h"
|
||||
#include "tensorflow/compiler/tf2xla/xla_op_registry.h"
|
||||
#include "tensorflow/compiler/xla/client/local_client.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_computation.h"
|
||||
#include "tensorflow/compiler/xla/status_macros.h"
|
||||
#include "tensorflow/core/common_runtime/device.h"
|
||||
#include "tensorflow/core/common_runtime/device_mgr.h"
|
||||
#include "tensorflow/core/common_runtime/function.h"
|
||||
#include "tensorflow/core/framework/function.h"
|
||||
#include "tensorflow/core/lib/core/errors.h"
|
||||
#include "tensorflow/core/platform/env.h"
|
||||
#include "tensorflow/core/platform/mutex.h"
|
||||
#include "tensorflow/core/platform/notification.h"
|
||||
#include "tensorflow/core/platform/thread_annotations.h"
|
||||
#include "tensorflow/core/public/version.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
class XlaContext;
|
||||
|
||||
// The XlaCompiler class is responsible for compilation of a self-contained
|
||||
// subgraph of a TensorFlow computation using the XLA linear algebra runtime.
|
||||
// It does a symbolic execution of the graph starting from specific input
|
||||
// shapes, using a JIT device to convert operators into XLA computations.
|
||||
//
|
||||
// XlaCompiler is typically invoked from an `XlaLaunch` operator once the
|
||||
// shapes of all input parameters to the computation are known. This is
|
||||
// because the symbolic execution requires known shapes for all operations.
|
||||
//
|
||||
// XlaCompiler compiles Tensorflow graphs that received inputs via _Arg nodes,
|
||||
// and return outputs via _Retval nodes.
|
||||
//
|
||||
// The XlaCompiler requires one Argument struct for each _Arg index, that
|
||||
// describes each argument. Arguments can be compile-time constants
|
||||
// (kind kConstant), run-time parameters (kind kParameter), or resources
|
||||
// (kind kResource).
|
||||
//
|
||||
// Only kParameter and initialized kResource arguments become runtime parameters
|
||||
// to the generated XLA computation.
|
||||
//
|
||||
// The run-time outputs of the XLA computation are arranged in the following
|
||||
// order:
|
||||
// +------------------+-----------------------------------------+
|
||||
// | _Retval values | Updated values of kResource arguments |
|
||||
// +------------------+-----------------------------------------+
|
||||
// _Retval values are ordered by _Retval index, whereas kResource values are
|
||||
// ordered by the original _Arg position of the variable.
|
||||
//
|
||||
// If a shape representation function is provided as part of
|
||||
// XlaCompiler::CompileOptions, kParameter arguments and return values to an
|
||||
// entry computation will be reshaped in accordance to the shape function.
|
||||
// Arguments and return values to a non-entry computation are not reshaped.
|
||||
// Variable resource arguments are passed and returned in reshaped form, even
|
||||
// for non-entry computations. This feature allows TensorFlow to keep on-device
|
||||
// tensors with a different shape to their representation inside the XLA
|
||||
// computation.
|
||||
//
|
||||
// In computation outputs, updated kResource values are placed the end. When
|
||||
// emitting While loop bodies, we must ensure that the loop body has
|
||||
// identical input and output signatures. By passing variable values
|
||||
// at the end of the argument list and using the
|
||||
// `return_updated_values_for_all_variables` option, we can ensure that the
|
||||
// input and output values of resources appear at the same positions.
|
||||
//
|
||||
// Resources are passed as parameters or returned as resource updates in
|
||||
// "packed" form.
|
||||
// kStack resources are packed as (array, size of stack) XLA tuples.
|
||||
// kTensorArray resources without gradients are packed as the array that
|
||||
// backs the TensorArray. If gradients are present (`tensor_array_gradients`),
|
||||
// the packed representation is a (array, gradient0, gradient1, ...) tuple,
|
||||
// where gradient_k is the value of the k-th gradient in the
|
||||
// `tensor_array_gradients` ordered set.
|
||||
class XlaCompiler {
|
||||
public:
|
||||
// Describes how to derive the value of each _Arg node in the graph/function
|
||||
// being compiled. There must be one Argument for each _Arg index.
|
||||
struct Argument {
|
||||
enum Kind {
|
||||
// Default value; not a valid kind.
|
||||
kInvalid,
|
||||
|
||||
// Argument is a compile-time constant. No associated runtime parameter.
|
||||
kConstant,
|
||||
|
||||
// Argument is a Variable, TensorArray, or Stack resource. Has an
|
||||
// associated runtime parameter iff `initialized` is true.
|
||||
kResource,
|
||||
|
||||
// Argument is a run-time parameter.
|
||||
kParameter,
|
||||
|
||||
// Argument is an XLA token.
|
||||
kToken,
|
||||
};
|
||||
|
||||
Kind kind = kInvalid;
|
||||
|
||||
// The type of the argument. If the argument is a resource, this
|
||||
// is the type of the variable's value, not DT_RESOURCE.
|
||||
DataType type;
|
||||
|
||||
// The shape of the argument. For:
|
||||
// * a parameter: the shape of the parameter.
|
||||
// * a constant: ignored; the shape given by constant_value is used
|
||||
// instead.
|
||||
// * an uninitialized resource: ignored. We don't yet know the shape of an
|
||||
// uninitialized resource (otherwise we would have initialized it!)
|
||||
// * an initialized variable: the shape of the variable's value.
|
||||
// * an initialized TensorArray or Stack resource: the shape of an entry in
|
||||
// the TensorArray/Stack. Note this is the size of a single entry, not the
|
||||
// XLA data structure that represents the complete stack/array.
|
||||
TensorShape shape;
|
||||
|
||||
// The value of the argument, if it is a compile-time constant. Must be a
|
||||
// host-memory tensor.
|
||||
Tensor constant_value;
|
||||
|
||||
// The name of this argument, used for debugging.
|
||||
string name;
|
||||
|
||||
// For a kResource, what kind of resource is it?
|
||||
XlaResource::Kind resource_kind = XlaResource::kInvalid;
|
||||
|
||||
// For a kResource, has this resource been initialized?
|
||||
bool initialized = false;
|
||||
|
||||
// For a TensorArray or Stack resource, what is the array's declared size?
|
||||
// (Used for lazy initialization.)
|
||||
int64 tensor_array_size = -1;
|
||||
|
||||
// TensorArray resource parameters are passed as (array, gradient array 0,
|
||||
// ..., gradient array k), where the gradient arrays are in the same order
|
||||
// as `tensor_array_gradients`.
|
||||
std::set<string> tensor_array_gradients;
|
||||
|
||||
bool operator==(const Argument& other) const;
|
||||
};
|
||||
|
||||
// Options pertaining to an individual call to CompileGraph() or
|
||||
// CompileFunction().
|
||||
struct CompileOptions {
|
||||
// If `use_tuple_arg` is true, a single tuple parameter will be used for all
|
||||
// arguments; if false, each argument gets its own parameter.
|
||||
bool use_tuple_arg = false;
|
||||
|
||||
// If 'return_updated_values_for_all_resources' is true, then updated
|
||||
// values of all resource arguments will be included in the
|
||||
// 'resource_updates' of the computation, even if the resource was not
|
||||
// modified by the computation. Used when compiling loop bodies to ensure
|
||||
// the input and output signatures match.
|
||||
bool return_updated_values_for_all_resources = false;
|
||||
|
||||
// If 'resolve_compile_time_constants' is true, then outputs of a
|
||||
// computation that are known to be compile-time constants will be returned
|
||||
// as Tensors at compile-time, rather than as run-time outputs of the
|
||||
// computation.
|
||||
bool resolve_compile_time_constants = true;
|
||||
|
||||
// If 'always_return_tuple' is true, then the output of a computation will
|
||||
// always be a tuple. Otherwise, a single-element output will not be wrapped
|
||||
// in a tuple.
|
||||
bool always_return_tuple = true;
|
||||
|
||||
// True when compiling the entry computation, false for subcomputations
|
||||
// (while, call, etc.)
|
||||
bool is_entry_computation = true;
|
||||
|
||||
// True when we should add XLA input & output to the graph/function.
|
||||
bool add_token_input_output = false;
|
||||
};
|
||||
|
||||
struct OutputDescription {
|
||||
// Type and shape of the output. The shape is the unflattened shape.
|
||||
// When `type` is DT_RESOURCE, `shape` is the shape of the resource
|
||||
// variable's value.
|
||||
DataType type;
|
||||
TensorShape shape;
|
||||
|
||||
// Constant output value, if known to be constant at JIT compilation time.
|
||||
// 'Tensor' is in host memory.
|
||||
bool is_constant = false;
|
||||
Tensor constant_value;
|
||||
|
||||
// When this output is a resource, i.e. `type == DT_RESOURCE`, this is
|
||||
// the index of the input that contains the resource.
|
||||
int input_index;
|
||||
};
|
||||
|
||||
// Describes a variable write side effect of the computation.
|
||||
struct ResourceUpdate {
|
||||
// Index of the input that contains the variable resource to write to.
|
||||
int input_index;
|
||||
|
||||
// Type and shape of the tensor to be written back.
|
||||
// The `shape` field has the same meaning as the Argument::shape field.
|
||||
DataType type;
|
||||
TensorShape shape;
|
||||
|
||||
// Was the value of the variable modified by the computation?
|
||||
// (Always true, unless `return_updated_values_for_all_resources` is true.)
|
||||
bool modified;
|
||||
|
||||
// If the resource is a TensorArray, the set of gradients read or written.
|
||||
std::set<string> tensor_array_gradients_accessed;
|
||||
};
|
||||
|
||||
struct CompilationResult {
|
||||
// Vector that maps from the parameters of the XLA computation to their
|
||||
// original argument positions. To handle compile-time constant inputs, the
|
||||
// parameters to the XLA computation may be a subset of the original
|
||||
// arguments. The relative ordering of parameters are maintained.
|
||||
std::vector<int> input_mapping;
|
||||
|
||||
// Input shapes of the computation. If we are flattening inputs, these are
|
||||
// the flattened shapes.
|
||||
std::vector<xla::Shape> xla_input_shapes;
|
||||
|
||||
// Output shape in XLA format. The output shape is always a tuple. If we
|
||||
// are flattening outputs, these are the flattened shapes.
|
||||
xla::Shape xla_output_shape;
|
||||
|
||||
// TensorFlow shapes of outputs, together with the values of any
|
||||
// constant arguments. Vector indexed by Tensorflow _Retval number,
|
||||
// containing both constant and non-constant results.
|
||||
std::vector<OutputDescription> outputs;
|
||||
|
||||
// TensorFlow shapes and types of sends/recvs from HostCompute Ops to their
|
||||
// matching RecvAtHost/SendFromHost Ops in the outer graph.
|
||||
tf2xla::HostComputeMetadata host_compute_metadata;
|
||||
|
||||
// Resources whose values were updated by the computation, ordered
|
||||
// by return value position (which is the same as the order the resources
|
||||
// were passed as arguments). Resource updates follow the non-constant
|
||||
// results in the outputs of XLA computation.
|
||||
std::vector<ResourceUpdate> resource_updates;
|
||||
|
||||
// The XLA computation built from the tensorflow subgraph.
|
||||
std::shared_ptr<xla::XlaComputation> computation;
|
||||
};
|
||||
|
||||
typedef std::function<xla::StatusOr<TensorShape>(const TensorShape&,
|
||||
DataType)>
|
||||
ShapeRepresentationFn;
|
||||
struct Options {
|
||||
// Name of the compilation device to use. It must be set by the caller.
|
||||
// The default empty value is invalid.
|
||||
DeviceType device_type = DeviceType("");
|
||||
|
||||
// The device to use during compilation to execute instructions on, for
|
||||
// example for auto-tuning.
|
||||
// Valid values are defined by `xla::Backend::devices_ordinal_supported()`.
|
||||
// -1 indicates the default device should be used.
|
||||
int device_ordinal = -1;
|
||||
|
||||
xla::Client* client = nullptr;
|
||||
|
||||
// Function library in which to find function definitions. Must be non-null.
|
||||
const FunctionLibraryDefinition* flib_def = nullptr;
|
||||
|
||||
// The graph def version to be compiled.
|
||||
int graph_def_version = TF_GRAPH_DEF_VERSION;
|
||||
|
||||
// If 'allow_cpu_custom_calls' is true, kernels may make use of CustomCall()
|
||||
// for CPU.
|
||||
bool allow_cpu_custom_calls = false;
|
||||
|
||||
// If set, the XLA representation of variables represented to XLA as the
|
||||
// shape given by this shape function. Variables are reshaped to this shape
|
||||
// on write, and reshaped to their original shape on read.
|
||||
ShapeRepresentationFn shape_representation_fn;
|
||||
|
||||
// If not nullptr, populate_resource_manager is called with the
|
||||
// compilation device's resource manager when the compilation
|
||||
// device is created, and can be used to create metadata objects
|
||||
// that can be accessed by XLA op kernels.
|
||||
std::function<Status(ResourceMgr*)>* populate_resource_manager = nullptr;
|
||||
|
||||
// If not nullptr, this memory allocator can be used by the compiler for
|
||||
// temporary allocations it might want to make during compilation.
|
||||
//
|
||||
// For example, the compiler may want to try out different algorithms and
|
||||
// choose the fastest one, and it might run those algorithms over buffers
|
||||
// created using this allocator.
|
||||
//
|
||||
// The compiler can function correctly without an explicit allocator given
|
||||
// here, but on some devices (notably, GPUs), TensorFlow tends to eagerly
|
||||
// allocate most or all available memory on the device, leaving none for the
|
||||
// compiler to access, unless it can use TensorFlow's allocator.
|
||||
xla::DeviceMemoryAllocator* device_allocator = nullptr;
|
||||
};
|
||||
|
||||
explicit XlaCompiler(Options options);
|
||||
|
||||
~XlaCompiler();
|
||||
|
||||
Status CompileFunction(const CompileOptions& options,
|
||||
const NameAttrList& fn_name_attrs,
|
||||
std::vector<Argument> args, CompilationResult* result);
|
||||
|
||||
// Compiles a tensorflow::Graph into an xla::XlaComputation.
|
||||
// Similar to CompileFunction, but takes a Graph as input rather than a
|
||||
// function.
|
||||
Status CompileGraph(const CompileOptions& options, string const& name,
|
||||
std::unique_ptr<Graph> graph,
|
||||
const std::vector<Argument>& args,
|
||||
CompilationResult* result);
|
||||
|
||||
// Compiles a single Op, given by an OpKernelContext, into an
|
||||
// xla::XlaComputation. Similar to CompileFunction but takes a single Op as
|
||||
// input.
|
||||
Status CompileSingleOp(const CompileOptions& options, string const& name,
|
||||
OpKernelContext* ctx,
|
||||
const std::vector<Argument>& args,
|
||||
CompilationResult* result);
|
||||
|
||||
// Returns the shape of the XLA parameter for an argument 'arg'.
|
||||
// See the class comment for more details about the argument passing
|
||||
// convention.
|
||||
Status XLAShapeForArgument(const Argument& arg, bool is_entry_computation,
|
||||
xla::Shape* xla_shape) const;
|
||||
|
||||
// Retrieves the channel handle associated with `key`. Allocates
|
||||
// a new channel handle if none exists.
|
||||
// Channel handles can be used to communicate between different
|
||||
// computations. Computations that communicate should be compiled with the
|
||||
// same XlaCompiler.
|
||||
Status GetChannelHandle(const string& key, xla::ChannelHandle* channel);
|
||||
|
||||
// Retrieves the host-to-device channel handle associated with `key`.
|
||||
// Allocates a new channel handle if none exists.
|
||||
Status GetHostToDeviceChannelHandle(const string& key,
|
||||
xla::ChannelHandle* channel);
|
||||
|
||||
// Retrieves the device-to-host channel handle associated with `key`.
|
||||
// Allocates a new channel handle if none exists.
|
||||
Status GetDeviceToHostChannelHandle(const string& key,
|
||||
xla::ChannelHandle* channel);
|
||||
|
||||
// Sets the shapes and types for the device to host transfer associated with
|
||||
// 'key'.
|
||||
Status SetDeviceToHostMetadata(const string& key,
|
||||
absl::Span<const DataType> types,
|
||||
absl::Span<const TensorShape> shapes);
|
||||
|
||||
// Gets the shapes the device to host transfer associated with 'key'.
|
||||
Status GetDeviceToHostShapes(const string& key,
|
||||
std::vector<TensorShape>* shapes) const;
|
||||
|
||||
// Sets the shapes and types for the host to device transfer associated with
|
||||
// 'key'.
|
||||
Status SetHostToDeviceMetadata(const string& key,
|
||||
absl::Span<const DataType> types,
|
||||
absl::Span<const TensorShape> shapes);
|
||||
|
||||
// In order to avoid deadlocks from dependencies in host computations, it can
|
||||
// be necessary to enforce a partial order on the execution of HostCompute
|
||||
// Ops. In particular it may be necessary to constrain the SendToHost for one
|
||||
// HostCompute to run before blocking on the RecvAtHost for another
|
||||
// HostCompute. The compiler maintains a mapping from 'host_compute_name' to
|
||||
// handle, where the handle is an 'output' of the HostCompute Op corresponding
|
||||
// to 'host_compute_name'. Another HostCompute Op that needs to be sequenced
|
||||
// later can add the handle as an 'input' to enforce the constraints.
|
||||
// 'host_compute_name' can be any string the client wishes to use to identify
|
||||
// a given HostCompute Op as long as the names are unique within the
|
||||
// compilation.
|
||||
Status GetHostComputeControlDependency(const string& host_compute_name,
|
||||
xla::XlaOp* handle);
|
||||
Status SetHostComputeControlDependency(const string& host_compute_name,
|
||||
const xla::XlaOp& handle);
|
||||
|
||||
const Options& options() const { return options_; }
|
||||
xla::Client* client() const { return options_.client; }
|
||||
FunctionLibraryRuntime* flib_runtime() const { return flib_runtime_; }
|
||||
|
||||
void PushNodeTokenMapping();
|
||||
Status PopNodeTokenMapping();
|
||||
Status SetNodeToken(const string& node_name, const xla::XlaOp& op);
|
||||
xla::StatusOr<xla::XlaOp> GetNodeToken(const string& node_name);
|
||||
|
||||
private:
|
||||
// Sets the function body `fbody` to the one registered as `function`.
|
||||
Status FindFunctionBody(const NameAttrList& function,
|
||||
const FunctionBody** fbody);
|
||||
|
||||
// Returns the optimized graph object in this function body.
|
||||
std::unique_ptr<Graph> GetGraph(const FunctionBody* fbody);
|
||||
|
||||
// Builds XLA computations for each of the arguments to the computation.
|
||||
// `args` are the arguments to the computation.
|
||||
Status BuildArguments(const Graph& graph,
|
||||
const std::vector<XlaCompiler::Argument>& args,
|
||||
bool use_tuple_arg, xla::XlaBuilder* builder,
|
||||
XlaContext* context, std::vector<int>* arg_cores,
|
||||
std::vector<XlaExpression>* arg_expressions,
|
||||
std::vector<int>* input_mapping,
|
||||
std::vector<xla::Shape>* input_shapes,
|
||||
bool is_entry_computation);
|
||||
|
||||
// Graph compiler needs to know how to get an optimized graph from a function
|
||||
// body.
|
||||
friend class GraphCompiler;
|
||||
friend class XlaCompilerTest;
|
||||
|
||||
Options options_;
|
||||
|
||||
// Status set to non-OK in the constructor if initialization fails.
|
||||
Status initialization_status_;
|
||||
|
||||
// Returns the next step sequence number.
|
||||
int64 NextStepId();
|
||||
|
||||
// Internal sequence number for steps executed on the compilation device.
|
||||
int64 next_step_id_;
|
||||
|
||||
XlaCompilationDevice* device_; // Owned by device_mgr_
|
||||
DeviceMgr device_mgr_;
|
||||
|
||||
// To avoid copying the client's function library, use a local function
|
||||
// library and runtime for functions created as part of the functionalize
|
||||
// control flow transformation.
|
||||
std::unique_ptr<FunctionLibraryDefinition> local_flib_def_;
|
||||
std::unique_ptr<ProcessFunctionLibraryRuntime> pflr_;
|
||||
std::unique_ptr<ProcessFunctionLibraryRuntime> local_pflr_;
|
||||
|
||||
FunctionLibraryRuntime* local_flib_runtime_; // owned by local_pflr_.
|
||||
FunctionLibraryRuntime* flib_runtime_; // owned by pflr_.
|
||||
|
||||
struct SignatureHash {
|
||||
uint64 operator()(
|
||||
const std::pair<string, std::vector<Argument>>& signature) const;
|
||||
};
|
||||
|
||||
std::unordered_map<std::pair<string, std::vector<Argument>>,
|
||||
CompilationResult, SignatureHash>
|
||||
cache_;
|
||||
|
||||
std::unordered_map<string, xla::ChannelHandle> channels_;
|
||||
|
||||
std::unordered_map<string, tf2xla::HostTransferMetadata> host_compute_sends_;
|
||||
std::unordered_map<string, tf2xla::HostTransferMetadata> host_compute_recvs_;
|
||||
|
||||
std::unordered_map<string, xla::XlaOp> host_compute_control_output_;
|
||||
|
||||
// This is used to store <node name, token output> mapping. Side-effecting
|
||||
// ops call SetNodeToken() to record its token output, so later side-effecting
|
||||
// ops can use GetNodeToken() to get it and use it as token input.
|
||||
//
|
||||
// It's a stack because we need a mapping like this for each level of nested
|
||||
// CompileGraph() call. In CompileGraph(), we will push a new mapping to the
|
||||
// stack, and pop the mapping before returning.
|
||||
std::stack<std::map<string, xla::XlaOp>> node_token_mapping_stack_;
|
||||
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(XlaCompiler);
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_XLA_COMPILER_H_
|
||||
@@ -0,0 +1,203 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// This file defines the contexts used during XLA compilation.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_TF2XLA_XLA_CONTEXT_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_XLA_CONTEXT_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/compiler/tf2xla/xla_compilation_device.h"
|
||||
#include "tensorflow/compiler/tf2xla/xla_compiler.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_computation.h"
|
||||
#include "tensorflow/compiler/xla/status_macros.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
#include "tensorflow/core/framework/op_kernel.h"
|
||||
#include "tensorflow/core/framework/resource_mgr.h"
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
class XlaOpKernelContext;
|
||||
|
||||
// The XlaContext is the data structure that holds the state of an XLA
|
||||
// compilation, that is accessible from OpKernelContexts when compiling a
|
||||
// subgraph of Ops using XLA.
|
||||
class XlaContext : public ResourceBase {
|
||||
public:
|
||||
// Retrieves the XlaContext of the current compilation.
|
||||
static XlaContext& Get(const OpKernelContext* ctx);
|
||||
static XlaContext& Get(const XlaOpKernelContext* ctx);
|
||||
|
||||
// Creates a new XlaContext. See the documentation on the class data fields
|
||||
// for descriptions of the arguments.
|
||||
XlaContext(XlaCompiler* compiler, xla::XlaBuilder* builder,
|
||||
bool allow_cpu_custom_calls, bool resolve_compile_time_constants,
|
||||
bool is_entry_computation,
|
||||
const std::function<xla::StatusOr<TensorShape>(
|
||||
const TensorShape&, DataType)>* shape_representation_fn);
|
||||
|
||||
// Virtual method defined by ResourceBase.
|
||||
string DebugString() override;
|
||||
|
||||
XlaCompiler* compiler() const { return compiler_; }
|
||||
|
||||
// Returns the XlaBuilder that Ops use for compiling new expressions.
|
||||
xla::XlaBuilder* builder();
|
||||
|
||||
bool allow_cpu_custom_calls() const { return allow_cpu_custom_calls_; }
|
||||
|
||||
bool resolve_compile_time_constants() const {
|
||||
return resolve_compile_time_constants_;
|
||||
}
|
||||
bool is_entry_computation() const { return is_entry_computation_; }
|
||||
|
||||
const std::vector<XlaExpression>& args() const { return args_; }
|
||||
void set_args(std::vector<XlaExpression> args);
|
||||
|
||||
struct Retval {
|
||||
DataType type;
|
||||
TensorShape shape;
|
||||
// An XlaExpression representing the Retval's value.
|
||||
XlaExpression expression;
|
||||
};
|
||||
const std::vector<Retval>& retvals() { return retvals_; }
|
||||
|
||||
// This is called by the Retval Op to associate a computed value
|
||||
// with a specific return value of the subgraph.
|
||||
void AddRetval(int retval_index, DataType type, const TensorShape& shape,
|
||||
const xla::XlaOp& handle);
|
||||
|
||||
// As for Retval, but for return values that are compile-time constants.
|
||||
Status AddConstRetval(int retval_index, DataType dtype,
|
||||
const xla::LiteralSlice& literal);
|
||||
|
||||
// As for Retval, but for return values that are resource handles.
|
||||
Status AddResourceRetval(int retval_index, XlaResource* resource);
|
||||
|
||||
// As for Retval, but for return values that are XLA tokens.
|
||||
Status AppendTokenRetval(const xla::XlaOp& token);
|
||||
|
||||
// Creates a resource with resource `kind` and initial value `handle`. `name`
|
||||
// is a descriptive name for use in error messages. See the `XlaResource`
|
||||
// constructor for a description of the remaining arguments.
|
||||
// Fails if the resource already exists.
|
||||
Status CreateResource(XlaResource::Kind kind, int arg_num, string name,
|
||||
DataType type, TensorShape shape,
|
||||
const xla::XlaOp& handle, int64 tensor_array_size,
|
||||
const std::set<string>& tensor_array_gradients,
|
||||
XlaResource** resource);
|
||||
|
||||
const std::vector<std::unique_ptr<XlaResource>>& resources() {
|
||||
return resources_;
|
||||
}
|
||||
|
||||
// Returns the XLA shape to be used to represent a variable of TF `shape`
|
||||
// and `type`, or of an argument or return value of a top-level computation.
|
||||
xla::StatusOr<TensorShape> RepresentationShape(const TensorShape& shape,
|
||||
DataType type) const;
|
||||
|
||||
// Get an XLA lambda to compute Max. This is cached in the
|
||||
// XlaContext since it may be used by multiple Ops. There is a
|
||||
// separate specialization of the computation for each DataType.
|
||||
const xla::XlaComputation* GetOrCreateMax(const DataType type);
|
||||
|
||||
// Get an XLA lambda to compute Min. This is cached in the
|
||||
// XlaContext since it may be used by multiple Ops. There is a
|
||||
// separate specialization of the computation for each DataType.
|
||||
const xla::XlaComputation* GetOrCreateMin(const DataType type);
|
||||
|
||||
// Get an XLA lambda to compute Add. This is cached in the
|
||||
// XlaContext since it may be used by multiple Ops. There is a
|
||||
// separate specialization of the computation for each DataType.
|
||||
const xla::XlaComputation* GetOrCreateAdd(const DataType type);
|
||||
|
||||
// Get an XLA lambda to compute Mul. This is cached in the
|
||||
// XlaContext since it may be used by multiple Ops. There is a
|
||||
// separate specialization of the computation for each DataType.
|
||||
const xla::XlaComputation* GetOrCreateMul(const DataType type);
|
||||
|
||||
// The name of the XlaContext resource during symbolic graph execution.
|
||||
static const char kXlaContextResourceName[];
|
||||
|
||||
private:
|
||||
XlaCompiler* const compiler_;
|
||||
|
||||
// The XlaBuilder used to construct the subgraph's compiled representation.
|
||||
xla::XlaBuilder* builder_;
|
||||
|
||||
// Allow ops to emit CustomCall operations for CPU.
|
||||
const bool allow_cpu_custom_calls_;
|
||||
|
||||
// If true, constant return values are returned as Tensors instead of
|
||||
// run-time computation outputs.
|
||||
const bool resolve_compile_time_constants_;
|
||||
|
||||
// Arguments to the Tensorflow graph, indexed by _Arg index.
|
||||
// Includes both compile-time constant arguments and runtime parameters.
|
||||
std::vector<XlaExpression> args_;
|
||||
|
||||
// Return values of the Tensorflow graph, indexed by _Retval index.
|
||||
std::vector<Retval> retvals_;
|
||||
|
||||
// Holds ownership of resources. The resources are not ordered.
|
||||
std::vector<std::unique_ptr<XlaResource>> resources_;
|
||||
|
||||
// Is this a top-level computation, or an inner computation (e.g., a while
|
||||
// body)?
|
||||
const bool is_entry_computation_;
|
||||
|
||||
// A function that describes how the shapes of
|
||||
// a) argument and return value, for entry computations
|
||||
// b) variables, for all computations,
|
||||
// should be represented in XLA. Parameters/return values will be shaped
|
||||
// according to this function, and reshaped back to/from their declared shapes
|
||||
// for computations. Must be non-null.
|
||||
const std::function<xla::StatusOr<TensorShape>(const TensorShape&, DataType)>*
|
||||
shape_representation_fn_;
|
||||
|
||||
// Cache of prebuilt computations indexed by their type.
|
||||
using ComputationMap = std::map<DataType, xla::XlaComputation>;
|
||||
|
||||
// Finds the value for the given type in out map if it already
|
||||
// exists or makes a new value with create function and keeps it the
|
||||
// map. The returned value != nullptr and is owned by the map.
|
||||
const xla::XlaComputation* LookupOrCreate(
|
||||
DataType type, ComputationMap* out,
|
||||
const std::function<xla::XlaComputation()>& create);
|
||||
|
||||
// Cached computation to compute Max of two elements, specialized by type.
|
||||
ComputationMap max_func_;
|
||||
|
||||
// Cached computation to compute Min of two elements, specialized by type.
|
||||
ComputationMap min_func_;
|
||||
|
||||
// Cached computation to compute Sum of two elements, specialized by type.
|
||||
ComputationMap add_func_;
|
||||
|
||||
// Cached computation to compute Mul of two elements, specialized by type.
|
||||
ComputationMap mul_func_;
|
||||
|
||||
// Cached computation to compute Sigmoid of an element, specialized by type.
|
||||
ComputationMap sigmoid_func_;
|
||||
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(XlaContext);
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_XLA_CONTEXT_H_
|
||||
@@ -0,0 +1,90 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// This file defines helper routines for the XLA device.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_TF2XLA_XLA_HELPERS_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_XLA_HELPERS_H_
|
||||
|
||||
#include "absl/types/span.h"
|
||||
#include "tensorflow/compiler/tf2xla/xla_context.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/core/framework/tensor.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Helper methods for building XLA computations.
|
||||
class XlaHelpers {
|
||||
public:
|
||||
// Returns a handle representing the zero value of a scalar
|
||||
// element of data_type.
|
||||
static xla::XlaOp Zero(xla::XlaBuilder* b, DataType data_type);
|
||||
|
||||
// Returns a handle representing the one value of a scalar
|
||||
// element of data_type.
|
||||
static xla::XlaOp One(xla::XlaBuilder* b, DataType data_type);
|
||||
|
||||
// Returns a handle representing the given value of an integer scalar
|
||||
// element of data_type.
|
||||
// Note that unlike One and Zero, does not work on boolean types.
|
||||
static xla::XlaOp IntegerLiteral(xla::XlaBuilder* b, DataType data_type,
|
||||
int64 value);
|
||||
|
||||
// Returns a handle representing the given value of a floating-point scalar
|
||||
// element of data_type.
|
||||
static xla::XlaOp FloatLiteral(xla::XlaBuilder* b, DataType data_type,
|
||||
double value);
|
||||
|
||||
// Reshapes literal 'input' to have 'shape'. Both the original shape and
|
||||
// 'shape' must contain the same number of elements.
|
||||
static Status ReshapeLiteral(const xla::Literal& input,
|
||||
absl::Span<const int64> shape,
|
||||
xla::Literal* output);
|
||||
|
||||
// Returns the argmax of `input` along `axis`. `output_type` is the type to
|
||||
// use for the output.
|
||||
static xla::XlaOp ArgMax(xla::XlaOp input, xla::PrimitiveType output_type,
|
||||
int axis);
|
||||
|
||||
// Returns the argmin of `input` along `axis`. `output_type` is the type to
|
||||
// use for the output.
|
||||
static xla::XlaOp ArgMin(xla::XlaOp input, xla::PrimitiveType output_type,
|
||||
int axis);
|
||||
|
||||
// Converts `indices` into a one-hot representation. `depth` is the size
|
||||
// of the new axis to add. `axis` is the position at which to add the new
|
||||
// axis. `indices_shape` is the shape of `indices`. `on_value` and
|
||||
// `off_value` represent the values to use for the on and off positions,
|
||||
// respectively.
|
||||
static Status OneHot(xla::XlaBuilder* builder, int64 depth, int axis,
|
||||
DataType index_type, const TensorShape& indices_shape,
|
||||
const xla::XlaOp& indices, const xla::XlaOp& on_value,
|
||||
const xla::XlaOp& off_value, xla::XlaOp* one_hot);
|
||||
|
||||
// Certain DataTypes should use increased precision DataTypes when performing
|
||||
// reductions. This function remaps a given DataType to a higher precision
|
||||
// DataType if needed.
|
||||
static DataType SumAccumulationType(const DataType& dtype);
|
||||
|
||||
// A helper for creating a ConvertElementType xla op given a DataType rather
|
||||
// than the xla::PrimitiveType.
|
||||
static xla::XlaOp ConvertElementType(xla::XlaBuilder* const builder,
|
||||
const xla::XlaOp& operand,
|
||||
const DataType new_element_type);
|
||||
};
|
||||
|
||||
} // end namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_XLA_HELPERS_H_
|
||||
@@ -0,0 +1,263 @@
|
||||
/* 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_COMPILER_TF2XLA_XLA_OP_KERNEL_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_XLA_OP_KERNEL_H_
|
||||
|
||||
#include "tensorflow/compiler/tf2xla/xla_compiler.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_computation.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
#include "tensorflow/core/framework/op_kernel.h"
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
class XlaOpKernelContext;
|
||||
|
||||
// Implementations of operators that generate XLA code should usually subclass
|
||||
// XlaOpKernel and implement the Compile() method. Unlike a regular OpKernel,
|
||||
// an XlaOpKernel produces and consumes symbolic values during compilation.
|
||||
//
|
||||
// See the comments in xla_context.h for more details.
|
||||
class XlaOpKernel : public OpKernel {
|
||||
public:
|
||||
explicit XlaOpKernel(OpKernelConstruction* construction);
|
||||
|
||||
// Subclasses should implement Compile(), much as standard OpKernels implement
|
||||
// Compute().
|
||||
virtual void Compile(XlaOpKernelContext* context) = 0;
|
||||
|
||||
private:
|
||||
void Compute(OpKernelContext* context) final;
|
||||
};
|
||||
|
||||
// The context passed to the Compile() method of XlaOpKernel. An
|
||||
// XlaOpKernelContext is a variant of the standard OpKernel class, tailored for
|
||||
// implementing operators that perform symbolic execution as part of the XLA
|
||||
// compiler. The key difference is that XlaOpKernelContext produces and consumes
|
||||
// data as XLA computations, rather than as standard Tensors.
|
||||
//
|
||||
// Under the hood, symbolic execution communicates using special Tensors that
|
||||
// wrap XlaExpression objects, however this is an implementation detail that
|
||||
// this class hides. The *only* correct way to allocate a Tensor during
|
||||
// compilation is using the XlaOpKernelContext methods, since they ensure there
|
||||
// is a valid XlaExpression backing the tensor. No Op should ever call
|
||||
// allocate_output or allocate_temp directly on the underlying OpKernelContext.
|
||||
class XlaOpKernelContext {
|
||||
public:
|
||||
explicit XlaOpKernelContext(OpKernelContext* context);
|
||||
|
||||
// Returns the XLA XlaBuilder containing the output of compilation.
|
||||
xla::XlaBuilder* builder() const;
|
||||
|
||||
// Inputs
|
||||
|
||||
// Returns the number of inputs to the operator.
|
||||
int num_inputs() const { return context_->num_inputs(); }
|
||||
|
||||
// Returns the type of input `index`.
|
||||
DataType input_type(int index) const;
|
||||
|
||||
// Returns the type of input `name`.
|
||||
DataType InputType(absl::string_view name);
|
||||
|
||||
// Returns the type of input `index` as an xla::PrimitiveType. If the type
|
||||
// is not representable as an XLA type, sets an error status and returns
|
||||
// xla::PRIMITIVE_TYPE_INVALID.
|
||||
xla::PrimitiveType input_xla_type(int index);
|
||||
|
||||
// Returns the shape of input `index`.
|
||||
TensorShape InputShape(int index);
|
||||
|
||||
// Returns the shape of input with name `name`.
|
||||
TensorShape InputShape(absl::string_view name);
|
||||
|
||||
// Returns input `index` as a XlaOp. Unlike
|
||||
// OpKernelContext::Input returns a symbolic value rather than a concrete
|
||||
// Tensor.
|
||||
const xla::XlaOp& Input(int index);
|
||||
// Returns input `name` as a XlaOp.
|
||||
const xla::XlaOp& Input(absl::string_view name);
|
||||
|
||||
// Returns true if all inputs are the same shape, otherwise sets the
|
||||
// status to a non-OK value and returns false.
|
||||
// Usage: if (!context->ValidateInputsAreSameShape(this)) return;
|
||||
bool ValidateInputsAreSameShape(OpKernel* op) TF_MUST_USE_RESULT;
|
||||
|
||||
// Returns the named list-valued immutable input in "list", as
|
||||
// defined in the OpDef. If the named output is not list-valued,
|
||||
// returns a one-element list.
|
||||
Status InputList(absl::string_view name, std::vector<xla::XlaOp>* handles,
|
||||
std::vector<TensorShape>* shapes);
|
||||
|
||||
// Helper methods for constant inputs.
|
||||
|
||||
// Evaluates input `index` and stores it in `*constant_literal`. If the
|
||||
// expression cannot be evaluated, e.g., because it depends on unbound
|
||||
// parameters, returns a non-OK status.
|
||||
Status ConstantInput(int index, xla::Literal* constant_literal);
|
||||
Status ConstantInput(absl::string_view name, xla::Literal* constant_literal);
|
||||
|
||||
// Evaluates input `index`, reshapes it to `new_shape` if new_shape !=
|
||||
// InputShape(index), and stores it in `*constant_literal`. If the input
|
||||
// cannot be evaluated, e.g., because it depends on unbound parameters,
|
||||
// returns a non-Ok status. If InputShape(index).num_elements() !=
|
||||
// new_shape.num_elements(), returns an error status.
|
||||
Status ConstantInputReshaped(int index, absl::Span<const int64> new_dims,
|
||||
xla::Literal* constant_literal);
|
||||
|
||||
// Converts a constant scalar int32 or int64 tensor into an int64.
|
||||
Status ConstantInputAsIntScalar(int index, int64* out);
|
||||
Status ConstantInputAsIntScalar(absl::string_view name, int64* out);
|
||||
|
||||
// Converts a constant scalar float32 or float64 tensor into a float64.
|
||||
Status ConstantInputAsFloatScalar(int index, double* out);
|
||||
|
||||
// Converts a constant 1D int32 or int64 tensor into a vector of int64s.
|
||||
Status ConstantInputAsIntVector(int index, std::vector<int64>* out);
|
||||
Status ConstantInputAsIntVector(absl::string_view name,
|
||||
std::vector<int64>* out);
|
||||
|
||||
// Reshapes and converts a constant int32 or int64 tensor into a vector of
|
||||
// int64s.
|
||||
Status ConstantInputReshapedToIntVector(int index, std::vector<int64>* out);
|
||||
|
||||
// Converts a constant int32 or int64 Tensor into an xla int64 Literal.
|
||||
Status ConstantInputAsInt64Literal(int index, xla::Literal* out);
|
||||
Status ConstantInputAsInt64Literal(absl::string_view name, xla::Literal* out);
|
||||
|
||||
// Converts a constant 1D int32 or int64 tensor into a TensorShape.
|
||||
Status ConstantInputAsShape(int index, TensorShape* shape);
|
||||
|
||||
// Returns the named list-valued immutable input in "list", as
|
||||
// defined in the OpDef. If the named output is not list-valued,
|
||||
// returns a one-element list.
|
||||
Status ConstantInputList(absl::string_view name,
|
||||
std::vector<xla::Literal>* literals);
|
||||
|
||||
// Outputs
|
||||
|
||||
int num_outputs() const { return context_->num_outputs(); }
|
||||
DataType expected_output_dtype(int index) const {
|
||||
return context_->expected_output_dtype(index);
|
||||
}
|
||||
|
||||
// Sets output `index` to the XlaOp `handle`.
|
||||
// All outputs should be set using SetOutput and SetConstantOutput, not
|
||||
// via the underlying OpKernelContext.
|
||||
void SetOutput(int index, const xla::XlaOp& handle);
|
||||
|
||||
// Sets output `index` to compile-time constant `host_tensor`, where
|
||||
// `host_tensor` is a tensor in host memory. It is preferable to use
|
||||
// SetConstantOutput where possible.
|
||||
void SetConstantOutput(int index, const Tensor& host_tensor);
|
||||
|
||||
// Sets output `index` to an invalid value.
|
||||
// Any subsequent attempt to consume this output will cause an error.
|
||||
void SetInvalidOutput(int index);
|
||||
|
||||
// Status handling.
|
||||
void SetStatus(const Status& status) { context_->SetStatus(status); }
|
||||
Status status() { return context_->status(); }
|
||||
|
||||
// Variables
|
||||
|
||||
// Sets `*resource` to the resource associated with input `index`.
|
||||
Status GetResourceInput(int index, XlaResource** resource);
|
||||
|
||||
// Sets output `index` to be a reference to resource `resource`.
|
||||
void SetResourceOutput(int index, XlaResource* resource);
|
||||
|
||||
// Sets `*type` and `*shape` to the current type and shape of a variable's
|
||||
// value.
|
||||
Status GetVariableTypeAndShape(int index, DataType* type,
|
||||
TensorShape* shape) const;
|
||||
|
||||
// Reads the current value of the resouce variable referred to by input
|
||||
// `index`. If `shape` is not nullptr, sets `*shape` to the shape of the
|
||||
// variable. Returns an error if the variable has not been initialized, or if
|
||||
// its type does not match `type`.
|
||||
Status ReadVariableInput(int index, DataType type, TensorShape* shape,
|
||||
xla::XlaOp* value);
|
||||
// Reads the current value of the resouce variable referred to by input
|
||||
// `name`.
|
||||
Status ReadVariableInput(absl::string_view name, DataType type,
|
||||
TensorShape* shape, xla::XlaOp* value);
|
||||
|
||||
// Assigns the value `handle` to the variable referenced by input
|
||||
// `input_index`. The variable must be of `type`. Returns an error if the
|
||||
// variable has been initialized with a different type or with a
|
||||
// different shape.
|
||||
Status AssignVariable(int input_index, DataType type, xla::XlaOp handle);
|
||||
// Assigns the value `handle` to the variable referenced by input `name`.
|
||||
Status AssignVariable(absl::string_view name, DataType type,
|
||||
xla::XlaOp handle);
|
||||
|
||||
// Helper routines for the OP_REQUIRES macros
|
||||
void CtxFailure(const Status& s);
|
||||
void CtxFailureWithWarning(const Status& s);
|
||||
void CtxFailure(const char* file, int line, const Status& s);
|
||||
void CtxFailureWithWarning(const char* file, int line, const Status& s);
|
||||
|
||||
// If this kernel invocation is within a function execution,
|
||||
// call_frame() returns the call frame for the function call.
|
||||
CallFrameInterface* call_frame() const { return context_->call_frame(); }
|
||||
|
||||
FunctionLibraryRuntime* function_library() const {
|
||||
return context_->function_library();
|
||||
}
|
||||
|
||||
const OpKernel& op_kernel() const { return context_->op_kernel(); }
|
||||
|
||||
// Returns the underlying OpKernelContext. Use rarely.
|
||||
OpKernelContext* op_kernel_context() const { return context_; }
|
||||
|
||||
// Returns the XlaCompiler that is performing the compilation. Used for, e.g.,
|
||||
// While to compile nested computations.
|
||||
XlaCompiler* compiler() const;
|
||||
|
||||
// TODO(phawkins): find a better home for these helpers.
|
||||
|
||||
// Gets an XLA lambda to compute Max. This is cached in the
|
||||
// XlaContext since it may be used by multiple Ops. There is a
|
||||
// separate specialization of the computation for each DataType.
|
||||
const xla::XlaComputation* GetOrCreateMax(const DataType type);
|
||||
|
||||
// Gets an XLA lambda to compute Min. This is cached in the
|
||||
// XlaContext since it may be used by multiple Ops. There is a
|
||||
// separate specialization of the computation for each DataType.
|
||||
const xla::XlaComputation* GetOrCreateMin(const DataType type);
|
||||
|
||||
// Gets an XLA lambda to compute Add. This is cached in the
|
||||
// XlaContext since it may be used by multiple Ops. There is a
|
||||
// separate specialization of the computation for each DataType.
|
||||
const xla::XlaComputation* GetOrCreateAdd(const DataType type);
|
||||
|
||||
// Gets an XLA lambda to compute Mul. This is cached in the
|
||||
// XlaContext since it may be used by multiple Ops. There is a
|
||||
// separate specialization of the computation for each DataType.
|
||||
const xla::XlaComputation* GetOrCreateMul(const DataType type);
|
||||
|
||||
private:
|
||||
// Returns the tensor of input `name`.
|
||||
const Tensor& GetInputTensorByName(absl::string_view name);
|
||||
|
||||
OpKernelContext* const context_;
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_XLA_OP_KERNEL_H_
|
||||
@@ -0,0 +1,317 @@
|
||||
/* 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_COMPILER_TF2XLA_XLA_OP_REGISTRY_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_XLA_OP_REGISTRY_H_
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/core/common_runtime/device_factory.h"
|
||||
#include "tensorflow/core/common_runtime/local_device.h"
|
||||
#include "tensorflow/core/framework/device_base.h"
|
||||
#include "tensorflow/core/framework/tensor.h"
|
||||
#include "tensorflow/core/framework/types.pb.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
#include "tensorflow/core/platform/mem.h"
|
||||
#include "tensorflow/core/platform/mutex.h"
|
||||
#include "tensorflow/core/platform/thread_annotations.h"
|
||||
#include "tensorflow/core/public/session_options.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Names of the XLA compilation devices. These are not user-visible, and are
|
||||
// used internally by the Tensorflow/XLA bridge to perform symbolic execution of
|
||||
// a Tensorflow graph.
|
||||
|
||||
extern const char* const DEVICE_CPU_XLA_JIT; // "CPU_XLA_JIT"
|
||||
extern const char* const DEVICE_GPU_XLA_JIT; // "GPU_XLA_JIT"
|
||||
|
||||
extern const char* const DEVICE_XLA_CPU;
|
||||
extern const char* const DEVICE_XLA_GPU;
|
||||
|
||||
constexpr std::array<DataType, 4> kFloatTypes = {
|
||||
{DT_HALF, DT_FLOAT, DT_DOUBLE, DT_BFLOAT16}};
|
||||
constexpr std::array<DataType, 11> kNumericTypes = {
|
||||
{DT_UINT8, DT_UINT32, DT_UINT64, DT_INT8, DT_INT32, DT_INT64, DT_HALF,
|
||||
DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, DT_BFLOAT16}};
|
||||
|
||||
constexpr std::array<DataType, 14> kCpuAllTypes = {
|
||||
{DT_UINT8, DT_QUINT8, DT_UINT32, DT_UINT64, DT_INT8, DT_QINT8, DT_INT32,
|
||||
DT_QINT32, DT_INT64, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, DT_BOOL}};
|
||||
|
||||
constexpr std::array<DataType, 15> kGpuAllTypes = {
|
||||
{DT_UINT8, DT_QUINT8, DT_UINT32, DT_UINT64, DT_INT8, DT_QINT8, DT_INT32,
|
||||
DT_QINT32, DT_INT64, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, DT_BOOL,
|
||||
DT_BFLOAT16}};
|
||||
|
||||
// Class that manages registrations of operators and devices for the XLA JIT.
|
||||
// Not thread-safe.
|
||||
class XlaOpRegistry {
|
||||
public:
|
||||
typedef OpKernel* (*Factory)(OpKernelConstruction*);
|
||||
|
||||
// Describes how to compile operators assigned to a device.
|
||||
struct DeviceRegistration {
|
||||
// The name of the an XLA compilation device to use to compile code.
|
||||
string compilation_device_name;
|
||||
|
||||
// Do operators assigned to this device require compilation?
|
||||
bool requires_compilation;
|
||||
|
||||
// If !requires_compilation, should we try to JIT operators on this device
|
||||
// when XLA JIT compilation is enabled globally via the SessionOptions?
|
||||
// (It is still possible to explicitly mark operators to JIT compile, even
|
||||
// if enable_jit_by_default is false.)
|
||||
bool enable_jit_by_default;
|
||||
|
||||
// Enable compilation of operators that use DT_RESOURCE types?
|
||||
bool compile_resource_ops = false;
|
||||
};
|
||||
|
||||
// Registers an XLA backend. `compilation_device_name` is the name of the
|
||||
// device used for symbolic execution during compilation. `supported_types`
|
||||
// is the list of non-resource types supported by the device. Each operators
|
||||
// will be registered for the intersection of the operator's supported types
|
||||
// and the device's supported types. `backend_op_filter` is a function used
|
||||
// to exclude or modify operator registrations on the device; it may be
|
||||
// nullptr, in which case all ops are included.
|
||||
// `backend_op_filter` should return true if the op should be registered on
|
||||
// the device; it may optionally modify the KernelDef.
|
||||
typedef bool (*BackendOpFilter)(KernelDef* kdef);
|
||||
static void RegisterBackend(const string& compilation_device_name,
|
||||
absl::Span<const DataType> supported_types,
|
||||
BackendOpFilter op_filter);
|
||||
|
||||
// Returns the names of the registered backends.
|
||||
static std::vector<string> BackendNames();
|
||||
|
||||
// Returns true iff a backend with the given name is registered.
|
||||
static bool IsBackendRegistered(const string& name);
|
||||
|
||||
// Registers `device_name` for XLA compilation, using information from
|
||||
// `registration`.
|
||||
static void RegisterCompilationDevice(const string& device_name,
|
||||
const DeviceRegistration& registration);
|
||||
|
||||
// Returns the JIT device name associated with 'device_name', setting
|
||||
// 'jit_device_name', 'requires_jit', and 'enabled_jit_by_default', if they
|
||||
// are not null. Returns false and leaves the outputs unchanged if no matching
|
||||
// JIT device is registered.
|
||||
// '*enable_jit_by_default' is set to true if we should try to JIT using this
|
||||
// device when the JIT is enabled via the Session OptimizerOptions.
|
||||
static bool GetCompilationDevice(const string& device_name,
|
||||
const DeviceRegistration** registration);
|
||||
|
||||
// Registers all JIT kernels on JIT devices, if not already registered.
|
||||
// Does nothing otherwise.
|
||||
static void RegisterCompilationKernels();
|
||||
|
||||
// Returns KernelDefs for compilation ops registered on
|
||||
// 'compilation_device_name'. Does not include kernels registered as
|
||||
// CompilationOnly, iff include_compilation_only_kernels=false.
|
||||
static std::vector<const KernelDef*> DeviceKernels(
|
||||
const string& compilation_device_name,
|
||||
bool include_compilation_only_kernels);
|
||||
|
||||
// Returns all operations for which there are XLA kernels on any device.
|
||||
static std::vector<string> GetAllRegisteredOps();
|
||||
|
||||
// Returns the set of compile-time constant inputs to 'op'. Returns nullptr
|
||||
// if the op is not registered.
|
||||
static const std::unordered_set<string>* CompileTimeConstantInputs(
|
||||
const string& op);
|
||||
|
||||
// Returns true if `op` is a "metadata" op, one that only looks at the shapes
|
||||
// of its operands and not their values.
|
||||
static bool IsMetadataOp(const string& op);
|
||||
|
||||
private:
|
||||
friend class XlaBackendRegistrar;
|
||||
friend class XlaOpRegistrar;
|
||||
friend class XlaOpRegistrationBuilder;
|
||||
|
||||
static XlaOpRegistry& Instance();
|
||||
|
||||
XlaOpRegistry();
|
||||
~XlaOpRegistry();
|
||||
|
||||
mutex mutex_;
|
||||
|
||||
// Describes an XLA backend.
|
||||
struct Backend {
|
||||
// Which types are supported by this device?
|
||||
std::set<DataType> supported_types;
|
||||
|
||||
// The per-backend operator filter function. See the comment on
|
||||
// RegisterBackend() for details.
|
||||
BackendOpFilter op_filter;
|
||||
|
||||
// KernelDefs built by RegisterCompilationKernels() for each op supported
|
||||
// by the device.
|
||||
std::vector<std::unique_ptr<KernelDef>> kernel_defs;
|
||||
};
|
||||
|
||||
// Map from compilation device names to a description of the backend.
|
||||
std::unordered_map<string, Backend> backends_ GUARDED_BY(mutex_);
|
||||
|
||||
// Map from Tensorflow device names to the corresponding JIT device metadata.
|
||||
std::unordered_map<string, DeviceRegistration> compilation_devices_
|
||||
GUARDED_BY(mutex_);
|
||||
|
||||
// A description of a Tensorflow operator that can be compiled to XLA.
|
||||
struct OpRegistration {
|
||||
string name;
|
||||
|
||||
// Should this operator be registered only on compilation devices, without a
|
||||
// dummy kernel registered on the corresponding XLA device?
|
||||
bool compilation_only = false;
|
||||
|
||||
// Should we allow resource types for type attributes? Used by _Arg to
|
||||
// allow DT_RESOURCE.
|
||||
bool allow_resource_types = false;
|
||||
|
||||
// Mapping from attribute name to a list of supported types.
|
||||
std::unordered_map<string, std::set<DataType>> type_constraints;
|
||||
|
||||
// An optional whitelist of devices. If there is no whitelist, all devices
|
||||
// are permitted.
|
||||
bool has_device_whitelist = false;
|
||||
std::unordered_set<string> device_whitelist;
|
||||
|
||||
// Names of arguments that must be compile-time constants.
|
||||
std::unordered_set<string> compile_time_constant_inputs;
|
||||
|
||||
// True if this is a "metadata" op, one that only looks at the shapes of its
|
||||
// operands and not their values.
|
||||
bool is_metadata_op = false;
|
||||
|
||||
// Factory used to build OpKernels that perform symbolic execution.
|
||||
Factory factory;
|
||||
};
|
||||
|
||||
// Returns true if registrations x and y can both be added to the registry.
|
||||
// This is always the case if they refer to different ops. If they refer to
|
||||
// the same op name, they must: have the same values for compilation_only and
|
||||
// allow_resource_types; use a device_whitelist; and their
|
||||
// whitelists must not intersect.
|
||||
static bool IsCompatible(const OpRegistration& x, const OpRegistration& y);
|
||||
|
||||
// Map from operator name to OpRegistrations, populated by REGISTER_XLA_OP.
|
||||
// Registrations present under the same key must satisfy IsCompatible above,
|
||||
// and this is checked during registration.
|
||||
std::unordered_map<string, std::vector<std::unique_ptr<OpRegistration>>> ops_
|
||||
GUARDED_BY(mutex_);
|
||||
|
||||
// Have we already registered the JIT kernels on the JIT devices?
|
||||
bool jit_kernels_registered_ = false;
|
||||
|
||||
// Holds ownership of OpKernelRegistrars that represent the Tensorflow kernel
|
||||
// registrations created by RegisterCompilationKernels() and
|
||||
// RegisterDeviceKernels().
|
||||
std::vector<std::unique_ptr<kernel_factory::OpKernelRegistrar>>
|
||||
kernel_registrars_ GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
// REGISTER_XLA_OP() registers an XLA OpKernel by name, for example:
|
||||
// REGISTER_XLA_OP(Name("Add"), AddOp);
|
||||
// where 'AddOp' is the name of a JIT OpKernel class that implements "Add".
|
||||
//
|
||||
// We don't use a variadic macro here because we don't expect JIT operators to
|
||||
// be templated.
|
||||
|
||||
#define REGISTER_XLA_OP(NAME, OP) \
|
||||
REGISTER_XLA_OP_UNIQ_HELPER(__COUNTER__, NAME, OP)
|
||||
|
||||
class XlaOpRegistrationBuilder {
|
||||
public:
|
||||
// Starts an operator registration chain.
|
||||
static XlaOpRegistrationBuilder Name(absl::string_view name);
|
||||
|
||||
// Specifies a whitelist of devices on which the operator may run.
|
||||
XlaOpRegistrationBuilder& Device(absl::string_view devices);
|
||||
XlaOpRegistrationBuilder& Device(absl::Span<const absl::string_view> devices);
|
||||
|
||||
// Specifies a type constraint for a type variable attribute. Each constraint
|
||||
// specifies the set of types that the type variable may assume.
|
||||
XlaOpRegistrationBuilder& TypeConstraint(absl::string_view attr_name,
|
||||
DataType allowed);
|
||||
|
||||
XlaOpRegistrationBuilder& TypeConstraint(absl::string_view attr_name,
|
||||
absl::Span<const DataType> allowed);
|
||||
|
||||
// Specifies that a dummy copy of this operator should not be registered on
|
||||
// XLA_* devices, but may be used during compilation.
|
||||
XlaOpRegistrationBuilder& CompilationOnly();
|
||||
|
||||
// Allow DT_RESOURCE types for type parameters.
|
||||
XlaOpRegistrationBuilder& AllowResourceTypes();
|
||||
|
||||
// Mark 'input_name' as an argument whose value must be known at compile-time.
|
||||
XlaOpRegistrationBuilder& CompileTimeConstInput(absl::string_view input_name);
|
||||
|
||||
// Mark this op as a "metadata" op, one that only looks at the shapes of its
|
||||
// operands and not their values.
|
||||
XlaOpRegistrationBuilder& IsMetadataOp();
|
||||
|
||||
std::unique_ptr<XlaOpRegistry::OpRegistration> Build(
|
||||
XlaOpRegistry::Factory factory);
|
||||
|
||||
private:
|
||||
XlaOpRegistrationBuilder(absl::string_view name);
|
||||
|
||||
std::unique_ptr<XlaOpRegistry::OpRegistration> registration_;
|
||||
};
|
||||
|
||||
// REGISTER_XLA_BACKEND() registers an XLA backend. Example usage:
|
||||
// REGISTER_XLA_BACKEND(DEVICE_GPU_XLA_JIT, kGpuAllTypes, GpuOpFilter);
|
||||
#define REGISTER_XLA_BACKEND(NAME, ...) \
|
||||
REGISTER_XLA_BACKEND_UNIQ_HELPER(__COUNTER__, NAME, __VA_ARGS__)
|
||||
|
||||
// Implementation details.
|
||||
|
||||
class XlaOpRegistrar {
|
||||
public:
|
||||
XlaOpRegistrar(std::unique_ptr<XlaOpRegistry::OpRegistration> registration);
|
||||
};
|
||||
|
||||
#define REGISTER_XLA_OP_UNIQ_HELPER(COUNTER, BUILDER, OP) \
|
||||
REGISTER_XLA_OP_UNIQ(COUNTER, BUILDER, OP)
|
||||
|
||||
#define REGISTER_XLA_OP_UNIQ(CTR, BUILDER, OP) \
|
||||
static ::tensorflow::XlaOpRegistrar xla_op_registrar__body__##CTR##__object( \
|
||||
::tensorflow::XlaOpRegistrationBuilder::BUILDER.Build( \
|
||||
[](::tensorflow::OpKernelConstruction* context) \
|
||||
-> ::tensorflow::OpKernel* { return new OP(context); }));
|
||||
|
||||
class XlaBackendRegistrar {
|
||||
public:
|
||||
XlaBackendRegistrar(absl::string_view name, absl::Span<const DataType> types,
|
||||
XlaOpRegistry::BackendOpFilter op_filter = nullptr);
|
||||
};
|
||||
|
||||
#define REGISTER_XLA_BACKEND_UNIQ_HELPER(COUNTER, NAME, ...) \
|
||||
REGISTER_XLA_BACKEND_UNIQ(COUNTER, NAME, __VA_ARGS__)
|
||||
|
||||
#define REGISTER_XLA_BACKEND_UNIQ(CTR, NAME, ...) \
|
||||
static ::tensorflow::XlaBackendRegistrar \
|
||||
xla_backend_registrar__body__##CTR##__object(NAME, __VA_ARGS__);
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_XLA_OP_REGISTRY_H_
|
||||
@@ -0,0 +1,152 @@
|
||||
/* 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_COMPILER_TF2XLA_XLA_RESOURCE_H_
|
||||
#define TENSORFLOW_COMPILER_TF2XLA_XLA_RESOURCE_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
#include "tensorflow/core/framework/tensor_shape.h"
|
||||
#include "tensorflow/core/framework/types.pb.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
|
||||
namespace tensorflow {
|
||||
|
||||
// Represents a resource, such as a Variable or TensorArray.
|
||||
class XlaResource {
|
||||
public:
|
||||
enum Kind {
|
||||
kInvalid,
|
||||
kVariable,
|
||||
kTensorArray,
|
||||
kStack,
|
||||
};
|
||||
|
||||
XlaResource(Kind kind, int arg_num, string name, DataType type,
|
||||
TensorShape shape, const xla::XlaOp& initial_value,
|
||||
int64 tensor_array_size,
|
||||
const std::set<string>& tensor_array_gradients);
|
||||
|
||||
XlaResource(const XlaResource&) = delete;
|
||||
XlaResource(XlaResource&&) = delete;
|
||||
XlaResource& operator=(const XlaResource&) = delete;
|
||||
XlaResource& operator=(XlaResource&&) = delete;
|
||||
|
||||
Kind kind() const { return kind_; }
|
||||
|
||||
// If this resource is visible externally to the computation, what was its
|
||||
// argument number?
|
||||
// < 0 means "not visible externally".
|
||||
int arg_num() const { return arg_num_; }
|
||||
|
||||
// A descriptive name for the resource, used in error messages.
|
||||
const string& name() const { return name_; }
|
||||
|
||||
// Current type and value of the resource. Uninitialized resources are
|
||||
// represented by a default (zero) handle and type DT_INVALID.
|
||||
// While the type of a resource is notionally fixed during execution, when
|
||||
// a resource is first initialized we do not yet know its type, so we keep
|
||||
// track of its type dynamically.
|
||||
DataType type() const { return type_; }
|
||||
|
||||
// Shape of the resource. For an uninitialized resource, this is ignored.
|
||||
// For a Variable, this is the shape of the value. For a TensorArray or Stack
|
||||
// this is the shape of each entry in the TensorArray/Stack.
|
||||
const TensorShape& shape() const { return shape_; }
|
||||
|
||||
const xla::XlaOp& value() const { return value_; }
|
||||
|
||||
// Value of the resource at computation entry. Used to detect which
|
||||
// variables have new values that need to be written back.
|
||||
const xla::XlaOp& initial_value() const { return initial_value_; }
|
||||
|
||||
// A variable is initialized if it has a value.
|
||||
bool initialized() const { return value_.valid(); }
|
||||
|
||||
// Sets the type and shape of the resource. The type and shape of a resource
|
||||
// must not change once the variable has been initialized.
|
||||
Status SetTypeAndShape(DataType type, const TensorShape& shape);
|
||||
|
||||
// Sets the current value of the resource. Returns an error if the type is not
|
||||
// set to a valid value.
|
||||
Status SetValue(const xla::XlaOp& value);
|
||||
|
||||
// Sets the current value of the resource to an all-zero value.
|
||||
Status SetZeroValue(xla::XlaBuilder* builder);
|
||||
|
||||
// Looks up the gradient for `source`, or creates it if it does not already
|
||||
// exist. The call target must be an initialized TensorArray resource. A
|
||||
// TensorArray can have multiple named gradients; see the operator
|
||||
// documentation for TensorArrayGradV3 for details.
|
||||
Status GetOrCreateTensorArrayGradient(const string& source,
|
||||
xla::XlaBuilder* builder,
|
||||
XlaResource** gradient_out);
|
||||
|
||||
// Packs a resource into a single XLA value `pack`, suitable for use as
|
||||
// an XlaCompiler::Argument. For non-TensorArrays or TensorArrays without
|
||||
// gradients, sets `*pack` to `value`.
|
||||
// For TensorArrays with gradients, packs the value and its gradient values in
|
||||
// a tuple; the gradients values are packed in order by source name.
|
||||
Status Pack(xla::XlaOp* pack, xla::XlaBuilder* builder) const;
|
||||
|
||||
// Updates the resource with values from `pack`. If `gradient_sources` is
|
||||
// non-empty, treats `pack` as a tuple that represents a TensorArray and
|
||||
// its gradients, and unpacks and updates the gradient resources.
|
||||
// If `reset_initial_values` is true, sets the initial_values as well as the
|
||||
// values.
|
||||
// Opposite of Pack().
|
||||
Status SetFromPack(const std::set<string>& gradient_sources,
|
||||
const xla::XlaOp& pack, xla::XlaBuilder* builder);
|
||||
|
||||
// TensorArray and Stack specific fields
|
||||
|
||||
// 'tensor_array_size' stores the expected size of the TensorArray or Stack.
|
||||
// We need to store this since sometimes TensorArrays must be initialized
|
||||
// lazily since we do not know the element shape at construction time.
|
||||
// Used by both TensorArrays and Stacks.
|
||||
int64 tensor_array_size() const { return tensor_array_size_; }
|
||||
void set_tensor_array_size(int64 size) { tensor_array_size_ = size; }
|
||||
|
||||
// 'tensor_array_gradient' is a map from TensorArrayGradV3 'source' attributes
|
||||
// to an XlaResource containing the gradient TensorArrays. We store a pointer
|
||||
// here since there should only be one gradient TensorArray per 'source'
|
||||
// string, irrespective of the number of calls to TensorArrayGrad. The map
|
||||
// is ordered since values are packed into tuples by Pack() sorted by name
|
||||
// order.
|
||||
const std::map<string, std::unique_ptr<XlaResource>>& tensor_array_gradients()
|
||||
const {
|
||||
return tensor_array_gradients_;
|
||||
}
|
||||
|
||||
private:
|
||||
const Kind kind_;
|
||||
const int arg_num_;
|
||||
const string name_;
|
||||
|
||||
DataType type_;
|
||||
TensorShape shape_;
|
||||
xla::XlaOp value_;
|
||||
xla::XlaOp initial_value_;
|
||||
|
||||
int64 tensor_array_size_ = -1;
|
||||
|
||||
std::map<string, std::unique_ptr<XlaResource>> tensor_array_gradients_;
|
||||
};
|
||||
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_TF2XLA_XLA_RESOURCE_H_
|
||||
@@ -0,0 +1,571 @@
|
||||
/* 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_COMPILER_XLA_ARRAY_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_ARRAY_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <initializer_list>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <numeric>
|
||||
#include <random>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/str_join.h"
|
||||
#include "absl/types/span.h"
|
||||
#include "tensorflow/compiler/xla/status.h"
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
#include "tensorflow/core/lib/core/bits.h"
|
||||
#include "tensorflow/core/platform/logging.h"
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
namespace array_impl {
|
||||
|
||||
// conjunction
|
||||
//
|
||||
// Performs a compile-time logical AND operation on the passed types (which
|
||||
// must have `::value` members convertible to `bool`. Short-circuits if it
|
||||
// encounters any `false` members (and does not compare the `::value` members
|
||||
// of any remaining arguments).
|
||||
//
|
||||
// This metafunction is designed to be a drop-in replacement for the C++17
|
||||
// `std::conjunction` metafunction.
|
||||
template <typename... Ts>
|
||||
struct conjunction;
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
struct conjunction<T, Ts...>
|
||||
: std::conditional<T::value, conjunction<Ts...>, T>::type {};
|
||||
|
||||
template <>
|
||||
struct conjunction<> : std::true_type {};
|
||||
|
||||
// A type trait that is valid when all elements in a parameter pack are of
|
||||
// integral type.
|
||||
template <typename... T>
|
||||
using pack_is_integral = conjunction<std::is_integral<T>...>;
|
||||
|
||||
// Compares three same-sized vectors elementwise. For each item in `values`,
|
||||
// returns false if any of values[i] is outside the half-open range [starts[i],
|
||||
// ends[i]).
|
||||
template <typename C1, typename C2, typename C3>
|
||||
bool all_inside_range(const C1& values, const C2& range_starts,
|
||||
const C3& range_ends) {
|
||||
for (size_t i = 0, e = values.size(); i < e; ++i) {
|
||||
if (values[i] < range_starts[i] || values[i] >= range_ends[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace array_impl
|
||||
|
||||
// General N dimensional array class with arbitrary value type.
|
||||
template <typename T>
|
||||
class Array {
|
||||
public:
|
||||
// Type inference can have a hard time parsing very deep initializer list
|
||||
// nests, especially if one or more dimensions is one as the compiler just
|
||||
// sees a single-element integer initializer. These typedefs allow casting
|
||||
// explicitly with less typing.
|
||||
using InitializerList1D = std::initializer_list<T>;
|
||||
using InitializerList2D = std::initializer_list<InitializerList1D>;
|
||||
using InitializerList3D = std::initializer_list<InitializerList2D>;
|
||||
using InitializerList4D = std::initializer_list<InitializerList3D>;
|
||||
|
||||
using value_type = T;
|
||||
|
||||
// Creates a new array with the specified dimensions.
|
||||
explicit Array(absl::Span<const int64> sizes) : Array(sizes, T()) {}
|
||||
|
||||
// Creates a new array with the specified dimensions and specified value for
|
||||
// every cell.
|
||||
Array(absl::Span<const int64> sizes, T value)
|
||||
: sizes_(sizes.begin(), sizes.end()), values_(new T[num_elements()]) {
|
||||
Fill(value);
|
||||
}
|
||||
|
||||
// Creates a 2D array from the given nested initializer list. The outer
|
||||
// initializer list is the first dimension, the inner is the second dimension.
|
||||
// For example, {{1, 2, 3}, {4, 5, 6}} results in an array with n1=2 and n2=3.
|
||||
Array(InitializerList2D values)
|
||||
: Array(ToInt64Vector({values.size(), values.begin()->size()})) {
|
||||
int64 idx = 0;
|
||||
for (const auto& it1 : values) {
|
||||
for (const auto& it2 : it1) {
|
||||
values_[idx] = it2;
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
CHECK(idx == num_elements());
|
||||
}
|
||||
|
||||
// Creates a 1D array of a floating-point type (half, bfloat16, float,
|
||||
// or double) from an initializer list of float values.
|
||||
template <typename T2, typename = typename std::enable_if<
|
||||
(std::is_same<T, Eigen::half>::value ||
|
||||
std::is_same<T, bfloat16>::value ||
|
||||
std::is_same<T, float>::value ||
|
||||
std::is_same<T, double>::value) &&
|
||||
std::is_same<T2, float>::value>::type>
|
||||
Array(std::initializer_list<T2> values)
|
||||
: Array(ToInt64Vector({values.size()})) {
|
||||
int64 idx = 0;
|
||||
for (const auto& it1 : values) {
|
||||
values_[idx] = static_cast<T>(it1);
|
||||
++idx;
|
||||
}
|
||||
CHECK(idx == num_elements());
|
||||
}
|
||||
|
||||
// Creates a 2D array of a floating-point type (half, bfloat16, float,
|
||||
// or double) from an initializer list of float values.
|
||||
template <typename T2, typename = typename std::enable_if<
|
||||
(std::is_same<T, Eigen::half>::value ||
|
||||
std::is_same<T, bfloat16>::value ||
|
||||
std::is_same<T, float>::value ||
|
||||
std::is_same<T, double>::value) &&
|
||||
std::is_same<T2, float>::value>::type>
|
||||
Array(std::initializer_list<std::initializer_list<T2>> values)
|
||||
: Array(ToInt64Vector({values.size(), values.begin()->size()})) {
|
||||
int64 idx = 0;
|
||||
for (const auto& it1 : values) {
|
||||
for (const auto& it2 : it1) {
|
||||
values_[idx] = static_cast<T>(it2);
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
CHECK(idx == num_elements());
|
||||
}
|
||||
|
||||
// Creates a 3D array from the given nested initializer list. The outer
|
||||
// initializer list is the first dimension, and so on.
|
||||
Array(InitializerList3D values)
|
||||
: Array(ToInt64Vector({values.size(), values.begin()->size(),
|
||||
values.begin()->begin()->size()})) {
|
||||
int64 idx = 0;
|
||||
for (const auto& it1 : values) {
|
||||
for (const auto& it2 : it1) {
|
||||
for (const auto& it3 : it2) {
|
||||
values_[idx] = it3;
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
CHECK(idx == num_elements());
|
||||
}
|
||||
|
||||
// Creates a 3D array of a floating-point type (half, bfloat16, float,
|
||||
// or double) from an initializer list of float values.
|
||||
template <typename T2, typename = typename std::enable_if<
|
||||
(std::is_same<T, Eigen::half>::value ||
|
||||
std::is_same<T, bfloat16>::value ||
|
||||
std::is_same<T, float>::value ||
|
||||
std::is_same<T, double>::value) &&
|
||||
std::is_same<T2, float>::value>::type>
|
||||
Array(std::initializer_list<std::initializer_list<std::initializer_list<T2>>>
|
||||
values)
|
||||
: Array(ToInt64Vector({values.size(), values.begin()->size(),
|
||||
values.begin()->begin()->size()})) {
|
||||
int64 idx = 0;
|
||||
for (const auto& it1 : values) {
|
||||
for (const auto& it2 : it1) {
|
||||
for (const auto& it3 : it2) {
|
||||
values_[idx] = static_cast<T>(it3);
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
CHECK(idx == num_elements());
|
||||
}
|
||||
|
||||
// Creates a 4D array from the given nested initializer list. The outer
|
||||
// initializer list is the first dimension, and so on.
|
||||
Array(InitializerList4D values)
|
||||
: Array(ToInt64Vector({values.size(), values.begin()->size(),
|
||||
values.begin()->begin()->size(),
|
||||
values.begin()->begin()->begin()->size()})) {
|
||||
int64 idx = 0;
|
||||
for (const auto& it1 : values) {
|
||||
for (const auto& it2 : it1) {
|
||||
for (const auto& it3 : it2) {
|
||||
for (const auto& it4 : it3) {
|
||||
values_[idx] = it4;
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
CHECK(idx == num_elements());
|
||||
}
|
||||
|
||||
// Creates a 4D array of a floating-point type (half, bfloat16, float,
|
||||
// or double) from an initializer list of float values.
|
||||
template <typename T2, typename = typename std::enable_if<
|
||||
(std::is_same<T, Eigen::half>::value ||
|
||||
std::is_same<T, bfloat16>::value ||
|
||||
std::is_same<T, float>::value ||
|
||||
std::is_same<T, double>::value) &&
|
||||
std::is_same<T2, float>::value>::type>
|
||||
Array(std::initializer_list<
|
||||
std::initializer_list<std::initializer_list<std::initializer_list<T2>>>>
|
||||
values)
|
||||
: Array(ToInt64Vector({values.size(), values.begin()->size(),
|
||||
values.begin()->begin()->size(),
|
||||
values.begin()->begin()->begin()->size()})) {
|
||||
int64 idx = 0;
|
||||
for (const auto& it1 : values) {
|
||||
for (const auto& it2 : it1) {
|
||||
for (const auto& it3 : it2) {
|
||||
for (const auto& it4 : it3) {
|
||||
values_[idx] = static_cast<T>(it4);
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
CHECK(idx == num_elements());
|
||||
}
|
||||
|
||||
Array(const Array<T>& other)
|
||||
: sizes_(other.sizes_), values_(new T[num_elements()]) {
|
||||
std::copy(&other.values_[0], &other.values_[0] + num_elements(),
|
||||
&values_[0]);
|
||||
}
|
||||
|
||||
Array<T>& operator=(const Array<T>& other) {
|
||||
sizes_ = other.sizes_;
|
||||
values_.reset(new T[num_elements()]);
|
||||
std::copy(&other.values_[0], &other.values_[0] + num_elements(),
|
||||
&values_[0]);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Fills the array with the specified value.
|
||||
void Fill(const T& value) {
|
||||
std::fill(&values_[0], &values_[0] + num_elements(), value);
|
||||
}
|
||||
|
||||
// Fills the array with sequentially increasing values.
|
||||
void FillIota(const T& value) {
|
||||
std::iota(&values_[0], &values_[0] + num_elements(), value);
|
||||
}
|
||||
|
||||
// Fills the array with the sequence i*multiplier for i=0,1,...
|
||||
void FillWithMultiples(const T& multiplier) {
|
||||
for (int64 i = 0; i < num_elements(); ++i) {
|
||||
values_[i] = static_cast<T>(i) * multiplier;
|
||||
}
|
||||
}
|
||||
|
||||
// Fills the array with random normal variables with the specified mean.
|
||||
void FillRandom(const T& value, const double mean = 0.0,
|
||||
const int seed = 12345) {
|
||||
std::mt19937 g(seed);
|
||||
std::normal_distribution<double> distribution(mean,
|
||||
static_cast<double>(value));
|
||||
for (int64 i = 0; i < num_elements(); ++i) {
|
||||
values_[i] = static_cast<T>(distribution(g));
|
||||
}
|
||||
}
|
||||
|
||||
// Sets all the values in the array to values specified in the container.
|
||||
template <typename Container = std::initializer_list<T>>
|
||||
void SetValues(const Container& container) {
|
||||
CHECK_EQ(std::distance(std::begin(container), std::end(container)),
|
||||
num_elements());
|
||||
std::copy(std::begin(container), std::end(container), &values_[0]);
|
||||
}
|
||||
|
||||
// Invokes a callback with the (indices, value_ptr) for each cell in the
|
||||
// array.
|
||||
void Each(std::function<void(absl::Span<const int64>, T*)> f) {
|
||||
std::vector<int64> index(sizes_.size());
|
||||
for (int64 i = 0; i < num_elements(); ++i, next_index(&index)) {
|
||||
f(index, &values_[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Invokes a callback with the (indices, value) for each cell in the array.
|
||||
void Each(std::function<void(absl::Span<const int64>, T)> f) const {
|
||||
std::vector<int64> index(sizes_.size());
|
||||
for (int64 i = 0; i < num_elements(); ++i, next_index(&index)) {
|
||||
f(index, values_[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Invokes a callback with the (indices, value_ptr) for each cell in the
|
||||
// array. If a callback returns a non-OK status, returns that else returns
|
||||
// Status::OK().
|
||||
Status EachStatus(std::function<Status(absl::Span<const int64>, T*)> f) {
|
||||
std::vector<int64> index(sizes_.size());
|
||||
for (int64 i = 0; i < num_elements(); ++i, next_index(&index)) {
|
||||
Status s = f(index, &values_[i]);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
// Invokes a callback with the (indices, value) for each cell in the array.
|
||||
// If a callback returns a non-OK status, returns that else returns
|
||||
// Status::OK().
|
||||
Status EachStatus(std::function<Status(absl::Span<const int64>, T)> f) const {
|
||||
std::vector<int64> index(sizes_.size());
|
||||
for (int64 i = 0; i < num_elements(); ++i, next_index(&index)) {
|
||||
Status s = f(index, values_[i]);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
// Returns the value at the cell specified by the indexes. The number of
|
||||
// arguments have to match with the number of dimensions for the array.
|
||||
//
|
||||
// The type trait is required to avoid this overload participating too
|
||||
// eagerly; a parameter pack can take zero or more elements, so we must
|
||||
// restrict this to only parameter packs that are all of integral type.
|
||||
template <typename... Dims>
|
||||
typename std::enable_if<array_impl::pack_is_integral<Dims...>::value,
|
||||
const T&>::type
|
||||
operator()(Dims... dims) const {
|
||||
// We are using a std::array to avoid having to allocate memory in this
|
||||
// function for performance reasons.
|
||||
std::array<int64, sizeof...(dims)> indexes{{static_cast<int64>(dims)...}};
|
||||
return values_[calculate_index(indexes)];
|
||||
}
|
||||
|
||||
// Returns the value at the cell specified by the indexes. The number of
|
||||
// arguments have to match with the number of dimensions for the array.
|
||||
template <typename... Dims>
|
||||
typename std::enable_if<array_impl::pack_is_integral<Dims...>::value,
|
||||
T&>::type
|
||||
operator()(Dims... dims) {
|
||||
// We are using a std::array to avoid having to allocate memory in this
|
||||
// function for performance reasons.
|
||||
std::array<int64, sizeof...(dims)> indexes{{static_cast<int64>(dims)...}};
|
||||
return values_[calculate_index(indexes)];
|
||||
}
|
||||
|
||||
// Returns the value at the cell specified by the indexes. The number of
|
||||
// arguments have to match with the number of dimensions for the array.
|
||||
const T& operator()(absl::Span<const int64> indexes) const {
|
||||
return values_[calculate_index(indexes)];
|
||||
}
|
||||
|
||||
// Returns the value at the cell specified by the indexes. The number of
|
||||
// arguments have to match with the number of dimensions for the array.
|
||||
T& operator()(absl::Span<const int64> indexes) {
|
||||
return values_[calculate_index(indexes)];
|
||||
}
|
||||
|
||||
// Low-level accessor for stuff like memcmp, handle with care. Returns pointer
|
||||
// to the underlying storage of the array (similarly to std::vector::data()).
|
||||
T* data() const {
|
||||
// TODO(tberghammer): Get rid of the const_cast. Currently it is needed
|
||||
// because the Eigen backend needs a non-const pointers even for reading
|
||||
// from the array.
|
||||
return const_cast<Array*>(this)->values_.get();
|
||||
}
|
||||
|
||||
// Returns the size of the dimension at the given index.
|
||||
int64 dim(int64 n) const {
|
||||
CHECK(n < sizes_.size());
|
||||
return sizes_[n];
|
||||
}
|
||||
|
||||
// Returns a vector containing the dimensions of the array.
|
||||
const std::vector<int64>& dimensions() const { return sizes_; }
|
||||
|
||||
int64 num_dimensions() const { return sizes_.size(); }
|
||||
|
||||
// Returns the total number of elements in the array.
|
||||
int64 num_elements() const {
|
||||
return std::accumulate(sizes_.begin(), sizes_.end(), 1LL,
|
||||
std::multiplies<int64>());
|
||||
}
|
||||
|
||||
const T* begin() const { return &values_[0]; }
|
||||
T* begin() { return &values_[0]; }
|
||||
const T* end() const { return &values_[num_elements()]; }
|
||||
T* end() { return &values_[num_elements()]; }
|
||||
|
||||
bool operator==(const Array<T>& other) const {
|
||||
if (sizes_.size() != other.sizes_.size()) {
|
||||
return false;
|
||||
}
|
||||
for (int64 i = 0; i < sizes_.size(); ++i) {
|
||||
if (sizes_[i] != other.sizes_[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (int64 i = 0; i < num_elements(); ++i) {
|
||||
if (values_[i] != other.values_[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator!=(const Array<T>& other) const { return !(*this == other); }
|
||||
|
||||
// Performs the equivalent of a slice operation on this array.
|
||||
Array<T> Slice(absl::Span<const int64> starts,
|
||||
absl::Span<const int64> limits) const {
|
||||
CHECK_EQ(starts.size(), num_dimensions());
|
||||
CHECK_EQ(limits.size(), num_dimensions());
|
||||
|
||||
std::vector<int64> sizes;
|
||||
std::transform(starts.begin(), starts.end(), limits.begin(),
|
||||
std::back_inserter(sizes),
|
||||
[](int64 start, int64 limit) { return limit - start; });
|
||||
Array<T> result(sizes);
|
||||
|
||||
std::vector<int64> index(sizes_.size());
|
||||
int64 slice_i = 0;
|
||||
for (int64 i = 0; i < num_elements(); ++i, next_index(&index)) {
|
||||
if (array_impl::all_inside_range(index, starts, limits)) {
|
||||
// Even though the bounds of result are different to our bounds, we're
|
||||
// iterating in the same order. So we can simply write successive linear
|
||||
// indices instead of recalculating a multi-dimensional index.
|
||||
result.values_[slice_i++] = values_[i];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Performs the equivalent of a DynamicUpdateSlice in-place on this array.
|
||||
void UpdateSlice(const Array<T>& from,
|
||||
absl::Span<const int64> start_indices) {
|
||||
CHECK_EQ(from.num_dimensions(), num_dimensions());
|
||||
std::vector<int64> limit_indices;
|
||||
std::transform(start_indices.begin(), start_indices.end(),
|
||||
from.dimensions().begin(), std::back_inserter(limit_indices),
|
||||
std::plus<int64>{});
|
||||
std::vector<int64> index(sizes_.size());
|
||||
int64 from_i = 0;
|
||||
for (int64 i = 0; i < num_elements(); ++i, next_index(&index)) {
|
||||
if (array_impl::all_inside_range(index, start_indices, limit_indices)) {
|
||||
// Even though the bounds of from are different to our bounds, we're
|
||||
// iterating in the same order. So we can simply write successive linear
|
||||
// indices instead of recalculating a multi-dimensional index.
|
||||
values_[i] = from.values_[from_i++];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Performs an in-place reshape, modifying the dimensions but not the
|
||||
// underlying data.
|
||||
void Reshape(absl::Span<const int64> new_dimensions) {
|
||||
int64 old_num_elements = num_elements();
|
||||
sizes_ = std::vector<int64>(new_dimensions.begin(), new_dimensions.end());
|
||||
CHECK_EQ(num_elements(), old_num_elements);
|
||||
}
|
||||
|
||||
// Returns a string representation of the array suitable for debugging.
|
||||
string ToString() const {
|
||||
std::vector<string> pieces;
|
||||
std::vector<int64> index(sizes_.size());
|
||||
do {
|
||||
// Emit leading spaces and opening square brackets
|
||||
if (index.back() == 0) {
|
||||
for (int64 i = sizes_.size() - 1; i >= 0; --i) {
|
||||
if (i == 0 || index[i - 1] != 0) {
|
||||
for (int64 j = 0; j < sizes_.size(); ++j) {
|
||||
pieces.push_back(j < i ? " " : "[");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pieces.push_back(absl::StrCat(values_[calculate_index(index)]));
|
||||
|
||||
// Emit comma if it isn't the last element
|
||||
if (index.back() != sizes_.back() - 1) {
|
||||
pieces.push_back(", ");
|
||||
}
|
||||
|
||||
// Emit closing square brackets
|
||||
for (int64 i = sizes_.size() - 1; i >= 0; --i) {
|
||||
if (index[i] != sizes_[i] - 1) {
|
||||
break;
|
||||
}
|
||||
pieces.push_back("]");
|
||||
if (i != 0 && index[i - 1] != sizes_[i - 1] - 1) {
|
||||
pieces.push_back(",\n");
|
||||
}
|
||||
}
|
||||
} while (next_index(&index));
|
||||
return absl::StrJoin(pieces, "");
|
||||
}
|
||||
|
||||
private:
|
||||
// Converts an initializer_list of type U to a vector of type int64. Used by
|
||||
// the initializer list based constructors to convert the size type into int64
|
||||
// to be passed to the size based constructor.
|
||||
template <typename U>
|
||||
static std::vector<int64> ToInt64Vector(
|
||||
const std::initializer_list<U>& data) {
|
||||
return std::vector<int64>(data.begin(), data.end());
|
||||
}
|
||||
|
||||
// Returns the linear index from the list of per-dimension indexes. Function
|
||||
// is templated so can be used with an std::array from operator() to avoid
|
||||
// memory allocation.
|
||||
template <typename U>
|
||||
int64 calculate_index(const U& indexes) const {
|
||||
CHECK_EQ(sizes_.size(), indexes.size());
|
||||
int64 index = 0;
|
||||
for (int64 i = 0; i < sizes_.size(); ++i) {
|
||||
index *= sizes_[i];
|
||||
index += indexes[i];
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
// Advances the specified set of indexes and returns true if we haven't
|
||||
// wrapped around (i.e. result isn't {0, 0, ...}).
|
||||
bool next_index(std::vector<int64>* index) const {
|
||||
CHECK_EQ(index->size(), sizes_.size());
|
||||
for (int64 i = sizes_.size() - 1; i >= 0; --i) {
|
||||
(*index)[i]++;
|
||||
if ((*index)[i] < sizes_[i]) {
|
||||
return true;
|
||||
}
|
||||
(*index)[i] = 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<int64> sizes_;
|
||||
std::unique_ptr<T[]> values_;
|
||||
};
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_ARRAY_H_
|
||||
@@ -0,0 +1,119 @@
|
||||
/* 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_COMPILER_XLA_ARRAY2D_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_ARRAY2D_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <initializer_list>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/memory/memory.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "tensorflow/compiler/xla/array.h"
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
#include "tensorflow/core/lib/core/bits.h"
|
||||
#include "tensorflow/core/platform/logging.h"
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
template <typename T>
|
||||
class Array2D : public Array<T> {
|
||||
public:
|
||||
Array2D() : Array<T>(std::vector<int64>{0, 0}) {}
|
||||
|
||||
Array2D(const int64 n1, const int64 n2)
|
||||
: Array<T>(std::vector<int64>{n1, n2}) {}
|
||||
|
||||
Array2D(const int64 n1, const int64 n2, const T value)
|
||||
: Array<T>({n1, n2}, value) {}
|
||||
|
||||
// Creates an array from the given nested initializer list. The outer
|
||||
// initializer list is the first dimension; the inner is the second dimension.
|
||||
// For example, {{1, 2, 3}, {4, 5, 6}} results in an array with n1=2 and n2=3.
|
||||
Array2D(std::initializer_list<std::initializer_list<T>> values)
|
||||
: Array<T>(values) {}
|
||||
|
||||
// Creates an array of a floating-point type (half, bfloat16, float,
|
||||
// or double) from the given nested initializer list of float values.
|
||||
template <typename T2, typename = typename std::enable_if<
|
||||
(std::is_same<T, Eigen::half>::value ||
|
||||
std::is_same<T, bfloat16>::value ||
|
||||
std::is_same<T, float>::value ||
|
||||
std::is_same<T, double>::value) &&
|
||||
std::is_same<T2, float>::value>::type>
|
||||
Array2D(std::initializer_list<std::initializer_list<T2>> values)
|
||||
: Array<T>(values) {}
|
||||
|
||||
Array2D(const Array2D<T>& other) : Array<T>(other) {}
|
||||
|
||||
int64 n1() const { return this->dim(0); }
|
||||
int64 n2() const { return this->dim(1); }
|
||||
|
||||
int64 height() const { return this->dim(0); }
|
||||
int64 width() const { return this->dim(1); }
|
||||
|
||||
// Fills the array with a pattern of values of the form:
|
||||
//
|
||||
// (rowno << log2ceil(width) | colno) + start_value
|
||||
//
|
||||
// This makes it easy to see distinct row/column values in the array.
|
||||
void FillUnique(T start_value = 0) {
|
||||
for (int64 i0 = 0; i0 < n1(); ++i0) {
|
||||
for (int64 i1 = 0; i1 < n2(); ++i1) {
|
||||
(*this)(i0, i1) =
|
||||
((i0 << tensorflow::Log2Ceiling64(n2())) | i1) + start_value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Applies f to all cells in this array, in row-major order.
|
||||
void Each(std::function<void(int64, int64, T*)> f) {
|
||||
for (int64 i0 = 0; i0 < n1(); ++i0) {
|
||||
for (int64 i1 = 0; i1 < n2(); ++i1) {
|
||||
f(i0, i1, &(*this)(i0, i1));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Returns a linspace-populated Array2D in the range [from, to] (inclusive)
|
||||
// with dimensions n1 x n2.
|
||||
template <typename NativeT = float>
|
||||
std::unique_ptr<Array2D<NativeT>> MakeLinspaceArray2D(double from, double to,
|
||||
int64 n1, int64 n2) {
|
||||
auto array = absl::make_unique<Array2D<NativeT>>(n1, n2);
|
||||
int64 count = n1 * n2;
|
||||
NativeT step =
|
||||
static_cast<NativeT>((count > 1) ? (to - from) / (count - 1) : 0);
|
||||
auto set = [&array, n1, n2](int64 index, NativeT value) {
|
||||
(*array)(index / n2, index % n2) = value;
|
||||
};
|
||||
for (int64 i = 0; i < count - 1; ++i) {
|
||||
set(i, (static_cast<NativeT>(from) +
|
||||
static_cast<NativeT>(i) * static_cast<NativeT>(step)));
|
||||
}
|
||||
set(count - 1, static_cast<NativeT>(to));
|
||||
return array;
|
||||
}
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_ARRAY2D_H_
|
||||
@@ -0,0 +1,80 @@
|
||||
/* 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_COMPILER_XLA_ARRAY3D_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_ARRAY3D_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <initializer_list>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <numeric>
|
||||
#include <random>
|
||||
|
||||
#include "tensorflow/compiler/xla/array.h"
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
#include "tensorflow/core/platform/logging.h"
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Simple 3D array structure.
|
||||
template <typename T>
|
||||
class Array3D : public Array<T> {
|
||||
public:
|
||||
Array3D() : Array<T>(std::vector<int64>{0, 0, 0}) {}
|
||||
|
||||
// Creates an array of dimensions n1 x n2 x n3, uninitialized values.
|
||||
Array3D(const int64 n1, const int64 n2, const int64 n3)
|
||||
: Array<T>(std::vector<int64>{n1, n2, n3}) {}
|
||||
|
||||
// Creates an array of dimensions n1 x n2 x n3, initialized to value.
|
||||
Array3D(const int64 n1, const int64 n2, const int64 n3, const T value)
|
||||
: Array<T>(std::vector<int64>{n1, n2, n3}, value) {}
|
||||
|
||||
// Creates an array from the given nested initializer list. The outer
|
||||
// initializer list is the first dimension, and so on.
|
||||
//
|
||||
// For example {{{1, 2}, {3, 4}, {5, 6}, {7, 8}},
|
||||
// {{9, 10}, {11, 12}, {13, 14}, {15, 16}},
|
||||
// {{17, 18}, {19, 20}, {21, 22}, {23, 24}}}
|
||||
// results in an array with n1=3, n2=4, n3=2.
|
||||
Array3D(std::initializer_list<std::initializer_list<std::initializer_list<T>>>
|
||||
values)
|
||||
: Array<T>(values) {}
|
||||
|
||||
// Creates an array of a floating-point type (half, bfloat16, float,
|
||||
// or double) from the given nested initializer list of float values.
|
||||
template <typename T2, typename = typename std::enable_if<
|
||||
(std::is_same<T, Eigen::half>::value ||
|
||||
std::is_same<T, bfloat16>::value ||
|
||||
std::is_same<T, float>::value ||
|
||||
std::is_same<T, double>::value) &&
|
||||
std::is_same<T2, float>::value>::type>
|
||||
Array3D(
|
||||
std::initializer_list<std::initializer_list<std::initializer_list<T2>>>
|
||||
values)
|
||||
: Array<T>(values) {}
|
||||
|
||||
int64 n1() const { return this->dim(0); }
|
||||
int64 n2() const { return this->dim(1); }
|
||||
int64 n3() const { return this->dim(2); }
|
||||
};
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_ARRAY3D_H_
|
||||
@@ -0,0 +1,160 @@
|
||||
/* 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_COMPILER_XLA_ARRAY4D_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_ARRAY4D_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <initializer_list>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <numeric>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/types/span.h"
|
||||
#include "tensorflow/compiler/xla/array.h"
|
||||
#include "tensorflow/compiler/xla/array2d.h"
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
#include "tensorflow/core/platform/logging.h"
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Simple 4D array structure, similar in form to Array2D, for use primarily in
|
||||
// testing and describing to XLA APIs values in the 4D array structures used
|
||||
// in convolutions.
|
||||
//
|
||||
// The data layout is, in order from major to minor:
|
||||
//
|
||||
// First dimension: plane, batch, n1
|
||||
// Second dimension: depth, feature, z, n2
|
||||
// Third dimension: height, y, n3
|
||||
// Fourth dimension: width, x, n4
|
||||
//
|
||||
// These dimensions are referred to by various names, so that is why
|
||||
// more than one name is given above. See operator() for the exact
|
||||
// calculation of 1d indices from 4d indices.
|
||||
template <typename T>
|
||||
class Array4D : public Array<T> {
|
||||
public:
|
||||
// Creates a 4D array, uninitialized values.
|
||||
Array4D(int64 planes, int64 depth, int64 height, int64 width)
|
||||
: Array<T>(std::vector<int64>{planes, depth, height, width}) {}
|
||||
|
||||
// Creates a 4D array, initialized to value.
|
||||
Array4D(int64 planes, int64 depth, int64 height, int64 width, T value)
|
||||
: Array<T>(std::vector<int64>{planes, depth, height, width}, value) {}
|
||||
|
||||
// Creates a 4D array, filled with values.
|
||||
//
|
||||
// We need to set a default type for Container so that code like
|
||||
// Array4D(1, 1, 1, 1, {1}) will work. The template cannot infer the
|
||||
// initializer_list type in that case without this default.
|
||||
template <typename Container = std::initializer_list<T>>
|
||||
Array4D(int64 planes, int64 depth, int64 height, int64 width,
|
||||
const Container& values)
|
||||
: Array4D(planes, depth, height, width) {
|
||||
this->SetValues(values);
|
||||
}
|
||||
|
||||
// Construct an Array4D with the given nested initializer list.
|
||||
Array4D(std::initializer_list<std::initializer_list<
|
||||
std::initializer_list<std::initializer_list<T>>>>
|
||||
values)
|
||||
: Array<T>(values) {}
|
||||
|
||||
// Creates an array of a floating-point type (half, bfloat16, float,
|
||||
// or double) from the given nested initializer list of float values.
|
||||
template <typename T2, typename = typename std::enable_if<
|
||||
(std::is_same<T, Eigen::half>::value ||
|
||||
std::is_same<T, bfloat16>::value ||
|
||||
std::is_same<T, float>::value ||
|
||||
std::is_same<T, double>::value) &&
|
||||
std::is_same<T2, float>::value>::type>
|
||||
Array4D(std::initializer_list<std::initializer_list<
|
||||
std::initializer_list<std::initializer_list<T2>>>>
|
||||
values)
|
||||
: Array<T>(values) {}
|
||||
|
||||
// Numerically-named aliases for the various dimensions. This matches the
|
||||
// dimension names used in array3d.
|
||||
int64 n4() const { return this->dim(3); }
|
||||
int64 n3() const { return this->dim(2); }
|
||||
int64 n2() const { return this->dim(1); }
|
||||
int64 n1() const { return this->dim(0); }
|
||||
|
||||
int64 width() const { return this->dim(3); }
|
||||
int64 height() const { return this->dim(2); }
|
||||
int64 depth() const { return this->dim(1); }
|
||||
int64 planes() const { return this->dim(0); }
|
||||
|
||||
// Fills all of the {p,z} with the array provided, which specifies {y,x}.
|
||||
void FillWithYX(const Array2D<T>& value) {
|
||||
CHECK_EQ(value.height(), height());
|
||||
CHECK_EQ(value.width(), width());
|
||||
for (int64 plane = 0; plane < planes(); ++plane) {
|
||||
for (int64 depth = 0; depth < this->depth(); ++depth) {
|
||||
for (int64 height = 0; height < this->height(); ++height) {
|
||||
for (int64 width = 0; width < this->width(); ++width) {
|
||||
(*this)(plane, depth, height, width) = value(height, width);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fills all of the {x,y} with the array provided, which specifies {p,z}.
|
||||
void FillWithPZ(const Array2D<T>& value) {
|
||||
CHECK_EQ(value.height(), planes());
|
||||
CHECK_EQ(value.width(), depth());
|
||||
for (int64 height = 0; height < this->height(); ++height) {
|
||||
for (int64 width = 0; width < this->width(); ++width) {
|
||||
for (int64 plane = 0; plane < planes(); ++plane) {
|
||||
for (int64 depth = 0; depth < this->depth(); ++depth) {
|
||||
(*this)(plane, depth, height, width) = value(plane, depth);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fills each of the minor-dim matrices with a number designating which minor
|
||||
// dim matrix is enclosed by the shape.
|
||||
void FillWithMinorDimNum() {
|
||||
LOG(INFO) << "width: " << this->width();
|
||||
LOG(INFO) << "height: " << this->height();
|
||||
LOG(INFO) << "depth: " << this->depth();
|
||||
LOG(INFO) << "planes: " << this->planes();
|
||||
for (int64 height = 0; height < this->height(); ++height) {
|
||||
for (int64 width = 0; width < this->width(); ++width) {
|
||||
for (int64 plane = 0; plane < planes(); ++plane) {
|
||||
for (int64 depth = 0; depth < this->depth(); ++depth) {
|
||||
float this_val = plane * this->depth() + depth;
|
||||
(*this)(plane, depth, height, width) = this_val;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_ARRAY4D_H_
|
||||
@@ -0,0 +1,210 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_CLIENT_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_CLIENT_H_
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/types/span.h"
|
||||
#include "tensorflow/compiler/xla/client/global_data.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_computation.h"
|
||||
#include "tensorflow/compiler/xla/literal.h"
|
||||
#include "tensorflow/compiler/xla/service/hlo.pb.h"
|
||||
#include "tensorflow/compiler/xla/service_interface.h"
|
||||
#include "tensorflow/compiler/xla/statusor.h"
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
#include "tensorflow/compiler/xla/xla.pb.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// XLA service's client object -- wraps the service with convenience and
|
||||
// lifetime-oriented methods.
|
||||
class Client {
|
||||
public:
|
||||
explicit Client(ServiceInterface* stub);
|
||||
virtual ~Client();
|
||||
|
||||
// Executes the computation with the given arguments and returns the global
|
||||
// data that was produced from the execution.
|
||||
// * If execution_options is not nullptr, these options are passed to the
|
||||
// service to affect how it compiles our computation. (The pointer does not
|
||||
// need to live beyond this call.)
|
||||
// * If execution_options.device_handles is not empty, the computation is
|
||||
// executed on the devices associated with the handles by partitioning the
|
||||
// computation based on the attached sharding attributes. Otherwise, a
|
||||
// device is chosen by the service.
|
||||
// * If execution_profile is not nullptr then the pointed-to ExecutionProfile
|
||||
// will be filled with profile data from the execution.
|
||||
StatusOr<std::unique_ptr<GlobalData>> Execute(
|
||||
const XlaComputation& computation,
|
||||
absl::Span<GlobalData* const> arguments,
|
||||
const ExecutionOptions* execution_options = nullptr,
|
||||
ExecutionProfile* execution_profile = nullptr);
|
||||
|
||||
// A struct to represent a computation instance to be executed.
|
||||
// * If execution_options.device_handles is not empty, the computation is
|
||||
// executed on the devices associated with the handles by partitioning the
|
||||
// computation based on the attached sharding attributes. Otherwise, a
|
||||
// device is chosen by the service.
|
||||
struct XlaComputationInstance {
|
||||
const XlaComputation& computation;
|
||||
std::vector<GlobalData*> arguments;
|
||||
ExecutionOptions execution_options;
|
||||
ExecutionProfile* execution_profile;
|
||||
|
||||
XlaComputationInstance(const XlaComputation& computation,
|
||||
std::vector<GlobalData*> arguments,
|
||||
ExecutionOptions execution_options,
|
||||
ExecutionProfile* execution_profile)
|
||||
: computation(computation),
|
||||
arguments(std::move(arguments)),
|
||||
execution_options(execution_options),
|
||||
execution_profile(execution_profile) {}
|
||||
};
|
||||
|
||||
// Executes a list XlaComputationInstances and returns global data produced
|
||||
// from each computation.
|
||||
//
|
||||
StatusOr<std::vector<std::unique_ptr<GlobalData>>> ExecuteParallel(
|
||||
absl::Span<const XlaComputationInstance> computations);
|
||||
|
||||
// Requests device_count device handles available on the target. The returned
|
||||
// device handles are used to specify the devices to execute the computations
|
||||
// (see ExecuteParallel) or to transfer data (see TransferToServer or
|
||||
// TransferToInfeed).
|
||||
StatusOr<std::vector<DeviceHandle>> GetDeviceHandles(int64 device_count);
|
||||
|
||||
// Transfer the global data provided to this client process, which is
|
||||
// returned in the provided literal. Use sparingly to avoid transfer
|
||||
// overheads.
|
||||
//
|
||||
// If shape_with_layout is not nullptr, it points to a shape whose layout will
|
||||
// be the layout of the returned literal.
|
||||
StatusOr<Literal> Transfer(const GlobalData& data,
|
||||
const Shape* shape_with_layout = nullptr);
|
||||
|
||||
// Transfer the given literal to the server. This allocates memory on the
|
||||
// device and copies the literal's contents over. Returns a global data handle
|
||||
// that can be used to refer to this value from the client.
|
||||
//
|
||||
// If device_handle is not nullptr, data is transferred to the associated
|
||||
// device (and its replicas if replication is enabled). Otherwise, data is
|
||||
// transferred to the default device (and its replicas).
|
||||
StatusOr<std::unique_ptr<GlobalData>> TransferToServer(
|
||||
const LiteralSlice& literal, const DeviceHandle* device_handle = nullptr);
|
||||
|
||||
// Transfer the given literal to the Infeed interface of the device.
|
||||
//
|
||||
// device_handle and replica_id together specify a particular device; a device
|
||||
// assigned for the given replica_id among the replicas that the given device
|
||||
// handle belongs to.
|
||||
Status TransferToInfeed(const LiteralSlice& literal, int64 replica_id = 0,
|
||||
const DeviceHandle* device_handle = nullptr);
|
||||
|
||||
// Transfers from the Outfeed of the device.
|
||||
//
|
||||
// device_handle and replica_id together specify a particular device; a device
|
||||
// assigned for the given replica_id among the replicas that the given device
|
||||
// handle belongs to.
|
||||
StatusOr<Literal> TransferFromOutfeed(
|
||||
const Shape* shape_with_layout, int64 replica_id = 0,
|
||||
const DeviceHandle* device_handle = nullptr);
|
||||
|
||||
// Resets the device, clearing all existing state on the device.
|
||||
Status ResetDevice();
|
||||
|
||||
// Executes the computation with the given arguments and transfers the result
|
||||
// to the client as a literal. Parameters are defined the same as for
|
||||
// Execute() and Transfer().
|
||||
StatusOr<Literal> ExecuteAndTransfer(
|
||||
const XlaComputation& computation,
|
||||
absl::Span<GlobalData* const> arguments,
|
||||
const ExecutionOptions* execution_options = nullptr,
|
||||
ExecutionProfile* execution_profile = nullptr);
|
||||
|
||||
// Computes the value of the given computation using a non-optimized
|
||||
// interpreter on the host.
|
||||
//
|
||||
// The computation must not depend on any parameters, or on stateful operators
|
||||
// such as `RngNormal` or `Infeed`.
|
||||
//
|
||||
// This functionality can be useful when translating a computation into XLA
|
||||
// where something that looked dynamic is required by XLA to be specified as a
|
||||
// constant. E.g. the source computation (outside of XLA) may include a
|
||||
// dynamic computation of the shape of something and ComputeConstant lets you
|
||||
// determine what the value of that computation is in the case where the value
|
||||
// can be determined at compile time.
|
||||
//
|
||||
// If output_layout is non-null, then the output of the computation will be
|
||||
// stored using that layout.
|
||||
StatusOr<Literal> ComputeConstant(
|
||||
const XlaComputation& computation,
|
||||
const Layout* output_layout = nullptr) const;
|
||||
|
||||
// Unregister the memory for the given GlobalData on the device.
|
||||
Status Unregister(const GlobalData& data);
|
||||
|
||||
// Returns a vector of global data handles that point to the tuple elements.
|
||||
StatusOr<std::vector<std::unique_ptr<GlobalData>>> DeconstructTuple(
|
||||
const GlobalData& data);
|
||||
|
||||
// Retrieves the statistics of the given computation.
|
||||
StatusOr<ComputationStats> GetComputationStats(
|
||||
const XlaComputation& computation,
|
||||
const DebugOptions& debug_options) const;
|
||||
|
||||
// Returns the Shape of the given array specified by 'data'. The shape
|
||||
// includes the Layout of the array as it is stored on the service.
|
||||
StatusOr<Shape> GetShape(const GlobalData& data);
|
||||
|
||||
// As above, but returns the shape of the provided computation (parameter
|
||||
// types/names and return type).
|
||||
StatusOr<std::unique_ptr<ProgramShape>> GetComputationShape(
|
||||
const XlaComputation& computation);
|
||||
|
||||
// Creates a channel handle that can be used to transfer data between two
|
||||
// computations on different devices via a pair of Send and Recv instructions.
|
||||
StatusOr<ChannelHandle> CreateChannelHandle();
|
||||
|
||||
// Create a channel for communicating with the host via a SendtoHost or
|
||||
// RecvFromHost operation.
|
||||
StatusOr<ChannelHandle> CreateHostToDeviceChannelHandle();
|
||||
StatusOr<ChannelHandle> CreateDeviceToHostChannelHandle();
|
||||
|
||||
StatusOr<XlaComputation> LoadSnapshot(const HloSnapshot& module);
|
||||
|
||||
ServiceInterface* stub() { return stub_; }
|
||||
|
||||
private:
|
||||
// Returns the execution statistics (e.g., gflop/s) as a string from the
|
||||
// ExecutionProfile returned from an execution of the computation.
|
||||
StatusOr<string> ExecutionStatsAsString(const XlaComputation& computation,
|
||||
const ExecutionProfile& profile);
|
||||
|
||||
StatusOr<ChannelHandle> CreateChannelHandleByType(
|
||||
ChannelHandle::ChannelType type);
|
||||
|
||||
ServiceInterface* stub_; // Stub that this client is connected on.
|
||||
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(Client);
|
||||
};
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_CLIENT_H_
|
||||
@@ -0,0 +1,134 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// The "client library" instantiates a local (in-process) XLA service for
|
||||
// use by this process, and connects to it with a singleton XLA local
|
||||
// client. ClientLibrary::GetOrCreateLocalClient will spawn a local service,
|
||||
// and return a client that's connected to it and ready to run XLA
|
||||
// computations.
|
||||
#ifndef TENSORFLOW_COMPILER_XLA_CLIENT_CLIENT_LIBRARY_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_CLIENT_LIBRARY_H_
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/compiler/xla/client/compile_only_client.h"
|
||||
#include "tensorflow/compiler/xla/client/local_client.h"
|
||||
#include "tensorflow/compiler/xla/service/compile_only_service.h"
|
||||
#include "tensorflow/compiler/xla/service/device_memory_allocator.h"
|
||||
#include "tensorflow/compiler/xla/service/local_service.h"
|
||||
#include "tensorflow/compiler/xla/statusor.h"
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
#include "tensorflow/core/platform/mutex.h"
|
||||
#include "tensorflow/core/platform/stream_executor_no_cuda.h"
|
||||
#include "tensorflow/core/platform/thread_annotations.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Options to configure the local client when it is created.
|
||||
class LocalClientOptions {
|
||||
public:
|
||||
LocalClientOptions(se::Platform* platform = nullptr,
|
||||
int number_of_replicas = 1,
|
||||
int intra_op_parallelism_threads = -1);
|
||||
|
||||
// Set the platform backing the service, or nullptr for the default platform.
|
||||
LocalClientOptions& set_platform(se::Platform* platform);
|
||||
se::Platform* platform() const;
|
||||
|
||||
// Set the number of replicas to use when compiling replicated
|
||||
// programs.
|
||||
LocalClientOptions& set_number_of_replicas(int number_of_replicas);
|
||||
int number_of_replicas() const;
|
||||
|
||||
// Sets the thread pool size for parallel execution of an individual operator.
|
||||
LocalClientOptions& set_intra_op_parallelism_threads(int num_threads);
|
||||
int intra_op_parallelism_threads() const;
|
||||
|
||||
private:
|
||||
se::Platform* platform_;
|
||||
int number_of_replicas_;
|
||||
int intra_op_parallelism_threads_;
|
||||
};
|
||||
|
||||
class ClientLibrary {
|
||||
public:
|
||||
// Singleton constructor-or-accessor -- returns a client for the application
|
||||
// to issue XLA commands on. Arguments:
|
||||
//
|
||||
// platform : The platform the underlying XLA service should target. If
|
||||
// null then default platform is used.
|
||||
static StatusOr<LocalClient*> GetOrCreateLocalClient(
|
||||
se::Platform* platform = nullptr);
|
||||
static StatusOr<LocalClient*> GetOrCreateLocalClient(
|
||||
const LocalClientOptions& options);
|
||||
|
||||
// Convenience "or-die" wrapper around the above which returns the existing
|
||||
// client library or creates one with default platform and allocator.
|
||||
static LocalClient* LocalClientOrDie();
|
||||
|
||||
// Returns the service from the service thread. Only used in unit tests to
|
||||
// access user computations from client.
|
||||
static LocalService* GetXlaService(se::Platform* platform);
|
||||
|
||||
// Singleton constructor-or-accessor for compile-only clients. Arguments:
|
||||
//
|
||||
// platform : The platform the underlying XLA service should target. If
|
||||
// null then default platform is used.
|
||||
static StatusOr<CompileOnlyClient*> GetOrCreateCompileOnlyClient(
|
||||
se::Platform* platform = nullptr);
|
||||
|
||||
// Clears the local instance and compile only instance caches. The client
|
||||
// pointers returned by the previous GetOrCreateLocalClient() or
|
||||
// GetOrCreateCompileOnlyClient() invocations are not valid anymore.
|
||||
static void DestroyLocalInstances();
|
||||
|
||||
private:
|
||||
// Returns the singleton instance of ClientLibrary.
|
||||
static ClientLibrary& Singleton();
|
||||
|
||||
ClientLibrary();
|
||||
~ClientLibrary();
|
||||
|
||||
struct LocalInstance {
|
||||
// Service that is wrapped by the singleton client object.
|
||||
std::unique_ptr<LocalService> service;
|
||||
// Singleton client object.
|
||||
std::unique_ptr<LocalClient> client;
|
||||
};
|
||||
|
||||
struct CompileOnlyInstance {
|
||||
// Service that is wrapped by the singleton client object.
|
||||
std::unique_ptr<CompileOnlyService> service;
|
||||
// Singleton client object.
|
||||
std::unique_ptr<CompileOnlyClient> client;
|
||||
};
|
||||
|
||||
tensorflow::mutex service_mutex_; // Guards the singleton creation state.
|
||||
std::unordered_map<se::Platform::Id, std::unique_ptr<LocalInstance>>
|
||||
local_instances_ GUARDED_BY(service_mutex_);
|
||||
|
||||
std::unordered_map<se::Platform::Id, std::unique_ptr<CompileOnlyInstance>>
|
||||
compile_only_instances_ GUARDED_BY(service_mutex_);
|
||||
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(ClientLibrary);
|
||||
};
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_CLIENT_LIBRARY_H_
|
||||
@@ -0,0 +1,68 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_COMPILE_ONLY_CLIENT_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_COMPILE_ONLY_CLIENT_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/client/client.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_computation.h"
|
||||
#include "tensorflow/compiler/xla/service/compile_only_service.h"
|
||||
#include "tensorflow/compiler/xla/service/compiler.h"
|
||||
#include "tensorflow/compiler/xla/statusor.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
#include "tensorflow/core/platform/stream_executor_no_cuda.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// An XLA Client specialization for doing ahead-of-time compilation. This does
|
||||
// not require (or attempt to instantiate) an execution-capable backend for the
|
||||
// relevant platform.
|
||||
class CompileOnlyClient : public Client {
|
||||
public:
|
||||
explicit CompileOnlyClient(CompileOnlyService* service)
|
||||
: Client(service), compiler_service_(service) {}
|
||||
|
||||
CompileOnlyClient(const CompileOnlyClient&) = delete;
|
||||
void operator=(const CompileOnlyClient&) = delete;
|
||||
|
||||
// A description of an xla computation to compile using CompileAheadOfTime.
|
||||
struct AotXlaComputationInstance {
|
||||
const XlaComputation* computation;
|
||||
// Inform the compiler of the expected layout for arguments.
|
||||
std::vector<const Shape*> argument_layouts;
|
||||
// Specifies the expected result layout.
|
||||
const Shape* result_layout;
|
||||
};
|
||||
|
||||
// Compiles a list of xla computations for ahead-of-time execution.
|
||||
// This is intended for use in static compilation. The |options|
|
||||
// parameter describes the target for which the compiler should emit
|
||||
// code. |metadata|, if provided, is populated during compilation.
|
||||
StatusOr<std::vector<std::unique_ptr<AotCompilationResult>>>
|
||||
CompileAheadOfTime(
|
||||
const absl::Span<const AotXlaComputationInstance> computations,
|
||||
const AotCompilationOptions& options,
|
||||
std::unique_ptr<AotCompilationMetadata>* metadata = nullptr);
|
||||
|
||||
// Returns the size of a pointer in bytes for a given triple.
|
||||
static int64 PointerSizeForTriple(absl::string_view triple);
|
||||
|
||||
private:
|
||||
CompileOnlyService* compiler_service_;
|
||||
};
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_COMPILE_ONLY_CLIENT_H_
|
||||
@@ -0,0 +1,112 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_EXECUTABLE_BUILD_OPTIONS_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_EXECUTABLE_BUILD_OPTIONS_H_
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/types/optional.h"
|
||||
#include "tensorflow/compiler/xla/service/device_memory_allocator.h"
|
||||
#include "tensorflow/compiler/xla/util.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Class containing options for building an LocalExecutable with
|
||||
// LocalClient::Compile.
|
||||
class ExecutableBuildOptions {
|
||||
public:
|
||||
// If set, this is the device to build the computation for. Valid
|
||||
// device_ordinal values are: 0 to # of devices - 1. These values are
|
||||
// identical to the device ordinal values used by StreamExecutor. The built
|
||||
// executable will be executable on any device equivalent to the specified
|
||||
// device as determined by Backend::devices_equivalent(). A value of -1
|
||||
// indicates this option has not been set.
|
||||
ExecutableBuildOptions& set_device_ordinal(int device_ordinal);
|
||||
int device_ordinal() const;
|
||||
|
||||
// If set, this specifies the layout of the result of the computation. If not
|
||||
// set, the service will chose the layout of the result. A Shape is used to
|
||||
// store the layout to accommodate tuple result shapes. A value of nullptr
|
||||
// indicates the option has not been set.
|
||||
ExecutableBuildOptions& set_result_layout(const Shape& shape_with_layout);
|
||||
const Shape* result_layout() const;
|
||||
|
||||
// If set, this specifies an allocator that can be used to allocate temporary
|
||||
// space on the device during compilation. For example, the compiler might
|
||||
// want to run various algorithms on the device and pick the fastest one -- it
|
||||
// might allocate buffers for use by these algorithms using this allocator.
|
||||
//
|
||||
// This does not need to be the same as the DeviceMemoryAllocator passed when
|
||||
// running the executable.
|
||||
ExecutableBuildOptions& set_device_allocator(
|
||||
DeviceMemoryAllocator* allocator);
|
||||
DeviceMemoryAllocator* device_allocator() const;
|
||||
|
||||
// If set, specifies a regexp of HLO graphs to dump (as in DebugOptions).
|
||||
ExecutableBuildOptions& set_generate_hlo_graph(string regex);
|
||||
const absl::optional<string>& generate_hlo_graph() const;
|
||||
|
||||
// If set, specifies a dirpath to dump the end-of-optimization-pipeline HLO
|
||||
// protobuf to (as in DebugOptions).
|
||||
ExecutableBuildOptions& set_dump_optimized_hlo_proto_to(
|
||||
absl::string_view dirpath);
|
||||
const absl::optional<string>& dump_optimized_hlo_proto_to() const;
|
||||
|
||||
// If set, specifies a dirpath to dump the start-of-optimization-pipeline HLO
|
||||
// protobuf to (as in DebugOptions).
|
||||
ExecutableBuildOptions& set_dump_unoptimized_hlo_proto_to(
|
||||
absl::string_view dirpath);
|
||||
const absl::optional<string>& dump_unoptimized_hlo_proto_to() const;
|
||||
|
||||
// If set, specifies a dirpath to dump the per-pass-in-pipeline HLO protobufs
|
||||
// to (as in DebugOptions).
|
||||
ExecutableBuildOptions& set_dump_per_pass_hlo_proto_to(
|
||||
absl::string_view dirpath);
|
||||
const absl::optional<string>& dump_per_pass_hlo_proto_to() const;
|
||||
|
||||
// If true, specifies that we should record an HLO profile during execution
|
||||
// and log it after execution (as in DebugOptions). If nullopt the default is
|
||||
// used.
|
||||
ExecutableBuildOptions& set_hlo_profile(bool enabled);
|
||||
absl::optional<bool> hlo_profile() const;
|
||||
|
||||
void add_disabled_hlo_pass(absl::string_view pass_name) {
|
||||
disabled_hlo_passes_.push_back(std::string(pass_name));
|
||||
}
|
||||
const absl::Span<const std::string> disabled_hlo_passes() const {
|
||||
return disabled_hlo_passes_;
|
||||
}
|
||||
|
||||
// Returns a string representation of the build options, suitable for
|
||||
// debugging.
|
||||
string ToString() const;
|
||||
|
||||
private:
|
||||
absl::optional<bool> hlo_profile_;
|
||||
int device_ordinal_ = -1;
|
||||
Shape result_layout_;
|
||||
bool result_layout_set_ = false;
|
||||
absl::optional<string> generate_hlo_graph_;
|
||||
absl::optional<string> dump_optimized_hlo_proto_to_;
|
||||
absl::optional<string> dump_unoptimized_hlo_proto_to_;
|
||||
absl::optional<string> dump_per_pass_hlo_proto_to_;
|
||||
DeviceMemoryAllocator* device_allocator_ = nullptr;
|
||||
std::vector<std::string> disabled_hlo_passes_;
|
||||
};
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_EXECUTABLE_BUILD_OPTIONS_H_
|
||||
@@ -0,0 +1,48 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_GLOBAL_DATA_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_GLOBAL_DATA_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/service_interface.h"
|
||||
#include "tensorflow/compiler/xla/xla.pb.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// A GlobalData object represents a globally-accessible allocation of
|
||||
// data in the associated XLA service.
|
||||
class GlobalData {
|
||||
public:
|
||||
// Gives ownership of the global data handle to this object.
|
||||
GlobalData(ServiceInterface* parent, GlobalDataHandle handle);
|
||||
|
||||
// Unregisters the wrapped handle, which causes the service to
|
||||
// deallocate the associated data.
|
||||
~GlobalData();
|
||||
|
||||
const GlobalDataHandle& handle() const { return handle_; }
|
||||
|
||||
private:
|
||||
GlobalDataHandle handle_; // Handle being wrapped.
|
||||
ServiceInterface* parent_; // Service used to unregister handle_.
|
||||
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(GlobalData);
|
||||
};
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_GLOBAL_DATA_H_
|
||||
@@ -0,0 +1,62 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_LIB_ARITHMETIC_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_LIB_ARITHMETIC_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_computation.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Creates a scalar add computation and returns it.
|
||||
XlaComputation CreateScalarAddComputation(PrimitiveType type,
|
||||
XlaBuilder* builder);
|
||||
|
||||
// Creates a scalar multiply computation and returns it.
|
||||
XlaComputation CreateScalarMultiplyComputation(PrimitiveType type,
|
||||
XlaBuilder* builder);
|
||||
|
||||
// Creates a scalar ge computation and returns it.
|
||||
XlaComputation CreateScalarGeComputation(PrimitiveType type,
|
||||
XlaBuilder* builder);
|
||||
|
||||
// Creates a scalar max computation and returns it.
|
||||
XlaComputation CreateScalarMaxComputation(PrimitiveType type,
|
||||
XlaBuilder* builder);
|
||||
|
||||
// Creates a scalar min computation and returns it.
|
||||
XlaComputation CreateScalarMinComputation(PrimitiveType type,
|
||||
XlaBuilder* builder);
|
||||
|
||||
// Creates a scalar logical AND computation and returns it.
|
||||
XlaComputation CreateScalarAndComputation(PrimitiveType type,
|
||||
XlaBuilder* builder);
|
||||
|
||||
// Creates a scalar logical OR computation and returns it.
|
||||
XlaComputation CreateScalarOrComputation(PrimitiveType type,
|
||||
XlaBuilder* builder);
|
||||
|
||||
// Returns whether any predicate in "predicates" is set.
|
||||
//
|
||||
// Note: if predicates is zero-sized, Any() vacuously returns false.
|
||||
XlaOp Any(XlaOp predicates);
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_LIB_ARITHMETIC_H_
|
||||
@@ -0,0 +1,124 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_LIB_CONSTANTS_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_LIB_CONSTANTS_H_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/primitive_util.h"
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Returns scalar 'value' as a scalar of 'type'. Unlike ConstantR0, 'type' is
|
||||
// determined at C++ run-time, rather than C++ compile-time.
|
||||
// If 'value' is floating point but 'type' is not, or if 'value' is complex but
|
||||
// 'type' is not, an error will be returned. This is to catch accidental
|
||||
// truncation; in such cases, use an explicit cast.
|
||||
template <typename T>
|
||||
XlaOp ConstantR0WithType(XlaBuilder* builder, PrimitiveType type, T value) {
|
||||
if (std::is_floating_point<T>::value &&
|
||||
!(primitive_util::IsFloatingPointType(type) ||
|
||||
primitive_util::IsComplexType(type))) {
|
||||
return builder->ReportError(InvalidArgument(
|
||||
"Invalid cast from floating point type to %s in ConstantR0WithType.",
|
||||
PrimitiveType_Name(type)));
|
||||
}
|
||||
if (std::is_same<T, complex64>::value &&
|
||||
!primitive_util::IsComplexType(type)) {
|
||||
return builder->ReportError(InvalidArgument(
|
||||
"Invalid cast from complex type to %s in ConstantR0WithType.",
|
||||
PrimitiveType_Name(type)));
|
||||
}
|
||||
switch (type) {
|
||||
case F16:
|
||||
return ConstantR0<half>(builder, static_cast<half>(value));
|
||||
case BF16:
|
||||
return ConstantR0<bfloat16>(builder, static_cast<bfloat16>(value));
|
||||
case F32:
|
||||
return ConstantR0<float>(builder, static_cast<float>(value));
|
||||
case F64:
|
||||
return ConstantR0<double>(builder, static_cast<double>(value));
|
||||
case C64:
|
||||
return ConstantR0<complex64>(builder, static_cast<complex64>(value));
|
||||
case U8:
|
||||
return ConstantR0<uint8>(builder, static_cast<uint8>(value));
|
||||
case U32:
|
||||
return ConstantR0<uint32>(builder, static_cast<uint32>(value));
|
||||
case U64:
|
||||
return ConstantR0<uint64>(builder, static_cast<uint64>(value));
|
||||
case S8:
|
||||
return ConstantR0<int8>(builder, static_cast<int8>(value));
|
||||
case S32:
|
||||
return ConstantR0<int32>(builder, static_cast<int32>(value));
|
||||
case S64:
|
||||
return ConstantR0<int64>(builder, static_cast<int64>(value));
|
||||
default:
|
||||
return builder->ReportError(
|
||||
InvalidArgument("Invalid type for ConstantR0WithType (%s).",
|
||||
PrimitiveType_Name(type)));
|
||||
}
|
||||
}
|
||||
|
||||
// Returns a scalar containing 'value' cast to the same run-time type as
|
||||
// 'prototype'.
|
||||
// If 'value' is floating point but 'prototype' is not, or if 'value' is complex
|
||||
// 'prototype' is not, an error will be returned.
|
||||
template <typename T>
|
||||
XlaOp ScalarLike(XlaOp prototype, T value) {
|
||||
XlaBuilder* builder = prototype.builder();
|
||||
return builder->ReportErrorOrReturn([&]() -> StatusOr<XlaOp> {
|
||||
TF_ASSIGN_OR_RETURN(Shape shape, builder->GetShape(prototype));
|
||||
return ConstantR0WithType(builder, shape.element_type(), value);
|
||||
});
|
||||
}
|
||||
|
||||
// Returns a scalar with value '0' of 'type'.
|
||||
XlaOp Zero(XlaBuilder* builder, PrimitiveType type);
|
||||
|
||||
// Returns a zero-filled tensor with shape `shape`.
|
||||
XlaOp Zeros(XlaBuilder* builder, const Shape& shape);
|
||||
|
||||
// Returns a zero-filled tensor with the same shape as `prototype`.
|
||||
XlaOp ZerosLike(XlaOp prototype);
|
||||
|
||||
// Returns a scalar with value '1' of 'type'.
|
||||
XlaOp One(XlaBuilder* builder, PrimitiveType type);
|
||||
|
||||
// Returns the machine epsilon for floating-point type `type`, i.e.,
|
||||
// the difference between 1.0 and the next representable value.
|
||||
XlaOp Epsilon(XlaBuilder* builder, PrimitiveType type);
|
||||
|
||||
// Returns the minimum representable finite or infinite value for 'type'.
|
||||
// Returns '-inf' for floating-point types.
|
||||
XlaOp MinValue(XlaBuilder* builder, PrimitiveType type);
|
||||
|
||||
// Returns the minimum representable finite value for 'type'. For a floating
|
||||
// point type, this is equal to -MaxFiniteValue().
|
||||
XlaOp MinFiniteValue(XlaBuilder* builder, PrimitiveType type);
|
||||
|
||||
// Returns the maximum representable finite or infinite value for 'type'.
|
||||
// Returns 'inf' for floating-point types.
|
||||
XlaOp MaxValue(XlaBuilder* builder, PrimitiveType type);
|
||||
|
||||
// Returns the maximum representable finite value for 'type'.
|
||||
XlaOp MaxFiniteValue(XlaBuilder* builder, PrimitiveType type);
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_LIB_CONSTANTS_H_
|
||||
@@ -0,0 +1,44 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_LIB_CONV_GRAD_SIZE_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_LIB_CONV_GRAD_SIZE_UTIL_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/client/padding.h"
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Information about a single spatial dimension for a convolution gradients and
|
||||
// windowed operations.
|
||||
struct SpatialDimensionOutputSizeAndPadding {
|
||||
// Effective size of the operation output (potentially expanded).
|
||||
int64 output_size;
|
||||
// Number of padding elements to be added before/after this dimension of
|
||||
// the input when computing the input gradient.
|
||||
int64 pad_before;
|
||||
int64 pad_after;
|
||||
};
|
||||
|
||||
// Verifies that the dimensions all match, and computes the size and padding of
|
||||
// a spatial dimension for convolution gradient operations.
|
||||
StatusOr<SpatialDimensionOutputSizeAndPadding>
|
||||
ConvGradExtractAndVerifyDimension(int64 input_size, int64 filter_size,
|
||||
int64 output_size, int64 dilation,
|
||||
int64 stride, Padding padding);
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_LIB_CONV_GRAD_SIZE_UTIL_H_
|
||||
@@ -0,0 +1,87 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_LIB_MATH_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_LIB_MATH_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Computes the square root of 'operand'.
|
||||
XlaOp Sqrt(XlaOp operand);
|
||||
|
||||
// Computes the reciprocal of the square root of 'operand'.
|
||||
XlaOp Rsqrt(XlaOp operand);
|
||||
|
||||
// Computes the square of 'operand'.
|
||||
XlaOp Square(XlaOp operand);
|
||||
|
||||
// Computes the reciprocal of 'operand'.
|
||||
XlaOp Reciprocal(XlaOp operand);
|
||||
|
||||
// Evaluates a polynomial given coefficients and `x`.
|
||||
// N.B. Coefficients should be supplied in decreasing order.
|
||||
XlaOp EvaluatePolynomial(XlaOp x, absl::Span<const float> coefficients);
|
||||
|
||||
// Computes an approximation of the error function complement (1 - erf(x)).
|
||||
XlaOp Erfc(XlaOp x);
|
||||
|
||||
// Computes an approximation of the error function.
|
||||
XlaOp Erf(XlaOp x);
|
||||
|
||||
// Computes an approximation of the inverse of the error function.
|
||||
XlaOp ErfInv(XlaOp x);
|
||||
|
||||
// Computes an approximation of the lgamma function.
|
||||
XlaOp Lgamma(XlaOp input);
|
||||
|
||||
// Computes an approximation of the digamma function.
|
||||
XlaOp Digamma(XlaOp input);
|
||||
|
||||
// Trigonometric functions
|
||||
|
||||
// Computes the arc cosine of 'x'.
|
||||
XlaOp Acos(XlaOp x);
|
||||
|
||||
// Computes the arc sine of 'x'.
|
||||
XlaOp Asin(XlaOp x);
|
||||
|
||||
// Computes the arc tangent of 'x'.
|
||||
XlaOp Atan(XlaOp x);
|
||||
|
||||
// Computes the tangent of 'x'.
|
||||
XlaOp Tan(XlaOp x);
|
||||
|
||||
// Hyperbolic trigonometric functions
|
||||
|
||||
// Computes the inverse hyperbolic cosine of 'x'.
|
||||
XlaOp Acosh(XlaOp x);
|
||||
|
||||
// Computes the inverse hyperbolic sine of 'x'.
|
||||
XlaOp Asinh(XlaOp x);
|
||||
|
||||
// Computes the inverse hyperbolic tangent of 'x'.
|
||||
XlaOp Atanh(XlaOp x);
|
||||
|
||||
// Computes the hyperbolic cosine of 'x'.
|
||||
XlaOp Cosh(XlaOp x);
|
||||
|
||||
// Computes the hyperbolic sine of 'x'.
|
||||
XlaOp Sinh(XlaOp x);
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_LIB_MATH_H_
|
||||
@@ -0,0 +1,48 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_LIB_NUMERIC_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_LIB_NUMERIC_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Returns a rank 1 tensor of `type` containing values [0, 1, 2, ...].
|
||||
XlaOp Iota(XlaBuilder* builder, PrimitiveType type, int64 size);
|
||||
|
||||
// Returns an m x n matrix with 1s on the diagonal elements, zeros everywhere
|
||||
// else.
|
||||
XlaOp IdentityMatrix(XlaBuilder* builder, PrimitiveType type, int64 m, int64 n);
|
||||
|
||||
// Get the diagonals of the last two dimensions. If 'x' has shape
|
||||
// [..., M, N], then the output has shape [..., min(M, N)], containing the
|
||||
// diagonal elements (i.e., with indices [..., i, i]).
|
||||
XlaOp GetMatrixDiagonal(XlaOp x);
|
||||
|
||||
// Get the upper or lower triangle part of the last two dimensions
|
||||
XlaOp Triangle(XlaOp x, bool lower);
|
||||
|
||||
// Get the upper triangle part of the last two dimensions
|
||||
XlaOp UpperTriangle(XlaOp x);
|
||||
|
||||
// Get the lower triangle part of the last two dimensions
|
||||
XlaOp LowerTriangle(XlaOp x);
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_LIB_NUMERIC_H_
|
||||
@@ -0,0 +1,80 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_LIB_POOLING_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_LIB_POOLING_H_
|
||||
|
||||
#include "absl/container/inlined_vector.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Tensor format for reduce window operations.
|
||||
class TensorFormat {
|
||||
public:
|
||||
TensorFormat(int batch_dimension, int feature_dimension,
|
||||
absl::Span<const int64> spatial_dimensions)
|
||||
: batch_dimension_(batch_dimension),
|
||||
feature_dimension_(feature_dimension),
|
||||
spatial_dimensions_(spatial_dimensions.begin(),
|
||||
spatial_dimensions.end()) {}
|
||||
|
||||
int batch_dimension() const { return batch_dimension_; }
|
||||
|
||||
int feature_dimension() const { return feature_dimension_; }
|
||||
|
||||
int spatial_dimension(int dim) const { return spatial_dimensions_[dim]; }
|
||||
|
||||
int num_spatial_dims() const { return spatial_dimensions_.size(); }
|
||||
|
||||
private:
|
||||
// The number of the dimension that represents the batch.
|
||||
int batch_dimension_;
|
||||
// The number of the dimension that represents the features.
|
||||
int feature_dimension_;
|
||||
// The dimension numbers for the spatial dimensions.
|
||||
absl::InlinedVector<int, 4> spatial_dimensions_;
|
||||
};
|
||||
|
||||
// Computes the max pool of 'operand'.
|
||||
XlaOp MaxPool(XlaOp operand, absl::Span<const int64> kernel_size,
|
||||
absl::Span<const int64> stride, Padding padding,
|
||||
const TensorFormat& data_format);
|
||||
|
||||
// Computes the average pool of 'operand'.
|
||||
XlaOp AvgPool(XlaOp operand, absl::Span<const int64> kernel_size,
|
||||
absl::Span<const int64> stride,
|
||||
absl::Span<const std::pair<int64, int64>> padding,
|
||||
const TensorFormat& data_format,
|
||||
const bool counts_include_padding);
|
||||
|
||||
// Returns the list of low and high padding elements in each spatial dimension
|
||||
// for the given 'padding' specification.
|
||||
std::vector<std::pair<int64, int64>> MakeSpatialPadding(
|
||||
absl::Span<const int64> input_size, absl::Span<const int64> kernel_size,
|
||||
absl::Span<const int64> stride, Padding padding,
|
||||
const TensorFormat& data_format);
|
||||
|
||||
// Computes the average pool gradient.
|
||||
XlaOp AvgPoolGrad(XlaOp out_backprop, absl::Span<const int64> gradients_size,
|
||||
absl::Span<const int64> kernel_size,
|
||||
absl::Span<const int64> stride,
|
||||
absl::Span<const std::pair<int64, int64>> spatial_padding,
|
||||
const TensorFormat& data_format,
|
||||
const bool counts_include_padding);
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_LIB_POOLING_H_
|
||||
@@ -0,0 +1,34 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_LIB_PRNG_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_LIB_PRNG_H_
|
||||
|
||||
#include <array>
|
||||
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Returns a tensor containing 'shape' random values uniformly distributed in
|
||||
// the range [minval, maxval). Requires 2 32-bit integer seeds.
|
||||
// Currently only 'shape's of type F32 are implemented.
|
||||
XlaOp StatelessRngUniform(std::array<XlaOp, 2> seeds, const Shape& shape,
|
||||
XlaOp minval, XlaOp maxval);
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_LIB_PRNG_H_
|
||||
@@ -0,0 +1,31 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_LIB_SORTING_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_LIB_SORTING_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/client/xla_builder.h"
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Returns a tuple composed of the top `k` values and corresponding indices in
|
||||
// `input`. Output values are in descending order, from largest to smallest.
|
||||
XlaOp TopK(XlaOp input, int64 k);
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_LIB_SORTING_H_
|
||||
@@ -0,0 +1,190 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_LOCAL_CLIENT_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_LOCAL_CLIENT_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "absl/types/span.h"
|
||||
#include "tensorflow/compiler/xla/client/client.h"
|
||||
#include "tensorflow/compiler/xla/client/executable_build_options.h"
|
||||
#include "tensorflow/compiler/xla/client/xla_computation.h"
|
||||
#include "tensorflow/compiler/xla/executable_run_options.h"
|
||||
#include "tensorflow/compiler/xla/service/compiler.h"
|
||||
#include "tensorflow/compiler/xla/service/device_memory_allocator.h"
|
||||
#include "tensorflow/compiler/xla/service/executable.h"
|
||||
#include "tensorflow/compiler/xla/service/hlo.pb.h"
|
||||
#include "tensorflow/compiler/xla/service/local_service.h"
|
||||
#include "tensorflow/compiler/xla/service/shaped_buffer.h"
|
||||
#include "tensorflow/compiler/xla/statusor.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
#include "tensorflow/core/platform/stream_executor_no_cuda.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
class LocalExecutable {
|
||||
public:
|
||||
// Run the compiled computation with the given arguments and options and
|
||||
// return the result.
|
||||
StatusOr<ScopedShapedBuffer> Run(
|
||||
const absl::Span<const ShapedBuffer* const> arguments,
|
||||
ExecutableRunOptions run_options);
|
||||
|
||||
// Return the options used to build the executable.
|
||||
const ExecutableBuildOptions& build_options() const { return build_options_; }
|
||||
|
||||
// Return the built executable.
|
||||
Executable* executable() const { return executable_.get(); }
|
||||
|
||||
private:
|
||||
// Only a local client can construct these objects.
|
||||
friend class LocalClient;
|
||||
|
||||
// Constructor invoked by LocalClient.
|
||||
LocalExecutable(std::unique_ptr<Executable> executable, Backend* backend,
|
||||
ExecutableBuildOptions build_options);
|
||||
|
||||
// Validates that the given arguments and options satisfy various constraints
|
||||
// of the computation.
|
||||
//
|
||||
// The given ExecutableRunOptions override any values from legacy_flags
|
||||
// (TF_XLA_FLAGS environment variable).
|
||||
Status ValidateExecutionOptions(
|
||||
const absl::Span<const ShapedBuffer* const> arguments,
|
||||
const ExecutableRunOptions& run_options, const Backend& backend);
|
||||
|
||||
// Records the computation in a SessionModule proto with the arguments used to
|
||||
// invoke it, and the result. Enabled by flag: --tla_dump_executions_to.
|
||||
//
|
||||
// The given ServiceExecutableRunOptions override any values from legacy_flags
|
||||
// (TF_XLA_FLAGS environment variable).
|
||||
StatusOr<ScopedShapedBuffer> ExecuteAndDump(
|
||||
const ServiceExecutableRunOptions* run_options,
|
||||
const absl::Span<const ShapedBuffer* const> arguments);
|
||||
|
||||
// Records the arguments used to invoke the computation in a SessionModule
|
||||
// proto.
|
||||
Status RecordArguments(const absl::Span<const ShapedBuffer* const> arguments,
|
||||
HloSnapshot* hlo_snapshot);
|
||||
|
||||
// Records the result of the computation in a SessionModule proto.
|
||||
Status RecordResult(const ShapedBuffer* result, HloSnapshot* hlo_snapshot);
|
||||
|
||||
// Returns a literal containing the contents of the given ShapedBuffer.
|
||||
StatusOr<Literal> LiteralFromShapedBuffer(const ShapedBuffer& shaped_buffer);
|
||||
|
||||
// The ordinal of the device which this executable was compiled for. The
|
||||
// executable can run on all equivalent devices (as determined by
|
||||
// Backend::devices_equivalent).
|
||||
int build_device_ordinal() const { return build_options_.device_ordinal(); }
|
||||
|
||||
// Compiled computation.
|
||||
std::unique_ptr<Executable> executable_;
|
||||
|
||||
// Execution backend.
|
||||
Backend* backend_ = nullptr;
|
||||
|
||||
// Options used to build the executable.
|
||||
const ExecutableBuildOptions build_options_;
|
||||
};
|
||||
|
||||
// An XLA Client specialization for use when the client and service run in
|
||||
// the same process.
|
||||
class LocalClient : public Client {
|
||||
public:
|
||||
explicit LocalClient(LocalService* service)
|
||||
: Client(service), local_service_(service) {}
|
||||
|
||||
LocalClient(const LocalClient&) = delete;
|
||||
void operator=(const LocalClient&) = delete;
|
||||
|
||||
// Build and return a LocalExecutable object. The executable is compiled using
|
||||
// the given XlaComputation, argument layouts and options.
|
||||
//
|
||||
// The given ExecutableBuildOptions override any values from legacy_flags
|
||||
// (TF_XLA_FLAGS environment variable).
|
||||
StatusOr<std::unique_ptr<LocalExecutable>> Compile(
|
||||
const XlaComputation& computation,
|
||||
const absl::Span<const Shape* const> argument_layouts,
|
||||
const ExecutableBuildOptions& options);
|
||||
|
||||
// Copy the literal data to the device with the given ordinal and return as a
|
||||
// ScopedShapedBuffer. If non-null the given memory allocator is used for
|
||||
// device memory allocation. If null, the default memory allocator for the
|
||||
// device is used.
|
||||
StatusOr<ScopedShapedBuffer> LiteralToShapedBuffer(
|
||||
const Literal& literal, int device_ordinal,
|
||||
DeviceMemoryAllocator* allocator = nullptr);
|
||||
|
||||
// Copy the data from the device contained in the given ShapedBuffer and
|
||||
// return as a Literal.
|
||||
StatusOr<Literal> ShapedBufferToLiteral(const ShapedBuffer& shaped_buffer);
|
||||
|
||||
// Converts a GlobalDataHandle into a pointer to a ShapedBuffer that's valid
|
||||
// as long as the handle is valid.
|
||||
StatusOr<const ShapedBuffer*> GlobalDataToShapedBuffer(
|
||||
const GlobalDataHandle& data, int replica_number);
|
||||
|
||||
// Transfer the given literal to the infeed queue of the given device.
|
||||
// TODO(b/69670845): Remove the 'Local' from the name when LocalClient does
|
||||
// not inherit from Client and there is no possibility of confusion with
|
||||
// Client::TransferToInfeed.
|
||||
Status TransferToInfeedLocal(const Literal& literal, int device_ordinal);
|
||||
|
||||
// Transfer and return a value of the given shape from the outfeed of the
|
||||
// given device.
|
||||
// TODO(b/69670845): Remove the 'Local' from the name when LocalClient does
|
||||
// not inherit from Client and there is no possibility of confusion with
|
||||
// Client::TransferFromOutfeed.
|
||||
StatusOr<Literal> TransferFromOutfeedLocal(const Shape& shape,
|
||||
int device_ordinal);
|
||||
|
||||
// Returns the device ordinal that corresponds to the given replica number.
|
||||
//
|
||||
// This returns an error if there is not a one-to-one correspondence of
|
||||
// replicas to device ordinals, but is useful as a short term mechanism for
|
||||
// the "easy" case where a single replica is a single device.
|
||||
StatusOr<int> ReplicaNumberToDeviceOrdinal(int replica_number);
|
||||
|
||||
// Returns the platform that the underlying service targets.
|
||||
se::Platform* platform() const;
|
||||
|
||||
// Returns the number of devices on the system of the service platform
|
||||
// type. Not all devices may be supported by the service (see
|
||||
// device_ordinal_supported method).
|
||||
int device_count() const;
|
||||
|
||||
// Returns the default device ordinal that the service will run computations
|
||||
// on if no device ordinal is specified in execute options.
|
||||
int default_device_ordinal() const;
|
||||
|
||||
// Returns whether the device with the given ordinal can be used by the
|
||||
// service to execute computations. Not all devices of a particular platform
|
||||
// may be usable by the service (eg, a GPU with insufficient CUDA compute
|
||||
// capability).
|
||||
bool device_ordinal_supported(int device_ordinal) const;
|
||||
|
||||
// Returns the backend used to execute computations.
|
||||
const Backend& backend() const;
|
||||
Backend* mutable_backend();
|
||||
|
||||
private:
|
||||
LocalService* local_service_;
|
||||
};
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_LOCAL_CLIENT_H_
|
||||
@@ -0,0 +1,66 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_PADDING_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_PADDING_H_
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/types/span.h"
|
||||
#include "tensorflow/compiler/xla/statusor.h"
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Describes the padding applied for a windowed operation like
|
||||
// convolution, where a window is placed inside a base area.
|
||||
enum class Padding {
|
||||
// Make the output have the same dimensions as the base area. For
|
||||
// example, for a 3x3 base area and a 2x2 window, the output will be
|
||||
// 3x3, so that requires padding the 3x3 base area to 4x4.
|
||||
kSame,
|
||||
|
||||
// Use no padding. For example, for a 4x4 base area and a 2x2
|
||||
// window, the output will be 3x3.
|
||||
kValid,
|
||||
};
|
||||
|
||||
// Validates that the slices are acceptable for determining padding -- this can
|
||||
// be used to check the preconditions of MakePadding below to produce an error
|
||||
// message that can be returned to the user.
|
||||
Status ValidatePaddingValues(absl::Span<const int64> input_dimensions,
|
||||
absl::Span<const int64> window_dimensions,
|
||||
absl::Span<const int64> window_strides);
|
||||
|
||||
// Returns the padding needed for the base area, given the base area dimensions,
|
||||
// window dimensions, strides, and the type of padding.
|
||||
//
|
||||
// If v is the returned vector, then for each dimension number i,
|
||||
// v[i].first is the padding to the left (i.e. in the direction of
|
||||
// lower indices) and v[i].second is the padding to the right (i.e. in
|
||||
// the direction of higher indices).
|
||||
//
|
||||
// Precondition: The number of dimensions (i.e., rank) in input_dimensions,
|
||||
// window_dimensions, and strides must match, which is equal to the number
|
||||
// of elements in the result vector.
|
||||
std::vector<std::pair<int64, int64>> MakePadding(
|
||||
absl::Span<const int64> input_dimensions,
|
||||
absl::Span<const int64> window_dimensions,
|
||||
absl::Span<const int64> window_strides, Padding padding);
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_PADDING_H_
|
||||
@@ -0,0 +1,59 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_SHARDING_BUILDER_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_SHARDING_BUILDER_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/compiler/xla/array.h"
|
||||
#include "tensorflow/compiler/xla/shape_tree.h"
|
||||
#include "tensorflow/compiler/xla/shape_util.h"
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
#include "tensorflow/compiler/xla/util.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
|
||||
namespace xla {
|
||||
namespace sharding_builder {
|
||||
// A shaped array used to describe the assignment of tiles to devices.
|
||||
using TileAssignment = Array<int64>;
|
||||
|
||||
// Creates a replicated sharding - replicate a tensor on every device.
|
||||
OpSharding Replicate();
|
||||
|
||||
// Creates a sharding that assigns a tensor to just one device.
|
||||
OpSharding AssignDevice(int device);
|
||||
|
||||
// Creates a tiled sharding with the given tile shape and assignment of tiles
|
||||
// to devices.
|
||||
//
|
||||
// If tile_shape is not evenly divisible by the number of devices in
|
||||
// tile_assignment, operations behave as if implicit padding had been inserted.
|
||||
// The value of this padding is undefined.
|
||||
OpSharding Tile(const Shape& tile_shape, const TileAssignment& tile_assignment);
|
||||
|
||||
// Creates a sharding in one dimension, with the given tile shape which must
|
||||
// be rank 1 and using devices [0..num_tiles).
|
||||
//
|
||||
// This is simply a convenience wrapper for Tile().
|
||||
OpSharding Tile1D(const Shape& tile_shape, int64 num_tiles);
|
||||
|
||||
// Creates a tuple sharding from the given ShapeTree of element shardings.
|
||||
OpSharding Tuple(const ShapeTree<OpSharding>& shardings);
|
||||
|
||||
} // namespace sharding_builder
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_SHARDING_BUILDER_H_
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,67 @@
|
||||
/* 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_COMPILER_XLA_CLIENT_XLA_COMPUTATION_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_CLIENT_XLA_COMPUTATION_H_
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "tensorflow/compiler/xla/service/hlo.pb.h"
|
||||
#include "tensorflow/compiler/xla/status_macros.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// The computation graph that the user builds up with the XlaBuilder.
|
||||
class XlaComputation {
|
||||
public:
|
||||
XlaComputation() : unique_id_(-1) {}
|
||||
XlaComputation(const HloModuleProto& proto)
|
||||
: unique_id_(proto.id()), proto_(proto) {}
|
||||
|
||||
~XlaComputation() {}
|
||||
|
||||
XlaComputation(const XlaComputation&) = delete;
|
||||
XlaComputation& operator=(const XlaComputation&) = delete;
|
||||
|
||||
XlaComputation(XlaComputation&& from) = default;
|
||||
|
||||
XlaComputation& operator=(XlaComputation&& from) = default;
|
||||
|
||||
// Returns the "program shape" (parameter and return shapes) for this
|
||||
// computation.
|
||||
StatusOr<ProgramShape> GetProgramShape() const;
|
||||
|
||||
const HloModuleProto& proto() const { return proto_; }
|
||||
|
||||
// Requests that we snapshot the computation into a serializable protocol
|
||||
// buffer form.
|
||||
StatusOr<std::unique_ptr<HloSnapshot>> Snapshot() const;
|
||||
|
||||
// Returns true if this object is a null Computation.
|
||||
bool IsNull() const { return unique_id_ == -1; }
|
||||
|
||||
private:
|
||||
XlaComputation(const int64 unique_id) : unique_id_(unique_id) {}
|
||||
HloModuleProto* mutable_proto() { return &proto_; }
|
||||
friend class XlaBuilder;
|
||||
|
||||
int64 unique_id_;
|
||||
HloModuleProto proto_;
|
||||
};
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_CLIENT_XLA_COMPUTATION_H_
|
||||
@@ -0,0 +1,105 @@
|
||||
/* 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_COMPILER_XLA_EXECUTABLE_RUN_OPTIONS_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_EXECUTABLE_RUN_OPTIONS_H_
|
||||
|
||||
// Pulls in the ::stream_executor -> ::xla::se namespace alias.
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
|
||||
// These classes are forward declared so that ExecutableRunOptions can be linked
|
||||
// into an XLA-compiled binary without having to link all of the pointed-to
|
||||
// objects (e.g., for an ahead-of-time compiled CPU binary, the gpu tools don't
|
||||
// need to be linked).
|
||||
namespace stream_executor {
|
||||
class Stream;
|
||||
class Platform;
|
||||
} // namespace stream_executor
|
||||
|
||||
namespace tensorflow {
|
||||
namespace thread {
|
||||
class ThreadPool;
|
||||
} // namespace thread
|
||||
} // namespace tensorflow
|
||||
|
||||
namespace Eigen {
|
||||
struct ThreadPoolDevice;
|
||||
} // namespace Eigen
|
||||
|
||||
namespace xla {
|
||||
|
||||
class DeviceMemoryAllocator;
|
||||
class DeviceAssignment;
|
||||
class ExecutionProfile;
|
||||
|
||||
// Class containing options for running a LocalExecutable.
|
||||
class ExecutableRunOptions {
|
||||
public:
|
||||
// Specifies the allocator to use during execution.
|
||||
ExecutableRunOptions& set_allocator(DeviceMemoryAllocator* allocator);
|
||||
DeviceMemoryAllocator* allocator() const;
|
||||
|
||||
// If set, this is the device to run the computation on. Valid device_ordinal
|
||||
// values are: 0 to # of devices - 1. These values are identical to the device
|
||||
// ordinal values used by StreamExecutor. The device must be of the same type
|
||||
// as the executable was compiled for. A value of -1 indicates this option has
|
||||
// not been set.
|
||||
ExecutableRunOptions& set_device_ordinal(int device_ordinal);
|
||||
int device_ordinal() const;
|
||||
|
||||
// If set, this is the stream to run the computation on. The platform of the
|
||||
// stream must match the platform the executable was built for. A value of
|
||||
// nullptr indicates the option has not been set.
|
||||
ExecutableRunOptions& set_stream(stream_executor::Stream* stream);
|
||||
stream_executor::Stream* stream() const;
|
||||
|
||||
// If set, this is the stream to perform any pre-computation transfers on.
|
||||
// The platform of the stream must match the platform the executable was
|
||||
// built for. A value of nullptr indicates the option has not been set.
|
||||
ExecutableRunOptions& set_host_to_device_stream(
|
||||
stream_executor::Stream* stream);
|
||||
stream_executor::Stream* host_to_device_stream() const;
|
||||
|
||||
// Sets the thread pool device on which to run Eigen subcomputations.
|
||||
// Does not take ownership.
|
||||
ExecutableRunOptions& set_intra_op_thread_pool(
|
||||
const Eigen::ThreadPoolDevice* intra_op_thread_pool);
|
||||
const Eigen::ThreadPoolDevice* intra_op_thread_pool() const;
|
||||
|
||||
// If set, profiling information is written to 'profile'.
|
||||
ExecutionProfile* execution_profile() const;
|
||||
ExecutableRunOptions& set_execution_profile(ExecutionProfile* profile);
|
||||
|
||||
ExecutableRunOptions& set_device_assignment(
|
||||
DeviceAssignment* device_assignment);
|
||||
const DeviceAssignment* device_assignment() const;
|
||||
|
||||
ExecutableRunOptions& set_rng_seed(int rng_seed);
|
||||
int rng_seed() const;
|
||||
|
||||
private:
|
||||
DeviceMemoryAllocator* allocator_ = nullptr;
|
||||
int device_ordinal_ = -1;
|
||||
DeviceAssignment* device_assignment_ = nullptr;
|
||||
stream_executor::Stream* stream_ = nullptr;
|
||||
const Eigen::ThreadPoolDevice* intra_op_thread_pool_ = nullptr;
|
||||
ExecutionProfile* execution_profile_ = nullptr;
|
||||
int rng_seed_ = 0;
|
||||
stream_executor::Stream* host_to_device_stream_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_EXECUTABLE_RUN_OPTIONS_H_
|
||||
@@ -0,0 +1,29 @@
|
||||
/* 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_COMPILER_XLA_EXECUTION_OPTIONS_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_EXECUTION_OPTIONS_UTIL_H_
|
||||
|
||||
#include "tensorflow/compiler/xla/xla.pb.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Create a default ExecutionOptions proto; this proto has its debug options
|
||||
// popupated to the default values taken from flags.
|
||||
ExecutionOptions CreateDefaultExecutionOptions();
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_EXECUTION_OPTIONS_UTIL_H_
|
||||
@@ -0,0 +1,88 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// Utility functions related to layouts of Shapes.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_XLA_INDEX_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_INDEX_UTIL_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "absl/types/span.h"
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Namespaced collection of (static) utilities related to indexing into
|
||||
// multidimensional arrays.
|
||||
class IndexUtil {
|
||||
public:
|
||||
// Converts a multidimensional index (eg {x, y, z}) into a linear index based
|
||||
// on the shape and its layout. The first index in the multi_index is
|
||||
// dimension 0.
|
||||
static int64 MultidimensionalIndexToLinearIndex(
|
||||
const Shape& shape, absl::Span<const int64> multi_index);
|
||||
|
||||
// Converts a linear index into multidimensional index (eg {x, y, z}) based on
|
||||
// the shape and its layout. The first index in the returned multidimensional
|
||||
// index is dimension 0.
|
||||
static std::vector<int64> LinearIndexToMultidimensionalIndex(
|
||||
const Shape& shape, int64 linear_index);
|
||||
|
||||
// Bumps a sequence of indices; e.g. {0,0,0,0} up by one index value; e.g. to
|
||||
// {0,0,0,1}. This is akin to std::next_permutation. If the index hits a limit
|
||||
// for the provided shape, the next most significant index is bumped, in a
|
||||
// counting-up process.
|
||||
//
|
||||
// E.g. for shape f32[2,3]
|
||||
// {0,0}=>{0,1}
|
||||
// {0,1}=>{0,2}
|
||||
// {0,2}=>{1,0}
|
||||
// etc.
|
||||
//
|
||||
// This is useful for traversing the indices in a literal.
|
||||
//
|
||||
// Returns true iff the indices were successfully bumped; false if we've hit
|
||||
// the limit where it can no longer be bumped in-bounds.
|
||||
static bool BumpIndices(const Shape& shape, absl::Span<int64> indices);
|
||||
|
||||
// Calculates the stride size (in number of elements, not byte size) of a
|
||||
// given logical shape dimension (from 0 to rank-1). If available, padded
|
||||
// dimensions are used.
|
||||
// Example:
|
||||
// GetDimensionStride(F32[5,8,10,4]{3,2,1,0}, 1) ==
|
||||
// sizeof(dimension(3)) * sizeof(dimension(2)) == 4 * 10
|
||||
static int64 GetDimensionStride(const Shape& shape, int64 dimension);
|
||||
|
||||
// Returns true iff the given multi-index is contained in the bounds for the
|
||||
// shape.
|
||||
static bool IndexInBounds(const Shape& shape, absl::Span<const int64> index);
|
||||
|
||||
// Compares the given indices in lexicographic order. lhs[0] and rhs[0] are
|
||||
// compared first, and lhs[rank-1] and rhs[rank-1] last. If lhs is larger,
|
||||
// then -1 is returned. If rhs is larger, then 1 is returned. Otherwise, 0 is
|
||||
// returned.
|
||||
static int CompareIndices(absl::Span<const int64> lhs,
|
||||
absl::Span<const int64> rhs);
|
||||
|
||||
private:
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(IndexUtil);
|
||||
};
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_INDEX_UTIL_H_
|
||||
@@ -0,0 +1,98 @@
|
||||
/* 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_COMPILER_XLA_ITERATOR_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_ITERATOR_UTIL_H_
|
||||
|
||||
#include <iterator>
|
||||
#include <utility>
|
||||
|
||||
namespace xla {
|
||||
|
||||
// UnwrappingIterator is a transforming iterator that calls get() on the
|
||||
// elements it returns.
|
||||
//
|
||||
// Together with tensorflow::gtl::iterator_range, this lets classes which
|
||||
// contain a collection of smart pointers expose a view of raw pointers to
|
||||
// consumers. For example:
|
||||
//
|
||||
// class MyContainer {
|
||||
// public:
|
||||
// tensorflow::gtl::iterator_range<
|
||||
// UnwrappingIterator<std::vector<std::unique_ptr<Thing>>::iterator>>
|
||||
// things() {
|
||||
// return {MakeUnwrappingIterator(things_.begin()),
|
||||
// MakeUnwrappingIterator(things_.end())};
|
||||
// }
|
||||
//
|
||||
// tensorflow::gtl::iterator_range<UnwrappingIterator<
|
||||
// std::vector<std::unique_ptr<Thing>>::const_iterator>>
|
||||
// things() const {
|
||||
// return {MakeUnwrappingIterator(things_.begin()),
|
||||
// MakeUnwrappingIterator(things_.end())};
|
||||
// }
|
||||
//
|
||||
// private:
|
||||
// std::vector<std::unique_ptr<Thing>> things_;
|
||||
// };
|
||||
//
|
||||
// MyContainer container = ...;
|
||||
// for (Thing* t : container.things()) {
|
||||
// ...
|
||||
// }
|
||||
//
|
||||
// For simplicity, UnwrappingIterator is currently unconditionally an
|
||||
// input_iterator -- it doesn't inherit any superpowers NestedIterator may have.
|
||||
template <typename NestedIter>
|
||||
class UnwrappingIterator
|
||||
: public std::iterator<std::input_iterator_tag,
|
||||
decltype(std::declval<NestedIter>()->get())> {
|
||||
private:
|
||||
NestedIter iter_;
|
||||
|
||||
public:
|
||||
explicit UnwrappingIterator(NestedIter iter) : iter_(std::move(iter)) {}
|
||||
|
||||
auto operator*() -> decltype(iter_->get()) { return iter_->get(); }
|
||||
auto operator-> () -> decltype(iter_->get()) { return iter_->get(); }
|
||||
UnwrappingIterator& operator++() {
|
||||
++iter_;
|
||||
return *this;
|
||||
}
|
||||
UnwrappingIterator operator++(int) {
|
||||
UnwrappingIterator temp(iter_);
|
||||
operator++();
|
||||
return temp;
|
||||
}
|
||||
|
||||
friend bool operator==(const UnwrappingIterator& a,
|
||||
const UnwrappingIterator& b) {
|
||||
return a.iter_ == b.iter_;
|
||||
}
|
||||
|
||||
friend bool operator!=(const UnwrappingIterator& a,
|
||||
const UnwrappingIterator& b) {
|
||||
return !(a == b);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename NestedIter>
|
||||
UnwrappingIterator<NestedIter> MakeUnwrappingIterator(NestedIter iter) {
|
||||
return UnwrappingIterator<NestedIter>(std::move(iter));
|
||||
}
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_ITERATOR_UTIL_H_
|
||||
@@ -0,0 +1,211 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// Utility functions related to layouts of Shapes.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_XLA_LAYOUT_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_LAYOUT_UTIL_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "absl/types/span.h"
|
||||
#include "tensorflow/compiler/xla/status.h"
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Namespaced collection of (static) Layout utilities.
|
||||
class LayoutUtil {
|
||||
public:
|
||||
// Creates a layout with the given minor-to-major dimension order. (This is a
|
||||
// convenience function for protobuf construction.)
|
||||
static Layout MakeLayout(absl::Span<const int64> minor_to_major);
|
||||
|
||||
// Similar to MakeLayout, but take indices in reverse order.
|
||||
static Layout MakeLayoutFromMajorToMinor(
|
||||
absl::Span<const int64> major_to_minor);
|
||||
|
||||
// Creates a sparse layout with the given maximum number of elements. (This is
|
||||
// a convenience function for protobuf construction.)
|
||||
static Layout MakeSparseLayout(int64 max_sparse_elements);
|
||||
|
||||
// Returns default layout for the given shape.
|
||||
static Layout GetDefaultLayoutForShape(const Shape& shape);
|
||||
|
||||
// Helper functions that create default layouts for various ranks.
|
||||
static Layout GetDefaultLayoutForRank(int64 rank);
|
||||
static Layout GetDefaultLayoutForR2();
|
||||
static Layout GetDefaultLayoutForR3();
|
||||
static Layout GetDefaultLayoutForR4();
|
||||
|
||||
// Sets the default layout on the Shape.
|
||||
static void SetToDefaultLayout(Shape* shape);
|
||||
|
||||
// Returns a shape with the same dimensions as `shape` but with the default
|
||||
// layout.
|
||||
static Shape GetWithDefaultLayout(const Shape& shape);
|
||||
|
||||
// Sets the layouts of all Shapes within the given ProgramShape to the
|
||||
// default.
|
||||
static void SetToDefaultLayout(ProgramShape* program_shape);
|
||||
|
||||
// Validates that the layout within the given shape is correct.
|
||||
static Status ValidateLayoutInShape(const Shape& shape);
|
||||
|
||||
// Validates that the provided layout satisfies invariants for the given
|
||||
// shape.
|
||||
static Status ValidateLayoutForShape(const Layout& layout,
|
||||
const Shape& shape);
|
||||
|
||||
// Clears the layout in the given Shape. After this function is called,
|
||||
// HasLayout will return false for the shape.
|
||||
static void ClearLayout(Shape* shape);
|
||||
|
||||
// Clears the layout on all Shapes within the given ProgramShape.
|
||||
static void ClearLayout(ProgramShape* program_shape);
|
||||
|
||||
// Returns whether the given Shape is an array and has a dense format layout.
|
||||
static bool IsDenseArray(const Shape& shape);
|
||||
|
||||
// Returns whether the given Layout has a dense format.
|
||||
static bool IsDense(const Layout& layout);
|
||||
|
||||
// Returns whether the layout is monotonic and dim 0 is minor in the layout.
|
||||
// * R0 and R1: this is always trivially true.
|
||||
// * R2+: equivalent to column-major. Dimension 0 is the minor, dimension 1 is
|
||||
// more major, and so on until dimension N-1 which is the major.
|
||||
static bool IsMonotonicWithDim0Minor(const Layout& layout);
|
||||
|
||||
// Returns whether the layout is monotonic and dim 0 is major in the layout.
|
||||
// * R0 and R1: this is always trivially true.
|
||||
// * R2+: equivalent to row-major. Dimension 0 is the major, dimension 1 is
|
||||
// more minor, and so on until dimension N-1 which is the minor.
|
||||
static bool IsMonotonicWithDim0Major(const Layout& layout);
|
||||
|
||||
// Returns whether the layout of the given shape has padding (a
|
||||
// padded_dimension value in Layout is greater than the corresponding
|
||||
// dimension size).
|
||||
static bool IsPadded(const Shape& shape);
|
||||
|
||||
// Returns the padded_dimensions array for the given Shape. Requires that the
|
||||
// shape is an array and has a dense layout.
|
||||
static absl::Span<const int64> PaddedDimensions(const Shape& shape);
|
||||
|
||||
// Returns the given index of the padded_dimensions array for the given Shape.
|
||||
// Requires that the shape is an array and has a dense layout.
|
||||
static int64 PaddedDimension(const Shape& shape, int64 index);
|
||||
|
||||
// Returns the padding_value for the given Shape. Requires that the shape is
|
||||
// an array and has a dense layout.
|
||||
static PaddingValue GetPaddingValue(const Shape& shape);
|
||||
|
||||
// Returns whether the given Shape is an array (i.e. not a tuple) and has a
|
||||
// sparse format layout.
|
||||
static bool IsSparseArray(const Shape& shape);
|
||||
|
||||
// Returns whether the given Layout has a sparse format.
|
||||
static bool IsSparse(const Layout& layout);
|
||||
|
||||
// Returns the maximum number of elements that can be stored in a sparse
|
||||
// layout.
|
||||
static int64 MaxSparseElements(const Layout& layout);
|
||||
|
||||
// Returns whether the given shape has a layout. For tuple shapes, true is
|
||||
// returned only if all elements have layouts.
|
||||
static bool HasLayout(const Shape& shape);
|
||||
|
||||
// Returns whether all Shapes within the given ProgramShape have layouts.
|
||||
static bool HasLayout(const ProgramShape& program_shape);
|
||||
|
||||
// Returns whether lhs and rhs are identical.
|
||||
static bool Equal(const Layout& lhs, const Layout& rhs);
|
||||
|
||||
// Returns the minor_to_major array for the given Shape. Requires that the
|
||||
// shape is an array and has a dense layout.
|
||||
static absl::Span<const int64> MinorToMajor(const Shape& shape);
|
||||
static absl::Span<const int64> MinorToMajor(const Layout& layout);
|
||||
|
||||
// Major(0) is the most major logical dimension number, Major(1) is the
|
||||
// second-most-major logical dimension number and so on.
|
||||
//
|
||||
// This can be used to translate physical dimension numbers to logical
|
||||
// dimension numbers. Assume that we are numbering the physical dimensions so
|
||||
// that the most major physical dimension has physical dimension number 0 and
|
||||
// so on. Then a physical dimension number p corresponds to the logical
|
||||
// dimension number Major(p). So this function could also be called
|
||||
// PhysicalToLogical().
|
||||
//
|
||||
// As an example, consider physical dimension number 0, which by definition is
|
||||
// the most major. Then Major(0) is the most major logical dimension, so Major
|
||||
// maps the physical dimension number 0 to the most major logical dimension
|
||||
// number Major(0).
|
||||
static int64 Major(const Layout& layout, int64 physical_dimension_number);
|
||||
|
||||
// Minor(0) is the most minor logical dimension number, minor(1) is the
|
||||
// second-most-minor logical dimension number and so on.
|
||||
static int64 Minor(const Layout& layout, int64 physical_dimension_number);
|
||||
|
||||
// Returns the inverse mapping of the Major() function. More precisely, return
|
||||
// a vector v such that if l == Major(p), then v[l] == p.
|
||||
//
|
||||
// This can be used to translate logical dimension numbers into physical
|
||||
// dimension numbers. Assume that we are numbering the physical dimensions so
|
||||
// that the most major physical dimension has physical dimension number 0 and
|
||||
// so on. Then a logical dimension number l corresponds to the physical
|
||||
// dimension number MakeLogicalToPhysical(layout)[l].
|
||||
//
|
||||
// As an example, consider physical dimension number 0, which by definition is
|
||||
// the most major. Then l := Major(0) is the most major logical dimension. If
|
||||
// v is the vector returned from this function, then v[l] == 0. So v maps the
|
||||
// most major logical dimension l to the physical dimension number 0.
|
||||
static std::vector<int64> MakeLogicalToPhysical(const Layout& layout);
|
||||
|
||||
// Returns a human-readable string that represents the given layout.
|
||||
static string HumanString(const Layout& layout);
|
||||
|
||||
// Copies the layout from 'src' to 'dst'. Recursively copies layouts of
|
||||
// tuples. 'src' and 'dst' need not be compatible but the two shapes must
|
||||
// have the same tuple structure (if any) and arrays must have the same
|
||||
// rank. within the shapes must have the same number of dimensions.
|
||||
static Status CopyLayoutBetweenShapes(const Shape& src, Shape* dst);
|
||||
|
||||
// Returns true if the layouts of lhs and rhs are equal, false
|
||||
// otherwise. Recursively compares layouts of tuples.
|
||||
//
|
||||
// lhs and rhs need not be compatible to have the same layout but the two
|
||||
// shapes must have the same tuple structure (if any) and arrays must have the
|
||||
// same rank. Element type is ignored.
|
||||
static bool LayoutsInShapesEqual(const Shape& lhs, const Shape& rhs);
|
||||
|
||||
// Returns whether the given dimensions are consecutive in the given layout,
|
||||
// not necessarily in the order given.
|
||||
static bool AreDimensionsConsecutive(const Layout& layout,
|
||||
absl::Span<const int64> dims);
|
||||
|
||||
// Compute a hash for `layout`.
|
||||
static size_t Hash(const Layout& layout);
|
||||
|
||||
private:
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(LayoutUtil);
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const Layout& layout);
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_LAYOUT_UTIL_H_
|
||||
@@ -0,0 +1,38 @@
|
||||
/* 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_COMPILER_XLA_LEGACY_FLAGS_DEBUG_OPTIONS_FLAGS_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_LEGACY_FLAGS_DEBUG_OPTIONS_FLAGS_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/compiler/xla/xla.pb.h"
|
||||
#include "tensorflow/core/util/command_line_flags.h"
|
||||
|
||||
namespace xla {
|
||||
namespace legacy_flags {
|
||||
|
||||
// Appends flag definitions for debug options to flag_list.
|
||||
void AppendDebugOptionsFlags(std::vector<tensorflow::Flag>* flag_list);
|
||||
|
||||
// Fetches a DebugOptions proto message from flags provided to the program.
|
||||
// Flags must be registered with the flags parser using AppendDebugOptionsFlags
|
||||
// first.
|
||||
xla::DebugOptions GetDebugOptionsFromFlags();
|
||||
|
||||
} // namespace legacy_flags
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_LEGACY_FLAGS_DEBUG_OPTIONS_FLAGS_H_
|
||||
@@ -0,0 +1,147 @@
|
||||
/* 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_COMPILER_XLA_LEGACY_FLAGS_DEBUG_OPTIONS_PARSERS_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_LEGACY_FLAGS_DEBUG_OPTIONS_PARSERS_H_
|
||||
|
||||
#include <vector>
|
||||
#include "absl/strings/numbers.h"
|
||||
#include "absl/strings/str_split.h"
|
||||
#include "tensorflow/compiler/xla/service/hlo_opcode.h"
|
||||
#include "tensorflow/compiler/xla/xla.pb.h"
|
||||
|
||||
namespace xla {
|
||||
namespace legacy_flags {
|
||||
namespace impl {
|
||||
|
||||
template <typename T>
|
||||
void parse_xla_backend_extra_options(T* extra_options_map,
|
||||
string comma_separated_values) {
|
||||
std::vector<string> extra_options_parts =
|
||||
absl::StrSplit(comma_separated_values, ',');
|
||||
|
||||
// The flag contains a comma-separated list of options; some options
|
||||
// have arguments following "=", some don't.
|
||||
for (const auto& part : extra_options_parts) {
|
||||
size_t eq_pos = part.find_first_of('=');
|
||||
if (eq_pos == string::npos) {
|
||||
(*extra_options_map)[part] = "";
|
||||
} else {
|
||||
string value = "";
|
||||
if (eq_pos + 1 < part.size()) {
|
||||
value = part.substr(eq_pos + 1);
|
||||
}
|
||||
(*extra_options_map)[part.substr(0, eq_pos)] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The --xla_reduce_precision option has the format "LOCATION=E,M:OPS;NAME",
|
||||
// where LOCATION is an HloReducePrecisionOptions::location, E and M are
|
||||
// integers for the exponent and matissa bit counts respectively, and OPS and
|
||||
// NAMES are comma-separated of the operation types and names to which to
|
||||
// attach the reduce-precision operations. The OPS values are matches to the
|
||||
// strings produced by HloOpcodeString, while the NAME values are arbitrary
|
||||
// strings subject to the requirements that they not contain any of "=,:;".
|
||||
// The NAME string (with its preceding semicolon) is optional.
|
||||
inline bool parse_xla_reduce_precision_option(
|
||||
HloReducePrecisionOptions* options, string option_string) {
|
||||
// Split off "LOCATION" from remainder of string.
|
||||
std::vector<string> eq_split = absl::StrSplit(option_string, '=');
|
||||
if (eq_split.size() != 2) {
|
||||
return false;
|
||||
}
|
||||
string& location = eq_split[0];
|
||||
if (location == "OP_INPUTS") {
|
||||
options->set_location(HloReducePrecisionOptions::OP_INPUTS);
|
||||
} else if (location == "OP_OUTPUTS") {
|
||||
options->set_location(HloReducePrecisionOptions::OP_OUTPUTS);
|
||||
} else if (location == "UNFUSED_OP_OUTPUTS") {
|
||||
options->set_location(HloReducePrecisionOptions::UNFUSED_OP_OUTPUTS);
|
||||
} else if (location == "FUSION_INPUTS_BY_CONTENT") {
|
||||
options->set_location(HloReducePrecisionOptions::FUSION_INPUTS_BY_CONTENT);
|
||||
} else if (location == "FUSION_OUTPUTS_BY_CONTENT") {
|
||||
options->set_location(HloReducePrecisionOptions::FUSION_OUTPUTS_BY_CONTENT);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Split off "E,M" from remainder of string.
|
||||
std::vector<string> colon_split = absl::StrSplit(eq_split[1], ':');
|
||||
if (colon_split.size() != 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Split E and M, and parse.
|
||||
std::vector<int32> bitsizes;
|
||||
for (const auto& s : absl::StrSplit(colon_split[0], ',')) {
|
||||
bitsizes.emplace_back();
|
||||
if (!absl::SimpleAtoi(s, &bitsizes.back())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
options->set_exponent_bits(bitsizes[0]);
|
||||
options->set_mantissa_bits(bitsizes[1]);
|
||||
|
||||
// Split off OPS comma-separated list from remainder of string, if the
|
||||
// remainder exists.
|
||||
std::vector<string> semicolon_split = absl::StrSplit(colon_split[1], ';');
|
||||
if (semicolon_split.size() > 2) {
|
||||
return false;
|
||||
}
|
||||
// The opcode values are either 'all' (meaning all opcodes), or matches to
|
||||
// the strings returned by HloOpcodeString. An empty string is also
|
||||
// interpreted as 'all', for convenience. Note that 'all' may not be part
|
||||
// of a comma-separated list; it must stand alone.
|
||||
string& opcode_string = semicolon_split[0];
|
||||
if (opcode_string == "" || opcode_string == "all") {
|
||||
for (int i = 0; i < HloOpcodeCount(); i++) {
|
||||
options->add_opcodes_to_suffix(i);
|
||||
}
|
||||
} else {
|
||||
std::vector<string> opcodes = absl::StrSplit(opcode_string, ',');
|
||||
for (const string& opcode : opcodes) {
|
||||
bool found = false;
|
||||
for (int i = 0; i < HloOpcodeCount(); i++) {
|
||||
if (opcode == HloOpcodeString(static_cast<HloOpcode>(i))) {
|
||||
options->add_opcodes_to_suffix(i);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process the NAMES string, if it exists.
|
||||
if (semicolon_split.size() == 2) {
|
||||
std::vector<string> opnames = absl::StrSplit(semicolon_split[1], ',');
|
||||
for (const string& opname : opnames) {
|
||||
if (opname.length() > 0) {
|
||||
options->add_opname_substrings_to_suffix(opname);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace impl
|
||||
} // namespace legacy_flags
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_LEGACY_FLAGS_DEBUG_OPTIONS_PARSERS_H_
|
||||
@@ -0,0 +1,66 @@
|
||||
/* 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_COMPILER_XLA_LEGACY_FLAGS_PARSE_FLAGS_FROM_ENV_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_LEGACY_FLAGS_PARSE_FLAGS_FROM_ENV_H_
|
||||
|
||||
// This module exports ParseFlagsFromEnv(), which allows other modules to parse
|
||||
// flags from the environtment variable TF_XLA_FLAGS, or (if the first
|
||||
// non-whitespace in the variable value is not '-'), a file named by that
|
||||
// environment variable. The accepted syntax is that flags arguments are of
|
||||
// the form --flag=value or (for boolean flags) --flag, and are whitespace
|
||||
// separated. The <value> may be one of:
|
||||
// - <non-whitespace, non-nul not starting with single-quote or double-quote>
|
||||
// in which case the effective value is the string itself
|
||||
// - <single-quote><characters string not containing nul or
|
||||
// single-quote><single_quote> in which case the effective value is the
|
||||
// string with the single-quotes removed
|
||||
// - <double-quote><character string not containing nul or unesecaped
|
||||
// double-quote><double_quote> in which case the effective value if the
|
||||
// string with the double-quotes removed, and escaped sequences of
|
||||
// <backslash><char> replaced by <char>.
|
||||
//
|
||||
// Flags values inconsistent with the type of the flag will be rejected by the
|
||||
// flag parser.
|
||||
//
|
||||
// Examples:
|
||||
// TF_XLA_FLAGS="--foo=bar --wombat='value with a space'"
|
||||
//
|
||||
// TF_XLA_FLAGS=/tmp/flagfile
|
||||
// where /tmp/flagfile might contain
|
||||
// --some_flag="This is a string containing a \" and a '."
|
||||
// --another_flag=wombats
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
#include "tensorflow/core/util/command_line_flags.h"
|
||||
|
||||
namespace xla {
|
||||
namespace legacy_flags {
|
||||
|
||||
// Call tensorflow::Flags::Parse(argc, argv, flag_list) against any as yet
|
||||
// unrecognized flags passed in from the environment, and return its
|
||||
// return value.
|
||||
bool ParseFlagsFromEnv(const std::vector<tensorflow::Flag>& flag_list);
|
||||
|
||||
// Used only for testing. Not to be used by clients.
|
||||
void ResetFlagsFromEnvForTesting(int** pargc, std::vector<char*>** pargv);
|
||||
|
||||
} // namespace legacy_flags
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_LEGACY_FLAGS_PARSE_FLAGS_FROM_ENV_H_
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,591 @@
|
||||
/* 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.
|
||||
==============================================================================*/
|
||||
|
||||
// Utilities for dealing with Literal protobufs.
|
||||
|
||||
#ifndef TENSORFLOW_COMPILER_XLA_LITERAL_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_LITERAL_UTIL_H_
|
||||
|
||||
#include <functional>
|
||||
#include <initializer_list>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/memory/memory.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/types/span.h"
|
||||
#include "tensorflow/compiler/xla/array2d.h"
|
||||
#include "tensorflow/compiler/xla/array3d.h"
|
||||
#include "tensorflow/compiler/xla/array4d.h"
|
||||
#include "tensorflow/compiler/xla/index_util.h"
|
||||
#include "tensorflow/compiler/xla/layout_util.h"
|
||||
#include "tensorflow/compiler/xla/literal.h"
|
||||
#include "tensorflow/compiler/xla/primitive_util.h"
|
||||
#include "tensorflow/compiler/xla/shape_util.h"
|
||||
#include "tensorflow/compiler/xla/sparse_index_array.h"
|
||||
#include "tensorflow/compiler/xla/status_macros.h"
|
||||
#include "tensorflow/compiler/xla/types.h"
|
||||
#include "tensorflow/compiler/xla/util.h"
|
||||
#include "tensorflow/compiler/xla/xla_data.pb.h"
|
||||
#include "tensorflow/core/lib/core/bitmap.h"
|
||||
#include "tensorflow/core/lib/core/status.h"
|
||||
#include "tensorflow/core/platform/logging.h"
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
#include "tensorflow/core/platform/protobuf.h"
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
class LiteralUtil {
|
||||
public:
|
||||
LiteralUtil() = delete;
|
||||
|
||||
// Returns a literal scalar representing the first element.
|
||||
static Literal GetFirstScalarLiteral(const LiteralSlice& literal);
|
||||
|
||||
// Creates a new literal of a given rank. To minimize ambiguity (for users
|
||||
// and the compiler) these CreateR[0-2] methods should explicitly specify the
|
||||
// native type. For example:
|
||||
//
|
||||
// CreateR1<float>({1.0, 42.0});
|
||||
// CreateR2<uint32>({{1, 2}, {3, 4}});
|
||||
//
|
||||
// The variants not ending with WithLayout use the default XLA layout for the
|
||||
// literal's linear representation in memory.
|
||||
template <typename NativeT>
|
||||
static Literal CreateR0(NativeT value);
|
||||
template <typename NativeT>
|
||||
static Literal CreateR1(absl::Span<const NativeT> values);
|
||||
static Literal CreateR1(const tensorflow::core::Bitmap& values);
|
||||
template <typename NativeT>
|
||||
static Literal CreateR2(
|
||||
std::initializer_list<std::initializer_list<NativeT>> values);
|
||||
template <typename NativeT>
|
||||
static Literal CreateR2WithLayout(
|
||||
std::initializer_list<std::initializer_list<NativeT>> values,
|
||||
const Layout& layout);
|
||||
template <typename NativeT>
|
||||
static Literal CreateR3(std::initializer_list<
|
||||
std::initializer_list<std::initializer_list<NativeT>>>
|
||||
values);
|
||||
template <typename NativeT>
|
||||
static Literal CreateR3WithLayout(
|
||||
std::initializer_list<
|
||||
std::initializer_list<std::initializer_list<NativeT>>>
|
||||
values,
|
||||
const Layout& layout);
|
||||
template <typename NativeT>
|
||||
static Literal CreateR4(
|
||||
std::initializer_list<std::initializer_list<
|
||||
std::initializer_list<std::initializer_list<NativeT>>>>
|
||||
values);
|
||||
template <typename NativeT>
|
||||
static Literal CreateR4WithLayout(
|
||||
std::initializer_list<std::initializer_list<
|
||||
std::initializer_list<std::initializer_list<NativeT>>>>
|
||||
values,
|
||||
const Layout& layout);
|
||||
|
||||
// Creates a literal with a sparse layout and the given indices and values.
|
||||
// The shape is initialized from the given dimensions. The minor dimension of
|
||||
// the indices array must equal the rank of the shape (i.e. size of the
|
||||
// dimensions array). The major dimension of the indices array must equal the
|
||||
// number of elements in the values array. The maximum number of elements in
|
||||
// the array is taken from the max_indices() value of the index array.
|
||||
//
|
||||
// XLA assumes that sparse literals are in sorted order for all operations. If
|
||||
// the `sort` argument is true, then the indices and values will be sorted
|
||||
// while copying them into the literal. If you have ensured that the indices
|
||||
// and values are already sorted, then you may set the `sort` argument to
|
||||
// false to skip the sorting step.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// CreateSparse(
|
||||
// {12, 12, 12},
|
||||
// SparseIndexArray(10, 3,
|
||||
// Array2D{
|
||||
// {0, 1, 2},
|
||||
// {3, 4, 5},
|
||||
// {6, 7, 8},
|
||||
// {9, 10, 11},
|
||||
// }),
|
||||
// {1.0, 2.0 3.0, 4.0})
|
||||
//
|
||||
// This creates an array with shape F64[12,12,12]sparse{10}, that has the
|
||||
// following non-zero values:
|
||||
//
|
||||
// [0, 1, 2]: 1.0
|
||||
// [3, 4, 5]: 2.0
|
||||
// [6, 7, 8]: 3.0
|
||||
// [9, 10, 11]: 4.0
|
||||
//
|
||||
template <typename NativeT>
|
||||
static Literal CreateSparse(absl::Span<const int64> dimensions,
|
||||
SparseIndexArray indices,
|
||||
absl::Span<const NativeT> values,
|
||||
bool sort = true);
|
||||
|
||||
// Creates a scalar literal value zero of the given primitive type.
|
||||
static Literal Zero(PrimitiveType primitive_type);
|
||||
// Creates a scalar literal value one of the given primitive type.
|
||||
static Literal One(PrimitiveType primitive_type);
|
||||
// Creates a scalar literal value containing the minimum value of the given
|
||||
// primitive type. For floating-point types, returns -inf.
|
||||
static Literal MinValue(PrimitiveType primitive_type);
|
||||
// Creates a scalar literal value containing the maximum value of the given
|
||||
// primitive type. For floating-point types, returns inf.
|
||||
static Literal MaxValue(PrimitiveType primitive_type);
|
||||
// Creates a literal of the given shape where each element is `value`.
|
||||
template <typename NativeT>
|
||||
static Literal CreateFullWithDescendingLayout(
|
||||
absl::Span<const int64> dimensions, NativeT value);
|
||||
|
||||
// Creates a new literal from an Array type. The variants not ending with
|
||||
// WithLayout use the default XLA layout for the literal's linear
|
||||
// representation in memory.
|
||||
template <typename NativeT>
|
||||
static Literal CreateFromArray(const Array<NativeT>& values);
|
||||
template <typename NativeT>
|
||||
static Literal CreateFromArrayWithLayout(const Array<NativeT>& values,
|
||||
const Layout& layout);
|
||||
template <typename NativeT>
|
||||
static Literal CreateR2FromArray2D(const Array2D<NativeT>& values);
|
||||
template <typename NativeT>
|
||||
static Literal CreateR2FromArray2DWithLayout(const Array2D<NativeT>& values,
|
||||
const Layout& layout);
|
||||
template <typename NativeT>
|
||||
static Literal CreateR3FromArray3D(const Array3D<NativeT>& values);
|
||||
template <typename NativeT>
|
||||
static Literal CreateR3FromArray3DWithLayout(const Array3D<NativeT>& values,
|
||||
const Layout& layout);
|
||||
template <typename NativeT>
|
||||
static Literal CreateR4FromArray4D(const Array4D<NativeT>& values);
|
||||
template <typename NativeT>
|
||||
static Literal CreateR4FromArray4DWithLayout(const Array4D<NativeT>& values,
|
||||
const Layout& layout);
|
||||
|
||||
// Creates a new vector of U8s literal value from a string.
|
||||
static Literal CreateR1U8(absl::string_view value);
|
||||
|
||||
// Creates a linspace-populated literal with the given number of rows and
|
||||
// columns.
|
||||
static Literal CreateR2F32Linspace(float from, float to, int64 rows,
|
||||
int64 cols);
|
||||
|
||||
// Creates a literal that projects the (x, y) dimensions given in values into
|
||||
// the z dimension given by "projection".
|
||||
template <typename NativeT>
|
||||
static Literal CreateR3Projected(
|
||||
std::initializer_list<std::initializer_list<NativeT>> values,
|
||||
int64 projection);
|
||||
|
||||
// Creates a literal that projects the (x, y) dimensions given in values into
|
||||
// the z and p dimensions given.
|
||||
template <typename NativeT>
|
||||
static Literal CreateR4Projected(
|
||||
std::initializer_list<std::initializer_list<NativeT>> values,
|
||||
int64 projection_p, int64 projection_z);
|
||||
|
||||
// Returns an identity matrix (rank 2) with the given row and column count.
|
||||
template <typename NativeT>
|
||||
static Literal MakeIdentityR2(int64 size);
|
||||
|
||||
// Returns a tuple literal composed of given literals. Data is copied from the
|
||||
// given elements into the returned literal.
|
||||
static Literal MakeTuple(absl::Span<const Literal* const> elements);
|
||||
|
||||
static Literal MakeTupleFromSlices(absl::Span<const LiteralSlice> elements);
|
||||
|
||||
// As above, but intended to be invoked with move semantics; i.e.
|
||||
//
|
||||
// std::vector<Literal> elements = ...;
|
||||
// auto result = LiteralUtil::MakeTupleOwned(std::move(elements));
|
||||
//
|
||||
// This would have been declared as an overload, but there is ambiguity
|
||||
// in invocation between the above signature and this one.
|
||||
static Literal MakeTupleOwned(std::vector<Literal> elements);
|
||||
|
||||
// This overload lets you pass a braced list of Literals to
|
||||
// MakeTupleOwned:
|
||||
//
|
||||
// LiteralUtil::MakeTupleOwned(LiteralUtil::CreateR1(...), ...).
|
||||
//
|
||||
// Simply relying on the MakeTupleOwned(std::vector<Literal>)
|
||||
// overload doesn't work because std::initializer_list's elements are always
|
||||
// const.
|
||||
//
|
||||
// The arguments to this function must all be Literal.
|
||||
template <typename... Ts>
|
||||
static Literal MakeTupleOwned(Ts... elements) {
|
||||
std::array<Literal, sizeof...(Ts)> arr{std::move(elements)...};
|
||||
std::vector<Literal> v;
|
||||
v.insert(v.begin(), std::make_move_iterator(arr.begin()),
|
||||
std::make_move_iterator(arr.end()));
|
||||
return MakeTupleOwned(std::move(v));
|
||||
}
|
||||
|
||||
// Create a constant token literal. Token types have no value.
|
||||
static Literal CreateToken();
|
||||
|
||||
// Creates a new Literal object with its values havings the primitive_type
|
||||
// type, and with dimensions defined by the dimensions parameter.
|
||||
// The content of the literal values is the default value of the primitive
|
||||
// type of literal itself (0 for numeric types, and false for predicates).
|
||||
static Literal CreateFromDimensions(PrimitiveType primitive_type,
|
||||
absl::Span<const int64> dimensions);
|
||||
|
||||
// If the given literal's data type is bfloat16, converts it to a float
|
||||
// literal; otherwise, returns a copy of it. If the literal is a tuple,
|
||||
// recursively converts its elements.
|
||||
static Literal ConvertBF16ToF32(const LiteralSlice& bf16_literal);
|
||||
|
||||
// If the given literal's data type is float, converts it to a bfloat16
|
||||
// literal; otherwise, returns a copy of it. If the literal is a tuple,
|
||||
// recursively converts its elements.
|
||||
static Literal ConvertF32ToBF16(const LiteralSlice& f32_literal);
|
||||
|
||||
// Creates a literal with a new shape with the given new dimensions using the
|
||||
// data in the given input literal. For reshaping purposes the (flat) data
|
||||
// buffer of the input literal is assumed to have the given minor_to_major
|
||||
// layout order.
|
||||
static Literal ReshapeSlice(absl::Span<const int64> new_dimensions,
|
||||
absl::Span<const int64> minor_to_major,
|
||||
const LiteralSlice& literal);
|
||||
|
||||
// Creates a literal with the supplied shape, and uses the provided value
|
||||
// generator to populate the literal's values.
|
||||
// Returns the new literal object, or an error Status if failed.
|
||||
template <
|
||||
PrimitiveType type,
|
||||
typename T = typename primitive_util::PrimitiveTypeToNative<type>::type>
|
||||
static StatusOr<Literal> CreateRandomLiteral(
|
||||
const Shape& shape,
|
||||
const std::function<T(absl::Span<const int64>)>& generator);
|
||||
|
||||
// Creates a literal with the supplied shape, and initializes the literal
|
||||
// values using a normal distribution with given mean and stddev standard
|
||||
// deviation, and using the engine as entropy generator.
|
||||
// Returns the new literal object, or an error Status if failed.
|
||||
template <
|
||||
PrimitiveType type, typename E,
|
||||
typename T = typename primitive_util::PrimitiveTypeToNative<type>::type>
|
||||
static StatusOr<Literal> CreateRandomLiteral(const Shape& shape, E* engine,
|
||||
T mean, T stddev);
|
||||
|
||||
// Creates a literal with the supplied shape, and initializes the literal
|
||||
// values using a normal distribution with given mean and stddev standard
|
||||
// deviation.
|
||||
// Returns the new literal object, or an error Status if failed.
|
||||
template <
|
||||
PrimitiveType type,
|
||||
typename T = typename primitive_util::PrimitiveTypeToNative<type>::type>
|
||||
static StatusOr<Literal> CreateRandomLiteral(const Shape& shape, T mean,
|
||||
T stddev);
|
||||
|
||||
//
|
||||
// End of factory methods.
|
||||
|
||||
// Returns a multi-dimensional index as a string. For example: '{7, 8}' will
|
||||
// be returned for a 2-dimensional index with dimension 0 index equal to 7,
|
||||
// dimension 1 equal to 8.
|
||||
static string MultiIndexAsString(absl::Span<const int64> multi_index);
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const Literal& literal);
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR0(NativeT value) {
|
||||
Literal literal(ShapeUtil::MakeShape(
|
||||
primitive_util::NativeToPrimitiveType<NativeT>(), {}));
|
||||
literal.Set({}, value);
|
||||
return literal;
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR1(absl::Span<const NativeT> values) {
|
||||
Literal literal(
|
||||
ShapeUtil::MakeShape(primitive_util::NativeToPrimitiveType<NativeT>(),
|
||||
{static_cast<int64>(values.size())}));
|
||||
literal.PopulateR1(values);
|
||||
return literal;
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR2WithLayout(
|
||||
std::initializer_list<std::initializer_list<NativeT>> values,
|
||||
const Layout& layout) {
|
||||
Literal literal(ShapeUtil::MakeShapeWithLayout(
|
||||
primitive_util::NativeToPrimitiveType<NativeT>(),
|
||||
{static_cast<int64>(values.size()),
|
||||
static_cast<int64>(values.begin()->size())},
|
||||
AsInt64Slice(layout.minor_to_major())));
|
||||
literal.PopulateR2(values);
|
||||
return literal;
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR2(
|
||||
std::initializer_list<std::initializer_list<NativeT>> values) {
|
||||
return CreateR2WithLayout(values, LayoutUtil::GetDefaultLayoutForR2());
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR3WithLayout(
|
||||
std::initializer_list<std::initializer_list<std::initializer_list<NativeT>>>
|
||||
values,
|
||||
const Layout& layout) {
|
||||
const int64 d0 = values.size();
|
||||
const int64 d1 = values.begin()->size();
|
||||
const int64 d2 = values.begin()->begin()->size();
|
||||
Array3D<NativeT> tmp(d0, d1, d2);
|
||||
int64 i0 = 0;
|
||||
for (auto d1_values : values) {
|
||||
int64 i1 = 0;
|
||||
for (auto d2_values : d1_values) {
|
||||
int64 i2 = 0;
|
||||
for (auto value : d2_values) {
|
||||
tmp(i0, i1, i2) = value;
|
||||
++i2;
|
||||
}
|
||||
++i1;
|
||||
}
|
||||
++i0;
|
||||
}
|
||||
return CreateR3FromArray3DWithLayout(tmp, layout);
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR3(
|
||||
std::initializer_list<std::initializer_list<std::initializer_list<NativeT>>>
|
||||
values) {
|
||||
return CreateR3WithLayout(values, LayoutUtil::GetDefaultLayoutForR3());
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR4WithLayout(
|
||||
std::initializer_list<std::initializer_list<
|
||||
std::initializer_list<std::initializer_list<NativeT>>>>
|
||||
values,
|
||||
const Layout& layout) {
|
||||
const int64 d0 = values.size();
|
||||
const int64 d1 = values.begin()->size();
|
||||
const int64 d2 = values.begin()->begin()->size();
|
||||
const int64 d3 = values.begin()->begin()->begin()->size();
|
||||
Array4D<NativeT> tmp(d0, d1, d2, d3);
|
||||
int64 i0 = 0;
|
||||
for (auto d1_values : values) {
|
||||
int64 i1 = 0;
|
||||
for (auto d2_values : d1_values) {
|
||||
int64 i2 = 0;
|
||||
for (auto d3_values : d2_values) {
|
||||
int64 i3 = 0;
|
||||
for (auto value : d3_values) {
|
||||
tmp(i0, i1, i2, i3) = value;
|
||||
++i3;
|
||||
}
|
||||
++i2;
|
||||
}
|
||||
++i1;
|
||||
}
|
||||
++i0;
|
||||
}
|
||||
return CreateR4FromArray4DWithLayout(tmp, layout);
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateSparse(
|
||||
absl::Span<const int64> dimensions, SparseIndexArray indices,
|
||||
absl::Span<const NativeT> values, bool sort) {
|
||||
int64 num_elements = values.size();
|
||||
int64 rank = dimensions.size();
|
||||
CHECK_EQ(num_elements, indices.index_count());
|
||||
CHECK_EQ(rank, indices.rank());
|
||||
Literal literal(ShapeUtil::MakeShapeWithSparseLayout(
|
||||
primitive_util::NativeToPrimitiveType<NativeT>(), dimensions,
|
||||
indices.max_indices()));
|
||||
literal.PopulateSparse(indices, values, sort);
|
||||
return literal;
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR4(
|
||||
std::initializer_list<std::initializer_list<
|
||||
std::initializer_list<std::initializer_list<NativeT>>>>
|
||||
values) {
|
||||
return CreateR4WithLayout(values, LayoutUtil::GetDefaultLayoutForR4());
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateFromArrayWithLayout(
|
||||
const Array<NativeT>& values, const Layout& layout) {
|
||||
Literal literal(ShapeUtil::MakeShapeWithLayout(
|
||||
primitive_util::NativeToPrimitiveType<NativeT>(), values.dimensions(),
|
||||
AsInt64Slice(layout.minor_to_major())));
|
||||
literal.PopulateFromArray(values);
|
||||
return literal;
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateFromArray(
|
||||
const Array<NativeT>& values) {
|
||||
return CreateFromArrayWithLayout(
|
||||
values, LayoutUtil::GetDefaultLayoutForRank(values.num_dimensions()));
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR2FromArray2DWithLayout(
|
||||
const Array2D<NativeT>& values, const Layout& layout) {
|
||||
return CreateFromArrayWithLayout(values, layout);
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR2FromArray2D(
|
||||
const Array2D<NativeT>& values) {
|
||||
return CreateFromArray(values);
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR3FromArray3DWithLayout(
|
||||
const Array3D<NativeT>& values, const Layout& layout) {
|
||||
return CreateFromArrayWithLayout(values, layout);
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR3FromArray3D(
|
||||
const Array3D<NativeT>& values) {
|
||||
return CreateFromArray(values);
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR3Projected(
|
||||
std::initializer_list<std::initializer_list<NativeT>> values,
|
||||
int64 projection) {
|
||||
int64 dim0_size = projection;
|
||||
int64 dim1_size = values.size();
|
||||
int64 dim2_size = values.begin()->size();
|
||||
|
||||
Array3D<NativeT> array(dim0_size, dim1_size, dim2_size);
|
||||
for (int64 dim0 = 0; dim0 < dim0_size; ++dim0) {
|
||||
int64 dim1 = 0;
|
||||
for (auto inner_list : values) {
|
||||
int64 dim2 = 0;
|
||||
for (auto value : inner_list) {
|
||||
array(dim0, dim1, dim2) = value;
|
||||
++dim2;
|
||||
}
|
||||
CHECK_EQ(dim2_size, dim2);
|
||||
++dim1;
|
||||
}
|
||||
CHECK_EQ(dim1_size, dim1);
|
||||
}
|
||||
return CreateR3FromArray3D(array);
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR4Projected(
|
||||
std::initializer_list<std::initializer_list<NativeT>> values,
|
||||
int64 projection_p, int64 projection_z) {
|
||||
int64 dim0_size = projection_p;
|
||||
int64 dim1_size = projection_z;
|
||||
int64 dim2_size = values.size();
|
||||
int64 dim3_size = values.begin()->size();
|
||||
|
||||
Array4D<NativeT> array(dim0_size, dim1_size, dim2_size, dim3_size);
|
||||
for (int64 dim0 = 0; dim0 < dim0_size; ++dim0) {
|
||||
for (int64 dim1 = 0; dim1 < dim1_size; ++dim1) {
|
||||
int64 dim2 = 0;
|
||||
for (auto inner_list : values) {
|
||||
int64 dim3 = 0;
|
||||
for (auto value : inner_list) {
|
||||
array(dim0, dim1, dim2, dim3) = value;
|
||||
++dim3;
|
||||
}
|
||||
CHECK_EQ(dim3_size, dim3);
|
||||
++dim2;
|
||||
}
|
||||
CHECK_EQ(dim2_size, dim2);
|
||||
}
|
||||
}
|
||||
return CreateR4FromArray4D(array);
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR4FromArray4D(
|
||||
const Array4D<NativeT>& values) {
|
||||
return CreateFromArray(values);
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateR4FromArray4DWithLayout(
|
||||
const Array4D<NativeT>& values, const Layout& layout) {
|
||||
return CreateFromArrayWithLayout(values, layout);
|
||||
}
|
||||
|
||||
// Returns an identity matrix (rank 2) with the given row and column count.
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::MakeIdentityR2(int64 size) {
|
||||
Array2D<NativeT> array(size, size, 0);
|
||||
for (int64 i = 0; i < size; ++i) {
|
||||
array(i, i) = 1;
|
||||
}
|
||||
return CreateR2FromArray2D(array);
|
||||
}
|
||||
|
||||
template <typename NativeT>
|
||||
/* static */ Literal LiteralUtil::CreateFullWithDescendingLayout(
|
||||
absl::Span<const int64> dimensions, NativeT value) {
|
||||
Literal literal(ShapeUtil::MakeShapeWithDescendingLayout(
|
||||
primitive_util::NativeToPrimitiveType<NativeT>(), dimensions));
|
||||
literal.PopulateWithValue(value);
|
||||
return literal;
|
||||
}
|
||||
|
||||
template <PrimitiveType type, typename T>
|
||||
/* static */ StatusOr<Literal> LiteralUtil::CreateRandomLiteral(
|
||||
const Shape& shape,
|
||||
const std::function<T(absl::Span<const int64>)>& generator) {
|
||||
using NativeT = typename primitive_util::PrimitiveTypeToNative<type>::type;
|
||||
TF_RET_CHECK(shape.element_type() == type);
|
||||
Literal literal(shape);
|
||||
TF_RETURN_IF_ERROR(literal.Populate<NativeT>(
|
||||
[&](absl::Span<const int64> indexes) { return generator(indexes); }));
|
||||
return std::move(literal);
|
||||
}
|
||||
|
||||
template <PrimitiveType type, typename E, typename T>
|
||||
/* static */ StatusOr<Literal> LiteralUtil::CreateRandomLiteral(
|
||||
const Shape& shape, E* engine, T mean, T stddev) {
|
||||
using NativeT = typename primitive_util::PrimitiveTypeToNative<type>::type;
|
||||
std::normal_distribution<NativeT> generator(mean, stddev);
|
||||
return CreateRandomLiteral<type, NativeT>(
|
||||
shape,
|
||||
[&](absl::Span<const int64> /*indexes*/) { return generator(*engine); });
|
||||
}
|
||||
|
||||
template <PrimitiveType type, typename T>
|
||||
/* static */ StatusOr<Literal> LiteralUtil::CreateRandomLiteral(
|
||||
const Shape& shape, T mean, T stddev) {
|
||||
std::minstd_rand0 engine;
|
||||
return CreateRandomLiteral<type>(shape, &engine, mean, stddev);
|
||||
}
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_LITERAL_UTIL_H_
|
||||
@@ -0,0 +1,111 @@
|
||||
/* 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_COMPILER_XLA_MAP_UTIL_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_MAP_UTIL_H_
|
||||
|
||||
#include <functional>
|
||||
#include <sstream>
|
||||
|
||||
#include "tensorflow/compiler/xla/statusor.h"
|
||||
#include "tensorflow/compiler/xla/util.h"
|
||||
#include "tensorflow/core/platform/logging.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// FindOrDie returns a const reference to the value associated with
|
||||
// the given key if it exists. Crashes otherwise.
|
||||
//
|
||||
// This is intended as a replacement for operator[] as an rvalue (for reading)
|
||||
// when the key is guaranteed to exist.
|
||||
template <class Collection>
|
||||
const typename Collection::value_type::second_type& FindOrDie(
|
||||
const Collection& collection,
|
||||
const typename Collection::value_type::first_type& key) {
|
||||
typename Collection::const_iterator it = collection.find(key);
|
||||
CHECK(it != collection.end()) << "Map key not found: " << key;
|
||||
return it->second;
|
||||
}
|
||||
|
||||
// Same as above, but returns a non-const reference.
|
||||
template <class Collection>
|
||||
typename Collection::value_type::second_type& FindOrDie(
|
||||
Collection& collection, // NOLINT
|
||||
const typename Collection::value_type::first_type& key) {
|
||||
typename Collection::iterator it = collection.find(key);
|
||||
CHECK(it != collection.end()) << "Map key not found: " << key;
|
||||
return it->second;
|
||||
}
|
||||
|
||||
// Like FindOrDie but returns an error instead of dying if `key` is not in
|
||||
// `container`.
|
||||
template <class Collection>
|
||||
StatusOr<
|
||||
std::reference_wrapper<const typename Collection::value_type::second_type>>
|
||||
MaybeFind(const Collection& collection,
|
||||
const typename Collection::value_type::first_type& key) {
|
||||
typename Collection::const_iterator it = collection.find(key);
|
||||
if (it == collection.end()) {
|
||||
std::ostringstream os;
|
||||
os << key;
|
||||
return NotFound("key not found: %s", os.str());
|
||||
}
|
||||
return {it->second};
|
||||
}
|
||||
|
||||
// Returns a const reference to the value associated with the given key if it
|
||||
// exists, otherwise returns a const reference to the provided default value.
|
||||
//
|
||||
// WARNING: If a temporary object is passed as the default "value,"
|
||||
// this function will return a reference to that temporary object,
|
||||
// which will be destroyed at the end of the statement. A common
|
||||
// example: if you have a map with string values, and you pass a char*
|
||||
// as the default "value," either use the returned value immediately
|
||||
// or store it in a string (not string&).
|
||||
template <class Collection>
|
||||
const typename Collection::value_type::second_type& FindOrDefault(
|
||||
const Collection& collection,
|
||||
const typename Collection::value_type::first_type& key,
|
||||
const typename Collection::value_type::second_type& value) {
|
||||
auto it = collection.find(key);
|
||||
if (it != collection.end()) return it->second;
|
||||
return value;
|
||||
}
|
||||
|
||||
// Inserts the key-value pair into the collection. Dies if key was already
|
||||
// present.
|
||||
template <class Collection, class Key, class Value>
|
||||
void InsertOrDie(Collection* const collection, Key&& key, Value&& value) {
|
||||
auto p = collection->insert(
|
||||
std::make_pair(std::forward<Key>(key), std::forward<Value>(value)));
|
||||
CHECK(p.second) << "duplicate key: " << key;
|
||||
}
|
||||
|
||||
// Returns true if and only if the given collection contains the given key.
|
||||
template <class Collection, class Key>
|
||||
bool ContainsKey(const Collection& collection, const Key& key) {
|
||||
return collection.find(key) != collection.end();
|
||||
}
|
||||
|
||||
// Inserts `value` into `set`. Dies if it was already present.
|
||||
template <class Set, class Value>
|
||||
void InsertOrDie(Set* const set, Value&& value) {
|
||||
CHECK(set->insert(std::forward<Value>(value)).second)
|
||||
<< "duplicate value: " << value;
|
||||
}
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_MAP_UTIL_H_
|
||||
@@ -0,0 +1,174 @@
|
||||
/* 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_COMPILER_XLA_METRIC_TABLE_REPORT_H_
|
||||
#define TENSORFLOW_COMPILER_XLA_METRIC_TABLE_REPORT_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "tensorflow/compiler/xla/util.h"
|
||||
|
||||
namespace xla {
|
||||
|
||||
// Class for creating a text format table showing entries with a metric
|
||||
// (e.g. cycles) and a text (e.g. name of function taking that many
|
||||
// cycles). Entries are grouped by a category and sorted in decreasing order of
|
||||
// the metric.
|
||||
//
|
||||
// Example of a categories table generated using this class:
|
||||
//
|
||||
// ********** microseconds report **********
|
||||
// There are 3,912,517 microseconds in total.
|
||||
// There are 123 microseconds ( 0.00%) not accounted for by the data.
|
||||
// There are 3002 ops.
|
||||
//
|
||||
// ********** categories table **********
|
||||
// The left hand side numbers are microseconds.
|
||||
// 1,749,414 (44.71% Σ44.72%) convolution (206 ops)
|
||||
// * 10.51% %convolution.202
|
||||
// * 10.51% %convolution.204
|
||||
// * 10.51% %convolution.203
|
||||
// * ... (203 more ops)
|
||||
// 884,939 (22.62% Σ67.33%) convolution window-dilated (7 ops)
|
||||
// * 7.50% %convolution-window-dilated.7
|
||||
// [...]
|
||||
//
|
||||
// The entry table is similar, it just has the entries directly as the entries
|
||||
// instead of grouping by categories first.
|
||||
class MetricTableReport {
|
||||
public:
|
||||
// Represents an entry in the table.
|
||||
struct Entry {
|
||||
// Text to show in the entry table for this entry.
|
||||
string text;
|
||||
|
||||
// Text to show in the category table for this entry.
|
||||
string short_text;
|
||||
|
||||
// Text that represents the category of this entry - entries with the same
|
||||
// category are grouped together in the category table.
|
||||
string category_text;
|
||||
|
||||
// The value of the metric for this entry.
|
||||
double metric = 0.0;
|
||||
};
|
||||
|
||||
void AddEntry(Entry entry);
|
||||
|
||||
// The default name for the metric is "units", this function allows setting a
|
||||
// more meaningful name.
|
||||
void SetMetricName(string metric_name);
|
||||
|
||||
// The default name for referring to entries is "entries", this functions
|
||||
// allows setting a more meaningful name.
|
||||
void SetEntryName(string entry_name);
|
||||
|
||||
// By default the size of the table is limited. Calling this function forces
|
||||
// all entries to be shown no matter how many there are.
|
||||
void SetShowAllEntries();
|
||||
|
||||
// Set option to show a table with data on the categories of entries.
|
||||
void SetShowCategoryTable();
|
||||
|
||||
// Set option to show a table with data on the entries.
|
||||
void SetShowEntryTable();
|
||||
|
||||
// Returns the report as a string. expected_metric_sum is the expected sum of
|
||||
// the metric across the entries. It is not an error for the actual sum to be
|
||||
// different from the expectation - the report will include the
|
||||
// discrepancy. All metric percentages are for the ratio with respect to the
|
||||
// expected sum, not the actual sum.
|
||||
string MakeReport(double expected_metric_sum);
|
||||
|
||||
// As MakeReport(), but writes the report to the INFO log in a way that avoids
|
||||
// cutting the report off if it is longer than the maximum line length for a
|
||||
// logged line. Individual lines in the report may still be cut off, but they
|
||||
// would have to be very long for that to happen.
|
||||
void WriteReportToInfoLog(double expected_metric_sum);
|
||||
|
||||
private:
|
||||
static constexpr double kDefaultMaxMetricProportionToShow = 0.99;
|
||||
static constexpr int64 kDefaultMaxEntriesToShow = 100;
|
||||
static constexpr int64 kDefaultMaxEntriesPerCategoryToShow = 5;
|
||||
|
||||
// Append all parameters to the report.
|
||||
template <typename... Args>
|
||||
void AppendLine(Args... args) {
|
||||
absl::StrAppend(&report_, std::forward<Args>(args)..., "\n");
|
||||
}
|
||||
|
||||
// Represents a set of entries with the same category_text.
|
||||
struct Category {
|
||||
string category_text;
|
||||
double metric_sum = 0.0; // Sum of metric across entries.
|
||||
std::vector<const Entry*> entries;
|
||||
};
|
||||
|
||||
// Returns a vector of categories of entries with the same category_text. The
|
||||
// vector is sorted in order of decreasing metric sum.
|
||||
//
|
||||
// The returned categories contain pointers into the entries parameter. The
|
||||
// style guide requires parameters to which references/pointers are retained
|
||||
// to be taken by pointer, even for const parameters, so that is why entries
|
||||
// is taken by pointer.
|
||||
static std::vector<Category> MakeCategories(
|
||||
const std::vector<Entry>* entries);
|
||||
|
||||
// Append a header to the report.
|
||||
void AppendHeader();
|
||||
|
||||
// Append a table of categories to the report.
|
||||
void AppendCategoryTable();
|
||||
|
||||
// Append a table of entries to the report.
|
||||
void AppendEntryTable();
|
||||
|
||||
// Appends a row of a table to the report.
|
||||
void AppendTableRow(const string& text, const double metric,
|
||||
const double running_metric_sum);
|
||||
|
||||
// Returns the discrepancy between the expected sum of the metric of the
|
||||
// entries and the actual sum.
|
||||
double UnaccountedMetric();
|
||||
|
||||
// Formats the metric value as a string.
|
||||
string MetricString(double metric);
|
||||
|
||||
// Returns a string representing the metric value as a proportion of the
|
||||
// expected metric sum.
|
||||
string MetricPercent(double metric);
|
||||
|
||||
// The entries to make a report about.
|
||||
std::vector<Entry> entries_;
|
||||
|
||||
double expected_metric_sum_ = 0.0;
|
||||
string metric_name_ = "units";
|
||||
string entry_name_ = "entries";
|
||||
bool show_category_table_ = false;
|
||||
bool show_entry_table_ = false;
|
||||
|
||||
// These members control how many categories and entries to show in tables.
|
||||
int64 max_entries_to_show_ = kDefaultMaxEntriesToShow;
|
||||
int64 max_entries_per_category_to_show_ = kDefaultMaxEntriesPerCategoryToShow;
|
||||
double max_metric_proportion_to_show_ = kDefaultMaxMetricProportionToShow;
|
||||
|
||||
// The report that is being created.
|
||||
string report_;
|
||||
};
|
||||
|
||||
} // namespace xla
|
||||
|
||||
#endif // TENSORFLOW_COMPILER_XLA_METRIC_TABLE_REPORT_H_
|
||||
Loaded 100 of 1283 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user