Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/optuna #128

Merged
merged 77 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
8019153
define interface
voorhs Feb 1, 2025
90af5ce
basic ho iterator
voorhs Feb 1, 2025
8601678
move obtaining data for train from node optimizer to modules themselves
voorhs Feb 1, 2025
ccf6e41
stage progress
voorhs Feb 2, 2025
a307fee
implement cv iterator
voorhs Feb 2, 2025
8649e14
minor bug fix
voorhs Feb 5, 2025
a300f19
implement cv iterator for decision node
voorhs Feb 5, 2025
815561f
move cv iteration to base module definition
voorhs Feb 5, 2025
f3ef812
implement cv iterator for embedding node
voorhs Feb 5, 2025
593744a
add training to `score_ho` of each node
voorhs Feb 5, 2025
6fa0b24
properly define base module
voorhs Feb 5, 2025
278855c
fix codestyle
voorhs Feb 5, 2025
bf9074f
remove regexp node
voorhs Feb 5, 2025
8f30ec9
remove regexp validator
voorhs Feb 5, 2025
b2c8986
fix typing problems (except `DataHandler._split_cv`)
voorhs Feb 5, 2025
e24bde4
add ingore oos decorator
voorhs Feb 5, 2025
a74e5dd
fix codestyle
voorhs Feb 5, 2025
8b94741
fix typing
voorhs Feb 5, 2025
cfb2f25
add oos handling to cv iterator
voorhs Feb 5, 2025
e1d41a9
remove `DataHandler.dump()`
voorhs Feb 5, 2025
9668dc6
minor bug fix
voorhs Feb 5, 2025
61130bb
implement splitting to cv folds
voorhs Feb 5, 2025
3084718
fix codestyle
voorhs Feb 5, 2025
ae13834
remove regex tests
voorhs Feb 5, 2025
4c49809
bug fix
voorhs Feb 5, 2025
2f8642f
bug fix
voorhs Feb 5, 2025
13e63d1
update tests
voorhs Feb 5, 2025
5ac89db
fix typing
voorhs Feb 5, 2025
d4bf50f
big fix
voorhs Feb 5, 2025
74dcb98
basic test on cv folding
voorhs Feb 5, 2025
f13592b
add tests for metrics to ignore oos samples
voorhs Feb 5, 2025
080d7cc
add tests for cv iterator
voorhs Feb 5, 2025
bee4e73
fix codestyle
voorhs Feb 5, 2025
75e47c8
minor bug fix
voorhs Feb 6, 2025
48fb2ff
pull dev
voorhs Feb 6, 2025
f64ee3b
fix codestyle
voorhs Feb 6, 2025
39e69d8
add test for cv
voorhs Feb 6, 2025
ef11594
bug fix
voorhs Feb 6, 2025
bffffb1
implement cv iterator for description scorer
voorhs Feb 6, 2025
9602af8
refactor cv iterator for description node
voorhs Feb 6, 2025
ea39b36
fix typing
voorhs Feb 6, 2025
903dfa7
add cache cleaning before refitting
voorhs Feb 6, 2025
5cbf83e
bug fix
voorhs Feb 6, 2025
89c6406
implement refitting the whole pipeline with all train data
voorhs Feb 6, 2025
7f4d7ac
fix typing
voorhs Feb 6, 2025
d22b2db
bug fix
voorhs Feb 6, 2025
82e8c3f
fix typing
voorhs Feb 6, 2025
4f2b4c8
respond to samoed
voorhs Feb 7, 2025
46ea859
create `ValidationType` in `autointent.custom_types`
voorhs Feb 10, 2025
79e25bf
fix docstring
voorhs Feb 10, 2025
2311244
properly expose `n_folds` argument
voorhs Feb 10, 2025
2929423
implement `_fit_bayes`
voorhs Feb 10, 2025
43b1910
add typing to param spaces
voorhs Feb 11, 2025
a0ef81c
minor bug fix
voorhs Feb 11, 2025
b2f4dc2
minor bug fix
voorhs Feb 11, 2025
203b5ee
fix codestyle
voorhs Feb 11, 2025
72649ba
resolve conflicts
voorhs Feb 11, 2025
a78a60c
add tuning selection to pipeline
voorhs Feb 11, 2025
5ed5ac3
add test on bayes
voorhs Feb 11, 2025
364e60a
disable search space validation for now
voorhs Feb 11, 2025
38475e6
fix codestyle
voorhs Feb 11, 2025
9f71c0b
remove `ParamSpaceCat` (it's redundant)
voorhs Feb 11, 2025
0c5eef6
move to optuna entirely
voorhs Feb 12, 2025
2fb6ac0
refactor yaml format a little bit
voorhs Feb 12, 2025
5072810
add test for random sampler
voorhs Feb 12, 2025
b6669ec
rename some variables
voorhs Feb 12, 2025
2b4ac0d
add config validation for optuna (#132)
Samoed Feb 14, 2025
1459b60
return CI config back to normal
voorhs Feb 14, 2025
f7798a1
fix default value for step in `ParamSpaceFloat`
voorhs Feb 14, 2025
671f38a
resolve conflicts
voorhs Feb 14, 2025
2adb593
update schema
voorhs Feb 14, 2025
c32c356
update callback test
voorhs Feb 14, 2025
70b4079
change CI config
voorhs Feb 14, 2025
d34a4c0
update search space configs for testing
voorhs Feb 14, 2025
d8d7852
enable validation back
voorhs Feb 14, 2025
f727bf4
remove TunableDecision from search spaces
voorhs Feb 14, 2025
fa006f8
upd schema
Samoed Feb 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions autointent/_pipeline/_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

from autointent import Context, Dataset
from autointent.configs import CrossEncoderConfig, EmbedderConfig, InferenceNodeConfig, LoggingConfig, VectorIndexConfig
from autointent.custom_types import ListOfGenericLabels, NodeType, ValidationScheme
from autointent.custom_types import ListOfGenericLabels, NodeType, TuningType, ValidationScheme
from autointent.metrics import PREDICTION_METRICS_MULTILABEL
from autointent.nodes import InferenceNode, NodeOptimizer
from autointent.nodes.schemes import OptimizationConfig
from autointent.utils import load_default_search_space, load_search_space

from ._schemas import InferencePipelineOutput, InferencePipelineUtteranceOutput
Expand Down Expand Up @@ -77,7 +76,7 @@ def from_search_space(cls, search_space: list[dict[str, Any]] | Path | str, seed
"""
if isinstance(search_space, Path | str):
search_space = load_search_space(search_space)
validated_search_space = OptimizationConfig(search_space).model_dump() # type: ignore[arg-type]
validated_search_space = search_space # OptimizationConfig(search_space).model_dump() # type: ignore[arg-type]
nodes = [NodeOptimizer(**node) for node in validated_search_space]
return cls(nodes=nodes, seed=seed)

Expand All @@ -93,7 +92,7 @@ def default_optimizer(cls, multilabel: bool, seed: int = 42) -> "Pipeline":
"""
return cls.from_search_space(search_space=load_default_search_space(multilabel), seed=seed)

def _fit(self, context: Context) -> None:
def _fit(self, context: Context, tuning: TuningType = "brute") -> None:
"""
Optimize the pipeline.

Expand All @@ -108,7 +107,7 @@ def _fit(self, context: Context) -> None:
for node_type in NodeType:
node_optimizer = self.nodes.get(node_type, None)
if node_optimizer is not None:
node_optimizer.fit(context) # type: ignore[union-attr]
node_optimizer.fit(context, tuning) # type: ignore[union-attr]
if not context.vector_index_config.save_db:
self._logger.info("removing vector database from file system...")
# TODO clear cache from appdirs
Expand All @@ -123,7 +122,12 @@ def _is_inference(self) -> bool:
return isinstance(self.nodes[NodeType.scoring], InferenceNode)

def fit(
self, dataset: Dataset, scheme: ValidationScheme = "ho", n_folds: int = 3, refit_after: bool = False
self,
dataset: Dataset,
scheme: ValidationScheme = "ho",
n_folds: int = 3,
refit_after: bool = False,
tuning: TuningType = "brute",
) -> Context:
"""
Optimize the pipeline from dataset.
Expand All @@ -141,7 +145,7 @@ def fit(
context.configure_vector_index(self.vector_index_config, self.embedder_config)
context.configure_cross_encoder(self.cross_encoder_config)
self.validate_modules(dataset)
self._fit(context)
self._fit(context, tuning)

if context.is_ram_to_clear():
nodes_configs = context.optimization_info.get_inference_nodes_config()
Expand Down
3 changes: 2 additions & 1 deletion autointent/configs/_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pydantic import BaseModel, Field

from autointent.custom_types import ValidationScheme
from autointent.custom_types import TuningType, ValidationScheme

from ._name import get_run_name

Expand All @@ -25,6 +25,7 @@ class TaskConfig(BaseModel):

search_space_path: Path | None = None
"""Path to the search space configuration file. If None, the default search space will be used"""
sampler: TuningType = "brute"


class LoggingConfig(BaseModel):
Expand Down
1 change: 1 addition & 0 deletions autointent/custom_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ class Split:
INTENTS = "intents"


TuningType = Literal["brute", "bayes"]
voorhs marked this conversation as resolved.
Show resolved Hide resolved
ValidationScheme = Literal["ho", "cv"]
124 changes: 117 additions & 7 deletions autointent/nodes/_optimization/_node_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,43 @@
import itertools as it
import logging
from copy import deepcopy
from functools import partial
from pathlib import Path
from typing import Any
from typing import Any, Literal, TypedDict

import optuna
import torch
from optuna.trial import Trial

from autointent import Dataset
from autointent.context import Context
from autointent.custom_types import NodeType
from autointent.custom_types import NodeType, TuningType
from autointent.nodes._nodes_info import NODES_INFO


class ParamSpaceCat(TypedDict):
choices: list[Any]


class ParamSpaceInt(TypedDict, total=False):
low: int
high: int
step: int
log: bool


class ParamSpaceFloat(TypedDict, total=False):
low: float
high: float
step: float
log: bool


class ParamSpace(TypedDict):
type: Literal["cat", "int", "float"]
content: ParamSpaceCat | ParamSpaceInt | ParamSpaceFloat
voorhs marked this conversation as resolved.
Show resolved Hide resolved


class NodeOptimizer:
"""Node optimizer class."""

Expand Down Expand Up @@ -43,30 +69,42 @@ def __init__(
self.modules_search_spaces = search_space
self._logger = logging.getLogger(__name__) # TODO solve duplicate logging messages problem

def fit(self, context: Context) -> None:
def fit(self, context: Context, tuning: TuningType = "brute") -> None:
"""
Fit the node optimizer.

:param context: Context
"""
self._logger.info("starting %s node optimization...", self.node_info.node_type)

if tuning == "brute":
self._fit_brute(context)
elif tuning == "bayes":
self._fit_bayes(context)
else:
msg = f"Unexepected tuning type: {tuning}"
raise ValueError(msg)

self._logger.info("%s node optimization is finished!", self.node_info.node_type)

def _fit_brute(self, context: Context) -> None:
for search_space in deepcopy(self.modules_search_spaces):
module_name = search_space.pop("module_name")

for j_combination, params_combination in enumerate(it.product(*search_space.values())):
module_kwargs = dict(zip(search_space.keys(), params_combination, strict=False))

self._logger.debug("initializing %s module...", module_name)
context.callback_handler.start_module(
module_name=module_name, num=j_combination, module_kwargs=module_kwargs
)
module = self.node_info.modules_available[module_name].from_context(context, **module_kwargs)

embedder_name = module.get_embedder_name()
if embedder_name is not None:
module_kwargs["embedder_name"] = embedder_name

context.callback_handler.start_module(
module_name=module_name, num=j_combination, module_kwargs=module_kwargs
)

self._logger.debug("scoring %s module...", module_name)
metrics_score = module.score(context, metrics=self.metrics)
metric_value = metrics_score[self.target_metric]
Expand Down Expand Up @@ -98,7 +136,79 @@ def fit(self, context: Context) -> None:
gc.collect()
torch.cuda.empty_cache()

self._logger.info("%s node optimization is finished!", self.node_info.node_type)
def _fit_bayes(self, context: Context, seed: int = 42, n_trials: int = 10) -> None:
for search_space in deepcopy(self.modules_search_spaces):
self._counter = 0
study = optuna.create_study(direction="maximize", sampler=optuna.samplers.TPESampler(seed=seed))
optuna.logging.set_verbosity(optuna.logging.WARNING)
module_name = search_space.pop("module_name")
obj = partial(self.objective, module_name=module_name, search_space=search_space, context=context)
study.optimize(obj, n_trials=n_trials)

def objective(
self, trial: Trial, module_name: str, search_space: dict[str, ParamSpace | list[Any]], context: Context
) -> float:
config = self.suggest(trial, search_space)

self._logger.debug("initializing %s module...", module_name)
module = self.node_info.modules_available[module_name].from_context(context, **config)

embedder_name = module.get_embedder_name()
if embedder_name is not None:
config["embedder_name"] = embedder_name

context.callback_handler.start_module(module_name=module_name, num=self._counter, module_kwargs=config)

self._logger.debug("scoring %s module...", module_name)
all_metrics = module.score(context, metrics=self.metrics)
target_metric = all_metrics[self.target_metric]

context.callback_handler.log_metrics(all_metrics)
context.callback_handler.end_module()

dump_dir = context.get_dump_dir()

if dump_dir is not None:
module_dump_dir = self.get_module_dump_dir(dump_dir, module_name, self._counter)
module.dump(module_dump_dir)
else:
module_dump_dir = None

context.optimization_info.log_module_optimization(
self.node_info.node_type,
module_name,
config,
target_metric,
self.target_metric,
module.get_assets(), # retriever name / scores / predictions
module_dump_dir,
module=module if not context.is_ram_to_clear() else None,
)

if context.is_ram_to_clear():
module.clear_cache()
gc.collect()
torch.cuda.empty_cache()

self._counter += 1

return target_metric

def suggest(self, trial: Trial, search_space: dict[str, ParamSpace | list[Any]]) -> dict[str, Any]:
res: dict[str, Any] = {}
for param_name, param_space in search_space.items():
if isinstance(param_space, list):
res[param_name] = trial.suggest_categorical(param_name, choices=param_space)
elif param_space["type"] == "cat":
res[param_name] = trial.suggest_categorical(param_name, **param_space["content"])
elif param_space["type"] == "int":
res[param_name] = trial.suggest_int(param_name, **param_space["content"])
elif param_space["type"] == "float":
res[param_name] = trial.suggest_float(param_name, **param_space["content"])
else:
msg = f"Unsupported type of param search space: {param_space}"
raise TypeError(msg)
return res

def get_module_dump_dir(self, dump_dir: Path, module_name: str, j_combination: int) -> str:
"""
Expand Down
32 changes: 32 additions & 0 deletions tests/assets/configs/bayes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
- node_type: embedding
target_metric: retrieval_hit_rate
search_space:
- module_name: retrieval
k: [10]
embedder_name:
- sentence-transformers/all-MiniLM-L6-v2
- avsolatorio/GIST-small-Embedding-v0
- node_type: scoring
target_metric: scoring_roc_auc
search_space:
- module_name: knn
k:
type: "int"
content:
low: 5
high: 10
step: 1
voorhs marked this conversation as resolved.
Show resolved Hide resolved
weights: [uniform, distance, closest]
- module_name: linear
- node_type: decision
target_metric: decision_accuracy
search_space:
- module_name: threshold
thresh:
type: float
content:
low: 0.1
high: 0.9
voorhs marked this conversation as resolved.
Show resolved Hide resolved
- module_name: tunable
- module_name: argmax
- module_name: jinoos
voorhs marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def dataset_unsplitted():
return Dataset.from_json(path)


TaskType = Literal["multiclass", "multilabel", "description"]
TaskType = Literal["multiclass", "multilabel", "description", "bayes"]


def get_search_space_path(task_type: TaskType):
Expand Down
14 changes: 12 additions & 2 deletions tests/pipeline/test_optimization.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
from typing import Literal

import pytest

Expand All @@ -11,7 +10,18 @@
)
from tests.conftest import get_search_space, setup_environment

TaskType = Literal["multiclass", "multilabel", "description"]

def test_bayes(dataset):
project_dir = setup_environment()
search_space = get_search_space("bayes")

pipeline_optimizer = Pipeline.from_search_space(search_space)

pipeline_optimizer.set_config(LoggingConfig(project_dir=project_dir, dump_modules=True, clear_ram=True))
pipeline_optimizer.set_config(VectorIndexConfig())
pipeline_optimizer.set_config(EmbedderConfig(batch_size=16, max_length=32, device="cpu"))

pipeline_optimizer.fit(dataset, scheme="cv", refit_after=True, tuning="bayes")


@pytest.mark.parametrize(
Expand Down
Loading