Spaces:
Sleeping
Sleeping
Commit
·
8c7aba9
1
Parent(s):
9910823
maj pour sauvegarder le model
Browse files- src/base_trainer.py +2 -1
- src/main.py +0 -11
- src/mlflow_integration/mlflow_decorator.py +18 -4
- src/utilities/cuml_pyfunc_wrapper.py +30 -12
src/base_trainer.py
CHANGED
@@ -6,6 +6,7 @@ from abc import ABC, abstractmethod
|
|
6 |
from typing import Union, Optional
|
7 |
import cupy as cp
|
8 |
from scipy.sparse import csr_matrix
|
|
|
9 |
|
10 |
from config import Config
|
11 |
from interfaces.metrics_calculator import MetricsCalculator
|
@@ -116,7 +117,7 @@ class BaseTrainer(ABC):
|
|
116 |
Implementé ici en tant que méthode non-abstraite, mais la logique de logging
|
117 |
devrait être assurée dans l'environnement MLflow approprié.
|
118 |
"""
|
119 |
-
|
120 |
# Logue les paramètres du config.model
|
121 |
if self.config.model.params:
|
122 |
mlflow.log_params(self.config.model.params)
|
|
|
6 |
from typing import Union, Optional
|
7 |
import cupy as cp
|
8 |
from scipy.sparse import csr_matrix
|
9 |
+
import mlflow
|
10 |
|
11 |
from config import Config
|
12 |
from interfaces.metrics_calculator import MetricsCalculator
|
|
|
117 |
Implementé ici en tant que méthode non-abstraite, mais la logique de logging
|
118 |
devrait être assurée dans l'environnement MLflow approprié.
|
119 |
"""
|
120 |
+
|
121 |
# Logue les paramètres du config.model
|
122 |
if self.config.model.params:
|
123 |
mlflow.log_params(self.config.model.params)
|
src/main.py
CHANGED
@@ -7,26 +7,15 @@ import os
|
|
7 |
import logging
|
8 |
import hydra
|
9 |
from omegaconf import DictConfig, OmegaConf
|
10 |
-
|
11 |
-
# Import des trainers
|
12 |
from trainers.cuml.svm_trainer import SvmTrainer
|
13 |
from trainers.cuml.random_forest_trainer import RandomForestTrainer
|
14 |
from trainers.cuml.logistic_regression_trainer import LogisticRegressionTrainer
|
15 |
from trainers.cuml.linear_regression_trainer import LinearRegressionTrainer
|
16 |
from trainers.huggingface.huggingface_transformer_trainer import HuggingFaceTransformerTrainer
|
17 |
-
|
18 |
-
# Import des optimizers
|
19 |
from optimizers.optuna_optimizer import OptunaOptimizer
|
20 |
from optimizers.ray_tune_optimizer import RayTuneOptimizer
|
21 |
-
|
22 |
-
# Import du décorateur MLflow
|
23 |
from mlflow_integration.mlflow_decorator import MLflowDecorator
|
24 |
-
|
25 |
-
# Import de la configuration
|
26 |
from config import Config
|
27 |
-
import mlflow
|
28 |
-
|
29 |
-
# Configuration du logging
|
30 |
logger = logging.getLogger(__name__)
|
31 |
|
32 |
|
|
|
7 |
import logging
|
8 |
import hydra
|
9 |
from omegaconf import DictConfig, OmegaConf
|
|
|
|
|
10 |
from trainers.cuml.svm_trainer import SvmTrainer
|
11 |
from trainers.cuml.random_forest_trainer import RandomForestTrainer
|
12 |
from trainers.cuml.logistic_regression_trainer import LogisticRegressionTrainer
|
13 |
from trainers.cuml.linear_regression_trainer import LinearRegressionTrainer
|
14 |
from trainers.huggingface.huggingface_transformer_trainer import HuggingFaceTransformerTrainer
|
|
|
|
|
15 |
from optimizers.optuna_optimizer import OptunaOptimizer
|
16 |
from optimizers.ray_tune_optimizer import RayTuneOptimizer
|
|
|
|
|
17 |
from mlflow_integration.mlflow_decorator import MLflowDecorator
|
|
|
|
|
18 |
from config import Config
|
|
|
|
|
|
|
19 |
logger = logging.getLogger(__name__)
|
20 |
|
21 |
|
src/mlflow_integration/mlflow_decorator.py
CHANGED
@@ -4,6 +4,8 @@
|
|
4 |
|
5 |
from typing import Callable, Dict, Any
|
6 |
import mlflow
|
|
|
|
|
7 |
|
8 |
|
9 |
class MLflowDecorator:
|
@@ -66,12 +68,24 @@ class MLflowDecorator:
|
|
66 |
|
67 |
def _log_artifacts(self, artifacts: Dict[str, Any]) -> None:
|
68 |
"""
|
69 |
-
Logue dans MLflow différents artefacts
|
70 |
-
|
71 |
|
72 |
-
:param artifacts: Dictionnaire
|
73 |
"""
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
def _end_run(self, status: str) -> None:
|
77 |
"""
|
|
|
4 |
|
5 |
from typing import Callable, Dict, Any
|
6 |
import mlflow
|
7 |
+
from hydra.utils import get_original_cwd
|
8 |
+
import os
|
9 |
|
10 |
|
11 |
class MLflowDecorator:
|
|
|
68 |
|
69 |
def _log_artifacts(self, artifacts: Dict[str, Any]) -> None:
|
70 |
"""
|
71 |
+
Logue dans MLflow différents artefacts en utilisant mlflow.pyfunc.
|
72 |
+
Gère spécifiquement les modèles cuML avec leur exemple d'entrée.
|
73 |
|
74 |
+
:param artifacts: Dictionnaire contenant les artefacts à logger.
|
75 |
"""
|
76 |
+
if "model" in artifacts and "input_example" in artifacts:
|
77 |
+
mlflow.pyfunc.log_model(
|
78 |
+
artifact_path="model",
|
79 |
+
python_model=artifacts["model"],
|
80 |
+
artifacts=artifacts.get("components", {}),
|
81 |
+
input_example=artifacts["input_example"],
|
82 |
+
pip_requirements=os.path.join(get_original_cwd(), "requirements.txt")
|
83 |
+
)
|
84 |
+
else:
|
85 |
+
# Pour les autres types d'artefacts
|
86 |
+
for name, path in artifacts.items():
|
87 |
+
if isinstance(path, str):
|
88 |
+
mlflow.log_artifact(path, name)
|
89 |
|
90 |
def _end_run(self, status: str) -> None:
|
91 |
"""
|
src/utilities/cuml_pyfunc_wrapper.py
CHANGED
@@ -5,6 +5,10 @@
|
|
5 |
from typing import Any, Dict, Optional
|
6 |
import pandas as pd
|
7 |
import numpy as np
|
|
|
|
|
|
|
|
|
8 |
|
9 |
from interfaces.vectorizer import Vectorizer
|
10 |
|
@@ -33,24 +37,38 @@ class CuMLPyFuncWrapper:
|
|
33 |
:param context: Contexte de chargement contenant d'éventuelles informations
|
34 |
sur l'environnement ou l'emplacement d'artefacts.
|
35 |
"""
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
def predict(self, context: Dict[str, Any],
|
40 |
model_input: pd.DataFrame) -> np.ndarray:
|
41 |
"""
|
42 |
Fonction de prédiction, appelée par MLflow PyFunc.
|
43 |
-
Convertit
|
44 |
-
puis appelle le modèle cuML.
|
45 |
|
46 |
-
:param context: Contexte d'exécution éventuel
|
47 |
:param model_input: Données d'entrée sous forme de DataFrame pandas.
|
48 |
:return: Un vecteur numpy des prédictions.
|
49 |
"""
|
50 |
-
|
51 |
-
|
52 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
from typing import Any, Dict, Optional
|
6 |
import pandas as pd
|
7 |
import numpy as np
|
8 |
+
import cudf
|
9 |
+
import cupy as cp
|
10 |
+
import pickle
|
11 |
+
import os
|
12 |
|
13 |
from interfaces.vectorizer import Vectorizer
|
14 |
|
|
|
37 |
:param context: Contexte de chargement contenant d'éventuelles informations
|
38 |
sur l'environnement ou l'emplacement d'artefacts.
|
39 |
"""
|
40 |
+
|
41 |
+
|
42 |
+
# Charger le vectorizer depuis les artefacts
|
43 |
+
vectorizer_path = os.path.join(context.artifacts["vectorizer"])
|
44 |
+
with open(vectorizer_path, "rb") as f:
|
45 |
+
self.vectorizer = pickle.load(f)
|
46 |
+
|
47 |
+
# Charger le classifier depuis les artefacts
|
48 |
+
classifier_path = os.path.join(context.artifacts["classifier"])
|
49 |
+
with open(classifier_path, "rb") as f:
|
50 |
+
self.classifier = pickle.load(f)
|
51 |
|
52 |
def predict(self, context: Dict[str, Any],
|
53 |
model_input: pd.DataFrame) -> np.ndarray:
|
54 |
"""
|
55 |
Fonction de prédiction, appelée par MLflow PyFunc.
|
56 |
+
Convertit model_input en cudf, vectorise, puis appelle le modèle cuML.
|
|
|
57 |
|
58 |
+
:param context: Contexte d'exécution éventuel.
|
59 |
:param model_input: Données d'entrée sous forme de DataFrame pandas.
|
60 |
:return: Un vecteur numpy des prédictions.
|
61 |
"""
|
62 |
+
|
63 |
+
|
64 |
+
# Convertir le DataFrame pandas en DataFrame cuDF
|
65 |
+
cudf_input = cudf.DataFrame(model_input)
|
66 |
+
|
67 |
+
# Vectoriser les données d'entrée
|
68 |
+
X_vectorized = self.vectorizer.transform(cudf_input)
|
69 |
+
|
70 |
+
# Effectuer la prédiction avec le classifieur cuML
|
71 |
+
predictions_gpu = self.classifier.predict(X_vectorized)
|
72 |
+
|
73 |
+
# Convertir les prédictions GPU en array numpy pour MLflow
|
74 |
+
return cp.asnumpy(predictions_gpu)
|