[Discussion] Three potential design issues in paddle.Model
[Discussion] Three potential design issues in paddle.Model: redundant static adapters, incomplete multi-loss logging, and simplistic loss aggregation
[讨论] paddle.Model 的三个设计问题:静态适配器冗余、多 Loss 日志不完整、Loss 聚合方式过于简单
Hi PaddlePaddle Team,
飞桨团队好,
I have been reading the implementation of paddle.Model in python/paddle/hapi/model.py and identified three design choices that might be worth discussing. I’d appreciate your insights to understand whether these are historical artifacts or have fundamental technical reasons.
我最近在阅读 python/paddle/hapi/model.py 中 paddle.Model 的实现时,发现了三个值得探讨的设计点。希望能听听你们的看法,了解这些是历史遗留问题,还是有其根本的技术原因。
1. Redundant Adapters for Static Graph Support 静态图支持的适配器冗余
The Model class currently uses three different adapters based on the execution mode:
DynamicGraphAdapter(dynamic mode)StaticGraphAdapter(static mode)StaticPIRGraphAdapter(PIR static mode)
目前 Model 类根据执行模式使用了三个不同的适配器(Adapter):
DynamicGraphAdapter(动态图模式)StaticGraphAdapter(静态图模式)StaticPIRGraphAdapter(PIR 静态图模式)
From what I understand, both StaticGraphAdapter and StaticPIRGraphAdapter seem to replicate the core functionality of paddle.jit.to_static—they convert a dynamically defined model into a static graph representation for execution. Could this architecture be simplified to DynamicGraphAdapter + paddle.jit.to_static, thereby reducing the codebase from ~2000+ lines to a much more maintainable version?
据我理解,StaticGraphAdapter 和 StaticPIRGraphAdapter 的核心功能似乎与 paddle.jit.to_static 高度重合——它们都将动态定义的模型转换为静态图表示以执行。这个架构能否简化为 DynamicGraphAdapter + paddle.jit.to_static 的组合,从而将代码量从 2000+ 行大幅精简,提高可维护性?
2. Incomplete Logging for Multiple Losses 多 Loss 场景下的日志记录不完整
In the training loop (_run_one_epoch method, around line 1850), there is the following logic:
在训练循环的核心方法 _run_one_epoch 中(约 1850 行),有以下代码:
if self._metrics and self._loss:
metrics = [[float(l) for l in outs[0]]] # Only the first loss is takenWhen a model returns multiple losses (e.g., in multi-task learning), only the first loss is logged to the console and visualized in tools like VisualDL. The remaining losses are discarded, making it difficult to monitor the convergence of each task and debug effectively.
当模型返回多个 Loss(例如多任务学习)时,只有第一个 Loss 被记录到控制台和 VisualDL 等可视化工具中,其余 Loss 被丢弃。这使得用户难以有效监控各个任务的收敛情况,也给调试带来了困难。
3. Oversimplified Loss Aggregation Loss 聚合方式过于简单且不透明
Before backpropagation, the code aggregates all losses using paddle.add_n(losses). This hardcoded summation introduces two issues:
- Lack of flexibility: Users cannot assign different weights to each loss or choose other aggregation strategies (e.g., averaging, weighted sum).
- Lack of transparency: The simple addition is opaque to users, making it harder to diagnose issues when gradient updates behave unexpectedly.
在反向传播前,代码通过 paddle.add_n(losses) 对所有 Loss 进行简单求和。这种硬编码的聚合方式存在两个问题:
- 缺乏灵活性:用户无法自定义各个 Loss 的权重,也无法选择其他聚合策略(如平均、加权平均)。
- 不够透明:用户无法直观地感知到这个求和过程,当梯度更新出现异常时,难以定位问题根源。
Core Question 核心问题
Are the above three issues historical artifacts from the early design of paddle.Model (predating the maturity of paddle.jit.to_static and a more flexible loss handling mechanism), or are there fundamental technical constraints that prevent them from being addressed?
以上三个问题是 paddle.Model 早期设计(在 paddle.jit.to_static 成熟和更灵活的 Loss 处理机制出现之前)的历史遗留产物吗?还是说存在某些技术上的根本性原因,导致它们无法被优化?
If they are historical, I believe they constitute a strong case for refactoring paddle.Model to be more modern, flexible, and maintainable.
如果是前者,我认为它们已经构成了对 paddle.Model 进行重构的充分理由,使其更现代化、更灵活、更易维护。
Motivation 背景动机
I am interested in contributing to PaddlePaddle by potentially redesigning paddle.Model to align with modern training frameworks like PyTorch Lightning (e.g., integrating LightningModule-style hooks while ensuring backward compatibility). However, before starting such work, I’d like to confirm whether these issues are indeed legacy problems that can be streamlined.
我未来有兴趣为飞桨做贡献,可能会尝试借鉴 PyTorch Lightning 的设计理念(例如将 LightningModule 风格的钩子融入 paddle.Model,同时保持 API 兼容性)来重构它。但在开始这项工作之前,我想先确认这些问题是否是历史包袱,以及是否可以借此机会一并优化。
Thank you for your time and insights!
期待您的回复!
Source: PaddlePaddle/Paddle