2323 annotations ,
2424)
2525
26+ import contextlib
2627import ctypes
2728import json
2829import logging
3334 deepcopy ,
3435)
3536from typing import (
37+ TYPE_CHECKING ,
3638 Any ,
3739)
3840
3941import numpy as np
4042import torch
4143
44+ if TYPE_CHECKING :
45+ from collections .abc import (
46+ Iterator ,
47+ )
48+
4249from deepmd .dpmodel .utils .nlist import (
4350 build_neighbor_list ,
4451 extend_coord_with_ghosts ,
@@ -857,6 +864,7 @@ def _export_with_comm_artifact(
857864# it defaults to exact float32.
858865_FREEZE_KERNEL_LEVELS = {"DP_TRITON_INFER" : "2" , "DP_CUDA_INFER" : "1" }
859866_FREEZE_DISABLED_LEVELS = {"DP_CUTILE_INFER" : "0" , "DP_CUTE_INFER" : "0" }
867+ _INFER_KERNEL_LEVELS = tuple (_FREEZE_KERNEL_LEVELS | _FREEZE_DISABLED_LEVELS )
860868
861869
862870def _apply_kernel_level_defaults (target_device : torch .device ) -> None :
@@ -869,12 +877,7 @@ def _apply_kernel_level_defaults(target_device: torch.device) -> None:
869877 Python-only eager backends and are disabled for every frozen archive.
870878 """
871879 if target_device .type != "cuda" :
872- for name in (
873- "DP_TRITON_INFER" ,
874- "DP_CUDA_INFER" ,
875- "DP_CUTILE_INFER" ,
876- "DP_CUTE_INFER" ,
877- ):
880+ for name in _INFER_KERNEL_LEVELS :
878881 os .environ [name ] = "0"
879882 log .info ("Freezing for CPU with accelerator-only DPA4 paths disabled." )
880883 return
@@ -892,6 +895,21 @@ def _apply_kernel_level_defaults(target_device: torch.device) -> None:
892895 )
893896
894897
898+ @contextlib .contextmanager
899+ def _kernel_level_defaults (target_device : torch .device ) -> Iterator [None ]:
900+ """Apply freeze-time kernel levels without changing the caller's environment."""
901+ saved = {name : os .environ .get (name ) for name in _INFER_KERNEL_LEVELS }
902+ try :
903+ _apply_kernel_level_defaults (target_device )
904+ yield
905+ finally :
906+ for name , value in saved .items ():
907+ if value is None :
908+ os .environ .pop (name , None )
909+ else :
910+ os .environ [name ] = value
911+
912+
895913def freeze_sezm_to_pt2 (
896914 ckpt_path : str ,
897915 out_path : str ,
@@ -929,14 +947,31 @@ def freeze_sezm_to_pt2(
929947 are ``DP_TRITON_INFER=2`` and ``DP_CUDA_INFER=1``, which is the fastest
930948 combination that keeps every operator in exact float32.
931949 """
950+ target_device = device if device is not None else DEVICE
951+ with _kernel_level_defaults (target_device ):
952+ _freeze_sezm_to_pt2 (
953+ ckpt_path ,
954+ out_path ,
955+ target_device = target_device ,
956+ head = head ,
957+ atomic_virial = atomic_virial ,
958+ )
959+
960+
961+ def _freeze_sezm_to_pt2 (
962+ ckpt_path : str ,
963+ out_path : str ,
964+ * ,
965+ target_device : torch .device ,
966+ head : str | None ,
967+ atomic_virial : bool ,
968+ ) -> None :
969+ """Build one AOTInductor archive under an established kernel policy."""
932970 from torch ._inductor import (
933971 aoti_compile_and_package ,
934972 )
935973 from torch ._inductor import config as inductor_config
936974
937- target_device = device if device is not None else DEVICE
938- _apply_kernel_level_defaults (target_device )
939-
940975 raw = torch .load (ckpt_path , map_location = "cpu" , weights_only = False )
941976 state_dict , params = _extract_state_and_params (raw )
942977 state_dict , params = _select_model_head (state_dict , params , head )
0 commit comments