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_ABSTRACT_CONTEXT_H_
16#define TENSORFLOW_C_EAGER_ABSTRACT_CONTEXT_H_
17
18#include <memory>
19
20#include "tensorflow/c/eager/abstract_function.h"
21#include "tensorflow/c/eager/abstract_operation.h"
22
23namespace tensorflow {
24
25// Abstract interface to a context.
26//
27// This serves as a factory for creating `AbstractOperation`s and for
28// registering traced functions.
29// Operations creation within a context can only be executed in that context
30// (for now at least).
31// Implementations of the context may contain some state e.g. an execution
32// environment, a traced representation etc.
33class AbstractContext {
34 protected:
35 enum AbstractContextKind { kGraph, kMlir, kEager, kTfrt, kTape, kOpHandler };
36 explicit AbstractContext(AbstractContextKind kind) : kind_(kind) {}
37 virtual ~AbstractContext() {}
38
39 public:
40 AbstractContextKind getKind() const { return kind_; }
41
42 // Release any underlying resources, including the interface object.
43 //
44 // WARNING: The destructor of this class is marked as protected to disallow
45 // clients from directly destroying this object since it may manage its own
46 // lifetime through ref counting. Thus clients MUST call Release() in order to
47 // destroy an instance of this class.
48 virtual void Release() = 0;
49
50 // Creates an operation builder and ties it to this context.
51 // The returned object can be used for setting operation's attributes,
52 // adding inputs and finally executing (immediately or lazily as in tracing)
53 // it in this context.
54 virtual AbstractOperation* CreateOperation() = 0;
55
56 // Registers a function with this context, after this the function is
57 // available to be called/referenced by its name in this context.
58 virtual Status RegisterFunction(AbstractFunction*) = 0;
59 // Remove a function. 'func' argument is the name of a previously added
60 // FunctionDef. The name is in fdef.signature.name.
61 virtual Status RemoveFunction(const string& func) = 0;
62
63 private:
64 const AbstractContextKind kind_;
65};
66
67namespace internal {
68struct AbstractContextDeleter {
69 void operator()(AbstractContext* p) const {
70 if (p != nullptr) {
71 p->Release();
72 }
73 }
74};
75} // namespace internal
76
77using AbstractContextPtr =
78 std::unique_ptr<AbstractContext, internal::AbstractContextDeleter>;
79
80} // namespace tensorflow
81
82#endif // TENSORFLOW_C_EAGER_ABSTRACT_CONTEXT_H_
83