Repository: huggingface/transformers
Pull request: Fix KeyError in GPT-OSS weight conversion script

# What does this PR do?
Fixes a `KeyError: 'rope_parameters_factor'` in the GPT-OSS weight conversion script that was preventing users from converting GPT-OSS model weights to HuggingFace format.

Fixes #42003

## Problem
After PR #39847 standardized RoPE parameter handling across models, the GPT-OSS conversion script was still trying to access individual parameter keys that no longer exist in the original config:

```python

# Old code - these keys don't exist anymore
"beta_fast": float(original_config.pop("rope_ntk_beta")), 
"beta_slow": float(original_config.pop("rope_ntk_alpha")),
"factor": float(original_config.pop("rope_parameters_factor")),
```

## Reproduction
```python
huggingface-cli download openai/gpt-oss-20b
python src/transformers/models/gpt_oss/convert_gpt_oss_weights_to_hf.py \
    --input_dir path/to/original \
    --output_dir ./output

# KeyError: 'rope_parameters_factor'
```

## Solution
Added proper handling for both config formats: 
```python
# Handle both old and new config formats for rope_parameters
    if "rope_parameters" in original_config:
        # New format: rope_parameters already exists as a dict
        rope_parameters = original_config.pop("rope_parameters")
        # Ensure rope_type is set
        if "rope_type" not in rope_parameters:
            rope_parameters["rope_type"] = "yarn"
    else:
        # Old format: construct rope_parameters from individual keys
        # Fallback to default values if rop_parameters is missing
        rope_parameters = {
            # "beta_fast": float(original_config.pop("rope_ntk_beta")),
            # "beta_slow": float(original_config.pop("rope_ntk_alpha")),
            # "factor": float(original_config.pop("rope_parameters_factor")),
            "rope_type": "yarn",
            "truncate": False,
            "original_max_position_embeddings": 4096,
            "beta_fast": 32.0,
            "beta_slow": 1.0,
            "factor": 32.0,
        }
```

## Testing
✅ Verified conversion script no longer throws KeyError
✅ Tested fallback logic with both config formats
✅ Default values match `configuration_gpt_oss.py`

## 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, Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum]? Please add a link to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? (N/A - bug fix in conversion script)
- [ ] Did you write any new necessary tests? (Added manual verification test)


## Who can review?
@zucchini-nlp 

Changed files:
filename: src/transformers/models/gpt_oss/convert_gpt_oss_weights_to_hf.py
patch: @@ -157,14 +157,24 @@ def write_model(
     original_config = json.loads((Path(input_base_path) / "config.json").read_text())
 
     num_local_experts = original_config.pop("num_experts")
-    rope_parameters = {
-        "beta_fast": float(original_config.pop("rope_ntk_beta")),
-        "beta_slow": float(original_config.pop("rope_ntk_alpha")),
-        "factor": float(original_config.pop("rope_parameters_factor")),
-        "rope_type": "yarn",
-        "truncate": False,
-        "original_max_position_embeddings": 4096,
-    }
+
+    # Handle both old and new config formats for rope_parameters
+    if "rope_parameters" in original_config:
+        # New format: rope_parameters already exists as a dict
+        rope_parameters = original_config.pop("rope_parameters")
+        # Ensure rope_type is set
+        if "rope_type" not in rope_parameters:
+            rope_parameters["rope_type"] = "yarn"
+    else:
+        # Old format: construct rope_parameters from individual keys with defaults matching GptOssConfig
+        rope_parameters = {
+            "factor": float(original_config.pop("rope_parameters_factor", 32.0)),
+            "beta_fast": float(original_config.pop("rope_ntk_beta", 32.0)),
+            "beta_slow": float(original_config.pop("rope_ntk_alpha", 1.0)),
+            "rope_type": "yarn",
+            "truncate": False,
+            "original_max_position_embeddings": 4096,
+        }
 
     config = GptOssConfig(
         num_local_experts=num_local_experts,