|
| 1 | +# FastDeploy Plugin Mechanism Documentation |
| 2 | + |
| 3 | +FastDeploy supports a plugin mechanism that allows users to extend functionality without modifying the core code. Plugins are automatically discovered and loaded through Python's `entry_points` mechanism. |
| 4 | + |
| 5 | +## How Plugins Work |
| 6 | + |
| 7 | +Plugins are essentially registration functions that are automatically called when FastDeploy starts. The system uses the `load_plugins_by_group` function to ensure that all processes (including child processes in distributed training scenarios) have loaded the required plugins before official operations begin. |
| 8 | + |
| 9 | +## Plugin Discovery Mechanism |
| 10 | + |
| 11 | +FastDeploy uses Python's `entry_points` mechanism to discover and load plugins. Developers need to register their plugins in the specified entry point group in their project. |
| 12 | + |
| 13 | +### Example: Creating a Plugin |
| 14 | + |
| 15 | +#### 1. Write Plugin Logic |
| 16 | + |
| 17 | +Assuming you have a custom model class `MyModelForCasualLM` and a pretrained class `MyPretrainedModel`, you can write the following registration function: |
| 18 | + |
| 19 | +```python |
| 20 | +# File: fd_add_dummy_model/__init__.py or fd_add_dummy_model/register.py |
| 21 | +from fastdeploy.model_registry import ModelRegistry |
| 22 | +from my_custom_model import MyModelForCasualLM, MyPretrainedModel |
| 23 | + |
| 24 | +def register(): |
| 25 | + if "MyModelForCasualLM" not in ModelRegistry.get_supported_archs(): |
| 26 | + ModelRegistry.register_model_class(MyModelForCasualLM) |
| 27 | + ModelRegistry.register_pretrained_model(MyPretrainedModel) |
| 28 | +``` |
| 29 | + |
| 30 | +#### 2. Register Plugin in `setup.py` |
| 31 | + |
| 32 | +```python |
| 33 | +# setup.py |
| 34 | +from setuptools import setup |
| 35 | + |
| 36 | +setup( |
| 37 | + name="fastdeploy-plugins", |
| 38 | + version="0.1", |
| 39 | + packages=["fd_add_dummy_model"], |
| 40 | + entry_points={ |
| 41 | + "fastdeploy.model_register_plugins": [ |
| 42 | + "fd_add_dummy_model = fd_add_dummy_model:register", |
| 43 | + ], |
| 44 | + }, |
| 45 | +) |
| 46 | +``` |
| 47 | + |
| 48 | +## Plugin Structure |
| 49 | + |
| 50 | +Plugins consist of three components: |
| 51 | + |
| 52 | +| Component | Description | |
| 53 | +|-----------|-------------| |
| 54 | +| **Plugin Group** | The functional group to which the plugin belongs, for example:<br> - `fastdeploy.model_register_plugins`: for model registration<br> - `fastdeploy.model_runner_plugins`: for model runner registration<br> Users can customize groups as needed. | |
| 55 | +| **Plugin Name** | The unique identifier for each plugin (e.g., `fd_add_dummy_model`), which can be controlled via the `FD_PLUGINS` environment variable to determine whether to load the plugin. | |
| 56 | +| **Plugin Value** | Format is `module_name:function_name`, pointing to the entry function that executes the registration logic. | |
| 57 | + |
| 58 | +## Controlling Plugin Loading Behavior |
| 59 | + |
| 60 | +By default, FastDeploy loads all registered plugins. To load only specific plugins, you can set the environment variable: |
| 61 | + |
| 62 | +```bash |
| 63 | +export FD_PLUGINS=fastdeploy-plugins |
| 64 | +``` |
| 65 | + |
| 66 | +Multiple plugin names can be separated by commas: |
| 67 | + |
| 68 | +```bash |
| 69 | +export FD_PLUGINS=plugin_a,plugin_b |
| 70 | +``` |
| 71 | + |
| 72 | +## Reference Example |
| 73 | + |
| 74 | +Please refer to the example plugin implementation in the project directory: |
| 75 | +``` |
| 76 | +./test/plugins/ |
| 77 | +``` |
| 78 | + |
| 79 | +It contains a complete plugin structure and `setup.py` configuration example. |
| 80 | + |
| 81 | +## Summary |
| 82 | + |
| 83 | +Through the plugin mechanism, users can easily add custom models or functional modules to FastDeploy without modifying the core source code. This not only enhances system extensibility but also facilitates third-party developers in extending functionality. |
| 84 | + |
| 85 | +For further plugin development, please refer to the `model_registry` and `plugin_loader` modules in the FastDeploy source code. |
0 commit comments