Operator dispatch mechanism#

This directory implements the operator dispatch mechanism for vllm-plugin-FL, providing a flexible operator dispatch system that selects between different backend implementations (FlagGems, PyTorch, vendor-specific) based on availability and policy configuration.

Directory structure#

dispatch/
β”œβ”€β”€ __init__.py              # Module entry point, exports public API
β”œβ”€β”€ types.py                 # Core type definitions (OpImpl, BackendImplKind)
β”œβ”€β”€ registry.py              # Thread-safe operator registry
β”œβ”€β”€ policy.py                # Selection policy management
β”œβ”€β”€ manager.py               # Core dispatch manager
β”œβ”€β”€ builtin_ops.py           # Built-in operator registration
β”œβ”€β”€ ops.py                   # Backend base interface
β”œβ”€β”€ discovery.py             # Plugin discovery mechanism
β”œβ”€β”€ logger_manager.py        # Centralized logging configuration
β”œβ”€β”€ config/                  # Platform-specific configurations
β”‚   β”œβ”€β”€ __init__.py          # Config loader module
β”‚   β”œβ”€β”€ ascend.yaml          # Ascend NPU default configuration
β”‚   └── cuda.yaml            # CUDA default configuration
└── backends/                # Backend implementations
    β”œβ”€β”€ base.py              # Backend abstract base class
    β”œβ”€β”€ flaggems/            # FlagGems backend (DEFAULT, priority 150)
    β”‚   β”œβ”€β”€ flaggems.py      # Backend class
    β”‚   β”œβ”€β”€ register_ops.py  # Registration function
    β”‚   └── impl/            # Operator implementations
    β”‚       β”œβ”€β”€ activation.py
    β”‚       β”œβ”€β”€ normalization.py
    β”‚       β”œβ”€β”€ rotary.py
    β”‚       β”œβ”€β”€ attention.py       # AttentionFLBackend, AttentionFLImpl
    β”‚       β”œβ”€β”€ mla.py             # MLAFLBackend, MLAFLImpl
    β”‚       └── custom_attention.py # Attention backend registration
    β”œβ”€β”€ reference/           # Reference backend (PyTorch, priority 50)
    └── vendor/              # Vendor-specific backends (priority 100)
        β”œβ”€β”€ cuda/            # NVIDIA CUDA backend
        β”‚   └── impl/
        β”‚       β”œβ”€β”€ activation.py
        β”‚       β”œβ”€β”€ normalization.py
        β”‚       └── rotary.py
        └── ascend/          # Huawei Ascend NPU backend
            └── impl/
                β”œβ”€β”€ activation.py
                β”œβ”€β”€ normalization.py
                β”œβ”€β”€ rotary.py
                β”œβ”€β”€ attention.py       # AscendAttentionBackend
                └── attention_mask.py  # Attention mask utilities

Core concepts#

1. Backend implementation kind (BackendImplKind)#

types.py includes backend implementation kinds as follows:

  • DEFAULT: Default implementation (FlagGems), priority 150

  • VENDOR: Vendor-specific implementation, priority 100

  • REFERENCE: Reference implementation (PyTorch native), priority 50

2. Operator implementation (OpImpl)#

types.py includes operator implementation,each operator implementation contains:

  • op_name: Operator name (e.g., β€œsilu_and_mul”, β€œrms_norm”)

  • impl_id: Unique implementation identifier (e.g., β€œdefault.flagos”)

  • kind: Implementation type

  • fn: Actual implementation function

  • vendor: Vendor name (required for VENDOR type)

  • priority: Selection priority (higher value = preferred)

3. Selection policy#

policy.py includes selection policy.

Policy controls operator implementation selection:

  • prefer: Preferred implementation type

  • strict: Strict mode, whether to raise error when primary implementation fails

  • per_op_order: Custom selection order for each operator

  • deny_vendors: List of denied vendors

  • allow_vendors: Whitelist of allowed vendors

Architecture overview#

Dispatch flow diagram#

  1. Cache Check: Check if dispatch cache hits

  2. Get Implementations: Retrieve all registered implementations from registry

  3. Vendor Filtering: Filter by policy’s allow/deny lists

  4. Availability Check: Call is_available() to check if implementation is available

  5. Priority Sorting: Select best implementation based on per-op order or default order

  6. Cache Result: Cache selection result to speed up subsequent calls

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         User Code                                β”‚
β”‚                 call_op("rms_norm", x, ...)                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
                             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                       OpManager                                  β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚ 1. Check Cache                                            β”‚  β”‚
β”‚  β”‚ 2. Get Policy (from env or context)                      β”‚  β”‚
β”‚  β”‚ 3. Query Registry for all implementations                β”‚  β”‚
β”‚  β”‚ 4. Filter by vendor allow/deny list                      β”‚  β”‚
β”‚  β”‚ 5. Check availability (is_available())                   β”‚  β”‚
β”‚  β”‚ 6. Sort by priority & selection order                    β”‚  β”‚
β”‚  β”‚ 7. Cache & return selected implementation                β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
                             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        OpRegistry                                β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”‚
β”‚  β”‚   FlagGems   β”‚  β”‚    Vendor    β”‚  β”‚  Reference   β”‚         β”‚
β”‚  β”‚ Priority: 150β”‚  β”‚ Priority: 100β”‚  β”‚ Priority: 50 β”‚         β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Priority selection flow#

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     VLLM_FL_PREFER=flagos                       β”‚
β”‚                    (Default Behavior)                            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
                             β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚                                          β”‚
        β–Ό                                          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  Available?  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  Available?
β”‚   FlagGems   │─────No──────▢│    Vendor    │─────No──────▢
β”‚ Priority: 150β”‚              β”‚ Priority: 100β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                              β”‚
       Yes                            Yes
        β”‚                              β”‚
        β–Ό                              β–Ό
    βœ“ Selected                    βœ“ Selected

                                                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                                  β”‚  Reference   β”‚
                                                  β”‚ Priority: 50 β”‚
                                                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                         β”‚
                                                        Yes
                                                         β”‚
                                                         β–Ό
                                                    βœ“ Selected

Plugin integration points#

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Plugin Discovery                              β”‚
β”‚                                                                   β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚   Built-in     β”‚  β”‚  Entry Points  β”‚  β”‚  Environment   β”‚   β”‚
β”‚  β”‚   backends/    β”‚  β”‚  (setuptools)  β”‚  β”‚  PLUGIN_MODULESβ”‚   β”‚
β”‚  β”‚   vendor/      β”‚  β”‚                β”‚  β”‚                β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚           β”‚                   β”‚                    β”‚            β”‚
β”‚           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜            β”‚
β”‚                               β”‚                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                β”‚
                                β–Ό
                        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                        β”‚   Registry    β”‚
                        β”‚  register()   β”‚
                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