Skip to content

完整层级与接口总树

Abstract

这篇只做一件事:

把当前最小例子 ETTh1.csv + DLinear + rolling_forecast 的完整层级、接口和父子关系一次性串起来。

这篇不重点解释数学细节,只关注:

  • 层级
  • 接口
  • 顺序
  • 父子关系

1. 当前最小例子

当前主线固定为:

  • 数据:ETTh1.csv
  • 模型:time_series_library.DLinear
  • adapter:transformer_adapter
  • strategy:rolling_forecast

关键参数:

  • seq_len = 96
  • label_len = 48
  • horizon = 24
  • batch_size = 4
  • tv_ratio = 0.8
  • train_ratio_in_tv = 0.75
  • stride = 24
  • num_rollings = 48

2. 总顺序图

3. 总抽象树

4. Level 1 到 Level 8 的接口表

4.1 Level 1 run_benchmark.py

入口接口:

python
if __name__ == "__main__":

核心出口接口:

python
log_filenames = pipeline(data_config, model_config, evaluation_config)

下层对应:

4.2 Level 2 pipeline(...)

入口接口:

python
pipeline(data_config, model_config, evaluation_config)

核心四块:

  • 2A 数据侧
  • 2B 模型侧
  • 2C 执行侧
  • 2D 收尾侧

当前主线真正继续下钻的是:

python
eval_model(model_factory, data_name_list, evaluation_config)

下层对应:

4.3 Level 3 eval_model(...)

入口接口:

python
eval_model(model_factory, series_list, evaluation_config)

最关键的任务提交接口:

python
eval_backend.schedule(strategy.execute, (series_name, model_factory))

这一层的输出接口:

python
EvalResult(strategy, result_list, model_factory, series_list)

下层真正继续展开的是:

python
strategy.execute(series_name, model_factory)

下层对应:

4.4 Level 4 单任务桥接层

总入口接口:

python
strategy.execute(series_name, model_factory)

桥接链:

python
ForecastingStrategy.execute(...)
-> self._execute(...)
-> RollingForecast._execute(...)

这一层真正进入业务主体的出口接口:

python
self._eval_batch(series, meta_info, model, series_name)

下层对应:

4.5 Level 5 _eval_batch(...)

入口接口:

python
_eval_batch(series, meta_info, model, series_name)

四个子块:

  • 5A 配置与切数
  • 5B 训练子块
  • 5C 预测子块
  • 5D 打分与收尾子块

这三个子块已经有下层文档:

4.6 Level 6 5B 训练子块

入口接口:

python
fit_method(
    target_train_valid_data,
    covariates=covariates_train,
    train_ratio_in_tv=train_ratio_in_tv,
)

当前例子里实际等价于:

python
model.forecast_fit(...)

再展开一层等价于:

python
DeepForecastingModelBase.forecast_fit(model, ...)

下层对应:

4.7 Level 6 5C 预测子块

入口接口:

python
index_list = self._get_index(...)
predicts = model.batch_forecast(horizon, predict_batch_maker)

这一层真正进入模型侧的接口是:

python
model.batch_forecast(horizon, predict_batch_maker)

下层对应:

4.8 Level 6 5D 打分与收尾

入口接口:

python
targets = batch_maker.make_batch_eval(horizon)["target"]
self.evaluator.evaluate(target, predicts, eval_scaler, hist_data)

当前主线在这里先收住,没有继续单独下钻 Evaluator 和各个 metric 函数。

对应文档:

4.9 Level 7 训练循环

入口接口:

python
for i, (input, target, input_mark, target_mark) in enumerate(self.train_data_loader):

当前例子里的典型 batch:

  • input.shape = (4, 96, 7)
  • target.shape = (4, 72, 7)
  • input_mark.shape = (4, 96, 4)
  • target_mark.shape = (4, 72, 4)

这一层真正进入模型前向的接口是:

python
self._process(input, target, input_mark, target_mark)

下层对应:

4.10 Level 7 batch_forecast(...)

入口接口:

python
batch_forecast(horizon, batch_maker)

这一层真正进入内部 rolling 前向循环的接口是:

python
_perform_rolling_predictions(horizon, input_np, all_mark, device)

下层对应:

4.11 Level 8 训练前向路径

入口接口:

python
self._process(input, target, input_mark, target_mark)

当前例子里的真实桥接链:

python
TransformerAdapter._process(...)
-> self.model(input, input_mark, dec_input, target_mark)
-> DLinear.forward(...)

这里的核心接口翻译是:

  • input -> x_enc
  • input_mark -> x_mark_enc
  • dec_input -> x_dec
  • target_mark -> x_mark_dec

4.12 Level 8 预测内循环

入口接口:

python
_perform_rolling_predictions(horizon, input_np, all_mark, device)

这一层内部会不断重复:

python
self._process(input, dec_input, input_mark, target_mark)

所以它最终还是会再次汇入:

  • TransformerAdapter._process(...)
  • DLinear.forward(...)

5. 当前例子的父子映射表

text
21 -> 22
22(2C) -> 23
23(3B / 3C) -> 24
24(4B-3) -> 25
25(5B) -> 26
25(5C) -> 27
25(5D) -> 28
26(6E) -> 29
29(7B) -> 30
27(6C-3) -> 31
31(7P-4) -> 32

6. 当前主线里没有继续下钻的节点

当前还没有继续单独展开的主要部分:

  • 2A 数据侧
  • 2B 模型侧
  • 2D 收尾侧
  • 5A 配置与切数
  • Level 6 5D 里面的 Evaluator / metric_funcs / metric

7. 最后压成一句

当前最小例子已经把 run_benchmark -> pipeline -> eval_model -> strategy.execute -> _eval_batch -> forecast_fit / batch_forecast -> _process -> DLinear.forward 这条主链完整串通;后续扩展应该优先在这棵树上补新的兄弟分支,而不是继续在局部做无边界 DFS。

*记录并在线阅读我的笔记*