From 26f7d7e8dec3b8ecf7d62543d8891a90e80fb41d Mon Sep 17 00:00:00 2001 From: Zac Bowling Date: Fri, 7 Feb 2025 14:58:20 -0800 Subject: [PATCH 01/11] Adding xgrammar --- .../xgrammar/001-fix-macos-no-triton.patch | 43 +++++++++++ recipes/xgrammar/recipe.yaml | 71 +++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 recipes/xgrammar/001-fix-macos-no-triton.patch create mode 100644 recipes/xgrammar/recipe.yaml diff --git a/recipes/xgrammar/001-fix-macos-no-triton.patch b/recipes/xgrammar/001-fix-macos-no-triton.patch new file mode 100644 index 0000000000000..43399fb1bbc85 --- /dev/null +++ b/recipes/xgrammar/001-fix-macos-no-triton.patch @@ -0,0 +1,43 @@ +diff --git a/python/xgrammar/kernels/__init__.py b/python/xgrammar/kernels/__init__.py +index f99dfba..1a1df35 100644 +--- a/python/xgrammar/kernels/__init__.py ++++ b/python/xgrammar/kernels/__init__.py +@@ -1,4 +1,9 @@ + """The kernels for XGrammar.""" + + from .apply_token_bitmask_inplace_cpu import apply_token_bitmask_inplace_cpu +-from .apply_token_bitmask_inplace_triton import apply_token_bitmask_inplace_triton ++ ++# Try to import the triton kernel. If it fails, we will use the CPU kernel. ++try: ++ from .apply_token_bitmask_inplace_triton import apply_token_bitmask_inplace_triton ++except ImportError: ++ pass +diff --git a/python/xgrammar/matcher.py b/python/xgrammar/matcher.py +index 64a5289..f8843ec 100644 +--- a/python/xgrammar/matcher.py ++++ b/python/xgrammar/matcher.py +@@ -8,7 +8,13 @@ import torch + + from .base import XGRObject, _core + from .compiler import CompiledGrammar +-from .kernels import apply_token_bitmask_inplace_cpu, apply_token_bitmask_inplace_triton ++from .kernels import apply_token_bitmask_inplace_cpu ++ ++# Try to import the triton kernel. If it fails, we will use the CPU kernel. ++try: ++ from .kernels import apply_token_bitmask_inplace_triton ++except ImportError: ++ pass + + """The dtype of the bitmask: int32.""" + bitmask_dtype = torch.int32 +@@ -122,6 +128,8 @@ def apply_token_bitmask_inplace( + ) + + if logits.device.type == "cuda": ++ if apply_token_bitmask_inplace_triton is None: ++ raise ImportError("Triton is not installed.`.") + apply_token_bitmask_inplace_triton(logits, bitmask, indices) + elif logits.device.type == "cpu": + apply_token_bitmask_inplace_cpu(logits, bitmask, indices) diff --git a/recipes/xgrammar/recipe.yaml b/recipes/xgrammar/recipe.yaml new file mode 100644 index 0000000000000..e170138bbf63d --- /dev/null +++ b/recipes/xgrammar/recipe.yaml @@ -0,0 +1,71 @@ +context: + name: xgrammar + version: 0.1.11 + +package: + name: ${{ name|lower }} + version: ${{ version }} + +source: + url: https://github.com/mlc-ai/xgrammar/archive/refs/tags/v${{ version }}.tar.gz + sha256: 99f5321454320b76d0ef8a0b060c92c9ccadb6c96a9ba7b9c1543ba7f14e5cf2 + patches: + - 001-fix-macos-no-triton.patch + +build: + number: 0 + if: win + then: + skip: True # xgrammar claims windows support but it's very broken + script: | + mkdir -p build + cd build + export CXXFLAGS="$CXXFLAGS -isystem $PREFIX/include" + cmake .. -G Ninja + ninja + cd ../python + $PYTHON -m pip install . -vv --no-deps --no-build-isolation + +requirements: + build: + - {{ compiler('c') }} + - {{ compiler('cxx') }} + - {{ stdlib("c") }} + - dlpack + - pybind11 + - python + - cmake + - pip + - ninja + host: + - dlpack + - python + - cmake + - pip + run: + - if: linux + then: + - triton + - pydantic + - python + - sentencepiece + - tiktoken + - pytorch + - transformers + - pytest + +tests: + - python: + imports: + - xgrammar + pip_check: true + +about: + license: Apache-2.0 AND BSD-4-Clause + license_file: + - LICENSE + - NOTICE + +extra: + recipe-maintainers: + - zbowling \ No newline at end of file From 4be253e5e792a84e4809741c760f4d60b48363f5 Mon Sep 17 00:00:00 2001 From: Zac Bowling Date: Fri, 7 Feb 2025 15:10:48 -0800 Subject: [PATCH 02/11] fix lint --- recipes/xgrammar/recipe.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/recipes/xgrammar/recipe.yaml b/recipes/xgrammar/recipe.yaml index e170138bbf63d..4f3cbcbe7316f 100644 --- a/recipes/xgrammar/recipe.yaml +++ b/recipes/xgrammar/recipe.yaml @@ -28,9 +28,9 @@ build: requirements: build: - - {{ compiler('c') }} - - {{ compiler('cxx') }} - - {{ stdlib("c") }} + - ${{ compiler('c') }} + - ${{ compiler('cxx') }} + - ${{ stdlib("c") }} - dlpack - pybind11 - python From b1045c2d4f762dbd2af6d871a4f008e2567ed634 Mon Sep 17 00:00:00 2001 From: Zac Bowling Date: Fri, 7 Feb 2025 15:12:28 -0800 Subject: [PATCH 03/11] fix lint --- recipes/xgrammar/recipe.yaml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/recipes/xgrammar/recipe.yaml b/recipes/xgrammar/recipe.yaml index 4f3cbcbe7316f..deefbe8146447 100644 --- a/recipes/xgrammar/recipe.yaml +++ b/recipes/xgrammar/recipe.yaml @@ -14,9 +14,8 @@ source: build: number: 0 - if: win - then: - skip: True # xgrammar claims windows support but it's very broken + skip: + - win script: | mkdir -p build cd build From 2f64159f4e3be03ea111f1d2ee8388de148c7fc5 Mon Sep 17 00:00:00 2001 From: Zac Bowling Date: Fri, 7 Feb 2025 15:13:11 -0800 Subject: [PATCH 04/11] fix lint --- recipes/xgrammar/recipe.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recipes/xgrammar/recipe.yaml b/recipes/xgrammar/recipe.yaml index deefbe8146447..683937da5698c 100644 --- a/recipes/xgrammar/recipe.yaml +++ b/recipes/xgrammar/recipe.yaml @@ -67,4 +67,4 @@ about: extra: recipe-maintainers: - - zbowling \ No newline at end of file + - zbowling From 584105ff5e856e0cffdecbf44cf833bad33023a7 Mon Sep 17 00:00:00 2001 From: Zac Bowling Date: Fri, 7 Feb 2025 15:15:17 -0800 Subject: [PATCH 05/11] fix lint --- recipes/xgrammar/recipe.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/recipes/xgrammar/recipe.yaml b/recipes/xgrammar/recipe.yaml index 683937da5698c..267eb98891e54 100644 --- a/recipes/xgrammar/recipe.yaml +++ b/recipes/xgrammar/recipe.yaml @@ -60,6 +60,8 @@ tests: pip_check: true about: + homepage: https://github.com/mlc-ai/xgrammar + summary: XGrammar is an open-source library for efficient, flexible, and portable structured generation. license: Apache-2.0 AND BSD-4-Clause license_file: - LICENSE From e095b3f08e7f51bd6c6e6fd893cd81cb7d7dd363 Mon Sep 17 00:00:00 2001 From: Zac Bowling Date: Fri, 7 Feb 2025 15:15:26 -0800 Subject: [PATCH 06/11] fix lint --- recipes/xgrammar/recipe.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recipes/xgrammar/recipe.yaml b/recipes/xgrammar/recipe.yaml index 267eb98891e54..aecd1b5fd49f5 100644 --- a/recipes/xgrammar/recipe.yaml +++ b/recipes/xgrammar/recipe.yaml @@ -61,7 +61,7 @@ tests: about: homepage: https://github.com/mlc-ai/xgrammar - summary: XGrammar is an open-source library for efficient, flexible, and portable structured generation. + summary: XGrammar is an open-source library for efficient, flexible, and portable structured generation. license: Apache-2.0 AND BSD-4-Clause license_file: - LICENSE From 83b5dd153dd0730a13f1654e7a9ada598e77b20b Mon Sep 17 00:00:00 2001 From: Zac Bowling Date: Fri, 7 Feb 2025 15:17:33 -0800 Subject: [PATCH 07/11] add setuptools --- recipes/xgrammar/recipe.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/recipes/xgrammar/recipe.yaml b/recipes/xgrammar/recipe.yaml index aecd1b5fd49f5..f8ec98e1356f8 100644 --- a/recipes/xgrammar/recipe.yaml +++ b/recipes/xgrammar/recipe.yaml @@ -41,6 +41,7 @@ requirements: - python - cmake - pip + - setuptools run: - if: linux then: From 46e5243070d502ea6db0f579e1df5e232bab75c5 Mon Sep 17 00:00:00 2001 From: Zac Bowling Date: Fri, 7 Feb 2025 15:29:42 -0800 Subject: [PATCH 08/11] -Wno-dangling-reference --- recipes/xgrammar/recipe.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recipes/xgrammar/recipe.yaml b/recipes/xgrammar/recipe.yaml index f8ec98e1356f8..dbafdb4453248 100644 --- a/recipes/xgrammar/recipe.yaml +++ b/recipes/xgrammar/recipe.yaml @@ -19,7 +19,7 @@ build: script: | mkdir -p build cd build - export CXXFLAGS="$CXXFLAGS -isystem $PREFIX/include" + export CXXFLAGS="$CXXFLAGS -isystem $PREFIX/include -Wno-dangling-reference" cmake .. -G Ninja ninja cd ../python From 409270ce89c0eef029e835d06bdbf7616fe4badb Mon Sep 17 00:00:00 2001 From: Zac Bowling Date: Fri, 7 Feb 2025 15:42:06 -0800 Subject: [PATCH 09/11] -Wno-dangling-reference for linux only --- recipes/xgrammar/recipe.yaml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/recipes/xgrammar/recipe.yaml b/recipes/xgrammar/recipe.yaml index dbafdb4453248..9560dd1d3c88d 100644 --- a/recipes/xgrammar/recipe.yaml +++ b/recipes/xgrammar/recipe.yaml @@ -19,7 +19,10 @@ build: script: | mkdir -p build cd build - export CXXFLAGS="$CXXFLAGS -isystem $PREFIX/include -Wno-dangling-reference" + export CXXFLAGS="$CXXFLAGS -isystem $PREFIX/include" + if [[ $(uname) == "Linux" ]]; then + export CXXFLAGS="$CXXFLAGS -Wno-dangling-reference" + fi cmake .. -G Ninja ninja cd ../python From c0829f34f8287b6e07d49c0a5b4d88077f678ec8 Mon Sep 17 00:00:00 2001 From: Zac Bowling Date: Fri, 7 Feb 2025 16:19:55 -0800 Subject: [PATCH 10/11] cross compile support --- recipes/xgrammar/recipe.yaml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/recipes/xgrammar/recipe.yaml b/recipes/xgrammar/recipe.yaml index 9560dd1d3c88d..7f54a0b09886c 100644 --- a/recipes/xgrammar/recipe.yaml +++ b/recipes/xgrammar/recipe.yaml @@ -15,7 +15,7 @@ source: build: number: 0 skip: - - win + - win # xgrammar claims windows support but it's very broken script: | mkdir -p build cd build @@ -26,10 +26,14 @@ build: cmake .. -G Ninja ninja cd ../python - $PYTHON -m pip install . -vv --no-deps --no-build-isolation + ${{ PYTHON }} -m pip install . -vv --no-deps --no-build-isolation requirements: build: + - if: build_platform != target_platform + then: + - python + - cross-python_${{ target_platform }} - ${{ compiler('c') }} - ${{ compiler('cxx') }} - ${{ stdlib("c") }} @@ -37,7 +41,6 @@ requirements: - pybind11 - python - cmake - - pip - ninja host: - dlpack From 2a0215d646b94f59a27b3a4a37894263b3e4b2ce Mon Sep 17 00:00:00 2001 From: Zac Bowling Date: Mon, 10 Feb 2025 12:20:47 -0800 Subject: [PATCH 11/11] Update 001-fix-macos-no-triton.patch --- recipes/xgrammar/001-fix-macos-no-triton.patch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recipes/xgrammar/001-fix-macos-no-triton.patch b/recipes/xgrammar/001-fix-macos-no-triton.patch index 43399fb1bbc85..e9927dca60040 100644 --- a/recipes/xgrammar/001-fix-macos-no-triton.patch +++ b/recipes/xgrammar/001-fix-macos-no-triton.patch @@ -37,7 +37,7 @@ index 64a5289..f8843ec 100644 if logits.device.type == "cuda": + if apply_token_bitmask_inplace_triton is None: -+ raise ImportError("Triton is not installed.`.") ++ raise ImportError("Triton is not installed.") apply_token_bitmask_inplace_triton(logits, bitmask, indices) elif logits.device.type == "cpu": apply_token_bitmask_inplace_cpu(logits, bitmask, indices)