xref: /aosp_15_r20/external/tensorflow/tensorflow/c/experimental/ops/resource_variable_ops.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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 // This file is MACHINE GENERATED! Do not edit.
17 
18 #include "tensorflow/c/experimental/ops/resource_variable_ops.h"
19 
20 #include "tensorflow/c/eager/abstract_context.h"
21 #include "tensorflow/c/eager/abstract_tensor_handle.h"
22 #include "tensorflow/c/eager/tracing_utils.h"
23 #include "tensorflow/core/framework/types.h"
24 #include "tensorflow/core/platform/errors.h"
25 
26 using tensorflow::tracing::MaybeSetOpName;
27 
28 namespace tensorflow {
29 namespace ops {
30 
31 // Op: VarHandleOp()
32 // Summary: Creates a handle to a Variable resource.
33 //
34 // Description:
VarHandleOp(AbstractContext * ctx,AbstractTensorHandle ** resource,DataType dtype,const PartialTensorShape shape,const char * container,const char * shared_name,absl::Span<string const> allowed_devices,const char * name,const char * raw_device_name)35 Status VarHandleOp(AbstractContext* ctx, AbstractTensorHandle** resource,
36                    DataType dtype, const PartialTensorShape shape,
37                    const char* container, const char* shared_name,
38                    absl::Span<string const> allowed_devices, const char* name,
39                    const char* raw_device_name) {
40   AbstractOperationPtr op_ptr(ctx->CreateOperation());
41   TF_RETURN_IF_ERROR(op_ptr->Reset("VarHandleOp", raw_device_name));
42   TF_RETURN_IF_ERROR(MaybeSetOpName(op_ptr.get(), name));
43   TF_RETURN_IF_ERROR(
44       op_ptr->SetAttrString("container", container, strlen(container)));
45   TF_RETURN_IF_ERROR(
46       op_ptr->SetAttrString("shared_name", shared_name, strlen(shared_name)));
47   TF_RETURN_IF_ERROR(op_ptr->SetAttrType("dtype", dtype));
48   TF_RETURN_IF_ERROR(op_ptr->SetAttrShape("shape", shape));
49   TF_RETURN_IF_ERROR(
50       op_ptr->SetAttrStringList("allowed_devices", allowed_devices));
51   int num_retvals = 1;
52   return op_ptr->Execute(absl::MakeSpan(resource, 1), &num_retvals);
53 }
54 
55 // Op: ReadVariableOp()
56 // Summary: Reads the value of a variable.
57 //
58 // Description:
59 //   The tensor returned by this operation is immutable.
60 //
61 //   The value returned by this operation is guaranteed to be influenced by all
62 //   the writes on which this operation depends directly or indirectly, and to
63 //   not be influenced by any of the writes which depend directly or indirectly
64 //   on this operation.
ReadVariableOp(AbstractContext * ctx,AbstractTensorHandle * const resource,AbstractTensorHandle ** value,DataType dtype,const char * name,const char * raw_device_name)65 Status ReadVariableOp(AbstractContext* ctx,
66                       AbstractTensorHandle* const resource,
67                       AbstractTensorHandle** value, DataType dtype,
68                       const char* name, const char* raw_device_name) {
69   AbstractOperationPtr op_ptr(ctx->CreateOperation());
70   TF_RETURN_IF_ERROR(op_ptr->Reset("ReadVariableOp", raw_device_name));
71   TF_RETURN_IF_ERROR(MaybeSetOpName(op_ptr.get(), name));
72   TF_RETURN_IF_ERROR(op_ptr->AddInput(resource));
73   TF_RETURN_IF_ERROR(op_ptr->SetAttrType("dtype", dtype));
74   int num_retvals = 1;
75   return op_ptr->Execute(absl::MakeSpan(value, 1), &num_retvals);
76 }
77 
78 // Op: AssignVariableOp()
79 // Summary: Assigns a new value to a variable.
80 //
81 // Description:
82 //   Any ReadVariableOp with a control dependency on this op is guaranteed to
83 //   return this value or a subsequent newer value of the variable.
AssignVariableOp(AbstractContext * ctx,AbstractTensorHandle * const resource,AbstractTensorHandle * const value,bool validate_shape,const char * name,const char * raw_device_name)84 Status AssignVariableOp(AbstractContext* ctx,
85                         AbstractTensorHandle* const resource,
86                         AbstractTensorHandle* const value, bool validate_shape,
87                         const char* name, const char* raw_device_name) {
88   AbstractOperationPtr op_ptr(ctx->CreateOperation());
89   TF_RETURN_IF_ERROR(op_ptr->Reset("AssignVariableOp", raw_device_name));
90   TF_RETURN_IF_ERROR(MaybeSetOpName(op_ptr.get(), name));
91   TF_RETURN_IF_ERROR(op_ptr->AddInput(resource));
92   TF_RETURN_IF_ERROR(op_ptr->AddInput(value));
93   TF_RETURN_IF_ERROR(op_ptr->SetAttrBool("validate_shape", validate_shape));
94   int num_retvals = 0;
95   std::vector<AbstractTensorHandle*> dummy_outputs;
96   return op_ptr->Execute(absl::MakeSpan(dummy_outputs), &num_retvals);
97 }
98 
99 // Op: DestroyResourceOp()
100 // Summary: Deletes the resource specified by the handle.
101 //
102 // Description:
103 //   All subsequent operations using the resource will result in a NotFound
104 //   error status.
DestroyResourceOp(AbstractContext * ctx,AbstractTensorHandle * const resource,bool ignore_lookup_error,const char * name,const char * raw_device_name)105 Status DestroyResourceOp(AbstractContext* ctx,
106                          AbstractTensorHandle* const resource,
107                          bool ignore_lookup_error, const char* name,
108                          const char* raw_device_name) {
109   AbstractOperationPtr op_ptr(ctx->CreateOperation());
110   TF_RETURN_IF_ERROR(op_ptr->Reset("DestroyResourceOp", raw_device_name));
111   TF_RETURN_IF_ERROR(MaybeSetOpName(op_ptr.get(), name));
112   TF_RETURN_IF_ERROR(op_ptr->AddInput(resource));
113   TF_RETURN_IF_ERROR(
114       op_ptr->SetAttrBool("ignore_lookup_error", ignore_lookup_error));
115   int num_retvals = 0;
116   std::vector<AbstractTensorHandle*> dummy_outputs;
117   return op_ptr->Execute(absl::MakeSpan(dummy_outputs), &num_retvals);
118 }
119 
120 }  // namespace ops
121 }  // namespace tensorflow
122