Resolve continuous batching XPU availability checks at runtime
Author: kaixuanliu
Pull request: yes

<!-- ci-dashboard-badge:start -->
[![CI](https://transformers-ci.lor-e.huggingface.cool/badge/pr?pr=47185)](https://transformers-ci.lor-e.huggingface.cool/d/pytest-observability-by-pr/pytest-observability-branch?var-pr=47185)
<!-- ci-dashboard-badge:end -->

## What does this PR do?

This PR updates continuous batching XPU availability checks to resolve through `transformers.utils` at runtime instead of importing `is_torch_xpu_available` directly into continuous batching modules.

This makes the behavior consistent with other runtime accelerator checks such as `torch.cuda.is_available()` and `torch.backends.mps.is_available()`, and allows tests that mock `transformers.utils.is_torch_xpu_available` to correctly simulate a CPU-only environment.

## Why is this needed?

`ContinuousBatchingNoAcceleratorTest::test_continuous_batching_no_accelerators` mocks accelerator availability checks to verify that `generate_batch` works when no accelerator is available.

However, continuous batching modules imported `is_torch_xpu_available` directly, so patching `transformers.utils.is_torch_xpu_available` did not affect the already-bound function references inside:

- `continuous_batching.initialization`
- `continuous_batching.requests`

On XPU-capable environments, the test could still observe XPU as available and fail before reaching the actual CPU-only continuous batching path.

## Changes

- Use `transformers.utils.is_torch_xpu_available()` at call time in continuous batching initialization and request memory/device detection.
- Update the no-accelerator test assertion to verify the patched public `transformers.utils` entry point.

## Tests

```bash
RUN_SLOW=1 pytest -rA tests/generation/test_continuous_batching.py::ContinuousBatchingNoAcceleratorTest::test_continuous_batching_no_accelerators
```
@remi-or @ArthurZucker @McPatate pls help review, thx!

Comments:
Hey @kaixuanliu ! Thanks for catching this, but this seems like a test-time bug. I feel like it should have a test-resident solution. Can we make the test work without touching the utils code please? Thanks! 

@remi-or ,Thanks a lot for taking a look!

I agree that this is primarily exposed by the test setup, and ideally the fix would stay entirely within the test. I tried to find a clean test-only solution, but it is a bit hard to do elegantly because the continuous batching modules import `is_torch_xpu_available` directly, so the test’s patch of `transformers.utils.is_torch_xpu_available` does not affect those already-bound references.

The current change does not modify `transformers.utils` itself or change the behavior of `is_torch_xpu_available`. It only makes continuous batching resolve the same helper through the module at call time. In normal usage this should be equivalent, with the practical difference limited to mock/patch scenarios like this test.

Given the small scope and unchanged runtime semantics, would you be open to accepting the current approach? I think it keeps the fix compact while avoiding more awkward test-specific patching.

Can't we patch "torch.xpu.is_available" in the test file instead? Sorry I would like to test things out myself but I cannot mock a XPU environment easily.

Well, I tried this way before, but `is_torch_xpu_available` is `lru_cache`: [L319](https://github.com/huggingface/transformers/blob/main/src/transformers/utils/import_utils.py#L319), we have to call `is_torch_xpu_available.cache_clear()` before and after the test, which looks weird...

> we have to call is_torch_xpu_available.cache_clear() before and after the test, which looks weird...

That, along with a comment on why we do it this way, seems way better to me. it keeps the test-oriented decision in the test file and the design of the code intact. Does that make sense? 

Thanks for your advice, have updated the code. @remi-or pls help review again.

<!-- ci-dashboard-recap:start -->

---

### CI recap

**Dashboard:** [View test results in Grafana](https://transformers-ci.lor-e.huggingface.cool/d/pytest-observability-by-pr/pytest-observability-branch?var-pr=47185)
**Latest run:** [29386645113:2](https://github.com/huggingface/transformers/actions/runs/29388272613)
**Result:** `success` | **Jobs:** 3 | **Tests:** 144 | **Failures:** 0 | **Duration:** 1m 53s

<!-- ci-dashboard-recap:end -->

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