xref: /aosp_15_r20/external/federated-compute/fcp/aggregation/tensorflow/checkpoint_reader.h (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef FCP_AGGREGATION_TENSORFLOW_CHECKPOINT_READER_H_
18 #define FCP_AGGREGATION_TENSORFLOW_CHECKPOINT_READER_H_
19 
20 #include <memory>
21 #include <string>
22 
23 #include "absl/container/flat_hash_map.h"
24 #include "absl/status/statusor.h"
25 #include "fcp/aggregation/core/datatype.h"
26 #include "fcp/aggregation/core/tensor.h"
27 #include "fcp/aggregation/core/tensor_shape.h"
28 #include "tensorflow/c/checkpoint_reader.h"
29 
30 namespace fcp::aggregation::tensorflow {
31 
32 // This class wraps Tensorflow checkpoint reader and provides a similar
33 // functionality but returns Aggregation Core tensors instead.
34 // This class is designed to read only dense tensors that consist of a
35 // single slice.
36 class CheckpointReader final {
37  public:
38   // CheckpointReader is neither copyable nor moveable
39   CheckpointReader(const CheckpointReader&) = delete;
40   CheckpointReader& operator=(const CheckpointReader&) = delete;
41 
42   using DataTypeMap = absl::flat_hash_map<std::string, DataType>;
43   using TensorShapeMap = absl::flat_hash_map<std::string, TensorShape>;
44 
45   static absl::StatusOr<std::unique_ptr<CheckpointReader>> Create(
46       const std::string& filename);
47 
GetDataTypeMap()48   const DataTypeMap& GetDataTypeMap() const { return data_type_map_; }
GetTensorShapeMap()49   const TensorShapeMap& GetTensorShapeMap() const { return shape_map_; }
50 
51   absl::StatusOr<Tensor> GetTensor(const std::string& name) const;
52 
53  private:
54   CheckpointReader(std::unique_ptr<::tensorflow::checkpoint::CheckpointReader>
55                        tensorflow_checkpoint_reader,
56                    DataTypeMap data_type_map, TensorShapeMap shape_map);
57 
58   std::unique_ptr<::tensorflow::checkpoint::CheckpointReader>
59       tf_checkpoint_reader_;
60   DataTypeMap data_type_map_;
61   TensorShapeMap shape_map_;
62 };
63 
64 }  // namespace fcp::aggregation::tensorflow
65 
66 #endif  // FCP_AGGREGATION_TENSORFLOW_CHECKPOINT_READER_H_
67