-
Notifications
You must be signed in to change notification settings - Fork 512
Causal Regression Modeling #788
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| The column in the dataset containing the expected output. | ||
|
|
||
| For classification, this needs to be an integer column starting from zero containing the class label. | ||
| For classification, this needs to be an integer column starting from zero containing the class label, while for regression, it needs to be a float column. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
llm_studio/python_configs/text_causal_regression_modeling_config.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| import os | ||
| from dataclasses import dataclass, field | ||
| from typing import Any, Dict, List, Tuple | ||
|
|
||
| import llm_studio.src.datasets.text_causal_regression_ds | ||
| import llm_studio.src.plots.text_causal_classification_modeling_plots | ||
| from llm_studio.python_configs.base import DefaultConfig, DefaultConfigProblemBase | ||
| from llm_studio.python_configs.text_causal_classification_modeling_config import ( | ||
| ConfigNLPCausalClassificationAugmentation, | ||
| ConfigNLPCausalClassificationDataset, | ||
| ConfigNLPCausalClassificationLogging, | ||
| ConfigNLPCausalClassificationTokenizer, | ||
| ConfigNLPCausalClassificationTraining, | ||
| ) | ||
| from llm_studio.python_configs.text_causal_language_modeling_config import ( | ||
| ConfigNLPCausalLMArchitecture, | ||
| ConfigNLPCausalLMEnvironment, | ||
| ConfigNLPCausalLMLogging, | ||
| ) | ||
| from llm_studio.src import possible_values | ||
| from llm_studio.src.losses import text_causal_regression_modeling_losses | ||
| from llm_studio.src.metrics import text_causal_regression_modeling_metrics | ||
| from llm_studio.src.models import text_causal_regression_modeling_model | ||
| from llm_studio.src.utils.modeling_utils import generate_experiment_name | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigNLPCausalRegressionDataset(ConfigNLPCausalClassificationDataset): | ||
| dataset_class: Any = llm_studio.src.datasets.text_causal_regression_ds.CustomDataset | ||
| num_classes: int = 1 | ||
|
|
||
| def __post_init__(self): | ||
| self.prompt_column = ( | ||
| tuple( | ||
| self.prompt_column, | ||
| ) | ||
| if isinstance(self.prompt_column, str) | ||
| else tuple(self.prompt_column) | ||
| ) | ||
| super().__post_init__() | ||
|
|
||
| self._visibility["num_classes"] = -1 | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigNLPCausalRegressionTraining(ConfigNLPCausalClassificationTraining): | ||
| loss_class: Any = text_causal_regression_modeling_losses.Losses | ||
| loss_function: str = "MSELoss" | ||
|
|
||
| learning_rate: float = 0.0001 | ||
| differential_learning_rate_layers: Tuple[str, ...] = ("classification_head",) | ||
| differential_learning_rate: float = 0.00001 | ||
|
|
||
| def __post_init__(self): | ||
| super().__post_init__() | ||
| self._possible_values["loss_function"] = self.loss_class.names() | ||
|
|
||
| self._possible_values["differential_learning_rate_layers"] = ( | ||
| possible_values.String( | ||
| values=("backbone", "embed", "classification_head"), | ||
psinger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| allow_custom=False, | ||
| placeholder="Select optional layers...", | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigNLPCausalRegressionArchitecture(ConfigNLPCausalLMArchitecture): | ||
| model_class: Any = text_causal_regression_modeling_model.Model | ||
|
|
||
| def __post_init__(self): | ||
| super().__post_init__() | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigNLPCausalRegressionPrediction(DefaultConfig): | ||
| metric_class: Any = text_causal_regression_modeling_metrics.Metrics | ||
| metric: str = "MSE" | ||
| batch_size_inference: int = 0 | ||
|
|
||
| def __post_init__(self): | ||
| super().__post_init__() | ||
|
|
||
| self._possible_values["metric"] = self.metric_class.names() | ||
| self._possible_values["batch_size_inference"] = (0, 512, 1) | ||
|
|
||
| self._visibility["metric_class"] = -1 | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigNLPCausalRegressionEnvironment(ConfigNLPCausalLMEnvironment): | ||
| _model_card_template: str = "text_causal_regression_model_card_template.md" | ||
| _summary_card_template: str = ( | ||
| "text_causal_regression_experiment_summary_card_template.md" | ||
| ) | ||
|
|
||
| def __post_init__(self): | ||
| super().__post_init__() | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigNLPCausalRegressionLogging(ConfigNLPCausalLMLogging): | ||
| plots_class: Any = ( | ||
| llm_studio.src.plots.text_causal_classification_modeling_plots.Plots | ||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigProblemBase(DefaultConfigProblemBase): | ||
| output_directory: str = f"output/{os.path.basename(__file__).split('.')[0]}" | ||
| experiment_name: str = field(default_factory=generate_experiment_name) | ||
| llm_backbone: str = "h2oai/h2o-danube2-1.8b-chat" | ||
|
|
||
| dataset: ConfigNLPCausalRegressionDataset = field( | ||
| default_factory=ConfigNLPCausalRegressionDataset | ||
| ) | ||
| tokenizer: ConfigNLPCausalClassificationTokenizer = field( | ||
psinger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| default_factory=ConfigNLPCausalClassificationTokenizer | ||
| ) | ||
| architecture: ConfigNLPCausalRegressionArchitecture = field( | ||
| default_factory=ConfigNLPCausalRegressionArchitecture | ||
| ) | ||
| training: ConfigNLPCausalRegressionTraining = field( | ||
| default_factory=ConfigNLPCausalRegressionTraining | ||
| ) | ||
| augmentation: ConfigNLPCausalClassificationAugmentation = field( | ||
| default_factory=ConfigNLPCausalClassificationAugmentation | ||
| ) | ||
| prediction: ConfigNLPCausalRegressionPrediction = field( | ||
| default_factory=ConfigNLPCausalRegressionPrediction | ||
| ) | ||
| environment: ConfigNLPCausalRegressionEnvironment = field( | ||
| default_factory=ConfigNLPCausalRegressionEnvironment | ||
| ) | ||
| logging: ConfigNLPCausalClassificationLogging = field( | ||
psinger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| default_factory=ConfigNLPCausalClassificationLogging | ||
| ) | ||
|
|
||
| def __post_init__(self): | ||
| super().__post_init__() | ||
|
|
||
| self._visibility["output_directory"] = -1 | ||
|
|
||
| self._possible_values["llm_backbone"] = possible_values.String( | ||
| values=( | ||
| "h2oai/h2o-danube2-1.8b-base", | ||
| "h2oai/h2o-danube2-1.8b-chat", | ||
| "h2oai/h2ogpt-4096-llama2-7b", | ||
| "h2oai/h2ogpt-4096-llama2-7b-chat", | ||
| "h2oai/h2ogpt-4096-llama2-13b", | ||
| "h2oai/h2ogpt-4096-llama2-13b-chat", | ||
| "h2oai/h2ogpt-4096-llama2-70b", | ||
| "h2oai/h2ogpt-4096-llama2-70b-chat", | ||
| "tiiuae/falcon-7b", | ||
| "mistralai/Mistral-7B-v0.1", | ||
| "HuggingFaceH4/zephyr-7b-beta", | ||
| "google/gemma-2b", | ||
| "google/gemma-7b", | ||
| "stabilityai/stablelm-3b-4e1t", | ||
| "microsoft/phi-2", | ||
| "facebook/opt-125m", | ||
| ), | ||
| allow_custom=True, | ||
| ) | ||
|
|
||
| def check(self) -> Dict[str, List]: | ||
| errors: Dict[str, List] = {"title": [], "message": []} | ||
|
|
||
| if self.training.loss_function == "CrossEntropyLoss": | ||
| if self.dataset.num_classes == 1: | ||
| errors["title"] += ["CrossEntropyLoss requires num_classes > 1"] | ||
| errors["message"] += [ | ||
| "CrossEntropyLoss requires num_classes > 1, " | ||
| "but num_classes is set to 1." | ||
| ] | ||
| elif self.training.loss_function == "BinaryCrossEntropyLoss": | ||
| if self.dataset.num_classes != 1: | ||
| errors["title"] += ["BinaryCrossEntropyLoss requires num_classes == 1"] | ||
| errors["message"] += [ | ||
| "BinaryCrossEntropyLoss requires num_classes == 1, " | ||
| "but num_classes is set to {}.".format(self.dataset.num_classes) | ||
| ] | ||
| if self.dataset.parent_id_column not in ["None", None]: | ||
| errors["title"] += ["Parent ID column is not supported for classification"] | ||
| errors["message"] += [ | ||
| "Parent ID column is not supported for classification datasets." | ||
| ] | ||
|
|
||
| return errors | ||
psinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import logging | ||
| from typing import Any, Dict | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
|
|
||
| from llm_studio.src.datasets.text_causal_language_modeling_ds import ( | ||
| CustomDataset as TextCausalLanguageModelingCustomDataset, | ||
| ) | ||
| from llm_studio.src.utils.exceptions import LLMDataException | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class CustomDataset(TextCausalLanguageModelingCustomDataset): | ||
| def __init__(self, df: pd.DataFrame, cfg: Any, mode: str = "train"): | ||
| super().__init__(df=df, cfg=cfg, mode=mode) | ||
| self.answers_float = df[cfg.dataset.answer_column].astype(float).values.tolist() | ||
|
|
||
| if cfg.dataset.parent_id_column != "None": | ||
| raise LLMDataException( | ||
| "Parent ID column is not supported for regression datasets." | ||
| ) | ||
|
|
||
| def __getitem__(self, idx: int) -> Dict: | ||
| sample = super().__getitem__(idx) | ||
| sample["class_label"] = self.answers_float[idx] | ||
| return sample | ||
|
|
||
| def postprocess_output(self, cfg, df: pd.DataFrame, output: Dict) -> Dict: | ||
| output["logits"] = output["logits"].float() | ||
| preds = output["logits"] | ||
| preds = np.array(preds).astype(float).astype(str).reshape(-1) | ||
| output["predicted_text"] = preds | ||
| return super().postprocess_output(cfg, df, output) | ||
|
|
||
| def clean_output(self, output, cfg): | ||
| return output |
53 changes: 53 additions & 0 deletions
53
llm_studio/src/losses/text_causal_regression_modeling_losses.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import logging | ||
| from typing import Any, KeysView | ||
|
|
||
| from torch import Tensor, nn | ||
|
|
||
| __all__ = ["Losses"] | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class MSELoss(nn.Module): | ||
| def __init__(self, cfg: Any): | ||
| super().__init__() | ||
| self.cfg = cfg | ||
| self.loss_fn = nn.MSELoss() | ||
|
|
||
| def forward(self, logits: Tensor, labels: Tensor) -> Tensor: | ||
| return self.loss_fn(logits, labels.reshape(-1)) | ||
|
|
||
|
|
||
| class MAELoss(nn.Module): | ||
| def __init__(self, cfg: Any): | ||
| super().__init__() | ||
| self.cfg = cfg | ||
| self.loss_fn = nn.L1Loss() | ||
|
|
||
| def forward(self, logits: Tensor, labels: Tensor) -> Tensor: | ||
| return self.loss_fn(logits, labels.reshape(-1)) | ||
|
|
||
|
|
||
| class Losses: | ||
| """Losses factory.""" | ||
|
|
||
| _losses = { | ||
| "MSELoss": MSELoss, | ||
| "MAELoss": MAELoss, | ||
| } | ||
|
|
||
| @classmethod | ||
| def names(cls) -> KeysView: | ||
| return cls._losses.keys() | ||
|
|
||
| @classmethod | ||
| def get(cls, name: str) -> Any: | ||
| """Access to Losses. | ||
|
|
||
| Args: | ||
| name: losses name | ||
| Returns: | ||
| A class to build the Losses | ||
| """ | ||
| return cls._losses.get(name, MSELoss) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.