add README, license to hello_world

This commit is contained in:
austinvhuang 2024-03-08 17:59:54 -05:00
parent 571a5449c4
commit 8c7b2cf61b
3 changed files with 58 additions and 1 deletions

View File

@ -24,6 +24,7 @@ FetchContent_Declare(sentencepiece GIT_REPOSITORY https://github.com/google/sent
FetchContent_MakeAvailable(sentencepiece) FetchContent_MakeAvailable(sentencepiece)
# Allow for both local and remote building) # Allow for both local and remote building)
option(BUILD_MODE "'local' or 'remote' git fetch for builds") option(BUILD_MODE "'local' or 'remote' git fetch for builds")
if (NOT BUILD_MODE) if (NOT BUILD_MODE)

View File

@ -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.
```

View File

@ -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 <iostream> #include <iostream>
// copybara:import_next_line:gemma_cpp // 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()); tokenize("Write a greeting to the world.", model.Tokenizer());
size_t ntokens = tokens.size(); 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()]( auto stream_token = [&pos, &gen, &ntokens, tokenizer = model.Tokenizer()](
int token, float) { int token, float) {
++pos; ++pos;