Skip to content

Commit 8690ab4

Browse files
authored
Add new register op get_attention_backend for METAX (#31)
# Description Add new register op get_attention_backend for METAX Fixes # (issue) ## Type of change - [ ] New feature (non-breaking change which adds functionality) ## Changes Please list the changes introduced in this PR: - Add register for get_attention_backend in register_ops.py - Add implement of get_attention_backend in metax.py # Checklist: - [x] I have read and followed the [contributing guidelines](https://github.com/NVIDIA/TransformerEngine/blob/main/CONTRIBUTING.rst) - [x] The functionality is complete - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes
1 parent 35e1809 commit 8690ab4

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

transformer_engine/plugin/core/backends/vendor/metax/metax.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,23 @@ def get_flash_attention_class(self):
158158
from .flash_attention import FlashAttentionMETAX
159159
return FlashAttentionMETAX
160160

161+
def get_attention_backend(self, attention_params=None):
162+
# Import the metax get_attention_backend function
163+
try:
164+
from transformer_engine_metax.pytorch.attention.dot_product_attention import utils
165+
return utils.get_attention_backend(attention_params)
166+
167+
except ImportError as e:
168+
raise RuntimeError(
169+
f"Failed to import metax FlashAttention: {e}. "
170+
"Please ensure flash-attn is installed and transformer_engine_metax is available."
171+
)
172+
except Exception as e:
173+
raise RuntimeError(
174+
f"Failed to get_attention_backend: {e}. "
175+
f"Attention_params: {self.attention_params}"
176+
)
177+
161178
def quantize(
162179
self,
163180
tensor: torch.Tensor,

transformer_engine/plugin/core/backends/vendor/metax/register_ops.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ def register_builtins(registry) -> None:
197197

198198
# FlashAttention class getter
199199
OpImpl(op_name="get_flash_attention_class", impl_id="vendor.metax", kind=BackendImplKind.VENDOR, fn=_bind_is_available(backend.get_flash_attention_class, is_avail), vendor="METAX", priority=100),
200+
# Attention backend selection
201+
OpImpl(op_name="get_attention_backend", impl_id="vendor.metax", kind=BackendImplKind.VENDOR, fn=_bind_is_available(backend.get_attention_backend, is_avail), vendor="METAX", priority=100),
200202
]
201203

202204
registry.register_many(impls)

0 commit comments

Comments
 (0)