As far as I understand your code, you are basically using a Dropout layer given a special flag which you defined.
If that’s the case, you can just use the functional dropout 7 in your forward method and use the training argument to activate or deactivate it.