function [dZ, dOffset, dScale] = internal_groupnormBackward(dZ, X, scale, numGroups, epsilon, channelDim)
% This function is undocumented and will change in a future release.

%   Copyright 2020-2021 The MathWorks, Inc.

% gpuArray method for internal_groupnormBackward. The help for this method
% is similar to the CPU method internal_groupnormBackward

dZ = deep.internal.recording.errorOnComplexGradientOutputs(dZ, 'groupnorm');

% Ensure all inputs have the same data type
[dZ, X, scale] = nnet.internal.cnngpu.castToSuperiorFloat(dZ, X, scale);

% Call the cnnhost backward method since no ad-hoc GPU implementation is
% yet available
[dZ, dOffset, dScale] = nnet.internal.cnnhost.groupNormalizationBackward(...
    dZ, X, scale, epsilon, channelDim, numGroups);

end
