Skip to content

Commit 9c774d1

Browse files
committed
Update base for Update on "[Executorch] Refactor op_add to support op_sub broadcasting"
Summary: Refactor op_add to conslidate commong broadcasting related improvements Test Plan: Previously added tests Reviewers: Subscribers: Tasks: Tags: cc larryliu0820 manuelcandales Differential Revision: [D69491817](https://our.internmc.facebook.com/intern/diff/D69491817) [ghstack-poisoned]
2 parents 0de5f8b + b71d873 commit 9c774d1

File tree

12 files changed

+126
-72
lines changed

12 files changed

+126
-72
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ option(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR "Build the Flat Tensor extension"
186186
OFF
187187
)
188188

189+
option(EXECUTORCH_BUILD_EXTENSION_LLM "Build the LLM extension"
190+
OFF
191+
)
192+
189193
option(EXECUTORCH_BUILD_EXTENSION_MODULE "Build the Module extension" OFF)
190194

191195
option(EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL "Build the Runner Util extension"
@@ -718,6 +722,10 @@ if(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR)
718722
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/flat_tensor/serialize)
719723
endif()
720724

725+
if(EXECUTORCH_BUILD_EXTENSION_LLM)
726+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/llm/tokenizer)
727+
endif()
728+
721729
if(EXECUTORCH_BUILD_EXTENSION_MODULE)
722730
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/module)
723731
endif()

build/Utils.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ function(executorch_print_configuration_summary)
7070
message(STATUS " EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR : "
7171
"${EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR}"
7272
)
73+
message(STATUS " EXECUTORCH_BUILD_EXTENSION_LLM : "
74+
"${EXECUTORCH_BUILD_EXTENSION_LLM}"
75+
)
7376
message(STATUS " EXECUTORCH_BUILD_EXTENSION_MODULE : "
7477
"${EXECUTORCH_BUILD_EXTENSION_MODULE}"
7578
)

build/cmake_deps.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,22 @@ deps = [
402402
"xnnpack_backend",
403403
]
404404

