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

Use C++ SDK package in the C++ quickstart example #2545

Merged
merged 20 commits into from
Nov 8, 2023
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ jobs:
- name: Run cppcheck
run: |
cd build
cppcheck --enable=all --inline-suppr --suppress=missingIncludeSystem -I../src/cc/flwr/include ../src/cc/flwr/src
cppcheck --enable=all -I../src/cc/flwr/include ../src/cc/flwr/src

- name: End-to-end test
run: |
cd examples/quickstart-cpp
cmake -S . -B build
cmake -DUSE_LOCAL_FLWR=ON -S . -B build
cmake --build build
pip install ../..
timeout 2m python server.py &
Expand Down
19 changes: 18 additions & 1 deletion examples/quickstart-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project(SimpleCppFlowerClient VERSION 0.10
DESCRIPTION "Creates a Simple C++ Flower client that trains a linear model on synthetic data."
LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
option(USE_LOCAL_FLWR "Use local Flower directory instead of fetching from GitHub" OFF)

######################
### Download gRPC
Expand All @@ -28,8 +29,24 @@ endif()

######################
### FLWR_LIB
if(USE_LOCAL_FLWR)
set(FLWR_SOURCE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../..")
else()
FetchContent_Declare(
flwr_repo
GIT_REPOSITORY https://github.com/adap/flower.git
GIT_TAG main
)

FetchContent_GetProperties(flwr_repo)
if(NOT flwr_repo_POPULATED)
FetchContent_Populate(flwr_repo)
endif()

set(FLWR_SOURCE_ROOT "${flwr_repo_SOURCE_DIR}")
endif()

set(FLWR_SDK_PATH "../../src/cc/flwr")
set(FLWR_SDK_PATH "${FLWR_SOURCE_ROOT}/src/cc/flwr")

file(GLOB FLWR_SRCS "${FLWR_SDK_PATH}/src/*.cc")
file(GLOB FLWR_PROTO_SRCS "${FLWR_SDK_PATH}/include/flwr/proto/*.cc")
Expand Down
8 changes: 4 additions & 4 deletions examples/quickstart-cpp/include/line_fit_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

#include <vector>

#include "synthetic_dataset.h"
#include "linear_algebra_util.h"
#include "synthetic_dataset.h"
#include <cstddef>
class LineFitModel {
public:
public:
LineFitModel(int num_iterations, double learning_rate, int num_params);

std::vector<double> predict(std::vector<std::vector<double>> X);
Expand All @@ -30,7 +30,7 @@ class LineFitModel {

size_t get_model_size();

private:
private:
int num_iterations;
int batch_size;
double learning_rate;
Expand All @@ -41,4 +41,4 @@ class LineFitModel {
double compute_mse(std::vector<double> true_y, std::vector<double> pred);
};

#endif //FLOWER_CPP_LINE_FIT_MODEL_H
#endif // FLOWER_CPP_LINE_FIT_MODEL_H
18 changes: 11 additions & 7 deletions examples/quickstart-cpp/include/linear_algebra_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
#include <vector>

class LinearAlgebraUtil {
public:
static std::vector<double> subtract_vector(std::vector<double> v1, std::vector<double> v2);
public:
static std::vector<double> subtract_vector(std::vector<double> v1,
std::vector<double> v2);

static std::vector<double> multiply_matrix_vector(std::vector<std::vector<double>> mat, std::vector<double> v);
static std::vector<double>
multiply_matrix_vector(std::vector<std::vector<double>> mat,
std::vector<double> v);

static std::vector<double> add_vector_scalar(std::vector<double> v, double a);

static std::vector<double> multiply_vector_scalar(std::vector<double> v, double a);

static std::vector<std::vector<double>> transpose_vector(std::vector<std::vector<double>> v);
static std::vector<double> multiply_vector_scalar(std::vector<double> v,
double a);

static std::vector<std::vector<double>>
transpose_vector(std::vector<std::vector<double>> v);
};

#endif //FLOWER_CPPV2_LINEAR_ALGEBRA_UTIL_H
#endif // FLOWER_CPPV2_LINEAR_ALGEBRA_UTIL_H
22 changes: 11 additions & 11 deletions examples/quickstart-cpp/include/simple_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,40 @@
* ********************************************************************************************************/
#pragma once
#include "client.h"
#include "synthetic_dataset.h"
#include "line_fit_model.h"
#include "synthetic_dataset.h"
#include <ctime>
#include <iostream>
#include <list>
#include <memory>
#include <sstream>
#include <string>
#include <tuple>
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
/**
* Validate the network on the entire test set
*
*/

class SimpleFlwrClient : public flwr_local::Client {
public:
SimpleFlwrClient(std::string client_id,
LineFitModel &model,
public:
SimpleFlwrClient(std::string client_id, LineFitModel &model,
SyntheticDataset &training_dataset,
SyntheticDataset &validation_dataset,
SyntheticDataset &test_dataset);
void set_parameters(flwr_local::Parameters params);

virtual flwr_local::ParametersRes get_parameters() override;
virtual flwr_local::PropertiesRes get_properties(flwr_local::PropertiesIns ins) override;
virtual flwr_local::EvaluateRes evaluate(flwr_local::EvaluateIns ins) override;
virtual flwr_local::PropertiesRes
get_properties(flwr_local::PropertiesIns ins) override;
virtual flwr_local::EvaluateRes
evaluate(flwr_local::EvaluateIns ins) override;
virtual flwr_local::FitRes fit(flwr_local::FitIns ins) override;

private:
private:
int64_t client_id;
LineFitModel &model;
SyntheticDataset &training_dataset;
SyntheticDataset &validation_dataset;
SyntheticDataset &test_dataset;

};
11 changes: 6 additions & 5 deletions examples/quickstart-cpp/include/synthetic_dataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
#ifndef FLOWER_CPP_SYNTHETIC_DATASET_H
#define FLOWER_CPP_SYNTHETIC_DATASET_H

#include <vector>
#include <cstddef>
#include <vector>
class SyntheticDataset {
public:
// Generates the synthetic dataset of size size around given vector m of size ms_size and given bias b.
public:
// Generates the synthetic dataset of size size around given vector m of size
// ms_size and given bias b.
SyntheticDataset(std::vector<double> ms, double b, size_t size);

// Returns the size of the dataset.
Expand All @@ -20,7 +21,7 @@ class SyntheticDataset {

int get_features_count();

private:
private:
std::vector<double> ms;
double b;

Expand All @@ -30,4 +31,4 @@ class SyntheticDataset {
std::vector<std::vector<double>> data_points;
};

#endif //FLOWER_CPP_SYNTHETIC_DATASET_H
#endif // FLOWER_CPP_SYNTHETIC_DATASET_H
Loading