1/* Copyright 2020 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#ifndef TENSORFLOW_C_EAGER_IMMEDIATE_EXECUTION_OPERATION_H_
16#define TENSORFLOW_C_EAGER_IMMEDIATE_EXECUTION_OPERATION_H_
17
18#include <memory>
19
20#include "absl/types/optional.h"
21#include "absl/types/span.h"
22#include "tensorflow/c/eager/abstract_operation.h"
23#include "tensorflow/c/eager/immediate_execution_tensor_handle.h"
24#include "tensorflow/c/tensor_interface.h"
25#include "tensorflow/core/framework/cancellation.h"
26#include "tensorflow/core/framework/device_attributes.pb.h"
27#include "tensorflow/core/framework/op_def.pb.h"
28#include "tensorflow/core/framework/types.pb.h"
29#include "tensorflow/core/platform/casts.h"
30#include "tensorflow/core/platform/status.h"
31#include "tensorflow/core/util/managed_stack_trace.h"
32
33struct TFE_Op;
34
35namespace tensorflow {
36
37class ImmediateExecutionContext;
38class AbstractOpAttrs;
39
40// Abstract interface to an operation.
41class ImmediateExecutionOperation : public AbstractOperation {
42 public:
43 virtual void Clear() = 0;
44
45 // Returns the inputs of this op.
46 virtual absl::Span<ImmediateExecutionTensorHandle* const> GetInputs()
47 const = 0;
48 virtual Status SetInput(size_t index,
49 ImmediateExecutionTensorHandle* input) = 0;
50
51 virtual ImmediateExecutionContext* GetContext() const = 0;
52
53 // Following two methods are used to support custom device.
54 // Return true if the inputs contain custom device tensor handle. It means
55 // that the argument need to be handled by a custom device.
56 virtual bool HasCustomDeviceInput() const = 0;
57
58 virtual const tensorflow::OpDef* OpDef() const = 0;
59
60 virtual Status InputLength(const char* input_name, int* length) = 0;
61 virtual Status OutputLength(const char* output_name, int* length) = 0;
62
63 // Set stack trace to be used for potential async error reporting.
64 virtual void SetStackTrace(ManagedStackTrace stack_trace) = 0;
65
66 virtual const tensorflow::AbstractOpAttrs* GetOpAttrs() const = 0;
67 virtual void AddAttrs(const AbstractOpAttrs* op_attrs) = 0;
68
69 virtual void SetCancellationManager(
70 CancellationManager* cancellation_manager) = 0;
71
72 // Returns the stack trace set by `SetStackTrace` if exists.
73 virtual absl::optional<ManagedStackTrace> GetStackTrace() = 0;
74
75 virtual void SetStepId(int64_t step_id) = 0;
76
77 // For LLVM style RTTI.
78 static bool classof(const AbstractOperation* ptr) {
79 return ptr->getKind() == kEager || ptr->getKind() == kTfrt;
80 }
81
82 protected:
83 explicit ImmediateExecutionOperation(AbstractOperationKind kind)
84 : AbstractOperation(kind) {}
85 ~ImmediateExecutionOperation() override {}
86};
87
88namespace internal {
89struct ImmediateExecutionOperationDeleter {
90 void operator()(ImmediateExecutionOperation* p) const {
91 if (p != nullptr) {
92 p->Release();
93 }
94 }
95};
96} // namespace internal
97
98using ImmediateOpPtr =
99 std::unique_ptr<ImmediateExecutionOperation,
100 internal::ImmediateExecutionOperationDeleter>;
101
102} // namespace tensorflow
103
104#endif // TENSORFLOW_C_EAGER_IMMEDIATE_EXECUTION_OPERATION_H_
105