405+
[targets.extension_llm_tokenizer]
406+
buck_targets = [
407+
"//extension/llm/tokenizer:bpe_tokenizer",
408+
"//extension/llm/tokenizer:tiktoken",
409+
]
410+
filters = [
411+
".cpp$",
412+
]
413+
excludes = [
414+
"^codegen",
415+
]
416+
deps = [
417+
"executorch",
418+
"executorch_core",
419+
]
420+
405421
[targets.llama_runner]
406422
buck_targets = [
407423
"//examples/models/llama/runner:runner",

extension/llm/export/builder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
from executorch.extension.export_util.utils import export_to_edge, save_pte_program
3636

37-
from executorch.extension.llm.export.export_passes import RemoveRedundantPermutes
37+
from executorch.extension.llm.export.export_passes import RemoveRedundantTransposes
3838
from executorch.extension.llm.tokenizer.utils import get_tokenizer
3939
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
4040
from torch.ao.quantization.quantizer import Quantizer
@@ -113,7 +113,7 @@ def __init__(
113113
self.calibration_seq_length = calibration_seq_length
114114
self.calibration_data = calibration_data
115115
self.tokenizer_path = tokenizer_path
116-
self.canonical_passes = [RemoveRedundantPermutes()]
116+
self.canonical_passes = [RemoveRedundantTransposes()]
117117

118118
def set_output_dir(self, output_dir: str) -> "LLMEdgeManager":
119119
"""

extension/llm/export/export_passes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def _normalize_dims(tensor: FakeTensor, dim_0: int, dim_1: int):
1919
return dim_0, dim_1
2020

2121

22-
class RemoveRedundantPermutes(ExportPass):
22+
class RemoveRedundantTransposes(ExportPass):
2323
"""
2424
This pass removes redundant transpose nodes in the graph.
2525
It checks if the next node is also a transpose node and if the two transpose nodes undo each other.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# Please this file formatted by running:
8+
# ~~~
9+
# cmake-format -i CMakeLists.txt
10+
# ~~~
11+
12+
cmake_minimum_required(VERSION 3.19)
13+
14+
# Source root directory for executorch.
15+
if(NOT EXECUTORCH_ROOT)
16+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
17+
endif()
18+
19+
set(ABSL_ENABLE_INSTALL ON)
20+
set(ABSL_PROPAGATE_CXX_STD ON)
21+
set(_pic_flag ${CMAKE_POSITION_INDEPENDENT_CODE})
22+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
23+
add_subdirectory(
24+
${CMAKE_CURRENT_SOURCE_DIR}/../third-party/abseil-cpp
25+
${CMAKE_CURRENT_BINARY_DIR}/abseil-cpp
26+
)
27+
add_subdirectory(
28+
${CMAKE_CURRENT_SOURCE_DIR}/../third-party/re2
29+
${CMAKE_CURRENT_BINARY_DIR}/re2
30+
)
31+
set(CMAKE_POSITION_INDEPENDENT_CODE ${_pic_flag})
32+
33+
list(TRANSFORM _extension_llm_tokenizer__srcs PREPEND "${EXECUTORCH_ROOT}/")
34+
add_library(extension_llm_tokenizer ${_extension_llm_tokenizer__srcs})
35+
target_include_directories(
36+
extension_llm_tokenizer PUBLIC ${EXECUTORCH_ROOT}/..
37+
${_common_include_directories}
38+
)
39+
40+
target_link_libraries(extension_llm_tokenizer re2::re2)
41+
target_compile_options(
42+
extension_llm_tokenizer PUBLIC ${_common_compile_options}
43+
)
44+
45+
# Install libraries
46+
install(
47+
TARGETS extension_llm_tokenizer
48+
DESTINATION lib
49+
INCLUDES
50+
DESTINATION ${_common_include_directories}
51+
)
52+
53+
target_include_directories(
54+
extension_llm_tokenizer
55+
PRIVATE ${CMAKE_INSTALL_PREFIX}/include
56+
${CMAKE_CURRENT_SOURCE_DIR}/../third-party/abseil-cpp
57+
)
58+
59+
if(BUILD_TESTING)
60+
add_subdirectory(test)
61+
endif()

extension/llm/tokenizer/test/CMakeLists.txt

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,18 @@
1212
#
1313

1414
cmake_minimum_required(VERSION 3.19)
15-
project(tokenizer_test)
16-
17-
# Use C++17 for test.
18-
set(CMAKE_CXX_STANDARD 17)
1915

2016
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../..)
2117

2218
include(${EXECUTORCH_ROOT}/build/Test.cmake)
2319

24-
set(_tokenizer_test_srcs
25-
test_tiktoken.cpp test_bpe_tokenizer.cpp
26-
${CMAKE_CURRENT_SOURCE_DIR}/../tiktoken.cpp
27-
${CMAKE_CURRENT_SOURCE_DIR}/../bpe_tokenizer.cpp
28-
)
20+
set(test_env "RESOURCES_PATH=${EXECUTORCH_ROOT}/extension/llm/tokenizer/test/resources")
2921

30-
set(ENV{RESOURCES_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/resources)
31-
set(ABSL_ENABLE_INSTALL ON)
32-
set(ABSL_PROPAGATE_CXX_STD ON)
33-
set(_pic_flag ${CMAKE_POSITION_INDEPENDENT_CODE})
34-
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
35-
add_subdirectory(
36-
${CMAKE_CURRENT_SOURCE_DIR}/../../third-party/abseil-cpp
37-
${CMAKE_CURRENT_BINARY_DIR}/abseil-cpp
38-
)
39-
add_subdirectory(
40-
${CMAKE_CURRENT_SOURCE_DIR}/../../third-party/re2
41-
${CMAKE_CURRENT_BINARY_DIR}/re2
42-
)
43-
set(CMAKE_POSITION_INDEPENDENT_CODE ${_pic_flag})
22+
set(_test_srcs test_bpe_tokenizer.cpp test_tiktoken.cpp)
4423

45-
et_cxx_test(tokenizer_test SOURCES ${_tokenizer_test_srcs} EXTRA_LIBS re2::re2)
46-
target_include_directories(
47-
tokenizer_test
48-
PRIVATE ${CMAKE_INSTALL_PREFIX}/include
49-
${CMAKE_CURRENT_SOURCE_DIR}/../../third-party/abseil-cpp
24+
et_cxx_test(
25+
extension_llm_tokenizer_test SOURCES ${_test_srcs} EXTRA_LIBS
26+
extension_llm_tokenizer
5027
)
28+
29+
set_property(TEST extension_llm_tokenizer_test PROPERTY ENVIRONMENT ${test_env})

extension/llm/tokenizer/test/targets.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
load(
2+
"@fbsource//tools/build_defs:default_platform_defs.bzl",
3+
"ANDROID",
4+
"CXX",
5+
)
16
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
27

38
def define_common_targets():
@@ -28,6 +33,7 @@ def define_common_targets():
2833
env = {
2934
"RESOURCES_PATH": "$(location :resources)/resources",
3035
},
36+
platforms = [CXX, ANDROID], # Cannot bundle resources on Apple platform.
3137
)
3238

3339
runtime.cxx_test(
@@ -41,6 +47,7 @@ def define_common_targets():
4147
env = {
4248
"RESOURCES_PATH": "$(location :resources)/resources",
4349
},
50+
platforms = [CXX, ANDROID], # Cannot bundle resources on Apple platform.
4451
external_deps = [
4552
"re2",
4653
],

extension/llm/tokenizer/test/test_bpe_tokenizer.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +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
129
#include <executorch/extension/llm/tokenizer/bpe_tokenizer.h>
1310
#include <executorch/runtime/platform/runtime.h>
1411
#include <gtest/gtest.h>
15-
#include <vector>
1612

1713
using namespace ::testing;
1814

@@ -26,13 +22,8 @@ class TokenizerExtensionTest : public Test {
2622
void SetUp() override {
2723
executorch::runtime::runtime_init();
2824
tokenizer_ = std::make_unique<BPETokenizer>();
29-
#ifdef EXECUTORCH_FB_BUCK
30-
modelPath_ = facebook::xplat::testing::getPathForTestResource(
31-
"resources/test_bpe_tokenizer.bin");
32-
#else
3325
modelPath_ =
3426
std::getenv("RESOURCES_PATH") + std::string("/test_bpe_tokenizer.bin");
35-
#endif
3627
}
3728

3829
std::unique_ptr<Tokenizer> tokenizer_;
@@ -50,15 +41,15 @@ TEST_F(TokenizerExtensionTest, DecodeWithoutLoadFails) {
5041
}
5142

5243
TEST_F(TokenizerExtensionTest, DecodeOutOfRangeFails) {
53-
Error res = tokenizer_->load(modelPath_.c_str());
44+
Error res = tokenizer_->load(modelPath_);
5445
EXPECT_EQ(res, Error::Ok);
5546
auto result = tokenizer_->decode(0, 64000);
5647
// The vocab size is 32000, and token 64000 is out of vocab range.
5748
EXPECT_EQ(result.error(), Error::NotSupported);
5849
}
5950

6051
TEST_F(TokenizerExtensionTest, TokenizerMetadataIsExpected) {
61-
Error res = tokenizer_->load(modelPath_.c_str());
52+
Error res = tokenizer_->load(modelPath_);
6253
EXPECT_EQ(res, Error::Ok);
6354
// test_bpe_tokenizer.bin has vocab_size 0, bos_id 0, eos_id 0 recorded.
6455
EXPECT_EQ(tokenizer_->vocab_size(), 0);

extension/llm/tokenizer/test/test_tiktoken.cpp

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
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
129
#include <executorch/extension/llm/tokenizer/tiktoken.h>
1310
#include <executorch/runtime/platform/runtime.h>
1411
#include <gmock/gmock.h>
1512
#include <gtest/gtest.h>
16-
#include <vector>
1713

1814
using namespace ::testing;
1915
using ::executorch::extension::llm::Tiktoken;
@@ -49,15 +45,6 @@ static inline std::unique_ptr<std::vector<std::string>> _get_special_tokens() {
4945
}
5046
return special_tokens;
5147
}
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-
6148
} // namespace
6249

6350
class TiktokenExtensionTest : public Test {
@@ -66,7 +53,8 @@ class TiktokenExtensionTest : public Test {
6653
executorch::runtime::runtime_init();
6754
tokenizer_ = std::make_unique<Tiktoken>(
6855
_get_special_tokens(), kBOSTokenIndex, kEOSTokenIndex);
69-
modelPath_ = _get_resource_path("test_tiktoken_tokenizer.model");
56+
modelPath_ = std::getenv("RESOURCES_PATH") +
57+
std::string("/test_tiktoken_tokenizer.model");
7058
}
7159

7260
std::unique_ptr<Tokenizer> tokenizer_;
@@ -84,15 +72,15 @@ TEST_F(TiktokenExtensionTest, DecodeWithoutLoadFails) {
8472
}
8573

8674
TEST_F(TiktokenExtensionTest, TokenizerVocabSizeIsExpected) {
87-
Error res = tokenizer_->load(modelPath_.c_str());
75+
Error res = tokenizer_->load(modelPath_);
8876
EXPECT_EQ(res, Error::Ok);
8977
EXPECT_EQ(tokenizer_->vocab_size(), 128256);
9078
EXPECT_EQ(tokenizer_->bos_tok(), 128000);
9179
EXPECT_EQ(tokenizer_->eos_tok(), 128001);
9280
}
9381

9482
TEST_F(TiktokenExtensionTest, TokenizerEncodeCorrectly) {
95-
Error res = tokenizer_->load(modelPath_.c_str());
83+
Error res = tokenizer_->load(modelPath_);
9684
EXPECT_EQ(res, Error::Ok);
9785
Result<std::vector<uint64_t>> out = tokenizer_->encode("hello world", 1, 0);
9886
EXPECT_EQ(out.error(), Error::Ok);
@@ -103,7 +91,7 @@ TEST_F(TiktokenExtensionTest, TokenizerEncodeCorrectly) {
10391
}
10492

10593
TEST_F(TiktokenExtensionTest, TokenizerDecodeCorrectly) {
106-
Error res = tokenizer_->load(modelPath_.c_str());
94+
Error res = tokenizer_->load(modelPath_);
10795
EXPECT_EQ(res, Error::Ok);
10896
std::vector<std::string> expected = {"<|begin_of_text|>", "hello", " world"};
10997
std::vector<uint64_t> tokens = {128000, 15339, 1917};
@@ -115,7 +103,7 @@ TEST_F(TiktokenExtensionTest, TokenizerDecodeCorrectly) {
115103
}
116104

117105
TEST_F(TiktokenExtensionTest, TokenizerDecodeOutOfRangeFails) {
118-
Error res = tokenizer_->load(modelPath_.c_str());
106+
Error res = tokenizer_->load(modelPath_);
119107
EXPECT_EQ(res, Error::Ok);
120108
// The vocab size is 128256, addes 256 just so the token is out of vocab
121109
// range.
@@ -160,31 +148,33 @@ TEST_F(TiktokenExtensionTest, LoadWithInvalidPath) {
160148
}
161149

162150
TEST_F(TiktokenExtensionTest, LoadTiktokenFileWithInvalidRank) {
163-
auto invalidModelPath =
164-
_get_resource_path("test_tiktoken_invalid_rank.model");
165-
Error res = tokenizer_->load(invalidModelPath.c_str());
151+
auto invalidModelPath = std::getenv("RESOURCES_PATH") +
152+
std::string("/test_tiktoken_invalid_rank.model");
153+
Error res = tokenizer_->load(invalidModelPath);
166154

167155
EXPECT_EQ(res, Error::InvalidArgument);
168156
}
169157

170158
TEST_F(TiktokenExtensionTest, LoadTiktokenFileWithInvalidBase64) {
171-
auto invalidModelPath =
172-
_get_resource_path("test_tiktoken_invalid_base64.model");
173-
Error res = tokenizer_->load(invalidModelPath.c_str());
159+
auto invalidModelPath = std::getenv("RESOURCES_PATH") +
160+
std::string("/test_tiktoken_invalid_base64.model");
161+
Error res = tokenizer_->load(invalidModelPath);
174162

175163
EXPECT_EQ(res, Error::InvalidArgument);
176164
}
177165

178166
TEST_F(TiktokenExtensionTest, LoadTiktokenFileWithNoSpace) {
179-
auto invalidModelPath = _get_resource_path("test_tiktoken_no_space.model");
180-
Error res = tokenizer_->load(invalidModelPath.c_str());
167+
auto invalidModelPath = std::getenv("RESOURCES_PATH") +
168+
std::string("/test_tiktoken_no_space.model");
169+
Error res = tokenizer_->load(invalidModelPath);
181170

182171
EXPECT_EQ(res, Error::InvalidArgument);
183172
}
184173

185174
TEST_F(TiktokenExtensionTest, LoadTiktokenFileWithBPEFile) {
186-
auto invalidModelPath = _get_resource_path("test_bpe_tokenizer.bin");
187-
Error res = tokenizer_->load(invalidModelPath.c_str());
175+
auto invalidModelPath =
176+
std::getenv("RESOURCES_PATH") + std::string("/test_bpe_tokenizer.bin");
177+
Error res = tokenizer_->load(invalidModelPath);
188178

189179
EXPECT_EQ(res, Error::InvalidArgument);
190180
}

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ addopts =
4949
backends/xnnpack/test/serialization
5050
# extension/
5151
extension/llm/modules/test
52+
extension/llm/export
5253
extension/pybindings/test
5354
# Runtime
5455
runtime

test/run_oss_cpp_tests.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ build_and_run_test() {
5757

5858
if [[ "$test_dir" =~ .*examples/models/llama/tokenizer.* ]]; then
5959
RESOURCES_PATH=$(realpath examples/models/llama/tokenizer/test/resources)
60-
elif [[ "$test_dir" =~ .*extension/llm/tokenizer.* ]]; then
61-
RESOURCES_PATH=$(realpath extension/llm/tokenizer/test/resources)
6260
fi
6361
export RESOURCES_PATH
6462

0 commit comments

Comments
 (0)