Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(framework) Add a Rust Flower SDK and a dummy example #2546

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cargo.lock
10 changes: 10 additions & 0 deletions examples/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "rust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
flwr = { path = "../../src/rust/flwr/" }
tokio = "1.33.0"
10 changes: 10 additions & 0 deletions examples/rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
tags: [quickstart, dummy, sdk]
dataset: []
framework: [Rust]
---

# Rust quickstart example

Currently this example only provides an empty skeleton for implementing
a Flower client in Rust.
69 changes: 69 additions & 0 deletions examples/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use flwr::client;
use flwr::start;
use flwr::typing;

struct TestClient;

impl client::Client for TestClient {
fn get_parameters(&self) -> typing::GetParametersRes {
println!("get_parameters");
typing::GetParametersRes {
parameters: typing::Parameters {
tensors: vec![vec![1 as u8]],
tensor_type: "".to_string(),
},
status: typing::Status {
code: typing::Code::OK,
message: "".to_string(),
},
}
}

fn get_properties(&self, ins: typing::GetPropertiesIns) -> typing::GetPropertiesRes {
println!("get_properties");
typing::GetPropertiesRes {
properties: std::collections::HashMap::new(),
status: typing::Status {
code: typing::Code::OK,
message: "".to_string(),
},
}
}

fn fit(&self, ins: typing::FitIns) -> typing::FitRes {
println!("fit");
typing::FitRes {
parameters: typing::Parameters {
tensors: vec![vec![1 as u8]],
tensor_type: "".to_string(),
},
num_examples: 1,
metrics: std::collections::HashMap::new(),
status: typing::Status {
code: typing::Code::OK,
message: "".to_string(),
},
}
}

fn evaluate(&self, ins: typing::EvaluateIns) -> typing::EvaluateRes {
println!("evaluate");
typing::EvaluateRes {
num_examples: 1,
metrics: std::collections::HashMap::new(),
loss: 1.0,
status: typing::Status {
code: typing::Code::OK,
message: "".to_string(),
},
}
}
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Start client...");
let _client =
start::start_client("http://127.0.0.1:9092", &TestClient, None, Some("rere")).await?;
Ok(())
}
1 change: 1 addition & 0 deletions src/rust/flwr/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cargo.lock
23 changes: 23 additions & 0 deletions src/rust/flwr/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "flwr"
version = "0.1.0"
edition = "2021"
include = ["src/**/*", "proto/**/*"]

[lib]
name = "flwr"
path = "src/lib.rs"

[dependencies]
async-channel = "2.0.0"
futures = "0.3.29"
prost = "0.12.1"
rustls = "0.21.8"
rustls-native-certs = "0.6.3"
tokio = { version = "1.33.0", features = ["full"] }
tokio-stream = "0.1.14"
tonic = { version = "0.10.2", features = ["transport", "tls"] }
uuid = { version = "1.5.0", features = ["v4"] }

[build-dependencies]
tonic-build = "0.10.2"
11 changes: 11 additions & 0 deletions src/rust/flwr/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let proto_src = format!("{}/../../proto/flwr/proto/fleet.proto", manifest_dir);
let proto_include = format!("{}/../../proto", manifest_dir);

tonic_build::configure()
.build_server(false)
.out_dir(format!("{}/src", manifest_dir))
.compile(&[&proto_src], &[&proto_include])
.unwrap();
}
22 changes: 22 additions & 0 deletions src/rust/flwr/src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::typing as local;

pub trait Client {
/// Return the current local model parameters
fn get_parameters(&self) -> local::GetParametersRes;

fn get_properties(&self, ins: local::GetPropertiesIns) -> local::GetPropertiesRes;

/// Refine the provided weights using the locally held dataset
///
/// The training instructions contain (global) model parameters
/// received from the server and a dictionary of configuration
/// values used to customize the local training process.
fn fit(&self, ins: local::FitIns) -> local::FitRes;

/// Evaluate the provided weights using the locally held dataset.
///
/// The evaluation instructions contain (global) model parameters
/// received from the server and a dictionary of configuration
/// values used to customize the local evaluation process.
fn evaluate(&self, ins: local::EvaluateIns) -> local::EvaluateRes;
}
Loading