6
6
* LICENSE file in the root directory of this source tree.
7
7
*/
8
8
9
+ #ifdef EXECUTORCH_FB_BUCK
10
+ #include < TestResourceUtils/TestResourceUtils.h>
11
+ #endif
9
12
#include < executorch/extension/llm/tokenizer/tiktoken.h>
10
13
#include < executorch/runtime/platform/runtime.h>
11
14
#include < gmock/gmock.h>
12
15
#include < gtest/gtest.h>
13
- #include < sstream>
14
16
#include < vector>
15
17
16
18
using namespace ::testing;
@@ -47,6 +49,15 @@ static inline std::unique_ptr<std::vector<std::string>> _get_special_tokens() {
47
49
}
48
50
return special_tokens;
49
51
}
52
+
53
+ static inline std::string _get_resource_path (const std::string& name) {
54
+ #ifdef EXECUTORCH_FB_BUCK
55
+ return facebook::xplat::testing::getPathForTestResource (" resources/" + name);
56
+ #else
57
+ return std::getenv (" RESOURCES_PATH" ) + std::string (" /" ) + name;
58
+ #endif
59
+ }
60
+
50
61
} // namespace
51
62
52
63
class TiktokenExtensionTest : public Test {
@@ -55,8 +66,7 @@ class TiktokenExtensionTest : public Test {
55
66
executorch::runtime::runtime_init ();
56
67
tokenizer_ = std::make_unique<Tiktoken>(
57
68
_get_special_tokens (), kBOSTokenIndex , kEOSTokenIndex );
58
- modelPath_ = std::getenv (" RESOURCES_PATH" ) +
59
- std::string (" /test_tiktoken_tokenizer.model" );
69
+ modelPath_ = _get_resource_path (" test_tiktoken_tokenizer.model" );
60
70
}
61
71
62
72
std::unique_ptr<Tokenizer> tokenizer_;
@@ -144,44 +154,36 @@ TEST_F(TiktokenExtensionTest, ConstructionWithInvalidEOSIndex) {
144
154
}
145
155
146
156
TEST_F (TiktokenExtensionTest, LoadWithInvalidPath) {
147
- auto invalidModelPath =
148
- std::getenv (" RESOURCES_PATH" ) + std::string (" /nonexistent.model" );
149
-
150
- Error res = tokenizer_->load (invalidModelPath.c_str ());
157
+ auto invalidModelPath = " ./nonexistent.model" ;
158
+ Error res = tokenizer_->load (invalidModelPath);
151
159
EXPECT_EQ (res, Error::InvalidArgument);
152
160
}
153
161
154
162
TEST_F (TiktokenExtensionTest, LoadTiktokenFileWithInvalidRank) {
155
- auto invalidModelPath = std::getenv (" RESOURCES_PATH" ) +
156
- std::string (" /test_tiktoken_invalid_rank.model" );
157
-
163
+ auto invalidModelPath =
164
+ _get_resource_path (" test_tiktoken_invalid_rank.model" );
158
165
Error res = tokenizer_->load (invalidModelPath.c_str ());
159
166
160
167
EXPECT_EQ (res, Error::InvalidArgument);
161
168
}
162
169
163
170
TEST_F (TiktokenExtensionTest, LoadTiktokenFileWithInvalidBase64) {
164
- auto invalidModelPath = std::getenv (" RESOURCES_PATH" ) +
165
- std::string (" /test_tiktoken_invalid_base64.model" );
166
-
171
+ auto invalidModelPath =
172
+ _get_resource_path (" test_tiktoken_invalid_base64.model" );
167
173
Error res = tokenizer_->load (invalidModelPath.c_str ());
168
174
169
175
EXPECT_EQ (res, Error::InvalidArgument);
170
176
}
171
177
172
178
TEST_F (TiktokenExtensionTest, LoadTiktokenFileWithNoSpace) {
173
- auto invalidModelPath = std::getenv (" RESOURCES_PATH" ) +
174
- std::string (" /test_tiktoken_no_space.model" );
175
-
179
+ auto invalidModelPath = _get_resource_path (" test_tiktoken_no_space.model" );
176
180
Error res = tokenizer_->load (invalidModelPath.c_str ());
177
181
178
182
EXPECT_EQ (res, Error::InvalidArgument);
179
183
}
180
184
181
185
TEST_F (TiktokenExtensionTest, LoadTiktokenFileWithBPEFile) {
182
- auto invalidModelPath =
183
- std::getenv (" RESOURCES_PATH" ) + std::string (" /test_bpe_tokenizer.bin" );
184
-
186
+ auto invalidModelPath = _get_resource_path (" test_bpe_tokenizer.bin" );
185
187
Error res = tokenizer_->load (invalidModelPath.c_str ());
186
188
187
189
EXPECT_EQ (res, Error::InvalidArgument);
0 commit comments