|
| 1 | +/* Copyright 2023 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 | +#ifndef TENSORFLOW_IO_CORE_KERNELS_AVRO_ATDS_DECODER_H_ |
| 17 | +#define TENSORFLOW_IO_CORE_KERNELS_AVRO_ATDS_DECODER_H_ |
| 18 | + |
| 19 | +#include "api/Decoder.hh" |
| 20 | +#include "api/GenericDatum.hh" |
| 21 | +#include "api/ValidSchema.hh" |
| 22 | +#include "tensorflow/core/framework/tensor.h" |
| 23 | +#include "tensorflow/core/framework/tensor_shape.h" |
| 24 | +#include "tensorflow/core/framework/types.pb.h" |
| 25 | +#include "tensorflow/core/platform/status.h" |
| 26 | +#include "tensorflow_io/core/kernels/avro/atds/decoder_base.h" |
| 27 | +#include "tensorflow_io/core/kernels/avro/atds/dense_feature_decoder.h" |
| 28 | +#include "tensorflow_io/core/kernels/avro/atds/errors.h" |
| 29 | +#include "tensorflow_io/core/kernels/avro/atds/sparse_feature_decoder.h" |
| 30 | +#include "tensorflow_io/core/kernels/avro/atds/varlen_feature_decoder.h" |
| 31 | + |
| 32 | +namespace tensorflow { |
| 33 | +namespace atds { |
| 34 | + |
| 35 | +class NullableFeatureDecoder : public DecoderBase { |
| 36 | + public: |
| 37 | + explicit NullableFeatureDecoder(std::unique_ptr<DecoderBase>& decoder, |
| 38 | + size_t non_null_index) |
| 39 | + : decoder_(std::move(decoder)), non_null_index_(non_null_index) {} |
| 40 | + |
| 41 | + Status operator()(avro::DecoderPtr& decoder, |
| 42 | + std::vector<Tensor>& dense_tensors, |
| 43 | + sparse::ValueBuffer& buffer, |
| 44 | + std::vector<avro::GenericDatum>& skipped_data, |
| 45 | + size_t offset) { |
| 46 | + auto index = decoder->decodeUnionIndex(); |
| 47 | + if (index != non_null_index_) { |
| 48 | + return NullValueError(); |
| 49 | + } |
| 50 | + return decoder_->operator()(decoder, dense_tensors, buffer, skipped_data, |
| 51 | + offset); |
| 52 | + } |
| 53 | + |
| 54 | + private: |
| 55 | + std::unique_ptr<DecoderBase> decoder_; |
| 56 | + const size_t non_null_index_; |
| 57 | +}; |
| 58 | + |
| 59 | +class ATDSDecoder { |
| 60 | + public: |
| 61 | + explicit ATDSDecoder(const std::vector<dense::Metadata>& dense_features, |
| 62 | + const std::vector<sparse::Metadata>& sparse_features, |
| 63 | + const std::vector<varlen::Metadata>& varlen_features) |
| 64 | + : dense_features_(dense_features), |
| 65 | + sparse_features_(sparse_features), |
| 66 | + varlen_features_(varlen_features) {} |
| 67 | + |
| 68 | + Status Initialize(const avro::ValidSchema&); |
| 69 | + |
| 70 | + Status DecodeATDSDatum(avro::DecoderPtr& decoder, |
| 71 | + std::vector<Tensor>& dense_tensors, |
| 72 | + sparse::ValueBuffer& buffer, |
| 73 | + std::vector<avro::GenericDatum>& skipped_data, |
| 74 | + size_t offset) { |
| 75 | + // LOG(INFO) << "Decode atds from offset: " << offset; |
| 76 | + for (size_t i = 0; i < decoders_.size(); i++) { |
| 77 | + Status status = decoders_[i]->operator()(decoder, dense_tensors, buffer, |
| 78 | + skipped_data, offset); |
| 79 | + if (TF_PREDICT_FALSE(!status.ok())) { |
| 80 | + return FeatureDecodeError(feature_names_[i], status.error_message()); |
| 81 | + } |
| 82 | + } |
| 83 | + // LOG(INFO) << "Decode atds from offset Done: " << offset; |
| 84 | + return OkStatus(); |
| 85 | + } |
| 86 | + |
| 87 | + const std::vector<avro::GenericDatum>& GetSkippedData() { |
| 88 | + return skipped_data_; |
| 89 | + } |
| 90 | + |
| 91 | + const avro::ValidSchema& GetSchema() { return schema_; } |
| 92 | + |
| 93 | + private: |
| 94 | + template <typename Metadata> |
| 95 | + Status InitializeFeatureDecoder(const avro::ValidSchema& schema, |
| 96 | + const avro::NodePtr& root_node, |
| 97 | + const Metadata& metadata) { |
| 98 | + size_t pos; |
| 99 | + if (!root_node->nameIndex(metadata.name, pos)) { |
| 100 | + return FeatureNotFoundError(metadata.name, schema.toJson()); |
| 101 | + } |
| 102 | + decoder_types_[pos] = metadata.type; |
| 103 | + feature_names_[pos] = metadata.name; |
| 104 | + |
| 105 | + auto& feature_node = root_node->leafAt(pos); |
| 106 | + if (feature_node->type() == avro::AVRO_UNION) { |
| 107 | + size_t non_null_index = 0; |
| 108 | + size_t num_union_types = feature_node->leaves(); |
| 109 | + |
| 110 | + if (num_union_types == 2 && |
| 111 | + feature_node->leafAt(0)->type() == avro::AVRO_NULL) { |
| 112 | + non_null_index = 1; |
| 113 | + } |
| 114 | + |
| 115 | + if (num_union_types == 1 || num_union_types == 2) { |
| 116 | + auto& non_null_feature_node = feature_node->leafAt(non_null_index); |
| 117 | + TF_RETURN_IF_ERROR(ValidateSchema(non_null_feature_node, metadata)); |
| 118 | + std::unique_ptr<DecoderBase> decoder_base = |
| 119 | + CreateFeatureDecoder(non_null_feature_node, metadata); |
| 120 | + decoders_[pos] = std::unique_ptr<DecoderBase>( |
| 121 | + new NullableFeatureDecoder(decoder_base, non_null_index)); |
| 122 | + } else { |
| 123 | + std::ostringstream oss; |
| 124 | + feature_node->printJson(oss, 0); |
| 125 | + return InvalidUnionTypeError(metadata.name, oss.str()); |
| 126 | + } |
| 127 | + } else { |
| 128 | + TF_RETURN_IF_ERROR(ValidateSchema(feature_node, metadata)); |
| 129 | + decoders_[pos] = CreateFeatureDecoder(feature_node, metadata); |
| 130 | + } |
| 131 | + |
| 132 | + return OkStatus(); |
| 133 | + } |
| 134 | + |
| 135 | + const std::vector<dense::Metadata>& dense_features_; |
| 136 | + const std::vector<sparse::Metadata>& sparse_features_; |
| 137 | + const std::vector<varlen::Metadata>& varlen_features_; |
| 138 | + |
| 139 | + std::vector<string> feature_names_; |
| 140 | + std::vector<std::unique_ptr<DecoderBase>> decoders_; |
| 141 | + std::vector<FeatureType> decoder_types_; |
| 142 | + |
| 143 | + std::vector<avro::GenericDatum> skipped_data_; |
| 144 | + avro::ValidSchema schema_; |
| 145 | +}; |
| 146 | + |
| 147 | +} // namespace atds |
| 148 | +} // namespace tensorflow |
| 149 | + |
| 150 | +#endif // TENSORFLOW_IO_CORE_KERNELS_AVRO_ATDS_DECODER_H_ |
0 commit comments