noamaanMulla-03/transformers-issues huggingface dataset:
warning bug in Qwen2DecoderLayer in transformers ==4.49
Author: Kyrie666
Pull request: no
Labels: name: bug

### System Info

 transformers ==4.49

### Who can help?

_No response_

### Information

- [x] The official example scripts
- [ ] My own modified scripts

### Tasks

- [x] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

`class Qwen2DecoderLayer(nn.Module):
    def __init__(self, config: Qwen2Config, layer_idx: int):
        super().__init__()
        self.hidden_size = config.hidden_size
        self.self_attn = Qwen2Attention(config=config, layer_idx=layer_idx)
        self.mlp = Qwen2MLP(config)
        self.input_layernorm = Qwen2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
        self.post_attention_layernorm = Qwen2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
        if config.sliding_window and config._attn_implementation != "flash_attention_2":
            logger.warning_once(
                f"Sliding Window Attention is enabled but not implemented for `{config._attn_implementation}`; "
                "unexpected results may be encountered."
            )
`
config.sliding_window is a number , the warning active 100%
the code should be config.use_sliding_window ?


### Expected behavior

`class Qwen2DecoderLayer(nn.Module):
    def __init__(self, config: Qwen2Config, layer_idx: int):
        super().__init__()
        self.hidden_size = config.hidden_size
        self.self_attn = Qwen2Attention(config=config, layer_idx=layer_idx)
        self.mlp = Qwen2MLP(config)
        self.input_layernorm = Qwen2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
        self.post_attention_layernorm = Qwen2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
        if config.sliding_window and config._attn_implementation != "flash_attention_2":
            logger.warning_once(
                f"Sliding Window Attention is enabled but not implemented for `{config._attn_implementation}`; "
                "unexpected results may be encountered."
            )
`
config.sliding_window is a number , the warning active 100%
the code should be config.use_sliding_window ?


Comments:
Hey @Kyrie666 I have submitted a PR to fix this issue: #36377


This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.
Fix test
Author: Cyrilvallez
Pull request: yes

# What does this PR do?

Device context manager is not the intended API to use with `from_pretrained`.
It used to work before, BUT ONLY with `low_cpu_mem_usage=False`, so without consistency.

Will try to investigate if we can easily detect the context and turch it into a simple `device_map` however.


Comments:
Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the `Ready for review` button (at the bottom of the PR page). This will assign reviewers and trigger CI.

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

we can merge this one however. The another fix is an  improvement but no need to use it for the test here :-). It it helpful for users however 👍
[CI] lazy loading external datasets
Author: gante
Pull request: yes

# What does this PR do?

Follow-up to #37202: add lazy loading to the class-level test dataset 🙌 

With recent changes, any test command using a tester that inherits from `PipelineTesterMixin` will trigger the dataset loading for all pipeline tester classes, even if those datasets are never user. For instance, `py.test tests/models/llama/test_modeling_llama.py` was now loading all datasets, causing the test time to rise substantially (from 17 to 24 seconds on my machine).

This PR adds lazy loading to the datasets, i.e. load a dataset only when a related test is run. It's still loaded once per pytest process, keeping the benefits from the previous PRs 🤗

Note: `setUpClass` is often used for this, but the pipeline tests have a complex inheritance system (related PR: https://github.com/huggingface/transformers/pull/37214)

Comments:
Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the `Ready for review` button (at the bottom of the PR page). This will assign reviewers and trigger CI.

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_37218). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.
[generate] prepare `attention_mask` outside `forward` whenever possible
Author: gante
Pull request: yes

# What does this PR do?

WIP

For better use of `generate` + `flex_attention`

Comments:
Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the `Ready for review` button (at the bottom of the PR page). This will assign reviewers and trigger CI.

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

Closing -- flex attention needs specialized manipulation of the block mask
Fix AST parsing when looking for remote code imports
Author: Rocketknight1
Pull request: yes

