Dev: cpu support for c extension - #34
Merged
Merged
Conversation
windy-pig
force-pushed
the
dev/cpu-support-for-c-extension
branch
from
June 12, 2025 15:42
05e9a13 to
a86cefc
Compare
There was a problem hiding this comment.
Pull Request Overview
Adds CPU support for the C extension by including a CPU implementation and adjusting build flags
- Introduce
_hamiltonian_cpu.cppto the source list for CPU execution - Modify
extra_cflagsto use debug flags (-g,-O0) instead of optimization
Comments suppressed due to low confidence (1)
qmb/hamiltonian.py:34
- Switching from
-O3to-O0disables optimizations and adds debug info, which will significantly degrade performance. Consider restoring-O3or making optimization levels configurable per build type.
extra_cflags=["-g", "-O0", "-ffast-math", "-march=native", f"-DN_QUBYTES={n_qubytes}", f"-DPARTICLE_CUT={particle_cut}"],
| name=name, | ||
| sources=[ | ||
| f"{folder}/_hamiltonian.cpp", | ||
| f"{folder}/_hamiltonian_cpu.cpp", |
There was a problem hiding this comment.
[nitpick] The CPU source file is always included in the build. To avoid unnecessary compilation overhead, consider making _hamiltonian_cpu.cpp conditional—e.g., based on a CPU/GPU flag or environment variable.
Suggested change
| f"{folder}/_hamiltonian_cpu.cpp", | |
| *(f"{folder}/_hamiltonian_cpu.cpp",) if os.getenv("USE_CPU", "True") == "True" else (), |
hzhangxyz
reviewed
Jun 16, 2025
hzhangxyz
left a comment
Member
There was a problem hiding this comment.
意见见上面的具体评论,同时参考分支: dev/cpu-support-for-c-extension-comment-by-hzhangxyz 。
| name=name, | ||
| sources=[ | ||
| f"{folder}/_hamiltonian.cpp", | ||
| f"{folder}/_hamiltonian_cpu.cpp", |
| f"{folder}/_hamiltonian_cuda.cu", | ||
| ], | ||
| is_python_module=n_qubytes == 0, | ||
| extra_cflags=["-O3", "-ffast-math", "-march=native", f"-DN_QUBYTES={n_qubytes}", f"-DPARTICLE_CUT={particle_cut}"], |
| return ((*data) >> index) & 1; | ||
| } | ||
|
|
||
| bool set_bit(std::uint8_t* data, std::uint8_t index, bool value) { |
| auto sorted_result_psi = torch::zeros({result_batch_size, 2}, torch::TensorOptions().dtype(torch::kFloat64).device(device, device_id)); | ||
|
|
||
| thrust::sort_by_key( | ||
| reinterpret_cast<std::array<std::uint8_t, n_qubytes>*>(sorted_result_configs.data_ptr()), |
| std::int64_t batch_index; | ||
|
|
||
| for(term_index = 0; term_index < term_number; term_index++){ | ||
| for(batch_index = 0; term_index < term_number; batch_index++){ |
| @@ -0,0 +1,595 @@ | |||
| #include <thrust/sort.h> | |||
Member
|
参考 https://github.com/USTC-KnowledgeComputingLab/qmb/tree/dev/cpu-support-for-c-extension-comment-by-hzhangxyz 分支,照着这个分支的改动做就可以了,由于你的中间commit比较混乱,请squash成一个commit。 实现CPU版torch kernel这件事结束了,后续你可以做的优化:
|
windy-pig
force-pushed
the
dev/cpu-support-for-c-extension
branch
from
June 16, 2025 13:31
f7f7786 to
a1c6002
Compare
Modified `_hamiltonian_cuda.cu` to support CPU as `_hamiltonian_cpu.cpp`. The changes have been registered in `hamiltonian.py`.
hzhangxyz
force-pushed
the
dev/cpu-support-for-c-extension
branch
from
June 16, 2025 14:37
a1c6002 to
0ffbc5e
Compare
hzhangxyz
approved these changes
Jun 16, 2025
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
修改 _hamiltonian_cuda.cu,将gpu的代码转化为cpu的,从而提供对CPU运行的支持。
Checklist: