Skip to content

Commit 9be45cd

Browse files
authored
Merge branch 'deepmodeling:master' into master
2 parents 311a620 + e679b8d commit 9be45cd

1 file changed

Lines changed: 29 additions & 16 deletions

File tree

deepmd/pt_expt/utils/comm.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,38 @@ def _check_underlying_ops_loaded() -> None:
5050
like DDP-spawned subprocesses that re-import modules from scratch
5151
and never see the test conftest's ``import deepmd.pt``.
5252
"""
53-
if not (
54-
hasattr(torch.ops, "deepmd_export")
55-
and hasattr(torch.ops.deepmd_export, "border_op")
56-
and hasattr(torch.ops.deepmd_export, "border_op_backward")
57-
):
53+
54+
def _ops_registered() -> bool:
55+
return (
56+
hasattr(torch.ops, "deepmd_export")
57+
and hasattr(torch.ops.deepmd_export, "border_op")
58+
and hasattr(torch.ops.deepmd_export, "border_op_backward")
59+
)
60+
61+
import_err: Exception | None = None
62+
if not _ops_registered():
5863
# Triggers cxx_op.py which torch.ops.load_library's the .so.
5964
try:
6065
import deepmd.pt # noqa: F401
61-
except Exception:
62-
# If deepmd.pt itself fails to import, fall through to the
63-
# explicit RuntimeError below — clearer than re-raising a
64-
# potentially-unrelated import error.
65-
pass
66-
67-
if not (
68-
hasattr(torch.ops, "deepmd_export")
69-
and hasattr(torch.ops.deepmd_export, "border_op")
70-
and hasattr(torch.ops.deepmd_export, "border_op_backward")
71-
):
66+
except Exception as exc:
67+
# ``deepmd/pt/__init__.py`` loads ``cxx_op`` (which registers
68+
# the ops) before running ``load_entry_point("deepmd.pt")``.
69+
# A broken third-party entry point can make the import raise
70+
# *after* the ops were already registered, so only re-raise
71+
# when the registration is still missing — that branch is the
72+
# one where the error (typically an ``undefined symbol`` ABI
73+
# mismatch against libdeepmd_op_pt.so) carries the diagnostic
74+
# detail that the generic RuntimeError below would hide.
75+
import_err = exc
76+
77+
if not _ops_registered():
78+
if import_err is not None:
79+
# Surface the raw import error (typically ``ImportError`` with
80+
# ``undefined symbol`` ABI detail) instead of burying it in a
81+
# generic message — that detail is what tells the user the
82+
# mismatch is between libdeepmd_op_pt.so and the runtime torch,
83+
# not a missing build.
84+
raise import_err
7285
raise RuntimeError(
7386
"torch.ops.deepmd_export.{border_op,border_op_backward} "
7487
"are not registered. Build libdeepmd_op_pt.so and ensure "

0 commit comments

Comments
 (0)