12#if defined (__HAS_IEEE_EXCEPTIONS)
13 USE ieee_exceptions,
ONLY: ieee_all, &
14 ieee_get_halting_mode, &
26#include "../base/base_uses.f90"
32 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'skala_torch_api'
40 INTEGER :: protocol_version = -1
41 CHARACTER(len=default_string_length),
ALLOCATABLE, &
42 DIMENSION(:) :: features
56 CHARACTER(len=*),
INTENT(IN) :: filename
57 LOGICAL,
INTENT(IN),
OPTIONAL :: fp64
59 CHARACTER(:),
ALLOCATABLE :: features_json, protocol_string
64 "protocol_version", protocol_string, &
65 "features", features_json)
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")
74 CALL parse_feature_list(features_json, model%features)
76 IF (
PRESENT(fp64)) use_fp64 = fp64
91 IF (
ALLOCATED(model%features))
DEALLOCATE (model%features)
92 model%protocol_version = -1
104 CHARACTER(len=*),
INTENT(IN) :: feature
105 LOGICAL :: needs_feature
107 CHARACTER(len=default_string_length) :: feature_key, model_feature
110 feature_key = adjustl(feature)
113 needs_feature = .false.
114 IF (.NOT.
ALLOCATED(model%features))
RETURN
116 DO i = 1,
SIZE(model%features)
117 model_feature = adjustl(model%features(i))
119 IF (trim(model_feature) == trim(feature_key))
THEN
120 needs_feature = .true.
134 INTEGER :: protocol_version
136 protocol_version = model%protocol_version
151#if defined (__HAS_IEEE_EXCEPTIONS)
152 LOGICAL,
DIMENSION(5) :: ieee_halt
154 CALL ieee_get_halting_mode(ieee_all, ieee_halt)
155 CALL ieee_set_halting_mode(ieee_all, .false.)
158#if defined (__HAS_IEEE_EXCEPTIONS)
159 CALL ieee_set_halting_mode(ieee_all, ieee_halt)
177 REAL(kind=
dp),
INTENT(OUT) :: exc
181#if defined (__HAS_IEEE_EXCEPTIONS)
182 LOGICAL,
DIMENSION(5) :: ieee_halt
184 CALL ieee_get_halting_mode(ieee_all, ieee_halt)
185 CALL ieee_set_halting_mode(ieee_all, .false.)
191#if defined (__HAS_IEEE_EXCEPTIONS)
192 CALL ieee_set_halting_mode(ieee_all, ieee_halt)
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
207 INTEGER :: end_pos, feature_count, i, pos, quote1, &
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
222 IF (feature_count == 0) cpabort(
"SKALA TorchScript model does not list any features")
223 ALLOCATE (features(feature_count))
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
236 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.
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.
subroutine, public torch_model_freeze_preserving_method(model, method_name)
Freeze a Torch model while preserving one exported method.
real(kind=dp) function, public torch_tensor_item_double(tensor)
Returns a scalar double value from a Torch tensor.
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.
subroutine, public torch_model_remap_device_constants(model)
Maps serialized TorchScript device constants to the active Torch device.
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_disable_parameter_gradients(model)
Disable gradients for inference-only model parameters.
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.
subroutine, public torch_tensor_release(tensor)
Releases a Torch tensor and all its ressources.