hamiltonian扩展条件编译 - #42
Conversation
hzhangxyz
left a comment
There was a problem hiding this comment.
大部分没啥问题,就是这个verify device有点丑。
| import torch.utils.cpp_extension | ||
|
|
||
|
|
||
| def verify_device(**tensors: torch.Tensor) -> str: |
There was a problem hiding this comment.
这个函数可以封装成 decorator。你要么封装成decorator,那样好看一些,要么别做这个检查,反正pytorch自己也会检查这个。
There was a problem hiding this comment.
如果要做的话,大概是这样:
def verify(func):
def wrapped_func(self, *args, **kwargs):
# check device here
...
func(*args, **kwargs)
return wrapped_func但不建议你这么做。
There was a problem hiding this comment.
Pull Request Overview
This PR extends the Hamiltonian module’s conditional compilation logic by adding device support and updating the caching mechanism.
- Added a device_type parameter to control CPU vs CUDA operator loading.
- Adjusted build configurations and caching to incorporate device differentiation.
| _hamiltonian_module: dict[tuple[int, str], object] = {} | ||
|
|
||
| @classmethod | ||
| def _load_module(cls, n_qubytes: int = 0, particle_cut: int = 0) -> object: | ||
| if n_qubytes not in cls._hamiltonian_module: | ||
| name = "qmb_hamiltonian" if n_qubytes == 0 else f"qmb_hamiltonian_{n_qubytes}_{particle_cut}" | ||
| def _load_module(cls, device_type: str = "cpu", n_qubytes: int = 0, particle_cut: int = 0) -> object: | ||
| if device_type not in {"cuda", "cpu"}: | ||
| raise RuntimeError("Unsupported device type") | ||
| if (n_qubytes, device_type) not in cls._hamiltonian_module: |
There was a problem hiding this comment.
The caching mechanism is based on n_qubytes and device_type, but the module name also depends on particle_cut. Consider including particle_cut in the caching key to prevent potential conflicts when particle_cut varies.
| """ | ||
| if configs_exclude is None: | ||
| configs_exclude = configs_i | ||
| device_type = configs_i.device.type |
| if n_qubytes not in cls._hamiltonian_module: | ||
| name = "qmb_hamiltonian" if n_qubytes == 0 else f"qmb_hamiltonian_{n_qubytes}_{particle_cut}" | ||
| def _load_module(cls, device_type: str = "cpu", n_qubytes: int = 0, particle_cut: int = 0) -> object: | ||
| if device_type not in {"cuda", "cpu"}: |
| cls._hamiltonian_module[n_qubytes] = torch.utils.cpp_extension.load( | ||
| if n_qubytes == 0: | ||
| ext_paths = [] | ||
| elif device_type == "cpu": |
There was a problem hiding this comment.
为了可读性,你可以这么干:
sources: list[str] = [f"{folder}/_hamiltonian.cpp"]
match (n_qubytes, device_type):
case 0, _:
pass
case _, "cpu":
sources.append(...)
case _, "cuda":
sources.append(...)
case _:
raise ValueError("Unsupported device")| """ | ||
|
|
||
| _hamiltonian_module: dict[int, object] = {} | ||
| _hamiltonian_module: dict[tuple[int, str], object] = {} |
There was a problem hiding this comment.
因为我平常碰到的particle_cut都是同一个值,所以当初这里没设置好,这里应该是 _hamiltonian_module: dict[tuple[str, int, int], object] = {} 才合适。
| def _load_module(cls, n_qubytes: int = 0, particle_cut: int = 0) -> object: | ||
| if n_qubytes not in cls._hamiltonian_module: | ||
| name = "qmb_hamiltonian" if n_qubytes == 0 else f"qmb_hamiltonian_{n_qubytes}_{particle_cut}" | ||
| def _load_module(cls, device_type: str = "cpu", n_qubytes: int = 0, particle_cut: int = 0) -> object: |
There was a problem hiding this comment.
之前没注意,这么写是不行的,_hamiltonian.cpp 里有pytorch算子的申明(不是c/cpp的申明),如果分成cpu和cuda会导致重复编译。所以需要切割成三个情况, declaration/cpu/cuda。如果device type不是declaration,则先编译declaration的版本。
| name = "qmb_hamiltonian" if n_qubytes == 0 else f"qmb_hamiltonian_{n_qubytes}_{particle_cut}" | ||
| def _load_module(cls, device_type: str = "cpu", n_qubytes: int = 0, particle_cut: int = 0) -> object: | ||
| if (n_qubytes, device_type) not in cls._hamiltonian_module: | ||
| name = "qmb_hamiltonian" if n_qubytes == 0 else f"qmb_hamiltonian_{n_qubytes}_{particle_cut}_{device_type}" |
There was a problem hiding this comment.
这个name的用处是python/pytorch模块名,而同一个算子是同一个模块的(废话),即使是适用于不同device的算子实现。
|
@windy-pig 有空看看那个comment分支,更新一下这个PR吧。 |
6d8b069 to
e9e444a
Compare
尝试修改hamiltonian.py,加入选择编译的功能。 删除verify_device,使用第一个传入tensor的设备类型。 按建议去除冗余混乱代码,优化可读性。
e9e444a to
1141a8e
Compare
Co-authored-by: Junkai Xu <michael17322021@163.com>
1141a8e to
49cb1b5
Compare
Description
修改hamiltonian.py的逻辑,判断并选择编译对应扩展。
Checklist: