• 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 
16 #include <vector>
17 
18 #include "tensorflow/c/eager/abstract_context.h"
19 #include "tensorflow/c/eager/abstract_tensor_handle.h"
20 #include "tensorflow/c/eager/c_api.h"
21 #include "tensorflow/c/eager/c_api_unified_experimental.h"
22 #include "tensorflow/c/eager/c_api_unified_experimental_internal.h"
23 #include "tensorflow/c/eager/immediate_execution_context.h"
24 #include "tensorflow/c/eager/immediate_execution_tensor_handle.h"
25 #include "tensorflow/c/eager/tfe_context_internal.h"
26 #include "tensorflow/c/eager/tfe_tensorhandle_internal.h"
27 #include "tensorflow/c/tf_status.h"
28 #include "tensorflow/core/lib/llvm_rtti/llvm_rtti.h"
29 #include "tensorflow/core/platform/strcat.h"
30 
31 // =============================================================================
32 // Public C API entry points
33 // These are only the entry points specific to the Eager API.
34 // =============================================================================
35 
36 using tensorflow::AbstractContext;
37 using tensorflow::AbstractTensorHandle;
38 using tensorflow::dyn_cast;
39 using tensorflow::ImmediateExecutionContext;
40 using tensorflow::ImmediateExecutionTensorHandle;
41 using tensorflow::string;
42 using tensorflow::unwrap;
43 using tensorflow::wrap;
44 using tensorflow::strings::StrCat;
45 
TF_NewEagerExecutionContext(TFE_ContextOptions * options,TF_Status * s)46 TF_ExecutionContext* TF_NewEagerExecutionContext(TFE_ContextOptions* options,
47                                                  TF_Status* s) {
48   TFE_Context* c_ctx = TFE_NewContext(options, s);
49   if (TF_GetCode(s) != TF_OK) {
50     return nullptr;
51   }
52   return wrap(static_cast<AbstractContext*>(unwrap(c_ctx)));
53 }
54 
TF_CreateAbstractTensorFromEagerTensor(TFE_TensorHandle * t,TF_Status * s)55 TF_AbstractTensor* TF_CreateAbstractTensorFromEagerTensor(TFE_TensorHandle* t,
56                                                           TF_Status* s) {
57   return wrap(static_cast<AbstractTensorHandle*>(unwrap(t)));
58 }
59 
TF_AbstractTensorGetEagerTensor(TF_AbstractTensor * at,TF_Status * s)60 TFE_TensorHandle* TF_AbstractTensorGetEagerTensor(TF_AbstractTensor* at,
61                                                   TF_Status* s) {
62   auto handle = dyn_cast<ImmediateExecutionTensorHandle>(unwrap(at));
63   if (!handle) {
64     string msg =
65         StrCat("Not an eager tensor handle.", reinterpret_cast<uintptr_t>(at));
66     TF_SetStatus(s, TF_INVALID_ARGUMENT, msg.c_str());
67     return nullptr;
68   }
69   return wrap(handle);
70 }
71 
TF_ExecutionContextGetTFEContext(TF_ExecutionContext * ctx,TF_Status * s)72 TFE_Context* TF_ExecutionContextGetTFEContext(TF_ExecutionContext* ctx,
73                                               TF_Status* s) {
74   auto imm_ctx = dyn_cast<ImmediateExecutionContext>(unwrap(ctx));
75   if (!imm_ctx) {
76     string msg =
77         StrCat("Not an eager context.", reinterpret_cast<uintptr_t>(ctx));
78     TF_SetStatus(s, TF_INVALID_ARGUMENT, msg.c_str());
79     return nullptr;
80   }
81   return wrap(imm_ctx);
82 }
83