12#if defined (__HAS_IEEE_EXCEPTIONS)
13 USE ieee_exceptions,
ONLY: ieee_all, &
14 ieee_get_halting_mode, &
25#include "./base/base_uses.f90"
31 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'skala_torch_api'
39 INTEGER :: protocol_version = -1
40 CHARACTER(len=default_string_length),
ALLOCATABLE, &
41 DIMENSION(:) :: features
54 CHARACTER(len=*),
INTENT(IN) :: filename
56 CHARACTER(:),
ALLOCATABLE :: features_json, protocol_string
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")
68 CALL parse_feature_list(features_json, model%features)
80 IF (
ALLOCATED(model%features))
DEALLOCATE (model%features)
81 model%protocol_version = -1
93 CHARACTER(len=*),
INTENT(IN) :: feature
94 LOGICAL :: needs_feature
96 CHARACTER(len=default_string_length) :: feature_key, model_feature
99 feature_key = adjustl(feature)
102 needs_feature = .false.
103 IF (.NOT.
ALLOCATED(model%features))
RETURN
105 DO i = 1,
SIZE(model%features)
106 model_feature = adjustl(model%features(i))
108 IF (trim(model_feature) == trim(feature_key))
THEN
109 needs_feature = .true.
123 INTEGER :: protocol_version
125 protocol_version = model%protocol_version
140#if defined (__HAS_IEEE_EXCEPTIONS)
141 LOGICAL,
DIMENSION(5) :: ieee_halt
143 CALL ieee_get_halting_mode(ieee_all, ieee_halt)
144 CALL ieee_set_halting_mode(ieee_all, .false.)
147#if defined (__HAS_IEEE_EXCEPTIONS)
148 CALL ieee_set_halting_mode(ieee_all, ieee_halt)
166 REAL(kind=
dp),
INTENT(OUT) :: exc
170#if defined (__HAS_IEEE_EXCEPTIONS)
171 LOGICAL,
DIMENSION(5) :: ieee_halt
173 CALL ieee_get_halting_mode(ieee_all, ieee_halt)
174 CALL ieee_set_halting_mode(ieee_all, .false.)
180#if defined (__HAS_IEEE_EXCEPTIONS)
181 CALL ieee_set_halting_mode(ieee_all, ieee_halt)
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
196 INTEGER :: end_pos, feature_count, i, pos, quote1, &
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
211 IF (feature_count == 0) cpabort(
"SKALA TorchScript model does not list any features")
212 ALLOCATE (features(feature_count))
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
225 END SUBROUTINE parse_feature_list
Defines the basic variable types.
integer, parameter, public dp
integer, parameter, public default_string_length
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.
subroutine, public torch_model_load(model, filename)
Loads a Torch model from given "*.pth" file. (In Torch lingo models are called modules)
subroutine, public torch_model_forward_mol_tensor(model, method_name, inputs, output)
Evaluates a TorchScript model method expecting keyword argument "mol".
subroutine, public torch_model_release(model)
Releases a Torch model and all its ressources.
subroutine, public torch_tensor_weighted_sum(values, weights, result)
Returns the weighted sum of two Torch tensors.
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)
subroutine, public torch_tensor_release(tensor)
Releases a Torch tensor and all its ressources.