alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "federated/utils.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
alanlxl | 9d26c1c | 2020-08-21 13:42:36 +1000 | [diff] [blame] | 10 | #include <base/strings/stringprintf.h> |
| 11 | |
alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame] | 12 | namespace federated { |
| 13 | |
| 14 | namespace { |
| 15 | using chromeos::federated::mojom::ExamplePtr; |
| 16 | using chromeos::federated::mojom::FloatList; |
| 17 | using chromeos::federated::mojom::Int64List; |
| 18 | using chromeos::federated::mojom::ValueList; |
| 19 | } // namespace |
| 20 | |
alanlxl | 9d26c1c | 2020-08-21 13:42:36 +1000 | [diff] [blame] | 21 | // TODO(alanlxl): just random numbers, need a discussion |
| 22 | constexpr size_t kMaxStreamingExampleCount = 4000; |
| 23 | constexpr size_t kMinExampleCount = 1; |
| 24 | |
| 25 | constexpr char kSessionStartedState[] = "started"; |
| 26 | constexpr char kSessionStoppedState[] = "stopped"; |
| 27 | constexpr char kUserDatabasePath[] = "/run/daemon-store/federated"; |
| 28 | constexpr char kDatabaseFileName[] = "examples.db"; |
| 29 | |
| 30 | // Get the database file path with the given sanitized_username. |
| 31 | base::FilePath GetDatabasePath(const std::string& sanitized_username) { |
| 32 | return base::FilePath(base::StringPrintf("%s/%s/%s", kUserDatabasePath, |
| 33 | sanitized_username.c_str(), |
| 34 | kDatabaseFileName)); |
| 35 | } |
| 36 | |
alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame] | 37 | tensorflow::Example ConvertToTensorFlowExampleProto(const ExamplePtr& example) { |
| 38 | tensorflow::Example tf_example; |
| 39 | auto& feature = *tf_example.mutable_features()->mutable_feature(); |
| 40 | |
| 41 | for (const auto& iter : example->features->feature) { |
| 42 | if (iter.second->which() == ValueList::Tag::INT64_LIST) { |
| 43 | std::vector<int64_t>& value_list = iter.second->get_int64_list()->value; |
| 44 | *feature[iter.first].mutable_int64_list()->mutable_value() = { |
| 45 | value_list.begin(), value_list.end()}; |
| 46 | } else if (iter.second->which() == ValueList::Tag::FLOAT_LIST) { |
| 47 | std::vector<double>& value_list = iter.second->get_float_list()->value; |
| 48 | *feature[iter.first].mutable_float_list()->mutable_value() = { |
| 49 | value_list.begin(), value_list.end()}; |
| 50 | } else if (iter.second->which() == ValueList::Tag::STRING_LIST) { |
| 51 | std::vector<std::string>& value_list = |
| 52 | iter.second->get_string_list()->value; |
| 53 | *feature[iter.first].mutable_bytes_list()->mutable_value() = { |
| 54 | value_list.begin(), value_list.end()}; |
| 55 | } |
| 56 | } |
| 57 | return tf_example; |
| 58 | } |
| 59 | |
| 60 | } // namespace federated |