The AST parsing code in `dynamic_module_utils` had a bug because it assumed that certain `Call` nodes had to be calling a named function and not something else like a method.

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_37245). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.
Add support for fast image processing in image-pretraining example
Author: jafraustro
Pull request: yes

# What does this PR do?
Add support for fast image processing in image-pretraining example

- Fix typo: correct tuple formatting in IMAGE_PROCESSOR_MAPPING_NAMES
- Add support for fast image processing in image-pretraining example

Fixes #37020 

## Before submitting
- [x] 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),
 
## Who can review?

Anyone in the community is free to review the PR once the tests have passed. 

Maintained examples:
@amyeroberts, @qubvel
 


Comments:
Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the `Ready for review` button (at the bottom of the PR page). This will assign reviewers and trigger CI.

@qubvel Thanks for the feedback. I have committed your suggestions

Waiting for CI green and merging, thanks @jafraustro
run_mim.py script from image-pretraining example is not working
Author: jafraustro
Pull request: no
Labels: name: bug

### System Info

- `transformers` version: 4.51.0.dev0
- Platform: Linux-5.15.0-73-generic-x86_64-with-glibc2.35
- Python version: 3.12.9
- Huggingface_hub version: 0.29.3
- Safetensors version: 0.5.3
- Accelerate version: 1.5.2
- Accelerate config:    not found
- DeepSpeed version: not installed
- PyTorch version (GPU?): 2.7.0.dev20250224+xpu (False)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed

### Who can help?

@amyeroberts, @qubvel

### Information

- [x] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

Follow README instructions to run `run_mim.py`

```bash
!python run_mim.py \
    --model_type vit \
    --output_dir ./outputs/ \
    --overwrite_output_dir \
    --remove_unused_columns False \
    --label_names bool_masked_pos \
    --do_train \
    --do_eval \
    --learning_rate 2e-5 \
    --weight_decay 0.05 \
    --num_train_epochs 100 \
    --per_device_train_batch_size 8 \
    --per_device_eval_batch_size 8 \
    --logging_strategy steps \
    --logging_steps 10 \
    --eval_strategy epoch \
    --save_strategy epoch \
    --load_best_model_at_end True \
    --save_total_limit 3 \
    --seed 1337
```

### Expected behavior

The example is broken.

Output:
```

  File "/repos/transformers/examples/pytorch/image-pretraining/run_mim.py", line 483, in <module>
    main()
  File "/repos/transformers/examples/pytorch/image-pretraining/run_mim.py", line 362, in main
    image_processor = IMAGE_PROCESSOR_TYPES[model_args.model_type]()
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'tuple' object is not callable

```

Comments:
Allow flexible generation params arg when checking pipeline specs
Author: Rocketknight1
Pull request: yes

We currently check the input and output signatures of our Pipeline classes against the specs in `huggingface_hub`, but this makes it hard for the Hub to change without breaking our CI. This PR allows them to change their spec from `generate_kwargs` to `generation_parameters` without causing failures at our end.

Comments:
cc @Wauplin 

Not mean to review this PR, but left 2 nit comments 

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

Added the docstring per @ydshieh's suggestion. Also yes, @wauplin is right - `js_generate_args` is an old name and the correct name should have been `hub_generate_args`. I made that replacement!
[CI] green llama tests
Author: gante
Pull request: yes

# What does this PR do?

Before start the work to refactor models, let's make the tests on our base model green 🤗 All tests on llama are green after this PR, except for the flex attention tests (which is a WIP feature)

Fixes:
- `batch_size` -> `max_batch_size` in `StaticCache` (#37007)
- `generate` + FA2 test -> don't pass attention masks with right-padding (FA2 doesn't support all attention mask patterns, and with generate the mask would have holes like `1 1 0 0 0 0 1` at generation time)
- models with compilation integration tests -> clear CUDA cache (see comment in `LlamaIntegrationTest.tearDown`)



Comments:
Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the `Ready for review` button (at the bottom of the PR page). This will assign reviewers and trigger CI.

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_37244). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.
More ReDOS fixes!
Author: Rocketknight1
Pull request: yes

