Sorry for the late reply. Hope you have already solved the problem.
The indicator functions are perfect and shouldn’t cause any problems in optimization.
Regarding model.weight.grad, your model doesn’t have a weight matrix attached, instead you should call the module object inside the model.
class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(2, 1)

...

model = SimpleModel()

You should call:
model.fc1.weight.grad