Skip to content

Commit 20c8e8c

Browse files
Fix test with resources (#7071)
Fix test failure due to resources not handled correctly by ios tests. Differential Revision: [D66392647](https://our.internmc.facebook.com/intern/diff/D66392647/) ghstack-source-id: 255370795 Pull Request resolved: #7062 Co-authored-by: Mengwei Liu <[email protected]>
1 parent a1f668d commit 20c8e8c

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

examples/models/llama/tokenizer/test/test_tiktoken.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,31 @@
1414

1515
#include <gtest/gtest.h>
1616

17+
#ifdef EXECUTORCH_FB_BUCK
18+
#include <TestResourceUtils/TestResourceUtils.h>
19+
#endif
20+
1721
using namespace ::testing;
1822

1923
using ::example::Version;
2024
using ::executorch::extension::llm::Tokenizer;
2125
using ::executorch::runtime::Error;
2226
using ::executorch::runtime::Result;
2327

28+
static std::string get_resource_path(const std::string& name) {
29+
#ifdef EXECUTORCH_FB_BUCK
30+
return facebook::xplat::testing::getPathForTestResource("resources/" + name);
31+
#else
32+
return std::getenv("RESOURCES_PATH") + std::string("/") + name;
33+
#endif
34+
}
35+
2436
class MultimodalTiktokenV5ExtensionTest : public Test {
2537
public:
2638
void SetUp() override {
2739
executorch::runtime::runtime_init();
2840
tokenizer_ = get_tiktoken_for_llama(Version::Multimodal);
29-
modelPath_ = std::getenv("RESOURCES_PATH") +
30-
std::string("/test_tiktoken_tokenizer.model");
41+
modelPath_ = get_resource_path("test_tiktoken_tokenizer.model");
3142
}
3243

3344
std::unique_ptr<Tokenizer> tokenizer_;

extension/llm/tokenizer/test/test_bpe_tokenizer.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9+
#ifdef EXECUTORCH_FB_BUCK
10+
#include <TestResourceUtils/TestResourceUtils.h>
11+
#endif
912
#include <executorch/extension/llm/tokenizer/bpe_tokenizer.h>
1013
#include <executorch/runtime/platform/runtime.h>
1114
#include <gtest/gtest.h>
@@ -23,8 +26,13 @@ class TokenizerExtensionTest : public Test {
2326
void SetUp() override {
2427
executorch::runtime::runtime_init();
2528
tokenizer_ = std::make_unique<BPETokenizer>();
29+
#ifdef EXECUTORCH_FB_BUCK
30+
modelPath_ = facebook::xplat::testing::getPathForTestResource(
31+
"resources/test_bpe_tokenizer.bin");
32+
#else
2633
modelPath_ =
2734
std::getenv("RESOURCES_PATH") + std::string("/test_bpe_tokenizer.bin");
35+
#endif
2836
}
2937

3038
std::unique_ptr<Tokenizer> tokenizer_;

extension/llm/tokenizer/test/test_tiktoken.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9+
#ifdef EXECUTORCH_FB_BUCK
10+
#include <TestResourceUtils/TestResourceUtils.h>
11+
#endif
912
#include <executorch/extension/llm/tokenizer/tiktoken.h>
1013
#include <executorch/runtime/platform/runtime.h>
1114
#include <gmock/gmock.h>
1215
#include <gtest/gtest.h>
13-
#include <sstream>
1416
#include <vector>
1517

1618
using namespace ::testing;
@@ -47,6 +49,15 @@ static inline std::unique_ptr<std::vector<std::string>> _get_special_tokens() {
4749
}
4850
return special_tokens;
4951
}
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+
5061
} // namespace
5162

5263
class TiktokenExtensionTest : public Test {
@@ -55,8 +66,7 @@ class TiktokenExtensionTest : public Test {
5566
executorch::runtime::runtime_init();
5667
tokenizer_ = std::make_unique<Tiktoken>(
5768
_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");
6070
}
6171

6272
std::unique_ptr<Tokenizer> tokenizer_;
@@ -144,44 +154,36 @@ TEST_F(TiktokenExtensionTest, ConstructionWithInvalidEOSIndex) {
144154
}
145155

146156
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);
151159
EXPECT_EQ(res, Error::InvalidArgument);
152160
}
153161

154162
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");
158165
Error res = tokenizer_->load(invalidModelPath.c_str());
159166

160167
EXPECT_EQ(res, Error::InvalidArgument);
161168
}
162169

163170
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");
167173
Error res = tokenizer_->load(invalidModelPath.c_str());
168174

169175
EXPECT_EQ(res, Error::InvalidArgument);
170176
}
171177

172178
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");
176180
Error res = tokenizer_->load(invalidModelPath.c_str());
177181

178182
EXPECT_EQ(res, Error::InvalidArgument);
179183
}
180184

181185
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");
185187
Error res = tokenizer_->load(invalidModelPath.c_str());
186188

187189
EXPECT_EQ(res, Error::InvalidArgument);

0 commit comments

Comments
 (0)