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

Dev/v0.7.0 #523

Merged
merged 6 commits into from
Sep 3, 2022
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
7 changes: 2 additions & 5 deletions android/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,8 @@ Please follow this tutorial (https://doc.fedml.ai/mlops/user_guide.html) to star
</tr>
</table>


## About Authors

FedML team starts working on Android Platform from year 2009, the beginning of Android Pixel One smartphone (yeah, we are fans of Google Android).
We are good at industrial grade Android development. See CTO and Senior Android Engineer's project experience in Android at https://chaoyanghe.com/industrial-experience/.
## How to Run?
https://doc.fedml.ai/cross-device/examples/cross_device_android_example.html


## Want More Advanced APIs or Features?
Expand Down
Binary file modified android/app/src/main/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed android/app/src/main/res/mipmap-mdpi/ic_launcher.png
Binary file not shown.
Binary file modified android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file modified android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file modified android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions android/fedmlsdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
cmake {
cppFlags '-fuse-ld=lld'
abiFilters 'arm64-v8a'
arguments '-DANDROID_STL=c++_shared', '-DMOBILE_BACKEND=MNN'
arguments '-DANDROID_STL=c++_shared', '-DCMAKE_BUILD_TYPE=Release', '-DMOBILE_BACKEND=MNN'
}
}
ndk {
Expand Down Expand Up @@ -108,7 +108,7 @@ afterEvaluate {

pom {
name = 'fedml-edge-android'
description = 'A FedML edge kit for Android'
description = 'A FedML Edge SDK for Android'
url = 'https://fedml.ai/'
packaging = 'aar'
properties = [
Expand Down
Binary file modified android/fedmlsdk/libs/MNN/arm64-v8a/libMobileNN.so
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ static void loadGpuLibrary(String name) {
try {
System.loadLibrary(name);
} catch (Throwable ce) {
Log.w("NativeFedMLUniTrainer", "load MNNTrain " + name + " GPU so exception=%s", ce);
Log.w("NativeFedMLUniTrainer", "load MNNTrain " + name + " GPU so exception.", ce);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void handleTrainStart(JSONObject msgParams) {
long runId = msgParams.optLong("runId", 0);

JSONObject hyperParameters = null;
final String strServerId = msgParams.optString(TRAIN_ARGS_SERVER_ID);
final String strServerId = msgParams.optString(TRAIN_SERVER_ID);
JSONObject runConfigJson = msgParams.optJSONObject(RUN_CONFIG);
if (runConfigJson != null) {
hyperParameters = runConfigJson.optJSONObject(HYPER_PARAMETERS_CONFIG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ public final class ClientManager implements MessageDefine, OnTrainListener {
private final int mEpochNum;
private final int mTrainSize;
private final int mTestSize;
private String mServerId;
private final OnTrainProgressListener mOnTrainProgressListener;

private interface OnUploadedListener {
void onUploaded();
}

public ClientManager(final long edgeId, final long runId, final String strServerId, JSONObject hyperParameters,
@NonNull final OnTrainProgressListener onTrainProgressListener) {

mEdgeId = edgeId;
mRunId = runId;
mOnTrainProgressListener = onTrainProgressListener;
if (hyperParameters != null) {
JSONObject trainArgs = hyperParameters.optJSONObject(TRAIN_ARGS);
mNumRounds = trainArgs != null ? trainArgs.optInt(COMM_ROUND, 0) : 0;
Expand All @@ -70,7 +75,6 @@ public ClientManager(final long edgeId, final long runId, final String strServer
mTrainSize = 600;
mTestSize = 100;
}
mServerId = strServerId;
LogHelper.d("ClientManager(%d, %d) dataSet=%s, hyperParameters=%s", edgeId, runId, mDataset, hyperParameters);
mTrainer = new TrainingExecutor(onTrainProgressListener);
eventLogger = new ProfilerEventLogger(edgeId, runId);
Expand All @@ -79,15 +83,15 @@ public ClientManager(final long edgeId, final long runId, final String strServer
remoteStorage = RemoteStorage.getInstance();
mRuntimeLogger = new RuntimeLogger(edgeId, runId);
mRuntimeLogger.initial();
registerMessageReceiveHandlers(mServerId);
registerMessageReceiveHandlers(strServerId);
}

public void registerMessageReceiveHandlers(final String serverId) {
final String runTopic = "fedml_" + mRunId + "_" + serverId + "_" + mEdgeId;
edgeCommunicator.subscribe(runTopic, this);
}

private void send_init_online_msg(JSONObject params){
private void send_init_online_msg(JSONObject params) {
LogHelper.d("handle_message_check_status: %s", params.toString());
// report MLOps that edge is OnLine now.
mReporter.reportEdgeOnLine(mRunId, mEdgeId);
Expand Down Expand Up @@ -182,7 +186,9 @@ public void onStateChanged(int id, TransferState state) {
.listener((modelPath, edgeId, clientIdx, trainSamples) -> {
eventLogger.logEventEnd("train", String.valueOf(clientRound));
LogHelper.d("training is complete and start to sendModelToServer()");
sendModelToServer(modelPath, edgeId, clientIdx, trainSamples, clientRound);
sendModelToServer(modelPath, edgeId, clientIdx, trainSamples, clientRound,
() -> mOnTrainProgressListener.onProgressChanged(clientRound, 100.0f)
);
}
).build();
mTrainer.training(params);
Expand Down Expand Up @@ -212,9 +218,10 @@ public void onError(int id, Exception ex) {
* @param clientIdx client index
* @param trainSamples train sample
* @param clientRound client Round
* @param listener OnUploadedListener
*/
public void sendModelToServer(@NonNull final String trainModelPath, final long edgeId, final int clientIdx,
final long trainSamples, final int clientRound) {
final long trainSamples, final int clientRound, @NonNull final OnUploadedListener listener) {
eventLogger.logEventStarted("comm_c2s", String.valueOf(clientRound));
final String uuidS3Key = trainModelPath.substring(trainModelPath.lastIndexOf(File.separator) + 1);
LogHelper.d("sendModelToServer uuidS3Key(%s)", uuidS3Key);
Expand All @@ -226,6 +233,7 @@ public void onStateChanged(int id, TransferState state) {
LogHelper.d("upload onStateChanged(%d, %s)", id, state);
if (state == TransferState.COMPLETED) {
sendModelMessage();
listener.onUploaded();
} else if (TransferState.FAILED == state && reTryCnt > 0) {
remoteStorage.upload(uuidS3Key, new File(trainModelPath), this);
reTryCnt--;
Expand Down Expand Up @@ -256,7 +264,7 @@ private void sendModelMessage() {
});
}

public void stopTrain(){
public void stopTrain() {
mTrainer.stopTrain();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ai.fedml.edge.service;

import android.content.Context;
import android.text.TextUtils;

import ai.fedml.edge.OnTrainProgressListener;
import ai.fedml.edge.OnTrainingStatusListener;
Expand All @@ -23,7 +24,7 @@ public void init(@NonNull Context context, @NonNull final OnTrainingStatusListen
Initializer.getInstance().initial(() -> {
LogHelper.d("Initializer initial finished");
mClientAgent = new ClientAgentManager(onTrainingStatusListener, onTrainProgressListener);
if (!isBound) {
if (!isBound && !TextUtils.isEmpty(mBindEdgeId)) {
mClientAgent.bindCommunicator(mBindEdgeId);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public interface MessageDefine {
String DATASET_TYPE = "dataset";
String TRAIN_ARGS_BATCH_SIZE = "batch_size";
String TRAIN_ARGS_LR = "learning_rate";
String TRAIN_ARGS_SERVER_ID = "server_id";
String TRAIN_SERVER_ID = "server_id";
String TRAIN_ARGS_EPOCH_NUM = "epochs";
String DATA_ARGS_TRAIN_SIZE = "train_size";
String DATA_ARGS_TEST_SIZE = "test_size";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public void reportSystemMetric(final long runId, final long edgeId) {
}

private void notifyClientStatus(final int status) {
LogHelper.d("notifyClientStatus [%d]", status);
mClientStatus = status;
if (mOnTrainingStatusListener != null) {
mOnTrainingStatusListener.onStatusChanged(status);
Expand Down
72 changes: 37 additions & 35 deletions android/fedmlsdk/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ set(MOBILENN_DIR ${CMAKE_SOURCE_DIR}/../../../MobileNN)
set(MNN_DIR ${CMAKE_SOURCE_DIR}/../../../MobileNN/MNN)
set(TORCH_DIR ${CMAKE_SOURCE_DIR}/../../../MobileNN/pytorch)

# MobileNN headers
include_directories(${MOBILENN_DIR}/includes/train)
include_directories(${MOBILENN_DIR}/includes)
# SDK JNI headers
include_directories(${CMAKE_SOURCE_DIR}/includes)
# other denpendencies
find_library(log-lib log)
find_library(jnigraphics-lib jnigraphics)

if(${MOBILE_BACKEND} MATCHES MNN)
message("########################## USING MNN BACKEND ##########################")
add_definitions(-DUSE_MNN_BACKEND)
add_definitions(-DMNN_USE_LOGCAT)
# MNN add-on headers
include_directories(${MOBILENN_DIR}/includes/MNN)
# MNN add-on cpp
file(GLOB_RECURSE MNN_SRCS ${MOBILENN_DIR}/src/MNN/*.cpp)

# MNN source header
include_directories(${MNN_DIR}/include)
Expand Down Expand Up @@ -45,7 +52,21 @@ if(${MOBILE_BACKEND} MATCHES MNN)
PROPERTIES IMPORTED_LOCATION
${LIB_DIR}/${MOBILE_BACKEND}/${ANDROID_ABI}/libMNN_Express.so
)

# MNN add-on cpp
file(GLOB_RECURSE MNN_SRCS
*.cpp
${MOBILENN_DIR}/src/MNN/*.cpp
${MOBILENN_DIR}/src/train/FedMLBaseTrainer.cpp
${MOBILENN_DIR}/src/train/FedMLTrainer.cpp
${MOBILENN_DIR}/src/FedMLClientManager.cpp
${MOBILENN_DIR}/src/train/FedMLMNNTrainer.cpp)

# MobileNN library
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${LIB_DIR}/${MOBILE_BACKEND}/${ANDROID_ABI}")
add_library(MobileNN SHARED ${MNN_SRCS})
list(APPEND MNN_LIBS MNN MNNTrain MNN_Express)
target_link_libraries(MobileNN ${MNN_LIBS} ${log-lib} ${jnigraphics-lib})
elseif(${MOBILE_BACKEND} MATCHES TORCH)
message("########################## USING TORCH BACKEND ##########################")
# set(CMAKE_CXX_STANDARD 14)
Expand All @@ -60,8 +81,6 @@ elseif(${MOBILE_BACKEND} MATCHES TORCH)

# torch add-on headers
include_directories(${MOBILENN_DIR}/includes/torch)
# torch add-on cpp
file(GLOB_RECURSE TORCH_SRCS ${MOBILENN_DIR}/src/torch/*.cpp)
# torch source headers
include_directories(${TORCH_DIR}/build_android/install/include)
include_directories(${TORCH_DIR}/build_android/install/include/torch/csrc/api/include)
Expand Down Expand Up @@ -92,35 +111,18 @@ elseif(${MOBILE_BACKEND} MATCHES TORCH)
PROPERTIES IMPORTED_LOCATION
${LIB_DIR}/${MOBILE_BACKEND}/${ANDROID_ABI}/libtorch.so
)
list(APPEND TORCH_LIBS c10 torch_cpu torch_global_deps torch)
endif()

# MobileNN headers
include_directories(${MOBILENN_DIR}/includes/train)
include_directories(${MOBILENN_DIR}/includes)
# MobileNN source cpp
file(GLOB_RECURSE MOBILENN_SRCS
${MOBILENN_DIR}/src/train/FedMLBaseTrainer.cpp
${MOBILENN_DIR}/src/train/FedMLTrainer.cpp
${MOBILENN_DIR}/src/FedMLClientManager.cpp)
if(${MOBILE_BACKEND} MATCHES MNN)
file(GLOB_RECURSE MOBILENN_SRCS
${MOBILENN_DIR}/src/train/FedMLMNNTrainer.cpp)
elseif(${MOBILE_BACKEND} MATCHES TORCH)
file(GLOB_RECURSE MOBILENN_SRCS
${MOBILENN_DIR}/src/train/FedMLTorchTrainer.cpp)
endif()

# SDK JNI headers
include_directories(${CMAKE_SOURCE_DIR}/includes)
# SDK JNI source cpp
file(GLOB_RECURSE MOBILENN_SRCS ${CMAKE_SOURCE_DIR}/*.cpp)

# MobileNN library
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${LIB_DIR}/${MOBILE_BACKEND}/${ANDROID_ABI}")
add_library(MobileNN SHARED ${MOBILENN_SRCS} ${MNN_SRCS} ${TROCH_SRCS})

# other denpendencies
find_library(log-lib log)
find_library(jnigraphics-lib jnigraphics)
target_link_libraries(MobileNN ${MNN_LIBS} ${TORCH_LIBS} ${log-lib} ${jnigraphics-lib})
# torch add-on cpp
file(GLOB_RECURSE TORCH_SRCS
*.cpp
${MOBILENN_DIR}/src/torch/*.cpp
${MOBILENN_DIR}/src/train/FedMLBaseTrainer.cpp
${MOBILENN_DIR}/src/train/FedMLTrainer.cpp
${MOBILENN_DIR}/src/FedMLClientManager.cpp
${MOBILENN_DIR}/src/train/FedMLTorchTrainer.cpp)
list(APPEND TORCH_LIBS c10 torch_cpu torch_global_deps torch)
# MobileNN library
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${LIB_DIR}/${MOBILE_BACKEND}/${ANDROID_ABI}")
add_library(MobileNN SHARED ${TORCH_SRCS})
target_link_libraries(MobileNN ${TORCH_LIBS} ${log-lib} ${jnigraphics-lib})
endif()
Binary file added doc/en/_static/image/FedML-Android-Arch.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/en/_static/image/android-pkg-uploading.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/en/_static/image/android_ndk_build.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/en/_static/image/launch_android_app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading