function dZ = internal_softmaxBackward(dZ, Z, channelDim)
% This function is undocumented and will change in a future release.

%   Copyright 2019-2021 The MathWorks, Inc.

if ~isreal(dZ)
    error(message('deep:dlarray:ComplexGradientOutput', 'softmax'));
end

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

Z = nnet.internal.cnn.util.boundAwayFromZero(Z);

dZ = nnet.internal.cnngpu.softmaxBackwardND(Z, dZ, channelDim);

end

