Do you mean something like:
    with torch.no_grad():
        mask = ... # mask generation code

You can then use the mask however you want. I’m actually curious if you need to explicitly use no_grad here if your mask generation code doesn’t rely on any weights…