Skip to content

Commit 7c6fa59

Browse files
lxd-cumtptrendx
andauthored
Add entrypoint for flagos multi-backend plugin system (#3401)
* Add NVTE_PLUGIN support for plugin system on main branch Port plugin system changes from backend_rc2.14: - common/__init__.py: load plugin after framework extension init - dot_product_attention.py: override FlashAttention and get_attention_backend when plugin is active Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn> * Fix lint Signed-off-by: Przemyslaw Tredak <ptrendx@gmail.com> --------- Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn> Signed-off-by: Przemyslaw Tredak <ptrendx@gmail.com> Co-authored-by: Xianduo Li <lixianduo@mail.nankai.edu.cn> Co-authored-by: Przemyslaw Tredak <ptredak@nvidia.com> Co-authored-by: Przemyslaw Tredak <ptrendx@gmail.com>
1 parent b97f5ab commit 7c6fa59

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎transformer_engine/common/__init__.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import sys
1717
import sysconfig
1818
from typing import Optional, Tuple
19+
import warnings
1920

2021

2122
@functools.lru_cache(maxsize=None)
@@ -193,6 +194,31 @@ def load_framework_extension(framework: str) -> None:
193194
sys.modules[module_name] = solib
194195
spec.loader.exec_module(solib)
195196

197+
# Plugin system: set NVTE_PLUGIN=<module_name> to let plugin stub take over
198+
# transformer_engine_torch and register original pybind as _nv for CUDA backend.
199+
# Only applies to the PyTorch extension — JAX has no plugin stub.
200+
_nvte_plugin = os.environ.get("NVTE_PLUGIN")
201+
if _nvte_plugin and framework == "torch":
202+
_original_module = sys.modules.get(module_name)
203+
try:
204+
# Register _nv alias BEFORE importing the plugin, because the
205+
# plugin module may import transformer_engine_torch_nv at top level.
206+
sys.modules[module_name + "_nv"] = solib
207+
_plugin = importlib.import_module(_nvte_plugin)
208+
_plugin.load_plugins()
209+
except Exception as e: # pylint: disable=broad-exception-caught
210+
# Rollback to pre-plugin state if plugin failed to fully initialize
211+
sys.modules.pop(module_name + "_nv", None)
212+
if _original_module is not None:
213+
sys.modules[module_name] = _original_module
214+
else:
215+
sys.modules.pop(module_name, None)
216+
warnings.warn(
217+
f"NVTE_PLUGIN={_nvte_plugin} but plugin loading failed: {e}",
218+
RuntimeWarning,
219+
stacklevel=2,
220+
)
221+
196222

197223
def sanity_checks_for_pypi_installation() -> None:
198224
"""Ensure that package is installed correctly if using PyPI."""

‎transformer_engine/pytorch/attention/dot_product_attention/dot_product_attention.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,18 @@ def _delayed_scaling_recipe() -> Optional[DelayedScaling]:
408408
_dpa_fp8ds_reduce_amax = os.getenv("NVTE_DPA_FP8DS_REDUCE_AMAX", "1") == "1"
409409

410410

411+
# Plugin system: override FlashAttention and get_attention_backend if enabled
412+
if os.environ.get("NVTE_PLUGIN"):
413+
import transformer_engine_torch as tex
414+
415+
_FlashAttentionNative = FlashAttention
416+
FlashAttention = getattr(tex, "flash_attention", _FlashAttentionNative)
417+
_plugin_get_attention_backend = getattr(tex, "get_attention_backend", None)
418+
if _plugin_get_attention_backend is not None:
419+
dpa_utils._original_get_attention_backend = dpa_utils.get_attention_backend
420+
dpa_utils.get_attention_backend = _plugin_get_attention_backend
421+
422+
411423
__all__ = ["DotProductAttention"]
412424

413425

0 commit comments

Comments
 (0)