From 73db33580b90f55042285b99139056c632f159bd Mon Sep 17 00:00:00 2001 From: weedge Date: Thu, 21 Mar 2024 19:50:50 +0800 Subject: [PATCH 1/6] fix: change hello_world cmake compiler use -std=c++17 && argv add const Signed-off-by: weedge --- examples/hello_world/CMakeLists.txt | 3 +++ run.cc | 2 +- util/app.h | 6 +++--- util/args.h | 10 +++++----- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/examples/hello_world/CMakeLists.txt b/examples/hello_world/CMakeLists.txt index 292c80c..88b1787 100644 --- a/examples/hello_world/CMakeLists.txt +++ b/examples/hello_world/CMakeLists.txt @@ -14,7 +14,10 @@ cmake_minimum_required(VERSION 3.11) project(hello_world) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +#set(CMAKE_CXX_FLAGS "-Wall -Wextra -fPIC -Wno-unused-parameter") include(FetchContent) FetchContent_Declare(highway GIT_REPOSITORY https://github.com/google/highway.git GIT_TAG da250571a45826b21eebbddc1e50d0c1137dee5f) diff --git a/run.cc b/run.cc index b08e4ca..4a3c9fc 100644 --- a/run.cc +++ b/run.cc @@ -273,7 +273,7 @@ void Run(LoaderArgs& loader, InferenceArgs& inference, AppArgs& app) { } // namespace gcpp -int main(int argc, char** argv) { +int main(int argc, const char** argv) { { PROFILER_ZONE("Startup.misc"); diff --git a/util/app.h b/util/app.h index cd6cb6c..f3bb208 100644 --- a/util/app.h +++ b/util/app.h @@ -95,7 +95,7 @@ class AppArgs : public ArgsBase { } public: - AppArgs(int argc, char* argv[]) { + AppArgs(int argc, const char* argv[]) { InitAndParse(argc, argv); ChooseNumThreads(); } @@ -128,7 +128,7 @@ class AppArgs : public ArgsBase { }; struct LoaderArgs : public ArgsBase { - LoaderArgs(int argc, char* argv[]) { InitAndParse(argc, argv); } + LoaderArgs(int argc, const char* argv[]) { InitAndParse(argc, argv); } static std::string ToLower(const std::string& text) { std::string result = text; @@ -205,7 +205,7 @@ struct LoaderArgs : public ArgsBase { }; struct InferenceArgs : public ArgsBase { - InferenceArgs(int argc, char* argv[]) { InitAndParse(argc, argv); } + InferenceArgs(int argc, const char* argv[]) { InitAndParse(argc, argv); } size_t max_tokens; size_t max_generated_tokens; diff --git a/util/args.h b/util/args.h index b9ab985..7a606ff 100644 --- a/util/args.h +++ b/util/args.h @@ -106,7 +106,7 @@ class ArgsBase { // consider adding a hash-map to speed this up. class ParseVisitor { public: - ParseVisitor(int argc, char* argv[]) : argc_(argc), argv_(argv) {} + ParseVisitor(int argc, const char* argv[]) : argc_(argc), argv_(argv) {} template void operator()(T& t, const char* name, const T& /*init*/, @@ -167,7 +167,7 @@ class ArgsBase { } int argc_; - char** argv_; + const char** argv_; }; // ParseVisitor template @@ -192,19 +192,19 @@ class ArgsBase { ForEach(visitor); } - void Parse(int argc, char* argv[]) { + void Parse(int argc, const char* argv[]) { ParseVisitor visitor(argc, argv); ForEach(visitor); } // For convenience, enables single-line constructor. - void InitAndParse(int argc, char* argv[]) { + void InitAndParse(int argc, const char* argv[]) { Init(); Parse(argc, argv); } }; -static inline HWY_MAYBE_UNUSED bool HasHelp(int argc, char* argv[]) { +static inline HWY_MAYBE_UNUSED bool HasHelp(int argc, const char* argv[]) { // TODO(austinvhuang): handle case insensitivity if (argc == 1) { // no arguments - print help From 7165806216cf1a75d427b171fb733679c4dd6232 Mon Sep 17 00:00:00 2001 From: weedge Date: Thu, 21 Mar 2024 19:56:53 +0800 Subject: [PATCH 2/6] change example helloworld gemma.cpp git tag Signed-off-by: weedge --- examples/hello_world/CMakeLists.txt | 2 +- examples/hello_world/run.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/hello_world/CMakeLists.txt b/examples/hello_world/CMakeLists.txt index 88b1787..59a2995 100644 --- a/examples/hello_world/CMakeLists.txt +++ b/examples/hello_world/CMakeLists.txt @@ -36,7 +36,7 @@ if (BUILD_MODE STREQUAL "local") # Relative path to gemma.cpp from examples/hello_world/build/ FetchContent_Declare(gemma SOURCE_DIR ../../..) else() - FetchContent_Declare(gemma GIT_REPOSITORY https://github.com/google/gemma.cpp.git GIT_TAG a9aa63fd2ea6b786ed0706d619588bfe2d43370e) + FetchContent_Declare(gemma GIT_REPOSITORY https://github.com/google/gemma.cpp.git GIT_TAG 73db33580b90f55042285b99139056c632f159bd) endif() FetchContent_MakeAvailable(gemma) diff --git a/examples/hello_world/run.cc b/examples/hello_world/run.cc index a994f31..628fb5e 100644 --- a/examples/hello_world/run.cc +++ b/examples/hello_world/run.cc @@ -37,7 +37,7 @@ std::vector tokenize( return tokens; } -int main(int argc, char** argv) { +int main(int argc, const char** argv) { gcpp::LoaderArgs loader(argc, argv); // Rough heuristic for the number of threads to use From 9b7804c75bea68a0f3a69ca5d45a308539b32f5b Mon Sep 17 00:00:00 2001 From: weedge Date: Thu, 21 Mar 2024 21:22:25 +0800 Subject: [PATCH 3/6] change main use char** argv Signed-off-by: weedge --- examples/hello_world/run.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/hello_world/run.cc b/examples/hello_world/run.cc index 628fb5e..ceebaa6 100644 --- a/examples/hello_world/run.cc +++ b/examples/hello_world/run.cc @@ -22,7 +22,7 @@ #include "util/args.h" // copybara:end // copybara:import_next_line:gemma_cpp -#include "util/app.h" // LoaderArgs +#include "util/app.h" // LoaderArgs // copybara:end #include "hwy/contrib/thread_pool/thread_pool.h" @@ -37,8 +37,8 @@ std::vector tokenize( return tokens; } -int main(int argc, const char** argv) { - gcpp::LoaderArgs loader(argc, argv); +int main(int argc, char** argv) { + gcpp::LoaderArgs loader(argc, (const char**)argv); // Rough heuristic for the number of threads to use size_t num_threads = static_cast(std::clamp( From 9c66eca9631207a3252787a32d34e57386283ac1 Mon Sep 17 00:00:00 2001 From: weedge Date: Thu, 21 Mar 2024 21:26:20 +0800 Subject: [PATCH 4/6] change main use char** argv Signed-off-by: weedge --- run.cc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/run.cc b/run.cc index 4a3c9fc..7487918 100644 --- a/run.cc +++ b/run.cc @@ -66,7 +66,8 @@ void ShowConfig(LoaderArgs& loader, InferenceArgs& inference, AppArgs& app) { << std::thread::hardware_concurrency() << std::endl << "Instruction set : " << hwy::TargetName(hwy::DispatchedTarget()) << " (" - << hwy::VectorBytes() * 8 << " bits)" << "\n" + << hwy::VectorBytes() * 8 << " bits)" + << "\n" << "Compiled config : " << CompiledConfig() << "\n" << "Weight Type : " << gcpp::TypeName(gcpp::WeightT()) << "\n" @@ -273,15 +274,15 @@ void Run(LoaderArgs& loader, InferenceArgs& inference, AppArgs& app) { } // namespace gcpp -int main(int argc, const char** argv) { +int main(int argc, char** argv) { { PROFILER_ZONE("Startup.misc"); - gcpp::LoaderArgs loader(argc, argv); - gcpp::InferenceArgs inference(argc, argv); - gcpp::AppArgs app(argc, argv); + gcpp::LoaderArgs loader(argc, (const char**)argv); + gcpp::InferenceArgs inference(argc, (const char**)argv); + gcpp::AppArgs app(argc, (const char**)argv); - if (gcpp::HasHelp(argc, argv)) { + if (gcpp::HasHelp(argc, (const char**)argv)) { ShowHelp(loader, inference, app); return 0; } From 2bdcf6f4268962c0da75e54c411b8612db9cb043 Mon Sep 17 00:00:00 2001 From: weedge Date: Fri, 22 Mar 2024 10:36:56 +0800 Subject: [PATCH 5/6] remove const argv Signed-off-by: weedge --- examples/hello_world/run.cc | 2 +- run.cc | 8 ++++---- util/app.h | 6 +++--- util/args.h | 10 +++++----- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/examples/hello_world/run.cc b/examples/hello_world/run.cc index ceebaa6..23f4fb2 100644 --- a/examples/hello_world/run.cc +++ b/examples/hello_world/run.cc @@ -38,7 +38,7 @@ std::vector tokenize( } int main(int argc, char** argv) { - gcpp::LoaderArgs loader(argc, (const char**)argv); + gcpp::LoaderArgs loader(argc, argv); // Rough heuristic for the number of threads to use size_t num_threads = static_cast(std::clamp( diff --git a/run.cc b/run.cc index 7487918..22ebe17 100644 --- a/run.cc +++ b/run.cc @@ -278,11 +278,11 @@ int main(int argc, char** argv) { { PROFILER_ZONE("Startup.misc"); - gcpp::LoaderArgs loader(argc, (const char**)argv); - gcpp::InferenceArgs inference(argc, (const char**)argv); - gcpp::AppArgs app(argc, (const char**)argv); + gcpp::LoaderArgs loader(argc, argv); + gcpp::InferenceArgs inference(argc, argv); + gcpp::AppArgs app(argc, argv); - if (gcpp::HasHelp(argc, (const char**)argv)) { + if (gcpp::HasHelp(argc, argv)) { ShowHelp(loader, inference, app); return 0; } diff --git a/util/app.h b/util/app.h index f3bb208..cd6cb6c 100644 --- a/util/app.h +++ b/util/app.h @@ -95,7 +95,7 @@ class AppArgs : public ArgsBase { } public: - AppArgs(int argc, const char* argv[]) { + AppArgs(int argc, char* argv[]) { InitAndParse(argc, argv); ChooseNumThreads(); } @@ -128,7 +128,7 @@ class AppArgs : public ArgsBase { }; struct LoaderArgs : public ArgsBase { - LoaderArgs(int argc, const char* argv[]) { InitAndParse(argc, argv); } + LoaderArgs(int argc, char* argv[]) { InitAndParse(argc, argv); } static std::string ToLower(const std::string& text) { std::string result = text; @@ -205,7 +205,7 @@ struct LoaderArgs : public ArgsBase { }; struct InferenceArgs : public ArgsBase { - InferenceArgs(int argc, const char* argv[]) { InitAndParse(argc, argv); } + InferenceArgs(int argc, char* argv[]) { InitAndParse(argc, argv); } size_t max_tokens; size_t max_generated_tokens; diff --git a/util/args.h b/util/args.h index 7a606ff..b9ab985 100644 --- a/util/args.h +++ b/util/args.h @@ -106,7 +106,7 @@ class ArgsBase { // consider adding a hash-map to speed this up. class ParseVisitor { public: - ParseVisitor(int argc, const char* argv[]) : argc_(argc), argv_(argv) {} + ParseVisitor(int argc, char* argv[]) : argc_(argc), argv_(argv) {} template void operator()(T& t, const char* name, const T& /*init*/, @@ -167,7 +167,7 @@ class ArgsBase { } int argc_; - const char** argv_; + char** argv_; }; // ParseVisitor template @@ -192,19 +192,19 @@ class ArgsBase { ForEach(visitor); } - void Parse(int argc, const char* argv[]) { + void Parse(int argc, char* argv[]) { ParseVisitor visitor(argc, argv); ForEach(visitor); } // For convenience, enables single-line constructor. - void InitAndParse(int argc, const char* argv[]) { + void InitAndParse(int argc, char* argv[]) { Init(); Parse(argc, argv); } }; -static inline HWY_MAYBE_UNUSED bool HasHelp(int argc, const char* argv[]) { +static inline HWY_MAYBE_UNUSED bool HasHelp(int argc, char* argv[]) { // TODO(austinvhuang): handle case insensitivity if (argc == 1) { // no arguments - print help From b9829ccc3c1a0b79cc236d6f3b9453b28f016923 Mon Sep 17 00:00:00 2001 From: weedge Date: Fri, 22 Mar 2024 10:40:12 +0800 Subject: [PATCH 6/6] revert hello_world gemma git tag Signed-off-by: weedge --- examples/hello_world/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hello_world/CMakeLists.txt b/examples/hello_world/CMakeLists.txt index 59a2995..88b1787 100644 --- a/examples/hello_world/CMakeLists.txt +++ b/examples/hello_world/CMakeLists.txt @@ -36,7 +36,7 @@ if (BUILD_MODE STREQUAL "local") # Relative path to gemma.cpp from examples/hello_world/build/ FetchContent_Declare(gemma SOURCE_DIR ../../..) else() - FetchContent_Declare(gemma GIT_REPOSITORY https://github.com/google/gemma.cpp.git GIT_TAG 73db33580b90f55042285b99139056c632f159bd) + FetchContent_Declare(gemma GIT_REPOSITORY https://github.com/google/gemma.cpp.git GIT_TAG a9aa63fd2ea6b786ed0706d619588bfe2d43370e) endif() FetchContent_MakeAvailable(gemma)