From 7492ef6915cec5caa9596b2922532e98e0c6f3d3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 23 Aug 2025 06:39:32 +0000 Subject: [PATCH 1/7] Initial plan From afe7af53cf2881eae000893ec53665a6bbacd7c6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 23 Aug 2025 06:46:47 +0000 Subject: [PATCH 2/7] Fix torch.tensor warnings by using clone().detach() for existing tensors Co-authored-by: njzjz <9496702+njzjz@users.noreply.github.com> --- deepmd/pt/utils/utils.py | 3 +++ source/tests/pt/common.py | 41 ++++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/deepmd/pt/utils/utils.py b/deepmd/pt/utils/utils.py index 054dc3c80b..b8f07979d4 100644 --- a/deepmd/pt/utils/utils.py +++ b/deepmd/pt/utils/utils.py @@ -244,6 +244,9 @@ def to_torch_tensor( if xx is None: return None assert xx is not None + # Handle PyTorch tensors - clone and move to target device/dtype if needed + if isinstance(xx, torch.Tensor): + return xx.clone().detach().to(device=DEVICE) if not isinstance(xx, np.ndarray): return xx # Create a reverse mapping of NP_PRECISION_DICT diff --git a/source/tests/pt/common.py b/source/tests/pt/common.py index 8709c8b4f9..215acbfd12 100644 --- a/source/tests/pt/common.py +++ b/source/tests/pt/common.py @@ -79,7 +79,10 @@ def eval_model( if spins is not None: assert isinstance(spins, torch.Tensor), err_msg assert isinstance(atom_types, torch.Tensor) or isinstance(atom_types, list) - atom_types = torch.tensor(atom_types, dtype=torch.int32, device=DEVICE) + if isinstance(atom_types, torch.Tensor): + atom_types = atom_types.clone().detach().to(dtype=torch.int32, device=DEVICE) + else: + atom_types = torch.tensor(atom_types, dtype=torch.int32, device=DEVICE) elif isinstance(coords, np.ndarray): if cells is not None: assert isinstance(cells, np.ndarray), err_msg @@ -101,28 +104,40 @@ def eval_model( else: natoms = len(atom_types[0]) - coord_input = torch.tensor( - coords.reshape([-1, natoms, 3]), dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE - ) + if isinstance(coords, torch.Tensor): + coord_input = coords.reshape([-1, natoms, 3]).clone().detach().to(dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE) + else: + coord_input = torch.tensor( + coords.reshape([-1, natoms, 3]), dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE + ) spin_input = None if spins is not None: - spin_input = torch.tensor( - spins.reshape([-1, natoms, 3]), - dtype=GLOBAL_PT_FLOAT_PRECISION, - device=DEVICE, - ) + if isinstance(spins, torch.Tensor): + spin_input = spins.reshape([-1, natoms, 3]).clone().detach().to(dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE) + else: + spin_input = torch.tensor( + spins.reshape([-1, natoms, 3]), + dtype=GLOBAL_PT_FLOAT_PRECISION, + device=DEVICE, + ) has_spin = getattr(model, "has_spin", False) if callable(has_spin): has_spin = has_spin() - type_input = torch.tensor(atom_types, dtype=torch.long, device=DEVICE) + if isinstance(atom_types, torch.Tensor): + type_input = atom_types.clone().detach().to(dtype=torch.long, device=DEVICE) + else: + type_input = torch.tensor(atom_types, dtype=torch.long, device=DEVICE) box_input = None if cells is None: pbc = False else: pbc = True - box_input = torch.tensor( - cells.reshape([-1, 3, 3]), dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE - ) + if isinstance(cells, torch.Tensor): + box_input = cells.reshape([-1, 3, 3]).clone().detach().to(dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE) + else: + box_input = torch.tensor( + cells.reshape([-1, 3, 3]), dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE + ) num_iter = int((nframes + infer_batch_size - 1) / infer_batch_size) for ii in range(num_iter): From 5eade9b29954edb87f9affca317a620da4b77062 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 23 Aug 2025 07:53:06 +0000 Subject: [PATCH 3/7] Fix PaddlePaddle tensor construction warnings and enhance PyTorch to_torch_tensor function Co-authored-by: njzjz <9496702+njzjz@users.noreply.github.com> --- deepmd/pd/utils/utils.py | 3 +++ source/tests/pd/common.py | 41 ++++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/deepmd/pd/utils/utils.py b/deepmd/pd/utils/utils.py index 175ac5019b..372f8c5ef6 100644 --- a/deepmd/pd/utils/utils.py +++ b/deepmd/pd/utils/utils.py @@ -267,6 +267,9 @@ def to_paddle_tensor( if xx is None: return None assert xx is not None + # Handle PaddlePaddle tensors - clone and move to target device/dtype if needed + if isinstance(xx, paddle.Tensor): + return xx.clone().detach().to(device=DEVICE) if not isinstance(xx, np.ndarray): return xx # Create a reverse mapping of NP_PRECISION_DICT diff --git a/source/tests/pd/common.py b/source/tests/pd/common.py index d73544c5f1..dc4b26f771 100644 --- a/source/tests/pd/common.py +++ b/source/tests/pd/common.py @@ -79,7 +79,10 @@ def eval_model( if spins is not None: assert isinstance(spins, paddle.Tensor), err_msg assert isinstance(atom_types, paddle.Tensor) or isinstance(atom_types, list) - atom_types = paddle.to_tensor(atom_types, dtype=paddle.int32, place=DEVICE) + if isinstance(atom_types, paddle.Tensor): + atom_types = atom_types.clone().detach().to(dtype=paddle.int32, place=DEVICE) + else: + atom_types = paddle.to_tensor(atom_types, dtype=paddle.int32, place=DEVICE) elif isinstance(coords, np.ndarray): if cells is not None: assert isinstance(cells, np.ndarray), err_msg @@ -101,28 +104,40 @@ def eval_model( else: natoms = len(atom_types[0]) - coord_input = paddle.to_tensor( - coords.reshape([-1, natoms, 3]), dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE - ) + if isinstance(coords, paddle.Tensor): + coord_input = coords.reshape([-1, natoms, 3]).clone().detach().to(dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE) + else: + coord_input = paddle.to_tensor( + coords.reshape([-1, natoms, 3]), dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE + ) spin_input = None if spins is not None: - spin_input = paddle.to_tensor( - spins.reshape([-1, natoms, 3]), - dtype=GLOBAL_PD_FLOAT_PRECISION, - place=DEVICE, - ) + if isinstance(spins, paddle.Tensor): + spin_input = spins.reshape([-1, natoms, 3]).clone().detach().to(dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE) + else: + spin_input = paddle.to_tensor( + spins.reshape([-1, natoms, 3]), + dtype=GLOBAL_PD_FLOAT_PRECISION, + place=DEVICE, + ) has_spin = getattr(model, "has_spin", False) if callable(has_spin): has_spin = has_spin() - type_input = paddle.to_tensor(atom_types, dtype=paddle.int64, place=DEVICE) + if isinstance(atom_types, paddle.Tensor): + type_input = atom_types.clone().detach().to(dtype=paddle.int64, place=DEVICE) + else: + type_input = paddle.to_tensor(atom_types, dtype=paddle.int64, place=DEVICE) box_input = None if cells is None: pbc = False else: pbc = True - box_input = paddle.to_tensor( - cells.reshape([-1, 3, 3]), dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE - ) + if isinstance(cells, paddle.Tensor): + box_input = cells.reshape([-1, 3, 3]).clone().detach().to(dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE) + else: + box_input = paddle.to_tensor( + cells.reshape([-1, 3, 3]), dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE + ) num_iter = int((nframes + infer_batch_size - 1) / infer_batch_size) for ii in range(num_iter): From a5536ab28022917e247f4b2270b3b4ae4d186f1b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 23 Aug 2025 08:48:32 +0000 Subject: [PATCH 4/7] Remove redundant tensor handling in to_torch_tensor and fix test tensor usage - Removed lines 247-249 in to_torch_tensor as they were redundant - line 250-251 already handle non-numpy inputs - Fixed TestCalculator and TestCalculatorWithFparamAparam to convert PyTorch tensors to numpy arrays before passing to ASE calculator - This prevents tensor construction warnings by avoiding torch.tensor() calls on existing tensors Co-authored-by: njzjz <9496702+njzjz@users.noreply.github.com> --- deepmd/pt/utils/utils.py | 3 --- source/tests/pt/test_calculator.py | 28 ++++++++++++++++++---------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/deepmd/pt/utils/utils.py b/deepmd/pt/utils/utils.py index b8f07979d4..054dc3c80b 100644 --- a/deepmd/pt/utils/utils.py +++ b/deepmd/pt/utils/utils.py @@ -244,9 +244,6 @@ def to_torch_tensor( if xx is None: return None assert xx is not None - # Handle PyTorch tensors - clone and move to target device/dtype if needed - if isinstance(xx, torch.Tensor): - return xx.clone().detach().to(device=DEVICE) if not isinstance(xx, np.ndarray): return xx # Create a reverse mapping of NP_PRECISION_DICT diff --git a/source/tests/pt/test_calculator.py b/source/tests/pt/test_calculator.py index 860a161fbd..7458117ca3 100644 --- a/source/tests/pt/test_calculator.py +++ b/source/tests/pt/test_calculator.py @@ -64,14 +64,18 @@ def test_calculator(self) -> None: atomic_numbers = [1, 1, 1, 8, 8] idx_perm = [1, 0, 4, 3, 2] + # Convert tensors to numpy for ASE compatibility + cell_np = cell.numpy() + coord_np = coord.numpy() + prec = 1e-10 low_prec = 1e-4 ase_atoms0 = Atoms( numbers=atomic_numbers, - positions=coord, + positions=coord_np, # positions=[tuple(item) for item in coordinate], - cell=cell, + cell=cell_np, calculator=self.calculator, pbc=True, ) @@ -83,9 +87,9 @@ def test_calculator(self) -> None: ase_atoms1 = Atoms( numbers=[atomic_numbers[i] for i in idx_perm], - positions=coord[idx_perm, :], + positions=coord_np[idx_perm, :], # positions=[tuple(item) for item in coordinate], - cell=cell, + cell=cell_np, calculator=self.calculator, pbc=True, ) @@ -141,19 +145,23 @@ def test_calculator(self) -> None: generator = torch.Generator(device="cpu").manual_seed(GLOBAL_SEED) coord = torch.rand([natoms, 3], dtype=dtype, device="cpu", generator=generator) coord = torch.matmul(coord, cell) - fparam = torch.IntTensor([1, 2]) - aparam = torch.IntTensor([[1], [0], [2], [1], [0]]) + fparam = torch.IntTensor([1, 2]).numpy() + aparam = torch.IntTensor([[1], [0], [2], [1], [0]]).numpy() atomic_numbers = [1, 1, 1, 8, 8] idx_perm = [1, 0, 4, 3, 2] + # Convert tensors to numpy for ASE compatibility + cell_np = cell.numpy() + coord_np = coord.numpy() + prec = 1e-10 low_prec = 1e-4 ase_atoms0 = Atoms( numbers=atomic_numbers, - positions=coord, + positions=coord_np, # positions=[tuple(item) for item in coordinate], - cell=cell, + cell=cell_np, calculator=self.calculator, pbc=True, ) @@ -166,9 +174,9 @@ def test_calculator(self) -> None: ase_atoms1 = Atoms( numbers=[atomic_numbers[i] for i in idx_perm], - positions=coord[idx_perm, :], + positions=coord_np[idx_perm, :], # positions=[tuple(item) for item in coordinate], - cell=cell, + cell=cell_np, calculator=self.calculator, pbc=True, ) From 0e9ae5152871b9cd5a25e17f381523faaa03a857 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 23 Aug 2025 08:54:58 +0000 Subject: [PATCH 5/7] Remove redundant tensor handling in to_paddle_tensor function Co-authored-by: njzjz <9496702+njzjz@users.noreply.github.com> --- deepmd/pd/utils/utils.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/deepmd/pd/utils/utils.py b/deepmd/pd/utils/utils.py index 372f8c5ef6..175ac5019b 100644 --- a/deepmd/pd/utils/utils.py +++ b/deepmd/pd/utils/utils.py @@ -267,9 +267,6 @@ def to_paddle_tensor( if xx is None: return None assert xx is not None - # Handle PaddlePaddle tensors - clone and move to target device/dtype if needed - if isinstance(xx, paddle.Tensor): - return xx.clone().detach().to(device=DEVICE) if not isinstance(xx, np.ndarray): return xx # Create a reverse mapping of NP_PRECISION_DICT From 4f4bd3f898d29031f91539f285fbb0180b720764 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 23 Aug 2025 08:58:07 +0000 Subject: [PATCH 6/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- source/tests/pd/common.py | 29 ++++++++++++++++++++++++----- source/tests/pt/common.py | 33 +++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/source/tests/pd/common.py b/source/tests/pd/common.py index dc4b26f771..e3812f2ca1 100644 --- a/source/tests/pd/common.py +++ b/source/tests/pd/common.py @@ -80,7 +80,9 @@ def eval_model( assert isinstance(spins, paddle.Tensor), err_msg assert isinstance(atom_types, paddle.Tensor) or isinstance(atom_types, list) if isinstance(atom_types, paddle.Tensor): - atom_types = atom_types.clone().detach().to(dtype=paddle.int32, place=DEVICE) + atom_types = ( + atom_types.clone().detach().to(dtype=paddle.int32, place=DEVICE) + ) else: atom_types = paddle.to_tensor(atom_types, dtype=paddle.int32, place=DEVICE) elif isinstance(coords, np.ndarray): @@ -105,15 +107,27 @@ def eval_model( natoms = len(atom_types[0]) if isinstance(coords, paddle.Tensor): - coord_input = coords.reshape([-1, natoms, 3]).clone().detach().to(dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE) + coord_input = ( + coords.reshape([-1, natoms, 3]) + .clone() + .detach() + .to(dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE) + ) else: coord_input = paddle.to_tensor( - coords.reshape([-1, natoms, 3]), dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE + coords.reshape([-1, natoms, 3]), + dtype=GLOBAL_PD_FLOAT_PRECISION, + place=DEVICE, ) spin_input = None if spins is not None: if isinstance(spins, paddle.Tensor): - spin_input = spins.reshape([-1, natoms, 3]).clone().detach().to(dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE) + spin_input = ( + spins.reshape([-1, natoms, 3]) + .clone() + .detach() + .to(dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE) + ) else: spin_input = paddle.to_tensor( spins.reshape([-1, natoms, 3]), @@ -133,7 +147,12 @@ def eval_model( else: pbc = True if isinstance(cells, paddle.Tensor): - box_input = cells.reshape([-1, 3, 3]).clone().detach().to(dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE) + box_input = ( + cells.reshape([-1, 3, 3]) + .clone() + .detach() + .to(dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE) + ) else: box_input = paddle.to_tensor( cells.reshape([-1, 3, 3]), dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE diff --git a/source/tests/pt/common.py b/source/tests/pt/common.py index 215acbfd12..2dbfdb84ff 100644 --- a/source/tests/pt/common.py +++ b/source/tests/pt/common.py @@ -80,7 +80,9 @@ def eval_model( assert isinstance(spins, torch.Tensor), err_msg assert isinstance(atom_types, torch.Tensor) or isinstance(atom_types, list) if isinstance(atom_types, torch.Tensor): - atom_types = atom_types.clone().detach().to(dtype=torch.int32, device=DEVICE) + atom_types = ( + atom_types.clone().detach().to(dtype=torch.int32, device=DEVICE) + ) else: atom_types = torch.tensor(atom_types, dtype=torch.int32, device=DEVICE) elif isinstance(coords, np.ndarray): @@ -105,15 +107,27 @@ def eval_model( natoms = len(atom_types[0]) if isinstance(coords, torch.Tensor): - coord_input = coords.reshape([-1, natoms, 3]).clone().detach().to(dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE) + coord_input = ( + coords.reshape([-1, natoms, 3]) + .clone() + .detach() + .to(dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE) + ) else: coord_input = torch.tensor( - coords.reshape([-1, natoms, 3]), dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE + coords.reshape([-1, natoms, 3]), + dtype=GLOBAL_PT_FLOAT_PRECISION, + device=DEVICE, ) spin_input = None if spins is not None: if isinstance(spins, torch.Tensor): - spin_input = spins.reshape([-1, natoms, 3]).clone().detach().to(dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE) + spin_input = ( + spins.reshape([-1, natoms, 3]) + .clone() + .detach() + .to(dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE) + ) else: spin_input = torch.tensor( spins.reshape([-1, natoms, 3]), @@ -133,10 +147,17 @@ def eval_model( else: pbc = True if isinstance(cells, torch.Tensor): - box_input = cells.reshape([-1, 3, 3]).clone().detach().to(dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE) + box_input = ( + cells.reshape([-1, 3, 3]) + .clone() + .detach() + .to(dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE) + ) else: box_input = torch.tensor( - cells.reshape([-1, 3, 3]), dtype=GLOBAL_PT_FLOAT_PRECISION, device=DEVICE + cells.reshape([-1, 3, 3]), + dtype=GLOBAL_PT_FLOAT_PRECISION, + device=DEVICE, ) num_iter = int((nframes + infer_batch_size - 1) / infer_batch_size) From a42db4322427aae6b2ad015a17ad6420a201eae2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 23 Aug 2025 09:54:24 +0000 Subject: [PATCH 7/7] Fix TypeError: change place= to device= in tensor.to() method calls Co-authored-by: njzjz <9496702+njzjz@users.noreply.github.com> --- source/tests/pd/common.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/source/tests/pd/common.py b/source/tests/pd/common.py index e3812f2ca1..ec36fd0eb9 100644 --- a/source/tests/pd/common.py +++ b/source/tests/pd/common.py @@ -81,7 +81,7 @@ def eval_model( assert isinstance(atom_types, paddle.Tensor) or isinstance(atom_types, list) if isinstance(atom_types, paddle.Tensor): atom_types = ( - atom_types.clone().detach().to(dtype=paddle.int32, place=DEVICE) + atom_types.clone().detach().to(dtype=paddle.int32, device=DEVICE) ) else: atom_types = paddle.to_tensor(atom_types, dtype=paddle.int32, place=DEVICE) @@ -111,7 +111,7 @@ def eval_model( coords.reshape([-1, natoms, 3]) .clone() .detach() - .to(dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE) + .to(dtype=GLOBAL_PD_FLOAT_PRECISION, device=DEVICE) ) else: coord_input = paddle.to_tensor( @@ -126,7 +126,7 @@ def eval_model( spins.reshape([-1, natoms, 3]) .clone() .detach() - .to(dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE) + .to(dtype=GLOBAL_PD_FLOAT_PRECISION, device=DEVICE) ) else: spin_input = paddle.to_tensor( @@ -138,7 +138,7 @@ def eval_model( if callable(has_spin): has_spin = has_spin() if isinstance(atom_types, paddle.Tensor): - type_input = atom_types.clone().detach().to(dtype=paddle.int64, place=DEVICE) + type_input = atom_types.clone().detach().to(dtype=paddle.int64, device=DEVICE) else: type_input = paddle.to_tensor(atom_types, dtype=paddle.int64, place=DEVICE) box_input = None @@ -151,7 +151,7 @@ def eval_model( cells.reshape([-1, 3, 3]) .clone() .detach() - .to(dtype=GLOBAL_PD_FLOAT_PRECISION, place=DEVICE) + .to(dtype=GLOBAL_PD_FLOAT_PRECISION, device=DEVICE) ) else: box_input = paddle.to_tensor(