xref: /aosp_15_r20/external/tensorflow/tensorflow/core/kernels/data/optional_ops_util.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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_UTIL_H_
16 #define TENSORFLOW_CORE_KERNELS_DATA_OPTIONAL_OPS_UTIL_H_
17 
18 #include <functional>
19 #include <memory>
20 #include <utility>
21 #include <vector>
22 
23 #include "tensorflow/core/framework/tensor.h"
24 #include "tensorflow/core/framework/variant_tensor_data.h"
25 #include "tensorflow/core/util/tensor_ops_util.h"
26 
27 namespace tensorflow {
28 namespace data {
29 
30 const char kOptionalVariantTypeName[] = "tensorflow::data::Optional";
31 
32 // An `OptionalVariant` can represent either an "actual value" (a tuple of
33 // tensors) or "none", and may be stored in a DT_VARIANT tensor.
34 class OptionalVariant {
35  public:
36   // Create an `OptionalVariant` with no actual value.
OptionalVariant()37   OptionalVariant() : values_(nullptr) {}
38 
39   // Create an `OptionalVariant` with the actual value given by the tuple of
40   // tensors in `values`.
OptionalVariant(std::vector<Tensor> values)41   explicit OptionalVariant(std::vector<Tensor> values) {
42     values_ = std::make_shared<std::vector<Tensor>>(std::move(values));
43   }
44 
OptionalVariant(const OptionalVariant & other)45   OptionalVariant(const OptionalVariant& other) : values_(other.values_) {}
46 
47   // Returns true if `this` represents an actual value.
has_value()48   bool has_value() const { return values_ != nullptr; }
49 
50   // REQUIRES: `this->has_value()` must be true.
get_values()51   const std::vector<Tensor>& get_values() const {
52     DCHECK(values_) << "Tried to get values from an empty OptionalVariant";
53     return *values_;
54   }
55 
56   // Implementations of the necessary methods for using `OptionalVariant`
57   // objects in DT_VARIANT tensors.
TypeName()58   string TypeName() const { return kOptionalVariantTypeName; }
Encode(VariantTensorData * data)59   void Encode(VariantTensorData* data) const {
60     data->set_metadata(values_ != nullptr);
61     if (values_ != nullptr) {
62       for (const auto& t : *values_) {
63         *(data->add_tensors()) = t;
64       }
65     }
66   }
67 
Decode(const VariantTensorData & data)68   bool Decode(const VariantTensorData& data) {
69     if (data.type_name() != TypeName()) {
70       return false;
71     }
72     bool has_value = false;
73     if (!data.get_metadata(&has_value)) {
74       return false;
75     }
76     if (has_value) {
77       values_ = std::make_shared<std::vector<Tensor>>(data.tensors());
78     } else {
79       values_.reset();
80     }
81     return true;
82   }
83 
DebugString()84   string DebugString() const {
85     if (values_) {
86       return strings::StrCat("OptionalVariant<", "values: (",
87                              absl::StrJoin(*values_, ", ",
88                                            [](string* s, const Tensor& elem) {
89                                              *s = elem.DebugString();
90                                            }),
91                              ")>");
92     } else {
93       return strings::StrCat("OptionalVariant<None>");
94     }
95   }
96 
97  private:
98   std::shared_ptr<const std::vector<Tensor>> values_;
99 };
100 
101 Status OptionalZerosLike(OpKernelContext* ctx, const OptionalVariant& x,
102                          OptionalVariant* y,
103                          std::function<Status(OpKernelContext* ctx,
104                                               const Tensor& input, Tensor* out)>
105                              zeros_like_func);
106 
107 Status OptionalBinaryAdd(
108     OpKernelContext* ctx, const OptionalVariant& a, const OptionalVariant& b,
109     OptionalVariant* out,
110     std::function<Status(OpKernelContext* ctx, const Tensor& a, const Tensor& b,
111                          Tensor* out)>
112         binary_add_func);
113 
114 }  // namespace data
115 }  // namespace tensorflow
116 
117 #endif  // TENSORFLOW_CORE_KERNELS_DATA_OPTIONAL_OPS_UTIL_H_
118