Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Build and run C++ Python tests #218

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
55 changes: 55 additions & 0 deletions .github/workflows/unit_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
on:
workflow_dispatch:
pull_request:
push:

jobs:
run_unit_test:
name: Run unit tests on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- {os: ubuntu-latest, arch: x86_64}
- {os: ubuntu-24.04-arm, arch: aarch64}
- {os: windows-latest, arch: AMD64}
- {os: macos-14, arch: arm64}
- {os: macos-13, arch: x86_64,}

steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive

- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: 3.11

- name: Build xgrammar from source
run: |
echo "set(XGRAMMAR_BUILD_CXX_TESTS ON)" >> cmake/config.cmake
python -m pip install --upgrade pip
pip install -e .

- name: Run C++ tests
run: |
cd build && ctest && cd ..
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be able to do a ctest --test-dir build

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


- name: Run Python tests
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
if: env.HF_TOKEN != ''
run: |
pip install pytest
pytest

- name: Run Python tests without HF_TOKEN
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
if: env.HF_TOKEN == ''
run: |
pip install pytest
pytest -m "not hf_token_required"
6 changes: 3 additions & 3 deletions cpp/support/thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <mutex>
#include <queue>
#include <thread>
#include <type_traits>
#include <vector>

#include "logging.h"
Expand Down Expand Up @@ -67,9 +68,8 @@ class ThreadPool {
* \note Tasks are executed in FIFO order but may complete in any order.
*/
template <class F, class... Args>
auto Submit(F&& f, Args&&... args)
-> std::shared_future<typename std::result_of<F(Args...)>::type> {
using return_type = typename std::result_of<F(Args...)>::type;
auto Submit(F&& f, Args&&... args) -> std::shared_future<std::invoke_result_t<F, Args...>> {
using return_type = std::invoke_result_t<F, Args...>;

// Package the task with its arguments into a shared pointer
auto task = std::make_shared<std::packaged_task<return_type()>>(
Expand Down
4 changes: 3 additions & 1 deletion tests/cpp/test_thread_safe_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ using namespace std::chrono_literals;
TEST(XGrammarParallelTest, CacheEfficiency) {
auto cache = ThreadSafeCache<std::string, MockGrammar>{[](const std::string&) {
std::this_thread::sleep_for(1s); // simulate a slow operation
return MockGrammar{.uuid = counter++, .padding = {}};
MockGrammar g{};
g.uuid = counter++;
return g;
}};
auto futures = std::vector<std::future<std::size_t>>{};

Expand Down
Loading