tributo-algorithms 0.1.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.
- tributo_algorithms-0.1.0.dist-info/METADATA +328 -0
- tributo_algorithms-0.1.0.dist-info/RECORD +110 -0
- tributo_algorithms-0.1.0.dist-info/WHEEL +4 -0
- tributo_algorithms-0.1.0.dist-info/entry_points.txt +57 -0
- tributo_algorithms-0.1.0.dist-info/licenses/LICENSE +202 -0
- tributo_algorithms_boosting/__init__.py +42 -0
- tributo_algorithms_boosting/algorithm.py +825 -0
- tributo_algorithms_boosting/checkpointing.py +441 -0
- tributo_algorithms_boosting/contracts.py +232 -0
- tributo_algorithms_boosting/data_config.py +59 -0
- tributo_algorithms_boosting/descriptor.py +124 -0
- tributo_algorithms_boosting/lightgbm.py +511 -0
- tributo_algorithms_boosting/lightgbm_contracts.py +186 -0
- tributo_algorithms_boosting/lightgbm_descriptor.py +112 -0
- tributo_algorithms_boosting/native.py +557 -0
- tributo_algorithms_boosting/stages.py +150 -0
- tributo_algorithms_catboost/__init__.py +5 -0
- tributo_algorithms_catboost/algorithm.py +379 -0
- tributo_algorithms_catboost/contracts.py +145 -0
- tributo_algorithms_catboost/descriptor.py +121 -0
- tributo_algorithms_catboost/exporter.py +106 -0
- tributo_algorithms_catboost/flavor.py +191 -0
- tributo_algorithms_causal_core/__init__.py +41 -0
- tributo_algorithms_causal_core/algorithm.py +1193 -0
- tributo_algorithms_causal_core/contracts.py +136 -0
- tributo_algorithms_causal_core/descriptor.py +270 -0
- tributo_algorithms_causal_core/exporter.py +52 -0
- tributo_algorithms_causal_discovery/__init__.py +23 -0
- tributo_algorithms_causal_discovery/algorithm.py +526 -0
- tributo_algorithms_causal_discovery/contracts.py +97 -0
- tributo_algorithms_causal_discovery/descriptor.py +130 -0
- tributo_algorithms_causal_discovery/exporter.py +43 -0
- tributo_algorithms_causal_dowhy/__init__.py +38 -0
- tributo_algorithms_causal_dowhy/algorithm.py +325 -0
- tributo_algorithms_causal_dowhy/contracts.py +209 -0
- tributo_algorithms_causal_dowhy/descriptor.py +106 -0
- tributo_algorithms_causal_dowhy/exporter.py +43 -0
- tributo_algorithms_causal_dowhy/gcm.py +634 -0
- tributo_algorithms_causal_dowhy/gcm_descriptor.py +110 -0
- tributo_algorithms_causal_dr/__init__.py +22 -0
- tributo_algorithms_causal_dr/algorithm.py +446 -0
- tributo_algorithms_causal_dr/contracts.py +94 -0
- tributo_algorithms_causal_dr/descriptor.py +104 -0
- tributo_algorithms_causal_xlearner/__init__.py +29 -0
- tributo_algorithms_causal_xlearner/algorithm.py +448 -0
- tributo_algorithms_causal_xlearner/contracts.py +97 -0
- tributo_algorithms_causal_xlearner/descriptor.py +120 -0
- tributo_algorithms_causal_xlearner/exporter.py +432 -0
- tributo_algorithms_causal_xlearner/flavor.py +120 -0
- tributo_algorithms_causal_xlearner/model.py +116 -0
- tributo_algorithms_classical/__init__.py +41 -0
- tributo_algorithms_classical/contracts.py +139 -0
- tributo_algorithms_classical/descriptors.py +370 -0
- tributo_algorithms_classical/exporter.py +166 -0
- tributo_algorithms_classical/isolation_forest.py +172 -0
- tributo_algorithms_classical/isolation_forest_descriptor.py +122 -0
- tributo_algorithms_classical/kmeans.py +315 -0
- tributo_algorithms_classical/kmeans_descriptor.py +147 -0
- tributo_algorithms_classical/linear_regression.py +206 -0
- tributo_algorithms_classical/logistic_regression.py +238 -0
- tributo_algorithms_classical/models.py +28 -0
- tributo_algorithms_classical/multinomial_nb.py +643 -0
- tributo_algorithms_classical/multinomial_nb_descriptor.py +183 -0
- tributo_algorithms_classical/pca.py +187 -0
- tributo_algorithms_classical/pca_descriptor.py +111 -0
- tributo_algorithms_classical/random_forest.py +304 -0
- tributo_algorithms_classical/sgd.py +331 -0
- tributo_algorithms_classical/sgd_contracts.py +121 -0
- tributo_algorithms_classical/sgd_descriptor.py +143 -0
- tributo_algorithms_classical/unsupervised_contracts.py +198 -0
- tributo_algorithms_classical/unsupervised_exporter.py +427 -0
- tributo_algorithms_classical/unsupervised_models.py +90 -0
- tributo_algorithms_graph_pyg/__init__.py +37 -0
- tributo_algorithms_graph_pyg/algorithm.py +883 -0
- tributo_algorithms_graph_pyg/contracts.py +260 -0
- tributo_algorithms_graph_pyg/descriptor.py +201 -0
- tributo_algorithms_multistage_torch/__init__.py +40 -0
- tributo_algorithms_multistage_torch/algorithm.py +532 -0
- tributo_algorithms_multistage_torch/contracts.py +220 -0
- tributo_algorithms_multistage_torch/descriptor.py +146 -0
- tributo_algorithms_multistage_torch/pretrain.py +570 -0
- tributo_algorithms_multistage_torch/pretrain_descriptor.py +153 -0
- tributo_algorithms_recsys_torch/__init__.py +35 -0
- tributo_algorithms_recsys_torch/contracts.py +295 -0
- tributo_algorithms_recsys_torch/descriptor.py +136 -0
- tributo_algorithms_recsys_torch/jagged.py +707 -0
- tributo_algorithms_recsys_torch/jagged_descriptor.py +130 -0
- tributo_algorithms_recsys_torch/recipe.py +192 -0
- tributo_algorithms_representation/__init__.py +20 -0
- tributo_algorithms_representation/contracts.py +117 -0
- tributo_algorithms_representation/descriptor.py +137 -0
- tributo_algorithms_representation/recipe.py +162 -0
- tributo_algorithms_tabular_torch/__init__.py +46 -0
- tributo_algorithms_tabular_torch/contracts.py +228 -0
- tributo_algorithms_tabular_torch/descriptor.py +223 -0
- tributo_algorithms_tabular_torch/priors.py +216 -0
- tributo_algorithms_tabular_torch/pu_metrics.py +269 -0
- tributo_algorithms_tabular_torch/pu_reducer.py +129 -0
- tributo_algorithms_tabular_torch/recipe.py +294 -0
- tributo_algorithms_timeseries/__init__.py +36 -0
- tributo_algorithms_timeseries/contracts.py +120 -0
- tributo_algorithms_timeseries/descriptor.py +137 -0
- tributo_algorithms_timeseries/recipe.py +195 -0
- tributo_algorithms_timeseries/rnn_contracts.py +170 -0
- tributo_algorithms_timeseries/rnn_descriptor.py +167 -0
- tributo_algorithms_timeseries/rnn_recipe.py +223 -0
- tributo_algorithms_transformers_nlp/__init__.py +20 -0
- tributo_algorithms_transformers_nlp/contracts.py +109 -0
- tributo_algorithms_transformers_nlp/descriptor.py +137 -0
- tributo_algorithms_transformers_nlp/recipe.py +235 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""Fixed-composition X-Learner model and prediction semantics."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Any, Mapping, cast
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
STAGES = ("mu0", "mu1", "tau0", "tau1", "propensity")
|
|
11
|
+
FORMULA = "cate=propensity*tau0+(1-propensity)*tau1"
|
|
12
|
+
QUADRANT_CODES = {
|
|
13
|
+
"low_response_low_uplift": 0,
|
|
14
|
+
"low_response_high_uplift": 1,
|
|
15
|
+
"high_response_low_uplift": 2,
|
|
16
|
+
"high_response_high_uplift": 3,
|
|
17
|
+
}
|
|
18
|
+
STAGE_OBJECTIVES = {
|
|
19
|
+
"mu0": "reg:squarederror",
|
|
20
|
+
"mu1": "reg:squarederror",
|
|
21
|
+
"tau0": "reg:squarederror",
|
|
22
|
+
"tau1": "reg:squarederror",
|
|
23
|
+
"propensity": "binary:logistic",
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(frozen=True)
|
|
28
|
+
class XLearnerPrediction:
|
|
29
|
+
mu0: np.ndarray
|
|
30
|
+
mu1: np.ndarray
|
|
31
|
+
tau0: np.ndarray
|
|
32
|
+
tau1: np.ndarray
|
|
33
|
+
propensity: np.ndarray
|
|
34
|
+
cate: np.ndarray
|
|
35
|
+
quadrant: np.ndarray
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class XLearnerModel:
|
|
39
|
+
def __init__(
|
|
40
|
+
self,
|
|
41
|
+
boosters: Mapping[str, object],
|
|
42
|
+
*,
|
|
43
|
+
feature_names: tuple[str, ...],
|
|
44
|
+
response_threshold: float,
|
|
45
|
+
propensity_clip: tuple[float, float],
|
|
46
|
+
) -> None:
|
|
47
|
+
if set(boosters) != set(STAGES):
|
|
48
|
+
raise ValueError("X-Learner requires exactly five Booster stages")
|
|
49
|
+
self.boosters = dict(boosters)
|
|
50
|
+
self.feature_names = feature_names
|
|
51
|
+
self.response_threshold = response_threshold
|
|
52
|
+
self.propensity_clip = propensity_clip
|
|
53
|
+
|
|
54
|
+
@classmethod
|
|
55
|
+
def from_raw(
|
|
56
|
+
cls,
|
|
57
|
+
raw: Mapping[str, bytes],
|
|
58
|
+
**kwargs: Any,
|
|
59
|
+
) -> XLearnerModel:
|
|
60
|
+
import xgboost
|
|
61
|
+
|
|
62
|
+
boosters = {}
|
|
63
|
+
for stage in STAGES:
|
|
64
|
+
booster = xgboost.Booster()
|
|
65
|
+
booster.load_model(bytearray(raw[stage]))
|
|
66
|
+
boosters[stage] = booster
|
|
67
|
+
return cls(boosters, **kwargs)
|
|
68
|
+
|
|
69
|
+
def predict(self, features: object) -> XLearnerPrediction:
|
|
70
|
+
import xgboost
|
|
71
|
+
|
|
72
|
+
values = np.asarray(features, dtype=np.float32)
|
|
73
|
+
if values.ndim != 2 or values.shape[1] != len(self.feature_names):
|
|
74
|
+
raise ValueError("X-Learner input shape does not match feature_names")
|
|
75
|
+
matrix = xgboost.DMatrix(values)
|
|
76
|
+
predictions = {
|
|
77
|
+
stage: np.asarray(cast(Any, self.boosters[stage]).predict(matrix))
|
|
78
|
+
for stage in STAGES
|
|
79
|
+
}
|
|
80
|
+
propensity = np.clip(
|
|
81
|
+
predictions["propensity"], self.propensity_clip[0], self.propensity_clip[1]
|
|
82
|
+
)
|
|
83
|
+
cate = (
|
|
84
|
+
propensity * predictions["tau0"] + (1.0 - propensity) * predictions["tau1"]
|
|
85
|
+
)
|
|
86
|
+
response = np.maximum(predictions["mu0"], predictions["mu1"])
|
|
87
|
+
high_response = response >= self.response_threshold
|
|
88
|
+
high_uplift = cate >= 0.0
|
|
89
|
+
quadrant = np.where(
|
|
90
|
+
high_response,
|
|
91
|
+
np.where(
|
|
92
|
+
high_uplift, "high_response_high_uplift", "high_response_low_uplift"
|
|
93
|
+
),
|
|
94
|
+
np.where(
|
|
95
|
+
high_uplift, "low_response_high_uplift", "low_response_low_uplift"
|
|
96
|
+
),
|
|
97
|
+
)
|
|
98
|
+
return XLearnerPrediction(
|
|
99
|
+
mu0=predictions["mu0"],
|
|
100
|
+
mu1=predictions["mu1"],
|
|
101
|
+
tau0=predictions["tau0"],
|
|
102
|
+
tau1=predictions["tau1"],
|
|
103
|
+
propensity=propensity,
|
|
104
|
+
cate=cate,
|
|
105
|
+
quadrant=quadrant,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
__all__ = [
|
|
110
|
+
"FORMULA",
|
|
111
|
+
"QUADRANT_CODES",
|
|
112
|
+
"STAGES",
|
|
113
|
+
"STAGE_OBJECTIVES",
|
|
114
|
+
"XLearnerModel",
|
|
115
|
+
"XLearnerPrediction",
|
|
116
|
+
]
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""Official bounded classical algorithms for Tributo."""
|
|
2
|
+
|
|
3
|
+
from tributo_algorithms_classical.descriptors import (
|
|
4
|
+
EXTRA_TREES_JOBLIB_DESCRIPTOR,
|
|
5
|
+
EXTRA_TREES_NATIVE_DESCRIPTOR,
|
|
6
|
+
LINEAR_REGRESSION_DESCRIPTOR,
|
|
7
|
+
LOGISTIC_REGRESSION_DESCRIPTOR,
|
|
8
|
+
RANDOM_FOREST_JOBLIB_DESCRIPTOR,
|
|
9
|
+
RANDOM_FOREST_NATIVE_DESCRIPTOR,
|
|
10
|
+
)
|
|
11
|
+
from tributo_algorithms_classical.isolation_forest_descriptor import (
|
|
12
|
+
ISOLATION_FOREST_DESCRIPTOR,
|
|
13
|
+
)
|
|
14
|
+
from tributo_algorithms_classical.kmeans_descriptor import (
|
|
15
|
+
KMEANS_DESCRIPTOR,
|
|
16
|
+
MINIBATCH_KMEANS_DESCRIPTOR,
|
|
17
|
+
)
|
|
18
|
+
from tributo_algorithms_classical.multinomial_nb_descriptor import (
|
|
19
|
+
MULTINOMIAL_NB_DESCRIPTOR,
|
|
20
|
+
)
|
|
21
|
+
from tributo_algorithms_classical.pca_descriptor import PCA_DESCRIPTOR
|
|
22
|
+
from tributo_algorithms_classical.sgd_descriptor import (
|
|
23
|
+
SGD_CLASSIFIER_DESCRIPTOR,
|
|
24
|
+
SGD_REGRESSOR_DESCRIPTOR,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"EXTRA_TREES_JOBLIB_DESCRIPTOR",
|
|
29
|
+
"EXTRA_TREES_NATIVE_DESCRIPTOR",
|
|
30
|
+
"ISOLATION_FOREST_DESCRIPTOR",
|
|
31
|
+
"KMEANS_DESCRIPTOR",
|
|
32
|
+
"LINEAR_REGRESSION_DESCRIPTOR",
|
|
33
|
+
"LOGISTIC_REGRESSION_DESCRIPTOR",
|
|
34
|
+
"MINIBATCH_KMEANS_DESCRIPTOR",
|
|
35
|
+
"MULTINOMIAL_NB_DESCRIPTOR",
|
|
36
|
+
"PCA_DESCRIPTOR",
|
|
37
|
+
"RANDOM_FOREST_JOBLIB_DESCRIPTOR",
|
|
38
|
+
"RANDOM_FOREST_NATIVE_DESCRIPTOR",
|
|
39
|
+
"SGD_CLASSIFIER_DESCRIPTOR",
|
|
40
|
+
"SGD_REGRESSOR_DESCRIPTOR",
|
|
41
|
+
]
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""Executable config, input, output, and coverage contracts."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ClassicalConfigValidator:
|
|
10
|
+
"""Validate the shared strict classical-algorithm configuration."""
|
|
11
|
+
|
|
12
|
+
api_version = 1
|
|
13
|
+
schema_digest = "5" * 64
|
|
14
|
+
_ALLOWED = {
|
|
15
|
+
"checkpoint",
|
|
16
|
+
"C",
|
|
17
|
+
"alpha",
|
|
18
|
+
"class_prior",
|
|
19
|
+
"class_weight",
|
|
20
|
+
"feature_count",
|
|
21
|
+
"fit_prior",
|
|
22
|
+
"force_alpha",
|
|
23
|
+
"learning_rate",
|
|
24
|
+
"max_depth",
|
|
25
|
+
"max_features",
|
|
26
|
+
"n_estimators",
|
|
27
|
+
"output",
|
|
28
|
+
"runtime",
|
|
29
|
+
"seed",
|
|
30
|
+
"task",
|
|
31
|
+
"task_count",
|
|
32
|
+
"tolerance",
|
|
33
|
+
"unit_count",
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
def validate(self, value: Mapping[str, Any]) -> Mapping[str, Any]:
|
|
37
|
+
unknown = sorted(set(value) - self._ALLOWED)
|
|
38
|
+
if unknown:
|
|
39
|
+
raise ValueError(f"unknown classical config keys: {unknown}")
|
|
40
|
+
normalized = dict(value)
|
|
41
|
+
task = normalized.get("task")
|
|
42
|
+
if task is not None and task not in {"classification", "regression"}:
|
|
43
|
+
raise ValueError("task must be classification or regression")
|
|
44
|
+
for name, minimum in (
|
|
45
|
+
("n_estimators", 1),
|
|
46
|
+
("unit_count", 1),
|
|
47
|
+
("seed", 0),
|
|
48
|
+
("feature_count", 1),
|
|
49
|
+
("task_count", 1),
|
|
50
|
+
):
|
|
51
|
+
item = normalized.get(name)
|
|
52
|
+
if item is None:
|
|
53
|
+
continue
|
|
54
|
+
if not isinstance(item, int) or isinstance(item, bool) or item < minimum:
|
|
55
|
+
raise ValueError(f"{name} must be an integer >= {minimum}")
|
|
56
|
+
for numeric_name, numeric_minimum in (
|
|
57
|
+
("learning_rate", 0.0),
|
|
58
|
+
("tolerance", 0.0),
|
|
59
|
+
("C", 0.0),
|
|
60
|
+
):
|
|
61
|
+
numeric_item = normalized.get(numeric_name)
|
|
62
|
+
if numeric_item is None:
|
|
63
|
+
continue
|
|
64
|
+
if (
|
|
65
|
+
not isinstance(numeric_item, (int, float))
|
|
66
|
+
or isinstance(numeric_item, bool)
|
|
67
|
+
or float(numeric_item) <= numeric_minimum
|
|
68
|
+
):
|
|
69
|
+
raise ValueError(
|
|
70
|
+
f"{numeric_name} must be greater than {numeric_minimum}"
|
|
71
|
+
)
|
|
72
|
+
normalized[numeric_name] = float(numeric_item)
|
|
73
|
+
output = normalized.get("output")
|
|
74
|
+
if output is not None and (
|
|
75
|
+
not isinstance(output, Mapping)
|
|
76
|
+
or not isinstance(output.get("bundle_uri"), str)
|
|
77
|
+
or not output["bundle_uri"]
|
|
78
|
+
):
|
|
79
|
+
raise ValueError("output.bundle_uri must be non-empty")
|
|
80
|
+
runtime = normalized.get("runtime")
|
|
81
|
+
if runtime is not None and not isinstance(runtime, Mapping):
|
|
82
|
+
raise ValueError("runtime must be a mapping")
|
|
83
|
+
return normalized
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class TabularInputValidator:
|
|
87
|
+
"""Validate one labeled train input without opening the data source."""
|
|
88
|
+
|
|
89
|
+
api_version = 1
|
|
90
|
+
schema_digest = "2" * 64
|
|
91
|
+
|
|
92
|
+
def validate(self, value: Mapping[str, Any]) -> Mapping[str, Any]:
|
|
93
|
+
bindings = value.get("bindings")
|
|
94
|
+
descriptors = value.get("descriptors")
|
|
95
|
+
if not isinstance(bindings, list) or not isinstance(descriptors, Mapping):
|
|
96
|
+
raise ValueError("tabular input requires bindings and descriptors")
|
|
97
|
+
roles = {
|
|
98
|
+
binding.get("name") for binding in bindings if isinstance(binding, Mapping)
|
|
99
|
+
}
|
|
100
|
+
if "train" not in roles or "train" not in descriptors:
|
|
101
|
+
raise ValueError("tabular input requires the train role")
|
|
102
|
+
return value
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class SklearnOutputValidator:
|
|
106
|
+
"""Require successful Bundle-backed fit output."""
|
|
107
|
+
|
|
108
|
+
api_version = 1
|
|
109
|
+
schema_digest = "3" * 64
|
|
110
|
+
|
|
111
|
+
def validate(self, value: Mapping[str, Any]) -> Mapping[str, Any]:
|
|
112
|
+
if value.get("status") != "succeeded":
|
|
113
|
+
raise ValueError("classical output requires successful execution")
|
|
114
|
+
outputs = value.get("outputs")
|
|
115
|
+
if not isinstance(outputs, Mapping) or not outputs.get("bundle_uri"):
|
|
116
|
+
raise ValueError("classical output requires bundle_uri")
|
|
117
|
+
return value
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class DistributedCoverageValidator:
|
|
121
|
+
"""Require complete model-internal distributed execution evidence."""
|
|
122
|
+
|
|
123
|
+
api_version = 1
|
|
124
|
+
schema_digest = "4" * 64
|
|
125
|
+
|
|
126
|
+
def validate(self, value: Mapping[str, Any]) -> Mapping[str, Any]:
|
|
127
|
+
if value.get("input_complete") is not True:
|
|
128
|
+
raise ValueError("classical distributed input coverage is incomplete")
|
|
129
|
+
if value.get("distributed") is not True:
|
|
130
|
+
raise ValueError("classical execution did not prove distributed training")
|
|
131
|
+
return value
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
__all__ = [
|
|
135
|
+
"ClassicalConfigValidator",
|
|
136
|
+
"DistributedCoverageValidator",
|
|
137
|
+
"SklearnOutputValidator",
|
|
138
|
+
"TabularInputValidator",
|
|
139
|
+
]
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
"""Official descriptors for distributed classical algorithms."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from tributo.algorithms import AlgorithmBuilder
|
|
6
|
+
from tributo.algorithms.api import (
|
|
7
|
+
ContractBinding,
|
|
8
|
+
ContractBindingSet,
|
|
9
|
+
DistributedExactness,
|
|
10
|
+
EnvironmentSpec,
|
|
11
|
+
ExecutionMode,
|
|
12
|
+
ExecutionProfile,
|
|
13
|
+
IterativeOptimizationPolicy,
|
|
14
|
+
JoblibEstimatorPolicy,
|
|
15
|
+
ParallelEnsemblePolicy,
|
|
16
|
+
QualifiedReference,
|
|
17
|
+
WorkerRange,
|
|
18
|
+
WorkerResources,
|
|
19
|
+
)
|
|
20
|
+
from tributo.training.algorithm_spec import AlgorithmSpec, Capability, ProblemType
|
|
21
|
+
|
|
22
|
+
_PACKAGE = "tributo-algorithms"
|
|
23
|
+
_VERSION = "0.1.0"
|
|
24
|
+
_COMPATIBILITY = ">=1.1"
|
|
25
|
+
_DEPENDENCIES = (
|
|
26
|
+
"numpy>=2,<3",
|
|
27
|
+
"onnx>=1.16",
|
|
28
|
+
"onnxruntime>=1.20",
|
|
29
|
+
"scikit-learn>=1.4,<2",
|
|
30
|
+
"skl2onnx>=1.17",
|
|
31
|
+
"tributo>=1.1",
|
|
32
|
+
f"tributo-algorithms[classical]=={_VERSION}",
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _binding(contract_id: str, digest: str, validator: str) -> ContractBinding:
|
|
37
|
+
return ContractBinding(
|
|
38
|
+
contract_id=contract_id,
|
|
39
|
+
schema_version=1,
|
|
40
|
+
schema_digest=digest * 64,
|
|
41
|
+
validator_ref=QualifiedReference.parse(
|
|
42
|
+
f"tributo_algorithms_classical.contracts:{validator}"
|
|
43
|
+
),
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _contracts(config_id: str, output_id: str) -> ContractBindingSet:
|
|
48
|
+
return ContractBindingSet(
|
|
49
|
+
config=_binding(config_id, "5", "ClassicalConfigValidator"),
|
|
50
|
+
input=_binding(
|
|
51
|
+
"tributo.official.tabular.labeled.v1",
|
|
52
|
+
"2",
|
|
53
|
+
"TabularInputValidator",
|
|
54
|
+
),
|
|
55
|
+
output=_binding(output_id, "3", "SklearnOutputValidator"),
|
|
56
|
+
coverage=_binding(
|
|
57
|
+
"tributo.official.distributed.coverage.v1",
|
|
58
|
+
"4",
|
|
59
|
+
"DistributedCoverageValidator",
|
|
60
|
+
),
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _spec(
|
|
65
|
+
name: str,
|
|
66
|
+
modes: tuple[ExecutionMode, ...],
|
|
67
|
+
config_id: str,
|
|
68
|
+
output_id: str,
|
|
69
|
+
) -> AlgorithmSpec:
|
|
70
|
+
return AlgorithmSpec(
|
|
71
|
+
name=name,
|
|
72
|
+
trainer_cls=None,
|
|
73
|
+
version="1.0.0",
|
|
74
|
+
default_config={},
|
|
75
|
+
supported_tasks=("fit",),
|
|
76
|
+
operations=("fit",),
|
|
77
|
+
problem_types=(
|
|
78
|
+
(ProblemType.REGRESSION,)
|
|
79
|
+
if name == "linear_regression"
|
|
80
|
+
else (ProblemType.BINARY_CLASSIFICATION,)
|
|
81
|
+
if name == "logistic_regression"
|
|
82
|
+
else (
|
|
83
|
+
ProblemType.BINARY_CLASSIFICATION,
|
|
84
|
+
ProblemType.MULTI_CLASS_CLASSIFICATION,
|
|
85
|
+
ProblemType.REGRESSION,
|
|
86
|
+
)
|
|
87
|
+
),
|
|
88
|
+
capabilities=(
|
|
89
|
+
Capability.DISTRIBUTED,
|
|
90
|
+
Capability.EXPORTABLE,
|
|
91
|
+
Capability.TUNABLE,
|
|
92
|
+
),
|
|
93
|
+
learning_paradigm="supervised",
|
|
94
|
+
model_family=(
|
|
95
|
+
"tree_ensemble"
|
|
96
|
+
if name in {"random_forest", "extra_trees"}
|
|
97
|
+
else "linear_model"
|
|
98
|
+
),
|
|
99
|
+
data_modalities=("tabular",),
|
|
100
|
+
lifecycle_kind="batch_fit",
|
|
101
|
+
allowed_execution_modes=tuple(mode.value for mode in modes),
|
|
102
|
+
config_contract_ref=config_id,
|
|
103
|
+
input_contract_ref="tributo.official.tabular.labeled.v1",
|
|
104
|
+
output_contract_ref=output_id,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
_ENVIRONMENT = EnvironmentSpec(
|
|
109
|
+
environment_id="tributo.official.classical.v1",
|
|
110
|
+
dependencies=_DEPENDENCIES,
|
|
111
|
+
)
|
|
112
|
+
_PROFILES = (ExecutionProfile.LOCAL, ExecutionProfile.CLUSTER)
|
|
113
|
+
_RESOURCES = WorkerResources(num_cpus=1)
|
|
114
|
+
_EXPORTER = "tributo_algorithms_classical.exporter:export_sklearn_model"
|
|
115
|
+
|
|
116
|
+
RANDOM_FOREST_JOBLIB_DESCRIPTOR = AlgorithmBuilder.from_joblib_estimator_recipe(
|
|
117
|
+
spec=_spec(
|
|
118
|
+
"random_forest",
|
|
119
|
+
(ExecutionMode.JOBLIB_ESTIMATOR, ExecutionMode.PARALLEL_ENSEMBLE),
|
|
120
|
+
"tributo.official.random_forest.config.v1",
|
|
121
|
+
"tributo.official.random_forest.onnx.v1",
|
|
122
|
+
),
|
|
123
|
+
implementation_id="tributo.official.random_forest.joblib",
|
|
124
|
+
implementation_version="1.0.0",
|
|
125
|
+
recipe=("tributo_algorithms_classical.random_forest:RandomForestJoblibRecipe"),
|
|
126
|
+
environment=_ENVIRONMENT,
|
|
127
|
+
allowed_config_keys=(
|
|
128
|
+
"class_weight",
|
|
129
|
+
"max_depth",
|
|
130
|
+
"max_features",
|
|
131
|
+
"n_estimators",
|
|
132
|
+
"output",
|
|
133
|
+
"seed",
|
|
134
|
+
"task",
|
|
135
|
+
"task_count",
|
|
136
|
+
),
|
|
137
|
+
supported_worker_range=WorkerRange(2, 256),
|
|
138
|
+
supported_execution_profiles=_PROFILES,
|
|
139
|
+
resources_per_worker=_RESOURCES,
|
|
140
|
+
package_name=_PACKAGE,
|
|
141
|
+
package_version=_VERSION,
|
|
142
|
+
tributo_version_spec=_COMPATIBILITY,
|
|
143
|
+
policy=JoblibEstimatorPolicy(max_materialized_rows=1_000_000),
|
|
144
|
+
exporter=_EXPORTER,
|
|
145
|
+
flavor_id="onnx-runtime-v1",
|
|
146
|
+
contract_bindings=_contracts(
|
|
147
|
+
"tributo.official.random_forest.config.v1",
|
|
148
|
+
"tributo.official.random_forest.onnx.v1",
|
|
149
|
+
),
|
|
150
|
+
descriptor_api_version=2,
|
|
151
|
+
is_default=False,
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
RANDOM_FOREST_NATIVE_DESCRIPTOR = AlgorithmBuilder.from_parallel_ensemble(
|
|
155
|
+
spec=_spec(
|
|
156
|
+
"random_forest",
|
|
157
|
+
(ExecutionMode.JOBLIB_ESTIMATOR, ExecutionMode.PARALLEL_ENSEMBLE),
|
|
158
|
+
"tributo.official.random_forest.config.v1",
|
|
159
|
+
"tributo.official.random_forest.onnx.v1",
|
|
160
|
+
),
|
|
161
|
+
implementation_id="tributo.official.random_forest.native_ensemble",
|
|
162
|
+
implementation_version="1.0.0",
|
|
163
|
+
algorithm="tributo_algorithms_classical.random_forest:RandomForestEnsemble",
|
|
164
|
+
environment=_ENVIRONMENT,
|
|
165
|
+
allowed_config_keys=(
|
|
166
|
+
"class_weight",
|
|
167
|
+
"max_depth",
|
|
168
|
+
"max_features",
|
|
169
|
+
"n_estimators",
|
|
170
|
+
"output",
|
|
171
|
+
"runtime",
|
|
172
|
+
"seed",
|
|
173
|
+
"task",
|
|
174
|
+
"unit_count",
|
|
175
|
+
),
|
|
176
|
+
supported_worker_range=WorkerRange(2, 256),
|
|
177
|
+
supported_execution_profiles=_PROFILES,
|
|
178
|
+
resources_per_worker=_RESOURCES,
|
|
179
|
+
package_name=_PACKAGE,
|
|
180
|
+
package_version=_VERSION,
|
|
181
|
+
tributo_version_spec=_COMPATIBILITY,
|
|
182
|
+
policy=ParallelEnsemblePolicy(
|
|
183
|
+
max_units=4096,
|
|
184
|
+
max_retries=1,
|
|
185
|
+
exactness=DistributedExactness.CONDITIONAL,
|
|
186
|
+
),
|
|
187
|
+
exporter=_EXPORTER,
|
|
188
|
+
flavor_id="onnx-runtime-v1",
|
|
189
|
+
contract_bindings=_contracts(
|
|
190
|
+
"tributo.official.random_forest.config.v1",
|
|
191
|
+
"tributo.official.random_forest.onnx.v1",
|
|
192
|
+
),
|
|
193
|
+
descriptor_api_version=2,
|
|
194
|
+
is_default=True,
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
EXTRA_TREES_JOBLIB_DESCRIPTOR = AlgorithmBuilder.from_joblib_estimator_recipe(
|
|
198
|
+
spec=_spec(
|
|
199
|
+
"extra_trees",
|
|
200
|
+
(ExecutionMode.JOBLIB_ESTIMATOR, ExecutionMode.PARALLEL_ENSEMBLE),
|
|
201
|
+
"tributo.official.extra_trees.config.v1",
|
|
202
|
+
"tributo.official.extra_trees.onnx.v1",
|
|
203
|
+
),
|
|
204
|
+
implementation_id="tributo.official.extra_trees.joblib",
|
|
205
|
+
implementation_version="1.0.0",
|
|
206
|
+
recipe="tributo_algorithms_classical.random_forest:ExtraTreesJoblibRecipe",
|
|
207
|
+
environment=_ENVIRONMENT,
|
|
208
|
+
allowed_config_keys=(
|
|
209
|
+
"class_weight",
|
|
210
|
+
"max_depth",
|
|
211
|
+
"max_features",
|
|
212
|
+
"n_estimators",
|
|
213
|
+
"output",
|
|
214
|
+
"seed",
|
|
215
|
+
"task",
|
|
216
|
+
"task_count",
|
|
217
|
+
),
|
|
218
|
+
supported_worker_range=WorkerRange(2, 256),
|
|
219
|
+
supported_execution_profiles=_PROFILES,
|
|
220
|
+
resources_per_worker=_RESOURCES,
|
|
221
|
+
package_name=_PACKAGE,
|
|
222
|
+
package_version=_VERSION,
|
|
223
|
+
tributo_version_spec=_COMPATIBILITY,
|
|
224
|
+
policy=JoblibEstimatorPolicy(max_materialized_rows=1_000_000),
|
|
225
|
+
exporter=_EXPORTER,
|
|
226
|
+
flavor_id="onnx-runtime-v1",
|
|
227
|
+
contract_bindings=_contracts(
|
|
228
|
+
"tributo.official.extra_trees.config.v1",
|
|
229
|
+
"tributo.official.extra_trees.onnx.v1",
|
|
230
|
+
),
|
|
231
|
+
descriptor_api_version=2,
|
|
232
|
+
is_default=False,
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
EXTRA_TREES_NATIVE_DESCRIPTOR = AlgorithmBuilder.from_parallel_ensemble(
|
|
236
|
+
spec=_spec(
|
|
237
|
+
"extra_trees",
|
|
238
|
+
(ExecutionMode.JOBLIB_ESTIMATOR, ExecutionMode.PARALLEL_ENSEMBLE),
|
|
239
|
+
"tributo.official.extra_trees.config.v1",
|
|
240
|
+
"tributo.official.extra_trees.onnx.v1",
|
|
241
|
+
),
|
|
242
|
+
implementation_id="tributo.official.extra_trees.native_ensemble",
|
|
243
|
+
implementation_version="1.0.0",
|
|
244
|
+
algorithm="tributo_algorithms_classical.random_forest:ExtraTreesEnsemble",
|
|
245
|
+
environment=_ENVIRONMENT,
|
|
246
|
+
allowed_config_keys=(
|
|
247
|
+
"class_weight",
|
|
248
|
+
"max_depth",
|
|
249
|
+
"max_features",
|
|
250
|
+
"n_estimators",
|
|
251
|
+
"output",
|
|
252
|
+
"runtime",
|
|
253
|
+
"seed",
|
|
254
|
+
"task",
|
|
255
|
+
"unit_count",
|
|
256
|
+
),
|
|
257
|
+
supported_worker_range=WorkerRange(2, 256),
|
|
258
|
+
supported_execution_profiles=_PROFILES,
|
|
259
|
+
resources_per_worker=_RESOURCES,
|
|
260
|
+
package_name=_PACKAGE,
|
|
261
|
+
package_version=_VERSION,
|
|
262
|
+
tributo_version_spec=_COMPATIBILITY,
|
|
263
|
+
policy=ParallelEnsemblePolicy(
|
|
264
|
+
max_units=4096,
|
|
265
|
+
max_retries=1,
|
|
266
|
+
exactness=DistributedExactness.CONDITIONAL,
|
|
267
|
+
),
|
|
268
|
+
exporter=_EXPORTER,
|
|
269
|
+
flavor_id="onnx-runtime-v1",
|
|
270
|
+
contract_bindings=_contracts(
|
|
271
|
+
"tributo.official.extra_trees.config.v1",
|
|
272
|
+
"tributo.official.extra_trees.onnx.v1",
|
|
273
|
+
),
|
|
274
|
+
descriptor_api_version=2,
|
|
275
|
+
is_default=True,
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
LOGISTIC_REGRESSION_DESCRIPTOR = AlgorithmBuilder.from_iterative_optimization(
|
|
279
|
+
spec=_spec(
|
|
280
|
+
"logistic_regression",
|
|
281
|
+
(ExecutionMode.ITERATIVE_OPTIMIZATION,),
|
|
282
|
+
"tributo.official.logistic_regression.config.v1",
|
|
283
|
+
"tributo.official.logistic_regression.onnx.v1",
|
|
284
|
+
),
|
|
285
|
+
implementation_id="tributo.official.logistic_regression.binary_l2",
|
|
286
|
+
implementation_version="1.0.0",
|
|
287
|
+
algorithm=(
|
|
288
|
+
"tributo_algorithms_classical.logistic_regression:BinaryL2LogisticRegression"
|
|
289
|
+
),
|
|
290
|
+
environment=_ENVIRONMENT,
|
|
291
|
+
allowed_config_keys=(
|
|
292
|
+
"C",
|
|
293
|
+
"feature_count",
|
|
294
|
+
"learning_rate",
|
|
295
|
+
"output",
|
|
296
|
+
"runtime",
|
|
297
|
+
"seed",
|
|
298
|
+
"tolerance",
|
|
299
|
+
),
|
|
300
|
+
supported_worker_range=WorkerRange(2, 256),
|
|
301
|
+
supported_execution_profiles=_PROFILES,
|
|
302
|
+
resources_per_worker=_RESOURCES,
|
|
303
|
+
package_name=_PACKAGE,
|
|
304
|
+
package_version=_VERSION,
|
|
305
|
+
tributo_version_spec=_COMPATIBILITY,
|
|
306
|
+
policy=IterativeOptimizationPolicy(
|
|
307
|
+
max_rounds=200,
|
|
308
|
+
checkpoint_interval=1,
|
|
309
|
+
max_retries=1,
|
|
310
|
+
),
|
|
311
|
+
exporter=_EXPORTER,
|
|
312
|
+
flavor_id="onnx-runtime-v1",
|
|
313
|
+
contract_bindings=_contracts(
|
|
314
|
+
"tributo.official.logistic_regression.config.v1",
|
|
315
|
+
"tributo.official.logistic_regression.onnx.v1",
|
|
316
|
+
),
|
|
317
|
+
descriptor_api_version=2,
|
|
318
|
+
is_default=True,
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
LINEAR_REGRESSION_DESCRIPTOR = AlgorithmBuilder.from_iterative_optimization(
|
|
322
|
+
spec=_spec(
|
|
323
|
+
"linear_regression",
|
|
324
|
+
(ExecutionMode.ITERATIVE_OPTIMIZATION,),
|
|
325
|
+
"tributo.official.linear_regression.config.v1",
|
|
326
|
+
"tributo.official.linear_regression.onnx.v1",
|
|
327
|
+
),
|
|
328
|
+
implementation_id="tributo.official.linear_regression.squared_l2",
|
|
329
|
+
implementation_version="1.0.0",
|
|
330
|
+
algorithm=(
|
|
331
|
+
"tributo_algorithms_classical.linear_regression:DistributedLinearRegression"
|
|
332
|
+
),
|
|
333
|
+
environment=_ENVIRONMENT,
|
|
334
|
+
allowed_config_keys=(
|
|
335
|
+
"feature_count",
|
|
336
|
+
"learning_rate",
|
|
337
|
+
"output",
|
|
338
|
+
"runtime",
|
|
339
|
+
"seed",
|
|
340
|
+
"tolerance",
|
|
341
|
+
),
|
|
342
|
+
supported_worker_range=WorkerRange(2, 256),
|
|
343
|
+
supported_execution_profiles=_PROFILES,
|
|
344
|
+
resources_per_worker=_RESOURCES,
|
|
345
|
+
package_name=_PACKAGE,
|
|
346
|
+
package_version=_VERSION,
|
|
347
|
+
tributo_version_spec=_COMPATIBILITY,
|
|
348
|
+
policy=IterativeOptimizationPolicy(
|
|
349
|
+
max_rounds=200,
|
|
350
|
+
checkpoint_interval=1,
|
|
351
|
+
max_retries=1,
|
|
352
|
+
),
|
|
353
|
+
exporter=_EXPORTER,
|
|
354
|
+
flavor_id="onnx-runtime-v1",
|
|
355
|
+
contract_bindings=_contracts(
|
|
356
|
+
"tributo.official.linear_regression.config.v1",
|
|
357
|
+
"tributo.official.linear_regression.onnx.v1",
|
|
358
|
+
),
|
|
359
|
+
descriptor_api_version=2,
|
|
360
|
+
is_default=True,
|
|
361
|
+
)
|
|
362
|
+
|
|
363
|
+
__all__ = [
|
|
364
|
+
"EXTRA_TREES_JOBLIB_DESCRIPTOR",
|
|
365
|
+
"EXTRA_TREES_NATIVE_DESCRIPTOR",
|
|
366
|
+
"LINEAR_REGRESSION_DESCRIPTOR",
|
|
367
|
+
"LOGISTIC_REGRESSION_DESCRIPTOR",
|
|
368
|
+
"RANDOM_FOREST_JOBLIB_DESCRIPTOR",
|
|
369
|
+
"RANDOM_FOREST_NATIVE_DESCRIPTOR",
|
|
370
|
+
]
|