Repository: huggingface/transformers
Pull request: Disable custom MRA kernels for ROCm

# What does this PR do?

This PR is a clone of https://github.com/huggingface/transformers/pull/38578 with a small fix, this time targeting main. It's part of a series of PRs propagating back fixes for AMD devices from https://github.com/huggingface/transformers/tree/amd-hf-ci-branch .

TLDR; it disables the MRA CUDA extensions for AMD devices as the kernels are broken on the platform at the moment.


## 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.

<!-- 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.

Models:

- text models: @ArthurZucker
- vision models: @amyeroberts, @qubvel
- speech models: @eustlb
- graph models: @clefourrier

Library:

- flax: @gante and @Rocketknight1
- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Rocketknight1
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @zach-huggingface and @SunMarc
- chat templates: @Rocketknight1

Integrations:

- deepspeed: HF Trainer/Accelerate: @SunMarc @zach-huggingface
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc @MekkCyber

Documentation: @stevhliu

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @Rocketknight1
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


Changed files:
filename: src/transformers/models/mra/modeling_mra.py
patch: @@ -35,7 +35,7 @@
 )
 from ...modeling_utils import PreTrainedModel
 from ...pytorch_utils import apply_chunking_to_forward, find_pruneable_heads_and_indices, prune_linear_layer
-from ...utils import auto_docstring, is_ninja_available, is_torch_cuda_available, logging
+from ...utils import auto_docstring, is_cuda_platform, is_ninja_available, is_torch_cuda_available, logging
 from .configuration_mra import MraConfig
 
 
@@ -528,7 +528,7 @@ def __init__(self, config, position_embedding_type=None):
             )
 
         kernel_loaded = mra_cuda_kernel is not None
-        if is_torch_cuda_available() and is_ninja_available() and not kernel_loaded:
+        if is_torch_cuda_available() and is_cuda_platform() and is_ninja_available() and not kernel_loaded:
             try:
                 load_cuda_kernels()
             except Exception as e:

filename: src/transformers/utils/__init__.py
patch: @@ -141,6 +141,7 @@
     is_ccl_available,
     is_coloredlogs_available,
     is_compressed_tensors_available,
+    is_cuda_platform,
     is_cv2_available,
     is_cython_available,
     is_datasets_available,
@@ -201,6 +202,7 @@
     is_quark_available,
     is_rich_available,
     is_rjieba_available,
+    is_rocm_platform,
     is_sacremoses_available,
     is_safetensors_available,
     is_sagemaker_dp_enabled,

filename: src/transformers/utils/import_utils.py
patch: @@ -484,6 +484,24 @@ def is_torch_cuda_available():
         return False
 
 
+def is_cuda_platform():
+    if is_torch_available():
+        import torch
+
+        return torch.version.cuda is not None
+    else:
+        return False
+
+
+def is_rocm_platform():
+    if is_torch_available():
+        import torch
+
+        return torch.version.hip is not None
+    else:
+        return False
+
+
 def is_mamba_ssm_available():
     if is_torch_available():
         import torch