deep.gpu.deterministicAlgorithms - Set determinism of deep learning operations on the GPU to get reproducible

  results - MATLAB ([original](https://www.mathworks.com/help/deeplearning/ref/deep.gpu.deterministicalgorithms.html)) ([raw](?raw))

Set determinism of deep learning operations on the GPU to get reproducible results

Since R2024b

Syntax

Description

`previousState` = deep.gpu.deterministicAlgorithms([newState](#mw%5Ff630348b-bf9a-4441-89d7-eec60c4dea40)) returns the current determinism state of GPU deep learning operations as1 (true) or 0 (false) before changing the state according to the inputnewState. If newState is 1 (true), then subsequent calls to GPU deep learning operations use only deterministic algorithms. This function requires Parallel Computing Toolbox™.

example

`state` = deep.gpu.deterministicAlgorithms returns the current determinism state of GPU deep learning operations as1 (true) or 0 (false). If state is 1 (true), then subsequent calls to GPU deep learning operations use only deterministic algorithms.

Tip

example

Examples

collapse all

Set GPU Determinism

To make GPU deep learning operations use only deterministic algorithms, set the determinism option to true. Store the previous state, so that you can restore it later.

previousState = deep.gpu.deterministicAlgorithms(true);

To make your GPU deep learning operations repeatable, also set the random number generators and seeds on the CPU and GPU using the rng and gpurng functions, respectively.

rng("default") gpurng("default")

After performing deep learning operations on your GPU, such as training or inference, revert the setting back to its original state.

deep.gpu.deterministicAlgorithms(previousState);

Reproduce Network Training on a GPU

This example shows how to train a network several times on a GPU and get identical results.

Ensuring the reproducibility of model training and inference on the GPU can be beneficial for experimentation and debugging. Reproducing model training on the GPU is particularly important in the verification of deep learning systems.

Prepare Training Data and Network

Use the supporting functions prepareDigitsData and prepareAutoencoderLayers to prepare the training data and the network architecture. These functions prepare the data and build the autoencoder network as described in the Prepare Datastore for Image-to-Image Regression example, and are attached to this example as supporting files.

[dsTrain,dsVal] = prepareDigitsData; layers = prepareAutoencoderLayers;

Define Training Options

Specify the training options. The options are the same as those in the Prepare Datastore for Image-to-Image Regression example, with these exceptions.

options = trainingOptions("adam", ... MaxEpochs=5, ... MiniBatchSize=500, ... ValidationData=dsVal, ... ValidationPatience=5, ... OutputNetwork="last-iteration", ... ExecutionEnvironment="gpu", ... Verbose=false);

Check whether a GPU is selected and is available for training.

gpu = gpuDevice; disp(gpu.Name + " selected.")

NVIDIA RTX A5000 selected.

Train Network Twice and Compare Results

Train the network twice using the trainnet function. To ensure that random number generation does not affect the training, set the random number generator and seed on the CPU and the GPU before training using the rng and gpurng (Parallel Computing Toolbox) functions, respectively.

rng("default") gpurng("default") net1 = trainnet(dsTrain,layers,"mse",options);

rng("default") gpurng("default") net2 = trainnet(dsTrain,layers,"mse",options);

Check whether the learnable parameters of the trained networks are equal. As the training uses nondeterministic algorithms, the learnable parameters of the two networks are different.

isequal(net1.Learnables.Value,net2.Learnables.Value)

Plot the difference between the weights of the first convolution layer between the first training run and the second training run. The plot shows that there is a small difference in the weights of the two networks.

learnablesDiff = net1.Learnables.Value{1}(:) - net2.Learnables.Value{1}(:); learnablesDiff = extractdata(learnablesDiff);

figure bar(learnablesDiff) ylabel("Difference in Weight Value") xlabel("Learnable Parameter Number")

Set Determinism Option and Train Networks

Use the deep.gpu.deterministicAlgorithms function to set the GPU determinism state to true, and capture the previous state of the GPU determinism so that you can restore it later. All subsequent calls to GPU deep learning operations use only deterministic algorithms.

previousState = deep.gpu.deterministicAlgorithms(true);

Train the network twice using the trainnet function, setting the CPU and GPU random number generator and seed each time. Using only deterministic algorithms can slow down training and inference.

rng("default") gpurng("default") net3 = trainnet(dsTrain,layers,"mse",options);

rng("default") gpurng("default") net4 = trainnet(dsTrain,layers,"mse",options);

Check whether the learnable parameters of the trained networks are equal. As only deterministic algorithms are used, the learnable parameters of the two networks are equal.

isequal(net3.Learnables.Value,net4.Learnables.Value)

Plot the difference between the weights of the first convolution layer between the first training run and the second training run. The plot shows that there is no difference in the weights of the two networks.

learnablesDiff = net3.Learnables.Value{1}(:) - net4.Learnables.Value{1}(:); learnablesDiff = extractdata(learnablesDiff);

figure bar(learnablesDiff) ylabel("Difference in Weight Value") xlabel("Learnable Parameter Number")

Restore the GPU determinism state to its original value.

deep.gpu.deterministicAlgorithms(previousState);

Input Arguments

collapse all

newState — New state of GPU determinism

1 (true) | 0 (false)

New state of GPU determinism, specified as 1 (true) or 0 (false).

If newState is 1 (true), then subsequent calls to GPU deep learning operations use only deterministic algorithms. If newState is 0 (false), then subsequent calls to GPU deep learning operations use the fastest available algorithms, which might be nondeterministic.

Data Types: logical

Limitations

Tips

Version History

Introduced in R2024b