This PR replaces three more problematic regexes. In all cases I eliminate the regex entirely. 

When parsing Python files for `import` statements we now rely on `ast.parse()` instead. This is the most complex code but it seems to pass tests.

In `chat.py` I think I figured out what the regex was doing and wrote a Python method to duplicate its functionality - cc @gante to confirm I got it right! 

Finally, the change in `configuration_utils.py` was very straightforward; we could just use `str.startswith()` and `str.endswith()` instead.

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

`ast.parse()` seems quite performant for me! I parsed `modeling_phi4.py` (2.3k LOC) in ~10ms. 

@Rocketknight1 yeah, the `chat` changes look good to me

One more performance note: Since most Transformers code is contained within classes, we can speed up walking the `ast` tree a lot if we just don't recurse into classes. This will miss imports inside classes, but I don't know if we count those as required imports or not, and probably we don't want to?

However, I don't think this is really that important - parsing and walking the tree takes << 1s for me. I'm not even sure it's slower than the regex solution!

cc @Michellehbn merged! Should be in the next release and then we can close out those issues

@Rocketknight1 

Hi Matt, I find the ast-style parsing would introduce an error if a function is `ast.Attribute` rather than a `ast.Name`. Here's a minimal reproduction:

```model.py
import torch.nn as nn


class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(10, 10)

    def forward(self, x):
        if x.size() == 1:   # error
            raise ValueError("x.size() == 1")
        return self.linear(x)
```

When I try to run `get_imports("debug_model.py")`, it would lead to this error. Here I changed the code a little bit to help debugging. Maybe we should add an additional if-else judgment to check if a func is `ast.Name` ?


![image](https://github.com/user-attachments/assets/b80a6ebc-095f-414f-80fb-9d462531e79f)


Hi @Spico197, yes, we noticed that bug too! My fault - I fixed it here: https://github.com/huggingface/transformers/pull/37245

> Hi @Spico197, yes, we noticed that bug too! My fault - I fixed it here: #37245

Thanks for the prompt reply! Really impressive fast feedback~
fix tied weigths isuue 
Author: ydshieh
Pull request: yes

# What does this PR do?

An update of #33913 before merge. That PR is approved by @ArthurZucker , but since then a huge refactorization is done by @Cyrilvallez, and I prefer to make sure the updated changes still looks fine for @Cyrilvallez .

In order to see the effect, run the following code snippet. On `main` it fails and on this PR it passes.

Fix #33689 and #33688


```python
import torch
from transformers import AutoModelForCausalLM

configs = [
    (torch.float32, False, "cpu"   ),  # fails
    #(torch.float16, True,  "cpu"   ),  # passes
    #(torch.float16, False, "cpu"   ),  # passes
    #(torch.float32, True,  "cpu"   ),  # passes
    #(torch.float32, False, "cpu"   ),  # fails
    #(torch.float32, False, "cuda:0"),  # passes
]

def test_model_save(torch_dtype, tie_word_embeddings, device_map, tmp_path="./"):
    model = AutoModelForCausalLM.from_pretrained(
        "Xenova/llama2.c-stories15M",
        torch_dtype=torch_dtype,
        tie_word_embeddings=tie_word_embeddings,
        device_map=device_map,
        revision="ccdd47c2dc554aeecd2bb4e713e1c988f206a296",
    )
    model.save_pretrained(tmp_path, safe_serialization=True)


for config in configs:
    test_model_save(*config)

```

Comments:
Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the `Ready for review` button (at the bottom of the PR page). This will assign reviewers and trigger CI.

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

For future reference 

```python
import torch
from transformers import LlamaForCausalLM, LlamaConfig

token = "YOUR_TOKEN"
config = LlamaConfig.from_pretrained("meta-llama/Llama-2-7b-hf", token=token)
config.vocab_size = 128
config.hidden_size = 64
config.intermediate_size = 64
config.num_attention_heads = 4
config.num_key_value_heads = 4
num_hidden_layers = 2

model = LlamaForCausalLM(config=config)
# model = LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf", token=token)

model.save_pretrained("tiny_llama", safe_serialization=False)

# Get a model with tied weights
tie_word_embeddings = True
model2 = LlamaForCausalLM.from_pretrained("tiny_llama", use_safetensors=False, tie_word_embeddings=tie_word_embeddings)
model2.save_pretrained("tiny_llama_tied_weights", safe_serialization=False)

model3 = LlamaForCausalLM.from_pretrained("tiny_llama_tied_weights", use_safetensors=False, tie_word_embeddings=False, torch_dtype=torch.float32, device_map="cpu")
model3.save_pretrained("tiny_llama_tied_weights_saved_again", safe_serialization=False)
model3.save_pretrained("tiny_llama_tied_weights_saved_again", safe_serialization=True)


```
[don't merge yet] Revert "Revert #37031"
Author: ydshieh
Pull request: yes

Reverts huggingface/transformers#37178



Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_37252). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.
Purge unused ModelTester code
Author: Rocketknight1
Pull request: yes

Delete lots of `ModelTester` methods that are never actually called inside test cases

Comments:
cc @ydshieh this should be ready for review now!

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_37085). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.
Bye bye env vars, keep everything as configs
Author: muellerzr
Pull request: yes

