Yes, of course your total loss L is (piecewise) differentiable. It can be more formally defined as:
L = sum( Li ) / sum( 1{Li ≠ 0} ),
where 1{c} is the indicator function (which is 1 when c is true and 0 otherwise).
Clearly, the function f ( Li ) = 1{Li ≠ 0} has derivative equal to 0 everywhere, except at Li = 0, where the derivative does not exist. In practice, you may assume that it is 0 everywhere.
Your biggest concern should be ensuring that you have no problem in using the tensor losses.data instead of the variable losses. This is because PyTorch will see torch.nonzero(losses.data).size(0) as a constant and not as a function of losses. Luckily, you may easily check that the derivative of L w.r.t. each of the losses Lj is the same whether you consider sum( 1{Li ≠ 0} ) as a function of Lj or not:
dL / dLj = 1 / sum( 1{Li ≠ 0} )