From 4b7fae551fb8e67fbdab7519e1fdcb6ad6a2ed2e Mon Sep 17 00:00:00 2001 From: Eunju Yang Date: Fri, 27 Dec 2024 18:01:22 +0900 Subject: [PATCH] [ App ] Update setOptimizer code to check its status - Update example applications to check their setOptimizer status. Signed-off-by: Eunju Yang --- Applications/LLaMA/jni/main.cpp | 7 +++++-- Applications/MixedPrecision/jni/main.cpp | 7 +++++-- Applications/Multi_input/jni/main.cpp | 7 +++++-- Applications/PicoGPT/jni/main.cpp | 5 ++++- Applications/Resnet/jni/main.cpp | 7 +++++-- Applications/SimpleFC/jni/main.cpp | 7 +++++-- Applications/YOLOv2/jni/main.cpp | 15 ++++++++++++--- Applications/YOLOv3/jni/main.cpp | 5 ++++- 8 files changed, 45 insertions(+), 15 deletions(-) diff --git a/Applications/LLaMA/jni/main.cpp b/Applications/LLaMA/jni/main.cpp index 32018d4098..2b0ada9ff0 100644 --- a/Applications/LLaMA/jni/main.cpp +++ b/Applications/LLaMA/jni/main.cpp @@ -668,9 +668,12 @@ void createAndRun(unsigned int epochs, unsigned int batch_size) { withKey("save_path", "test_model.bin")}); auto optimizer = ml::train::createOptimizer("sgd", {"learning_rate=0.001"}); - g_model->setOptimizer(std::move(optimizer)); + int status = g_model->setOptimizer(std::move(optimizer)); + if (status) { + throw std::invalid_argument("failed to set optimizer!"); + } - int status = g_model->compile(); + status = g_model->compile(); if (status) { throw std::invalid_argument("model compilation failed!"); } diff --git a/Applications/MixedPrecision/jni/main.cpp b/Applications/MixedPrecision/jni/main.cpp index a5a8b2f850..ece6160f95 100644 --- a/Applications/MixedPrecision/jni/main.cpp +++ b/Applications/MixedPrecision/jni/main.cpp @@ -134,9 +134,12 @@ void createAndRun(unsigned int epochs, unsigned int batch_size, #endif auto optimizer = ml::train::createOptimizer("adam", {"learning_rate=0.001"}); - model->setOptimizer(std::move(optimizer)); + int status = model->setOptimizer(std::move(optimizer)); + if (status) { + throw std::invalid_argument("failed to set optimizer!"); + } - int status = model->compile(); + status = model->compile(); if (status) { throw std::invalid_argument("model compilation failed!"); } diff --git a/Applications/Multi_input/jni/main.cpp b/Applications/Multi_input/jni/main.cpp index 23e556f890..a3029b8ddd 100644 --- a/Applications/Multi_input/jni/main.cpp +++ b/Applications/Multi_input/jni/main.cpp @@ -101,9 +101,12 @@ void createAndRun(unsigned int epochs, unsigned int batch_size, withKey("save_path", "resnet_full.bin")}); auto optimizer = ml::train::createOptimizer("adam", {"learning_rate=0.001"}); - model->setOptimizer(std::move(optimizer)); + int status = model->setOptimizer(std::move(optimizer)); + if (status) { + throw std::invalid_argument("failed to set optimizer!"); + }; - int status = model->compile(); + status = model->compile(); if (status) { throw std::invalid_argument("model compilation failed!"); } diff --git a/Applications/PicoGPT/jni/main.cpp b/Applications/PicoGPT/jni/main.cpp index 30eace3946..7701eef789 100644 --- a/Applications/PicoGPT/jni/main.cpp +++ b/Applications/PicoGPT/jni/main.cpp @@ -267,8 +267,11 @@ std::shared_ptr genModel() { {"name=layer_normalization", "axis=3", "epsilon=1e-5"}); model->addLayer(layer_normalization); - model->setOptimizer( + int status = model->setOptimizer( ml::train::createOptimizer("sgd", {"learning_rate = 0.1"})); + if (status) { + throw std::invalid_argument("failed to set optimizer!"); + } model->setProperty({"input_layers=wte_input, wpe_input"}); return model; diff --git a/Applications/Resnet/jni/main.cpp b/Applications/Resnet/jni/main.cpp index b782d709ac..5807f56bd7 100644 --- a/Applications/Resnet/jni/main.cpp +++ b/Applications/Resnet/jni/main.cpp @@ -272,9 +272,12 @@ void createAndRun(unsigned int epochs, unsigned int batch_size, withKey("save_path", "resnet_full.bin")}); auto optimizer = ml::train::createOptimizer("adam", {"learning_rate=0.001"}); - model->setOptimizer(std::move(optimizer)); + int status = model->setOptimizer(std::move(optimizer)); + if (status) { + throw std::invalid_argument("failed to set optimizer!"); + } - int status = model->compile(); + status = model->compile(); if (status) { throw std::invalid_argument("model compilation failed!"); } diff --git a/Applications/SimpleFC/jni/main.cpp b/Applications/SimpleFC/jni/main.cpp index 6ae0b4e4cb..dbd27795fa 100644 --- a/Applications/SimpleFC/jni/main.cpp +++ b/Applications/SimpleFC/jni/main.cpp @@ -140,9 +140,12 @@ void createAndRun(unsigned int epochs, unsigned int batch_size, withKey("model_tensor_type", "FP16-FP16")}); auto optimizer = ml::train::createOptimizer("sgd", {"learning_rate=0.001"}); - model->setOptimizer(std::move(optimizer)); + int status = model->setOptimizer(std::move(optimizer)); + if (status) { + throw std::invalid_argument("failed to set optimizer!"); + } - int status = model->compile(ml::train::ExecutionMode::INFERENCE); + status = model->compile(ml::train::ExecutionMode::INFERENCE); if (status) { throw std::invalid_argument("model compilation failed!"); } diff --git a/Applications/YOLOv2/jni/main.cpp b/Applications/YOLOv2/jni/main.cpp index 4aeefab16d..6101f882ed 100644 --- a/Applications/YOLOv2/jni/main.cpp +++ b/Applications/YOLOv2/jni/main.cpp @@ -305,11 +305,20 @@ int main(int argc, char *argv[]) { // create optimizer auto optimizer = ml::train::createOptimizer( "adam", {"learning_rate=0.001", "epsilon=1e-8", "torch_ref=true"}); - model->setOptimizer(std::move(optimizer)); + int status = model->setOptimizer(std::move(optimizer)); + if (status) { + throw std::invalid_argument("failed to set optimizer"); + } // compile and initialize model - model->compile(); - model->initialize(); + status = model->compile(); + if (status) { + throw std::invalid_argument("model compilation failed!"); + } + status = model->initialize(); + if (status) { + throw std::invalid_argument("model initialization failed!"); + } model->save("./yolov2.ini", ml::train::ModelFormat::MODEL_FORMAT_INI); // model->load(MODEL_INIT_BIN_PATH); diff --git a/Applications/YOLOv3/jni/main.cpp b/Applications/YOLOv3/jni/main.cpp index 783c59d226..a86ec64b3a 100644 --- a/Applications/YOLOv3/jni/main.cpp +++ b/Applications/YOLOv3/jni/main.cpp @@ -430,7 +430,10 @@ int main(int argc, char *argv[]) { // create optimizer auto optimizer = ml::train::createOptimizer( "adam", {"learning_rate=0.000001", "epsilon=1e-8", "torch_ref=true"}); - model->setOptimizer(std::move(optimizer)); + int status = model->setOptimizer(std::move(optimizer)); + if (status) { + throw std::invalid_argument("failed to set optimizer"); + } // compile and initialize model model->compile();