noamaanMulla-03/transformers-issues huggingface dataset:
DataCollatorForLanguageModeling warning error fixed
Author: mjaliz
Pull request: yes

# What does this PR do?

This PR fixes a bug in the `DataCollatorForLanguageModeling` class that causes a `TypeError` when instantiating the data collator with `whole_word_mask=True`.

## Problem

When users try to instantiate `DataCollatorForLanguageModeling` with `whole_word_mask=True`, they encounter the following error:

```python
TypeError: category must be a Warning subclass, not 'str'
This error occurs in the __post_init__ method at line 724-727, where warnings.warn() is called incorrectly with two separate string arguments. The second string is being interpreted as the category parameter, which must be a Warning subclass (like UserWarning), not a string.
Solution
The fix combines the two warning message strings into a single message and explicitly passes UserWarning as the category parameter, following the pattern used elsewhere in the same file (e.g., line 719-722). Before:
warnings.warn(
    "Random token replacement is not supported with whole word masking.",
    "Setting mask_replace_prob to 1.",
)
After:
warnings.warn(
    "Random token replacement is not supported with whole word masking. "
    "Setting mask_replace_prob to 1.",
    UserWarning,
)
This ensures the data collator can be instantiated correctly when using whole word masking for masked language modeling tasks.
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).
 Did you write any new necessary tests?
Who can review?
@SunMarc @ArthurZucker - This is a small bug fix in the data collator that prevents instantiation with whole word masking enabled.
Note: Discovered and fixed by @mjaliz
y


Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_42144). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Thank you so much for your attention.
Fix qwen moe Load balancing loss calculation outside training
Author: diegoakel
Pull request: yes

# What does this PR do?

Removes the calculation of load balancing loss for Qwen MoE models when outside training mode.

Fixes #42100


## 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?

@Rocketknight1 


Comments:
**[For maintainers]** Suggested jobs to run (before merge)

run-slow: qwen3_moe, qwen3_omni_moe, qwen3_vl_moe

Posted in #42100 as well, but do you know how `output_router_logits` is getting set? This should only happen when that's set to `True`.
Enforce check_auto_docstring
Author: yonigozlan
Pull request: yes

# What does this PR do?

Fix some small issues with auto_docstring and raise an error instead of just warning if something is wrong when running check_auto_docstring

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_41635). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

**[For maintainers]** Suggested jobs to run (before merge)

run-slow: nougat
New `save_strategy` option called "best" to save when a new best performance is achieved.
Author: seanswyi
Pull request: no
Labels: name: Feature request

### Feature request

Introduce a new option for the `save_strategy` argument called `"best"` which would save the model once a new best performance is achieved.

### Motivation

The `save_strategy` argument was first introduced a few years ago in https://github.com/huggingface/transformers/pull/10286. Currently the supported options are `"no"`, `"epoch"`, and `"steps"`. I'm assuming that this is to match the [`IntervalStrategy`](https://github.com/huggingface/transformers/blob/0f67ba1d741d65b07d549daf4ee157609ce4f9c1/src/transformers/trainer_utils.py#L221:L224) that's used by evaluation as well.

Judging by a conversation on a HuggingFace Discussion Forum topic, the best model is always kept by default and therefore if saving occurs at any time during the process then the best model is saved (ref: https://discuss.huggingface.co/t/save-only-best-model-in-trainer/8442). If the user deems that saving often is too burdensome then they may set `save_strategy = "no"` which would save the best model at the end of training.

I believe that introducing some flexibility for saving would be beneficial so that users don't have to perform saving so often but also don't have to wait until the end for a checkpoint.

### Your contribution

If this feature is deemed worth it by the core maintainers then I'd be willing to take this on myself and open a PR. There are some aspects that I believe might warrant further discussion (e.g., which metric should be used to determine "best," how to override `IntervalStrategy`, etc.).

Comments:
cc @muellerzr @SunMarc 

Sure, this seems reasonable. Feel free to open a PR!

It seems like this has been implemented by now. Maybe close the issue?

Done!

Is it impossible to use this option with `load_best_model_at_end`? `load_best_model_at_end` requires that `save_strategy` is the same as `eval_strategy`, but you can't set `eval_strategy` to `best`.
[`T5Gemma`] Fix cross attention cache
Author: vasqu
Pull request: yes

Fixes #41875

The encoder-decoder's cross attn cache was initialized with the same layer types as the self attn cache. However, the model only uses full attention for its cross attentions making this initialization faulty. This only happens with fa2 as it relies on the mask to match with the size of the cache while sdpa just wrongly cuts parts of the mask for example. 

References for cross attn only using full attn:
https://github.com/huggingface/transformers/blob/8472ac683604fca316fa1e5bb10c82064dac7d1b/src/transformers/models/t5gemma/modeling_t5gemma.py#L810-L815
https://github.com/huggingface/transformers/blob/8472ac683604fca316fa1e5bb10c82064dac7d1b/src/transformers/models/t5gemma/modeling_t5gemma.py#L834

Something to note for future PRs with encoder-decoder like models, we should rethink how we use `_get_cache` in the generation utils. Atm, t5gemma doesn't seem to have a default cache implementation so it does not rely on that function. However, newer models may do and we should properly allow to have different caches per self and cross attn. 

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_41890). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

**[For maintainers]** Suggested jobs to run (before merge)

run-slow: t5gemma
fix: improve visibility of ValueError root causes in model config loading
Author: scottzh8
Pull request: yes

##  What does this PR do?

This PR improves error visibility when loading model configurations via `PretrainedConfig._get_config_dict`.

Previously, any `ValueError` raised deep in the Hugging Face Hub stack (for example, when the `hf_transfer` package was missing) was caught by a generic `except Exception` and rethrown as a vague `OSError`.  

With this change, `ValueError`s are explicitly caught and wrapped in an `OSError` that preserves the original message, making it much easier to diagnose environment and dependency issues.

**Example improvement:**
```
Before:
OSError: Can't load the configuration of 'mobicham/Qwen2.5-VL-3B-Instruct_int8wo_ao'. 
If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name...

