ltr 0.1.0
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.
- checksums.yaml +7 -0
- data/ARCHITECTURE.md +92 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE +21 -0
- data/README.md +186 -0
- data/docs/AGENTS.md +67 -0
- data/docs/aggregation-engines.md +80 -0
- data/docs/config.md +516 -0
- data/docs/data-contracts.md +94 -0
- data/docs/data-splits.md +34 -0
- data/docs/dataset-builder-steps.md +13 -0
- data/docs/dataset-csv.md +73 -0
- data/docs/dataset-enrichers.md +62 -0
- data/docs/errors.md +33 -0
- data/docs/feature-selection.md +108 -0
- data/docs/features.md +202 -0
- data/docs/glossary.md +56 -0
- data/docs/host-integration.md +128 -0
- data/docs/host-run-pipelines.md +48 -0
- data/docs/index.md +50 -0
- data/docs/inference.md +164 -0
- data/docs/label-transformers.md +242 -0
- data/docs/labels-from-behavior.md +94 -0
- data/docs/learning-to-rank.md +119 -0
- data/docs/metrics.md +166 -0
- data/docs/model-files.md +73 -0
- data/docs/optimization.md +72 -0
- data/docs/public-api.md +67 -0
- data/docs/rake-tasks.md +82 -0
- data/docs/relevance-grades.md +80 -0
- data/docs/run-locally.md +584 -0
- data/docs/run-on-aws.md +647 -0
- data/docs/training-objective.md +110 -0
- data/docs/ubi-documents.md +75 -0
- data/docs/ubi-logging.md +125 -0
- data/lib/ltr/README.md +28 -0
- data/lib/ltr/configuration/README.md +12 -0
- data/lib/ltr/configuration/config_loader.rb +326 -0
- data/lib/ltr/configuration/config_validator.rb +476 -0
- data/lib/ltr/configuration/configuration.rb +84 -0
- data/lib/ltr/configuration/example_config.yml +182 -0
- data/lib/ltr/data/README.md +20 -0
- data/lib/ltr/data/csv/README.md +9 -0
- data/lib/ltr/data/csv/typed_columns.rb +101 -0
- data/lib/ltr/data/csv.rb +126 -0
- data/lib/ltr/data/dataframe.rb +80 -0
- data/lib/ltr/data/dataset_loader.rb +29 -0
- data/lib/ltr/data/row_store.rb +188 -0
- data/lib/ltr/data/split_cache.rb +30 -0
- data/lib/ltr/data/splits.rb +86 -0
- data/lib/ltr/dataset_aggregation/README.md +32 -0
- data/lib/ltr/dataset_aggregation/aggregated_sqlite_loader.rb +64 -0
- data/lib/ltr/dataset_aggregation/cascade_actions.rb +58 -0
- data/lib/ltr/dataset_aggregation/column_name.rb +47 -0
- data/lib/ltr/dataset_aggregation/dataset_builder/README.md +15 -0
- data/lib/ltr/dataset_aggregation/dataset_builder/context.rb +159 -0
- data/lib/ltr/dataset_aggregation/dataset_builder/steps/README.md +16 -0
- data/lib/ltr/dataset_aggregation/dataset_builder/steps/behavioural_feature_builder.rb +77 -0
- data/lib/ltr/dataset_aggregation/dataset_builder/steps/dataset_filters.rb +29 -0
- data/lib/ltr/dataset_aggregation/dataset_builder/steps/label_creator.rb +125 -0
- data/lib/ltr/dataset_aggregation/dataset_builder/steps/negative_downsampler.rb +52 -0
- data/lib/ltr/dataset_aggregation/dataset_builder/steps/query_aggregator.rb +163 -0
- data/lib/ltr/dataset_aggregation/dataset_builder/steps/split_assigner.rb +76 -0
- data/lib/ltr/dataset_aggregation/dataset_builder/steps/ubi_joiner.rb +137 -0
- data/lib/ltr/dataset_aggregation/dataset_builder/steps/user_query_aggregator.rb +68 -0
- data/lib/ltr/dataset_aggregation/dataset_builder.rb +82 -0
- data/lib/ltr/dataset_aggregation/dataset_enricher.rb +168 -0
- data/lib/ltr/dataset_aggregation/dataset_finalizer.rb +116 -0
- data/lib/ltr/dataset_aggregation/dataset_pipeline.rb +113 -0
- data/lib/ltr/dataset_aggregation/dataset_saver.rb +79 -0
- data/lib/ltr/dataset_aggregation/derived_features.rb +45 -0
- data/lib/ltr/dataset_aggregation/display_positions.rb +25 -0
- data/lib/ltr/dataset_aggregation/dropped_events.rb +29 -0
- data/lib/ltr/dataset_aggregation/labels/README.md +18 -0
- data/lib/ltr/dataset_aggregation/labels/identity_label_transformer.rb +11 -0
- data/lib/ltr/dataset_aggregation/labels/label_transformer.rb +61 -0
- data/lib/ltr/dataset_aggregation/labels/log_plus_one_label_transformer.rb +18 -0
- data/lib/ltr/dataset_aggregation/labels/per_query_k_means_label_transformer.rb +141 -0
- data/lib/ltr/dataset_aggregation/labels/per_query_label_transformer.rb +52 -0
- data/lib/ltr/dataset_aggregation/labels/per_query_min_max_label_transformer.rb +27 -0
- data/lib/ltr/dataset_aggregation/labels/quantile_label_transformer.rb +73 -0
- data/lib/ltr/dataset_aggregation/labels/sqrt_label_transformer.rb +18 -0
- data/lib/ltr/dataset_aggregation/missing_hit_ids.rb +20 -0
- data/lib/ltr/dataset_aggregation/negative_rank.rb +15 -0
- data/lib/ltr/dataset_aggregation/sql/aggregate.sqlite.sql.erb +209 -0
- data/lib/ltr/dataset_aggregation/sqlite_aggregator.rb +167 -0
- data/lib/ltr/dataset_aggregation/sqlite_event_stager.rb +79 -0
- data/lib/ltr/dataset_aggregation/sqlite_query_stager.rb +148 -0
- data/lib/ltr/dataset_aggregation/sqlite_staging.rb +180 -0
- data/lib/ltr/dataset_aggregation/sqlite_ubi_data_loader.rb +91 -0
- data/lib/ltr/dataset_aggregation/ubi_data_loader.rb +138 -0
- data/lib/ltr/dataset_aggregation/ubi_data_loader_base.rb +147 -0
- data/lib/ltr/dataset_aggregation/ubi_query_fields.rb +54 -0
- data/lib/ltr/deploy/README.md +21 -0
- data/lib/ltr/deploy/artifacts/code/inference.py +371 -0
- data/lib/ltr/deploy/artifacts/code/requirements.txt +1 -0
- data/lib/ltr/deploy/feature_extractor.rb +94 -0
- data/lib/ltr/deploy/feature_store_deployer.rb +88 -0
- data/lib/ltr/deploy/local_deployer.rb +61 -0
- data/lib/ltr/deploy/model_packager.rb +90 -0
- data/lib/ltr/deploy/sagemaker_deployer.rb +85 -0
- data/lib/ltr/deploy/sagemaker_endpoint.rb +199 -0
- data/lib/ltr/deploy/ssm_feature_source.rb +35 -0
- data/lib/ltr/errors/README.md +16 -0
- data/lib/ltr/errors/configuration_error.rb +5 -0
- data/lib/ltr/errors/deploy_error.rb +5 -0
- data/lib/ltr/errors/error.rb +5 -0
- data/lib/ltr/errors/invalid_dataset_error.rb +5 -0
- data/lib/ltr/errors/missing_dependency_error.rb +5 -0
- data/lib/ltr/errors/sqlite_error.rb +14 -0
- data/lib/ltr/features/README.md +19 -0
- data/lib/ltr/features/encoders/README.md +10 -0
- data/lib/ltr/features/encoders/enriched_column_encoder.rb +77 -0
- data/lib/ltr/features/encoders/feature_encoder.rb +61 -0
- data/lib/ltr/features/feature_engineering.rb +194 -0
- data/lib/ltr/features/feature_transformer.rb +92 -0
- data/lib/ltr/features/float32_range.rb +20 -0
- data/lib/ltr/features/transform_steps.rb +34 -0
- data/lib/ltr/features/transform_store.rb +123 -0
- data/lib/ltr/inference/README.md +11 -0
- data/lib/ltr/inference/base.rb +52 -0
- data/lib/ltr/inference/local_reranker.rb +147 -0
- data/lib/ltr/inference/sagemaker_client.rb +62 -0
- data/lib/ltr/metrics/README.md +12 -0
- data/lib/ltr/metrics/metric_scores.rb +67 -0
- data/lib/ltr/metrics/metrics.rb +139 -0
- data/lib/ltr/optimize/README.md +25 -0
- data/lib/ltr/optimize/bayesian_optimizer.rb +212 -0
- data/lib/ltr/optimize/candidate_selector.rb +97 -0
- data/lib/ltr/optimize/cholesky_decomposition.rb +81 -0
- data/lib/ltr/optimize/expected_improvement.rb +33 -0
- data/lib/ltr/optimize/feature_selection/README.md +15 -0
- data/lib/ltr/optimize/feature_selection/greedy_backward.rb +38 -0
- data/lib/ltr/optimize/feature_selection/importance_ranked.rb +23 -0
- data/lib/ltr/optimize/feature_selection/rfe.rb +22 -0
- data/lib/ltr/optimize/feature_selection/strategy.rb +112 -0
- data/lib/ltr/optimize/feature_selection/trial.rb +39 -0
- data/lib/ltr/optimize/gaussian_process.rb +125 -0
- data/lib/ltr/optimize/optimization_dataset.rb +16 -0
- data/lib/ltr/optimize/optimize_features.rb +129 -0
- data/lib/ltr/optimize/optimize_hyperparameters.rb +165 -0
- data/lib/ltr/optimize/optimize_pipeline.rb +105 -0
- data/lib/ltr/optimize/search_space.rb +165 -0
- data/lib/ltr/optimize/update_config.rb +196 -0
- data/lib/ltr/ssm/README.md +11 -0
- data/lib/ltr/ssm/loader.rb +61 -0
- data/lib/ltr/ssm/parameter_paths.rb +19 -0
- data/lib/ltr/ssm/saver.rb +76 -0
- data/lib/ltr/support/README.md +13 -0
- data/lib/ltr/support/callable.rb +10 -0
- data/lib/ltr/support/optional_dependency.rb +31 -0
- data/lib/ltr/support/pipeline_result.rb +22 -0
- data/lib/ltr/support/pipeline_task.rb +81 -0
- data/lib/ltr/support/version.rb +7 -0
- data/lib/ltr/tasks/ltr.rake +24 -0
- data/lib/ltr/train/README.md +19 -0
- data/lib/ltr/train/deploy_gate.rb +77 -0
- data/lib/ltr/train/deploy_metric.rb +63 -0
- data/lib/ltr/train/eval_logger.rb +16 -0
- data/lib/ltr/train/evaluation_cache.rb +101 -0
- data/lib/ltr/train/modelling_pipeline.rb +82 -0
- data/lib/ltr/train/optimization_split_report.rb +54 -0
- data/lib/ltr/train/utils.rb +35 -0
- data/lib/ltr/train/xgb_evaluator.rb +96 -0
- data/lib/ltr/train/xgb_trainer.rb +154 -0
- data/lib/ltr/ubi.rb +86 -0
- data/lib/ltr.rb +61 -0
- data/scripts/seed_ubi.rb +75 -0
- metadata +346 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Loads the YAML config and merges an override file on top.
|
|
4
|
+
# Reads, validates and saves the config values.
|
|
5
|
+
|
|
6
|
+
class Ltr::ConfigLoader
|
|
7
|
+
DEFAULT_SEED = 1967
|
|
8
|
+
DEFAULT_QUERY_ID_FIELD = "query_id"
|
|
9
|
+
DEFAULT_DOC_ID_FIELD = "doc_id"
|
|
10
|
+
DEFAULT_HIT_IDS_FIELD = "query_response_hit_ids"
|
|
11
|
+
|
|
12
|
+
# The merged config hash, with string keys.
|
|
13
|
+
attr_reader :config
|
|
14
|
+
# The base config file path as a `Pathname`. A relative path was resolved against `Ltr.config.root`.
|
|
15
|
+
attr_reader :base_config_path
|
|
16
|
+
# The override config file path as a `Pathname`, or nil when there is none.
|
|
17
|
+
attr_reader :override_config_path
|
|
18
|
+
|
|
19
|
+
# Loads the config at once with `reload!`. Raises `Ltr::ConfigurationError` when a config file is missing or cannot be loaded.
|
|
20
|
+
def initialize(base_config_path:, override_config_path: nil, validate: true)
|
|
21
|
+
@base_config_path = resolve_config_path(base_config_path)
|
|
22
|
+
@override_config_path = resolve_config_path(override_config_path) if override_config_path
|
|
23
|
+
@validate = validate
|
|
24
|
+
|
|
25
|
+
reload!
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Returns the file that `save!` writes to. This is the override file when there is one, else the base file.
|
|
29
|
+
def writable_config_path
|
|
30
|
+
override_config_path || base_config_path
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Reads `name`. Returns nil when it is not set.
|
|
34
|
+
def name
|
|
35
|
+
config.dig("name")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Reads the top-level `seed`.
|
|
39
|
+
# It seeds the split shuffle, negative downsampling, the hyperparameter search and XGBoost.
|
|
40
|
+
# Defaults to 1967, also when `seed` is nil.
|
|
41
|
+
def seed
|
|
42
|
+
config["seed"] || DEFAULT_SEED
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Reads `model.name`. Returns nil when it is not set.
|
|
46
|
+
def model_name
|
|
47
|
+
config.dig("model", "name")
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Reads `model.hyperparameters`. Returns an empty hash when the key is missing or blank.
|
|
51
|
+
def hyperparameters
|
|
52
|
+
config.dig("model", "hyperparameters") || {}
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Replaces `model.hyperparameters` in memory, with the keys converted to strings. The `model` section must already exist. Call `save!` to write it to disk.
|
|
56
|
+
def hyperparameters=(new_hyperparameters)
|
|
57
|
+
change(%w[model hyperparameters], new_hyperparameters&.transform_keys(&:to_s))
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# True only when `deploy.local` is exactly true.
|
|
61
|
+
def local_deploy_enabled?
|
|
62
|
+
deploy["local"] == true
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Returns `deploy.local_path` as an absolute path string. A relative path resolves against `Ltr.config.root`. Returns nil when it is blank.
|
|
66
|
+
def local_deploy_path
|
|
67
|
+
path = deploy["local_path"]
|
|
68
|
+
return nil if path.blank?
|
|
69
|
+
|
|
70
|
+
resolve_config_path(path).to_s
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Returns every feature name the model can use.
|
|
74
|
+
# When `optimized_features` is set, that list is returned as is.
|
|
75
|
+
# Otherwise this joins the search time features, the derived features and the feature store features, without repeats.
|
|
76
|
+
def all_features
|
|
77
|
+
optimized_features || (search_time_features + derived_feature_names + feature_store_features).uniq
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Reads `optimized_features`, the exact list that `ltr:optimize` chose. Returns nil when the key is missing or the list is empty.
|
|
81
|
+
def optimized_features
|
|
82
|
+
config["optimized_features"].presence
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Replaces `optimized_features` in memory. Call `save!` to write it to disk.
|
|
86
|
+
def optimized_features=(new_features)
|
|
87
|
+
change(%w[optimized_features], new_features)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Reads `feature_engineering`. Returns an empty array when the key is missing or blank.
|
|
91
|
+
def feature_engineering
|
|
92
|
+
config["feature_engineering"] || []
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Reads `search_time_features`. Returns an empty array when the key is missing or blank.
|
|
96
|
+
def search_time_features
|
|
97
|
+
config["search_time_features"] || []
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Replaces `search_time_features` in memory. Call `save!` to write it to disk.
|
|
101
|
+
def search_time_features=(new_features)
|
|
102
|
+
change(%w[search_time_features], new_features)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Reads `feature_store.features`. Returns an empty array when the key is missing or blank.
|
|
106
|
+
def feature_store_features
|
|
107
|
+
config.dig("feature_store", "features") || []
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Replaces `feature_store.features` in memory. The `feature_store` section must already exist. Call `save!` to write it to disk.
|
|
111
|
+
def feature_store_features=(new_features)
|
|
112
|
+
change(%w[feature_store features], new_features)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Reads the `evaluation` section. Returns an empty hash when the key is missing or blank.
|
|
116
|
+
def evaluation
|
|
117
|
+
config["evaluation"] || {}
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Reads `training.early_stopping_split`. Defaults to the test split.
|
|
121
|
+
def early_stopping_split
|
|
122
|
+
training["early_stopping_split"] || Ltr::Data::Splits::TEST
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Reads `evaluation.split`. Defaults to the test split.
|
|
126
|
+
def evaluation_split
|
|
127
|
+
evaluation["split"] || Ltr::Data::Splits::TEST
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Reads `optimization.split`. Defaults to the validation split.
|
|
131
|
+
def optimization_split
|
|
132
|
+
optimization["split"] || Ltr::Data::Splits::VAL
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Reads `training.modelling_dataset_path`. A relative local path resolves against `Ltr.config.root`, which is absolute once set, and comes back as an absolute path string.
|
|
136
|
+
# An `s3://` URL and an absolute path stay as they are. Returns nil when it is not set.
|
|
137
|
+
def modelling_dataset_path
|
|
138
|
+
path = config.dig("training", "modelling_dataset_path")
|
|
139
|
+
return path if path.blank? || path.start_with?("s3://")
|
|
140
|
+
|
|
141
|
+
resolve_config_path(path).to_s
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Reads the `optimization` section. Returns an empty hash when the key is missing or blank.
|
|
145
|
+
def optimization
|
|
146
|
+
config["optimization"] || {}
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Reads `training.s3_region`. Returns nil when it is not set.
|
|
150
|
+
def s3_region
|
|
151
|
+
config.dig("training", "s3_region")
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Reads the `deploy` section. Returns an empty hash when the key is missing or blank.
|
|
155
|
+
def deploy
|
|
156
|
+
config["deploy"] || {}
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Reads `optimization.metric_to_optimize` in lowercase. Returns nil when it is not set.
|
|
160
|
+
def metric_to_optimize
|
|
161
|
+
metric = config.dig("optimization", "metric_to_optimize")
|
|
162
|
+
metric && Ltr::Metrics::MetricScores.normalize_name(metric)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Reads the `feature_store` section. Returns an empty hash when the key is missing or blank.
|
|
166
|
+
def feature_store
|
|
167
|
+
config["feature_store"] || {}
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Reads `ubi_data_loader.custom_filters`. Returns nil when it is not set.
|
|
171
|
+
def custom_filters
|
|
172
|
+
config.dig("ubi_data_loader", "custom_filters")
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# Reads the `ubi_data_loader` section. Returns an empty hash when the key is missing or blank.
|
|
176
|
+
def ubi_data_loader
|
|
177
|
+
config["ubi_data_loader"] || {}
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Reads the `dataset_builder` section. Returns an empty hash when the key is missing or blank.
|
|
181
|
+
def dataset_builder
|
|
182
|
+
config["dataset_builder"] || {}
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# True only when `dataset_builder.debug_mode` is exactly true.
|
|
186
|
+
def debug_mode?
|
|
187
|
+
dataset_builder.dig("debug_mode") == true
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# Reads `dataset_builder.query_id_field`. Defaults to `query_id`.
|
|
191
|
+
def query_id_field
|
|
192
|
+
dataset_builder["query_id_field"] || DEFAULT_QUERY_ID_FIELD
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# Reads `dataset_builder.doc_id_field`. Defaults to `doc_id`.
|
|
196
|
+
def doc_id_field
|
|
197
|
+
dataset_builder["doc_id_field"] || DEFAULT_DOC_ID_FIELD
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Reads `dataset_builder.hit_ids_field`. Defaults to `query_response_hit_ids`.
|
|
201
|
+
def hit_ids_field
|
|
202
|
+
dataset_builder["hit_ids_field"] || DEFAULT_HIT_IDS_FIELD
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Reads the `dataset_aggregation` section. Returns an empty hash when the key is missing or blank.
|
|
206
|
+
def dataset_aggregation
|
|
207
|
+
config["dataset_aggregation"] || {}
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# Reads `dataset_enricher.class`, the enricher class name. Returns nil when it is not set.
|
|
211
|
+
def dataset_enricher
|
|
212
|
+
config.dig("dataset_enricher", "class")
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# Reads `label_transformer.class`, the transformer class name. Returns nil when it is not set.
|
|
216
|
+
def label_transformer
|
|
217
|
+
config.dig("label_transformer", "class")
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# Reads the whole `label_transformer` section. Returns an empty hash when the key is missing or blank.
|
|
221
|
+
def label_transformer_settings
|
|
222
|
+
config["label_transformer"] || {}
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# Reads the `feature_transformations` section. Returns an empty hash when the key is missing or blank.
|
|
226
|
+
def feature_transformations
|
|
227
|
+
config["feature_transformations"] || {}
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# Reads the `training` section. Returns an empty hash when the key is missing or blank.
|
|
231
|
+
def training
|
|
232
|
+
config["training"] || {}
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# Reads `training.nthread`. Returns nil when it is not set.
|
|
236
|
+
def nthread
|
|
237
|
+
config.dig("training", "nthread")
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# Reads the `ssm` section. Returns an empty hash when the key is missing or blank.
|
|
241
|
+
def ssm
|
|
242
|
+
config["ssm"] || {}
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# True only when `ssm.enabled` is exactly true.
|
|
246
|
+
def ssm_enabled?
|
|
247
|
+
ssm&.dig("enabled") == true
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
# Reads the `inference` section. Returns an empty hash when the key is missing or blank.
|
|
251
|
+
def inference
|
|
252
|
+
config["inference"] || {}
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# Reads the base config file and merges the override file on top. Hashes merge key by key. A nil override value keeps the base value. Any other override value replaces it.
|
|
256
|
+
# Then it checks the feature engineering methods, and the config values unless validation is off. It logs a warning when SSM is off and `optimized_features` no longer matches `search_time_features` plus `feature_store.features`. Returns the merged config hash.
|
|
257
|
+
# Raises `Ltr::ConfigurationError` when a file is missing or cannot be parsed, or a value is invalid. Raises ArgumentError for an unknown feature engineering method.
|
|
258
|
+
# When it raises, the loader keeps its old config and its pending changes.
|
|
259
|
+
def reload!
|
|
260
|
+
base = load_yaml_file(@base_config_path)
|
|
261
|
+
config = @override_config_path ? deep_merge(base, load_yaml_file(@override_config_path)) : base
|
|
262
|
+
Ltr::Features::FeatureEngineering.validate_methods!(config["feature_engineering"])
|
|
263
|
+
Ltr::ConfigValidator.new(config).validate! if @validate
|
|
264
|
+
@changed_paths = []
|
|
265
|
+
@config = config
|
|
266
|
+
warn_when_optimized_features_stale
|
|
267
|
+
@config
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
# Writes the keys set by the writers since the last `save!` into the text of `writable_config_path`, with `Ltr::Optimize::UpdateConfig`.
|
|
271
|
+
# Other keys, ERB tags and comments outside the changed sections stay as they are. The file is not parsed. It does nothing when no key changed.
|
|
272
|
+
def save!
|
|
273
|
+
return if @changed_paths.empty?
|
|
274
|
+
|
|
275
|
+
Ltr::Optimize::UpdateConfig.call(self, paths: @changed_paths)
|
|
276
|
+
@changed_paths = []
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
private
|
|
280
|
+
|
|
281
|
+
def change(keys, value)
|
|
282
|
+
parent = keys[0...-1].reduce(config) { |hash, key| hash[key] }
|
|
283
|
+
parent[keys.last] = value
|
|
284
|
+
@changed_paths |= [keys]
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
def resolve_config_path(path)
|
|
288
|
+
pathname = Pathname.new(path)
|
|
289
|
+
return pathname if pathname.absolute?
|
|
290
|
+
|
|
291
|
+
Ltr.config.root.join(path)
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
def load_yaml_file(path)
|
|
295
|
+
# ERB runs before YAML parsing, so config files can use <%= %> tags.
|
|
296
|
+
content = ERB.new(File.read(path)).result
|
|
297
|
+
data = YAML.safe_load(content, aliases: true, permitted_classes: [Date, Time]) || {}
|
|
298
|
+
raise Ltr::ConfigurationError, "the top level must be a mapping" unless data.is_a?(Hash)
|
|
299
|
+
|
|
300
|
+
data
|
|
301
|
+
rescue SyntaxError, StandardError => e
|
|
302
|
+
raise Ltr::ConfigurationError, "Failed to load configuration from #{path}: #{e.message}"
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
def deep_merge(base, override)
|
|
306
|
+
base.merge(override) do |_key, base_val, override_val|
|
|
307
|
+
next base_val if override_val.nil?
|
|
308
|
+
|
|
309
|
+
(base_val.is_a?(Hash) && override_val.is_a?(Hash)) ? deep_merge(base_val, override_val) : override_val
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
def warn_when_optimized_features_stale
|
|
314
|
+
return if optimized_features.nil? || ssm_enabled?
|
|
315
|
+
return if optimized_features.sort == (search_time_features + feature_store_features).uniq.sort
|
|
316
|
+
|
|
317
|
+
Ltr.config.logger.warn(
|
|
318
|
+
"`optimized_features` does not match `search_time_features` plus `feature_store.features`. " \
|
|
319
|
+
"Training uses `optimized_features`. Re-run `ltr:optimize`."
|
|
320
|
+
)
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
def derived_feature_names
|
|
324
|
+
Ltr::Features::FeatureEngineering.derived_feature_names(search_time_features, feature_engineering)
|
|
325
|
+
end
|
|
326
|
+
end
|