Your loss implementation won’t work, unfortunately.
You are creating hamming_loss as a new leaf variable, which is not connected to the computation graph. You are using logits to manipulate hamming_loss, but the future backward() call will only calculate the gradients up the the hamming_loss creation, not the complete model (i.e. Autograd will not calculate the gradients for any parameters, which were used to create logits).
Also, torch.add and torch.div are not inplace methods, so you are currently not using the result at all. Inplace operations use an underscore (such as tensor.add_()), but might create errors during the backward call, if you have overwritten an activation, which would be needed for the gradient calculation.
To fix this you would have to calculate the hamming_loss from operations on logits (assuming that logits is the model output).
Also note that threshold operations (such as torch.floor) would create mostly zero gradients.