• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_FRAMEWORK_FUNCTION_H_
17 #define TENSORFLOW_CORE_FRAMEWORK_FUNCTION_H_
18 
19 #include <vector>
20 
21 // clang-format off
22 // Required for IS_MOBILE_PLATFORM
23 #include "tensorflow/core/platform/platform.h"
24 // clang-format on
25 
26 #include "absl/types/optional.h"
27 #include "tensorflow/core/framework/attr_value.pb.h"
28 #include "tensorflow/core/framework/attr_value_util.h"
29 #include "tensorflow/core/framework/function.pb.h"
30 #include "tensorflow/core/framework/node_def_util.h"
31 #include "tensorflow/core/framework/op.h"
32 #include "tensorflow/core/framework/op_kernel.h"
33 #include "tensorflow/core/framework/selective_registration.h"
34 #include "tensorflow/core/framework/types.h"
35 #include "tensorflow/core/lib/gtl/flatmap.h"
36 #include "tensorflow/core/lib/hash/hash.h"
37 #include "tensorflow/core/lib/random/random.h"
38 #include "tensorflow/core/platform/env.h"
39 #include "tensorflow/core/platform/macros.h"
40 #include "tensorflow/core/platform/mutex.h"
41 #include "tensorflow/core/platform/protobuf.h"
42 #include "tensorflow/core/protobuf/config.pb.h"
43 #if !defined(IS_MOBILE_PLATFORM)
44 #include "tensorflow/core/protobuf/remote_tensor_handle.pb.h"
45 #endif  // IS_MOBILE_PLATFORM
46 
47 namespace tensorflow {
48 
49 class CancellationManager;
50 class CollectiveExecutor;
51 class DeviceSet;
52 class Graph;
53 class GraphDef;
54 class OpKernel;
55 class ProcessFunctionLibraryRuntime;
56 class ResourceMgr;
57 class Rendezvous;
58 class ScopedStepContainer;
59 class StepStatsCollectorInterface;
60 class Node;
61 
62 // FunctionDefHelper::Create is a convenient helper to construct a
63 // FunctionDef proto.
64 // E.g.,
65 //   FunctionDef my_func = FunctionDefHelper::Create(
66 //     "my_func_name",
67 //     {"x:T", "y:T" /* one string per argument */},
68 //     {"z:T" /* one string per return value */},
69 //     {"T: {float, double}" /* one string per attribute  */},
70 //     {
71 //        {{"o"}, "Mul", {"x", "y"}, {{"T", "$T"}}}
72 //        /* one entry per function node */
73 //     },
74 //     /* Mapping between function returns and function node outputs. */
75 //     {{"z", "o:z"}});
76 //
77 // For the old Function::Node approach, use FunctionDefHelper::Define()
78 // E.g.,
79 //   FunctionDef my_func = FunctionDefHelper::Define(
80 //     "my_func_name",
81 //     {"x:T", "y:T" /* one string per argument */},
82 //     {"z:T" /* one string per return value */},
83 //     {"T: {float, double}" /* one string per attribute  */},
84 //     {
85 //        {{"z"}, "Mul", {"x", "y"}, {{"T", "$T"}}}
86 //        /* one entry per function node */
87 //     });
88 class FunctionDefHelper {
89  public:
90   // AttrValueWrapper has copy constructors for the type T so that
91   // it's easy to construct a simple AttrValue proto.
92   //
93   // If T is a string type (const char*, string, or StringPiece), and
94   // it starts with "$", we construct a AttrValue of "placeholder".
95   //
96   // E.g.,
97   //   std::<string, AttrValueWrapper> x = {"T", "$T"}
98   // is a named attr value placeholder.
99   struct AttrValueWrapper {
100     AttrValue proto;
101 
AttrValueWrapperAttrValueWrapper102     AttrValueWrapper() {}
103 
104     template <typename T>
AttrValueWrapperAttrValueWrapper105     AttrValueWrapper(T val) {  // NOLINT(runtime/explicit)
106       SetAttrValue(val, &proto);
107     }
108 
109    private:
110     void InitFromString(StringPiece val);
111   };
112 
113   // Constructs an AttrValue.func given the "name" and "attrs".
114   static AttrValueWrapper FunctionRef(
115       const string& name,
116       gtl::ArraySlice<std::pair<string, AttrValueWrapper>> attrs);
FunctionRef(const string & name)117   static AttrValueWrapper FunctionRef(const string& name) {
118     return FunctionRef(name, {});
119   }
120 
121   // Node is used to construct FunctionDef.Node using initialization
122   // lists. E.g.,
123   //  Node n = {{"z"}, "Mul", {"x", "y"}, {{"T", "$T"}}};  // z = x * y
124   struct Node {
125     // When constructing a NodeDef, the first entry in ret is used as
126     // the node name, the remaining values are ignored.
127     std::vector<string> ret;
128     string op;
129     std::vector<string> arg;
130     std::vector<std::pair<string, AttrValueWrapper>> attr;
131     std::vector<string> dep;
132     string device;
133 
134     NodeDef ToNodeDef() const;
135   };
136 
137   // Creates a FunctionDef from the given parameters. Node inputs must use
138   // function encoding (node_name:output_name[:output_index]).
139   // - `ret_def` holds a mapping from the function output names from `out_def`
140   //   to the node outputs from `node_def`.
141   // - `control_ret_def` holds a mapping from the function control
142   //   output names to the nodes from `node_def`.
143   static FunctionDef Create(
144       const string& function_name, gtl::ArraySlice<string> in_def,
145       gtl::ArraySlice<string> out_def, gtl::ArraySlice<string> attr_def,
146       gtl::ArraySlice<Node> node_def,
147       gtl::ArraySlice<std::pair<string, string>> ret_def,
148       gtl::ArraySlice<std::pair<string, string>> control_ret_def);
149 
150   // Creates a FunctionDef from the given parameters. Node inputs must use
151   // function encoding (node_name:output_name[:output_index]).
152   // - `ret_def` holds a mapping from the function output names from `out_def`
153   //   to the node outputs from `node_def`.
154   static FunctionDef Create(const string& function_name,
155                             gtl::ArraySlice<string> in_def,
156                             gtl::ArraySlice<string> out_def,
157                             gtl::ArraySlice<string> attr_def,
158                             gtl::ArraySlice<Node> node_def,
159                             gtl::ArraySlice<std::pair<string, string>> ret_def);
160 
161   // TODO(josh11b): Get rid of these and transition to the one above.
162   static FunctionDef Define(const string& function_name,
163                             gtl::ArraySlice<string> arg_def,
164                             gtl::ArraySlice<string> ret_def,
165                             gtl::ArraySlice<string> attr_def,
166                             gtl::ArraySlice<Node> node_def);
167 
168   // Defines an anonymous function. I.e., its name is not relevant.
169   static FunctionDef Define(gtl::ArraySlice<string> arg_def,
170                             gtl::ArraySlice<string> ret_def,
171                             gtl::ArraySlice<string> attr_def,
172                             gtl::ArraySlice<Node> node_def);
173 
174   // Helpers to construct a constant scalar.
175   template <typename T>
Const(const string & name,const T & val)176   static Node Const(const string& name, const T& val) {
177     Node n = {{name}, "Const"};
178     const DataType dtype = DataTypeToEnum<T>::value;
179     n.attr.push_back({"dtype", dtype});
180     Tensor t(dtype, TensorShape({}));
181     t.scalar<T>()() = val;
182     n.attr.push_back({"value", t});
183     return n;
184   }
185 
186   template <typename T>
Const(const string & name,gtl::ArraySlice<T> vals)187   static Node Const(const string& name, gtl::ArraySlice<T> vals) {
188     Node n = {{name}, "Const"};
189     const DataType dtype = DataTypeToEnum<T>::value;
190     n.attr.push_back({"dtype", dtype});
191     int64 num = vals.size();
192     Tensor t(dtype, TensorShape({num}));
193     for (size_t i = 0; i < vals.size(); ++i) {
194       t.flat<T>()(i) = vals[i];
195     }
196     n.attr.push_back({"value", t});
197     return n;
198   }
199 };
200 
201 template <>
AttrValueWrapper(const char * val)202 inline FunctionDefHelper::AttrValueWrapper::AttrValueWrapper(const char* val) {
203   InitFromString(val);
204 }
205 
206 template <>
AttrValueWrapper(const string & val)207 inline FunctionDefHelper::AttrValueWrapper::AttrValueWrapper(
208     const string& val) {
209   InitFromString(val);
210 }
211 
212 template <>
AttrValueWrapper(StringPiece val)213 inline FunctionDefHelper::AttrValueWrapper::AttrValueWrapper(StringPiece val) {
214   InitFromString(val);
215 }
216 
217 // Instantiate a function.
218 //
219 // "fdef" encodes a TF function with some attrs in fdef.signature.attr
220 // containing placeholders.  InstantiateFunction binds these
221 // placeholders and produces an instantiated function encoded in
222 // "result.gdef". The value to substitute a placeholder is given by
223 // "attr_values", which is a map from a placeholder name to an attr
224 // value.
225 //
226 // InstantiateFunction calls "get_function" to find signatures of other
227 // functions and primitive ops.
228 
229 // GetFunctionSignature(func name, opdef) returns OK if the func name is found
230 // and opdef is filled with a pointer to the corresponding signature
231 // (a OpDef proto). Otherwise, returns an error.
232 typedef std::function<Status(const string&, const OpDef**)>
233     GetFunctionSignature;
234 
235 struct InstantiationResult {
236   DataTypeVector arg_types;
237   DataTypeVector ret_types;
238   std::vector<NodeDef> nodes;
239 };
240 Status InstantiateFunction(const FunctionDef& fdef, AttrSlice attr_values,
241                            GetFunctionSignature get_function,
242                            InstantiationResult* result);
243 
244 // Returns a debug string for a function definition.
245 //
246 // The returned text is multiple-line. It is intended to be
247 // human-readable rather than being friendly to parsers. It is _NOT_
248 // intended to be the canonical string representation of "func_def".
249 // Particularly, it may not include all information presented in
250 // "func_def" (e.g., comments, description of the function arguments,
251 // etc.)
252 string DebugString(const FunctionDef& func_def);
253 string DebugString(const GraphDef& instantiated_func_def);
254 string DebugString(gtl::ArraySlice<NodeDef> instantiated_func_nodes);
255 
256 // Returns a debug string for a top level graph (the main program and
257 // its supporting functions defined in its library).
258 string DebugStringWhole(const GraphDef& gdef);
259 
260 // Returns true if f1 == f2. Compares all fields, including descriptions. Order
261 // of NodeDefs doesn't matter.
262 bool FunctionDefsEqual(const FunctionDef& f1, const FunctionDef& f2);
263 
264 // Return a hash of `fdef` that is consistent with FunctionDefsEqual method.
265 // In other words, if two fdefs compare equal, their hash values will be the
266 // same.
267 uint64 FunctionDefHash(const FunctionDef& fdef);
268 
269 class CallFrameInterface {
270  public:
~CallFrameInterface()271   virtual ~CallFrameInterface() {}
272 
273   virtual size_t num_args() const = 0;
274   virtual size_t num_retvals() const = 0;
275 
276   virtual Status GetArg(int index, Tensor* val) const = 0;
277   virtual Status SetRetval(int index, const Tensor& val) = 0;
278 };
279 
280 // Represents a function call frame. I.e., the data structure used to
281 // pass arguments to a function and retrieve its results.
282 //
283 // Runtime must arrange accesses to one FunctionCallFrame s.t.
284 //   1. SetArgs() happens before any GetArg();
285 //   2. GetRetvals happens after all SetRetval();
286 class FunctionCallFrame : public CallFrameInterface {
287  public:
288   FunctionCallFrame(DataTypeSlice arg_types, DataTypeSlice ret_types);
289   ~FunctionCallFrame() override;
290 
291   // Caller methods.
292   Status SetArgs(gtl::ArraySlice<Tensor> args);
293   Status GetRetvals(std::vector<Tensor>* rets) const;
294 
295   // Moves the return values from the frame to rets. If allow_dead_tensors is
296   // false it will fail if any of the retvals do not have a value.
297   Status ConsumeRetvals(std::vector<Tensor>* rets, bool allow_dead_tensors);
298 
num_args()299   size_t num_args() const override { return arg_types_.size(); }
num_retvals()300   size_t num_retvals() const override { return ret_types_.size(); }
301 
302   // Callee methods.
303   Status GetArg(int index, Tensor* val) const override;
304   Status SetRetval(int index, const Tensor& val) override;
305 
306  private:
307   DataTypeVector arg_types_;
308   DataTypeVector ret_types_;
309   gtl::InlinedVector<Tensor, 4> args_;
310   struct Retval {
311     bool has_val = false;
312     Tensor val;
313   };
314   gtl::InlinedVector<Retval, 4> rets_;
315 
316   TF_DISALLOW_COPY_AND_ASSIGN(FunctionCallFrame);
317 };
318 
319 // Helper to maintain a map between function names in a given
320 // FunctionDefLibrary and function definitions.
321 //
322 // This class is thread-safe.
323 class FunctionLibraryDefinition : public OpRegistryInterface {
324  public:
325   // Ops created for function arguments bear the name given by `kArgOp`; those
326   // created for return values bear the name given by `kRetOp`.
327   static constexpr const char* const kArgOp = "_Arg";
328   static constexpr const char* const kDeviceArgOp = "_DeviceArg";
329   static constexpr const char* const kRetOp = "_Retval";
330   static constexpr const char* const kDeviceRetOp = "_DeviceRetval";
331   static constexpr const char* const kIntsOnDeviceAttr =
332       "experimental_ints_on_device";
333 
334   static constexpr const char* const kGradientOp = "SymbolicGradient";
335   static constexpr const char* const kFuncAttr = "f";
336 
337   // Note: This constructor grabs `lib_def`'s lock in shared mode.
338   FunctionLibraryDefinition(const FunctionLibraryDefinition& lib_def);
339   FunctionLibraryDefinition(const OpRegistryInterface* default_registry,
340                             const FunctionDefLibrary& lib_def);
341   ~FunctionLibraryDefinition() override;
342 
343   FunctionLibraryDefinition& operator=(const FunctionLibraryDefinition&) =
344       delete;
345 
346   // Returns True if the library contains `func`, False otherwise.
347   bool Contains(const string& func) const;
348 
349   // Returns nullptr if "func" is not defined in "lib_def". Otherwise,
350   // returns its definition proto.
351   //
352   // NB: This function returns a borrowed pointer, which can be invalidated by a
353   // subsequent call to `ReplaceFunction()` with the given name.
354   const FunctionDef* Find(const string& func) const LOCKS_EXCLUDED(mu_);
355 
356   // Adds function definition 'fdef' to this function library.
357   // Returns status 'ok' on success, or error otherwise. This is a no-op if
358   // 'fdef' already exists in this function library.
359   // If 'fdef' is successfully added to the library, it will be accessible
360   // from 'LookUp' and included in the proto returned by 'ToProto'.
361   // This operation is atomic.
362   Status AddFunctionDef(const FunctionDef& fdef) LOCKS_EXCLUDED(mu_);
363 
364   // Adds gradient definition 'grad' to this function library.
365   // This is a no-op if 'grad' already exists in this function library.
366   // If 'grad' is successfully added, it will be accessible via 'FindGradient'
367   // and included in the proto returned by 'ToProto'.
368   // This operation is atomic.
369   Status AddGradientDef(const GradientDef& grad) LOCKS_EXCLUDED(mu_);
370 
371   // Replaces the function corresponding to `func` with `fdef`. Returns
372   // a non-OK status if "func" was not found in the library, OK otherwise.
373   // Please be careful when replacing function: make sure all previous pointers
374   // returned by `Find()` are no longer in use.
375   Status ReplaceFunction(const string& func, const FunctionDef& fdef)
376       LOCKS_EXCLUDED(mu_);
377 
378   // Replaces the gradient corresponding to `grad.function_name()`. Returns
379   // a non-OK status if "grad.function_name()" was not found in the library, OK
380   // otherwise.
381   Status ReplaceGradient(const GradientDef& grad) LOCKS_EXCLUDED(mu_);
382 
383   // Removes the function corresponding to 'func'. Returns a non-OK status if
384   // 'func' was not found in the library, OK otherwise.
385   // Please be careful when removing function: make sure there are no other
386   // nodes using the function, and all previous pointers returned by `Find()`
387   // are no longer in use.
388   Status RemoveFunction(const string& func) LOCKS_EXCLUDED(mu_);
389 
390   // Adds the functions and gradients in 'other' to this function library.
391   // Duplicate functions and gradients are ignored.
392   // This operation is atomic.
393   Status AddLibrary(const FunctionLibraryDefinition& other) LOCKS_EXCLUDED(mu_);
394 
395   // Adds the functions and gradients in 'lib_def' to this function library.
396   // Duplicate functions and gradients are ignored.
397   // This operation is atomic.
398   Status AddLibrary(const FunctionDefLibrary& lib_def) LOCKS_EXCLUDED(mu_);
399 
400   // If the gradient function for 'func' is specified explicitly in
401   // the library, returns the gradient function name.  Otherwise,
402   // returns an empty string.
403   string FindGradient(const string& func) const LOCKS_EXCLUDED(mu_);
404 
405   // OpRegistryInterface method. Useful for constructing a Graph.
406   //
407   // If "op" is defined in the library, returns its signature.
408   // Otherwise, assume "op" is a primitive op and returns its op
409   // signature and shape inference function.
410   //
411   // NB: This function outputs a borrowed pointer, which can be invalidated by a
412   // subsequent call to `ReplaceFunction()` with the given name.
413   Status LookUp(const string& op_type_name,
414                 const OpRegistrationData** op_reg_data) const override
415       LOCKS_EXCLUDED(mu_);
416 
417   // Generates new function name with the specified prefix that is unique
418   // across this library.
419   string UniqueFunctionName(StringPiece prefix) const LOCKS_EXCLUDED(mu_);
420 
421   // Given a node def 'ndef', inspects attributes of the callee
422   // function to derive the attribute 'value' for 'attr'. Returns OK
423   // iff the attribute is given by the function's definition.
424   // TODO(irving): Remove; keep only the const Node& version.
425   template <typename T>
426   Status GetAttr(const NodeDef& ndef, const string& attr, T* value) const;
427 
428   // Given a node, inspects attributes of the callee function to derive the
429   // attribute 'value' for 'attr'. Returns OK iff the attribute is given by the
430   // function's definition.
431   template <typename T>
432   Status GetAttr(const Node& node, const string& attr, T* value) const;
433 
434   // Returns a proto representation of the state of this function library.
435   FunctionDefLibrary ToProto() const LOCKS_EXCLUDED(mu_);
436 
num_functions()437   size_t num_functions() const {
438     tf_shared_lock l(mu_);
439     return function_defs_.size();
440   }
441 
442   // Returns all the function names in the FunctionLibraryDefinition.
443   std::vector<string> ListFunctionNames() const LOCKS_EXCLUDED(mu_);
444 
default_registry()445   const OpRegistryInterface* default_registry() const {
446     return default_registry_;
447   }
448 
449   // Returns a copy of `*this` with only the subset of functions that are
450   // reachable from the nodes of `graph` or `func`.
451   FunctionLibraryDefinition ReachableDefinitions(const GraphDef& graph) const;
452   FunctionLibraryDefinition ReachableDefinitions(const FunctionDef& func) const;
453 
454   // Copies the function named `func` from `other` to this
455   // FunctionLibraryDefinition.
456   // REQUIRES: `this->default_registry() == other.default_registry()`.
457   // Returns OK on success, or error otherwise. This is a no-op if a function
458   // name `func` already exists in this function library, and has the same
459   // implementation as in `other`. If the implementations conflict, an invalid
460   // argument error is returned.
461   Status CopyFunctionDefFrom(const string& func,
462                              const FunctionLibraryDefinition& other)
463       LOCKS_EXCLUDED(mu_);
464 
465  private:
466   // Shape inference for functions is handled separately by ShapeRefiner.
467 
468   struct FunctionDefAndOpRegistration {
469     explicit FunctionDefAndOpRegistration(const FunctionDef& fdef_in);
470 
471     const FunctionDef fdef;
472     const OpRegistrationData op_registration_data;
473   };
474 
475   std::shared_ptr<FunctionDefAndOpRegistration> FindHelper(
476       const string& func) const SHARED_LOCKS_REQUIRED(mu_);
477   string FindGradientHelper(const string& func) const
478       SHARED_LOCKS_REQUIRED(mu_);
479 
480   Status AddHelper(std::shared_ptr<FunctionDefAndOpRegistration> registration,
481                    bool* added) EXCLUSIVE_LOCKS_REQUIRED(mu_);
482 
483   // Same as AddFunctionDef/AddGradientDef except these methods set
484   // `added` to true if the `fdef`/`grad` were actually added to this.
485   Status AddFunctionDefHelper(const FunctionDef& fdef, bool* added)
486       EXCLUSIVE_LOCKS_REQUIRED(mu_);
487   Status AddGradientDefHelper(const GradientDef& grad, bool* added)
488       EXCLUSIVE_LOCKS_REQUIRED(mu_);
489 
490   // Helper function for GetAttr. Returns the FunctionDef* to get the
491   // attr from.
492   const FunctionDef* GetAttrImpl(const NodeDef& ndef) const LOCKS_EXCLUDED(mu_);
493 
494   // Remove all functions in `funcs` and all gradients of functions in
495   // `funcs_with_grads` from this library.
496   void Remove(const std::vector<string>& funcs,
497               const std::vector<string>& funcs_with_grads)
498       EXCLUSIVE_LOCKS_REQUIRED(mu_);
499 
500   // Remove `func` from the library. Returns non-OK Status unless `func` is in
501   // the library. This should only be called when there is a guarantee that the
502   // function being removed hasn't been retrieved with `Find`.
503   Status RemoveFunctionHelper(const string& func) EXCLUSIVE_LOCKS_REQUIRED(mu_);
504 
505   // Remove gradient of function `func` from the library. Returns non-OK Status
506   // unless `func` has a gradient.
507   Status RemoveGradient(const string& func) EXCLUSIVE_LOCKS_REQUIRED(mu_);
508 
509   mutable mutex mu_;
510   const OpRegistryInterface* const default_registry_;
511   gtl::FlatMap<string, std::shared_ptr<FunctionDefAndOpRegistration>>
512       function_defs_ GUARDED_BY(mu_);
513   gtl::FlatMap<string, string> func_grad_ GUARDED_BY(mu_);
514 };
515 
516 // Forward declare. Defined in common_runtime/function.h
517 struct FunctionBody;
518 
519 // Forward declare. Defined in common_runtime/device.h
520 class Device;
521 // Forward declare. Defined in common_runtime/device_mgr.h
522 class DeviceMgr;
523 
524 class FunctionLibraryRuntime {
525  public:
~FunctionLibraryRuntime()526   virtual ~FunctionLibraryRuntime() {}
527 
528   // Instantiate a function with the given "attrs".
529   //
530   // Returns OK and fills in "handle" if the instantiation succeeds.
531   // Otherwise returns an error and "handle" is undefined.
532   struct InstantiateOptions {
533     // The canonical device name of the device on which the function
534     // should be instantiated. If empty, the function will be
535     // instantiated on the local device.
536     string target;
537 
538     // Should the function be instantiated as a multi-device function?
539     bool is_multi_device_function = false;
540 
541     // For multi-device functions, a vector of canonical device names for
542     // function's inputs. The device of resource inputs must be the device
543     // backing the resource, not the CPU device backing the resource handle.
544     // Must have the same length as number of inputs to the function.
545     std::vector<string> input_devices;
546 
547     // For multi-device functions, a vector of canonical device names for
548     // function's outputs.
549     //
550     // (a) If specified (must have the same length as number of outputs):
551     //
552     // Specified devices will be assigned to Retval nodes inserted into the
553     // function body graph in place of function outputs. It is allowed to
554     // specify output device as empty string, in this case Retval device
555     // assignment will be inferred later when function graph will be placed
556     // before partitioning (this is required for resource outputs). Placer will
557     // respect colocation constraints.
558     //
559     // (b) If not specified:
560     //
561     // Function runtime will infer Retval device by following input edges, until
562     // it will reach a node with a device specification. This device
563     // specification must identify a unique device, i.e. a general specification
564     // like "job:foo" matching multiple devices will result in an error.
565     //
566     // IMPORTANT: Resource outputs
567     //
568     // Multi device functions might return resources on a devices different from
569     // the function call device. If output device is not specified for the
570     // resource output, and node producing that resource is a function call,
571     // runtime will leave device specification empty and will rely on Placer to
572     // infer correct device.
573     std::vector<string> output_devices;
574 
575     // This interface is EXPERIMENTAL and subject to change.
576     //
577     // For multi-device functions, a mapping from _Arg node index to type and
578     // shape for input resources.
579     // REQUIRES: if input_resource_dtypes_and_shapes.count(i) > 0 then i-th
580     // argument type must be DT_RESOURCE.
581     std::unordered_map<int, DtypeAndPartialTensorShape>
582         input_resource_dtypes_and_shapes;
583 
584     // This interface is EXPERIMENTAL and subject to change.
585     //
586     // If non-null, the runtime will use `lib_def` to resolve function(s) named
587     // in `function_name` and `attrs`. Otherwise, the runtime will use its
588     // internal library.
589     //
590     // NOTE(mrry): If provided, all functions defined in `lib_def` must be
591     // self-contained, and cannot refer to functions defined in other libraries.
592     const FunctionLibraryDefinition* lib_def = nullptr;
593 
594     // This interface is EXPERIMENTAL and subject to change.
595     //
596     // If non-empty, the runtime will use `state_handle` to identify
597     // cached state related the instantiated function. Two functions
598     // of the same name and attrs, instantiated with the same
599     // `state_handle` will have the same handle and share the same
600     // state (in stateful kernels); and two functions with different
601     // values for `state_handle` will have independent state.
602     string state_handle;
603 
604     // This interface is EXPERIMENTAL and subject to change.
605     //
606     // Instantiates the function using an executor of the given type. If empty,
607     // the default TensorFlow executor will be used.
608     string executor_type;
609 
610     // If true, the runtime will attempt to create kernels for the function at
611     // instantiation time, rather than on the first run. This can be used to
612     // surface errors earlier.
613     bool create_kernels_eagerly = false;
614 
615     // This interface is EXPERIMENTAL and subject to change.
616     //
617     // Instantiates the function with the provided config_proto.
618     ConfigProto config_proto;
619 
620     // If provided, this optimization function will be invoked before
621     // the placer for multi-device functions.
622     std::function<Status(std::vector<string> /*ret_node_names*/,
623                          std::vector<string> /*keep_node_names*/,
624                          FunctionLibraryDefinition*, const DeviceSet&,
625                          Device* /*cpu_device*/, std::unique_ptr<Graph>*)>
626         optimize_graph_fn;
627 
628     // If set, partitioned functions will be added to `graph_collector`.
629     // `graph_collector` must be alive during the call to Instantiate.
630     GraphCollector* graph_collector = nullptr;
631 
632     // Indicates whether the multi-device function backend should default the
633     // placement of ops without request device to `target`.
634     bool default_device_to_target = true;
635 
636     // If true, the optimized Graph will be stored so that
637     // `FunctionLibraryRuntime::DebugString(handle)` contains the optimized
638     // Graph. Otherwise, the unoptimized function Graph will be returned.
639     bool include_optimized_graph_in_debug_string = false;
640   };
641   typedef uint64 Handle;
642   virtual Status Instantiate(const string& function_name, AttrSlice attrs,
643                              const InstantiateOptions& options,
644                              Handle* handle) = 0;
Instantiate(const string & function_name,AttrSlice attrs,Handle * handle)645   Status Instantiate(const string& function_name, AttrSlice attrs,
646                      Handle* handle) {
647     auto opts = absl::make_unique<InstantiateOptions>();
648     return Instantiate(function_name, attrs, *opts, handle);
649   }
650 
651   // Releases state associated with the handle.
652   virtual Status ReleaseHandle(Handle handle) = 0;
653 
654   // Returns the function body for the instantiated function given its
655   // handle 'h'. Returns nullptr if "h" is not found.
656   //
657   // *this keeps the ownership of the returned object, which remains alive
658   // as long as *this.
659   virtual const FunctionBody* GetFunctionBody(Handle h) = 0;
660 
661   // Returns the return types for the function identified by handle `h`.
662   virtual Status GetRetTypes(Handle h, DataTypeVector* ret_types) = 0;
663 
664   // Asynchronously invokes the instantiated function identified by
665   // "handle".
666   //
667   // If function execution succeeds, "done" is called with OK and
668   // "*rets" is filled with the function's return values. Otheriwse,
669   // "done" is called with an error status.
670   //
671   // Does not take ownership of "rets".
672   // In the cross-process scenario, runner isn't used for making the Async
673   // RPC calls.
674   struct Options {
OptionsOptions675     Options() {}
OptionsOptions676     explicit Options(const int64 step_id) : step_id(step_id) {}
677     // Choose a step ID that is guaranteed not to clash with any
678     // Session-generated step ID. DirectSession only generates
679     // non-negative step IDs (contiguous, starting from 0), and
680     // MasterSession generates 56-bit random step IDs whose MSB is
681     // always 0, so a negative random step ID should suffice.
682     const int64 step_id = -std::abs(static_cast<int64>(random::New64()));
683 
684     // op_id of the function running in eager mode. Set when we want to copy
685     // remote outputs lazily. All components of a remote multi-device function
686     // should use the same op_id, in order to correctly map remote output
687     // tensors to the remote TensorHandles in the default device.
688     absl::optional<int64> op_id = absl::nullopt;
689 
690     RendezvousInterface* rendezvous = nullptr;
691     CancellationManager* cancellation_manager = nullptr;
692     CollectiveExecutor* collective_executor = nullptr;
693     ScopedStepContainer* step_container = nullptr;
694     StepStatsCollectorInterface* stats_collector = nullptr;
695 
696     std::function<void(std::function<void()>)>* runner = nullptr;
697 
698     // Parameters for remote function execution.
699     bool remote_execution = false;
700     string source_device = "";  // Fully specified device name.
701 
702     // Allocator attributes specifying where the args are / rets should be put.
703     // These should either be {} or match the length of args / retvals. If {},
704     // the default allocator attributes will be assumed for all args / retvals.
705     std::vector<AllocatorAttributes> args_alloc_attrs;
706     std::vector<AllocatorAttributes> rets_alloc_attrs;
707 
708     // If true, we create a new IntraProcessRendezvous, else use the existing
709     // one.
710     bool create_rendezvous = false;
711 
712     // If True, allow returning dead tensors.
713     bool allow_dead_tensors = false;
714 
715     // Returns a human readable representation of this.
716     string DebugString() const;
717   };
718   typedef std::function<void(const Status&)> DoneCallback;
719   virtual void Run(const Options& opts, Handle handle,
720                    gtl::ArraySlice<Tensor> args, std::vector<Tensor>* rets,
721                    DoneCallback done) = 0;
722   virtual void Run(const Options& opts, Handle handle,
723                    CallFrameInterface* call_frame, DoneCallback done) = 0;
724 
725   // Creates a "kernel" for the given node def "ndef".
726   //
727   // If succeeds, returns OK and the caller takes the ownership of the
728   // returned "*kernel". Otherwise, returns an error.
729   virtual Status CreateKernel(const NodeDef& ndef, OpKernel** kernel) = 0;
730 
731   // Returns true iff the function named `function_name` is stateful.
732   //
733   // NOTE(mrry): This method assumes that the runtime is associated with a
734   // default function library, and looks up `function_name` in that library.
735   // It does not support overriding the function library.
736   virtual bool IsStateful(const string& function_name) const = 0;
737 
738   // Returns the device on which the function executes.
739   virtual Device* device() = 0;
740   virtual const Device* device() const = 0;
741 
742   // Returns the default runner in which the ops should be launched. If the
743   // device on which the function executes has a private thread pool, return
744   // runner on the device local thread pool.
745   virtual std::function<void(std::function<void()>)>* runner() = 0;
746 
747   // Get the DeviceMgr from which the device was obtained.
748   virtual const DeviceMgr* device_mgr() const = 0;
749 
750   // Returns the function library definition that backs this runtime.
751   //
752   // NOTE(mrry): The returned library definition is the default function library
753   // for this runtime. The caller may override the function library used by the
754   // runtime to instantiate functions, which will not be reflected in the return
755   // value of this function.
756   virtual const FunctionLibraryDefinition* GetFunctionLibraryDefinition()
757       const = 0;
758 
759   // Returns the environment on which the function executes.
760   virtual Env* env() = 0;
761 
762   // Returns the ConfigProto passed to the session used to create the function.
763   virtual const ConfigProto* const config_proto() = 0;
764 
765   // Returns a debug string showing the definition of the function of
766   // 'handle'.
767   virtual string DebugString(Handle handle) = 0;
768 
769   // Returns the graph version number.
770   virtual int graph_def_version() const = 0;
771 
772   typedef uint64 LocalHandle;
773 
774   // Creates a copy of ProcessFunctionLibraryRuntime (transferring ownership to
775   // the caller), FunctionLibraryRuntime (owned by the returned
776   // ProcessFunctionLibraryRuntime), FunctionLibraryDefinition (transferring
777   // ownership to the caller). Note that both the ProcessFunctionLibraryRuntime
778   // and FunctionLibraryRuntime borrow a pointer to the
779   // FunctionLibraryDefinition and so the FunctionLibraryDefinition should
780   // outlive both.
781   //
782   // The `skip_flib_def` argument controls whether the method should clone the
783   // FunctionLibraryDefinition (default behavior) or return an empty function
784   // library. The latter is used by tf.data, which manages
785   // FunctionLibraryDefinitions for its functions independently (and passes
786   // these into the FunctionLibraryRuntime through an overlay), to avoid linear
787   // runtime w.r.t. to number of functions in the current function library.
788   virtual Status Clone(std::unique_ptr<FunctionLibraryDefinition>* out_lib_def,
789                        std::unique_ptr<ProcessFunctionLibraryRuntime>* out_pflr,
790                        FunctionLibraryRuntime** out_flr,
791                        bool skip_flib_def = false) = 0;
792 
793   // Returns the name of the executor class (in the sense of
794   // `ExecutorFactory::GetFactory()`) that will be used based on the given
795   // dynamic `options` and static `attrs`. If none is specified, this method
796   // will return an empty string, which leaves the decision up to the runtime.
797   static string ExecutorType(const InstantiateOptions& options,
798                              AttrSlice attrs);
799 };
800 
801 // Returns a canonicalized string for the instantiation of the
802 // function of the given "name", attributes "attrs", and "options".
803 //
804 // The returned string is guaranteed to be stable within one address
805 // space. But it may be change as the implementation
806 // evolves. Therefore, it should not be persisted or compared across
807 // address spaces.
808 string Canonicalize(const string& funcname, AttrSlice attrs,
809                     const FunctionLibraryRuntime::InstantiateOptions& options);
810 string Canonicalize(const string& funcname, AttrSlice attrs);
811 
812 const FunctionLibraryRuntime::Handle kInvalidHandle = -1;
813 const FunctionLibraryRuntime::LocalHandle kInvalidLocalHandle = -1;
814 
815 class CustomKernelCreator {
816  public:
~CustomKernelCreator()817   virtual ~CustomKernelCreator() {}
818 
819   // Given a NodeDef 'node_def' and the function library runtime 'flr',
820   // validate if the class supports creating such a kernel.
821   virtual bool CanCreateKernel(const FunctionLibraryRuntime& flr,
822                                const NodeDef& node_def) const = 0;
823 
824   // Given a supported NodeDef, returns a kernel that computes the node.
825   virtual Status CreateKernel(FunctionLibraryRuntime* flr, const NodeDef& ndef,
826                               std::unique_ptr<OpKernel>* kernel) const = 0;
827 };
828 
829 // Used to instantiate and run functions in a distributed system.
830 class DistributedFunctionLibraryRuntime {
831  public:
~DistributedFunctionLibraryRuntime()832   virtual ~DistributedFunctionLibraryRuntime() {}
833 
834   // The _target attr in attrs determines where the function is instantiated.
835   virtual void Instantiate(
836       const string& function_name, const FunctionLibraryDefinition& lib_def,
837       AttrSlice attrs,
838       const FunctionLibraryRuntime::InstantiateOptions& options,
839       FunctionLibraryRuntime::LocalHandle* handle,
840       FunctionLibraryRuntime::DoneCallback done) = 0;
841 
842   // opts.runner isn't used for execution.
843   virtual void Run(const FunctionLibraryRuntime::Options& opts,
844                    FunctionLibraryRuntime::LocalHandle handle,
845                    gtl::ArraySlice<Tensor> args, std::vector<Tensor>* rets,
846                    FunctionLibraryRuntime::DoneCallback done) = 0;
847 
848 #if !defined(IS_MOBILE_PLATFORM)
849   // TODO(yujingzhang): Support outputting tensors on remote devices.
Run(const FunctionLibraryRuntime::Options & opts,FunctionLibraryRuntime::LocalHandle handle,std::vector<eager::RemoteTensorHandle> * args,FunctionLibraryRuntime::DoneCallback done)850   virtual void Run(const FunctionLibraryRuntime::Options& opts,
851                    FunctionLibraryRuntime::LocalHandle handle,
852                    std::vector<eager::RemoteTensorHandle>* args,
853                    FunctionLibraryRuntime::DoneCallback done) {
854     done(errors::Unimplemented("Unimplemented."));
855   }
856 #endif  // IS_MOBILE_PLATFORM
857 
858   virtual void CleanUp(uint64 step_id,
859                        FunctionLibraryRuntime::LocalHandle handle,
860                        FunctionLibraryRuntime::DoneCallback done) = 0;
861 
862   // DeviceMgr with *all* available devices.
863   virtual DeviceMgr* remote_device_mgr() const = 0;
864 };
865 
866 // Extracts the actual type from "attr_values" based on its definition
867 // "arg_def".
868 //
869 // If "arg_def" is a N*T type, *is_type_list is set to false, and
870 // *dtypes is set to be a vector of size N and each element is T.
871 //
872 // If "arg_def" is a list(type), *is_type_list is set to true, and
873 // *dtypes is set to be a vector of types specified in attrs for
874 // arg_def.
875 //
876 // Otherwise (arg_def is a simple type T), *is_type_list is set to
877 // false, and *dtypes is set to a single element vector, whose only
878 // element is T.
879 Status ArgNumType(AttrSlice attrs, const OpDef::ArgDef& arg_def,
880                   bool* is_type_list, DataTypeVector* dtypes);
881 
882 // To register a gradient function for a builtin op, one should use
883 //   REGISTER_OP_GRADIENT(<op_name>, <c++ grad factory>);
884 //
885 // Typically, the c++ grad factory is a plan function that can be
886 // converted into ::tensorflow::gradient::Creator, which is
887 //   std::function<Status(const AttrSlice&, FunctionDef*)>.
888 //
889 // A ::tensorflow::gradient::Creator should populate in FunctionDef* with a
890 // definition of a brain function which compute the gradient for the
891 // <op_name> when the <op_name> is instantiated with the given attrs.
892 //
893 // E.g.,
894 //
895 // Status MatMulGrad(const AttrSlice& attrs, FunctionDef* g) {
896 //   bool transpose_a;
897 //   TF_RETURN_IF_ERROR(attrs.Get("transpose_a", &transpose_a));
898 //   bool transpose_b;
899 //   TF_RETURN_IF_ERROR(attrs.Get("transpose_b", &transpose_b));
900 //   DataType dtype;
901 //   TF_RETURN_IF_ERROR(attrs.Get("dtype", &dtype));
902 //   if (!transpose_a && !transpose_b) {
903 //     *g = FunctionDefHelper::Define(
904 //       "MatMulGrad",
905 //       {"x:T ", "y:T", "dz:T"},    // Inputs to this function
906 //       {"dx:T", "dy:T"},           // Outputs from this function
907 //       {"T: {float, double}"},     // Attributes needed by this function
908 //       {
909 //         {{"x_t"}, "Transpose", {"x"}, {{"T", "$T"}}},
910 //         {{"y_t"}, "Transpose", {"y"}, {{"T", "$T"}}},
911 //         {{"dx"}, "MatMul", {"dz", "y_t"}, {{"T", "$T"}}},
912 //         {{"dy"}, "MatMul", {"x_", "dz"}, {{"T", "$T"}}},
913 //       });
914 //   } else {
915 //     ... ...
916 //   }
917 //   return Status::OK();
918 // }
919 //
920 // NOTE: $T is substituted with the type variable "T" when the
921 // gradient function MatMul is instantiated.
922 //
923 // TODO(zhifengc): Better documentation somewhere.
924 
925 // Macros to define a gradient function factory for a primitive
926 // operation.
927 #define REGISTER_OP_GRADIENT(name, fn) \
928   REGISTER_OP_GRADIENT_UNIQ_HELPER(__COUNTER__, name, fn)
929 
930 #define REGISTER_OP_NO_GRADIENT(name) \
931   REGISTER_OP_GRADIENT_UNIQ_HELPER(__COUNTER__, name, nullptr)
932 
933 #define REGISTER_OP_GRADIENT_UNIQ_HELPER(ctr, name, fn) \
934   REGISTER_OP_GRADIENT_UNIQ(ctr, name, fn)
935 
936 #define REGISTER_OP_GRADIENT_UNIQ(ctr, name, fn)      \
937   static bool unused_grad_##ctr TF_ATTRIBUTE_UNUSED = \
938       SHOULD_REGISTER_OP_GRADIENT &&                  \
939       ::tensorflow::gradient::RegisterOp(name, fn)
940 
941 namespace gradient {
942 // Register a gradient creator for the "op".
943 typedef std::function<Status(const AttrSlice& attrs, FunctionDef*)> Creator;
944 bool RegisterOp(const string& op, Creator func);
945 
946 // Returns OK the gradient creator for the "op" is found (may be
947 // nullptr if REGISTER_OP_NO_GRADIENT is used.
948 Status GetOpGradientCreator(const string& op, Creator* creator);
949 };  // namespace gradient
950 
951 // Declare explicit instantiations of GetAttr
952 #define GET_ATTR(T)                                          \
953   extern template Status FunctionLibraryDefinition::GetAttr( \
954       const Node&, const string&, T*) const;                 \
955   extern template Status FunctionLibraryDefinition::GetAttr( \
956       const NodeDef&, const string&, T*) const;
957 GET_ATTR(string)
958 GET_ATTR(bool)
959 #undef GET_ATTR
960 
961 }  // end namespace tensorflow
962 
963 #endif  // TENSORFLOW_CORE_FRAMEWORK_FUNCTION_H_
964