(git:5e7fe52)
Loading...
Searching...
No Matches
skala_torch_api.F
Go to the documentation of this file.
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! **************************************************************************************************
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
20 USE torch_api, ONLY: &
26#include "./base/base_uses.f90"
27
28 IMPLICIT NONE
29
30 PRIVATE
31
32 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'skala_torch_api'
33
37
39 PRIVATE
40 INTEGER :: protocol_version = -1
41 CHARACTER(len=default_string_length), ALLOCATABLE, &
42 DIMENSION(:) :: features
43 TYPE(torch_model_type) :: torch_model
45
46CONTAINS
47
48! **************************************************************************************************
49!> \brief Load a SKALA TorchScript model and its feature metadata.
50!> \param model ...
51!> \param filename ...
52! **************************************************************************************************
53 SUBROUTINE skala_torch_model_load(model, filename)
54 TYPE(skala_torch_model_type), INTENT(INOUT) :: model
55 CHARACTER(len=*), INTENT(IN) :: filename
56
57 CHARACTER(:), ALLOCATABLE :: features_json, protocol_string
58 INTEGER :: ios
59
60 CALL torch_model_load_with_metadata(model%torch_model, filename, &
61 "protocol_version", protocol_string, &
62 "features", features_json)
63 CALL torch_model_remap_device_constants(model%torch_model)
64 CALL torch_model_disable_parameter_gradients(model%torch_model)
65 READ (protocol_string, *, iostat=ios) model%protocol_version
66 IF (ios /= 0) cpabort("Could not parse SKALA TorchScript protocol_version metadata")
67 IF (model%protocol_version /= 2) THEN
68 cpabort("Unsupported SKALA TorchScript protocol version")
69 END IF
70
71 CALL parse_feature_list(features_json, model%features)
72 ! Preserve the exported SKALA entry point while folding constant model state.
73 CALL torch_model_freeze_preserving_method(model%torch_model, "get_exc_density")
74
75 END SUBROUTINE skala_torch_model_load
76
77! **************************************************************************************************
78!> \brief Release a loaded SKALA TorchScript model.
79!> \param model ...
80! **************************************************************************************************
81 SUBROUTINE skala_torch_model_release(model)
82 TYPE(skala_torch_model_type), INTENT(INOUT) :: model
83
84 CALL torch_model_release(model%torch_model)
85 IF (ALLOCATED(model%features)) DEALLOCATE (model%features)
86 model%protocol_version = -1
87
88 END SUBROUTINE skala_torch_model_release
89
90! **************************************************************************************************
91!> \brief Check whether a loaded SKALA model requests a feature.
92!> \param model ...
93!> \param feature ...
94!> \return ...
95! **************************************************************************************************
96 FUNCTION skala_torch_model_needs_feature(model, feature) RESULT(needs_feature)
97 TYPE(skala_torch_model_type), INTENT(IN) :: model
98 CHARACTER(len=*), INTENT(IN) :: feature
99 LOGICAL :: needs_feature
100
101 CHARACTER(len=default_string_length) :: feature_key, model_feature
102 INTEGER :: i
103
104 feature_key = adjustl(feature)
105 CALL uppercase(feature_key)
106
107 needs_feature = .false.
108 IF (.NOT. ALLOCATED(model%features)) RETURN
109
110 DO i = 1, SIZE(model%features)
111 model_feature = adjustl(model%features(i))
112 CALL uppercase(model_feature)
113 IF (trim(model_feature) == trim(feature_key)) THEN
114 needs_feature = .true.
115 RETURN
116 END IF
117 END DO
118
120
121! **************************************************************************************************
122!> \brief Return the loaded SKALA TorchScript protocol version.
123!> \param model ...
124!> \return ...
125! **************************************************************************************************
126 FUNCTION skala_torch_model_protocol_version(model) RESULT(protocol_version)
127 TYPE(skala_torch_model_type), INTENT(IN) :: model
128 INTEGER :: protocol_version
129
130 protocol_version = model%protocol_version
131
133
134! **************************************************************************************************
135!> \brief Evaluate the SKALA exchange-correlation energy density.
136!> \param model ...
137!> \param inputs ...
138!> \param exc_density ...
139! **************************************************************************************************
140 SUBROUTINE skala_torch_model_get_exc_density(model, inputs, exc_density)
141 TYPE(skala_torch_model_type), INTENT(INOUT) :: model
142 TYPE(torch_dict_type), INTENT(IN) :: inputs
143 TYPE(torch_tensor_type), INTENT(INOUT) :: exc_density
144
145#if defined (__HAS_IEEE_EXCEPTIONS)
146 LOGICAL, DIMENSION(5) :: ieee_halt
147
148 CALL ieee_get_halting_mode(ieee_all, ieee_halt)
149 CALL ieee_set_halting_mode(ieee_all, .false.)
150#endif
151 CALL torch_model_forward_mol_tensor(model%torch_model, "get_exc_density", inputs, exc_density)
152#if defined (__HAS_IEEE_EXCEPTIONS)
153 CALL ieee_set_halting_mode(ieee_all, ieee_halt)
154#endif
155
157
158! **************************************************************************************************
159!> \brief Evaluate the weighted SKALA exchange-correlation energy.
160!> \param model ...
161!> \param inputs ...
162!> \param grid_weights ...
163!> \param exc_tensor ...
164!> \param exc ...
165! **************************************************************************************************
166 SUBROUTINE skala_torch_model_get_exc(model, inputs, grid_weights, exc_tensor, exc)
167 TYPE(skala_torch_model_type), INTENT(INOUT) :: model
168 TYPE(torch_dict_type), INTENT(IN) :: inputs
169 TYPE(torch_tensor_type), INTENT(IN) :: grid_weights
170 TYPE(torch_tensor_type), INTENT(INOUT) :: exc_tensor
171 REAL(kind=dp), INTENT(OUT) :: exc
172
173 TYPE(torch_tensor_type) :: exc_density
174
175#if defined (__HAS_IEEE_EXCEPTIONS)
176 LOGICAL, DIMENSION(5) :: ieee_halt
177
178 CALL ieee_get_halting_mode(ieee_all, ieee_halt)
179 CALL ieee_set_halting_mode(ieee_all, .false.)
180#endif
181 CALL torch_model_forward_mol_tensor(model%torch_model, "get_exc_density", inputs, exc_density)
182 CALL torch_tensor_weighted_sum(exc_density, grid_weights, exc_tensor)
183 CALL torch_tensor_release(exc_density)
184 exc = torch_tensor_item_double(exc_tensor)
185#if defined (__HAS_IEEE_EXCEPTIONS)
186 CALL ieee_set_halting_mode(ieee_all, ieee_halt)
187#endif
188
189 END SUBROUTINE skala_torch_model_get_exc
190
191! **************************************************************************************************
192!> \brief Parse a TorchScript extra_files JSON list of feature names.
193!> \param features_json ...
194!> \param features ...
195! **************************************************************************************************
196 SUBROUTINE parse_feature_list(features_json, features)
197 CHARACTER(len=*), INTENT(IN) :: features_json
198 CHARACTER(len=default_string_length), &
199 ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: features
200
201 INTEGER :: end_pos, feature_count, i, pos, quote1, &
202 quote2, start_pos
203
204 feature_count = 0
205 pos = 1
206 DO
207 quote1 = index(features_json(pos:), '"')
208 IF (quote1 == 0) EXIT
209 start_pos = pos + quote1
210 quote2 = index(features_json(start_pos:), '"')
211 IF (quote2 == 0) EXIT
212 feature_count = feature_count + 1
213 pos = start_pos + quote2
214 END DO
215
216 IF (feature_count == 0) cpabort("SKALA TorchScript model does not list any features")
217 ALLOCATE (features(feature_count))
218 features = ""
219
220 pos = 1
221 DO i = 1, feature_count
222 quote1 = index(features_json(pos:), '"')
223 start_pos = pos + quote1
224 quote2 = index(features_json(start_pos:), '"')
225 end_pos = start_pos + quote2 - 2
226 features(i) = features_json(start_pos:end_pos)
227 pos = start_pos + quote2
228 END DO
229
230 END SUBROUTINE parse_feature_list
231
232END MODULE skala_torch_api
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
Small CP2K wrapper around the SKALA TorchScript functional protocol.
subroutine, public skala_torch_model_release(model)
Release a loaded SKALA TorchScript model.
logical function, public skala_torch_model_needs_feature(model, feature)
Check whether a loaded SKALA model requests a feature.
subroutine, public skala_torch_model_get_exc(model, inputs, grid_weights, exc_tensor, exc)
Evaluate the weighted SKALA exchange-correlation energy.
integer function, public skala_torch_model_protocol_version(model)
Return the loaded SKALA TorchScript protocol version.
subroutine, public skala_torch_model_get_exc_density(model, inputs, exc_density)
Evaluate the SKALA exchange-correlation energy density.
subroutine, public skala_torch_model_load(model, filename)
Load a SKALA TorchScript model and its feature metadata.
Utilities for string manipulations.
elemental subroutine, public uppercase(string)
Convert all lower case characters in a string to upper case.
subroutine, public torch_model_freeze_preserving_method(model, method_name)
Freeze a Torch model while preserving one exported method.
Definition torch_api.F:2282
real(kind=dp) function, public torch_tensor_item_double(tensor)
Returns a scalar double value from a Torch tensor.
Definition torch_api.F:1674
subroutine, public torch_model_load_with_metadata(model, filename, key1, value1, key2, value2)
Loads a Torch model and reads two metadata entries in the same archive pass.
Definition torch_api.F:1897
subroutine, public torch_model_remap_device_constants(model)
Maps serialized TorchScript device constants to the active Torch device.
Definition torch_api.F:1949
subroutine, public torch_model_forward_mol_tensor(model, method_name, inputs, output)
Evaluates a TorchScript model method expecting keyword argument "mol".
Definition torch_api.F:2031
subroutine, public torch_model_disable_parameter_gradients(model)
Disable gradients for inference-only model parameters.
Definition torch_api.F:1972
subroutine, public torch_model_release(model)
Releases a Torch model and all its ressources.
Definition torch_api.F:2075
subroutine, public torch_tensor_weighted_sum(values, weights, result)
Returns the weighted sum of two Torch tensors.
Definition torch_api.F:1643
subroutine, public torch_tensor_release(tensor)
Releases a Torch tensor and all its ressources.
Definition torch_api.F:1701