function [dZ, dZdT] = internal_l1lossBackward(dZ, X, T, weights, mask, normFactor)
% This function is undocumented and will change in a future release.

%   Copyright 2021-2023 The MathWorks, Inc.

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

if isempty(normFactor)
    % Assign normalization factor of 1 for 'Reduction' value 'none'.
    normFactor = 1;
end

err = X-T;
ddX = sign(err);

dZ = dZ.*weights.*ddX./normFactor;
dZ = internal_mask(dZ, mask);

if nargout > 1
    dZdT = -dZ;
end

end