xref: /aosp_15_r20/external/tensorflow/tensorflow/core/kernels/data/optional_ops.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2018 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 #ifndef TENSORFLOW_CORE_KERNELS_DATA_OPTIONAL_OPS_H_
16 #define TENSORFLOW_CORE_KERNELS_DATA_OPTIONAL_OPS_H_
17 
18 #include <vector>
19 
20 #include "tensorflow/core/framework/tensor.h"
21 #include "tensorflow/core/framework/variant_tensor_data.h"
22 #include "tensorflow/core/kernels/data/optional_ops_util.h"
23 #include "tensorflow/core/util/tensor_ops_util.h"
24 
25 namespace tensorflow {
26 namespace data {
27 
28 // Stores a DT_VARIANT value representing an Optional with the given value
29 // in the `output_index`^th output of the given kernel execution context.
30 Status WriteOptionalWithValueToOutput(OpKernelContext* ctx, int output_index,
31                                       std::vector<Tensor> value);
32 
33 // Stores a DT_VARIANT value representing an Optional with no value
34 // in the `output_index`^th output of the given kernel execution context.
35 Status WriteOptionalNoneToOutput(OpKernelContext* ctx, int output_index);
36 
37 template <typename Device>
OptionalZerosLike(OpKernelContext * ctx,const OptionalVariant & x,OptionalVariant * y)38 Status OptionalZerosLike(OpKernelContext* ctx, const OptionalVariant& x,
39                          OptionalVariant* y) {
40   return OptionalZerosLike(ctx, x, y, ZerosLikeTensor<Device>);
41 }
42 
43 template <typename Device>
OptionalBinaryAdd(OpKernelContext * ctx,const OptionalVariant & a,const OptionalVariant & b,OptionalVariant * out)44 Status OptionalBinaryAdd(OpKernelContext* ctx, const OptionalVariant& a,
45                          const OptionalVariant& b, OptionalVariant* out) {
46   return OptionalBinaryAdd(ctx, a, b, out, BinaryAddTensors<Device>);
47 }
48 
49 class OptionalNoneOp : public OpKernel {
50  public:
OptionalNoneOp(OpKernelConstruction * ctx)51   explicit OptionalNoneOp(OpKernelConstruction* ctx) : OpKernel(ctx) {}
52 
53   void Compute(OpKernelContext* ctx) override;
54 };
55 
56 class OptionalFromValueOp : public OpKernel {
57  public:
OptionalFromValueOp(OpKernelConstruction * ctx)58   explicit OptionalFromValueOp(OpKernelConstruction* ctx) : OpKernel(ctx) {}
59 
60   void Compute(OpKernelContext* ctx) override;
61 };
62 
63 class OptionalHasValueOp : public OpKernel {
64  public:
OptionalHasValueOp(OpKernelConstruction * ctx)65   explicit OptionalHasValueOp(OpKernelConstruction* ctx) : OpKernel(ctx) {}
66 
67   void Compute(OpKernelContext* ctx) override;
68 };
69 
70 class OptionalGetValueOp : public OpKernel {
71  public:
OptionalGetValueOp(OpKernelConstruction * ctx)72   explicit OptionalGetValueOp(OpKernelConstruction* ctx) : OpKernel(ctx) {
73     OP_REQUIRES_OK(ctx, ctx->GetAttr("output_shapes", &output_shapes_));
74     OP_REQUIRES_OK(ctx, ctx->GetAttr("output_types", &output_types_));
75     OP_REQUIRES(
76         ctx, output_shapes_.size() == output_types_.size(),
77         errors::InvalidArgument(
78             "output_types and output_shapes must be same length, got:\n",
79             "output_types: ", output_types_.size(), "\n",
80             "output_shapes: ", output_shapes_.size()));
81   }
82 
83   void Compute(OpKernelContext* ctx) override;
84 
85  private:
86   DataTypeVector output_types_;
87   std::vector<PartialTensorShape> output_shapes_;
88 };
89 
90 }  // namespace data
91 }  // namespace tensorflow
92 
93 #endif  // TENSORFLOW_CORE_KERNELS_DATA_OPTIONAL_OPS_H_
94