|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/examples/models/llama2/tokenizer/tokenizer.h> |
| 10 | +#include <executorch/runtime/platform/runtime.h> |
| 11 | +#include <gtest/gtest.h> |
| 12 | +#include "tools/cxx/Resources.h" |
| 13 | + |
| 14 | +using namespace ::testing; |
| 15 | + |
| 16 | +namespace torch { |
| 17 | +namespace executor { |
| 18 | + |
| 19 | +class TokenizerExtensionTest : public ::testing::Test { |
| 20 | + public: |
| 21 | + void SetUp() override { |
| 22 | + torch::executor::runtime_init(); |
| 23 | + modelPath_ = |
| 24 | + build::getResourcePath( |
| 25 | + "executorch/examples/models/llama2/tokenizer/test/test.bin") |
| 26 | + .string(); |
| 27 | + tokenizer_ = std::make_unique<Tokenizer>(32000); |
| 28 | + } |
| 29 | + |
| 30 | + std::unique_ptr<Tokenizer> tokenizer_; |
| 31 | + std::string modelPath_; |
| 32 | +}; |
| 33 | + |
| 34 | +TEST_F(TokenizerExtensionTest, EncodeWithoutLoadFails) { |
| 35 | + Error error = tokenizer_->encode("hello world", 0, 0, nullptr, nullptr); |
| 36 | + EXPECT_EQ(error, Error::NotSupported); |
| 37 | +} |
| 38 | + |
| 39 | +TEST_F(TokenizerExtensionTest, DecodeWithoutLoadFails) { |
| 40 | + auto result = tokenizer_->decode(0, 0); |
| 41 | + EXPECT_EQ(result.error(), Error::NotSupported); |
| 42 | +} |
| 43 | + |
| 44 | +TEST_F(TokenizerExtensionTest, TokenizerVocabSizeIsExpected) { |
| 45 | + Error res = tokenizer_->load(modelPath_.c_str()); |
| 46 | + EXPECT_EQ(res, Error::Ok); |
| 47 | + // test.bin has vocab size 0 but the tokenizer respects the vocab size being |
| 48 | + // passed in and add placeholder tokens. |
| 49 | + EXPECT_EQ(tokenizer_->vocab_size(), 32000); |
| 50 | + EXPECT_EQ(tokenizer_->bos_tok(), 1); |
| 51 | + EXPECT_EQ(tokenizer_->eos_tok(), 2); |
| 52 | +} |
| 53 | + |
| 54 | +} // namespace executor |
| 55 | +} // namespace torch |
0 commit comments