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
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
36using tensorflow::AbstractContext;
37using tensorflow::AbstractTensorHandle;
38using tensorflow::dyn_cast;
39using tensorflow::ImmediateExecutionContext;
40using tensorflow::ImmediateExecutionTensorHandle;
41using tensorflow::string;
42using tensorflow::unwrap;
43using tensorflow::wrap;
44using tensorflow::strings::StrCat;
45
46TF_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
55TF_AbstractTensor* TF_CreateAbstractTensorFromEagerTensor(TFE_TensorHandle* t,
56 TF_Status* s) {
57 return wrap(static_cast<AbstractTensorHandle*>(unwrap(t)));
58}
59
60TFE_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
72TFE_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