xref: /aosp_15_r20/external/tensorflow/tensorflow/core/data/service/validate_utils.cc (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 #include "tensorflow/core/data/service/validate_utils.h"
16 
17 #include <string>
18 
19 #include "tensorflow/core/platform/errors.h"
20 #include "tensorflow/core/platform/protobuf.h"
21 #include "tensorflow/core/platform/status.h"
22 #include "tensorflow/core/platform/statusor.h"
23 #include "tensorflow/core/protobuf/data_service.pb.h"
24 #include "tensorflow/core/protobuf/struct.pb.h"
25 
26 namespace tensorflow {
27 namespace data {
28 namespace {
29 
30 using ::tensorflow::protobuf::util::MessageDifferencer;
31 
DecodeElementSpec(const std::string & dataset_id,const std::string & encoded_spec)32 StatusOr<StructuredValue> DecodeElementSpec(const std::string& dataset_id,
33                                             const std::string& encoded_spec) {
34   if (encoded_spec.empty()) {
35     return StructuredValue();
36   }
37 
38   StructuredValue decoded_spec;
39   if (!decoded_spec.ParsePartialFromString(encoded_spec)) {
40     return errors::InvalidArgument("Failed to parse element_spec for dataset ",
41                                    dataset_id, ": ", encoded_spec, ".");
42   }
43   return decoded_spec;
44 }
45 
ValidateElementSpec(const std::string & dataset_id,const std::string & encoded_spec1,const std::string & encoded_spec2)46 Status ValidateElementSpec(const std::string& dataset_id,
47                            const std::string& encoded_spec1,
48                            const std::string& encoded_spec2) {
49   if (encoded_spec1.empty() && encoded_spec2.empty()) {
50     return OkStatus();
51   }
52   TF_ASSIGN_OR_RETURN(StructuredValue element_spec1,
53                       DecodeElementSpec(dataset_id, encoded_spec1));
54   TF_ASSIGN_OR_RETURN(StructuredValue element_spec2,
55                       DecodeElementSpec(dataset_id, encoded_spec2));
56 
57   MessageDifferencer differ;
58   std::string diff;
59   differ.ReportDifferencesToString(&diff);
60   differ.set_message_field_comparison(MessageDifferencer::EQUIVALENT);
61   differ.set_repeated_field_comparison(MessageDifferencer::AS_SET);
62   differ.set_float_comparison(MessageDifferencer::APPROXIMATE);
63   if (!differ.Compare(element_spec1, element_spec2)) {
64     return errors::InvalidArgument(
65         "Datasets with the same ID should have the same structure, got diff ",
66         "for dataset ID ", dataset_id, " with different element_spec: ", diff,
67         ". To fix this error, make sure you're registering the same dataset ",
68         "with the same ID.");
69   }
70   return OkStatus();
71 }
72 
ValidateDatasetMetadata(const std::string & dataset_id,const DataServiceMetadata & metadata1,const DataServiceMetadata & metadata2)73 Status ValidateDatasetMetadata(const std::string& dataset_id,
74                                const DataServiceMetadata& metadata1,
75                                const DataServiceMetadata& metadata2) {
76   TF_RETURN_IF_ERROR(ValidateElementSpec(dataset_id, metadata1.element_spec(),
77                                          metadata2.element_spec()));
78   MessageDifferencer differ;
79   std::string diff;
80   differ.ReportDifferencesToString(&diff);
81   differ.IgnoreField(
82       DataServiceMetadata::descriptor()->FindFieldByName("element_spec"));
83   differ.set_message_field_comparison(MessageDifferencer::EQUIVALENT);
84   differ.set_repeated_field_comparison(MessageDifferencer::AS_SET);
85   differ.set_float_comparison(MessageDifferencer::APPROXIMATE);
86   if (!differ.Compare(metadata1, metadata2)) {
87     return errors::InvalidArgument(
88         "Datasets with the same ID should have the same structure, got diff ",
89         "for dataset ID ", dataset_id, ": ", diff, ". To fix this error, make ",
90         "sure you're registering the same dataset with the same ID.");
91   }
92   return OkStatus();
93 }
94 
95 }  // namespace
96 
ValidateMatchingDataset(const std::string & dataset_id,const DataServiceMetadata & metadata1,const DataServiceMetadata & metadata2)97 Status ValidateMatchingDataset(const std::string& dataset_id,
98                                const DataServiceMetadata& metadata1,
99                                const DataServiceMetadata& metadata2) {
100   return ValidateDatasetMetadata(dataset_id, metadata1, metadata2);
101 }
102 
103 }  // namespace data
104 }  // namespace tensorflow
105