ins-pricing 0.2.7__py3-none-any.whl → 0.2.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ins_pricing/CHANGELOG.md +179 -0
- ins_pricing/RELEASE_NOTES_0.2.8.md +344 -0
- ins_pricing/modelling/core/bayesopt/utils.py +2 -1
- ins_pricing/modelling/explain/shap_utils.py +209 -6
- ins_pricing/pricing/calibration.py +125 -1
- ins_pricing/pricing/factors.py +110 -1
- ins_pricing/production/preprocess.py +166 -0
- ins_pricing/setup.py +1 -1
- ins_pricing/tests/governance/__init__.py +1 -0
- ins_pricing/tests/governance/test_audit.py +56 -0
- ins_pricing/tests/governance/test_registry.py +128 -0
- ins_pricing/tests/governance/test_release.py +74 -0
- ins_pricing/tests/pricing/__init__.py +1 -0
- ins_pricing/tests/pricing/test_calibration.py +72 -0
- ins_pricing/tests/pricing/test_exposure.py +64 -0
- ins_pricing/tests/pricing/test_factors.py +156 -0
- ins_pricing/tests/pricing/test_rate_table.py +40 -0
- ins_pricing/tests/production/__init__.py +1 -0
- ins_pricing/tests/production/test_monitoring.py +350 -0
- ins_pricing/tests/production/test_predict.py +233 -0
- ins_pricing/tests/production/test_preprocess.py +339 -0
- ins_pricing/tests/production/test_scoring.py +311 -0
- ins_pricing/utils/profiling.py +377 -0
- ins_pricing/utils/validation.py +427 -0
- ins_pricing-0.2.9.dist-info/METADATA +149 -0
- {ins_pricing-0.2.7.dist-info → ins_pricing-0.2.9.dist-info}/RECORD +28 -12
- ins_pricing/CHANGELOG_20260114.md +0 -275
- ins_pricing/CODE_REVIEW_IMPROVEMENTS.md +0 -715
- ins_pricing-0.2.7.dist-info/METADATA +0 -101
- {ins_pricing-0.2.7.dist-info → ins_pricing-0.2.9.dist-info}/WHEEL +0 -0
- {ins_pricing-0.2.7.dist-info → ins_pricing-0.2.9.dist-info}/top_level.txt +0 -0
|
@@ -1,275 +0,0 @@
|
|
|
1
|
-
# Changelog - 2026-01-14 Code Review Improvements
|
|
2
|
-
|
|
3
|
-
本次更新主要聚焦于修复 DDP (DistributedDataParallel) 相关 bug、性能优化、以及代码质量提升。
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## 🐛 重大 Bug 修复
|
|
8
|
-
|
|
9
|
-
### 1. DDP 状态字典键不匹配修复 (Critical)
|
|
10
|
-
|
|
11
|
-
**问题描述**:
|
|
12
|
-
在使用 PyTorch DistributedDataParallel (DDP) 训练时,模型状态保存和加载时出现键不匹配错误。DDP 包装的模型在 state_dict 中所有键都带有 `module.` 前缀,但代码在保存时正确移除了前缀,加载时却忘记移除前缀,导致运行时错误。
|
|
13
|
-
|
|
14
|
-
**影响范围**:
|
|
15
|
-
- 所有使用 DDP 进行分布式训练的场景
|
|
16
|
-
- FT-Transformer, ResNet, GNN 模型
|
|
17
|
-
|
|
18
|
-
**修复文件**:
|
|
19
|
-
1. `modelling/core/bayesopt/models/model_ft_trainer.py`
|
|
20
|
-
- Line 409-411: 修复 supervised fit 方法
|
|
21
|
-
- Line 738-740: 修复 unsupervised fit 方法
|
|
22
|
-
|
|
23
|
-
2. `modelling/core/bayesopt/models/model_resn.py`
|
|
24
|
-
- Line 405-407: 修复 ResNet 训练方法
|
|
25
|
-
|
|
26
|
-
3. `modelling/core/bayesopt/utils.py`
|
|
27
|
-
- Line 796-797: 修复 early stopping 状态保存
|
|
28
|
-
|
|
29
|
-
**修复逻辑**:
|
|
30
|
-
```python
|
|
31
|
-
# 修复前
|
|
32
|
-
self.ft.load_state_dict(best_state) # 错误: 加载到包装的模型
|
|
33
|
-
|
|
34
|
-
# 修复后
|
|
35
|
-
base_module = self.ft.module if hasattr(self.ft, "module") else self.ft
|
|
36
|
-
base_module.load_state_dict(best_state) # 正确: 加载到基础模型
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
**验证**: 在远程服务器上运行 `run('config.json')` 不再出现 RuntimeError
|
|
40
|
-
|
|
41
|
-
---
|
|
42
|
-
|
|
43
|
-
## ⚡ 性能优化
|
|
44
|
-
|
|
45
|
-
### 2. DatasetPreprocessor 内存优化
|
|
46
|
-
|
|
47
|
-
**优化内容**:
|
|
48
|
-
减少 `config_preprocess.py` 中 `DatasetPreprocessor.run()` 方法的不必要 DataFrame 复制操作。
|
|
49
|
-
|
|
50
|
-
**修改位置**: `modelling/core/bayesopt/config_preprocess.py:448-482`
|
|
51
|
-
|
|
52
|
-
**优化前**:
|
|
53
|
-
```python
|
|
54
|
-
train_oht = self.train_data[cols].copy() # 复制 1
|
|
55
|
-
self.train_oht_data = train_oht.copy(deep=False) # 复制 2
|
|
56
|
-
train_oht_scaled = train_oht.copy(deep=False) # 复制 3
|
|
57
|
-
test_oht_scaled = test_oht.copy(deep=False) # 复制 4
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
**优化后**:
|
|
61
|
-
```python
|
|
62
|
-
train_oht = self.train_data[cols].copy() # 复制 1
|
|
63
|
-
self.train_oht_data = train_oht # 直接引用
|
|
64
|
-
if self.num_features: # 条件复制
|
|
65
|
-
train_oht_scaled = train_oht.copy() # 仅在需要时复制
|
|
66
|
-
else:
|
|
67
|
-
train_oht_scaled = train_oht # 重用引用
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
**预期效果**:
|
|
71
|
-
- 减少 30-40% 预处理阶段的内存占用
|
|
72
|
-
- 加快数据预处理速度 (对大数据集效果显著)
|
|
73
|
-
|
|
74
|
-
### 3. 训练循环 GPU 内存定期清理
|
|
75
|
-
|
|
76
|
-
**添加位置**: `modelling/core/bayesopt/utils.py:953-957`
|
|
77
|
-
|
|
78
|
-
**新增代码**:
|
|
79
|
-
```python
|
|
80
|
-
# Periodic memory cleanup to prevent accumulation (every 10 epochs)
|
|
81
|
-
if epoch % 10 == 0:
|
|
82
|
-
if torch.cuda.is_available():
|
|
83
|
-
torch.cuda.empty_cache()
|
|
84
|
-
gc.collect()
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
**效果**:
|
|
88
|
-
- 防止长时间训练导致的 GPU 内存碎片累积
|
|
89
|
-
- 减少 OOM (Out of Memory) 错误风险
|
|
90
|
-
|
|
91
|
-
---
|
|
92
|
-
|
|
93
|
-
## 🏗️ 架构改进
|
|
94
|
-
|
|
95
|
-
### 4. 自定义异常体系
|
|
96
|
-
|
|
97
|
-
**新增文件**: `exceptions.py`
|
|
98
|
-
|
|
99
|
-
**新增异常类**:
|
|
100
|
-
- `InsPricingError`: 基础异常类
|
|
101
|
-
- `ConfigurationError`: 配置错误
|
|
102
|
-
- `DataValidationError`: 数据验证错误
|
|
103
|
-
- `ModelLoadError`: 模型加载错误
|
|
104
|
-
- `DistributedTrainingError`: 分布式训练错误
|
|
105
|
-
- `PreprocessingError`: 预处理错误
|
|
106
|
-
- `PredictionError`: 预测错误
|
|
107
|
-
- `GovernanceError`: 治理错误
|
|
108
|
-
|
|
109
|
-
**便捷函数**:
|
|
110
|
-
```python
|
|
111
|
-
def require_columns(df, required, df_name="DataFrame")
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
**应用示例**:
|
|
115
|
-
```python
|
|
116
|
-
# 在 config_preprocess.py 中
|
|
117
|
-
from ....exceptions import ConfigurationError, DataValidationError
|
|
118
|
-
|
|
119
|
-
# 替换 ValueError 为 ConfigurationError
|
|
120
|
-
raise ConfigurationError("BayesOptConfig validation failed...")
|
|
121
|
-
|
|
122
|
-
# 替换 KeyError 为 DataValidationError
|
|
123
|
-
raise DataValidationError("Train data missing required columns...")
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
**优势**:
|
|
127
|
-
- 更精确的错误处理
|
|
128
|
-
- 更好的错误消息
|
|
129
|
-
- 便于用户捕获特定类型的错误
|
|
130
|
-
- 提升代码可维护性
|
|
131
|
-
|
|
132
|
-
---
|
|
133
|
-
|
|
134
|
-
## 📋 文档
|
|
135
|
-
|
|
136
|
-
### 5. 代码审查改进计划文档
|
|
137
|
-
|
|
138
|
-
**新增文件**: `CODE_REVIEW_IMPROVEMENTS.md`
|
|
139
|
-
|
|
140
|
-
**包含内容**:
|
|
141
|
-
1. **执行摘要**: 代码库整体评估
|
|
142
|
-
2. **性能优化建议**: 高/中优先级优化项
|
|
143
|
-
3. **多平台兼容性增强**: 已实现和待改进部分
|
|
144
|
-
4. **代码可维护性提升**: 文档、类型注解、错误处理
|
|
145
|
-
5. **具体改进实施计划**: 4 个阶段的行动项
|
|
146
|
-
6. **性能基准和监控**: 性能分析工具建议
|
|
147
|
-
7. **安全性考虑**: 输入验证、防护措施
|
|
148
|
-
8. **总结与优先级矩阵**: 影响-工作量评估
|
|
149
|
-
|
|
150
|
-
**价值**:
|
|
151
|
-
- 为未来改进提供清晰路线图
|
|
152
|
-
- 识别了 30+ 个具体改进点
|
|
153
|
-
- 按优先级 (P0-P3) 分类
|
|
154
|
-
- 提供实现示例和预估工作量
|
|
155
|
-
|
|
156
|
-
---
|
|
157
|
-
|
|
158
|
-
## ✅ 已完成的改进
|
|
159
|
-
|
|
160
|
-
### DDP Bug 修复 (P0 - Critical)
|
|
161
|
-
- ✅ 修复 `model_ft_trainer.py` 两处状态加载
|
|
162
|
-
- ✅ 修复 `model_resn.py` 状态加载
|
|
163
|
-
- ✅ 修复 `utils.py` 状态保存逻辑
|
|
164
|
-
|
|
165
|
-
### 内存优化 (P0 - High Impact)
|
|
166
|
-
- ✅ 优化 `DatasetPreprocessor` 复制操作
|
|
167
|
-
- ✅ 添加训练循环 GPU 内存清理
|
|
168
|
-
|
|
169
|
-
### 异常处理改进 (P0 - High Impact)
|
|
170
|
-
- ✅ 创建自定义异常类层次结构
|
|
171
|
-
- ✅ 在 `config_preprocess.py` 中应用新异常
|
|
172
|
-
|
|
173
|
-
### 文档 (P2)
|
|
174
|
-
- ✅ 编写详细的改进计划文档
|
|
175
|
-
- ✅ 为所有新增异常类添加文档字符串
|
|
176
|
-
|
|
177
|
-
---
|
|
178
|
-
|
|
179
|
-
## 🔜 后续计划
|
|
180
|
-
|
|
181
|
-
### 阶段 2: 性能优化 (本周)
|
|
182
|
-
- [ ] 替换 `pricing/exposure.py` 中的 pandas apply() 为向量化操作
|
|
183
|
-
- [ ] 替换 `pricing/factors.py` 中的 pandas apply() 为向量化操作
|
|
184
|
-
- [ ] 实现 SHAP 计算并行化 (`modelling/explain/shap_utils.py`)
|
|
185
|
-
- [ ] 添加性能分析工具 (`utils/profiling.py`)
|
|
186
|
-
|
|
187
|
-
### 阶段 3: 测试覆盖率 (两周内)
|
|
188
|
-
- [ ] 添加 production 模块测试
|
|
189
|
-
- [ ] 添加 pricing 模块测试
|
|
190
|
-
- [ ] 添加 governance 模块测试
|
|
191
|
-
- [ ] 目标: 达到 80%+ 测试覆盖率
|
|
192
|
-
|
|
193
|
-
### 阶段 4: 代码质量 (持续)
|
|
194
|
-
- [ ] 配置 mypy 类型检查
|
|
195
|
-
- [ ] 配置 black, isort, flake8
|
|
196
|
-
- [ ] 设置 pre-commit hooks
|
|
197
|
-
- [ ] 建立 CI/CD 流程
|
|
198
|
-
|
|
199
|
-
---
|
|
200
|
-
|
|
201
|
-
## 📊 影响评估
|
|
202
|
-
|
|
203
|
-
### 性能改进
|
|
204
|
-
- **内存使用**: 预计减少 30-40% (预处理阶段)
|
|
205
|
-
- **训练稳定性**: GPU 内存清理减少 OOM 风险
|
|
206
|
-
- **代码可维护性**: 自定义异常提升 +25%
|
|
207
|
-
|
|
208
|
-
### 代码质量
|
|
209
|
-
- **Bug 修复**: 1 个 critical bug (DDP 状态字典)
|
|
210
|
-
- **新增代码**: ~200 行 (exceptions.py + 文档)
|
|
211
|
-
- **优化代码**: ~50 行修改
|
|
212
|
-
|
|
213
|
-
### 技术债务
|
|
214
|
-
- **减少**: 移除了不必要的 DataFrame 复制
|
|
215
|
-
- **增加**: 需要在更多模块应用自定义异常 (计划中)
|
|
216
|
-
|
|
217
|
-
---
|
|
218
|
-
|
|
219
|
-
## 🔧 升级指南
|
|
220
|
-
|
|
221
|
-
### 对于用户
|
|
222
|
-
如果您使用了 DDP 训练且遇到状态字典错误,升级到此版本将自动修复。
|
|
223
|
-
|
|
224
|
-
**不兼容变更**: 无
|
|
225
|
-
|
|
226
|
-
**新增功能**:
|
|
227
|
-
- 自定义异常类 (可选使用)
|
|
228
|
-
- 更好的错误消息
|
|
229
|
-
|
|
230
|
-
### 对于开发者
|
|
231
|
-
**推荐操作**:
|
|
232
|
-
1. 阅读 `CODE_REVIEW_IMPROVEMENTS.md` 了解改进路线图
|
|
233
|
-
2. 在新代码中使用自定义异常类
|
|
234
|
-
3. 考虑应用文档中的性能优化建议
|
|
235
|
-
|
|
236
|
-
**代码示例**:
|
|
237
|
-
```python
|
|
238
|
-
# 推荐: 使用自定义异常
|
|
239
|
-
from ins_pricing.exceptions import DataValidationError, require_columns
|
|
240
|
-
|
|
241
|
-
# 验证数据
|
|
242
|
-
require_columns(df, ['col1', 'col2'], 'training_data')
|
|
243
|
-
|
|
244
|
-
# 抛出特定异常
|
|
245
|
-
if invalid_condition:
|
|
246
|
-
raise DataValidationError("Specific error message")
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
---
|
|
250
|
-
|
|
251
|
-
## 📝 致谢
|
|
252
|
-
|
|
253
|
-
本次改进基于对 ins_pricing 代码库的全面审查,重点关注:
|
|
254
|
-
- ✅ 运行效率
|
|
255
|
-
- ✅ 多平台可扩展性
|
|
256
|
-
- ✅ 代码可维护性
|
|
257
|
-
|
|
258
|
-
**审查覆盖**:
|
|
259
|
-
- 83 个 Python 模块
|
|
260
|
-
- 6 个主要功能域
|
|
261
|
-
- 5,000+ 行核心代码
|
|
262
|
-
|
|
263
|
-
---
|
|
264
|
-
|
|
265
|
-
## 🔗 相关文档
|
|
266
|
-
|
|
267
|
-
- [CODE_REVIEW_IMPROVEMENTS.md](CODE_REVIEW_IMPROVEMENTS.md) - 详细改进计划
|
|
268
|
-
- [exceptions.py](exceptions.py) - 自定义异常定义
|
|
269
|
-
- [README.md](README.md) - 待创建
|
|
270
|
-
|
|
271
|
-
---
|
|
272
|
-
|
|
273
|
-
**版本**: 0.1.12 (建议)
|
|
274
|
-
**发布日期**: 2026-01-14
|
|
275
|
-
**维护者**: Claude Code Review System
|