After:
OSError: Fast download using 'hf_transfer' is enabled (HF_HUB_ENABLE_HF_TRANSFER=1) 
but 'hf_transfer' package is not available in your environment. Try `pip install hf_transfer`.
```

---

## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

Comments:
Hmn, I think this makes sense in some cases, but the problem is that it might make a lot of errors start surfacing in deep internal code and users might not know that this just means something like "you have a directory with the same name as the repo" or something. cc @arthurzucker @cyrilvallez WDYT?

Hey @scottzh8! It can indeed be a good idea, for that very particular case when `HF_HUB_ENABLE_HF_TRANSFER` is set without the associated lib installed. I believe it's the only ValueError that we do not control directly and that can happen. It should however directly be catched and transformed as OSError in `cached_files` in the `hub.py` integration, as we have several instances of the usage of the function. Do you wish to make the change, or do you want me to open a PR?

Thanks @Cyrilvallez and @Rocketknight1 for the context! I'll update the PR and keep you posted
fix: fix Ray Tune trials failing due to removed datasets.load.init_dy…
Author: Priyanshjain10
Pull request: yes
Labels: name: Code agent slop

## Problem

Ray Tune trials fail during initialization due to the function `datasets.load.init_dynamic_modules()` no longer existing in the current datasets library.

## Root Cause

The `init_dynamic_modules()` function was removed from the datasets library API. The code at line 414 of `integration_utils.py` attempts to call this non-existent function, causing an `AttributeError`.

## Solution

Replace the call to the removed function with direct use of the `datasets_modules` variable, which is already initialized and available in the code (line 416).

## Changes

- Modified line 414 to use `datasets_modules` instead of calling `datasets.load.init_dynamic_modules()`
- This allows Ray Tune hyperparameter search to properly initialize worker processes

Fixes #42147

Comments:
feat(kernels): add opt-out flag to disable kernels hub usage through the lib
Author: mfuntowicz
Pull request: yes

This PR attempts to bring an opt-out mecanism to disable hub kernels (aka `kernels`) globally through `transformers`. 

The default value for the `USE_HUB_KERNELS` environment variable defaults to `YES` to maintain backward compatiblity with the current machinery.

In more details, it works by proxying the `kernels.use_kernel_forward_from_hub` method to include our `_kernels_enabled` branching logic. In case of  `_kernels_enabled == False`, the function `use_kernel_forward_from_hub` will return an identity decorator which should just default to the current class the decorator is attached to.

I added tests to a new folder `integrations.tests_hub_kernels.py` to match the layout structure of the sources folder. 
Let me know if it was not the correct way to do, will swizzle at the right place. 

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_41990). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.
handle inputs from Siglip/Siglip2 non-automapped encoder layers
Author: molbap
Pull request: yes

# What does this PR do?

Should fix #41929 . The `check_model_inputs` / `can_record_outputs` interaction is not always trivial and models with several entrypoints such as `VisionModel` vs `VisionTransformer` are missing some, adding it here. Also added a modification in `generic` to make sure the flag was captured, not 100% sure it's needed. 

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_41930). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

run-slow: siglip, siglip2

This comment contains run-slow, running the specified jobs: 

models: ['models/siglip', 'models/siglip2']
quantizations: [] ...

run-slow: siglip, siglip2

This comment contains run-slow, running the specified jobs: 

models: ['models/siglip', 'models/siglip2']
quantizations: [] ...

ended up down the rabbit hole of wrong `PreTrainedModel` inheritances hehe, @yonigozlan if you want to take a look

run-slow: siglip, siglip2

This comment contains run-slow, running the specified jobs: 

models: ['models/siglip', 'models/siglip2']
quantizations: [] ...

run-slow: siglip, siglip2

This comment contains run-slow, running the specified jobs: 

models: ['models/siglip', 'models/siglip2']
quantizations: [] ...

run-slow: siglip, siglip2

This comment contains run-slow, running the specified jobs: 

models: ['models/siglip', 'models/siglip2']
quantizations: [] ...

run-slow: siglip, siglip2

This comment contains run-slow, running the specified jobs: 

models: ['models/siglip', 'models/siglip2']
quantizations: [] ...

yes, needs another PR to make sure tests are actually testing that attentions and hidden states are traced!

Wanna merge this @molbap? I think it's better to go for it than for it to be forgotten :eyes: 

You're right @vasqu , thanks for the push! merging

**[For maintainers]** Suggested jobs to run (before merge)

run-slow: siglip, siglip2
Add slow to some examples tests 
Author: SunMarc
Pull request: yes

# What does this PR do?

This PR puts the slow tests to some e2e tests that takes > 1min on our CI. This should be fine as the CI never really failed because of those tests in my experience except due to flakiness. 

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_42164). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.
Fix lr_scheduler_parsing
Author: SunMarc
Pull request: yes
Labels: name: for patch

# What does this PR do?

This PR fixes `lr_scheduler_kwargs` type to `Optional[Union[dict, str]] `as HfArgumentParser doesn't handled well args that needs to parsed into dict. For now, it is better to just follow what is done for fsdp_config for example. 

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_41322). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.
Check model inputs - hidden states
Author: zucchini-nlp
Pull request: yes
Labels: name: for patch

# What does this PR do?

Fixes https://github.com/huggingface/transformers/issues/40964

In most vision models the `output.hidden_states` are the hiddens right after encoder blocks, i.e. before the last layernorm. Therefore for these models `output.hidden_states != output.last_hidden_state`

Currently `check_model_inputs` assumes that last hidden state is the correct one to return which is true for language models only. This PR adds a kwarg for `check_model_inputs` which decides whether to replace last hidden state or not

TBH, i think the way it is done in LMs is the ultimate correct version and we probably need to "break" vision models. But I can't think of a way to obtain pre-norm last hidden states which are needed for some VLMs

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_40994). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Hi @zucchini-nlp . Are you still working on this PR? Please let me know when it's ready, I'd like to verify it. Thanks!

It should be working with VLMs, just need to fix CI and code style. I'll take it after finishing one huge PR I'm working on currently

I hate rebasing, looks like new models were added to `check_model_inputs`. I will fix them soon

**[For maintainers]** Suggested jobs to run (before merge)

run-slow: aimv2, albert, apertus, arcee, aria, audio_spectrogram_transformer, aya_vision, bert, bert_generation, bitnet, blip, blip_2, blt, camembert, cohere, cohere2
fix(ci): unexpected keyword argument `streaming`
Author: McPatate
Pull request: yes
Labels: name: run-benchmark

I suspect some sort of error where we're not working with the correct up to date code of transformers in the benchmarks, which definitely is an issue.

EDIT: culprit seems to be:
```yml
working-directory: /transformers
```
and thinking of it indeed, it's not what we just checked out but transformers dir of the docler image

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_42102). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

> It's not a technical reason, but it's what I did for all workflow files where we use our CI docker images. But since you maintain the benchmark workflows, it's up to you to make the call :-)

I'd rather keep my workflow standard with the actions/checkout step! Will change if it is too confusing

Ty all!
pin `pytest<9` for now
Author: ydshieh
Pull request: yes

# What does this PR do?

`pytest 9.0.0` is released 4 days ago, causing some problems in our slack reporting. 

There is `9.0.1` just released, let's try it.

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_42162). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

TODO: handle the new format  given by pytest 9

> = 1 failed, 103 passed, 84 skipped, 7 warnings, 16 subtests passed in 61.35s (0:01:01) =






**[For maintainers]** Suggested jobs to run (before merge)

run-slow: vit
Fixes Flash Attention implementation for models 
Author: i3hz
Pull request: yes

# What does this PR do?
Fixes the issue where models use an outdated `if self.config._attn_implementation != "flash_attention_2":` check.

Models changed - SmolVLM, idefics3 , idefics2 

Fixes #42121 


## 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?
@zucchini-nlp  @vasqu 



Comments:
tests seem to be failing so I don't think its the correct one 

**[For maintainers]** Suggested jobs to run (before merge)

run-slow: idefics3, smolvlm

run-slow: idefics3, smolvlm

This comment contains `run-slow`, running the specified jobs: 

models: ["models/idefics3", "models/smolvlm"]
quantizations: []

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_42149). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

## CI Results
[Workflow Run ⚙️](https://github.com/huggingface/transformers/actions/runs/19292926402)

✅ No failing test specific to this PR 🎉 !


> tests seem to be failing so I don't think its the correct one

Hmm it might need a dummy embedding of the correct size along the latents. Would leave this for a different PR 

Also thx for all the PR :hugs: 

>LGTM! I think there were more models which still check for flash_attention_2, would be nice to batch update all. It can totally go in a separate PR later :)

Yeah I didn't want to make a whole lot of changes in a single PR . I find it very confusing , sorry if that wasn't the ideal choice
Does Trainer uses grad scaler for training?
Author: quic-meetkuma
Pull request: no

I am not able to see the grad scaler usage in Trainer code. If not using it then I need to understand how are we using mixed precision training with fp16 precision without grad scaler.

Comments:
Yes. Trainer has an internal variable Accelerator (https://github.com/huggingface/transformers/blob/main/src/transformers/trainer.py#L3793), which utilizes GradScaler. (See: https://github.com/huggingface/accelerate/blob/main/src/accelerate/accelerator.py)

Hey, 
Thanks for a quick reply. Your references are helpful. We can close the thread. :)
[`Pop2Piano`] Fix cache usage
Author: vasqu
Pull request: yes

I don't want to speak about his :see_no_evil: 

Comments:
**[For maintainers]** Suggested jobs to run (before merge)

run-slow: pop2piano

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_42170). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

The speed difference comes from using the cache (or not afterwards). The headmasking PR mistakenly outcommented passing the cache.
Fix helper fn for new processor config format
Author: zucchini-nlp
Pull request: yes

# What does this PR do?

As per title, these helpers are used in vLLM and don't work if we save a processor after v5. Now all processor's subcomponents are saved in one single place in `processor_config.json`, so we need to check it as well when trying to locate the config file

fyi @hmellor 

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_42085). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Oke, now it works. We have to load both (legacy vs non-legacy) configs and then decide which one takes priority because it depends on their content

**[For maintainers]** Suggested jobs to run (before merge)

run-slow: auto