Repository: huggingface/transformers
Pull request: [`ModernBERT`] Never save 'reference_compile' config; should be set based on end user

# What does this PR do?
* Never save `reference_compile` in `ModernBertConfig`

## Details
The ModernBERT architecture uses a `reference_compile` config option to determine whether various model layers are compiled. However, not every system supports this, so we either 1) use the user's provided value, 2) use the option in the config, or 3) set it automatically based on the user's system.

However, it seems that if you save a model, it will save `reference_compile` into the config. This is very problematic, as end users who cannot run compilation will be "forced" into compilation (option 2) instead of determining whether compilation is possible (option 3). This is quite annoying for smooth usage.

When this is merged and released, I'll consider writing a script that makes PRs on all ~850 [ModernBERT models](https://huggingface.co/models?other=modernbert) to remove the `reference_compile` option from their configs, so it's always just based on the user's system (or manual parameters).

## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [x] Did you write any new necessary tests?


## Who can review?

@ArthurZucker



- Tom Aarsen


Changed files:
filename: src/transformers/models/modernbert/configuration_modernbert.py
patch: @@ -214,5 +214,10 @@ def __init__(
                 f'Invalid value for `classifier_pooling`, should be either "cls" or "mean", but is {self.classifier_pooling}.'
             )
 
+    def to_dict(self):
+        output = super().to_dict()
+        output.pop("reference_compile", None)
+        return output
+
 
 __all__ = ["ModernBertConfig"]

filename: src/transformers/models/modernbert/modular_modernbert.py
patch: @@ -248,6 +248,11 @@ def __init__(
                 f'Invalid value for `classifier_pooling`, should be either "cls" or "mean", but is {self.classifier_pooling}.'
             )
 
+    def to_dict(self):
+        output = super().to_dict()
+        output.pop("reference_compile", None)
+        return output
+
 
 def _unpad_modernbert_input(
     inputs: torch.Tensor,

filename: tests/models/modernbert/test_modeling_modernbert.py
patch: @@ -12,7 +12,9 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+import json
 import os
+import tempfile
 import unittest
 
 import pytest
@@ -366,6 +368,14 @@ def test_flash_attn_2_inference_equivalence_right_padding(self):
     def test_flash_attn_2_conversion(self):
         self.skipTest(reason="ModernBert doesn't use the ModernBertFlashAttention2 class method.")
 
+    def test_saved_config_excludes_reference_compile(self):
+        config = ModernBertConfig(reference_compile=True)
+        with tempfile.TemporaryDirectory() as tmpdirname:
+            config.save_pretrained(tmpdirname)
+            with open(os.path.join(tmpdirname, "config.json"), "r") as f:
+                config_dict = json.load(f)
+            self.assertNotIn("reference_compile", config_dict)
+
 
 @require_torch
 class ModernBertModelIntegrationTest(unittest.TestCase):