[xLSTM] Crash in train mode with return_last_states=False: backend returns bare tensor but xLSTMLayer always unpacks (h, state)
Author: Heatdh
Pull request: no

### System Info

- `transformers` version: 5.12.1 (bug also present on current `main`)
- Platform: Windows 11 (also reproducible on Linux)
- Python version: 3.13
- PyTorch version: 2.11.0+cu128

### Who can help?

@vasqu @ArthurZucker

### Information

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

### Reproduction

```python
import torch
from transformers import xLSTMConfig
from transformers.models.xlstm.modeling_xlstm import xLSTMModel

config = xLSTMConfig(
    hidden_size=128,
    num_hidden_layers=2,
    num_heads=4,
    mode="train",
    return_last_states=False,  # documented option, config default is True
    chunkwise_kernel="chunkwise--native_autograd",
    sequence_kernel="native_sequence__native",
    step_kernel="native",
)
model = xLSTMModel(config)
input_ids = torch.randint(0, config.vocab_size, (2, 64))
out = model(input_ids)  # raises
```

Output (batch_size=2):

```
  File ".../transformers/models/xlstm/modeling_xlstm.py", line 1164, in forward
    raise ValueError(f"Got {h.shape}, expected {expected_h_shape}")
ValueError: Got torch.Size([4, 64, 32]), expected (2, 4, 64, 32)
```

With any batch size other than 2, e.g. `(3, 64)`:

```
  File ".../transformers/models/xlstm/modeling_xlstm.py", line 1147, in forward
    h, state = self.mlstm_backend(
ValueError: too many values to unpack (expected 2)
```

### Root cause

`xLSTMBackend.forward` in train mode returns whatever the chunkwise kernel returns — a **bare tensor** when `return_last_states=False`, a tuple `(h, last_states)` when `True`:

https://github.com/huggingface/transformers/blob/main/src/transformers/models/xlstm/modeling_xlstm.py#L823-L833

But the only caller, `xLSTMLayer.forward`, unconditionally unpacks two values:

https://github.com/huggingface/transformers/blob/main/src/transformers/models/xlstm/modeling_xlstm.py#L1147-L1156

So with `return_last_states=False` the returned hidden-state tensor of shape `(batch, nh, seq, dh)` is iterated along the batch dimension:

- `batch_size == 2`: it "successfully" unpacks into two `(nh, seq, dh)` tensors and only the shape assertion at line 1163 catches it.
- any other batch size: `ValueError: too many/not enough values to unpack`.

### Relation to #43208 / #43209

Distinct from bug 3 of #43208. #43209 fixed the `return_last_states` **default plumbing** (signature default `False` → `None` so the config value is honored). Since the config default is `True`, the default path now works — but explicitly configuring `return_last_states=False` (the documented option for training, where last states are not needed) still crashes as above.

### Expected behavior

Forward pass succeeds and returns hidden states for any batch size when `return_last_states=False` in train mode.

Possible fixes: have `xLSTMLayer.forward` handle the bare-tensor return (`state = None`), or make `xLSTMBackend.forward` always return a `(h, state)` tuple with `state=None` when last states are not requested.

**Workaround**: force `return_last_states=True` in the config even for training and discard the returned states.


Comments:
I think we should always return a tuple and use the output recorder (similar to router logits in moe or attentions). I don't have much time atm but open for any PR to fix this 🤗 

Reproduced this and opened a fix: https://github.com/huggingface/transformers/pull/47026

I opened #47026 first for the core crash in #47013, but after reviewing #47046 I think it's the more complete fix. It additionally handles `use_cache=True` with `train_with_padding` (falls back with a warning instead of crashing) and a backward pass failure caused by an in-place cache update, with a regression test that calls `.backward()` to catch it. My PR does not cover either of those cases. I'd suggest merging #47046 and closing #47026, thanks to @Heatdh for catching the additional issues.