Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Small CP2K wrapper around the SKALA TorchScript functional protocol.
10 : ! **************************************************************************************************
11 : MODULE skala_torch_api
12 : #if defined (__HAS_IEEE_EXCEPTIONS)
13 : USE ieee_exceptions, ONLY: ieee_all, &
14 : ieee_get_halting_mode, &
15 : ieee_set_halting_mode
16 : #endif
17 : USE kinds, ONLY: default_string_length, &
18 : dp
19 : USE string_utilities, ONLY: uppercase
20 : USE torch_api, ONLY: &
21 : torch_dict_type, torch_model_disable_parameter_gradients, torch_model_forward_mol_tensor, &
22 : torch_model_freeze_preserving_method, torch_model_load_with_metadata, torch_model_release, &
23 : torch_model_remap_device_constants, torch_model_promote_float32, torch_model_type, &
24 : torch_model_prepare_higher_derivatives, &
25 : torch_tensor_item_double, torch_tensor_release, torch_tensor_type, &
26 : torch_tensor_weighted_sum
27 : #include "../base/base_uses.f90"
28 :
29 : IMPLICIT NONE
30 :
31 : PRIVATE
32 :
33 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'skala_torch_api'
34 :
35 : PUBLIC :: skala_torch_model_type, skala_torch_model_load, skala_torch_model_release
36 : PUBLIC :: skala_torch_model_get_exc, skala_torch_model_get_exc_density
37 : PUBLIC :: skala_torch_model_needs_feature, skala_torch_model_protocol_version
38 :
39 : TYPE skala_torch_model_type
40 : PRIVATE
41 : INTEGER :: protocol_version = -1
42 : CHARACTER(len=default_string_length), ALLOCATABLE, &
43 : DIMENSION(:) :: features
44 : TYPE(torch_model_type) :: torch_model
45 : END TYPE skala_torch_model_type
46 :
47 : CONTAINS
48 :
49 : ! **************************************************************************************************
50 : !> \brief Load a SKALA TorchScript model and its feature metadata.
51 : !> \param model ...
52 : !> \param filename ...
53 : !> \param fp64 Promote float32 numerical state and casts; default true for differential consistency.
54 : !> \param higher_derivatives Prepare Torch operations for second input derivatives.
55 : ! **************************************************************************************************
56 19 : SUBROUTINE skala_torch_model_load(model, filename, fp64, higher_derivatives)
57 : TYPE(skala_torch_model_type), INTENT(INOUT) :: model
58 : CHARACTER(len=*), INTENT(IN) :: filename
59 : LOGICAL, INTENT(IN), OPTIONAL :: fp64, higher_derivatives
60 :
61 19 : CHARACTER(:), ALLOCATABLE :: features_json, protocol_string
62 : INTEGER :: ios
63 : LOGICAL :: prepare_derivatives, use_fp64
64 :
65 : CALL torch_model_load_with_metadata(model%torch_model, filename, &
66 : "protocol_version", protocol_string, &
67 19 : "features", features_json)
68 19 : CALL torch_model_remap_device_constants(model%torch_model)
69 19 : CALL torch_model_disable_parameter_gradients(model%torch_model)
70 19 : READ (protocol_string, *, IOSTAT=ios) model%protocol_version
71 19 : IF (ios /= 0) CPABORT("Could not parse SKALA TorchScript protocol_version metadata")
72 19 : IF (model%protocol_version /= 2) THEN
73 0 : CPABORT("Unsupported SKALA TorchScript protocol version")
74 : END IF
75 :
76 19 : CALL parse_feature_list(features_json, model%features)
77 19 : use_fp64 = .TRUE.
78 19 : IF (PRESENT(fp64)) use_fp64 = fp64
79 19 : prepare_derivatives = .FALSE.
80 19 : IF (PRESENT(higher_derivatives)) prepare_derivatives = higher_derivatives
81 19 : IF (prepare_derivatives) THEN
82 2 : CALL torch_model_prepare_higher_derivatives(model%torch_model)
83 17 : ELSE IF (use_fp64) THEN
84 17 : CALL torch_model_promote_float32(model%torch_model)
85 : END IF
86 : ! Preserve the exported SKALA entry point while folding constant model state.
87 19 : CALL torch_model_freeze_preserving_method(model%torch_model, "get_exc_density")
88 :
89 19 : END SUBROUTINE skala_torch_model_load
90 :
91 : ! **************************************************************************************************
92 : !> \brief Release a loaded SKALA TorchScript model.
93 : !> \param model ...
94 : ! **************************************************************************************************
95 19 : SUBROUTINE skala_torch_model_release(model)
96 : TYPE(skala_torch_model_type), INTENT(INOUT) :: model
97 :
98 19 : CALL torch_model_release(model%torch_model)
99 19 : IF (ALLOCATED(model%features)) DEALLOCATE (model%features)
100 19 : model%protocol_version = -1
101 :
102 19 : END SUBROUTINE skala_torch_model_release
103 :
104 : ! **************************************************************************************************
105 : !> \brief Check whether a loaded SKALA model requests a feature.
106 : !> \param model ...
107 : !> \param feature ...
108 : !> \return ...
109 : ! **************************************************************************************************
110 0 : FUNCTION skala_torch_model_needs_feature(model, feature) RESULT(needs_feature)
111 : TYPE(skala_torch_model_type), INTENT(IN) :: model
112 : CHARACTER(len=*), INTENT(IN) :: feature
113 : LOGICAL :: needs_feature
114 :
115 : CHARACTER(len=default_string_length) :: feature_key, model_feature
116 : INTEGER :: i
117 :
118 0 : feature_key = ADJUSTL(feature)
119 0 : CALL uppercase(feature_key)
120 :
121 0 : needs_feature = .FALSE.
122 0 : IF (.NOT. ALLOCATED(model%features)) RETURN
123 :
124 0 : DO i = 1, SIZE(model%features)
125 0 : model_feature = ADJUSTL(model%features(i))
126 0 : CALL uppercase(model_feature)
127 0 : IF (TRIM(model_feature) == TRIM(feature_key)) THEN
128 0 : needs_feature = .TRUE.
129 : RETURN
130 : END IF
131 : END DO
132 :
133 0 : END FUNCTION skala_torch_model_needs_feature
134 :
135 : ! **************************************************************************************************
136 : !> \brief Return the loaded SKALA TorchScript protocol version.
137 : !> \param model ...
138 : !> \return ...
139 : ! **************************************************************************************************
140 0 : FUNCTION skala_torch_model_protocol_version(model) RESULT(protocol_version)
141 : TYPE(skala_torch_model_type), INTENT(IN) :: model
142 : INTEGER :: protocol_version
143 :
144 0 : protocol_version = model%protocol_version
145 :
146 0 : END FUNCTION skala_torch_model_protocol_version
147 :
148 : ! **************************************************************************************************
149 : !> \brief Evaluate the SKALA exchange-correlation energy density.
150 : !> \param model ...
151 : !> \param inputs ...
152 : !> \param exc_density ...
153 : ! **************************************************************************************************
154 0 : SUBROUTINE skala_torch_model_get_exc_density(model, inputs, exc_density)
155 : TYPE(skala_torch_model_type), INTENT(INOUT) :: model
156 : TYPE(torch_dict_type), INTENT(IN) :: inputs
157 : TYPE(torch_tensor_type), INTENT(INOUT) :: exc_density
158 :
159 : #if defined (__HAS_IEEE_EXCEPTIONS)
160 : LOGICAL, DIMENSION(5) :: ieee_halt
161 :
162 : CALL ieee_get_halting_mode(IEEE_ALL, ieee_halt)
163 : CALL ieee_set_halting_mode(IEEE_ALL, .FALSE.)
164 : #endif
165 0 : CALL torch_model_forward_mol_tensor(model%torch_model, "get_exc_density", inputs, exc_density)
166 : #if defined (__HAS_IEEE_EXCEPTIONS)
167 : CALL ieee_set_halting_mode(IEEE_ALL, ieee_halt)
168 : #endif
169 :
170 0 : END SUBROUTINE skala_torch_model_get_exc_density
171 :
172 : ! **************************************************************************************************
173 : !> \brief Evaluate the weighted SKALA exchange-correlation energy.
174 : !> \param model ...
175 : !> \param inputs ...
176 : !> \param grid_weights ...
177 : !> \param exc_tensor ...
178 : !> \param exc ...
179 : ! **************************************************************************************************
180 342 : SUBROUTINE skala_torch_model_get_exc(model, inputs, grid_weights, exc_tensor, exc)
181 : TYPE(skala_torch_model_type), INTENT(INOUT) :: model
182 : TYPE(torch_dict_type), INTENT(IN) :: inputs
183 : TYPE(torch_tensor_type), INTENT(IN) :: grid_weights
184 : TYPE(torch_tensor_type), INTENT(INOUT) :: exc_tensor
185 : REAL(KIND=dp), INTENT(OUT) :: exc
186 :
187 : TYPE(torch_tensor_type) :: exc_density
188 :
189 : #if defined (__HAS_IEEE_EXCEPTIONS)
190 : LOGICAL, DIMENSION(5) :: ieee_halt
191 :
192 : CALL ieee_get_halting_mode(IEEE_ALL, ieee_halt)
193 : CALL ieee_set_halting_mode(IEEE_ALL, .FALSE.)
194 : #endif
195 342 : CALL torch_model_forward_mol_tensor(model%torch_model, "get_exc_density", inputs, exc_density)
196 342 : CALL torch_tensor_weighted_sum(exc_density, grid_weights, exc_tensor)
197 342 : CALL torch_tensor_release(exc_density)
198 342 : exc = torch_tensor_item_double(exc_tensor)
199 : #if defined (__HAS_IEEE_EXCEPTIONS)
200 : CALL ieee_set_halting_mode(IEEE_ALL, ieee_halt)
201 : #endif
202 :
203 342 : END SUBROUTINE skala_torch_model_get_exc
204 :
205 : ! **************************************************************************************************
206 : !> \brief Parse a TorchScript extra_files JSON list of feature names.
207 : !> \param features_json ...
208 : !> \param features ...
209 : ! **************************************************************************************************
210 19 : SUBROUTINE parse_feature_list(features_json, features)
211 : CHARACTER(len=*), INTENT(IN) :: features_json
212 : CHARACTER(len=default_string_length), &
213 : ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: features
214 :
215 : INTEGER :: end_pos, feature_count, i, pos, quote1, &
216 : quote2, start_pos
217 :
218 19 : feature_count = 0
219 19 : pos = 1
220 171 : DO
221 190 : quote1 = INDEX(features_json(pos:), '"')
222 190 : IF (quote1 == 0) EXIT
223 171 : start_pos = pos + quote1
224 171 : quote2 = INDEX(features_json(start_pos:), '"')
225 171 : IF (quote2 == 0) EXIT
226 171 : feature_count = feature_count + 1
227 171 : pos = start_pos + quote2
228 : END DO
229 :
230 19 : IF (feature_count == 0) CPABORT("SKALA TorchScript model does not list any features")
231 57 : ALLOCATE (features(feature_count))
232 190 : features = ""
233 :
234 : pos = 1
235 190 : DO i = 1, feature_count
236 171 : quote1 = INDEX(features_json(pos:), '"')
237 171 : start_pos = pos + quote1
238 171 : quote2 = INDEX(features_json(start_pos:), '"')
239 171 : end_pos = start_pos + quote2 - 2
240 171 : features(i) = features_json(start_pos:end_pos)
241 190 : pos = start_pos + quote2
242 : END DO
243 :
244 19 : END SUBROUTINE parse_feature_list
245 :
246 0 : END MODULE skala_torch_api
|