# What does this PR do?

As @BenjaminBossan found, (https://github.com/huggingface/accelerate/pull/3252), the `TrainingArguments` will set environmental variables automatically when using Accelerate because before it wouldn't work otherwise. Nowadays the only env variable required for things to run smoothly is the ones for model init (fsdp cpu eff ram). 

This PR does a few things:

1. We completely remove the need for environmental variables, creating the proper configs (dynamo relies on https://github.com/huggingface/accelerate/pull/3251)
2. I've refactored how `mixed_precision` gets set, to simplify the training arguments and combine 7 args into 2. 

Fixes # (issue)


## 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?
- [ ] 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?

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.

@SunMarc @Rocketknight1 


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

Thanks for cleaning up the use of env vars. I only did a quick check, are there deprecation warnings added for fp16, bf16, etc.? It should be clearly communicated when these arguments will be removed, as a lot of code would be affected.

Not yet but it will be, meant to open this as a draft just to make sure we're all okay w/ the core parts

We should not forget about this PR.

Will be opening a new PR for this since there's lots of conflicts today
[qwen-vl] fix image processor
Author: zucchini-nlp
Pull request: yes

# What does this PR do?

The example in the docs for configuring image size doesn't work anymore after the last refactor (https://huggingface.co/docs/transformers/en/model_doc/qwen2_vl#image-resolution-trade-off). This PR ensure backwards compatibility for qwen based image processors

Found when fixing Qwen-Omni which uses this processor class


Comments:
Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the `Ready for review` button (at the bottom of the PR page). This will assign reviewers and trigger CI.

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_37258). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.
Update model card for electra
Author: Wu-n0
Pull request: yes

# What does this PR do?

This PR updates the ELECTRA model card to follow the standardized format outlined in issue #36979. The updated model card includes:

- A conversational description of how ELECTRA works and what makes it unique
- Code examples for using ELECTRA with both Pipeline and AutoModel
- A quantization example for the large model
- Detailed usage notes with practical tips

Note: I did not include a transformers-cli example because ELECTRA is a discriminative model for classification tasks rather than text generation, making it less suitable for CLI interaction.

I also couldn't include an AttentionMaskVisualizer example because it's not currently supported with ELECTRA. When attempting to implement it locally, I encountered this error:
`AttributeError: '_ModelWrapper' object has no attribute '_update_causal_mask'`

## Before submitting
- [X] This PR fixes a typo or improves the docs 


## Who can review?

@stevhliu


Comments:
Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the `Ready for review` button (at the bottom of the PR page). This will assign reviewers and trigger CI.

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

I've implemented all the requested changes.