Skip to content

Commit 4e25c91

Browse files
authored
Merge pull request #514 from wudidapaopao/fix_import_torch
Isolate pybind11 internals to resolve torch import conflict
2 parents 34816da + 3880a49 commit 4e25c91

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

.github/workflows/build_linux_arm64_wheels-gh.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,17 @@ jobs:
376376
pyenv shell --unset
377377
done
378378
continue-on-error: false
379+
- name: Test torch compatibility on Python 3.11
380+
run: |
381+
export PATH="$HOME/.pyenv/bin:$PATH"
382+
eval "$(pyenv init -)"
383+
pyenv shell 3.11
384+
python -m pip install dist/*.whl --force-reinstall
385+
python -m pip install pytest
386+
python -m pip install torch --index-url https://download.pytorch.org/whl/cpu
387+
python -m pytest tests/test_torch_compatibility.py -v
388+
pyenv shell --unset
389+
continue-on-error: false
379390
- name: Test DataStore on Python 3.11
380391
run: |
381392
ulimit -c unlimited

.github/workflows/build_linux_x86_wheels.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,17 @@ jobs:
361361
pyenv shell --unset
362362
done
363363
continue-on-error: false
364+
- name: Test torch compatibility on Python 3.11
365+
run: |
366+
export PATH="$HOME/.pyenv/bin:$PATH"
367+
eval "$(pyenv init -)"
368+
pyenv shell 3.11
369+
python -m pip install dist/*.whl --force-reinstall
370+
python -m pip install pytest
371+
python -m pip install torch --index-url https://download.pytorch.org/whl/cpu
372+
python -m pytest tests/test_torch_compatibility.py -v
373+
pyenv shell --unset
374+
continue-on-error: false
364375
- name: Test DataStore on Python 3.11
365376
run: |
366377
ulimit -c unlimited

.github/workflows/build_macos_arm64_wheels.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,17 @@ jobs:
353353
pyenv shell --unset
354354
done
355355
continue-on-error: false
356+
- name: Test torch compatibility on Python 3.11
357+
run: |
358+
export PATH="$HOME/.pyenv/bin:$PATH"
359+
eval "$(pyenv init -)"
360+
pyenv shell 3.11
361+
python -m pip install dist/*.whl --force-reinstall --no-cache-dir
362+
python -m pip install pytest
363+
python -m pip install torch --index-url https://download.pytorch.org/whl/cpu
364+
python -m pytest tests/test_torch_compatibility.py -v
365+
pyenv shell --unset
366+
continue-on-error: false
356367
- name: Test DataStore on Python 3.11
357368
run: |
358369
export PATH="$HOME/.pyenv/bin:$PATH"

.github/workflows/build_macos_x86_wheels.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,17 @@ jobs:
353353
pyenv shell --unset
354354
done
355355
continue-on-error: false
356+
- name: Test torch compatibility on Python 3.11
357+
run: |
358+
export PATH="$HOME/.pyenv/bin:$PATH"
359+
eval "$(pyenv init -)"
360+
pyenv shell 3.11
361+
python -m pip install dist/*.whl --force-reinstall
362+
python -m pip install pytest
363+
python -m pip install torch --index-url https://download.pytorch.org/whl/cpu
364+
python -m pytest tests/test_torch_compatibility.py -v
365+
pyenv shell --unset
366+
continue-on-error: false
356367
- name: Test DataStore on Python 3.11
357368
run: |
358369
ulimit -c unlimited

contrib/pybind11-cmake/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ endif()
6060
target_include_directories(pybind11_stubs PRIVATE ${PYBIND11_INCLUDE_DIR})
6161
target_include_directories(pybind11_stubs PRIVATE ${Python_INCLUDE_DIRS})
6262
target_compile_definitions(pybind11_stubs PUBLIC
63+
PYBIND11_INTERNALS_KIND="_chdb"
6364
PYBIND11_NONLIMITEDAPI_LIB_SUFFIX_FOR_MODULE="${PYBIND11_LIB_SUFFIX}"
6465
Py_LIMITED_API=0x03090000
6566
)

tests/test_torch_compatibility.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!python3
2+
3+
import importlib.util
4+
import unittest
5+
6+
HAS_TORCH = importlib.util.find_spec("torch") is not None
7+
8+
9+
@unittest.skipUnless(HAS_TORCH, "torch is not installed")
10+
class TestTorchCompatibility(unittest.TestCase):
11+
"""Test that chdb and torch can coexist in the same process."""
12+
13+
def test_import_torch_then_chdb(self):
14+
import torch
15+
import chdb
16+
17+
# torch: basic tensor operation
18+
t = torch.tensor([1.0, 2.0, 3.0])
19+
self.assertEqual(t.sum().item(), 6.0)
20+
21+
# chdb: basic SQL query
22+
res = chdb.query("SELECT 1 + 2 AS result", "CSV")
23+
self.assertIn("3", str(res))
24+
25+
def test_import_chdb_then_torch(self):
26+
import chdb
27+
import torch
28+
29+
# chdb: basic SQL query
30+
res = chdb.query("SELECT 'ok' AS status", "CSV")
31+
self.assertIn("ok", str(res))
32+
33+
# torch: basic tensor operation
34+
t = torch.tensor([[1, 2], [3, 4]])
35+
self.assertEqual(t.shape, (2, 2))
36+
37+
38+
if __name__ == "__main__":
39+
unittest.main()

0 commit comments

Comments
 (0)