My experience with AdamW told me that once you pass the parameters into an optimizer and train them for a step, then the only flag that determines whether to calculate grad, is NOT

param.requires_grad

but

param.grad is None

If you don’t want to re-instantiate the optimizer and just want to tell the existing optimizer not to calculate the grad of some parameters, you have to manually set param.grad to None.
For the same reason, if you want to resume the training of some parameters, requires_grad = True will not  work. Instead you have to do use param.grad = torch.zeros_like(param)
For your reference, my pytorch version is 1.7.1