Add minimal GPT-2 chat example - #467
Closed
Dobiasd wants to merge 2 commits into
Closed
Conversation
Provides the pieces needed to load keras_hub's gpt2_base_en into a small C++ chat REPL on top of frugally-deep: - keras_export/save_gpt2_backbone_for_fdeep.py: rebuilds the keras_hub GPT2Backbone (or GPT2CausalLM with --with-lm-head) as a plain Keras Functional model using only fdeep-supported primitives, with weights copied over. Verified to match keras_hub within float32 noise on logits. - keras_export/save_gpt2_weights_bin.py: dumps the rebuilt model's weights to a flat binary file consumed by the cached inference engine. - include/fdeep/llm/gpt2_bpe.hpp: byte-level BPE tokenizer (vocab.json + merges.txt). ASCII-focused pre-tokenization; round-trip safe and matches keras_hub on the test set. - include/fdeep/llm/gpt2_generator.hpp: slow but correct generation loop on top of fdeep::model::predict, useful as a reference path. - include/fdeep/llm/gpt2_cached.hpp: stateful Eigen-based GPT-2 forward with a per-layer K/V cache. Loads weights from the binary file; prefill() seeds the cache, step() advances by one token in roughly constant time. - examples/gpt2_chat: simple stdin REPL using the cached engine. Greedy / temperature / top-k sampling. ~18 ms/token sustained on CPU for gpt2_base_en at seq_len 256. The cached engine bypasses fdeep::model::predict because frugally-deep's runtime is stateless and a per-step KV cache cleanly fits a custom forward path. The slow generator path remains available for users who'd rather stay on the generic runtime. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
No functional changes; satisfies the project's formatting CI check. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Owner
Author
|
Closing — landed as an experiment, not pursuing for merge. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stacks on top of #466. Adds the pieces to run a small GPT-2 chat REPL in C++ on top of frugally-deep.
keras_export/save_gpt2_backbone_for_fdeep.py— rebuilds keras_hub'sGPT2Backbone(orGPT2CausalLMwith--with-lm-head) as a plain KerasFunctionalmodel using only fdeep-supported primitives. Weights copied over; output matches keras_hub within float32 noise.keras_export/save_gpt2_weights_bin.py— dumps weights to a flat binary file.include/fdeep/llm/gpt2_bpe.hpp— byte-level BPE tokenizer. ASCII-focused pre-tokenization; round-trip safe; token IDs match keras_hub on the test set.include/fdeep/llm/gpt2_generator.hpp— slow-but-correct generation loop on top offdeep::model::predict(full re-encode each step). Useful as a reference path.include/fdeep/llm/gpt2_cached.hpp— stateful Eigen-based GPT-2 forward with a per-layer K/V cache, loaded from the binary file.prefill()seeds the cache;step()advances by one token in roughly constant time.examples/gpt2_chat/— stdin REPL with greedy / temperature / top-k sampling, opt-in via-DFDEEP_BUILD_GPT2_CHAT=ON.The cached engine bypasses
fdeep::model::predictbecause frugally-deep's runtime is stateless and a per-step KV cache fits a custom forward path more cleanly. The slow generator remains available for anyone who'd rather stay on the generic runtime.Numbers (
gpt2_base_en, single CPU thread)seq_len=64: ~966 ms/token, identical token sequence at temperature 0 — ~55× speedup.Caveats
\p{L}\p{N}regex). Encode/decode is byte-safe; non-Latin scripts simply won't split exactly the way HuggingFace's reference does.Test plan
gpt2_generatorreference path on "Once upon a time, in a small village"🤖 Generated with Claude Code