Skip to content

Commit 1ea384c

Browse files
author
njzjz-bot
committed
test(dpa4): simplify backend imports
Restore the established eager conditional-import pattern for the PyTorch backends while retaining the array-api-strict coverage.\n\nCoding-Agent: Codex\nCodex-Version: codex-cli 0.144.1\nModel: gpt-5.6-sol\nReasoning-Effort: xhigh
1 parent a5b9a6d commit 1ea384c

2 files changed

Lines changed: 30 additions & 101 deletions

File tree

source/tests/consistent/descriptor/test_dpa4.py

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,14 @@
2929
DescriptorTest,
3030
)
3131

32-
DescrptDPA4PT = None
33-
DescrptDPA4PTExpt = None
34-
35-
36-
def _get_descrpt_dpa4_pt() -> type | None:
37-
global DescrptDPA4PT
38-
if not INSTALLED_PT:
39-
return None
40-
if DescrptDPA4PT is None:
41-
from deepmd.pt.model.descriptor.sezm import (
42-
DescrptSeZM,
43-
)
44-
45-
DescrptDPA4PT = DescrptSeZM
46-
return DescrptDPA4PT
47-
48-
49-
def _get_descrpt_dpa4_pt_expt() -> type | None:
50-
global DescrptDPA4PTExpt
51-
if not INSTALLED_PT_EXPT:
52-
return None
53-
if DescrptDPA4PTExpt is None:
54-
from deepmd.pt_expt.descriptor.dpa4 import (
55-
DescrptDPA4,
56-
)
57-
58-
DescrptDPA4PTExpt = DescrptDPA4
59-
return DescrptDPA4PTExpt
60-
61-
32+
if INSTALLED_PT:
33+
from deepmd.pt.model.descriptor.sezm import DescrptSeZM as DescrptDPA4PT
34+
else:
35+
DescrptDPA4PT = None
36+
if INSTALLED_PT_EXPT:
37+
from deepmd.pt_expt.descriptor.dpa4 import DescrptDPA4 as DescrptDPA4PTExpt
38+
else:
39+
DescrptDPA4PTExpt = None
6240
if INSTALLED_ARRAY_API_STRICT:
6341
from ...array_api_strict.descriptor.dpa4 import DescrptDPA4 as DescrptDPA4Strict
6442
else:
@@ -173,28 +151,19 @@ def data(self) -> dict:
173151

174152
@property
175153
def skip_pt(self) -> bool:
176-
return CommonTest.skip_pt or _get_descrpt_dpa4_pt() is None
177-
178-
@property
179-
def skip_pt_expt(self) -> bool:
180-
return not INSTALLED_PT_EXPT or _get_descrpt_dpa4_pt_expt() is None
181-
182-
@property
183-
def pt_class(self) -> type | None:
184-
return _get_descrpt_dpa4_pt()
185-
186-
@property
187-
def pt_expt_class(self) -> type | None:
188-
return _get_descrpt_dpa4_pt_expt()
154+
return CommonTest.skip_pt
189155

190156
skip_dp = False
191157
skip_tf = True
192158
skip_jax = True
193159
skip_pd = True
160+
skip_pt_expt = not INSTALLED_PT_EXPT
194161
skip_array_api_strict = not INSTALLED_ARRAY_API_STRICT
195162

196163
tf_class = DescrptDPA4TF
197164
dp_class = DescrptDPA4DP
165+
pt_class = DescrptDPA4PT
166+
pt_expt_class = DescrptDPA4PTExpt
198167
jax_class = None
199168
pd_class = None
200169
array_api_strict_class = DescrptDPA4Strict

source/tests/consistent/fitting/test_dpa4_ener.py

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -28,53 +28,22 @@
2828
FittingTest,
2929
)
3030

31-
torch = None
32-
PT_DEVICE = None
33-
PT_EXPT_DEVICE = None
34-
SeZMEnerFittingPT = None
35-
SeZMEnerFittingPTExpt = None
36-
37-
38-
def _get_sezm_ener_fitting_pt() -> type | None:
39-
global PT_DEVICE, SeZMEnerFittingPT, torch
40-
if not INSTALLED_PT:
41-
return None
42-
if SeZMEnerFittingPT is None:
43-
import torch as torch_module
44-
45-
from deepmd.pt.model.task.sezm_ener import (
46-
SeZMEnergyFittingNet,
47-
)
48-
from deepmd.pt.utils.env import (
49-
DEVICE,
50-
)
51-
52-
torch = torch_module
53-
PT_DEVICE = DEVICE
54-
SeZMEnerFittingPT = SeZMEnergyFittingNet
55-
return SeZMEnerFittingPT
56-
57-
58-
def _get_sezm_ener_fitting_pt_expt() -> type | None:
59-
global PT_EXPT_DEVICE, SeZMEnerFittingPTExpt, torch
60-
if not INSTALLED_PT_EXPT:
61-
return None
62-
if SeZMEnerFittingPTExpt is None:
63-
import torch as torch_module
64-
65-
from deepmd.pt_expt.fitting.dpa4_ener import (
66-
SeZMEnergyFittingNet,
67-
)
68-
from deepmd.pt_expt.utils.env import (
69-
DEVICE,
70-
)
71-
72-
torch = torch_module
73-
PT_EXPT_DEVICE = DEVICE
74-
SeZMEnerFittingPTExpt = SeZMEnergyFittingNet
75-
return SeZMEnerFittingPTExpt
31+
if INSTALLED_PT:
32+
import torch
7633

34+
from deepmd.pt.model.task.sezm_ener import SeZMEnergyFittingNet as SeZMEnerFittingPT
35+
from deepmd.pt.utils.env import DEVICE as PT_DEVICE
36+
else:
37+
SeZMEnerFittingPT = None
38+
if INSTALLED_PT_EXPT:
39+
import torch
7740

41+
from deepmd.pt_expt.fitting.dpa4_ener import (
42+
SeZMEnergyFittingNet as SeZMEnerFittingPTExpt,
43+
)
44+
from deepmd.pt_expt.utils.env import DEVICE as PT_EXPT_DEVICE
45+
else:
46+
SeZMEnerFittingPTExpt = None
7847
if INSTALLED_ARRAY_API_STRICT:
7948
import array_api_strict
8049

@@ -113,28 +82,19 @@ def data(self) -> dict:
11382

11483
@property
11584
def skip_pt(self) -> bool:
116-
return CommonTest.skip_pt or _get_sezm_ener_fitting_pt() is None
117-
118-
@property
119-
def skip_pt_expt(self) -> bool:
120-
return not INSTALLED_PT_EXPT or _get_sezm_ener_fitting_pt_expt() is None
121-
122-
@property
123-
def pt_class(self) -> type | None:
124-
return _get_sezm_ener_fitting_pt()
125-
126-
@property
127-
def pt_expt_class(self) -> type | None:
128-
return _get_sezm_ener_fitting_pt_expt()
85+
return CommonTest.skip_pt
12986

13087
skip_dp = False
13188
skip_tf = True
13289
skip_jax = True
13390
skip_pd = True
91+
skip_pt_expt = not INSTALLED_PT_EXPT
13492
skip_array_api_strict = not INSTALLED_ARRAY_API_STRICT
13593

13694
tf_class = SeZMEnerFittingTF
13795
dp_class = SeZMEnerFittingDP
96+
pt_class = SeZMEnerFittingPT
97+
pt_expt_class = SeZMEnerFittingPTExpt
13898
jax_class = None
13999
pd_class = None
140100
array_api_strict_class = SeZMEnerFittingStrict

0 commit comments

Comments
 (0)