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,476 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Checks that each section of the loaded config has valid values.
|
|
4
|
+
# `validate!` runs when the config loads. `validate_aggregation!` runs when the UBI loaders start, because only aggregation needs those rules.
|
|
5
|
+
|
|
6
|
+
class Ltr::ConfigValidator
|
|
7
|
+
AGGREGATION_ENGINES = %w[ruby sqlite].freeze
|
|
8
|
+
|
|
9
|
+
# The keys the gem reads, by section. A nil value is a leaf or a hash the host fills with its own keys.
|
|
10
|
+
KNOWN_KEYS = {
|
|
11
|
+
"name" => nil,
|
|
12
|
+
"seed" => nil,
|
|
13
|
+
"model" => { "type" => nil, "name" => nil, "hyperparameters" => nil },
|
|
14
|
+
"training" => { "nthread" => nil, "early_stopping_split" => nil, "modelling_dataset_path" => nil, "s3_region" => nil },
|
|
15
|
+
"search_time_features" => nil,
|
|
16
|
+
"optimized_features" => nil,
|
|
17
|
+
"feature_engineering" => nil,
|
|
18
|
+
"feature_store" => { "features" => nil, "include_in_deployment" => nil },
|
|
19
|
+
"feature_transformations" => nil,
|
|
20
|
+
"evaluation" => { "metrics" => nil, "split" => nil, "min_relevance_grade" => nil, "min_relevance_percentile" => nil },
|
|
21
|
+
"optimization" => {
|
|
22
|
+
"split" => nil, "metric_to_optimize" => nil, "complexity_penalty" => nil, "min_features" => nil,
|
|
23
|
+
"feature_selection" => { "strategy" => nil }, "search_time_features" => nil, "feature_store_features" => nil,
|
|
24
|
+
"iterations" => nil, "n_init" => nil, "hyperparameter_search_space" => nil,
|
|
25
|
+
"bayesian_optimizer" => Ltr::Optimize::BayesianOptimizer::DEFAULT_CONFIG.keys.to_h { |key| [key.to_s, nil] }
|
|
26
|
+
},
|
|
27
|
+
"ubi_data_loader" => {
|
|
28
|
+
"n_events" => nil, "max_days_ago" => nil, "batch_size" => nil,
|
|
29
|
+
"custom_filters" => nil,
|
|
30
|
+
"opensearch" => { "url" => nil, "username" => nil, "password" => nil },
|
|
31
|
+
"index" => { "queries" => nil, "events" => nil }
|
|
32
|
+
},
|
|
33
|
+
"dataset_builder" => %w[
|
|
34
|
+
aggregation_engine split_ratios query_id_field doc_id_field hit_ids_field additional_qid_fields
|
|
35
|
+
should_shuffle_data min_documents_per_query label_quantiles cascade_cutoff negative_downsample_ratio
|
|
36
|
+
debug_mode conversion_rate_targets conversion_features
|
|
37
|
+
].to_h { |key| [key, nil] },
|
|
38
|
+
"dataset_aggregation" => { "staging_root" => nil, "sqlite" => { "cache_size_mb" => nil, "page_size" => nil } },
|
|
39
|
+
"dataset_enricher" => { "class" => nil },
|
|
40
|
+
"label_transformer" => { "class" => nil, "groups" => nil, "offset" => nil, "range" => nil },
|
|
41
|
+
"deploy" => %w[
|
|
42
|
+
enabled local local_path sagemaker_bucket execution_role instance_type instance_count image_uri metric minimum_metric_value
|
|
43
|
+
].to_h { |key| [key, nil] },
|
|
44
|
+
"inference" => { "sagemaker" => { "open_timeout" => nil, "read_timeout" => nil } },
|
|
45
|
+
"ssm" => ["enabled", *Ltr::Ssm::ParameterPaths::KEYS.values].to_h { |key| [key, nil] },
|
|
46
|
+
}.freeze
|
|
47
|
+
|
|
48
|
+
# Takes the merged config hash to check.
|
|
49
|
+
def initialize(config)
|
|
50
|
+
@config = config
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Checks each config section in turn. A section that is missing is skipped.
|
|
54
|
+
# Raises `ConfigurationError` on the first invalid value.
|
|
55
|
+
# Logs a warning for each key outside `KNOWN_KEYS`.
|
|
56
|
+
def validate!
|
|
57
|
+
warn_unknown_keys(config, known_keys, [])
|
|
58
|
+
validate_seed!
|
|
59
|
+
validate_model!
|
|
60
|
+
validate_training!
|
|
61
|
+
validate_dataset_builder!
|
|
62
|
+
validate_dataset_aggregation!
|
|
63
|
+
validate_label_transformer!
|
|
64
|
+
validate_evaluation!
|
|
65
|
+
validate_optimization!
|
|
66
|
+
validate_deploy!
|
|
67
|
+
validate_ssm!
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Checks the `dataset_builder` settings that only aggregation needs. `validate!` does not run these,
|
|
71
|
+
# because a config used only to log events, train or deploy has no rate targets.
|
|
72
|
+
# Raises `ConfigurationError` when `conversion_rate_targets` is missing or empty.
|
|
73
|
+
# Raises it too when a `conversion_features` numerator or denominator is not a cascade action.
|
|
74
|
+
# A cascade action is an action that a rate target names, or `impression`.
|
|
75
|
+
# Raises it too when a cascade action's `<action>_count` column, or an `additional_qid_fields` entry,
|
|
76
|
+
# is not a usable column name.
|
|
77
|
+
def validate_aggregation!
|
|
78
|
+
dataset_builder = config["dataset_builder"] || {}
|
|
79
|
+
rate_targets = dataset_builder["conversion_rate_targets"]
|
|
80
|
+
raise Ltr::ConfigurationError, "dataset_builder.conversion_rate_targets must be a non-empty array" unless rate_targets.is_a?(Array) && rate_targets.any?
|
|
81
|
+
|
|
82
|
+
cascade_actions = Ltr::DatasetAggregation::CascadeActions.for(rate_targets)
|
|
83
|
+
allowed_actions = cascade_actions + [Ltr::DatasetAggregation::CascadeActions.default_action]
|
|
84
|
+
(dataset_builder["conversion_features"] || []).each { |feature| validate_feature_actions!(feature, allowed_actions, rate_targets) }
|
|
85
|
+
validate_action_columns!(cascade_actions)
|
|
86
|
+
validate_qid_fields!(dataset_builder["additional_qid_fields"], cascade_actions)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
attr_reader :config
|
|
92
|
+
|
|
93
|
+
def known_keys
|
|
94
|
+
return KNOWN_KEYS unless custom_label_transformer?
|
|
95
|
+
|
|
96
|
+
KNOWN_KEYS.merge("label_transformer" => nil)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def custom_label_transformer?
|
|
100
|
+
section = config["label_transformer"]
|
|
101
|
+
transformer_class = section["class"] if section.is_a?(Hash)
|
|
102
|
+
transformer_class.present? && !transformer_class.to_s.delete_prefix("::").start_with?("Ltr::DatasetAggregation::")
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def warn_unknown_keys(section, known, path)
|
|
106
|
+
section.each do |key, value|
|
|
107
|
+
key_path = (path + [key]).join(".")
|
|
108
|
+
if !known.key?(key)
|
|
109
|
+
Ltr.config.logger.warn("Unknown config key `#{key_path}`. The gem ignores it. Check the spelling against docs/config.md.")
|
|
110
|
+
elsif known[key].is_a?(Hash) && value.is_a?(Hash)
|
|
111
|
+
warn_unknown_keys(value, known[key], path + [key])
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def validate_seed!
|
|
117
|
+
return unless config.key?("seed")
|
|
118
|
+
|
|
119
|
+
seed = config["seed"]
|
|
120
|
+
raise Ltr::ConfigurationError, "seed must be an integer (got #{seed.inspect})" unless seed.is_a?(Integer)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def validate_model!
|
|
124
|
+
model = config["model"]
|
|
125
|
+
return if model.nil?
|
|
126
|
+
|
|
127
|
+
raise Ltr::ConfigurationError, "model.name is required" if model["name"].blank?
|
|
128
|
+
|
|
129
|
+
hyperparameters = model["hyperparameters"]
|
|
130
|
+
if hyperparameters && !hyperparameters.is_a?(Hash)
|
|
131
|
+
raise Ltr::ConfigurationError, "model.hyperparameters must be a hash"
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def validate_training!
|
|
136
|
+
training = config["training"]
|
|
137
|
+
return if training.nil?
|
|
138
|
+
|
|
139
|
+
path = training["modelling_dataset_path"]
|
|
140
|
+
raise Ltr::ConfigurationError, "training.modelling_dataset_path is required" if path.blank?
|
|
141
|
+
|
|
142
|
+
validate_split_role!("training.early_stopping_split", training["early_stopping_split"])
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def validate_split_role!(key, split)
|
|
146
|
+
return if split.nil?
|
|
147
|
+
|
|
148
|
+
return if Ltr::Data::Splits::NAMES.include?(split.to_s)
|
|
149
|
+
|
|
150
|
+
raise Ltr::ConfigurationError,
|
|
151
|
+
"#{key} must be one of #{Ltr::Data::Splits::NAMES.join(", ")} (got #{split.inspect})"
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def validate_dataset_builder!
|
|
155
|
+
dataset_builder = config["dataset_builder"]
|
|
156
|
+
return if dataset_builder.nil?
|
|
157
|
+
|
|
158
|
+
validate_aggregation_engine!(dataset_builder["aggregation_engine"])
|
|
159
|
+
validate_split_ratios!(dataset_builder["split_ratios"])
|
|
160
|
+
validate_conversion_rate_targets!(dataset_builder["conversion_rate_targets"])
|
|
161
|
+
validate_conversion_features!(dataset_builder)
|
|
162
|
+
validate_min_documents_per_query!(dataset_builder["min_documents_per_query"])
|
|
163
|
+
validate_debug_mode!(dataset_builder["debug_mode"])
|
|
164
|
+
validate_negative_sampling!(dataset_builder["cascade_cutoff"], dataset_builder["negative_downsample_ratio"])
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def validate_aggregation_engine!(engine)
|
|
168
|
+
return if engine.nil?
|
|
169
|
+
|
|
170
|
+
unless AGGREGATION_ENGINES.include?(engine)
|
|
171
|
+
raise Ltr::ConfigurationError,
|
|
172
|
+
"dataset_builder.aggregation_engine must be one of #{AGGREGATION_ENGINES.join(", ")} (got #{engine.inspect})"
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def validate_split_ratios!(split_ratios)
|
|
177
|
+
return if split_ratios.nil?
|
|
178
|
+
|
|
179
|
+
Ltr::Data::Splits.validate_ratios!(split_ratios)
|
|
180
|
+
rescue ArgumentError => e
|
|
181
|
+
raise Ltr::ConfigurationError, "dataset_builder.split_ratios #{e.message}"
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def validate_conversion_features!(dataset_builder)
|
|
185
|
+
query_id_field = dataset_builder["query_id_field"] || Ltr::ConfigLoader::DEFAULT_QUERY_ID_FIELD
|
|
186
|
+
doc_id_field = dataset_builder["doc_id_field"] || Ltr::ConfigLoader::DEFAULT_DOC_ID_FIELD
|
|
187
|
+
reserved_names = ["split", "label", query_id_field, doc_id_field]
|
|
188
|
+
|
|
189
|
+
(dataset_builder["conversion_features"] || []).each do |feature|
|
|
190
|
+
next unless reserved_names.include?(feature["name"])
|
|
191
|
+
|
|
192
|
+
raise Ltr::ConfigurationError,
|
|
193
|
+
"dataset_builder.conversion_features name '#{feature["name"]}' collides with a dataset column"
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# Raises when a rate target action has a `<action>_count` column that is not a usable name.
|
|
198
|
+
# The bare action name is not checked, because it is only a string value in the events.
|
|
199
|
+
def validate_action_columns!(cascade_actions)
|
|
200
|
+
cascade_actions.each do |action|
|
|
201
|
+
Ltr::DatasetAggregation::ColumnName.validate!("#{action}_count", "dataset_builder.conversion_rate_targets cascade action column")
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Raises when `additional_qid_fields` is not an array of strings.
|
|
206
|
+
# Raises when an entry is not a usable name, is listed twice, or equals the count column of a cascade action.
|
|
207
|
+
def validate_qid_fields!(fields, cascade_actions)
|
|
208
|
+
return if fields.nil?
|
|
209
|
+
|
|
210
|
+
kind = "dataset_builder.additional_qid_fields"
|
|
211
|
+
raise Ltr::ConfigurationError, "#{kind} must be an array of strings" unless fields.is_a?(Array) && fields.all?(String)
|
|
212
|
+
|
|
213
|
+
count_columns = cascade_actions.to_h { |action| ["#{action}_count", action] }
|
|
214
|
+
fields.each_with_index do |field, index|
|
|
215
|
+
Ltr::DatasetAggregation::ColumnName.validate!(field, kind)
|
|
216
|
+
raise Ltr::ConfigurationError, "#{kind} lists '#{field}' more than once" if fields.index(field) != index
|
|
217
|
+
|
|
218
|
+
if count_columns.key?(field)
|
|
219
|
+
raise Ltr::ConfigurationError,
|
|
220
|
+
"#{kind} '#{field}' collides with the count column of the cascade action '#{count_columns[field]}'"
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def validate_feature_actions!(feature, allowed_actions, rate_targets)
|
|
226
|
+
%w[numerator denominator].each do |role|
|
|
227
|
+
next if allowed_actions.include?(feature[role].to_s.delete_suffix("_count"))
|
|
228
|
+
|
|
229
|
+
raise Ltr::ConfigurationError,
|
|
230
|
+
"dataset_builder.conversion_features '#{feature["name"]}' #{role} '#{feature[role]}' is not a cascade action. " \
|
|
231
|
+
"The actions that conversion_rate_targets names are #{allowed_actions.join(", ")} " \
|
|
232
|
+
"(from #{rate_targets.map { |rate| rate["name"] }.join(", ")})"
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def validate_conversion_rate_targets!(rate_targets)
|
|
237
|
+
return if rate_targets.nil?
|
|
238
|
+
|
|
239
|
+
unless rate_targets.is_a?(Array) && rate_targets.any?
|
|
240
|
+
raise Ltr::ConfigurationError, "dataset_builder.conversion_rate_targets must be a non-empty array when present"
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
rate_targets.each { |rate| validate_conversion_rate_target!(rate) }
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def validate_conversion_rate_target!(rate)
|
|
247
|
+
unless rate.is_a?(Hash)
|
|
248
|
+
raise Ltr::ConfigurationError, "conversion_rate_targets entries must be hashes, got: #{rate.inspect}"
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
name = rate["name"]
|
|
252
|
+
raise Ltr::ConfigurationError, "conversion_rate_targets entry is missing a name: #{rate.inspect}" if name.blank?
|
|
253
|
+
|
|
254
|
+
raise_conversion_rate_error(name, "numerator is required") if rate["numerator"].blank?
|
|
255
|
+
|
|
256
|
+
unless non_negative_number?(rate["weight"])
|
|
257
|
+
raise_conversion_rate_error(name, "weight must be numeric and non-negative")
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
raise_conversion_rate_error(name, "nu must be greater than 0") unless positive_number?(rate["nu"])
|
|
261
|
+
|
|
262
|
+
unless rate["denominator"].nil? || rate["denominator"].is_a?(String)
|
|
263
|
+
raise_conversion_rate_error(name, "denominator must be a string when present")
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
unless rate["allow_invariant_violation"].nil? || boolean?(rate["allow_invariant_violation"])
|
|
267
|
+
raise_conversion_rate_error(name, "allow_invariant_violation must be true or false when present")
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def raise_conversion_rate_error(name, problem)
|
|
272
|
+
raise Ltr::ConfigurationError, "conversion_rate_targets['#{name}'].#{problem}"
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
def non_negative_number?(value)
|
|
276
|
+
value.is_a?(Numeric) && value >= 0
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def positive_number?(value)
|
|
280
|
+
value.is_a?(Numeric) && value > 0
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
def boolean?(value)
|
|
284
|
+
[true, false].include?(value)
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
def validate_min_documents_per_query!(min_documents_per_query)
|
|
288
|
+
return if min_documents_per_query.nil?
|
|
289
|
+
|
|
290
|
+
unless positive_integer?(min_documents_per_query)
|
|
291
|
+
raise Ltr::ConfigurationError, "dataset_builder.min_documents_per_query must be a positive integer when present"
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def positive_integer?(value)
|
|
296
|
+
value.is_a?(Integer) && value > 0
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def validate_debug_mode!(debug_mode)
|
|
300
|
+
return if debug_mode.nil?
|
|
301
|
+
|
|
302
|
+
unless boolean?(debug_mode)
|
|
303
|
+
raise Ltr::ConfigurationError, "dataset_builder.debug_mode must be true or false when present"
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
def validate_negative_sampling!(cascade_cutoff, ratio)
|
|
308
|
+
unless cascade_cutoff.nil? || boolean?(cascade_cutoff)
|
|
309
|
+
raise Ltr::ConfigurationError, "dataset_builder.cascade_cutoff must be true or false when present"
|
|
310
|
+
end
|
|
311
|
+
return if ratio.nil?
|
|
312
|
+
|
|
313
|
+
unless positive_number?(ratio)
|
|
314
|
+
raise Ltr::ConfigurationError, "dataset_builder.negative_downsample_ratio must be a positive number when present"
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
unless cascade_cutoff == false
|
|
318
|
+
raise Ltr::ConfigurationError,
|
|
319
|
+
"dataset_builder.negative_downsample_ratio requires cascade_cutoff: false (with the cut-off on there are no negatives)"
|
|
320
|
+
end
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
def validate_dataset_aggregation!
|
|
324
|
+
dataset_aggregation = config["dataset_aggregation"]
|
|
325
|
+
return if dataset_aggregation.nil?
|
|
326
|
+
|
|
327
|
+
unless dataset_aggregation.is_a?(Hash)
|
|
328
|
+
raise Ltr::ConfigurationError, "dataset_aggregation must be a hash"
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
validate_sqlite_settings!(dataset_aggregation["sqlite"])
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
def validate_sqlite_settings!(sqlite)
|
|
335
|
+
return if sqlite.nil?
|
|
336
|
+
|
|
337
|
+
raise Ltr::ConfigurationError, "dataset_aggregation.sqlite must be a hash" unless sqlite.is_a?(Hash)
|
|
338
|
+
|
|
339
|
+
%w[cache_size_mb page_size].each do |key|
|
|
340
|
+
value = sqlite[key]
|
|
341
|
+
next if value.nil?
|
|
342
|
+
|
|
343
|
+
unless positive_integer?(value)
|
|
344
|
+
raise Ltr::ConfigurationError,
|
|
345
|
+
"dataset_aggregation.sqlite.#{key} must be a positive integer when present"
|
|
346
|
+
end
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
validate_page_size!(sqlite["page_size"])
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
def validate_page_size!(page_size)
|
|
353
|
+
return if page_size.nil?
|
|
354
|
+
|
|
355
|
+
valid = page_size.between?(512, 65_536) && (page_size & (page_size - 1)).zero?
|
|
356
|
+
return if valid
|
|
357
|
+
|
|
358
|
+
raise Ltr::ConfigurationError,
|
|
359
|
+
"dataset_aggregation.sqlite.page_size must be a power of two between 512 and 65536 (got #{page_size})"
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
def validate_label_transformer!
|
|
363
|
+
settings = config["label_transformer"]
|
|
364
|
+
return if settings.nil?
|
|
365
|
+
|
|
366
|
+
transformer = settings["class"]&.safe_constantize
|
|
367
|
+
return unless transformer.is_a?(Class)
|
|
368
|
+
|
|
369
|
+
if transformer < Ltr::DatasetAggregation::PerQueryLabelTransformer
|
|
370
|
+
transformer.settings_from(settings)
|
|
371
|
+
elsif transformer <= Ltr::DatasetAggregation::QuantileLabelTransformer
|
|
372
|
+
transformer.label_quantiles_from(config["dataset_builder"] || {})
|
|
373
|
+
end
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
def validate_evaluation!
|
|
377
|
+
evaluation = config["evaluation"]
|
|
378
|
+
return if evaluation.nil?
|
|
379
|
+
|
|
380
|
+
metrics = evaluation["metrics"]
|
|
381
|
+
raise Ltr::ConfigurationError, "evaluation.metrics must be a non-empty array" if metrics.blank?
|
|
382
|
+
|
|
383
|
+
validate_split_role!("evaluation.split", evaluation["split"])
|
|
384
|
+
validate_min_relevance!(evaluation)
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
def validate_min_relevance!(evaluation)
|
|
388
|
+
percentile = evaluation["min_relevance_percentile"]
|
|
389
|
+
return if percentile.nil?
|
|
390
|
+
|
|
391
|
+
unless percentile.is_a?(Numeric) && percentile > 0 && percentile < 100
|
|
392
|
+
raise Ltr::ConfigurationError,
|
|
393
|
+
"evaluation.min_relevance_percentile must be numeric and between 0 and 100 exclusive (got #{percentile.inspect})"
|
|
394
|
+
end
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
def validate_optimization!
|
|
398
|
+
optimization = config["optimization"]
|
|
399
|
+
return if optimization.nil?
|
|
400
|
+
|
|
401
|
+
validate_split_role!("optimization.split", optimization["split"])
|
|
402
|
+
Ltr::Optimize::FeatureSelection::Strategy.validate!(optimization.dig("feature_selection", "strategy"))
|
|
403
|
+
metric = optimization["metric_to_optimize"]
|
|
404
|
+
Ltr::Metrics::MetricScores.validate_spec!(metric, "optimization.metric_to_optimize") unless metric.nil?
|
|
405
|
+
space = optimization["hyperparameter_search_space"]
|
|
406
|
+
Ltr::Optimize::SearchSpace.validate!(space, "optimization.hyperparameter_search_space") unless space.nil?
|
|
407
|
+
validate_bayesian_optimizer!(optimization)
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
def validate_bayesian_optimizer!(optimization)
|
|
411
|
+
Ltr::Optimize::BayesianOptimizer.validate!(Ltr::Optimize::OptimizeHyperparameters.optimizer_options(optimization))
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
def validate_deploy!
|
|
415
|
+
deploy = config["deploy"]
|
|
416
|
+
return if deploy.nil?
|
|
417
|
+
|
|
418
|
+
if deploy["local"] == true && deploy["local_path"].blank?
|
|
419
|
+
raise Ltr::ConfigurationError, "deploy.local_path is required when deploy.local is true"
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
return unless deploy["enabled"] == true || deploy["local"] == true
|
|
423
|
+
|
|
424
|
+
validate_sagemaker_settings!(deploy) if deploy["enabled"] == true
|
|
425
|
+
validate_deploy_metric!(deploy)
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
def validate_sagemaker_settings!(deploy)
|
|
429
|
+
%w[execution_role sagemaker_bucket].each do |key|
|
|
430
|
+
raise Ltr::ConfigurationError, "deploy.#{key} is required when deploy.enabled is true" if deploy[key].blank?
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
image_uri = deploy["image_uri"]
|
|
434
|
+
unless image_uri.nil? || image_uri.is_a?(String)
|
|
435
|
+
raise Ltr::ConfigurationError, "deploy.image_uri must be a string (got #{image_uri.inspect})"
|
|
436
|
+
end
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
def validate_deploy_metric!(deploy)
|
|
440
|
+
metric = Ltr::Metrics::MetricScores.normalize_name(deploy["metric"])
|
|
441
|
+
raise Ltr::ConfigurationError, "deploy.metric is required when deploy.enabled or deploy.local is true" if metric.blank?
|
|
442
|
+
|
|
443
|
+
threshold = deploy["minimum_metric_value"]
|
|
444
|
+
begin
|
|
445
|
+
Float(threshold)
|
|
446
|
+
rescue ArgumentError, TypeError
|
|
447
|
+
raise Ltr::ConfigurationError,
|
|
448
|
+
"deploy.minimum_metric_value must be numeric when deploy.enabled or deploy.local is true (got #{threshold.inspect})"
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
evaluation_metrics = config.dig("evaluation", "metrics")&.map { |name| Ltr::Metrics::MetricScores.normalize_name(name) } || []
|
|
452
|
+
return if evaluation_metrics.empty?
|
|
453
|
+
|
|
454
|
+
unless evaluation_metrics.include?(metric)
|
|
455
|
+
raise Ltr::ConfigurationError,
|
|
456
|
+
"deploy.metric '#{metric}' must be listed in evaluation.metrics (#{evaluation_metrics.join(", ")})"
|
|
457
|
+
end
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
def validate_ssm!
|
|
461
|
+
ssm = config["ssm"]
|
|
462
|
+
return if ssm.nil?
|
|
463
|
+
|
|
464
|
+
enabled = ssm["enabled"]
|
|
465
|
+
unless [nil, true, false].include?(enabled)
|
|
466
|
+
Ltr.config.logger.warn(
|
|
467
|
+
"ssm.enabled is #{enabled.inspect}, not a boolean. Only the unquoted boolean true turns SSM on. SSM stays off."
|
|
468
|
+
)
|
|
469
|
+
end
|
|
470
|
+
return unless enabled == true
|
|
471
|
+
|
|
472
|
+
Ltr::Ssm::ParameterPaths::KEYS.each_value do |key|
|
|
473
|
+
raise Ltr::ConfigurationError, "ssm.#{key} is required when ssm.enabled is true" if ssm[key].blank?
|
|
474
|
+
end
|
|
475
|
+
end
|
|
476
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Holds the gem wide settings such as the logger, S3 client, root path and error reporter.
|
|
4
|
+
|
|
5
|
+
class Ltr::Configuration
|
|
6
|
+
OPENSEARCH_REQUEST_TIMEOUT = 20
|
|
7
|
+
OPENSEARCH_OPEN_TIMEOUT = 60
|
|
8
|
+
|
|
9
|
+
attr_writer :logger, :s3_client_builder, :error_reporter
|
|
10
|
+
# The object that `report_error` calls with the error and its context hash. It is nil by default.
|
|
11
|
+
attr_reader :error_reporter
|
|
12
|
+
attr_writer :opensearch_connect_options_builder
|
|
13
|
+
|
|
14
|
+
# Returns the logger. It defaults to the Rails logger when Rails is loaded, else a logger on stdout.
|
|
15
|
+
def logger
|
|
16
|
+
@logger ||= default_logger
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Returns the S3 client. It calls `s3_client_builder` each time when that is set.
|
|
20
|
+
# Otherwise it returns an `Aws::S3::Client` that is built once and reused.
|
|
21
|
+
def s3_client
|
|
22
|
+
@s3_client_builder ? @s3_client_builder.call : default_s3_client
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Passes the error and its context to the error reporter. Does nothing when no reporter is set.
|
|
26
|
+
def report_error(error, context = {})
|
|
27
|
+
error_reporter&.call(error, context)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Returns the root path that relative config paths resolve against. Defaults to the current directory.
|
|
31
|
+
def root
|
|
32
|
+
@root ||= Pathname.new(Dir.pwd)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Sets the root path for relative config paths. A relative path is made absolute now, so a later `Dir.chdir` does not move the root.
|
|
36
|
+
def root=(path)
|
|
37
|
+
@root = Pathname.new(path).expand_path
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Returns the OpenSearch client options for the url, user and password. It calls `opensearch_connect_options_builder` when that is set.
|
|
41
|
+
# Otherwise it builds one host with request timeouts, and uses the url's port. That is 443 for an https url without a port.
|
|
42
|
+
def opensearch_connect_options(url, user, password)
|
|
43
|
+
if @opensearch_connect_options_builder
|
|
44
|
+
@opensearch_connect_options_builder.call(url, user, password)
|
|
45
|
+
else
|
|
46
|
+
default_opensearch_connect_options(url, user, password)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def default_logger
|
|
53
|
+
(defined?(::Rails) && ::Rails.logger) ? ::Rails.logger : Logger.new($stdout)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def default_s3_client
|
|
57
|
+
@default_s3_client ||= begin
|
|
58
|
+
Ltr::OptionalDependency.require!("aws-sdk-s3", feature: "the default S3 client")
|
|
59
|
+
Aws::S3::Client.new
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def default_opensearch_connect_options(url, user, password)
|
|
64
|
+
uri = URI.parse(url)
|
|
65
|
+
|
|
66
|
+
{
|
|
67
|
+
hosts: [
|
|
68
|
+
{
|
|
69
|
+
host: uri.host,
|
|
70
|
+
port: uri.port,
|
|
71
|
+
scheme: uri.scheme,
|
|
72
|
+
user: user,
|
|
73
|
+
password: password,
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
transport_options: {
|
|
77
|
+
request: {
|
|
78
|
+
timeout: OPENSEARCH_REQUEST_TIMEOUT,
|
|
79
|
+
open_timeout: OPENSEARCH_OPEN_TIMEOUT,
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
}
|
|
83
|
+
end
|
|
84
|
+
end
|