What I believe happening here is, when you call
config = AutoConfig.from_pretrained('bert-base-uncased', output_hidden_states=True) model = AutoModelForMaskedLM.from_config(config) 
the model is a randomly initialized BERT model having the same configuration with “bert-base-uncased” model. So, after fine-tuning, model predictions are not satisfactory because the initial model is not a pretrained one. I have checked it on a colab notebook like this:

image669×736 35.6 KB

If your intention is just to enable output_hidden_states functionality, you can try:
AutoModelForMaskedLM.from_pretrained("bert-base-uncased", output_hidden_states=True)
When I ran the line I recommended on colab, the download time was noticably greater which is a potential indicator that it is indeed the model parameters that are being loaded, not just the configuration file.