Fix GraniteMoeHybrid in transformers v5
Author: avihu111
Pull request: yes

# What does this PR do?
Re-applies RoPE in GraniteMoEHybrid models when needed (only on non-hybrid models). As done in transformers v4.
<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes https://github.com/huggingface/transformers/issues/42862 


## 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?
@ArthurZucker @Cyrilvallez @zucchini-nlp 
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.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.
 CC: @shawntan 

Models:

- text models: @ArthurZucker @Cyrilvallez
- vision models: @yonigozlan @molbap
- audio models: @eustlb @ebezzam @vasqu
- multimodal models: @zucchini-nlp
- graph models: @clefourrier

Library:

- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- continuous batching: @remi-or @ArthurZucker @McPatate
- pipelines: @Rocketknight1
- tokenizers: @ArthurZucker and @itazap
- trainer: @SunMarc
- attention: @vasqu @ArthurZucker @CyrilVallez
- model loading (from pretrained, etc): @CyrilVallez
- distributed: @3outeille @ArthurZucker
- CIs: @ydshieh

Integrations:

- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization: @SunMarc @MekkCyber
- kernels: @MekkCyber @drbh
- peft: @BenjaminBossan @githubnemo

Devices/Backends:

- AMD ROCm: @ivarflakstad
- Intel XPU: @IlyasMoutawwakil
- Ascend NPU: @ivarflakstad 

Documentation: @stevhliu

Research projects are not maintained and should be taken as is.

 -->


Comments:
I wrote this simple script when making the change - to ensure I get identical values on both a hybrid/non-hybrid model that uses `GraniteMoeHybrid`
```
import transformers
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
device = "cuda"
print("transformers version:", transformers.__version__)
for model_path in ["ibm-granite/granite-4.0-micro", "ibm-granite/granite-4.0-h-tiny"]:
    tokenizer = AutoTokenizer.from_pretrained(model_path)
    model = AutoModelForCausalLM.from_pretrained(model_path, device_map=device)
    model.eval()
    for content in ["Please list one IBM Research laboratory located in the United States. You should only output its name and location.", "Who are you?"]:
        print("=========")
        print(model_path, content)
        chat = [
            { "role": "user", "content": content },
        ]
        chat = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
        input_tokens = tokenizer(chat, return_tensors="pt").to(device)
        output = model.generate(**input_tokens, max_new_tokens=100)
        loss = model(input_ids=output, labels=output).loss.item()
        
        input_length = input_tokens.input_ids.shape[-1]
        output_text = tokenizer.batch_decode(output[...,input_length:])[0]
        print("output: ", output_text)
        print("loss:", loss)      
```



The output I got for version `4.57.3` and `5.0.0.dev0` was identical. Specifically, it was:
```
transformers version: 5.0.0.dev0 # or 4.57.3, same values
use_kernel_func_from_hub is not available in the installed kernels version. Please upgrade kernels to use this feature.
Loading weights: 100%|███████████████████████████████████████████████████████████████| 322/322 [00:01<00:00, 168.90it/s, Materializing param=model.norm.weight]
=========
ibm-granite/granite-4.0-micro Please list one IBM Research laboratory located in the United States. You should only output its name and location.
output:  IBM Research - Almaden

loss: 6.374353408813477
=========
ibm-granite/granite-4.0-micro Who are you?
output:  I am an AI assistant designed to provide accurate, safe, and professional responses to your inquiries. I'm here to help you with a wide range of tasks, from answering questions to providing information and assistance.

loss: 4.3385772705078125
The fast path is not available because one of `(selective_state_update, causal_conv1d_fn, causal_conv1d_update)` is None. Falling back to the naive implementation. To install follow https://github.com/state-spaces/mamba/#installation and https://github.com/Dao-AILab/causal-conv1d
Loading weights: 100%|███████████████████████████████████████████████████████████████| 586/586 [00:04<00:00, 140.39it/s, Materializing param=model.norm.weight]
=========
ibm-granite/granite-4.0-h-tiny Please list one IBM Research laboratory located in the United States. You should only output its name and location.
output:  The name of one IBM Research laboratory located in the United States is the "Almaden Research Center" and it is located in San Jose, California.

loss: 4.215579986572266
=========
ibm-granite/granite-4.0-h-tiny Who are you?
output:  I am an AI language model, designed to assist with a wide range of tasks related to text processing and generation. My goal is to provide helpful, accurate, and safe responses to your queries.

loss: 3.7737364768981934
```

run-slow: granitemoehybrid

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

models: ["models/granitemoehybrid"]
quantizations: []

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

Tests seem to pass!
Interestingly, I get a different outcome when running my testing script:
```
transformers version: 5.0.0.dev0
use_kernel_func_from_hub is not available in the installed kernels version. Please upgrade kernels to use this feature.
Loading weights: 100%|███████████████████████████████████████████████████████████████| 322/322 [00:02<00:00, 124.55it/s, Materializing param=model.norm.weight]
=========
ibm-granite/granite-4.0-micro Please list one IBM Research laboratory located in the United States. You should only output its name and location.
output:  IBM Research - Almaden

loss: 6.3713555335998535
=========
ibm-granite/granite-4.0-micro Who are you?
output:  I am an AI assistant designed to provide accurate, safe, and professional responses to your inquiries. I'm here to help you with a wide range of tasks, from answering questions to providing information and assistance.

loss: 4.341152191162109
The fast path is not available because one of `(selective_state_update, causal_conv1d_fn, causal_conv1d_update)` is None. Falling back to the naive implementation. To install follow https://github.com/state-spaces/mamba/#installation and https://github.com/Dao-AILab/causal-conv1d
Loading weights: 100%|███████████████████████████████████████████████████████████████| 586/586 [00:05<00:00, 109.25it/s, Materializing param=model.norm.weight]
=========
ibm-granite/granite-4.0-h-tiny Please list one IBM Research laboratory located in the United States. You should only output its name and location.
output:  Name: IBM Almaden Research Center
Location: San Jose, California, United States

loss: 4.918633460998535
=========
ibm-granite/granite-4.0-h-tiny Who are you?
output:  I am an AI language model, designed to assist with a wide range of tasks related to text processing and generation. My goal is to provide helpful, accurate, and safe responses to your queries.

loss: 3.7083933353424072
```



Let's also wait for the results from CI tester, seems like there are no available runners at the moment. The test is just waiting...

I will review now, and we'll wait for tests before merging :)

run-slow: granitemoehybrid

Thanks! Looks like they all passed 🎉 

run-slow: granitemoehybrid



slow tests not being triggered again 😢 

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

⚠️ No test being reported (jobs are skipped or cancelled)!


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

models: ["models/granitemoehybrid"]
quantizations: []

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

run-slow: granitemoehybrid

@zucchini-nlp I updated the branch to rerun the tests. 
Can I invoke `build_pr_documentation` somehow?

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

✅ No failing test specific to this PR 🎉 !


I checked the model test file and the integration slow tests were never working 😭  They were skipped until model is released and the authors apparently forgot. The PR deactivates the fn that worked before and you checked that model generates sensible text, so I will merge it

Thanks, and I will activate back the tests