(git:6344619)
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!> \param fp64 Promote float32 numerical state and casts; default true for differential consistency.
53! **************************************************************************************************
54 SUBROUTINE skala_torch_model_load(model, filename, fp64)
55 TYPE(skala_torch_model_type), INTENT(INOUT) :: model
56 CHARACTER(len=*), INTENT(IN) :: filename
57 LOGICAL, INTENT(IN), OPTIONAL :: fp64
58
59 CHARACTER(:), ALLOCATABLE :: features_json, protocol_string
60 INTEGER :: ios
61 LOGICAL :: use_fp64
62
63 CALL torch_model_load_with_metadata(model%torch_model, filename, &
64 "protocol_version", protocol_string, &
65 "features", features_json)
66 CALL torch_model_remap_device_constants(model%torch_model)
67 CALL torch_model_disable_parameter_gradients(model%torch_model)
68 READ (protocol_string, *, iostat=ios) model%protocol_version
69 IF (ios /= 0) cpabort("Could not parse SKALA TorchScript protocol_version metadata")
70 IF (model%protocol_version /= 2) THEN
71 cpabort("Unsupported SKALA TorchScript protocol version")
72 END IF
73
74 CALL parse_feature_list(features_json, model%features)
75 use_fp64 = .true.
76 IF (PRESENT(fp64)) use_fp64 = fp64
77 IF (use_fp64) CALL torch_model_promote_float32(model%torch_model)
78 ! Preserve the exported SKALA entry point while folding constant model state.
79 CALL torch_model_freeze_preserving_method(model%torch_model, "get_exc_density")
80
81 END SUBROUTINE skala_torch_model_load
82
83! **************************************************************************************************
84!> \brief Release a loaded SKALA TorchScript model.
85!> \param model ...
86! **************************************************************************************************
87 SUBROUTINE skala_torch_model_release(model)
88 TYPE(skala_torch_model_type), INTENT(INOUT) :: model
89
90 CALL torch_model_release(model%torch_model)
91 IF (ALLOCATED(model%features)) DEALLOCATE (model%features)
92 model%protocol_version = -1
93
94 END SUBROUTINE skala_torch_model_release
95
96! **************************************************************************************************
97!> \brief Check whether a loaded SKALA model requests a feature.
98!> \param model ...
99!> \param feature ...
100!> \return ...
101! **************************************************************************************************
102 FUNCTION skala_torch_model_needs_feature(model, feature) RESULT(needs_feature)
103 TYPE(skala_torch_model_type), INTENT(IN) :: model
104 CHARACTER(len=*), INTENT(IN) :: feature
105 LOGICAL :: needs_feature
106
107 CHARACTER(len=default_string_length) :: feature_key, model_feature
108 INTEGER :: i
109
110 feature_key = adjustl(feature)
111 CALL uppercase(feature_key)
112
113 needs_feature = .false.
114 IF (.NOT. ALLOCATED(model%features)) RETURN
115
116 DO i = 1, SIZE(model%features)
117 model_feature = adjustl(model%features(i))
118 CALL uppercase(model_feature)
119 IF (trim(model_feature) == trim(feature_key)) THEN
120 needs_feature = .true.
121 RETURN
122 END IF
123 END DO
124
126
127! **************************************************************************************************
128!> \brief Return the loaded SKALA TorchScript protocol version.
129!> \param model ...
130!> \return ...
131! **************************************************************************************************
132 FUNCTION skala_torch_model_protocol_version(model) RESULT(protocol_version)
133 TYPE(skala_torch_model_type), INTENT(IN) :: model
134 INTEGER :: protocol_version
135
136 protocol_version = model%protocol_version
137
139
140! **************************************************************************************************
141!> \brief Evaluate the SKALA exchange-correlation energy density.
142!> \param model ...
143!> \param inputs ...
144!> \param exc_density ...
145! **************************************************************************************************
146 SUBROUTINE skala_torch_model_get_exc_density(model, inputs, exc_density)
147 TYPE(skala_torch_model_type), INTENT(INOUT) :: model
148 TYPE(torch_dict_type), INTENT(IN) :: inputs
149 TYPE(torch_tensor_type), INTENT(INOUT) :: exc_density
150
151#if defined (__HAS_IEEE_EXCEPTIONS)
152 LOGICAL, DIMENSION(5) :: ieee_halt
153
154 CALL ieee_get_halting_mode(ieee_all, ieee_halt)
155 CALL ieee_set_halting_mode(ieee_all, .false.)
156#endif
157 CALL torch_model_forward_mol_tensor(model%torch_model, "get_exc_density", inputs, exc_density)
158#if defined (__HAS_IEEE_EXCEPTIONS)
159 CALL ieee_set_halting_mode(ieee_all, ieee_halt)
160#endif
161
163
164! **************************************************************************************************
165!> \brief Evaluate the weighted SKALA exchange-correlation energy.
166!> \param model ...
167!> \param inputs ...
168!> \param grid_weights ...
169!> \param exc_tensor ...
170!> \param exc ...
171! **************************************************************************************************
172 SUBROUTINE skala_torch_model_get_exc(model, inputs, grid_weights, exc_tensor, exc)
173 TYPE(skala_torch_model_type), INTENT(INOUT) :: model
174 TYPE(torch_dict_type), INTENT(IN) :: inputs
175 TYPE(torch_tensor_type), INTENT(IN) :: grid_weights
176 TYPE(torch_tensor_type), INTENT(INOUT) :: exc_tensor
177 REAL(kind=dp), INTENT(OUT) :: exc
178
179 TYPE(torch_tensor_type) :: exc_density
180
181#if defined (__HAS_IEEE_EXCEPTIONS)
182 LOGICAL, DIMENSION(5) :: ieee_halt
183
184 CALL ieee_get_halting_mode(ieee_all, ieee_halt)
185 CALL ieee_set_halting_mode(ieee_all, .false.)
186#endif
187 CALL torch_model_forward_mol_tensor(model%torch_model, "get_exc_density", inputs, exc_density)
188 CALL torch_tensor_weighted_sum(exc_density, grid_weights, exc_tensor)
189 CALL torch_tensor_release(exc_density)
190 exc = torch_tensor_item_double(exc_tensor)
191#if defined (__HAS_IEEE_EXCEPTIONS)
192 CALL ieee_set_halting_mode(ieee_all, ieee_halt)
193#endif
194
195 END SUBROUTINE skala_torch_model_get_exc
196
197! **************************************************************************************************
198!> \brief Parse a TorchScript extra_files JSON list of feature names.
199!> \param features_json ...
200!> \param features ...
201! **************************************************************************************************
202 SUBROUTINE parse_feature_list(features_json, features)
203 CHARACTER(len=*), INTENT(IN) :: features_json
204 CHARACTER(len=default_string_length), &
205 ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: features
206
207 INTEGER :: end_pos, feature_count, i, pos, quote1, &
208 quote2, start_pos
209
210 feature_count = 0
211 pos = 1
212 DO
213 quote1 = index(features_json(pos:), '"')
214 IF (quote1 == 0) EXIT
215 start_pos = pos + quote1
216 quote2 = index(features_json(start_pos:), '"')
217 IF (quote2 == 0) EXIT
218 feature_count = feature_count + 1
219 pos = start_pos + quote2
220 END DO
221
222 IF (feature_count == 0) cpabort("SKALA TorchScript model does not list any features")
223 ALLOCATE (features(feature_count))
224 features = ""
225
226 pos = 1
227 DO i = 1, feature_count
228 quote1 = index(features_json(pos:), '"')
229 start_pos = pos + quote1
230 quote2 = index(features_json(start_pos:), '"')
231 end_pos = start_pos + quote2 - 2
232 features(i) = features_json(start_pos:end_pos)
233 pos = start_pos + quote2
234 END DO
235
236 END SUBROUTINE parse_feature_list
237
238END 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.
subroutine, public skala_torch_model_load(model, filename, fp64)
Load a SKALA TorchScript model and its feature metadata.
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.
Utilities for string manipulations.
elemental subroutine, public uppercase(string)
Convert all lower case characters in a string to upper case.
subroutine, public torch_model_promote_float32(model)
Promote float32 model state and explicit floating-point casts to double precision.
Definition torch_api.F:1973
subroutine, public torch_model_freeze_preserving_method(model, method_name)
Freeze a Torch model while preserving one exported method.
Definition torch_api.F:2305
real(kind=dp) function, public torch_tensor_item_double(tensor)
Returns a scalar double value from a Torch tensor.
Definition torch_api.F:1675
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:1898
subroutine, public torch_model_remap_device_constants(model)
Maps serialized TorchScript device constants to the active Torch device.
Definition torch_api.F:1950
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:2054
subroutine, public torch_model_disable_parameter_gradients(model)
Disable gradients for inference-only model parameters.
Definition torch_api.F:1995
subroutine, public torch_model_release(model)
Releases a Torch model and all its ressources.
Definition torch_api.F:2098
subroutine, public torch_tensor_weighted_sum(values, weights, result)
Returns the weighted sum of two Torch tensors.
Definition torch_api.F:1644
subroutine, public torch_tensor_release(tensor)
Releases a Torch tensor and all its ressources.
Definition torch_api.F:1702