xref: /aosp_15_r20/external/federated-compute/fcp/client/fl_runner.h (revision 14675a029014e728ec732f129a32e299b2da0601)
1*14675a02SAndroid Build Coastguard Worker /*
2*14675a02SAndroid Build Coastguard Worker  * Copyright 2020 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 #ifndef FCP_CLIENT_FL_RUNNER_H_
17*14675a02SAndroid Build Coastguard Worker #define FCP_CLIENT_FL_RUNNER_H_
18*14675a02SAndroid Build Coastguard Worker 
19*14675a02SAndroid Build Coastguard Worker #include <string>
20*14675a02SAndroid Build Coastguard Worker 
21*14675a02SAndroid Build Coastguard Worker #include "absl/status/statusor.h"
22*14675a02SAndroid Build Coastguard Worker #include "fcp/base/monitoring.h"
23*14675a02SAndroid Build Coastguard Worker #include "fcp/client/engine/engine.pb.h"
24*14675a02SAndroid Build Coastguard Worker #include "fcp/client/event_publisher.h"
25*14675a02SAndroid Build Coastguard Worker #include "fcp/client/federated_protocol.h"
26*14675a02SAndroid Build Coastguard Worker #include "fcp/client/federated_select.h"
27*14675a02SAndroid Build Coastguard Worker #include "fcp/client/files.h"
28*14675a02SAndroid Build Coastguard Worker #include "fcp/client/fl_runner.pb.h"
29*14675a02SAndroid Build Coastguard Worker #include "fcp/client/flags.h"
30*14675a02SAndroid Build Coastguard Worker #include "fcp/client/http/http_client.h"
31*14675a02SAndroid Build Coastguard Worker #include "fcp/client/interruptible_runner.h"
32*14675a02SAndroid Build Coastguard Worker #include "fcp/client/log_manager.h"
33*14675a02SAndroid Build Coastguard Worker #include "fcp/client/opstats/opstats_logger.h"
34*14675a02SAndroid Build Coastguard Worker #include "fcp/client/phase_logger.h"
35*14675a02SAndroid Build Coastguard Worker #include "fcp/client/simple_task_environment.h"
36*14675a02SAndroid Build Coastguard Worker #include "fcp/protos/plan.pb.h"
37*14675a02SAndroid Build Coastguard Worker #include "tensorflow/core/framework/tensor.h"
38*14675a02SAndroid Build Coastguard Worker 
39*14675a02SAndroid Build Coastguard Worker namespace fcp {
40*14675a02SAndroid Build Coastguard Worker namespace client {
41*14675a02SAndroid Build Coastguard Worker 
42*14675a02SAndroid Build Coastguard Worker inline constexpr absl::string_view kTensorflowCheckpointAggregand =
43*14675a02SAndroid Build Coastguard Worker     "tensorflow_checkpoint";
44*14675a02SAndroid Build Coastguard Worker 
45*14675a02SAndroid Build Coastguard Worker // Prod entry point for running a federated computation. Concurrent calls, with
46*14675a02SAndroid Build Coastguard Worker // the same SimpleTaskEnvironment::GetBaseDir(), are not supported.
47*14675a02SAndroid Build Coastguard Worker //
48*14675a02SAndroid Build Coastguard Worker // This is a long running blocking call that - for a successful run -
49*14675a02SAndroid Build Coastguard Worker // encompasses connecting to a server, downloading and running a computation,
50*14675a02SAndroid Build Coastguard Worker // uploading results, and storing logs about the run in an operational stats DB.
51*14675a02SAndroid Build Coastguard Worker // During that call, the function will call back (from both the calling and from
52*14675a02SAndroid Build Coastguard Worker // newly created threads) into the dependencies injected here for to query for
53*14675a02SAndroid Build Coastguard Worker // examples, check whether it should abort, publish events / logs for telemetry,
54*14675a02SAndroid Build Coastguard Worker // create files, and query feature flags.
55*14675a02SAndroid Build Coastguard Worker //
56*14675a02SAndroid Build Coastguard Worker // Arguments:
57*14675a02SAndroid Build Coastguard Worker // - federated_service_uri, api_key: used to connect to the Federated server.
58*14675a02SAndroid Build Coastguard Worker // - test_cert_path: a file path to a CA certificate to be used in tests. Should
59*14675a02SAndroid Build Coastguard Worker //     be empty for production use; when used in tests, the URI must use the
60*14675a02SAndroid Build Coastguard Worker //     https+test:// scheme.
61*14675a02SAndroid Build Coastguard Worker // - session_name: A client-side identifier of the type of work this computation
62*14675a02SAndroid Build Coastguard Worker //     performs; used to annotate log entries in the operational stats DB.
63*14675a02SAndroid Build Coastguard Worker // - population_name: a string provided to the Federated server to identify
64*14675a02SAndroid Build Coastguard Worker //     what population this device is checking in for.
65*14675a02SAndroid Build Coastguard Worker // - client_version: A platform-specific identifier that is used by the server
66*14675a02SAndroid Build Coastguard Worker //     to serve versioned computations - that is, versions of a computation that
67*14675a02SAndroid Build Coastguard Worker //     have been tested and found to be compatible with this device's version -
68*14675a02SAndroid Build Coastguard Worker //     or reject the device.
69*14675a02SAndroid Build Coastguard Worker // - attestation_measurement: An opaque string from a "measurement" that can be
70*14675a02SAndroid Build Coastguard Worker // used
71*14675a02SAndroid Build Coastguard Worker //     by the server to attest the device integrity.
72*14675a02SAndroid Build Coastguard Worker //
73*14675a02SAndroid Build Coastguard Worker // Returns:
74*14675a02SAndroid Build Coastguard Worker // On success, the returned FLRunnerResult contains information on when the
75*14675a02SAndroid Build Coastguard Worker // function should be called again for this session.
76*14675a02SAndroid Build Coastguard Worker absl::StatusOr<FLRunnerResult> RunFederatedComputation(
77*14675a02SAndroid Build Coastguard Worker     SimpleTaskEnvironment* env_deps, EventPublisher* event_publisher,
78*14675a02SAndroid Build Coastguard Worker     Files* files, LogManager* log_manager, const Flags* flags,
79*14675a02SAndroid Build Coastguard Worker     const std::string& federated_service_uri, const std::string& api_key,
80*14675a02SAndroid Build Coastguard Worker     const std::string& test_cert_path, const std::string& session_name,
81*14675a02SAndroid Build Coastguard Worker     const std::string& population_name, const std::string& retry_token,
82*14675a02SAndroid Build Coastguard Worker     const std::string& client_version,
83*14675a02SAndroid Build Coastguard Worker     const std::string& attestation_measurement);
84*14675a02SAndroid Build Coastguard Worker 
85*14675a02SAndroid Build Coastguard Worker // This is exposed for use in tests that require a mocked FederatedProtocol and
86*14675a02SAndroid Build Coastguard Worker // OpStatsLogger. Otherwise, this is used internally by the other
87*14675a02SAndroid Build Coastguard Worker // RunFederatedComputation method once the FederatedProtocol and OpStatsLogger
88*14675a02SAndroid Build Coastguard Worker // objects have been created.
89*14675a02SAndroid Build Coastguard Worker absl::StatusOr<FLRunnerResult> RunFederatedComputation(
90*14675a02SAndroid Build Coastguard Worker     SimpleTaskEnvironment* env_deps, PhaseLogger& phase_logger,
91*14675a02SAndroid Build Coastguard Worker     EventPublisher* event_publisher, Files* files, LogManager* log_manager,
92*14675a02SAndroid Build Coastguard Worker     ::fcp::client::opstats::OpStatsLogger* opstats_logger, const Flags* flags,
93*14675a02SAndroid Build Coastguard Worker     FederatedProtocol* federated_protocol,
94*14675a02SAndroid Build Coastguard Worker     FederatedSelectManager* fedselect_manager,
95*14675a02SAndroid Build Coastguard Worker     const fcp::client::InterruptibleRunner::TimingConfig& timing_config,
96*14675a02SAndroid Build Coastguard Worker     const absl::Time reference_time, const std::string& session_name,
97*14675a02SAndroid Build Coastguard Worker     const std::string& population_name);
98*14675a02SAndroid Build Coastguard Worker 
99*14675a02SAndroid Build Coastguard Worker // This is exposed for use in compatibility tests only. Prod code should call
100*14675a02SAndroid Build Coastguard Worker // RunFederatedComputation.
101*14675a02SAndroid Build Coastguard Worker FLRunnerTensorflowSpecResult RunPlanWithTensorflowSpecForTesting(
102*14675a02SAndroid Build Coastguard Worker     SimpleTaskEnvironment* env_deps, EventPublisher* event_publisher,
103*14675a02SAndroid Build Coastguard Worker     Files* files, LogManager* log_manager, const Flags* flags,
104*14675a02SAndroid Build Coastguard Worker     const google::internal::federated::plan::ClientOnlyPlan& client_plan,
105*14675a02SAndroid Build Coastguard Worker     const std::string& checkpoint_input_filename,
106*14675a02SAndroid Build Coastguard Worker     const fcp::client::InterruptibleRunner::TimingConfig& timing_config,
107*14675a02SAndroid Build Coastguard Worker     const absl::Time run_plan_start_time, const absl::Time reference_time);
108*14675a02SAndroid Build Coastguard Worker 
109*14675a02SAndroid Build Coastguard Worker }  // namespace client
110*14675a02SAndroid Build Coastguard Worker }  // namespace fcp
111*14675a02SAndroid Build Coastguard Worker 
112*14675a02SAndroid Build Coastguard Worker #endif  // FCP_CLIENT_FL_RUNNER_H_
113