You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(pt,pd): remove redundant tensor handling to eliminate tensor construction warnings (#4907)
This PR fixes deprecation warnings that occur when `torch.tensor()` or
`paddle.to_tensor()` is called on existing tensor objects:
**PyTorch warning:**
```
UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
```
**PaddlePaddle warning:**
```
UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach(), rather than paddle.to_tensor(sourceTensor).
```
## Root Cause
The warnings were being triggered in multiple locations:
1. **PyTorch**: Test cases were passing tensor objects directly to ASE
calculators, which internally convert them using `torch.tensor()`
2. **PaddlePaddle**: Similar issues in `eval_model` function and
`to_paddle_tensor` utility, plus a TypeError where `tensor.to()` method
was incorrectly using `place=` instead of `device=`
## Solution
**For PyTorch:**
- Modified test cases to convert tensor inputs to numpy arrays before
passing to ASE calculators
- Removed redundant tensor handling in `to_torch_tensor` utility
function since the non-numpy check already handles tensors by returning
them as-is
**For PaddlePaddle:**
- Added proper type checking in `eval_model` function to handle existing
tensors with `clone().detach()`
- Removed redundant tensor handling in `to_paddle_tensor` utility
function, applying the same optimization as PyTorch
- Fixed TypeError by changing `place=` to `device=` in all `tensor.to()`
method calls (PaddlePaddle's tensor `.to()` method expects `device=`
parameter, while `paddle.to_tensor()` correctly uses `place=`)
## Changes Made
1. **`source/tests/pt/test_calculator.py`**: Fixed `TestCalculator` and
`TestCalculatorWithFparamAparam` to convert PyTorch tensors to numpy
arrays before passing to ASE calculator
2. **`deepmd/pt/utils/utils.py`**: Removed redundant tensor-specific
handling in `to_torch_tensor` function
3. **`source/tests/pd/common.py`**: Updated `eval_model` function with
type checking for PaddlePaddle tensors and fixed `tensor.to()` method
calls to use `device=` instead of `place=`
4. **`deepmd/pd/utils/utils.py`**: Removed redundant tensor-specific
handling in `to_paddle_tensor` function for consistency with PyTorch
Both utility functions now use a simplified approach where the `if not
isinstance(xx, np.ndarray): return xx` check handles all non-numpy
inputs (including tensors) by returning them unchanged, eliminating the
need for separate tensor-specific code paths.
This change is backward compatible and maintains the same functionality
while eliminating both deprecation warnings and TypeErrors, improving
code consistency between PyTorch and PaddlePaddle backends.
Fixes#3790.
<!-- START COPILOT CODING AGENT TIPS -->
---
💡 You can make Copilot smarter by setting up custom instructions,
customizing its development environment and configuring Model Context
Protocol (MCP) servers. Learn more [Copilot coding agent
tips](https://gh.io/copilot-coding-agent-tips) in the docs.
---------
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: njzjz <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
0 commit comments