Skip to content

hamiltonian扩展条件编译 - #42

Merged
hzhangxyz merged 1 commit into
mainfrom
dev/conditional-compilation
Jun 26, 2025
Merged

hamiltonian扩展条件编译#42
hzhangxyz merged 1 commit into
mainfrom
dev/conditional-compilation

Conversation

@windy-pig

Copy link
Copy Markdown
Collaborator

Description

修改hamiltonian.py的逻辑,判断并选择编译对应扩展。

Checklist:

@hzhangxyz hzhangxyz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

大部分没啥问题,就是这个verify device有点丑。

Comment thread qmb/hamiltonian.py Outdated
import torch.utils.cpp_extension


def verify_device(**tensors: torch.Tensor) -> str:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

这个函数可以封装成 decorator。你要么封装成decorator,那样好看一些,要么别做这个检查,反正pytorch自己也会检查这个。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

如果要做的话,大概是这样:

def verify(func):
   def wrapped_func(self, *args, **kwargs):
      # check device here
      ...
      func(*args, **kwargs)
   return wrapped_func

但不建议你这么做。

@hzhangxyz
hzhangxyz requested a review from Copilot June 17, 2025 08:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread qmb/hamiltonian.py Outdated
Comment on lines +17 to +21
_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:

Copilot AI Jun 17, 2025

Copy link

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.

@hzhangxyz hzhangxyz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

基本没啥问题,有一些小的建议。

Comment thread qmb/hamiltonian.py Outdated
"""
if configs_exclude is None:
configs_exclude = configs_i
device_type = configs_i.device.type

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

只用一次的变量,为啥要定义。。。

Comment thread qmb/hamiltonian.py Outdated
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"}:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

这个可以放在后面顺便检查。

Comment thread qmb/hamiltonian.py Outdated
cls._hamiltonian_module[n_qubytes] = torch.utils.cpp_extension.load(
if n_qubytes == 0:
ext_paths = []
elif device_type == "cpu":

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

为了可读性,你可以这么干:

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")

@hzhangxyz hzhangxyz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

你可以通过简单的测试脚本: qmb precompile hubbard -P4 -P1 -Dcpuqmb precompile hubbard -P4 -P1 -Dcuda来测试是否能编译/运行。

你参考 这个branch

Comment thread qmb/hamiltonian.py Outdated
"""

_hamiltonian_module: dict[int, object] = {}
_hamiltonian_module: dict[tuple[int, str], object] = {}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

因为我平常碰到的particle_cut都是同一个值,所以当初这里没设置好,这里应该是 _hamiltonian_module: dict[tuple[str, int, int], object] = {} 才合适。

Comment thread qmb/hamiltonian.py Outdated
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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

之前没注意,这么写是不行的,_hamiltonian.cpp 里有pytorch算子的申明(不是c/cpp的申明),如果分成cpu和cuda会导致重复编译。所以需要切割成三个情况, declaration/cpu/cuda。如果device type不是declaration,则先编译declaration的版本。

Comment thread qmb/hamiltonian.py Outdated
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}"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

这个name的用处是python/pytorch模块名,而同一个算子是同一个模块的(废话),即使是适用于不同device的算子实现。

@hzhangxyz

Copy link
Copy Markdown
Member

@windy-pig 有空看看那个comment分支,更新一下这个PR吧。

@hzhangxyz
hzhangxyz force-pushed the dev/conditional-compilation branch 2 times, most recently from 6d8b069 to e9e444a Compare June 26, 2025 04:03
hzhangxyz pushed a commit that referenced this pull request Jun 26, 2025
尝试修改hamiltonian.py,加入选择编译的功能。

删除verify_device,使用第一个传入tensor的设备类型。

按建议去除冗余混乱代码,优化可读性。
@hzhangxyz
hzhangxyz force-pushed the dev/conditional-compilation branch from e9e444a to 1141a8e Compare June 26, 2025 04:04
Co-authored-by: Junkai Xu <michael17322021@163.com>
@hzhangxyz
hzhangxyz force-pushed the dev/conditional-compilation branch from 1141a8e to 49cb1b5 Compare June 26, 2025 04:06
@hzhangxyz
hzhangxyz merged commit 5b23052 into main Jun 26, 2025
2 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants