1/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#ifndef TENSORFLOW_CORE_KERNELS_PARTITIONED_FUNCTION_OPS_H_
17#define TENSORFLOW_CORE_KERNELS_PARTITIONED_FUNCTION_OPS_H_
18
19#include "tensorflow/core/common_runtime/function.h"
20#include "tensorflow/core/framework/function.h"
21#include "tensorflow/core/framework/op_kernel.h"
22#include "tensorflow/core/framework/tensor.h"
23
24namespace tensorflow {
25
26class NameAttrList;
27class ConfigProto;
28
29// A `PartitionedCallOp` asynchronously executes a function, potentially across
30// multiple devices but within a single process. The kernel places and
31// partitions a given function's underlying graph, and executes each of the
32// partitioned subgraphs as a function.
33//
34// TODO(akshayka): Support distributed execution.
35class PartitionedCallOp : public AsyncOpKernel {
36 public:
37 explicit PartitionedCallOp(OpKernelConstruction* ctx);
38
39 ~PartitionedCallOp() override;
40
41 void ComputeAsync(OpKernelContext* ctx, DoneCallback done) override;
42
43 private:
44 Status FillOutputDevices(const FunctionLibraryRuntime& lib,
45 const Device& cpu_device, AttrSlice attrs,
46 FunctionLibraryRuntime::InstantiateOptions* opts);
47
48 Status Instantiate(FunctionLibraryRuntime* lib, OpKernelContext* ctx,
49 std::vector<Tensor>* inputs,
50 FunctionLibraryRuntime::Handle* handle);
51
52 void RunFunction(FunctionLibraryRuntime::Handle handle,
53 const std::vector<Tensor>& inputs,
54 FunctionLibraryRuntime* lib, OpKernelContext* ctx,
55 DoneCallback done);
56
57 // Using unique pointers to avoid including proto headers in kernel headers
58 std::unique_ptr<NameAttrList> func_;
59 std::unique_ptr<ConfigProto> config_proto_;
60 string executor_type_;
61 bool shared_rendezvous_;
62 mutex mu_;
63 // Cache the handle per FLR because this kernel may be instantiated for
64 // a stateful op, different invocations of it may use different FLRs.
65 // Different device placements of PartitionedCallOp also use
66 // different FLRs.
67 gtl::FlatMap<FunctionLibraryRuntime*, FunctionLibraryRuntime::Handle> handles_
68 TF_GUARDED_BY(mu_);
69};
70
71} // namespace tensorflow
72
73#endif // TENSORFLOW_CORE_KERNELS_PARTITIONED_FUNCTION_OPS_H_
74