TE-FL Custom Backend Examples#
This directory contains examples demonstrating two ways to add custom backends.
Two Approaches#
Approach |
Use Case |
Example File |
|---|---|---|
In-tree |
Open source contribution, direct integration |
|
Out-of-tree |
Closed-source / third-party plugin, standalone package |
|
Quick Start#
cd transformer_engine/plugin/examples
# In-tree approach
python example_intree.py
# Out-of-tree approach
python example_outtree.py
In-tree Approach (3 Steps)#
from transformer_engine.plugin.core import (
OpRegistry, OpManager, OpImpl, BackendImplKind
)
# 1. Define your operator implementation
def my_rmsnorm(input, weight, eps=1e-5, **kwargs):
variance = input.pow(2).mean(-1, keepdim=True)
return input * torch.rsqrt(variance + eps) * weight, torch.rsqrt(variance + eps)
# 2. Register to Registry
registry = OpRegistry()
registry.register_impl(OpImpl(
op_name="rmsnorm_fwd",
impl_id="vendor.mybackend",
kind=BackendImplKind.VENDOR,
vendor="mybackend",
fn=my_rmsnorm,
priority=200,
))
# 3. Call via Manager
manager = OpManager(registry)
output, rsigma = manager.call("rmsnorm_fwd", input, weight)
Out-of-tree Approach (Plugin Package)#
Plugin Package Structure#
my_vendor_plugin/
├── __init__.py # Contains register(registry) function
└── setup.py # or pyproject.toml
__init__.py#
from transformer_engine.plugin.core import OpImpl, BackendImplKind
def my_rmsnorm(input, weight, eps=1e-5, **kwargs):
# Your implementation
...
def register(registry):
"""Called automatically by TE-FL"""
registry.register_impl(OpImpl(
op_name="rmsnorm_fwd",
impl_id="vendor.myvendor",
kind=BackendImplKind.VENDOR,
vendor="myvendor",
fn=my_rmsnorm,
priority=200,
))
Loading Methods#
# Method 1: Environment variable
export TE_FL_PLUGIN_MODULES=my_vendor_plugin
python your_script.py
# Method 2: pip install (requires entry_points configuration)
pip install my-vendor-plugin
python your_script.py
Environment Variables#
Backend Selection#
Variable |
Description |
Values |
Default |
|---|---|---|---|
|
Preferred backend type (highest priority) |
|
|
|
Prefer vendor backend (legacy, lower priority than |
|
|
|
Strict mode - raise error if preferred implementation fails instead of fallback |
|
|
Vendor Filtering#
Variable |
Description |
Example |
|---|---|---|
|
Whitelist of allowed vendors (comma-separated) |
|
|
Blacklist of denied vendors (comma-separated) |
|
Per-Operator Configuration#
Variable |
Description |
Example |
|---|---|---|
|
Per-operator backend ordering |
|
Format: op_name=backend1|backend2;op_name2=backend3|backend4
Plugin Discovery#
Variable |
Description |
Example |
|---|---|---|
|
Plugin modules to load (comma-separated) |
|
Build Configuration#
Variable |
Description |
Values |
Default |
|---|---|---|---|
|
Skip CUDA backend (both build-time and runtime) |
|
|
|
CUDA installation path |
|
Auto-detected |
|
Alternative CUDA path variable |
|
Auto-detected |
Logging#
Variable |
Description |
Values |
Default |
|---|---|---|---|
|
Log level for TE-FL |
|
|
Examples#
Prefer vendor backend#
export TE_FL_PREFER=vendor
python your_script.py
Only allow specific vendors#
export TE_FL_ALLOW_VENDORS=nvidia,acme
python your_script.py
Custom per-operator ordering#
# Use acme vendor for rmsnorm, flagos for others
export TE_FL_PER_OP="rmsnorm_fwd=vendor:acme|flagos"
python your_script.py
Skip CUDA and use FlagOS only#
export TE_FL_SKIP_CUDA=1
export TE_FL_PREFER=flagos
python your_script.py
Enable debug logging#
export TEFL_LOG_LEVEL=DEBUG
python your_script.py
Expected Output#
When running, you should see logs like:
[TE-FL manager.py:133 INFO] Registered impl_ids: ['default.flagos', 'reference.torch', 'vendor.mybackend']
[TE-FL manager.py:390 INFO] Op 'rmsnorm_fwd' using 'vendor.mybackend' (kind=vendor, vendor=mybackend)