function dZ = internal_geluBackward(dZ, X, approx)
% This function is undocumented and will change in a future release.

%   Copyright 2022 The MathWorks, Inc.

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

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

switch approx
    case "none"
        dZ = nnet.internal.cnngpu.geluBackwardM(X, dZ, X);
    case "tanh"
        dZ = nnet.internal.cnngpu.geluBackwardTanhApproximationM(X, dZ, X);
end
end