Since you are only multiplying with -1 for a negative w, your function should not break the computation graph.
I’ve changed the input to a tensor and it seems to work fine:
def positive_real_quat(quat):
    x, y, z, w = quat.split(1, 1)
    if w >= 0:
        return quat
    else:
        return -quat
    
# Positive
x1 = torch.tensor([[1., 1., 1., 1.]], requires_grad=True)
out1 = positive_real_quat(x1)
out1.mean().backward()
# Check grad
print(x1.grad)
> tensor([[0.2500, 0.2500, 0.2500, 0.2500]])

# Negative
x2 = torch.tensor([[1., 1., 1., -1.]], requires_grad=True)
out2 = positive_real_quat(x2)
out2.mean().backward()
# Check grad
print(x2.grad)
> tensor([[-0.2500, -0.2500, -0.2500, -0.2500]])