Repository: huggingface/transformers
Pull request: Patch set_attn_implementation for Windows

# What does this PR do?

It patches the following on Windows devices:
```python
from transformers import ChineseCLIPModel

model = ChineseCLIPModel.from_pretrained("OFA-Sys/chinese-clip-vit-base-patch16")
model.set_attn_implementation("eager")
print(model.config._attn_implementation)
```
```
Traceback (most recent call last):
  File "[sic]\demo_chinese_clip.py", line 4, in <module>
    model.set_attn_implementation("eager")
  File "[sic]\transformers\modeling_utils.py", line 1975, in set_attn_implementation
    if not submodule._can_set_attn_implementation():
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[sic]\transformers\modeling_utils.py", line 1921, in _can_set_attn_implementation
    code = f.read()
           ^^^^^^^^
  File "[sic]\transformers\Lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 44039: character maps to <undefined>
```

With this PR in place, it simply returns:
```
eager
```

Follow-up from https://github.com/huggingface/transformers/pull/42564#discussion_r2717965498

## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [x] 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).
- [ ] Did you write any new necessary tests?


## Who can review?

@vasqu
It would be very nice to include this in the v5. This caused test failures for my work on #42564 for ChineseCLIP and AyaVision, likely as those modeling files have characters that the default Windows `cp1252` encoding doesn't recognize. 

- Tom Aarsen

Changed files:
filename: src/transformers/modeling_utils.py
patch: @@ -1909,7 +1909,7 @@ def _can_set_attn_implementation(cls) -> bool:
         opening the file, but avoids maintaining yet another property flag.
         """
         class_file = sys.modules[cls.__module__].__file__
-        with open(class_file, "r") as f:
+        with open(class_file, "r", encoding="utf-8") as f:
             code = f.read()
         # heuristic -> if we find those patterns, the model uses the correct interface
         if re.search(r"class \w+Attention\(nn.Module\)", code):
@@ -1927,7 +1927,7 @@ def _can_set_experts_implementation(cls) -> bool:
         opening the file, but avoids maintaining yet another property flag.
         """
         class_file = sys.modules[cls.__module__].__file__
-        with open(class_file, "r") as f:
+        with open(class_file, "r", encoding="utf-8") as f:
             code = f.read()
         # heuristic -> if we the use_experts_implementation decorator is used, then we can set it
         return "@use_experts_implementation" in code