From 8c7b2cf61b9794b806de091685dc6739dd3db837 Mon Sep 17 00:00:00 2001 From: austinvhuang Date: Fri, 8 Mar 2024 17:59:54 -0500 Subject: [PATCH] add README, license to hello_world --- examples/hello_world/CMakeLists.txt | 1 + examples/hello_world/README.md | 41 +++++++++++++++++++++++++++++ examples/hello_world/run.cc | 17 +++++++++++- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 examples/hello_world/README.md diff --git a/examples/hello_world/CMakeLists.txt b/examples/hello_world/CMakeLists.txt index eb574aa..b49d02e 100644 --- a/examples/hello_world/CMakeLists.txt +++ b/examples/hello_world/CMakeLists.txt @@ -24,6 +24,7 @@ FetchContent_Declare(sentencepiece GIT_REPOSITORY https://github.com/google/sent FetchContent_MakeAvailable(sentencepiece) + # Allow for both local and remote building) option(BUILD_MODE "'local' or 'remote' git fetch for builds") if (NOT BUILD_MODE) diff --git a/examples/hello_world/README.md b/examples/hello_world/README.md new file mode 100644 index 0000000..fc117fe --- /dev/null +++ b/examples/hello_world/README.md @@ -0,0 +1,41 @@ +# Hello World Example + +This is a minimal/template project for using `gemma.cpp` as a library. Instead of an interactive interface, it sets up the model state and generates text for a single hard coded prompt. + +Build steps are similar to the main `gemma` executable. From inside the top-level directory. For now only `cmake`/`make` is available for builds (PRs welcome for other build options). + +First use `cmake` to configure the project, assuming you are in the `hello_world` example directory (`gemma.cpp/examples/hello_world`): + +```sh +cmake -B build +``` + +This sets up a build configuration in `gemma.cpp/examples/hello_world/build`. Note that this fetches `libgemma` from a git commit hash on github. Alternatively if you want to build using the local version of `gemma.cpp` use: + + +```sh +cmake -B build -DBUILD_MODE=local +``` + +Make sure you delete the contents of the build directory before changing configurations. + +Then use `make` to build the project: + +```sh +cd build +make hello_world +``` + +As with the top-level `gemma.cpp` project you can use the `make` commands `-j` flag to use parallel threads for faster builds. + +From inside the `gemma.cpp/examples/hello_world/build` directory, there should be a `hello_world` executable. You can run it with the same 3 model arguments as gemma.cpp specifying the tokenizer, compressed weights file, and model type, for example: + +```sh +./hello_world --tokenizer tokenizer.spm --compressed_weights 2b-it-sfp.sbs --model 2b-it +``` + +Should print a greeting to the terminal: + +``` +"Hello, world! It's a pleasure to greet you all. May your day be filled with joy, peace, and all the things that make your heart soar. +``` diff --git a/examples/hello_world/run.cc b/examples/hello_world/run.cc index 7b57403..fc74130 100644 --- a/examples/hello_world/run.cc +++ b/examples/hello_world/run.cc @@ -1,3 +1,18 @@ +// Copyright 2024 Google LLC +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include // copybara:import_next_line:gemma_cpp @@ -43,7 +58,7 @@ int main(int argc, char** argv) { tokenize("Write a greeting to the world.", model.Tokenizer()); size_t ntokens = tokens.size(); - // Callback + // This callback function gets invoked everytime a token is generated auto stream_token = [&pos, &gen, &ntokens, tokenizer = model.Tokenizer()]( int token, float) { ++pos;