Skip to content

Commit c54996a

Browse files
larryliu0820meta-codesync[bot]
authored andcommitted
Add missing HF functionalities (#170) (#17569)
Summary: Pull Request resolved: #17569 * [FEAT] Add prepend normalizer This commit introduces prepend normalizer, similiar to hugging faces's rust implementation. * [FEAT] Add "skip_special_tokens" parameter to the decode function Added decode function parameter, to optionaly skip decoding special tokens. Similiary to the HF Rust implementaiton. This change should be agnostic unless set to true. * [FEAT] Add funciton "piece_to_id" This commit introduces public member function that converts string to token id. This function is reverse of already existing 'id_to_piece' * [FEAT] Add handling of null pretokenizer and bytefallback json fields Added: - Handling of pretokenizer field explicitly set to null - Handling of bytefallback field along with encode logic * [FIX] Changed decode API to work on vectors instead of singular tokens * [REFACTOR] Changed tests to reflect new decode API * [FIX] Change decoders to work on vectors * [FEAT] Make postprocessing a separte step Postprocessing is now separate, configurable step similiar to normalization, pretokenization or decoding. * Revert "[FIX] Changed decode API to work on vectors instead of singular tokens" This reverts commit 08e1b399e4fafcecc78c1941b6331782f7d65469. * [REFACTOR] Split loading function of HFTokenizer * Revert tests as there's no longer vectorized decode api * [FIX] Fix handling of unknown tokens in bpm * [FIX] Added FuseDecoder implementation * Fix python bindings * [FIX] post_processor, remove silent fails This commit, removes BertProcessor and RobertaProcessor skeleton classes. * chore: Add test cases This commit adds test cases for: - PieceToId logic - skip_special_tokens logic - PrependNormalizer * chore: add python binding for batch decode * chore: add post_processor to BUCK file * chore: fix formatting in token_decoder.h, remove placeholder code in post_processor.h * chore: change copyright handle to SWM * feat: add tests requested in review * chore: Unify logs in piece_to_id definitions * chore: fix tests to ensure parity with rust implementaiton outputs * chore: add python test for batch_decode Differential Revision: D93019471
1 parent a398a96 commit c54996a

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

extension/llm/runner/test/test_text_llm_runner.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,28 @@ namespace {
3131
// Mock classes for dependencies
3232
class MockTokenizer : public ::tokenizers::Tokenizer {
3333
public:
34-
MOCK_METHOD(::tokenizers::Error, load, (const std::string&), ());
35-
MOCK_METHOD(bool, is_loaded, (), (const));
34+
MOCK_METHOD(::tokenizers::Error, load, (const std::string&), (override));
35+
MOCK_METHOD(bool, is_loaded, (), (const, override));
3636
MOCK_METHOD(
3737
::tokenizers::Result<std::vector<uint64_t>>,
3838
encode,
3939
(const std::string&, int8_t, int8_t),
40-
(const));
40+
(const, override));
4141
MOCK_METHOD(
4242
::tokenizers::Result<std::string>,
4343
decode,
44-
(uint64_t, uint64_t),
45-
(const));
44+
(uint64_t, uint64_t, bool),
45+
(const, override));
4646
MOCK_METHOD(
4747
::tokenizers::Result<std::string>,
4848
id_to_piece,
4949
(uint64_t),
50-
(const));
51-
MOCK_METHOD(uint64_t, bos_tok, (), (const));
52-
MOCK_METHOD(uint64_t, eos_tok, (), (const));
53-
MOCK_METHOD(uint64_t, vocab_size, (), (const));
50+
(const, override));
51+
MOCK_METHOD(
52+
::tokenizers::Result<uint64_t>,
53+
piece_to_id,
54+
(const std::string&),
55+
(const, override));
5456
};
5557

5658
class MockModule : public ::executorch::extension::Module {
@@ -128,17 +130,17 @@ class RunnerTest : public Test {
128130
std::vector<uint64_t>{1, 2, 3});
129131
});
130132

131-
ON_CALL(*tokenizer, decode).WillByDefault([](uint64_t, uint64_t) {
133+
ON_CALL(*tokenizer, decode).WillByDefault([](uint64_t, uint64_t, bool) {
132134
return ::tokenizers::Result<std::string>("token");
133135
});
134136

135137
ON_CALL(*tokenizer, id_to_piece).WillByDefault([](uint64_t) {
136138
return ::tokenizers::Result<std::string>("piece");
137139
});
138140

139-
ON_CALL(*tokenizer, bos_tok()).WillByDefault(Return(1));
140-
ON_CALL(*tokenizer, eos_tok()).WillByDefault(Return(2));
141-
ON_CALL(*tokenizer, vocab_size()).WillByDefault(Return(100));
141+
ON_CALL(*tokenizer, piece_to_id).WillByDefault([](const std::string&) {
142+
return ::tokenizers::Result<uint64_t>(0);
143+
});
142144

143145
return tokenizer;
144146
}

0 commit comments

Comments
 (0)