Skip to content

Commit 9214f71

Browse files
authored
[Grammar] Migrate to XGrammar (#3015)
This PR migrates the implementation of the grammar engine to an external library XGrammar. The implementation of XGrammar is basically the same as our current version, but more modularized and robust. XGrammar will be continuously enhanced in the future. See https://github.com/mlc-ai/xgrammar for more details.
1 parent 2f37a63 commit 9214f71

35 files changed

+55
-7658
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
[submodule "3rdparty/stb"]
1414
path = 3rdparty/stb
1515
url = https://github.com/nothings/stb.git
16+
[submodule "3rdparty/xgrammar"]
17+
path = 3rdparty/xgrammar
18+
url = https://github.com/mlc-ai/xgrammar.git

3rdparty/xgrammar

Submodule xgrammar added at d4f57c4

CMakeLists.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ set(MLC_LLM_RUNTIME_LINKER_LIB "")
6868
set(TOKENZIER_CPP_PATH 3rdparty/tokenizers-cpp)
6969
add_subdirectory(${TOKENZIER_CPP_PATH} tokenizers EXCLUDE_FROM_ALL)
7070

71-
71+
set(XGRAMMAR_PATH 3rdparty/xgrammar)
7272
tvm_file_glob(GLOB_RECURSE MLC_LLM_SRCS cpp/*.cc)
73+
tvm_file_glob(GLOB_RECURSE XGRAMMAR_SRCS ${XGRAMMAR_PATH}/cpp/*.cc)
74+
list(FILTER XGRAMMAR_SRCS EXCLUDE REGEX "${XGRAMMAR_PATH}/cpp/pybind/.*\\.cc")
75+
list(APPEND MLC_LLM_SRCS ${XGRAMMAR_SRCS})
7376
add_library(mlc_llm_objs OBJECT ${MLC_LLM_SRCS})
7477

7578
set(
@@ -83,12 +86,14 @@ set(
8386
set(MLC_LLM_COMPILE_DEFS ${MLC_LLM_COMPILE_DEFS} DMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>)
8487
set(MLC_LLM_COMPILE_DEFS ${MLC_LLM_COMPILE_DEFS} __STDC_FORMAT_MACROS=1)
8588
set(MLC_LLM_COMPILE_DEFS ${MLC_LLM_COMPILE_DEFS} PICOJSON_USE_INT64)
89+
set(MLC_LLM_COMPILE_DEFS ${MLC_LLM_COMPILE_DEFS} XGRAMMAR_ENABLE_LOG_DEBUG=0)
8690

87-
target_include_directories(mlc_llm_objs PRIVATE ${MLC_LLM_INCLUDES})
8891
target_compile_definitions(mlc_llm_objs PRIVATE ${MLC_LLM_COMPILE_DEFS})
89-
target_include_directories(mlc_llm_objs PRIVATE ${TOKENZIER_CPP_PATH}/include)
9092
target_compile_definitions(mlc_llm_objs PRIVATE -DMLC_LLM_EXPORTS)
93+
target_include_directories(mlc_llm_objs PRIVATE ${MLC_LLM_INCLUDES})
9194
target_include_directories(mlc_llm_objs PRIVATE 3rdparty/stb)
95+
target_include_directories(mlc_llm_objs PRIVATE ${TOKENZIER_CPP_PATH}/include)
96+
target_include_directories(mlc_llm_objs PRIVATE ${XGRAMMAR_PATH}/include)
9297

9398
add_library(mlc_llm SHARED $<TARGET_OBJECTS:mlc_llm_objs>)
9499
add_library(mlc_llm_static STATIC $<TARGET_OBJECTS:mlc_llm_objs>)
@@ -135,7 +140,6 @@ add_library(mlc_llm_module SHARED $<TARGET_OBJECTS:mlc_llm_objs>)
135140
target_link_libraries(mlc_llm_module PUBLIC tvm)
136141
target_link_libraries(mlc_llm_module PRIVATE tokenizers_cpp)
137142

138-
139143
set_property(TARGET mlc_llm_module APPEND PROPERTY LINK_OPTIONS "${MLC_VISIBILITY_FLAG}")
140144
set_property(TARGET mlc_llm APPEND PROPERTY LINK_OPTIONS "${MLC_VISIBILITY_FLAG}")
141145

cpp/grammar/grammar.cc

Lines changed: 0 additions & 175 deletions
This file was deleted.

0 commit comments

Comments
 (0)