From c69af34d976c5a47100e4f935081c1ce8a67494d Mon Sep 17 00:00:00 2001 From: "The gemma.cpp Authors" Date: Fri, 28 Aug 2026 09:00:58 -0700 Subject: [PATCH] Internal Changes PiperOrigin-RevId: 972619142 --- deepseek/dsv4_tokenizer.cc | 16 ++++++++++++++-- deepseek/dsv4_tokenizer.h | 9 +++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/deepseek/dsv4_tokenizer.cc b/deepseek/dsv4_tokenizer.cc index 15716e44..e339d809 100644 --- a/deepseek/dsv4_tokenizer.cc +++ b/deepseek/dsv4_tokenizer.cc @@ -22,8 +22,10 @@ #include #include #include +#include #include + #include "hwy/base.h" // HWY_ABORT #include "nlohmann/json.hpp" @@ -172,12 +174,22 @@ std::string ReadFileToStringOrAbort(const std::string& path) { Dsv4Tokenizer::Dsv4Tokenizer(const std::string& tokenizer_json_path) { const std::string contents = ReadFileToStringOrAbort(tokenizer_json_path); - json j = json::parse(contents, /*cb=*/nullptr, /*allow_exceptions=*/false); + Init(contents); +} + +Dsv4Tokenizer::Dsv4Tokenizer(std::string_view json_content, bool /*is_content*/) { + Init(json_content); +} + +void Dsv4Tokenizer::Init(std::string_view json_content) { + json j = json::parse(json_content.begin(), json_content.end(), /*cb=*/nullptr, + /*allow_exceptions=*/false); if (j.is_discarded()) { - HWY_ABORT("Failed to parse tokenizer JSON %s", tokenizer_json_path.c_str()); + HWY_ABORT("Failed to parse tokenizer JSON"); } uint32_t byte_to_cp[256]; + BuildByteToCp(byte_to_cp); std::unordered_map cp_to_byte; for (int b = 0; b < 256; ++b) { diff --git a/deepseek/dsv4_tokenizer.h b/deepseek/dsv4_tokenizer.h index 15bc9e8a..705e7b5e 100644 --- a/deepseek/dsv4_tokenizer.h +++ b/deepseek/dsv4_tokenizer.h @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -35,6 +36,11 @@ class Dsv4Tokenizer { // Aborts on I/O or parse errors. explicit Dsv4Tokenizer(const std::string& tokenizer_json_path); + // Initializes tokenizer directly from JSON content in memory. + // 'is_content' is used to distinguish from path constructor. + Dsv4Tokenizer(std::string_view json_content, bool is_content); + + // Extracts added tokens (chat markers etc.), pre-tokenizes and BPE-encodes // everything in between. Equivalent to HF encode(add_special_tokens=false). std::vector Encode(const std::string& text) const; @@ -57,6 +63,9 @@ class Dsv4Tokenizer { int id; }; + void Init(std::string_view json_content); + + // Splits `text` (a span with no added tokens) into pre-tokenization pieces // and BPE-encodes each, appending ids. void EncodeSegment(const char* bytes, size_t len,