Support parallel encoding#1261
Closed
Senthil455 wants to merge 2 commits into
Closed
Conversation
- Add EncodeParallel() methods to SentencePieceProcessor for multi-threaded encoding - Implement text splitting at UTF-8 boundaries to ensure correctness - Normalize entire input first, then encode chunks in parallel - Maintain backward compatibility with existing API - Add convenient wrapper methods like EncodeAsPiecesParallel() - Include fallback to single-threaded encoding for short texts
Collaborator
|
ParallelEncode has already been implemented. See the head. There are some comments on the parallel encoding.
|
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.
Support parallel encoding to handle super-long text. When input exceeds 10KB, the text is split into chunks and encoded concurrently using
std::async, then the results are stitched back together.Changes
src/sentencepiece_processor.ccEncodeParallel(input, pieces, num_threads)— new overload returningvector<string>EncodeParallel(input, ids, num_threads)— new overload returningvector<int>EncodeParallel(input, spt, num_threads)— new overload returningSentencePieceText(core implementation)Encode()for inputs <10KB or whennum_threads=1SplitInputIntoChunksstd::asyncand combines resultsPopulateSentencePieceTextto build the final output with correct byte offsetsSplitInputIntoChunks(input, num_chunks)— new helper that splits text at UTF-8 character boundaries (avoids splitting multi-byte sequences)#include <future>and#include <thread>src/sentencepiece_processor.hEncodeParallelvirtual methods (3 overloads) to the public APIEncodeAsPiecesParallel(input, num_threads)EncodeAsIdsParallel(input, num_threads)EncodeAsImmutableProtoParallel(input, num_threads)SplitInputIntoChunksas a private helpersrc/sentencepiece_processor_test.ccAdded 5 test cases:
EncodeParallelShortInputTestEncodeParallelSingleThreadTestnum_threads=1matches serialEncode()EncodeParallelInvalidNumThreadsTestnum_threads=0returns errorEncodeParallelConsistentWithSerialTestEncodeParallelMultiByteUtf8TestCharByCharModeltest stub that tokenizes character-by-character without input verification, enabling parallel-mode testing.Implementation Details
string_util::IsTrailByte)std::async(std::launch::async, ...)for thread-safe parallel executionPopulateSentencePieceTextmaps byte offsets back to original (unnormalized) inputFixes Support parallel encoding #1229