xref: /aosp_15_r20/external/federated-compute/fcp/aggregation/testing/testing.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1*14675a02SAndroid Build Coastguard Worker /*
2*14675a02SAndroid Build Coastguard Worker  * Copyright 2022 Google LLC
3*14675a02SAndroid Build Coastguard Worker  *
4*14675a02SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*14675a02SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*14675a02SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*14675a02SAndroid Build Coastguard Worker  *
8*14675a02SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*14675a02SAndroid Build Coastguard Worker  *
10*14675a02SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*14675a02SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*14675a02SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*14675a02SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*14675a02SAndroid Build Coastguard Worker  * limitations under the License.
15*14675a02SAndroid Build Coastguard Worker  */
16*14675a02SAndroid Build Coastguard Worker 
17*14675a02SAndroid Build Coastguard Worker #include "fcp/aggregation/testing/testing.h"
18*14675a02SAndroid Build Coastguard Worker 
19*14675a02SAndroid Build Coastguard Worker #include <memory>
20*14675a02SAndroid Build Coastguard Worker #include <ostream>
21*14675a02SAndroid Build Coastguard Worker #include <string>
22*14675a02SAndroid Build Coastguard Worker #include <utility>
23*14675a02SAndroid Build Coastguard Worker 
24*14675a02SAndroid Build Coastguard Worker #include "fcp/base/platform.h"
25*14675a02SAndroid Build Coastguard Worker #include "fcp/tensorflow/status.h"
26*14675a02SAndroid Build Coastguard Worker #include "fcp/testing/testing.h"
27*14675a02SAndroid Build Coastguard Worker #include "tensorflow/c/checkpoint_reader.h"
28*14675a02SAndroid Build Coastguard Worker #include "tensorflow/c/tf_status.h"
29*14675a02SAndroid Build Coastguard Worker #include "tensorflow/c/tf_status_helper.h"
30*14675a02SAndroid Build Coastguard Worker #include "tensorflow/cc/framework/scope.h"
31*14675a02SAndroid Build Coastguard Worker #include "tensorflow/cc/ops/io_ops.h"
32*14675a02SAndroid Build Coastguard Worker #include "tensorflow/core/framework/tensor.h"
33*14675a02SAndroid Build Coastguard Worker #include "tensorflow/core/public/session.h"
34*14675a02SAndroid Build Coastguard Worker 
35*14675a02SAndroid Build Coastguard Worker namespace fcp::aggregation {
36*14675a02SAndroid Build Coastguard Worker 
37*14675a02SAndroid Build Coastguard Worker using ::tensorflow::StatusFromTF_Status;
38*14675a02SAndroid Build Coastguard Worker using ::tensorflow::TF_StatusPtr;
39*14675a02SAndroid Build Coastguard Worker using ::tensorflow::checkpoint::CheckpointReader;
40*14675a02SAndroid Build Coastguard Worker 
operator <<(std::ostream & os,const Tensor & tensor)41*14675a02SAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, const Tensor& tensor) {
42*14675a02SAndroid Build Coastguard Worker   DTYPE_CASES(tensor.dtype(), T,
43*14675a02SAndroid Build Coastguard Worker               DescribeTensor<T>(&os, tensor.dtype(), tensor.shape(),
44*14675a02SAndroid Build Coastguard Worker                                 TensorValuesToVector<T>(tensor)));
45*14675a02SAndroid Build Coastguard Worker   return os;
46*14675a02SAndroid Build Coastguard Worker }
47*14675a02SAndroid Build Coastguard Worker 
CreateStringTfTensor(std::initializer_list<int64_t> dim_sizes,std::initializer_list<string_view> values)48*14675a02SAndroid Build Coastguard Worker tf::Tensor CreateStringTfTensor(std::initializer_list<int64_t> dim_sizes,
49*14675a02SAndroid Build Coastguard Worker                                 std::initializer_list<string_view> values) {
50*14675a02SAndroid Build Coastguard Worker   tf::TensorShape shape;
51*14675a02SAndroid Build Coastguard Worker   EXPECT_TRUE(tf::TensorShape::BuildTensorShape(dim_sizes, &shape).ok());
52*14675a02SAndroid Build Coastguard Worker   tf::Tensor tensor(tf::DT_STRING, shape);
53*14675a02SAndroid Build Coastguard Worker   auto* tensor_data_ptr = reinterpret_cast<tf::tstring*>(tensor.data());
54*14675a02SAndroid Build Coastguard Worker   for (auto value : values) {
55*14675a02SAndroid Build Coastguard Worker     *tensor_data_ptr++ = value;
56*14675a02SAndroid Build Coastguard Worker   }
57*14675a02SAndroid Build Coastguard Worker   return tensor;
58*14675a02SAndroid Build Coastguard Worker }
59*14675a02SAndroid Build Coastguard Worker 
CreateTfCheckpoint(tf::Input filename,tf::Input tensor_names,tf::InputList tensors)60*14675a02SAndroid Build Coastguard Worker tf::Status CreateTfCheckpoint(tf::Input filename, tf::Input tensor_names,
61*14675a02SAndroid Build Coastguard Worker                               tf::InputList tensors) {
62*14675a02SAndroid Build Coastguard Worker   tf::Scope scope = tf::Scope::NewRootScope();
63*14675a02SAndroid Build Coastguard Worker 
64*14675a02SAndroid Build Coastguard Worker   tf::ops::Save save(scope, std::move(filename), std::move(tensor_names),
65*14675a02SAndroid Build Coastguard Worker                      std::move(tensors));
66*14675a02SAndroid Build Coastguard Worker 
67*14675a02SAndroid Build Coastguard Worker   tf::GraphDef graph;
68*14675a02SAndroid Build Coastguard Worker   if (auto s = scope.ToGraphDef(&graph); !s.ok()) return s;
69*14675a02SAndroid Build Coastguard Worker 
70*14675a02SAndroid Build Coastguard Worker   auto session = absl::WrapUnique(tf::NewSession(tf::SessionOptions()));
71*14675a02SAndroid Build Coastguard Worker   if (auto s = session->Create(graph); !s.ok()) return s;
72*14675a02SAndroid Build Coastguard Worker   return session->Run({}, {}, {save.operation.node()->name()}, nullptr);
73*14675a02SAndroid Build Coastguard Worker }
74*14675a02SAndroid Build Coastguard Worker 
75*14675a02SAndroid Build Coastguard Worker absl::StatusOr<absl::flat_hash_map<std::string, std::string>>
SummarizeCheckpoint(const absl::Cord & checkpoint)76*14675a02SAndroid Build Coastguard Worker SummarizeCheckpoint(const absl::Cord& checkpoint) {
77*14675a02SAndroid Build Coastguard Worker   std::string filename = TemporaryTestFile(".ckpt");
78*14675a02SAndroid Build Coastguard Worker   FCP_RETURN_IF_ERROR(WriteCordToFile(filename, checkpoint));
79*14675a02SAndroid Build Coastguard Worker 
80*14675a02SAndroid Build Coastguard Worker   TF_StatusPtr tf_status(TF_NewStatus());
81*14675a02SAndroid Build Coastguard Worker   auto reader = std::make_unique<CheckpointReader>(filename, tf_status.get());
82*14675a02SAndroid Build Coastguard Worker   FCP_RETURN_IF_ERROR(
83*14675a02SAndroid Build Coastguard Worker       ConvertFromTensorFlowStatus(StatusFromTF_Status(tf_status.get())));
84*14675a02SAndroid Build Coastguard Worker 
85*14675a02SAndroid Build Coastguard Worker   absl::flat_hash_map<std::string, std::string> tensors;
86*14675a02SAndroid Build Coastguard Worker   for (const auto& [name, shape] : reader->GetVariableToShapeMap()) {
87*14675a02SAndroid Build Coastguard Worker     std::unique_ptr<::tensorflow::Tensor> tensor;
88*14675a02SAndroid Build Coastguard Worker     reader->GetTensor(name, &tensor, tf_status.get());
89*14675a02SAndroid Build Coastguard Worker     FCP_RETURN_IF_ERROR(
90*14675a02SAndroid Build Coastguard Worker         ConvertFromTensorFlowStatus(StatusFromTF_Status(tf_status.get())));
91*14675a02SAndroid Build Coastguard Worker     tensors[name] = tensor->SummarizeValue(/*max_entries=*/10);
92*14675a02SAndroid Build Coastguard Worker   }
93*14675a02SAndroid Build Coastguard Worker   return tensors;
94*14675a02SAndroid Build Coastguard Worker }
95*14675a02SAndroid Build Coastguard Worker }  // namespace fcp::aggregation
96