(git:a660c7f)
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: &
25#include "./base/base_uses.f90"
26
27 IMPLICIT NONE
28
29 PRIVATE
30
31 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'skala_torch_api'
32
36
38 PRIVATE
39 INTEGER :: protocol_version = -1
40 CHARACTER(len=default_string_length), ALLOCATABLE, &
41 DIMENSION(:) :: features
42 TYPE(torch_model_type) :: torch_model
44
45CONTAINS
46
47! **************************************************************************************************
48!> \brief Load a SKALA TorchScript model and its feature metadata.
49!> \param model ...
50!> \param filename ...
51! **************************************************************************************************
52 SUBROUTINE skala_torch_model_load(model, filename)
53 TYPE(skala_torch_model_type), INTENT(INOUT) :: model
54 CHARACTER(len=*), INTENT(IN) :: filename
55
56 CHARACTER(:), ALLOCATABLE :: features_json, protocol_string
57 INTEGER :: ios
58
59 CALL torch_model_load(model%torch_model, filename)
60 protocol_string = torch_model_read_metadata(filename, "protocol_version")
61 features_json = torch_model_read_metadata(filename, "features")
62 READ (protocol_string, *, iostat=ios) model%protocol_version
63 IF (ios /= 0) cpabort("Could not parse SKALA TorchScript protocol_version metadata")
64 IF (model%protocol_version /= 2) THEN
65 cpabort("Unsupported SKALA TorchScript protocol version")
66 END IF
67
68 CALL parse_feature_list(features_json, model%features)
69
70 END SUBROUTINE skala_torch_model_load
71
72! **************************************************************************************************
73!> \brief Release a loaded SKALA TorchScript model.
74!> \param model ...
75! **************************************************************************************************
76 SUBROUTINE skala_torch_model_release(model)
77 TYPE(skala_torch_model_type), INTENT(INOUT) :: model
78
79 CALL torch_model_release(model%torch_model)
80 IF (ALLOCATED(model%features)) DEALLOCATE (model%features)
81 model%protocol_version = -1
82
83 END SUBROUTINE skala_torch_model_release
84
85! **************************************************************************************************
86!> \brief Check whether a loaded SKALA model requests a feature.
87!> \param model ...
88!> \param feature ...
89!> \return ...
90! **************************************************************************************************
91 FUNCTION skala_torch_model_needs_feature(model, feature) RESULT(needs_feature)
92 TYPE(skala_torch_model_type), INTENT(IN) :: model
93 CHARACTER(len=*), INTENT(IN) :: feature
94 LOGICAL :: needs_feature
95
96 CHARACTER(len=default_string_length) :: feature_key, model_feature
97 INTEGER :: i
98
99 feature_key = adjustl(feature)
100 CALL uppercase(feature_key)
101
102 needs_feature = .false.
103 IF (.NOT. ALLOCATED(model%features)) RETURN
104
105 DO i = 1, SIZE(model%features)
106 model_feature = adjustl(model%features(i))
107 CALL uppercase(model_feature)
108 IF (trim(model_feature) == trim(feature_key)) THEN
109 needs_feature = .true.
110 RETURN
111 END IF
112 END DO
113
115
116! **************************************************************************************************
117!> \brief Return the loaded SKALA TorchScript protocol version.
118!> \param model ...
119!> \return ...
120! **************************************************************************************************
121 FUNCTION skala_torch_model_protocol_version(model) RESULT(protocol_version)
122 TYPE(skala_torch_model_type), INTENT(IN) :: model
123 INTEGER :: protocol_version
124
125 protocol_version = model%protocol_version
126
128
129! **************************************************************************************************
130!> \brief Evaluate the SKALA exchange-correlation energy density.
131!> \param model ...
132!> \param inputs ...
133!> \param exc_density ...
134! **************************************************************************************************
135 SUBROUTINE skala_torch_model_get_exc_density(model, inputs, exc_density)
136 TYPE(skala_torch_model_type), INTENT(INOUT) :: model
137 TYPE(torch_dict_type), INTENT(IN) :: inputs
138 TYPE(torch_tensor_type), INTENT(INOUT) :: exc_density
139
140#if defined (__HAS_IEEE_EXCEPTIONS)
141 LOGICAL, DIMENSION(5) :: ieee_halt
142
143 CALL ieee_get_halting_mode(ieee_all, ieee_halt)
144 CALL ieee_set_halting_mode(ieee_all, .false.)
145#endif
146 CALL torch_model_forward_mol_tensor(model%torch_model, "get_exc_density", inputs, exc_density)
147#if defined (__HAS_IEEE_EXCEPTIONS)
148 CALL ieee_set_halting_mode(ieee_all, ieee_halt)
149#endif
150
152
153! **************************************************************************************************
154!> \brief Evaluate the weighted SKALA exchange-correlation energy.
155!> \param model ...
156!> \param inputs ...
157!> \param grid_weights ...
158!> \param exc_tensor ...
159!> \param exc ...
160! **************************************************************************************************
161 SUBROUTINE skala_torch_model_get_exc(model, inputs, grid_weights, exc_tensor, exc)
162 TYPE(skala_torch_model_type), INTENT(INOUT) :: model
163 TYPE(torch_dict_type), INTENT(IN) :: inputs
164 TYPE(torch_tensor_type), INTENT(IN) :: grid_weights
165 TYPE(torch_tensor_type), INTENT(INOUT) :: exc_tensor
166 REAL(kind=dp), INTENT(OUT) :: exc
167
168 TYPE(torch_tensor_type) :: exc_density
169
170#if defined (__HAS_IEEE_EXCEPTIONS)
171 LOGICAL, DIMENSION(5) :: ieee_halt
172
173 CALL ieee_get_halting_mode(ieee_all, ieee_halt)
174 CALL ieee_set_halting_mode(ieee_all, .false.)
175#endif
176 CALL torch_model_forward_mol_tensor(model%torch_model, "get_exc_density", inputs, exc_density)
177 CALL torch_tensor_weighted_sum(exc_density, grid_weights, exc_tensor)
178 CALL torch_tensor_release(exc_density)
179 exc = torch_tensor_item_double(exc_tensor)
180#if defined (__HAS_IEEE_EXCEPTIONS)
181 CALL ieee_set_halting_mode(ieee_all, ieee_halt)
182#endif
183
184 END SUBROUTINE skala_torch_model_get_exc
185
186! **************************************************************************************************
187!> \brief Parse a TorchScript extra_files JSON list of feature names.
188!> \param features_json ...
189!> \param features ...
190! **************************************************************************************************
191 SUBROUTINE parse_feature_list(features_json, features)
192 CHARACTER(len=*), INTENT(IN) :: features_json
193 CHARACTER(len=default_string_length), &
194 ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: features
195
196 INTEGER :: end_pos, feature_count, i, pos, quote1, &
197 quote2, start_pos
198
199 feature_count = 0
200 pos = 1
201 DO
202 quote1 = index(features_json(pos:), '"')
203 IF (quote1 == 0) EXIT
204 start_pos = pos + quote1
205 quote2 = index(features_json(start_pos:), '"')
206 IF (quote2 == 0) EXIT
207 feature_count = feature_count + 1
208 pos = start_pos + quote2
209 END DO
210
211 IF (feature_count == 0) cpabort("SKALA TorchScript model does not list any features")
212 ALLOCATE (features(feature_count))
213 features = ""
214
215 pos = 1
216 DO i = 1, feature_count
217 quote1 = index(features_json(pos:), '"')
218 start_pos = pos + quote1
219 quote2 = index(features_json(start_pos:), '"')
220 end_pos = start_pos + quote2 - 2
221 features(i) = features_json(start_pos:end_pos)
222 pos = start_pos + quote2
223 END DO
224
225 END SUBROUTINE parse_feature_list
226
227END 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.
real(kind=dp) function, public torch_tensor_item_double(tensor)
Returns a scalar double value from a Torch tensor.
Definition torch_api.F:1631
subroutine, public torch_model_load(model, filename)
Loads a Torch model from given "*.pth" file. (In Torch lingo models are called modules)
Definition torch_api.F:1823
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:1890
subroutine, public torch_model_release(model)
Releases a Torch model and all its ressources.
Definition torch_api.F:1934
subroutine, public torch_tensor_weighted_sum(values, weights, result)
Returns the weighted sum of two Torch tensors.
Definition torch_api.F:1600
character(:) function, allocatable, public torch_model_read_metadata(filename, key)
Reads metadata entry from given "*.pth" file. (In Torch lingo they are called extra files)
Definition torch_api.F:1958
subroutine, public torch_tensor_release(tensor)
Releases a Torch tensor and all its ressources.
Definition torch_api.F:1658