Your training loop part looks fine to me.

Are you referring to an open-source codebase for this?
Can you overfit your model to a small dataset first and ensure the model is training perfectly before generalizing? You can also set the random seeds to ensure consistent masking.