modelEvaluateStatefulRecurrentNeuralNet
Extends from BaseClasses.BaseStatefulRecurrentNeuralNet.
Information
Use this block if you want to include a recurrent neural network in Modelica which has been created with the flag stateful=True in TensorFlow. Please notice, that TFLite is not capable to handle the stateful states internally. Therefore, the neural network has to be created with access to all states as additional inputs and outputs. In this context the inputs and outputs have to be additional access points to the neural networks. SMArtInt will handle the updates of the states by storing the values of the state outputs and feed them back into the state inputs. Therefore, for all states matching in- and output have to exist. When creating the neural network the user has to take care of this. The stateful PI controller created in the script ExampleNeuralNets/PIController/PI.py gives an example how to expose the states as in- an outputs.
Please place this block in your own model. After that
- give the path to the TFLite/ONNX model
- specify the number of inputs
- specify the sampling interval
- Provide values for the input of the block in the same way as they are given in the training
Most likely, the stateful RNN will be trained for time discret data and therefore it has to be called only at discrete time instances. The user has to provide the samplingInterval for the discrete time instances. If continuous = false, the model will create events at each of the time instances and will only call the model at these instances. The many events have an impact on simulation performance. To increase performance the user can set continuous = true. In that case the model can be called for any times as it is demanded by the solver. SMArtInt will internally call the neural network only at the time sampled time points. Hence, no events are created, but the inputs for the neural network have to be interpolated. Additionally, solution accuracy for the states of the neural network and the impact of the interpolation of the inputs cannot be controlled by the solver directly. Only the impact on the states in the model can be evaluated. Therefore, this approach might created inaccurate solutions especially if mult-step solver like DASSL and Cvode are used with a high tolerance value. The user should compare the simulation results to those with continuous = false or to results with continuous = true and lower tolerance and/or single step solver like (Radau).
For an example take a look at the model TF_PI_Stateful_tflite. This example does not use this block directly but the usage is similar as it extends the model which is extended by this block.
TensorFlow Lite FlexOps
To use FlexOps with the TensorFlow Lite runtime, additional dynamic libraries must be provided in the SMArtInt library resources. Follow the steps below:
-
Copy the appropriate FlexOps library into the corresponding platform folder
(
win64orlinux64) inside the Resources Library folder:- Windows:
tensorflowlite_flex.dll - Linux:
libtensorflowlite_flex.so
- Windows:
- Activate TensorFlow Lite FlexOps usage in the model parameter dialog.
CUDA GPU Support for ONNX Runtime
To use ONNX Runtime with GPU acceleration, CUDA must be installed on the operating system (tested with CUDA version 13.0), and a CUDA-compatible GPU must be available. In addition, the required ONNX Runtime GPU provider libraries must be provided in the SMArtInt library resources. Follow the steps below:
-
Download the ONNX Runtime build with GPU support from:
ONNX Runtime v1.23.2 Release -
Select and extract the appropriate package for your operating system:
- Windows:
onnxruntime-win-x64-gpu-1.23.2.zip - Linux:
onnxruntime-linux-x64-gpu-1.23.2.tgz
- Windows:
-
Copy the required provider libraries into the corresponding platform folder
(
win64orlinux64) inside the Resources Library folder:onnxruntime_providers_cuda.dll/.soonnxruntime_providers_shared.dll/.so
- Activate GPU usage in the model parameter dialog.
Parameters
| Type | Name | Default | Description |
|---|---|---|---|
| Integer | nInputElements (from BaseNeuralNet) | product(inputSizes) | |
| Integer | nOutputElements (from BaseNeuralNet) | product(outputSizes) | |
| Selected Model | |||
| String | pathToAIModel (from BaseNeuralNet) | "" | Choose path to AI model |
| Tensor sizing | |||
| Integer | inputDimensions (from BaseNeuralNet) | 1 | Number of input dimension |
| Integer | inputSizes (from BaseNeuralNet) | {1} | Vector with size of tensor in each dimension |
| Integer | outputDimensions (from BaseNeuralNet) | 1 | Number of output dimension |
| Integer | outputSizes (from BaseNeuralNet) | {1} | Vector with size of tensor in each dimension |
| RNN Timing Settings | |||
| Boolean | stateful (from BaseNeuralNet) | false | Activate state handling for RNN with state in-/outputs |
| Real | samplePeriod (from BaseNeuralNet) | 0 | Fixed sample period for RNNs |
| Boolean | continuous (from BaseStatefulRecurrentNeuralNet) | true | =true: model operates continuously; input values are delayed |
| Advanced › Performance | |||
| Integer | numberOfThreads (from BaseNeuralNet) | 1 | Number of threads used for inference (0: Number of CPU cores) |
| Advanced › TFLite | |||
| Boolean | useFlexOps (from BaseNeuralNet) | false | Activate to allow Tensorflow Flex Ops for tflite models |
| Advanced › ONNX | |||
| Boolean | useGPU (from BaseNeuralNet) | false | Activate to use a compatible CUDA GPU for inference |
| Integer | gpuDeviceID (from BaseNeuralNet) | 0 | CUDA device ID |
| SMArtInt.Internal.Types.ExecutionMode | executionMode (from BaseNeuralNet) | SMArtInt.Internal.Types.ExecutionMode.Sequential | Execution mode for run inference |
| Input/Output Sizing | |||
| Integer | numberOfInputs (from BaseStatefulRecurrentNeuralNet) | 1 | Number of input values |
| Integer | numberOfOutputs (from BaseStatefulRecurrentNeuralNet) | 1 | Number of output values |
| Integer | batchSize (from BaseStatefulRecurrentNeuralNet) | 1 | Number of parallel batched simulations |
Connectors
| Type | Name | Default | Description |
|---|---|---|---|
| Modelica.Blocks.Interfaces.RealInput[batchSize,numberOfInputs] | u | ||
| Modelica.Blocks.Interfaces.RealOutput[batchSize,numberOfOutputs] | y |
Components
| Type | Name | Default | Description |
|---|---|---|---|
| Internal.Utilities.Dependencies.OnnxGpuDependencies | onnxGpuDependencies (from BaseNeuralNet) | ||
| Internal.Utilities.Dependencies.TFFlexOpsDependencies | tFFlexOpsDependencies (from BaseNeuralNet) | ||
| Internal.Utilities.RunInferenceFlatInputStatefulRNN | runInferenceFlatInputStatefulRNN (from BaseStatefulRecurrentNeuralNet) | ||
| Internal.Utilities.SubModels.Array2DFlatteningModel | array2DFlatteningModel (from BaseStatefulRecurrentNeuralNet) | ||
| Internal.Utilities.SubModels.Array2DDeflatteningModel | array2DDeflatteningModel (from BaseStatefulRecurrentNeuralNet) |