-
Notifications
You must be signed in to change notification settings - Fork 512
Causal Classification Problem Type #449
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 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d34cddb
first implementation
psinger 31999bf
Merge branch 'main' into psi/classification
4d07203
updates
psinger c3cbec5
format
psinger e70f88b
cfg
psinger 25244e5
feedback
psinger 88e232e
import
psinger 2b21dc0
Merge branch 'main' into psi/classification
8a1108f
readme
psinger 2f1f0bb
Merge branch 'psi/classification' of github.com:h2oai/h2o-llmstudio i…
psinger 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| The column in the dataset containing the expected output. | ||
| The column in the dataset containing the expected output. | ||
|
|
||
| For classification, this needs to be an integer column containing the class label. |
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 @@ | ||
| The number of possible classes for the classification task. For binary classification, a single class should be selected. |
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
186 changes: 186 additions & 0 deletions
186
llm_studio/python_configs/text_causal_classification_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,186 @@ | ||
| import os | ||
| from dataclasses import dataclass, field | ||
| from typing import Any, Tuple | ||
|
|
||
| from llm_studio.python_configs.base import DefaultConfigProblemBase | ||
| from llm_studio.python_configs.text_causal_language_modeling_config import ( | ||
| ConfigNLPAugmentation, | ||
| ConfigNLPCausalLMArchitecture, | ||
| ConfigNLPCausalLMDataset, | ||
| ConfigNLPCausalLMEnvironment, | ||
| ConfigNLPCausalLMLogging, | ||
| ConfigNLPCausalLMPrediction, | ||
| ConfigNLPCausalLMTokenizer, | ||
| ConfigNLPCausalLMTraining, | ||
| ) | ||
| from llm_studio.src import possible_values | ||
| from llm_studio.src.losses import text_causal_classification_modeling_losses | ||
| from llm_studio.src.metrics import text_causal_classification_modeling_metrics | ||
| from llm_studio.src.models import text_causal_classification_modeling_model | ||
| from llm_studio.src.utils.modeling_utils import generate_experiment_name | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigNLPCausalClassificationDataset(ConfigNLPCausalLMDataset): | ||
| system_column: str = "None" | ||
| prompt_column: Tuple[str, ...] = ("instruction", "input") | ||
| answer_column: str = "label" | ||
| num_classes: int = 1 | ||
| parent_id_column: str = "None" | ||
|
|
||
| text_system_start: str = "" | ||
| text_prompt_start: str = "" | ||
| text_answer_separator: str = "" | ||
|
|
||
| add_eos_token_to_system: bool = False | ||
| add_eos_token_to_prompt: bool = False | ||
| add_eos_token_to_answer: bool = False | ||
|
|
||
| _allowed_file_extensions: Tuple[str, ...] = ("csv", "pq", "parquet") | ||
|
|
||
| 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._possible_values["num_classes"] = (1, 100, 1) | ||
|
|
||
| self._visibility["personalize"] = -1 | ||
| self._visibility["chatbot_name"] = -1 | ||
| self._visibility["chatbot_author"] = -1 | ||
| self._visibility["mask_prompt_labels"] = -1 | ||
| self._visibility["add_eos_token_to_answer"] = -1 | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigNLPCausalClassificationTraining(ConfigNLPCausalLMTraining): | ||
| loss_class: Any = text_causal_classification_modeling_losses.Losses | ||
| loss_function: str = "BinaryCrossEntropyLoss" | ||
|
|
||
| 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"), | ||
| allow_custom=False, | ||
| placeholder="Select optional layers...", | ||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigNLPCausalClassificationTokenizer(ConfigNLPCausalLMTokenizer): | ||
| max_length_prompt: int = 512 | ||
| max_length: int = 512 | ||
|
|
||
| def __post_init__(self): | ||
| super().__post_init__() | ||
|
|
||
| self._visibility["max_length_answer"] = -1 | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigNLPCausalClassificationArchitecture(ConfigNLPCausalLMArchitecture): | ||
| model_class: Any = text_causal_classification_modeling_model.Model | ||
|
|
||
| def __post_init__(self): | ||
| super().__post_init__() | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigNLPCausalClassificationPrediction(ConfigNLPCausalLMPrediction): | ||
| metric_class: Any = text_causal_classification_modeling_metrics.Metrics | ||
| metric: str = "AUC" | ||
|
|
||
| def __post_init__(self): | ||
| super().__post_init__() | ||
| self._possible_values["metric"] = self.metric_class.names() | ||
|
|
||
| for k in [ | ||
psinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "min_length_inference", | ||
| "max_length_inference", | ||
| "do_sample", | ||
| "num_beams", | ||
| "temperature", | ||
| "repetition_penalty", | ||
| "stop_tokens", | ||
| "top_k", | ||
| "top_p", | ||
| ]: | ||
| self._visibility[k] = -1 | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigNLPCausalClassificationEnvironment(ConfigNLPCausalLMEnvironment): | ||
| _model_card_template: str = "text_causal_classification_model_card_template.md" | ||
| _summary_card_template: str = ( | ||
| "text_causal_classification_experiment_summary_card_template.md" | ||
| ) | ||
|
|
||
| def __post_init__(self): | ||
| super().__post_init__() | ||
|
|
||
|
|
||
| @dataclass | ||
| class ConfigProblemBase(DefaultConfigProblemBase): | ||
| output_directory: str = f"output/{os.path.basename(__file__).split('.')[0]}" | ||
| experiment_name: str = field(default_factory=generate_experiment_name) | ||
| _parent_experiment: str = "" | ||
| llm_backbone: str = "h2oai/h2ogpt-4096-llama2-7b" | ||
| type: str = "causal_classification" | ||
|
|
||
| dataset: ConfigNLPCausalClassificationDataset = field( | ||
| default_factory=ConfigNLPCausalClassificationDataset | ||
| ) | ||
| tokenizer: ConfigNLPCausalLMTokenizer = field( | ||
| default_factory=ConfigNLPCausalLMTokenizer | ||
| ) | ||
| architecture: ConfigNLPCausalClassificationArchitecture = field( | ||
| default_factory=ConfigNLPCausalClassificationArchitecture | ||
| ) | ||
| training: ConfigNLPCausalClassificationTraining = field( | ||
| default_factory=ConfigNLPCausalClassificationTraining | ||
| ) | ||
| augmentation: ConfigNLPAugmentation = field(default_factory=ConfigNLPAugmentation) | ||
| prediction: ConfigNLPCausalClassificationPrediction = field( | ||
| default_factory=ConfigNLPCausalClassificationPrediction | ||
| ) | ||
| environment: ConfigNLPCausalClassificationEnvironment = field( | ||
| default_factory=ConfigNLPCausalClassificationEnvironment | ||
| ) | ||
| logging: ConfigNLPCausalLMLogging = field(default_factory=ConfigNLPCausalLMLogging) | ||
|
|
||
| def __post_init__(self): | ||
| super().__post_init__() | ||
|
|
||
| self._visibility["output_directory"] = -1 | ||
|
|
||
| self._possible_values["llm_backbone"] = possible_values.String( | ||
| values=( | ||
| "h2oai/h2ogpt-4096-llama2-70b", | ||
| "h2oai/h2ogpt-4096-llama2-70b-chat", | ||
| "h2oai/h2ogpt-4096-llama2-13b", | ||
| "h2oai/h2ogpt-4096-llama2-13b-chat", | ||
| "h2oai/h2ogpt-4096-llama2-7b", | ||
| "h2oai/h2ogpt-4096-llama2-7b-chat", | ||
| "tiiuae/falcon-40b", | ||
| "tiiuae/falcon-7b", | ||
| "openlm-research/open_llama_13b", | ||
| "openlm-research/open_llama_7b", | ||
| "openlm-research/open_llama_3b", | ||
| "EleutherAI/gpt-j-6B", | ||
| "facebook/opt-125m", | ||
| ), | ||
| allow_custom=True, | ||
| ) | ||
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
53 changes: 53 additions & 0 deletions
53
llm_studio/src/losses/text_causal_classification_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 nn | ||
|
|
||
| __all__ = ["Losses"] | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class CrossEntropyLoss(nn.Module): | ||
| def __init__(self, cfg: Any): | ||
| super().__init__() | ||
| self.cfg = cfg | ||
| self.loss_fn = nn.CrossEntropyLoss() | ||
|
|
||
| def forward(self, logits, labels): | ||
maxjeblick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return self.loss_fn(logits, labels.reshape(-1).long()) | ||
|
|
||
|
|
||
| class BinaryCrossEntropyLoss(nn.Module): | ||
| def __init__(self, cfg: Any): | ||
| super().__init__() | ||
| self.cfg = cfg | ||
| self.loss_fn = nn.BCEWithLogitsLoss() | ||
|
|
||
| def forward(self, logits, labels): | ||
| return self.loss_fn(logits, labels) | ||
|
|
||
|
|
||
| class Losses: | ||
| """Losses factory.""" | ||
|
|
||
| _losses = { | ||
| "CrossEntropyLoss": CrossEntropyLoss, | ||
| "BinaryCrossEntropyLoss": BinaryCrossEntropyLoss, | ||
| } | ||
|
|
||
| @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, CrossEntropyLoss) | ||
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.