function dZ = internal_maxpoolBackward(dZ, X, ~, poolSize, padding, stride)
% 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', 'maxpool'));
end

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

% Reshape as [top1 top2 ... topn bot1 bot2 ... botn] to match the CPU builtin
padding = reshape(reshape(padding, 2, [])', 1, []);

% Call the builtin
dZ = builtin('_maxpoolBackward', dZ, X, poolSize, padding, stride);

end
