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