doc: update doc. (#30475)

This commit is contained in:
Haojun Liao 2025-03-26 04:02:32 +08:00 committed by GitHub
parent 306dbbb939
commit dea62af077
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 281 additions and 222 deletions

View File

@ -14,6 +14,24 @@ systemctl stop taosanoded
systemctl status taosanoded
```
### 启停时间序列基础模型服务
考虑到时序基础模型服务,需要较大的资源。避免启动过程中资源不足导致的启动失败,暂不提供自动启动方式。如果您希望体验时序基础模型服务,需要手动执行如下命令
```bash
# 启动涛思时序数据基础模型
start-tdtsfm
# 启动 Time-MoE 基础模型
start-timer-moe
```
```bash
# 停止涛思时序数据基础模型
stop-tdtsfm
# 停止 Time-MoE 基础模型
stop-timer-moe
```
### 目录及配置说明
安装完成后Anode 主体目录结构如下:
@ -108,21 +126,40 @@ Query OK, 1 row(s) in set (0.037205s)
```SQL
SHOW ANODES FULL;
taos> show anodes full;
id | type | algo |
============================================================================
1 | anomaly-detection | shesd |
1 | anomaly-detection | iqr |
1 | anomaly-detection | ksigma |
1 | anomaly-detection | lof |
1 | anomaly-detection | grubbs |
1 | anomaly-detection | ad_encoder |
1 | forecast | holtwinters |
1 | forecast | arima |
Query OK, 8 row(s) in set (0.008796s)
taos> show anodes full;
id | type | algo |
============================================================================
1 | anomaly-detection | grubbs |
1 | anomaly-detection | lof |
1 | anomaly-detection | shesd |
1 | anomaly-detection | ksigma |
1 | anomaly-detection | iqr |
1 | anomaly-detection | sample_ad_model |
1 | forecast | arima |
1 | forecast | holtwinters |
1 | forecast | tdtsfm_1 |
1 | forecast | timemoe-fc |
Query OK, 10 row(s) in set (0.028750s)
```
列表中的算法分为两个部分,分别是异常检测算法集合,包含六个算法模型,四个预测算法集。算法模型如下:
|类型 |模型名称|说明 |
|--------|--------|--------------------|
|异常检测 |grubbs |基于数学统计学检测模型|
|异常检测 |lof |基于密度的检测模型 |
|异常检测 |shesd |季节性ESD算法模型 |
|异常检测 |ksigma |数学统计学检测模型 |
|异常检测 |iqr |数学统计学检测模型 |
|预测分析 |sample_ad_model |基于自编码器的异常检测示例模型|
|预测分析 |arima |移动平均自回归预测算法|
|预测分析 |holtwinters |多次指数平滑预测算法|
|预测分析 |tdtsfm_1 |涛思时序数据基础模型 v1.0 版本|
|预测分析 |timemoe-fc |Time-MoE时序基础模型的预测能力|
相关算法的具体介绍和使用说明见后续章节。
#### 刷新集群中的分析算法缓存
```SQL
UPDATE ANODE {anode_id}

View File

@ -1,29 +0,0 @@
---
title: "时序基础模型"
sidebar_label: "时序基础模型"
---
TDgpt 内置了涛思数据时序基础模型和 Time-MoE 两个时序基础模型。
## 功能概述
时序数据基础模型是专门训练,用以处理时间序列数据预测和异常检测、数据补齐等功能的基础模型,时序基础模型继承了大模型的优良泛化能力,无需要设置复杂的输入参数,即可根据输入数据进行预测分析。
|序号|参数|说明
|---|---|---|
|1| tdtsfm_1 | 涛思时序数据基础模型 v1.0|
|2| time-moe | 亿级别参数 MoE时序基础模型|
TDgpt 集成了时序基础模型的预测能力,无需设置模型相关参数,使用 SQL 语句即可轻松调用时序基础模型的进行预测。
- 通过以下 SQL 语句调用涛思时序基础模型tdtfsm预测数据。
```SQL
SELECT _frowts, FORECAST(i32, "algo=tdtsfm_1,rows=10") from foo
```
- 通过以下 SQL 语句,调用 TimeMoE 时序基础模型预测数据
```SQL
SELECT _frowts, FORECAST(i32, "algo=algo=timemoe-fc,rows=10") from foo
```

View File

@ -1,35 +0,0 @@
---
title: "MLP"
sidebar_label: "MLP"
---
本节说明 MLP 模型的使用方法。
## 功能概述
MLPMutiLayers Perceptron多层感知机是一种典的神经网络模型能够通过学习历史数据的非线性关系
捕捉时间序列中的模式并进行未来值预测。它通过多层全连接网络进行特征提取和映射,
对输入的历史数据生成预测结果。由于不直接考虑趋势或季节性变化,通常需要结合数据预处理来提升效果,
适合解决非线性和复杂的时间序列问题。
完整的调用SQL语句如下
```SQL
SELECT _frowts, FORECAST(i32, "algo=mlp") from foo
```
```json5
{
"rows": fc_rows, // 返回结果的行数
"period": period, // 返回结果的周期性,同输入
"alpha": alpha, // 返回结果的置信区间,同输入
"algo": "mlp", // 返回结果使用的算法
"mse": mse, // 拟合输入时间序列时候生成模型的最小均方误差(MSE)
"res": res // 列模式的结果
}
```
### 参考文献
- [1]Rumelhart D E, Hinton G E, Williams R J. Learning representations by back-propagating errors[J]. nature, 1986, 323(6088): 533-536.
- [2]Rosenblatt F. The perceptron: a probabilistic model for information storage and organization in the brain[J]. Psychological review, 1958, 65(6): 386.
- [3]LeCun Y, Bottou L, Bengio Y, et al. Gradient-based learning applied to document recognition[J]. Proceedings of the IEEE, 1998, 86(11): 2278-2324.

View File

@ -0,0 +1,29 @@
---
title: "时序基础模型"
sidebar_label: "时序基础模型"
---
TDgpt 内置了涛思数据时序基础模型和 Time-MoE 两个时序基础模型,当前 TDgpt 只整合了时序基础模型的预测能力,后续还将继续整合异常检测、数据补齐等能力。
## 功能概述
时序数据基础模型是专门训练用以处理时间序列数据预测和异常检测、数据补齐等高级时序数据分析功能的基础模型,时序基础模型继承了大模型的优良泛化能力,无需要设置复杂的输入参数,即可根据输入数据进行预测分析。
|序号|参数 | 说明 |
|---|----------|------------------------|
|1 | tdtsfm_1 | 涛思时序数据基础模型 v1.0|
|2 | time-moe | 亿级别参数 MoE时序基础模型|
TDgpt 集成了时序基础模型的预测能力,无需设置模型相关参数,使用 SQL 语句即可轻松调用时序基础模型的进行预测。
- 通过以下 SQL 语句调用涛思时序基础模型tdtfsm预测数据并返回 10 条预测记录。
```SQL
SELECT _frowts, FORECAST(i32, "algo=tdtsfm_1,rows=10") from foo
```
- 通过以下 SQL 语句,调用 TimeMoE 时序基础模型预测数据,并返回 10 条预测记录。
```SQL
SELECT _frowts, FORECAST(i32, "algo=timemoe-fc,rows=10") from foo
```

View File

@ -3,18 +3,32 @@ title: "机器学习算法"
sidebar_label: "机器学习算法"
---
Autoencoder<sup>[1]</sup>: TDgpt 内置使用自编码器Autoencoder的异常检测算法
对周期性的时间序列数据具有较好的检测结果。使用该模型需要针对输入时序数据进行预训练,
同时将训练完成的模型保存在到服务目录 `ad_autoencoder` 中,然后在 SQL 语句中指定调用该算法模型即可使用。
Autoencoder<sup>[1]</sup>: TDgpt 内置了一个使用自编码器Autoencoder构建的深度学习网络的异常检测模型。该异常检测模型基于 NAB 的 [art_daily_small_noise 数据集](https://raw.githubusercontent.com/numenta/NAB/master/data/artificialNoAnomaly/art_daily_small_noise.csv) 进行训练,该模型的详细信息请参见 添加机器学习模型/[准备模型](../dev/ml/index) 部分。
```SQL
--- 在 options 中增加 model 的名称ad_autoencoder_foo 针对 foo 数据集(表)训练的采用自编码器的异常检测模型进行异常检测
SELECT COUNT(*), _WSTART
FROM foo
ANOMALY_WINDOW(col1, 'algo=encoder, model=ad_autoencoder_foo');
我们并没有将该模型预置与 `model` 目录中。需要该模型正常运行需要下载模型文件,请点击此处[下载](https://github.com/taosdata/TDengine/blob/main/tools/tdgpt/model/sample-ad-autoencoder/), 并在 `/var/lib/taos/taosanode/model/` 目录中创建子目录 `sample-ad-autoencoder`,保存下载两个模型文件,然后需要重启 taosanode 服务。相关操作原理及方式请参考[添加机器学习模型](../dev/ml) 的介绍。
此时 `model` 文件夹结构如下:
```bash
.
└── model
└── sample-ad-autoencoder
├── sample-ad-autoencoder.keras
└── sample-ad-autoencoder.info
```
```SQL
--- 在 options 中增加 model 参数 sample-ad-autoencoder 采用自编码器的异常检测模型进行异常检测
SELECT _wstart, count(*)
FROM foo anomaly_window(val, 'algo=sample_ad_model,model=sample-ad-autoencoder');
```
其中的 `algo` 设置为 `sample_ad_model` 为示例异常检测模型,`model` 指定加载模型文件的信息。需要注意的是,该模型只针对训练的数据集具有较好的检测效果,针对非训练相关数据集,可能无法
得出合适的结果。
后续添加机器(深度)学习异常检测算法
- Isolation Forest
- One-Class Support Vector Machines (SVM)
- Prophet

View File

@ -154,10 +154,10 @@ class _AutoEncoderDetectionService(AbstractAnomalyDetectionService):
该模型已经预置在系统中,您可通过 `show anodes full` 直接查看。一个新的算法适配完成以后,需要重新启动 taosanode并执行命令 `update all anodes` 更新 mnode 的算法列表。
- 通过设置参数 `algo=sample_ad_model`,告诉 TDgpt 调用自编码器算法训练的模型(该算法模型在可用算法列表中)。
- 通过设置参数 `model=sample-ad-autoencoder`指定自编码器针对某数据集训练的模型。
- 通过设置参数 `model=sample-ad-autoencoder`告诉自编码器加载特定的模型。
```SQL
--- 在 options 中增加 model 的名称ad_autoencoder_foo 针对 foo 数据集(表)训练的采用自编码器的异常检测模型进行异常检测
--- 在 options 中增加 model 参数 sample-ad-autoencoder 针对 foo 数据集(表)训练的采用自编码器的异常检测模型进行异常检测
SELECT _wstart, count(*)
FROM ad_sample anomaly_window(val, 'algo=sample_ad_model,model=sample-ad-autoencoder');
FROM foo anomaly_window(val, 'algo=sample_ad_model,model=sample-ad-autoencoder');
```

View File

@ -1,86 +0,0 @@
---
title: "部署 Time-MoE 模型"
sidebar_label: "部署 Time-MoE 模型"
---
本章介绍如何本地部署 [Time-MoE] (https://github.com/Time-MoE/Time-MoE) 时序基础模型并与 TDgpt 适配完成后,提供时序数据预测服务。
# 准备环境
为了使用时间序列基础模型,需要在本地部署环境支持其运行。首先需要准备 Python 环境。使用 PiPy 安装必要的依赖包:
```shell
pip install torch==2.4.1+cpu -f https://download.pytorch.org/whl/torch_stable.html
pip install flask==3.0.3
pip install transformers==4.40.0
pip install accelerate
```
您可以使用安装 TDgpt 过程中自动创建的虚拟环境,也可以创建一个独立虚拟环境,使用该虚拟环境之前,确保安装了上述的依赖包。
# 设置服务端口和地址
TDgpt 安装根目录下的 `./lib/taosanalytics/time-moe.py` 文件负责 Time-MoE 模型的部署和服务,修改文件设置合适的服务 URL。
```Python
@app.route('/ds_predict', methods=['POST'])
def time_moe():
...
```
修改 `ds_predict` 为需要开启的 URL 服务地址,或者使用默认值即可。
```Python
app.run(
host='0.0.0.0',
port=5001,
threaded=True,
debug=False
)
```
其中的 port 修改为希望开启的端口,重启脚本即可。
# 启动部署 Python 脚本
```shell
nohup python time-moe.py > service_output.out 2>&1 &
```
第一次启动脚本会从 huggingface 自动加载 [2 亿参数模型](https://huggingface.co/Maple728/TimeMoE-200M)。该模型是 Time-MoE 200M参数版本如果您需要部署参数规模更小的版本请将 `time-moe.py` 文件中 `'Maple728/TimeMoE-200M'` 修改为 `Maple728/TimeMoE-50M`,此时将加载 [0.5 亿参数模型](https://huggingface.co/Maple728/TimeMoE-50M)。
如果加载失败,请尝试执行如下命令切换为国内镜像下载模型。
```shell
export HF_ENDPOINT=https://hf-mirror.com
```
再执行脚本:
```shell
nohup python time-moe.py > service_output.out 2>&1 &
```
检查 `service_output.out` 文件,有如下输出,则说明加载成功
```shell
Running on all addresses (0.0.0.0)
Running on http://127.0.0.1:5001
```
# 验证服务是否正常
使用 Shell 命令可以验证服务是否正常
```shell
curl 127.0.0.1:5001/ds_predict
```
如果看到如下返回信息表明服务正常,自此部署 Time-MoE 完成。
```html
<!doctype html>
<html lang=en>
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>
```
# 参考文献
- Time-MoE: Billion-Scale Time Series Foundation Models with Mixture of Experts. [[paper](https://arxiv.org/abs/2409.16040)] [[GitHub Repo](https://github.com/Time-MoE/Time-MoE)]

View File

@ -1,40 +0,0 @@
---
title: "添加模型服务"
sidebar_label: "添加模型服务"
---
TDgpt 默认已经内置了 Time-MoE 模型的支持功能, 执行 `show anodes full`,可以看到 Time-MoE 的预测服务 `timemoe-fc`,现在只适配了预测服务,所以其后增加了后缀名 fc。
```shell
taos> show anodes full;
id | type | algo |
============================================================================
1 | anomaly-detection | grubbs |
1 | anomaly-detection | lof |
1 | anomaly-detection | shesd |
1 | anomaly-detection | ksigma |
1 | anomaly-detection | iqr |
1 | anomaly-detection | sample_ad_model |
1 | forecast | arima |
1 | forecast | holtwinters |
1 | forecast | tdtsfm_1 |
1 | forecast | timemoe-fc |
```
正确调用 Time-MoE 模型的时间序列数据预测能力,需要您在本地或云端已经部署完成 Time-MoE 服务(需要执行 `./taosanode/lib/taosanalytics/time-moe.py` 的脚本部署 Time-MoE
服务。具体过程请参见 [部署 Time-MoE 服务](./02-deploy-timemoe))。
修改 `/etc/taos/taosanode.ini` 配置文件中如下部分:
```ini
[tsfm-service]
timemoe-fc = http://192.168.2.90:5001/ds_predict
```
设置正确的 IP 和端口,以及服务地址。
然后重启 taosnode 服务,并更新服务端算法缓存列表 `update all anodes`,之后即可通过 SQL 语句调用 Time-MoE 的时间序列数据预测服务。
```sql
SELECT FORECAST(i32, 'algo=timemoe-fc')
FROM foo;
```

View File

@ -1,15 +1,184 @@
---
sidebar_label: 添加时序基础模型
title: 添加时序基础模型
title: "部署 Time-MoE 模型"
sidebar_label: "部署 Time-MoE 模型"
---
本章主要介绍部署并使用开源时序基础模型Time Series Fundation Model, TSFM由众多研究机构及企业开源时序基础模型极大地简化了时序数据分析的复杂程度在数据分析算法、机器学习和传统深度学习模型之外提供了一个时间序列数据高级分析的新选择。
本章主要介绍部署并使用开源时序基础模型Time Series Foundation Model, TSFM由众多研究机构及企业开源时序基础模型极大地简化
了时序数据分析的复杂程度,在数据分析算法、机器学习和传统深度学习模型之外,提供了一个时间序列数据高级分析的新选择。
本章节将详细介绍如何部署使用 Time-MoE 等的时序基础模型,并通过 SQL 语句直接调用其时序数据分析能力。
TDgpt 已经内置原生支持了两个时序基础模型涛思时序基础模型 (TDtsfm v1.0) 和 Time-MoE。但是越来越多的开源或商业时序模型需要
用户按需将其整合到 TDgpt 中,本章将以支持 Time-MoE 模型为例,说明如何将一个独立部署的 MaaS 服务整合到 TDgpt 中,
并通过 SQL 语句调用其时序数据分析能力。
```mdx-code-block
import DocCardList from '@theme/DocCardList';
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
本章介绍如何本地部署 [Time-MoE](https://github.com/Time-MoE/Time-MoE) 时序基础模型并与 TDgpt 适配后,提供时序数据预测服务。
<DocCardList items={useCurrentSidebarCategory().items}/>
# 准备环境
为了使用时间序列基础模型,需要在本地部署环境支持其运行。首先需要准备 Python 环境,使用 PiPy 安装必要的依赖包:
```shell
pip install torch==2.4.1+cpu -f https://download.pytorch.org/whl/torch_stable.html
pip install flask==3.0.3
pip install transformers==4.40.0
pip install accelerate
```
您可以使用 TDgpt 的虚拟环境,也可以新创建一个虚拟环境,使用该虚拟环境之前,确保正确安装了上述依赖包。
# 设置本地时序基础模型服务地址
TDgpt 安装根目录下的 `./lib/taosanalytics/time-moe.py` 文件负责 Time-MoE 模型的部署和服务,修改文件设置合适的服务 URL。
```python
@app.route('/ds_predict', methods=['POST'])
def time_moe():
...
```
修改 `ds_predict` 为需要开启的 URL 服务地址,或者使用默认值亦可。
```Python
app.run(
host='0.0.0.0',
port=5001,
threaded=True,
debug=False
)
```
其中的 port 修改为希望开启的端口,包括使用默认值亦可。完成之后重启服务。
# 启动部署 Python 脚本
```shell
nohup python time-moe.py > service_output.out 2>&1 &
```
第一次启动脚本会从 huggingface 自动加载 [2 亿参数模型](https://huggingface.co/Maple728/TimeMoE-200M)。该模型是 Time-MoE 200M参数版本如果您需要部署参数规模更小的版本请将 `time-moe.py` 文件中 `'Maple728/TimeMoE-200M'` 修改为 `Maple728/TimeMoE-50M`,此时将加载 [0.5 亿参数模型](https://huggingface.co/Maple728/TimeMoE-50M)。
如果加载失败,请尝试执行如下命令切换为国内镜像下载模型。
```shell
export HF_ENDPOINT=https://hf-mirror.com
```
再执行脚本:
```shell
nohup python time-moe.py > service_output.out 2>&1 &
```
检查 `service_output.out` 文件,有如下输出,则说明加载成功
```shell
Running on all addresses (0.0.0.0)
Running on http://127.0.0.1:5001
```
# 验证服务是否正常
使用 Shell 命令可以验证服务是否正常
```shell
curl 127.0.0.1:5001/ds_predict
```
如果看到如下返回信息表明服务正常,自此部署 Time-MoE 完成。
```html
<!doctype html>
<html lang=en>
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>
```
# 添加模型预测分析适配代码
您可用参考 https://github.com/taosdata/TDengine/blob/main/tools/tdgpt/taosanalytics/algo/fc/timemoe.py 文件进行 MaaS 服务的适配。
```python
class _TimeMOEService(AbstractForecastService):
# 模型名称,用户可根据需求定义,该名称也是后续调用该模型的 key
name = 'timemoe-fc'
# 说明信息
desc = ("Time-MoE: Billion-Scale Time Series Foundation Models with Mixture of Experts; "
"Ref to https://github.com/Time-MoE/Time-MoE")
def __init__(self):
super().__init__()
self.table_name = None
# 读取 taosanode.ini 配置文件中的该模型对应的服务的地址信息,如果未读到,使用默认地址,用户可根据需求确定
service_host = conf.get_tsfm_service("timemoe-fc")
if service_host is not None:
self.service_host = service_host
else:
self.service_host = 'http://127.0.0.1:5001/timemoe'
self.headers = {'Content-Type': 'application/json'}
def execute(self):
"""分析主程序方法"""
if self.list is None or len(self.list) < self.period:
raise ValueError("number of input data is less than the periods")
if self.rows <= 0:
raise ValueError("fc rows is not specified yet")
# let's request the gpt service
data = {"input": self.list, 'next_len': self.rows}
try:
# 请求时序基础模型服务
response = requests.post(self.service_host, data=json.dumps(data), headers=self.headers)
except Exception as e:
app_logger.log_inst.error(f"failed to connect the service: {self.service_host} ", str(e))
raise e
# 检查返回值
if response.status_code == 404:
app_logger.log_inst.error(f"failed to connect the service: {self.service_host} ")
raise ValueError("invalid host url")
elif response.status_code != 200:
app_logger.log_inst.error(f"failed to request the service: {self.service_host}, reason: {response.text}")
raise ValueError(f"failed to request the service, {response.text}")
pred_y = response.json()['output']
res = {
"res": [pred_y]
}
# 按照约定要求,组装预测分析结果
insert_ts_list(res["res"], self.start_ts, self.time_step, self.rows)
return res
```
将代码添加到 `/usr/local/taos/taosanode/lib/taosanalytics/algo/fc` 路径下。您可以在该路径下找到 `timemoe.py` 的文件,该文件即为系统内置的支持 `Time-MoE` 的适配文件。
TDgpt 默认已经内置了 Time-MoE 模型的支持,能够使用 Time-MoE 的能力进行时序数据预测分析, 执行 `show anodes full`,可以看到 Time-MoE 的预测服务 `timemoe-fc`
# 设置模型服务地址
修改 `/etc/taos/taosanode.ini` 配置文件中[tsfm-service]部分:
```ini
[tsfm-service]
timemoe-fc = http://127.0.0.1:5001/ds_predict
```
添加服务的地址。此时的 `key` 是模型的名称,此时即为 `timemoe-fc``value` 是 Time-MoE本地服务的地址http://127.0.0.1:5001/ds_predict。
然后重启 taosnode 服务,并更新服务端算法缓存列表 `update all anodes`,之后即可通过 SQL 语句调用 Time-MoE 的时间序列数据预测服务。
# SQL 调用基础模型预测能力
```sql
SELECT FORECAST(i32, 'algo=timemoe-fc')
FROM foo;
```
# 添加其他开源时序基础模型
模型在本地部署服务以后在TDgpt 中注册的逻辑相似。只需要修改类名称和模型服务名称(Key)、设置正确的服务地址即可。
# 参考文献
- Time-MoE: Billion-Scale Time Series Foundation Models with Mixture of Experts. [[paper](https://arxiv.org/abs/2409.16040)] [[GitHub Repo](https://github.com/Time-MoE/Time-MoE)]