Skip to content

Commit 98979f7

Browse files
committed
add docs
1 parent d51c3f6 commit 98979f7

File tree

3 files changed

+172
-1
lines changed

3 files changed

+172
-1
lines changed

docs/features/plugins.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.

docs/zh/features/plugins.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
```markdown
2+
# FastDeploy 插件机制说明文档
3+
4+
FastDeploy 支持插件机制,允许用户在不修改核心代码的前提下扩展功能。插件通过 Python 的 `entry_points` 机制实现自动发现与加载。
5+
6+
## 插件工作原理
7+
8+
插件本质上是在 FastDeploy 启动时被自动调用的注册函数。系统使用 `load_plugins_by_group` 函数确保所有进程(包括分布式训练场景下的子进程)在正式运行前都已加载所需的插件。
9+
10+
## 插件发现机制
11+
12+
FastDeploy 利用 Python 的 `entry_points` 机制来发现并加载插件。开发者需在自己的项目中将插件注册到指定的 entry point 组中。
13+
14+
### 示例:创建一个插件
15+
16+
#### 1. 编写插件逻辑
17+
18+
假设你有一个自定义模型类 `MyModelForCasualLM` 和预训练类 `MyPretrainedModel`,你可以编写如下注册函数:
19+
20+
```python
21+
# 文件:fd_add_dummy_model/__init__.py
22+
from fastdeploy.model_registry import ModelRegistry
23+
from my_custom_model import MyModelForCasualLM, MyPretrainedModel
24+
25+
def register():
26+
if "MyModelForCasualLM" not in ModelRegistry.get_supported_archs():
27+
ModelRegistry.register_model_class(MyModelForCasualLM)
28+
ModelRegistry.register_pretrained_model(MyPretrainedModel)
29+
```
30+
31+
#### 2. 注册插件到 `setup.py`
32+
33+
```python
34+
# setup.py
35+
from setuptools import setup
36+
37+
setup(
38+
name="fastdeploy-plugins",
39+
version="0.1",
40+
packages=["fd_add_dummy_model"],
41+
entry_points={
42+
"fastdeploy.model_register_plugins": [
43+
"fd_add_dummy_model = fd_add_dummy_model:register",
44+
],
45+
},
46+
)
47+
```
48+
49+
## 插件结构说明
50+
51+
插件由三部分组成:
52+
53+
| 组件 | 说明 |
54+
|------|------|
55+
| **插件组(Group)** | 插件所属的功能分组,例如:<br> - `fastdeploy.model_register_plugins`: 用于注册模型<br> - `fastdeploy.model_runner_plugins`: 用于注册模型运行器<br> 用户可根据需要自定义分组。 |
56+
| **插件名(Name)** | 每个插件的唯一标识名(如 `fd_add_dummy_model`),可通过环境变量 `FD_PLUGINS` 控制是否加载该插件。 |
57+
| **插件值(Value)** | 格式为 `模块名:函数名`,指向实际执行注册逻辑的入口函数。 |
58+
59+
## 控制插件加载行为
60+
61+
默认情况下,FastDeploy 会加载所有已注册的插件。若只想加载特定插件,可以设置环境变量:
62+
63+
```bash
64+
export FD_PLUGINS=fastdeploy-plugins
65+
```
66+
67+
多个插件名之间可以用逗号分隔:
68+
69+
```bash
70+
export FD_PLUGINS=plugin_a,plugin_b
71+
```
72+
73+
## 参考示例
74+
75+
请参见项目目录下的示例插件实现:
76+
```
77+
./test/plugins/
78+
```
79+
80+
其中包含完整的插件结构和 `setup.py` 配置示例。
81+
82+
## 总结
83+
84+
通过插件机制,用户可以轻松地为 FastDeploy 添加自定义模型或功能模块,而无需修改核心源码。这不仅提升了系统的可扩展性,也方便了第三方开发者进行功能拓展。
85+
86+
如需进一步开发插件,请参考 FastDeploy 源码中的 `model_registry``plugin_loader` 模块。
87+
```

fastdeploy/model_executor/models/model_base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class ModelRegistry:
3535
def register_model_class(cls, model_class):
3636
"""register model class"""
3737
if issubclass(model_class, ModelForCasualLM) and model_class is not ModelForCasualLM:
38-
# print(model_class.name())
3938
cls._arch_to_model_cls[model_class.name()] = model_class
4039
return model_class
4140

0 commit comments

Comments
 (0)