Skip to content

libgemma API refactor - decouple from interactive repl demo specifics, add hello world example using libgemma #82

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

Merged
merged 22 commits into from
Mar 12, 2024

Conversation

austinvhuang
Copy link
Collaborator

@austinvhuang austinvhuang commented Mar 6, 2024

This PR refactors the library interfaces to facilitate multiple frontends / apps to make better use of libgemma. It removes coupling between the specific interactive interface of run.cc, it's set of CLI arguments and the core library API in gemma.h and gemma.cc. As a test of this library use case, we add a minimal usage example in examples/hello_world/ with minimal setup to interact with the library interfaces.

  • Decouple Gemma, GemmaImpl, GemmaInterface, Generate, from CLI arguments
  • Add a simple "hello world" library example
  • Additional convenience functions / types to reduce verbosity
  • This branch also includes PR Separate KV cache from GemmaImpl #81 from @ufownl decoupling KV cache from GemmaImpl.

Minor:

  • Add examples/ directory and README for examples/hello_world
  • Fix LoaderArgs validaiton order
  • Minor computation quality tweaks
  • Fix up indentation, line breaks in --help message

See also:

@austinvhuang austinvhuang changed the title [DRAFT WIP] - do not review / merge - refactor for library usage + standalone hello world library example libgemma API refactor - decouple from interactive repl demo specifics, add hello world example using libgemma Mar 8, 2024
@austinvhuang austinvhuang marked this pull request as ready for review March 8, 2024 23:11
Copy link
Member

@jan-wassenberg jan-wassenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// - Bundle runtime parameters as RuntimeConfig
// - No threadpools within threadpools (inner_pool = dummy)
// - All tokens accepted
void GenerateGemma(Gemma& gemma, RuntimeConfig runtime_config,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simplified interface seems useful. Maybe we should also move stream_token and gen inside RuntimeConfig? (Which we should pass by ref = RuntimeConfig&)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of this too, but decided for now to make stateful things more explicit for now. For example, say a user who's new to the code wants to make the randomness reproducible - it's implied what to do from this interface - seed the RNG that gets passed in.

Packing it inside RuntimeConfig adds a level of indirection + opacity - first they have to know or guess that there's an rng that is getting passed in (and not say, initialized somewhere internally), then know to check the internals of RuntimeConfig to find it, then find the place in application codebase where RuntimeConfig is constructed, and finally make the change.

Granted for us w'ere able to track this without too much cognitive overhead, but I want to try to keep things flatly legible as much as we can until other tradeoffs make it obviously needed+beneficial.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable and fine for now :)

@austinvhuang austinvhuang added the copybara-import Trigger Copybara for merging pull requests label Mar 10, 2024
@austinvhuang austinvhuang added copybara-import Trigger Copybara for merging pull requests and removed copybara-import Trigger Copybara for merging pull requests labels Mar 10, 2024
Copy link
Member

@jan-wassenberg jan-wassenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@copybara-service copybara-service bot merged commit ccd055e into dev Mar 12, 2024
@austinvhuang austinvhuang deleted the examples branch March 13, 2024 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
copybara-import Trigger Copybara for merging pull requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants