ins-pricing 0.1.11__py3-none-any.whl → 0.2.0__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/README.md +9 -6
- ins_pricing/__init__.py +3 -11
- ins_pricing/cli/BayesOpt_entry.py +24 -0
- ins_pricing/{modelling → cli}/BayesOpt_incremental.py +197 -64
- ins_pricing/cli/Explain_Run.py +25 -0
- ins_pricing/{modelling → cli}/Explain_entry.py +169 -124
- ins_pricing/cli/Pricing_Run.py +25 -0
- ins_pricing/cli/__init__.py +1 -0
- ins_pricing/cli/bayesopt_entry_runner.py +1312 -0
- ins_pricing/cli/utils/__init__.py +1 -0
- ins_pricing/cli/utils/cli_common.py +320 -0
- ins_pricing/cli/utils/cli_config.py +375 -0
- ins_pricing/{modelling → cli/utils}/notebook_utils.py +74 -19
- {ins_pricing_gemini/modelling → ins_pricing/cli}/watchdog_run.py +2 -2
- ins_pricing/{modelling → docs/modelling}/BayesOpt_USAGE.md +69 -49
- ins_pricing/docs/modelling/README.md +34 -0
- ins_pricing/modelling/__init__.py +57 -6
- ins_pricing/modelling/core/__init__.py +1 -0
- ins_pricing/modelling/{bayesopt → core/bayesopt}/config_preprocess.py +64 -1
- ins_pricing/modelling/{bayesopt → core/bayesopt}/core.py +150 -810
- ins_pricing/modelling/core/bayesopt/model_explain_mixin.py +296 -0
- ins_pricing/modelling/core/bayesopt/model_plotting_mixin.py +548 -0
- ins_pricing/modelling/core/bayesopt/models/__init__.py +27 -0
- ins_pricing/modelling/core/bayesopt/models/model_ft_components.py +316 -0
- ins_pricing/modelling/core/bayesopt/models/model_ft_trainer.py +808 -0
- ins_pricing/modelling/core/bayesopt/models/model_gnn.py +675 -0
- ins_pricing/modelling/core/bayesopt/models/model_resn.py +435 -0
- ins_pricing/modelling/core/bayesopt/trainers/__init__.py +19 -0
- ins_pricing/modelling/core/bayesopt/trainers/trainer_base.py +1020 -0
- ins_pricing/modelling/core/bayesopt/trainers/trainer_ft.py +787 -0
- ins_pricing/modelling/core/bayesopt/trainers/trainer_glm.py +195 -0
- ins_pricing/modelling/core/bayesopt/trainers/trainer_gnn.py +312 -0
- ins_pricing/modelling/core/bayesopt/trainers/trainer_resn.py +261 -0
- ins_pricing/modelling/core/bayesopt/trainers/trainer_xgb.py +348 -0
- ins_pricing/modelling/{bayesopt → core/bayesopt}/utils.py +2 -2
- ins_pricing/modelling/core/evaluation.py +115 -0
- ins_pricing/production/__init__.py +4 -0
- ins_pricing/production/preprocess.py +71 -0
- ins_pricing/setup.py +10 -5
- {ins_pricing_gemini/modelling/tests → ins_pricing/tests/modelling}/test_plotting.py +2 -2
- {ins_pricing-0.1.11.dist-info → ins_pricing-0.2.0.dist-info}/METADATA +4 -4
- ins_pricing-0.2.0.dist-info/RECORD +125 -0
- {ins_pricing-0.1.11.dist-info → ins_pricing-0.2.0.dist-info}/top_level.txt +0 -1
- ins_pricing/modelling/BayesOpt_entry.py +0 -633
- ins_pricing/modelling/Explain_Run.py +0 -36
- ins_pricing/modelling/Pricing_Run.py +0 -36
- ins_pricing/modelling/README.md +0 -33
- ins_pricing/modelling/bayesopt/models.py +0 -2196
- ins_pricing/modelling/bayesopt/trainers.py +0 -2446
- ins_pricing/modelling/cli_common.py +0 -136
- ins_pricing/modelling/tests/test_plotting.py +0 -63
- ins_pricing/modelling/watchdog_run.py +0 -211
- ins_pricing-0.1.11.dist-info/RECORD +0 -169
- ins_pricing_gemini/__init__.py +0 -23
- ins_pricing_gemini/governance/__init__.py +0 -20
- ins_pricing_gemini/governance/approval.py +0 -93
- ins_pricing_gemini/governance/audit.py +0 -37
- ins_pricing_gemini/governance/registry.py +0 -99
- ins_pricing_gemini/governance/release.py +0 -159
- ins_pricing_gemini/modelling/Explain_Run.py +0 -36
- ins_pricing_gemini/modelling/Pricing_Run.py +0 -36
- ins_pricing_gemini/modelling/__init__.py +0 -151
- ins_pricing_gemini/modelling/cli_common.py +0 -141
- ins_pricing_gemini/modelling/config.py +0 -249
- ins_pricing_gemini/modelling/config_preprocess.py +0 -254
- ins_pricing_gemini/modelling/core.py +0 -741
- ins_pricing_gemini/modelling/data_container.py +0 -42
- ins_pricing_gemini/modelling/explain/__init__.py +0 -55
- ins_pricing_gemini/modelling/explain/gradients.py +0 -334
- ins_pricing_gemini/modelling/explain/metrics.py +0 -176
- ins_pricing_gemini/modelling/explain/permutation.py +0 -155
- ins_pricing_gemini/modelling/explain/shap_utils.py +0 -146
- ins_pricing_gemini/modelling/features.py +0 -215
- ins_pricing_gemini/modelling/model_manager.py +0 -148
- ins_pricing_gemini/modelling/model_plotting.py +0 -463
- ins_pricing_gemini/modelling/models.py +0 -2203
- ins_pricing_gemini/modelling/notebook_utils.py +0 -294
- ins_pricing_gemini/modelling/plotting/__init__.py +0 -45
- ins_pricing_gemini/modelling/plotting/common.py +0 -63
- ins_pricing_gemini/modelling/plotting/curves.py +0 -572
- ins_pricing_gemini/modelling/plotting/diagnostics.py +0 -139
- ins_pricing_gemini/modelling/plotting/geo.py +0 -362
- ins_pricing_gemini/modelling/plotting/importance.py +0 -121
- ins_pricing_gemini/modelling/run_logging.py +0 -133
- ins_pricing_gemini/modelling/tests/conftest.py +0 -8
- ins_pricing_gemini/modelling/tests/test_cross_val_generic.py +0 -66
- ins_pricing_gemini/modelling/tests/test_distributed_utils.py +0 -18
- ins_pricing_gemini/modelling/tests/test_explain.py +0 -56
- ins_pricing_gemini/modelling/tests/test_geo_tokens_split.py +0 -49
- ins_pricing_gemini/modelling/tests/test_graph_cache.py +0 -33
- ins_pricing_gemini/modelling/tests/test_plotting_library.py +0 -150
- ins_pricing_gemini/modelling/tests/test_preprocessor.py +0 -48
- ins_pricing_gemini/modelling/trainers.py +0 -2447
- ins_pricing_gemini/modelling/utils.py +0 -1020
- ins_pricing_gemini/pricing/__init__.py +0 -27
- ins_pricing_gemini/pricing/calibration.py +0 -39
- ins_pricing_gemini/pricing/data_quality.py +0 -117
- ins_pricing_gemini/pricing/exposure.py +0 -85
- ins_pricing_gemini/pricing/factors.py +0 -91
- ins_pricing_gemini/pricing/monitoring.py +0 -99
- ins_pricing_gemini/pricing/rate_table.py +0 -78
- ins_pricing_gemini/production/__init__.py +0 -21
- ins_pricing_gemini/production/drift.py +0 -30
- ins_pricing_gemini/production/monitoring.py +0 -143
- ins_pricing_gemini/production/scoring.py +0 -40
- ins_pricing_gemini/reporting/__init__.py +0 -11
- ins_pricing_gemini/reporting/report_builder.py +0 -72
- ins_pricing_gemini/reporting/scheduler.py +0 -45
- ins_pricing_gemini/scripts/BayesOpt_incremental.py +0 -722
- ins_pricing_gemini/scripts/Explain_entry.py +0 -545
- ins_pricing_gemini/scripts/__init__.py +0 -1
- ins_pricing_gemini/scripts/train.py +0 -568
- ins_pricing_gemini/setup.py +0 -55
- ins_pricing_gemini/smoke_test.py +0 -28
- /ins_pricing/{modelling → cli/utils}/run_logging.py +0 -0
- /ins_pricing/modelling/{BayesOpt.py → core/BayesOpt.py} +0 -0
- /ins_pricing/modelling/{bayesopt → core/bayesopt}/__init__.py +0 -0
- /ins_pricing/{modelling/tests → tests/modelling}/conftest.py +0 -0
- /ins_pricing/{modelling/tests → tests/modelling}/test_cross_val_generic.py +0 -0
- /ins_pricing/{modelling/tests → tests/modelling}/test_distributed_utils.py +0 -0
- /ins_pricing/{modelling/tests → tests/modelling}/test_explain.py +0 -0
- /ins_pricing/{modelling/tests → tests/modelling}/test_geo_tokens_split.py +0 -0
- /ins_pricing/{modelling/tests → tests/modelling}/test_graph_cache.py +0 -0
- /ins_pricing/{modelling/tests → tests/modelling}/test_plotting_library.py +0 -0
- /ins_pricing/{modelling/tests → tests/modelling}/test_preprocessor.py +0 -0
- {ins_pricing-0.1.11.dist-info → ins_pricing-0.2.0.dist-info}/WHEEL +0 -0
|
@@ -1,633 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
CLI entry point generated from BayesOpt_AutoPricing.ipynb so the workflow can
|
|
3
|
-
run non‑interactively (e.g., via torchrun).
|
|
4
|
-
|
|
5
|
-
Example:
|
|
6
|
-
python -m torch.distributed.run --standalone --nproc_per_node=2 \\
|
|
7
|
-
ins_pricing/modelling/BayesOpt_entry.py \\
|
|
8
|
-
--config-json ins_pricing/modelling/demo/config_template.json \\
|
|
9
|
-
--model-keys ft --max-evals 50 --use-ft-ddp
|
|
10
|
-
"""
|
|
11
|
-
|
|
12
|
-
from __future__ import annotations
|
|
13
|
-
|
|
14
|
-
import argparse
|
|
15
|
-
import os
|
|
16
|
-
from pathlib import Path
|
|
17
|
-
from typing import Dict, List
|
|
18
|
-
|
|
19
|
-
import pandas as pd
|
|
20
|
-
from sklearn.model_selection import train_test_split
|
|
21
|
-
|
|
22
|
-
try:
|
|
23
|
-
from . import bayesopt as ropt # type: ignore
|
|
24
|
-
from .cli_common import ( # type: ignore
|
|
25
|
-
PLOT_MODEL_LABELS,
|
|
26
|
-
PYTORCH_TRAINERS,
|
|
27
|
-
build_model_names,
|
|
28
|
-
dedupe_preserve_order,
|
|
29
|
-
load_config_json,
|
|
30
|
-
normalize_config_paths,
|
|
31
|
-
parse_model_pairs,
|
|
32
|
-
resolve_config_path,
|
|
33
|
-
resolve_path,
|
|
34
|
-
set_env,
|
|
35
|
-
)
|
|
36
|
-
except Exception: # pragma: no cover
|
|
37
|
-
try:
|
|
38
|
-
import bayesopt as ropt # type: ignore
|
|
39
|
-
from cli_common import ( # type: ignore
|
|
40
|
-
PLOT_MODEL_LABELS,
|
|
41
|
-
PYTORCH_TRAINERS,
|
|
42
|
-
build_model_names,
|
|
43
|
-
dedupe_preserve_order,
|
|
44
|
-
load_config_json,
|
|
45
|
-
normalize_config_paths,
|
|
46
|
-
parse_model_pairs,
|
|
47
|
-
resolve_config_path,
|
|
48
|
-
resolve_path,
|
|
49
|
-
set_env,
|
|
50
|
-
)
|
|
51
|
-
except Exception:
|
|
52
|
-
try:
|
|
53
|
-
import ins_pricing.bayesopt as ropt # type: ignore
|
|
54
|
-
from ins_pricing.cli_common import ( # type: ignore
|
|
55
|
-
PLOT_MODEL_LABELS,
|
|
56
|
-
PYTORCH_TRAINERS,
|
|
57
|
-
build_model_names,
|
|
58
|
-
dedupe_preserve_order,
|
|
59
|
-
load_config_json,
|
|
60
|
-
normalize_config_paths,
|
|
61
|
-
parse_model_pairs,
|
|
62
|
-
resolve_config_path,
|
|
63
|
-
resolve_path,
|
|
64
|
-
set_env,
|
|
65
|
-
)
|
|
66
|
-
except Exception:
|
|
67
|
-
import BayesOpt as ropt # type: ignore
|
|
68
|
-
from cli_common import ( # type: ignore
|
|
69
|
-
PLOT_MODEL_LABELS,
|
|
70
|
-
PYTORCH_TRAINERS,
|
|
71
|
-
build_model_names,
|
|
72
|
-
dedupe_preserve_order,
|
|
73
|
-
load_config_json,
|
|
74
|
-
normalize_config_paths,
|
|
75
|
-
parse_model_pairs,
|
|
76
|
-
resolve_config_path,
|
|
77
|
-
resolve_path,
|
|
78
|
-
set_env,
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
import matplotlib
|
|
82
|
-
|
|
83
|
-
if os.name != "nt" and not os.environ.get("DISPLAY") and not os.environ.get("MPLBACKEND"):
|
|
84
|
-
matplotlib.use("Agg")
|
|
85
|
-
import matplotlib.pyplot as plt
|
|
86
|
-
|
|
87
|
-
try:
|
|
88
|
-
from .run_logging import configure_run_logging # type: ignore
|
|
89
|
-
except Exception: # pragma: no cover
|
|
90
|
-
try:
|
|
91
|
-
from run_logging import configure_run_logging # type: ignore
|
|
92
|
-
except Exception: # pragma: no cover
|
|
93
|
-
configure_run_logging = None # type: ignore
|
|
94
|
-
|
|
95
|
-
try:
|
|
96
|
-
from .plotting.diagnostics import plot_loss_curve as plot_loss_curve_common
|
|
97
|
-
except Exception: # pragma: no cover
|
|
98
|
-
try:
|
|
99
|
-
from ins_pricing.plotting.diagnostics import plot_loss_curve as plot_loss_curve_common
|
|
100
|
-
except Exception: # pragma: no cover
|
|
101
|
-
plot_loss_curve_common = None
|
|
102
|
-
|
|
103
|
-
def _parse_args() -> argparse.Namespace:
|
|
104
|
-
parser = argparse.ArgumentParser(
|
|
105
|
-
description="Batch trainer generated from BayesOpt_AutoPricing notebook."
|
|
106
|
-
)
|
|
107
|
-
parser.add_argument(
|
|
108
|
-
"--config-json",
|
|
109
|
-
required=True,
|
|
110
|
-
help="Path to the JSON config describing datasets and feature columns.",
|
|
111
|
-
)
|
|
112
|
-
parser.add_argument(
|
|
113
|
-
"--model-keys",
|
|
114
|
-
nargs="+",
|
|
115
|
-
default=["ft"],
|
|
116
|
-
choices=["glm", "xgb", "resn", "ft", "gnn", "all"],
|
|
117
|
-
help="Space-separated list of trainers to run (e.g., --model-keys glm xgb). Include 'all' to run every trainer.",
|
|
118
|
-
)
|
|
119
|
-
parser.add_argument(
|
|
120
|
-
"--stack-model-keys",
|
|
121
|
-
nargs="+",
|
|
122
|
-
default=None,
|
|
123
|
-
choices=["glm", "xgb", "resn", "ft", "gnn", "all"],
|
|
124
|
-
help=(
|
|
125
|
-
"Only used when ft_role != 'model' (FT runs as feature generator). "
|
|
126
|
-
"When provided (or when config defines stack_model_keys), these trainers run after FT features "
|
|
127
|
-
"are generated. Use 'all' to run every non-FT trainer."
|
|
128
|
-
),
|
|
129
|
-
)
|
|
130
|
-
parser.add_argument(
|
|
131
|
-
"--max-evals",
|
|
132
|
-
type=int,
|
|
133
|
-
default=50,
|
|
134
|
-
help="Optuna trial count per dataset.",
|
|
135
|
-
)
|
|
136
|
-
parser.add_argument(
|
|
137
|
-
"--use-resn-ddp",
|
|
138
|
-
action="store_true",
|
|
139
|
-
help="Force ResNet trainer to use DistributedDataParallel.",
|
|
140
|
-
)
|
|
141
|
-
parser.add_argument(
|
|
142
|
-
"--use-ft-ddp",
|
|
143
|
-
action="store_true",
|
|
144
|
-
help="Force FT-Transformer trainer to use DistributedDataParallel.",
|
|
145
|
-
)
|
|
146
|
-
parser.add_argument(
|
|
147
|
-
"--use-resn-dp",
|
|
148
|
-
action="store_true",
|
|
149
|
-
help="Enable ResNet DataParallel fall-back regardless of config.",
|
|
150
|
-
)
|
|
151
|
-
parser.add_argument(
|
|
152
|
-
"--use-ft-dp",
|
|
153
|
-
action="store_true",
|
|
154
|
-
help="Enable FT-Transformer DataParallel fall-back regardless of config.",
|
|
155
|
-
)
|
|
156
|
-
parser.add_argument(
|
|
157
|
-
"--use-gnn-dp",
|
|
158
|
-
action="store_true",
|
|
159
|
-
help="Enable GNN DataParallel fall-back regardless of config.",
|
|
160
|
-
)
|
|
161
|
-
parser.add_argument(
|
|
162
|
-
"--use-gnn-ddp",
|
|
163
|
-
action="store_true",
|
|
164
|
-
help="Force GNN trainer to use DistributedDataParallel.",
|
|
165
|
-
)
|
|
166
|
-
parser.add_argument(
|
|
167
|
-
"--gnn-no-ann",
|
|
168
|
-
action="store_true",
|
|
169
|
-
help="Disable approximate k-NN for GNN graph construction and use exact search.",
|
|
170
|
-
)
|
|
171
|
-
parser.add_argument(
|
|
172
|
-
"--gnn-ann-threshold",
|
|
173
|
-
type=int,
|
|
174
|
-
default=None,
|
|
175
|
-
help="Row threshold above which approximate k-NN is preferred (overrides config).",
|
|
176
|
-
)
|
|
177
|
-
parser.add_argument(
|
|
178
|
-
"--gnn-graph-cache",
|
|
179
|
-
default=None,
|
|
180
|
-
help="Optional path to persist/load cached adjacency matrix for GNN.",
|
|
181
|
-
)
|
|
182
|
-
parser.add_argument(
|
|
183
|
-
"--gnn-max-gpu-nodes",
|
|
184
|
-
type=int,
|
|
185
|
-
default=None,
|
|
186
|
-
help="Overrides the maximum node count allowed for GPU k-NN graph construction.",
|
|
187
|
-
)
|
|
188
|
-
parser.add_argument(
|
|
189
|
-
"--gnn-gpu-mem-ratio",
|
|
190
|
-
type=float,
|
|
191
|
-
default=None,
|
|
192
|
-
help="Overrides the fraction of free GPU memory the k-NN builder may consume.",
|
|
193
|
-
)
|
|
194
|
-
parser.add_argument(
|
|
195
|
-
"--gnn-gpu-mem-overhead",
|
|
196
|
-
type=float,
|
|
197
|
-
default=None,
|
|
198
|
-
help="Overrides the temporary GPU memory overhead multiplier for k-NN estimation.",
|
|
199
|
-
)
|
|
200
|
-
parser.add_argument(
|
|
201
|
-
"--output-dir",
|
|
202
|
-
default=None,
|
|
203
|
-
help="Override output root for models/results/plots.",
|
|
204
|
-
)
|
|
205
|
-
parser.add_argument(
|
|
206
|
-
"--plot-curves",
|
|
207
|
-
action="store_true",
|
|
208
|
-
help="Enable lift/diagnostic plots after training (config file may also request plotting).",
|
|
209
|
-
)
|
|
210
|
-
parser.add_argument(
|
|
211
|
-
"--ft-as-feature",
|
|
212
|
-
action="store_true",
|
|
213
|
-
help="Alias for --ft-role embedding (keep tuning, export embeddings; skip FT plots/SHAP).",
|
|
214
|
-
)
|
|
215
|
-
parser.add_argument(
|
|
216
|
-
"--ft-role",
|
|
217
|
-
default=None,
|
|
218
|
-
choices=["model", "embedding", "unsupervised_embedding"],
|
|
219
|
-
help="How to use FT: model (default), embedding (export pooling embeddings), or unsupervised_embedding.",
|
|
220
|
-
)
|
|
221
|
-
parser.add_argument(
|
|
222
|
-
"--ft-feature-prefix",
|
|
223
|
-
default="ft_feat",
|
|
224
|
-
help="Prefix used for generated FT features (columns: pred_<prefix>_0.. or pred_<prefix>).",
|
|
225
|
-
)
|
|
226
|
-
parser.add_argument(
|
|
227
|
-
"--reuse-best-params",
|
|
228
|
-
action="store_true",
|
|
229
|
-
help="Skip Optuna and reuse best_params saved in Results/versions or bestparams CSV when available.",
|
|
230
|
-
)
|
|
231
|
-
return parser.parse_args()
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
def _plot_curves_for_model(model: ropt.BayesOptModel, trained_keys: List[str], cfg: Dict) -> None:
|
|
235
|
-
plot_cfg = cfg.get("plot", {})
|
|
236
|
-
legacy_lift_flags = {
|
|
237
|
-
"glm": cfg.get("plot_lift_glm", False),
|
|
238
|
-
"xgb": cfg.get("plot_lift_xgb", False),
|
|
239
|
-
"resn": cfg.get("plot_lift_resn", False),
|
|
240
|
-
"ft": cfg.get("plot_lift_ft", False),
|
|
241
|
-
}
|
|
242
|
-
plot_enabled = plot_cfg.get("enable", any(legacy_lift_flags.values()))
|
|
243
|
-
if not plot_enabled:
|
|
244
|
-
return
|
|
245
|
-
|
|
246
|
-
n_bins = int(plot_cfg.get("n_bins", 10))
|
|
247
|
-
oneway_enabled = plot_cfg.get("oneway", True)
|
|
248
|
-
|
|
249
|
-
available_models = dedupe_preserve_order(
|
|
250
|
-
[m for m in trained_keys if m in PLOT_MODEL_LABELS]
|
|
251
|
-
)
|
|
252
|
-
|
|
253
|
-
lift_models = plot_cfg.get("lift_models")
|
|
254
|
-
if lift_models is None:
|
|
255
|
-
lift_models = [m for m, enabled in legacy_lift_flags.items() if enabled]
|
|
256
|
-
if not lift_models:
|
|
257
|
-
lift_models = available_models
|
|
258
|
-
lift_models = dedupe_preserve_order(
|
|
259
|
-
[m for m in lift_models if m in available_models]
|
|
260
|
-
)
|
|
261
|
-
|
|
262
|
-
if oneway_enabled:
|
|
263
|
-
oneway_pred = bool(plot_cfg.get("oneway_pred", False))
|
|
264
|
-
oneway_pred_models = plot_cfg.get("oneway_pred_models")
|
|
265
|
-
pred_plotted = False
|
|
266
|
-
if oneway_pred:
|
|
267
|
-
if oneway_pred_models is None:
|
|
268
|
-
oneway_pred_models = lift_models or available_models
|
|
269
|
-
oneway_pred_models = dedupe_preserve_order(
|
|
270
|
-
[m for m in oneway_pred_models if m in available_models]
|
|
271
|
-
)
|
|
272
|
-
for model_key in oneway_pred_models:
|
|
273
|
-
label, pred_nme = PLOT_MODEL_LABELS[model_key]
|
|
274
|
-
if pred_nme not in model.train_data.columns:
|
|
275
|
-
print(
|
|
276
|
-
f"[Oneway] Missing prediction column '{pred_nme}'; skip.",
|
|
277
|
-
flush=True,
|
|
278
|
-
)
|
|
279
|
-
continue
|
|
280
|
-
model.plot_oneway(
|
|
281
|
-
n_bins=n_bins,
|
|
282
|
-
pred_col=pred_nme,
|
|
283
|
-
pred_label=label,
|
|
284
|
-
plot_subdir="oneway/post",
|
|
285
|
-
)
|
|
286
|
-
pred_plotted = True
|
|
287
|
-
if not oneway_pred or not pred_plotted:
|
|
288
|
-
model.plot_oneway(n_bins=n_bins, plot_subdir="oneway/post")
|
|
289
|
-
|
|
290
|
-
if not available_models:
|
|
291
|
-
return
|
|
292
|
-
|
|
293
|
-
for model_key in lift_models:
|
|
294
|
-
label, pred_nme = PLOT_MODEL_LABELS[model_key]
|
|
295
|
-
model.plot_lift(model_label=label, pred_nme=pred_nme, n_bins=n_bins)
|
|
296
|
-
|
|
297
|
-
if not plot_cfg.get("double_lift", True) or len(available_models) < 2:
|
|
298
|
-
return
|
|
299
|
-
|
|
300
|
-
raw_pairs = plot_cfg.get("double_lift_pairs")
|
|
301
|
-
if raw_pairs:
|
|
302
|
-
pairs = [
|
|
303
|
-
(a, b)
|
|
304
|
-
for a, b in parse_model_pairs(raw_pairs)
|
|
305
|
-
if a in available_models and b in available_models and a != b
|
|
306
|
-
]
|
|
307
|
-
else:
|
|
308
|
-
pairs = [(a, b) for i, a in enumerate(available_models) for b in available_models[i + 1 :]]
|
|
309
|
-
|
|
310
|
-
for first, second in pairs:
|
|
311
|
-
model.plot_dlift([first, second], n_bins=n_bins)
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
def _plot_loss_curve_for_trainer(model_name: str, trainer) -> None:
|
|
315
|
-
model_obj = getattr(trainer, "model", None)
|
|
316
|
-
history = None
|
|
317
|
-
if model_obj is not None:
|
|
318
|
-
history = getattr(model_obj, "training_history", None)
|
|
319
|
-
if not history:
|
|
320
|
-
history = getattr(trainer, "training_history", None)
|
|
321
|
-
if not history:
|
|
322
|
-
return
|
|
323
|
-
train_hist = list(history.get("train") or [])
|
|
324
|
-
val_hist = list(history.get("val") or [])
|
|
325
|
-
if not train_hist and not val_hist:
|
|
326
|
-
return
|
|
327
|
-
try:
|
|
328
|
-
plot_dir = trainer.output.plot_path(
|
|
329
|
-
f"{model_name}/loss/loss_{model_name}_{trainer.model_name_prefix}.png"
|
|
330
|
-
)
|
|
331
|
-
except Exception:
|
|
332
|
-
default_dir = Path("plot") / model_name / "loss"
|
|
333
|
-
default_dir.mkdir(parents=True, exist_ok=True)
|
|
334
|
-
plot_dir = str(
|
|
335
|
-
default_dir / f"loss_{model_name}_{trainer.model_name_prefix}.png")
|
|
336
|
-
if plot_loss_curve_common is not None:
|
|
337
|
-
plot_loss_curve_common(
|
|
338
|
-
history=history,
|
|
339
|
-
title=f"{trainer.model_name_prefix} Loss Curve ({model_name})",
|
|
340
|
-
save_path=plot_dir,
|
|
341
|
-
show=False,
|
|
342
|
-
)
|
|
343
|
-
else:
|
|
344
|
-
epochs = range(1, max(len(train_hist), len(val_hist)) + 1)
|
|
345
|
-
fig, ax = plt.subplots(figsize=(8, 4))
|
|
346
|
-
if train_hist:
|
|
347
|
-
ax.plot(range(1, len(train_hist) + 1),
|
|
348
|
-
train_hist, label="Train Loss", color="tab:blue")
|
|
349
|
-
if val_hist:
|
|
350
|
-
ax.plot(range(1, len(val_hist) + 1),
|
|
351
|
-
val_hist, label="Validation Loss", color="tab:orange")
|
|
352
|
-
ax.set_xlabel("Epoch")
|
|
353
|
-
ax.set_ylabel("Weighted Loss")
|
|
354
|
-
ax.set_title(
|
|
355
|
-
f"{trainer.model_name_prefix} Loss Curve ({model_name})")
|
|
356
|
-
ax.grid(True, linestyle="--", alpha=0.3)
|
|
357
|
-
ax.legend()
|
|
358
|
-
plt.tight_layout()
|
|
359
|
-
plt.savefig(plot_dir, dpi=300)
|
|
360
|
-
plt.close(fig)
|
|
361
|
-
print(
|
|
362
|
-
f"[Plot] Saved loss curve for {model_name}/{trainer.label} -> {plot_dir}")
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
def train_from_config(args: argparse.Namespace) -> None:
|
|
366
|
-
script_dir = Path(__file__).resolve().parent
|
|
367
|
-
config_path = resolve_config_path(args.config_json, script_dir)
|
|
368
|
-
cfg = load_config_json(
|
|
369
|
-
config_path,
|
|
370
|
-
required_keys=["data_dir", "model_list", "model_categories", "target", "weight"],
|
|
371
|
-
)
|
|
372
|
-
cfg = normalize_config_paths(cfg, config_path)
|
|
373
|
-
|
|
374
|
-
set_env(cfg.get("env", {}))
|
|
375
|
-
plot_requested = bool(args.plot_curves or cfg.get("plot_curves", False))
|
|
376
|
-
|
|
377
|
-
def _safe_int_env(key: str, default: int) -> int:
|
|
378
|
-
try:
|
|
379
|
-
return int(os.environ.get(key, default))
|
|
380
|
-
except (TypeError, ValueError):
|
|
381
|
-
return default
|
|
382
|
-
|
|
383
|
-
dist_world_size = _safe_int_env("WORLD_SIZE", 1)
|
|
384
|
-
dist_rank = _safe_int_env("RANK", 0)
|
|
385
|
-
dist_active = dist_world_size > 1
|
|
386
|
-
def _ddp_barrier(reason: str) -> None:
|
|
387
|
-
if not dist_active:
|
|
388
|
-
return
|
|
389
|
-
torch_mod = getattr(ropt, "torch", None)
|
|
390
|
-
dist_mod = getattr(torch_mod, "distributed", None)
|
|
391
|
-
if dist_mod is None:
|
|
392
|
-
return
|
|
393
|
-
try:
|
|
394
|
-
if not getattr(dist_mod, "is_available", lambda: False)():
|
|
395
|
-
return
|
|
396
|
-
if not dist_mod.is_initialized():
|
|
397
|
-
ddp_ok, _, _, _ = ropt.DistributedUtils.setup_ddp()
|
|
398
|
-
if not ddp_ok or not dist_mod.is_initialized():
|
|
399
|
-
return
|
|
400
|
-
dist_mod.barrier()
|
|
401
|
-
except Exception as exc:
|
|
402
|
-
print(f"[DDP] barrier failed during {reason}: {exc}", flush=True)
|
|
403
|
-
raise
|
|
404
|
-
|
|
405
|
-
data_dir = Path(cfg["data_dir"])
|
|
406
|
-
data_dir.mkdir(parents=True, exist_ok=True)
|
|
407
|
-
|
|
408
|
-
prop_test = cfg.get("prop_test", 0.25)
|
|
409
|
-
rand_seed = cfg.get("rand_seed", 13)
|
|
410
|
-
epochs = cfg.get("epochs", 50)
|
|
411
|
-
output_dir = args.output_dir or cfg.get("output_dir")
|
|
412
|
-
if isinstance(output_dir, str) and output_dir.strip():
|
|
413
|
-
resolved = resolve_path(output_dir, config_path.parent)
|
|
414
|
-
if resolved is not None:
|
|
415
|
-
output_dir = str(resolved)
|
|
416
|
-
reuse_best_params = bool(args.reuse_best_params or cfg.get("reuse_best_params", False))
|
|
417
|
-
xgb_max_depth_max = int(cfg.get("xgb_max_depth_max", 25))
|
|
418
|
-
xgb_n_estimators_max = int(cfg.get("xgb_n_estimators_max", 500))
|
|
419
|
-
optuna_storage = cfg.get("optuna_storage")
|
|
420
|
-
optuna_study_prefix = cfg.get("optuna_study_prefix")
|
|
421
|
-
best_params_files = cfg.get("best_params_files")
|
|
422
|
-
|
|
423
|
-
model_names = build_model_names(
|
|
424
|
-
cfg["model_list"], cfg["model_categories"])
|
|
425
|
-
if not model_names:
|
|
426
|
-
raise ValueError(
|
|
427
|
-
"No model names generated from model_list/model_categories.")
|
|
428
|
-
|
|
429
|
-
results: Dict[str, ropt.BayesOptModel] = {}
|
|
430
|
-
trained_keys_by_model: Dict[str, List[str]] = {}
|
|
431
|
-
|
|
432
|
-
for model_name in model_names:
|
|
433
|
-
# Per-dataset training loop: load data, split train/test, and train requested models.
|
|
434
|
-
csv_path = data_dir / f"{model_name}.csv"
|
|
435
|
-
if not csv_path.exists():
|
|
436
|
-
raise FileNotFoundError(f"Missing dataset: {csv_path}")
|
|
437
|
-
|
|
438
|
-
print(f"\n=== Processing model {model_name} ===")
|
|
439
|
-
raw = pd.read_csv(csv_path, low_memory=False)
|
|
440
|
-
raw = raw.copy()
|
|
441
|
-
for col in raw.columns:
|
|
442
|
-
s = raw[col]
|
|
443
|
-
if pd.api.types.is_numeric_dtype(s):
|
|
444
|
-
raw[col] = pd.to_numeric(s, errors="coerce").fillna(0)
|
|
445
|
-
else:
|
|
446
|
-
raw[col] = s.astype("object").fillna("<NA>")
|
|
447
|
-
|
|
448
|
-
train_df, test_df = train_test_split(
|
|
449
|
-
raw, test_size=prop_test, random_state=rand_seed
|
|
450
|
-
)
|
|
451
|
-
|
|
452
|
-
use_resn_dp = args.use_resn_dp or cfg.get(
|
|
453
|
-
"use_resn_data_parallel", False)
|
|
454
|
-
use_ft_dp = args.use_ft_dp or cfg.get("use_ft_data_parallel", True)
|
|
455
|
-
use_resn_ddp = args.use_resn_ddp or cfg.get("use_resn_ddp", False)
|
|
456
|
-
use_ft_ddp = args.use_ft_ddp or cfg.get("use_ft_ddp", False)
|
|
457
|
-
use_gnn_dp = args.use_gnn_dp or cfg.get("use_gnn_data_parallel", False)
|
|
458
|
-
use_gnn_ddp = args.use_gnn_ddp or cfg.get("use_gnn_ddp", False)
|
|
459
|
-
gnn_use_ann = cfg.get("gnn_use_approx_knn", True)
|
|
460
|
-
if args.gnn_no_ann:
|
|
461
|
-
gnn_use_ann = False
|
|
462
|
-
gnn_threshold = args.gnn_ann_threshold if args.gnn_ann_threshold is not None else cfg.get(
|
|
463
|
-
"gnn_approx_knn_threshold", 50000)
|
|
464
|
-
gnn_graph_cache = args.gnn_graph_cache or cfg.get("gnn_graph_cache")
|
|
465
|
-
if isinstance(gnn_graph_cache, str) and gnn_graph_cache.strip():
|
|
466
|
-
resolved_cache = resolve_path(gnn_graph_cache, config_path.parent)
|
|
467
|
-
if resolved_cache is not None:
|
|
468
|
-
gnn_graph_cache = str(resolved_cache)
|
|
469
|
-
gnn_max_gpu_nodes = args.gnn_max_gpu_nodes if args.gnn_max_gpu_nodes is not None else cfg.get(
|
|
470
|
-
"gnn_max_gpu_knn_nodes", 200000)
|
|
471
|
-
gnn_gpu_mem_ratio = args.gnn_gpu_mem_ratio if args.gnn_gpu_mem_ratio is not None else cfg.get(
|
|
472
|
-
"gnn_knn_gpu_mem_ratio", 0.9)
|
|
473
|
-
gnn_gpu_mem_overhead = args.gnn_gpu_mem_overhead if args.gnn_gpu_mem_overhead is not None else cfg.get(
|
|
474
|
-
"gnn_knn_gpu_mem_overhead", 2.0)
|
|
475
|
-
|
|
476
|
-
binary_target = cfg.get("binary_target") or cfg.get("binary_resp_nme")
|
|
477
|
-
feature_list = cfg.get("feature_list")
|
|
478
|
-
categorical_features = cfg.get("categorical_features")
|
|
479
|
-
|
|
480
|
-
ft_role = args.ft_role or cfg.get("ft_role", "model")
|
|
481
|
-
if args.ft_as_feature and args.ft_role is None:
|
|
482
|
-
# Keep legacy behavior as a convenience alias only when the config
|
|
483
|
-
# didn't already request a non-default FT role.
|
|
484
|
-
if str(cfg.get("ft_role", "model")) == "model":
|
|
485
|
-
ft_role = "embedding"
|
|
486
|
-
ft_feature_prefix = str(cfg.get("ft_feature_prefix", args.ft_feature_prefix))
|
|
487
|
-
ft_num_numeric_tokens = cfg.get("ft_num_numeric_tokens")
|
|
488
|
-
|
|
489
|
-
model = ropt.BayesOptModel(
|
|
490
|
-
train_df,
|
|
491
|
-
test_df,
|
|
492
|
-
model_name,
|
|
493
|
-
cfg["target"],
|
|
494
|
-
cfg["weight"],
|
|
495
|
-
feature_list,
|
|
496
|
-
binary_resp_nme=binary_target,
|
|
497
|
-
cate_list=categorical_features,
|
|
498
|
-
prop_test=prop_test,
|
|
499
|
-
rand_seed=rand_seed,
|
|
500
|
-
epochs=epochs,
|
|
501
|
-
use_resn_data_parallel=use_resn_dp,
|
|
502
|
-
use_ft_data_parallel=use_ft_dp,
|
|
503
|
-
use_resn_ddp=use_resn_ddp,
|
|
504
|
-
use_ft_ddp=use_ft_ddp,
|
|
505
|
-
use_gnn_data_parallel=use_gnn_dp,
|
|
506
|
-
use_gnn_ddp=use_gnn_ddp,
|
|
507
|
-
output_dir=output_dir,
|
|
508
|
-
xgb_max_depth_max=xgb_max_depth_max,
|
|
509
|
-
xgb_n_estimators_max=xgb_n_estimators_max,
|
|
510
|
-
resn_weight_decay=cfg.get("resn_weight_decay"),
|
|
511
|
-
final_ensemble=bool(cfg.get("final_ensemble", False)),
|
|
512
|
-
final_ensemble_k=int(cfg.get("final_ensemble_k", 3)),
|
|
513
|
-
final_refit=bool(cfg.get("final_refit", True)),
|
|
514
|
-
optuna_storage=optuna_storage,
|
|
515
|
-
optuna_study_prefix=optuna_study_prefix,
|
|
516
|
-
best_params_files=best_params_files,
|
|
517
|
-
gnn_use_approx_knn=gnn_use_ann,
|
|
518
|
-
gnn_approx_knn_threshold=gnn_threshold,
|
|
519
|
-
gnn_graph_cache=gnn_graph_cache,
|
|
520
|
-
gnn_max_gpu_knn_nodes=gnn_max_gpu_nodes,
|
|
521
|
-
gnn_knn_gpu_mem_ratio=gnn_gpu_mem_ratio,
|
|
522
|
-
gnn_knn_gpu_mem_overhead=gnn_gpu_mem_overhead,
|
|
523
|
-
ft_role=ft_role,
|
|
524
|
-
ft_feature_prefix=ft_feature_prefix,
|
|
525
|
-
ft_num_numeric_tokens=ft_num_numeric_tokens,
|
|
526
|
-
infer_categorical_max_unique=int(cfg.get("infer_categorical_max_unique", 50)),
|
|
527
|
-
infer_categorical_max_ratio=float(cfg.get("infer_categorical_max_ratio", 0.05)),
|
|
528
|
-
reuse_best_params=reuse_best_params,
|
|
529
|
-
)
|
|
530
|
-
|
|
531
|
-
if plot_requested:
|
|
532
|
-
plot_cfg = cfg.get("plot", {})
|
|
533
|
-
legacy_lift_flags = {
|
|
534
|
-
"glm": cfg.get("plot_lift_glm", False),
|
|
535
|
-
"xgb": cfg.get("plot_lift_xgb", False),
|
|
536
|
-
"resn": cfg.get("plot_lift_resn", False),
|
|
537
|
-
"ft": cfg.get("plot_lift_ft", False),
|
|
538
|
-
}
|
|
539
|
-
plot_enabled = plot_cfg.get("enable", any(legacy_lift_flags.values()))
|
|
540
|
-
if plot_enabled and plot_cfg.get("pre_oneway", False) and plot_cfg.get("oneway", True):
|
|
541
|
-
n_bins = int(plot_cfg.get("n_bins", 10))
|
|
542
|
-
model.plot_oneway(n_bins=n_bins, plot_subdir="oneway/pre")
|
|
543
|
-
|
|
544
|
-
if "all" in args.model_keys:
|
|
545
|
-
requested_keys = ["glm", "xgb", "resn", "ft", "gnn"]
|
|
546
|
-
else:
|
|
547
|
-
requested_keys = args.model_keys
|
|
548
|
-
requested_keys = dedupe_preserve_order(requested_keys)
|
|
549
|
-
|
|
550
|
-
if ft_role != "model":
|
|
551
|
-
requested_keys = [k for k in requested_keys if k != "ft"]
|
|
552
|
-
if not requested_keys:
|
|
553
|
-
stack_keys = args.stack_model_keys or cfg.get("stack_model_keys")
|
|
554
|
-
if stack_keys:
|
|
555
|
-
if "all" in stack_keys:
|
|
556
|
-
requested_keys = ["glm", "xgb", "resn", "gnn"]
|
|
557
|
-
else:
|
|
558
|
-
requested_keys = [k for k in stack_keys if k != "ft"]
|
|
559
|
-
requested_keys = dedupe_preserve_order(requested_keys)
|
|
560
|
-
if dist_active:
|
|
561
|
-
ft_trainer = model.trainers.get("ft")
|
|
562
|
-
if ft_trainer is None:
|
|
563
|
-
raise ValueError("FT trainer is not available.")
|
|
564
|
-
ft_trainer_uses_ddp = bool(
|
|
565
|
-
getattr(ft_trainer, "enable_distributed_optuna", False))
|
|
566
|
-
if not ft_trainer_uses_ddp:
|
|
567
|
-
raise ValueError(
|
|
568
|
-
"FT embedding under torchrun requires enabling FT DDP (use --use-ft-ddp or set use_ft_ddp=true)."
|
|
569
|
-
)
|
|
570
|
-
missing = [key for key in requested_keys if key not in model.trainers]
|
|
571
|
-
if missing:
|
|
572
|
-
raise ValueError(
|
|
573
|
-
f"Trainer(s) {missing} not available for {model_name}")
|
|
574
|
-
|
|
575
|
-
executed_keys: List[str] = []
|
|
576
|
-
if ft_role != "model":
|
|
577
|
-
print(
|
|
578
|
-
f"Optimizing ft as {ft_role} for {model_name} (max_evals={args.max_evals})")
|
|
579
|
-
model.optimize_model("ft", max_evals=args.max_evals)
|
|
580
|
-
model.trainers["ft"].save()
|
|
581
|
-
if getattr(ropt, "torch", None) is not None and ropt.torch.cuda.is_available():
|
|
582
|
-
ropt.free_cuda()
|
|
583
|
-
for key in requested_keys:
|
|
584
|
-
trainer = model.trainers[key]
|
|
585
|
-
trainer_uses_ddp = bool(
|
|
586
|
-
getattr(trainer, "enable_distributed_optuna", False))
|
|
587
|
-
if dist_active and not trainer_uses_ddp:
|
|
588
|
-
if dist_rank != 0:
|
|
589
|
-
print(
|
|
590
|
-
f"[Rank {dist_rank}] Skip {model_name}/{key} because trainer is not DDP-enabled."
|
|
591
|
-
)
|
|
592
|
-
_ddp_barrier(f"start_non_ddp_{model_name}_{key}")
|
|
593
|
-
if dist_rank != 0:
|
|
594
|
-
_ddp_barrier(f"finish_non_ddp_{model_name}_{key}")
|
|
595
|
-
continue
|
|
596
|
-
|
|
597
|
-
print(
|
|
598
|
-
f"Optimizing {key} for {model_name} (max_evals={args.max_evals})")
|
|
599
|
-
model.optimize_model(key, max_evals=args.max_evals)
|
|
600
|
-
model.trainers[key].save()
|
|
601
|
-
_plot_loss_curve_for_trainer(model_name, model.trainers[key])
|
|
602
|
-
if key in PYTORCH_TRAINERS:
|
|
603
|
-
ropt.free_cuda()
|
|
604
|
-
if dist_active and not trainer_uses_ddp:
|
|
605
|
-
_ddp_barrier(f"finish_non_ddp_{model_name}_{key}")
|
|
606
|
-
executed_keys.append(key)
|
|
607
|
-
|
|
608
|
-
if not executed_keys:
|
|
609
|
-
continue
|
|
610
|
-
|
|
611
|
-
results[model_name] = model
|
|
612
|
-
trained_keys_by_model[model_name] = executed_keys
|
|
613
|
-
|
|
614
|
-
if not plot_requested:
|
|
615
|
-
return
|
|
616
|
-
|
|
617
|
-
for name, model in results.items():
|
|
618
|
-
_plot_curves_for_model(
|
|
619
|
-
model,
|
|
620
|
-
trained_keys_by_model.get(name, []),
|
|
621
|
-
cfg,
|
|
622
|
-
)
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
def main() -> None:
|
|
626
|
-
if configure_run_logging:
|
|
627
|
-
configure_run_logging(prefix="bayesopt_entry")
|
|
628
|
-
args = _parse_args()
|
|
629
|
-
train_from_config(args)
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
if __name__ == "__main__":
|
|
633
|
-
main()
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import argparse
|
|
4
|
-
from pathlib import Path
|
|
5
|
-
from typing import Optional
|
|
6
|
-
|
|
7
|
-
try:
|
|
8
|
-
from .notebook_utils import run_from_config # type: ignore
|
|
9
|
-
except Exception: # pragma: no cover
|
|
10
|
-
from notebook_utils import run_from_config # type: ignore
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def run(config_json: str | Path) -> None:
|
|
14
|
-
"""Run explain by config.json (runner.mode=explain)."""
|
|
15
|
-
run_from_config(config_json)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
def _build_parser() -> argparse.ArgumentParser:
|
|
19
|
-
parser = argparse.ArgumentParser(
|
|
20
|
-
description="Explain_Run: run explain by config.json (runner.mode=explain)."
|
|
21
|
-
)
|
|
22
|
-
parser.add_argument(
|
|
23
|
-
"--config-json",
|
|
24
|
-
required=True,
|
|
25
|
-
help="Path to config.json (relative paths are resolved from ins_pricing/modelling/ when possible).",
|
|
26
|
-
)
|
|
27
|
-
return parser
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
def main(argv: Optional[list[str]] = None) -> None:
|
|
31
|
-
args = _build_parser().parse_args(argv)
|
|
32
|
-
run(args.config_json)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if __name__ == "__main__":
|
|
36
|
-
main()
|