xref: /aosp_15_r20/external/federated-compute/fcp/aggregation/tensorflow/checkpoint_reader_test.cc (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 #include "fcp/aggregation/tensorflow/checkpoint_reader.h"
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <initializer_list>
22 #include <memory>
23 #include <utility>
24 
25 #include "gmock/gmock.h"
26 #include "gtest/gtest.h"
27 #include "absl/strings/string_view.h"
28 #include "fcp/aggregation/testing/testing.h"
29 #include "fcp/base/monitoring.h"
30 #include "fcp/base/platform.h"
31 #include "fcp/testing/testing.h"
32 
33 namespace fcp::aggregation::tensorflow {
34 namespace {
35 
36 using ::testing::Key;
37 using ::testing::UnorderedElementsAre;
38 
TEST(CheckpointReaderTest,ReadTensors)39 TEST(CheckpointReaderTest, ReadTensors) {
40   // Write a test TF checkpoint with 3 tensors
41   auto temp_filename = TemporaryTestFile(".ckpt");
42   auto tensor_a =
43       CreateTfTensor<float>(tf::DT_FLOAT, {4}, {1.0, 2.0, 3.0, 4.0});
44   auto tensor_b =
45       CreateTfTensor<int32_t>(tf::DT_INT32, {2, 3}, {11, 12, 13, 14, 15, 16});
46   auto tensor_c = CreateStringTfTensor({}, {"foobar"});
47   EXPECT_TRUE(CreateTfCheckpoint(temp_filename, {"a", "b", "c"},
48                                  {tensor_a, tensor_b, tensor_c})
49                   .ok());
50 
51   // Read the checkpoint using the Aggregation Core checkpoint reader.
52   auto checkpoint_reader_or_status = CheckpointReader::Create(temp_filename);
53   EXPECT_OK(checkpoint_reader_or_status.status());
54 
55   auto checkpoint_reader = std::move(checkpoint_reader_or_status).value();
56   EXPECT_THAT(checkpoint_reader->GetDataTypeMap(),
57               UnorderedElementsAre(Key("a"), Key("b"), Key("c")));
58   EXPECT_THAT(checkpoint_reader->GetTensorShapeMap(),
59               UnorderedElementsAre(Key("a"), Key("b"), Key("c")));
60 
61   // Read and verify the tensors.
62   EXPECT_THAT(*checkpoint_reader->GetTensor("a"),
63               IsTensor<float>({4}, {1.0, 2.0, 3.0, 4.0}));
64   EXPECT_THAT(*checkpoint_reader->GetTensor("b"),
65               IsTensor<int32_t>({2, 3}, {11, 12, 13, 14, 15, 16}));
66   EXPECT_THAT(*checkpoint_reader->GetTensor("c"),
67               IsTensor<string_view>({}, {"foobar"}));
68 }
69 
TEST(CheckpointReaderTest,InvalidFileName)70 TEST(CheckpointReaderTest, InvalidFileName) {
71   auto checkpoint_reader_or_status = CheckpointReader::Create("foo/bar");
72   EXPECT_THAT(checkpoint_reader_or_status, IsCode(INTERNAL));
73 }
74 
TEST(CheckpointReaderTest,MalformedFile)75 TEST(CheckpointReaderTest, MalformedFile) {
76   auto temp_filename = TemporaryTestFile(".ckpt");
77   WriteStringToFile(temp_filename, "foobar").IgnoreError();
78   auto checkpoint_reader_or_status = CheckpointReader::Create(temp_filename);
79   EXPECT_THAT(checkpoint_reader_or_status, IsCode(INTERNAL));
80 }
81 
82 }  // namespace
83 }  // namespace fcp::aggregation::tensorflow
84