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 
16 #ifndef TENSORFLOW_CC_EXPERIMENTAL_BASE_PUBLIC_RUNTIME_BUILDER_H_
17 #define TENSORFLOW_CC_EXPERIMENTAL_BASE_PUBLIC_RUNTIME_BUILDER_H_
18 
19 #include <memory>
20 
21 #include "tensorflow/c/eager/c_api.h"
22 #include "tensorflow/c/eager/c_api_experimental.h"
23 #include "tensorflow/cc/experimental/base/public/runtime.h"
24 #include "tensorflow/cc/experimental/base/public/status.h"
25 
26 namespace tensorflow {
27 namespace experimental {
28 namespace cc {
29 
30 // RuntimeBuilder is a builder used to construct a tensorflow::cc::Runtime.
31 // Use this to set configuration options, like threadpool size, etc.
32 class RuntimeBuilder {
33  public:
RuntimeBuilder()34   RuntimeBuilder() : options_(TFE_NewContextOptions()) {}
35 
36   // If `use_tfrt` is true, we will use the new Tensorflow Runtime
37   // (https://blog.tensorflow.org/2020/04/tfrt-new-tensorflow-runtime.html) as
38   // our runtime implementation.
39   RuntimeBuilder& SetUseTFRT(bool use_tfrt);
40 
41   // Build a Tensorflow Runtime.
42   //
43   // Params:
44   //  status - Set to OK on success and an appropriate error on failure.
45   // Returns:
46   //  If status is not OK, returns nullptr. Otherwise, returns a
47   //  unique_ptr<tensorflow::cc::Runtime>.
48   std::unique_ptr<Runtime> Build(Status* status);
49 
50   // RuntimeBuilder is movable, but not copyable.
51   RuntimeBuilder(RuntimeBuilder&&) = default;
52   RuntimeBuilder& operator=(RuntimeBuilder&&) = default;
53 
54  private:
55   // RuntimeBuilder is not copyable
56   RuntimeBuilder(const RuntimeBuilder&) = delete;
57   RuntimeBuilder& operator=(const RuntimeBuilder&) = delete;
58 
59   struct TFEContextOptionsDeleter {
operatorTFEContextOptionsDeleter60     void operator()(TFE_ContextOptions* p) const {
61       TFE_DeleteContextOptions(p);
62     }
63   };
64   std::unique_ptr<TFE_ContextOptions, TFEContextOptionsDeleter> options_;
65 };
66 
SetUseTFRT(bool use_tfrt)67 inline RuntimeBuilder& RuntimeBuilder::SetUseTFRT(bool use_tfrt) {
68   TFE_ContextOptionsSetTfrt(options_.get(), use_tfrt);
69   return *this;
70 }
71 
Build(Status * status)72 inline std::unique_ptr<Runtime> RuntimeBuilder::Build(Status* status) {
73   TFE_Context* result = TFE_NewContext(options_.get(), status->GetTFStatus());
74   if (!status->ok()) {
75     return nullptr;
76   }
77   // We can't use std::make_unique here because of its interaction with a
78   // private constructor: https://abseil.io/tips/134
79   return std::unique_ptr<Runtime>(new Runtime(result));
80 }
81 
82 }  // namespace cc
83 }  // namespace experimental
84 }  // namespace tensorflow
85 
86 #endif  // TENSORFLOW_CC_EXPERIMENTAL_BASE_PUBLIC_RUNTIME_BUILDER_H_
87