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

Lora config data model and utilities #270

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
22 changes: 21 additions & 1 deletion caikit_nlp/data_model/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""
# Standard
from enum import Enum
from typing import List
from typing import List, Union

# First Party
from caikit.core import DataObjectBase
Expand Down Expand Up @@ -73,6 +73,26 @@ class TuningConfig(DataObjectBase):
# encoder_hidden_size: int # Optional - The hidden size of the prompt encoder.


@caikit.core.dataobject(package="caikit_data_model.caikit_nlp")
class LoraTuningConfig(DataObjectBase):
# Lora attention dimension.
r: int
# List of module names or regex expression of the module names to replace with Lora.
# For example, ['q', 'v'] or '.*decoder.*(SelfAttention|EncDecAttention).*(q|v)$
target_modules: Union[List[str], str]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add an example of this in the comment as well? Also wondering how will a user know about these modules?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the same from HF. There are ways to know modules of a model by loading model with transformers and printing it. I will have to include that in actual examples that we commit to caikit NLP when feature is complete. we might have to expose a helper function in caikit NLP to get_module_names(model) or something . but I think I can implement that in follow up PRs when we get to adding examples.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would also need to figure out how do we expose these functions to cloud users then, also we would need to explain them what these modules are and what are its implication. Any suggestions on how this would be exposed to cloud users?

Copy link
Collaborator Author

@Ssukriti Ssukriti Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean ya the consuming products would have to document what parameters mean (they do that already). Model factsheets include the architecture and layers
Screenshot 2023-12-03 at 10 55 45 PM

users dont have to run any function if they know model architecture which I think should be exposed to users already from factsheet

Besides, it is optional parameter and upto cloud products if they want to expose it or not (though it is commonly tuned parameter from blog posts). I will make it more clear that it can be None as well and is Optional (I think I have to make some changes in the data model to make it Optional type - I will do that)

from library perspective we can still expose it and have further discussions down the road . Even if the parameter is left unused from cloud and not exposed to users to begin with, its not a big deal to have it in library

Copy link
Collaborator Author

@Ssukriti Ssukriti Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other ways of doing it would be to document model architectures from a common page for supported models; or if cloud products want to create a functionality to obtain model architecture in real time, we can have that discussion independently

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, when users have their own model they will most likely know the architecture

# The alpha parameter for Lora scaling.
lora_alpha: int
# The dropout probability for Lora layers.
lora_dropout: float
# Bias type for Lora. Can be ‘none’, ‘all’ or ‘lora_only’.
# If ‘all’ or ‘lora_only’, the corresponding biases will be updated during training.
# Be aware that this means that, even when disabling the adapters,
# the model will not produce the same output
# as the base model would have without adaptation.
bias: str
output_model_types: List[str]


@caikit.core.dataobject(package="caikit_data_model.caikit_nlp")
class ExponentialDecayLengthPenalty(DataObjectBase):
start_index: int
Expand Down
206 changes: 145 additions & 61 deletions caikit_nlp/modules/text_generation/peft_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import re

# Third Party
from peft import MultitaskPromptTuningInit
from peft import LoraConfig, MultitaskPromptTuningInit, PromptTuningConfig
from transformers import AutoConfig

# First Party
Expand Down Expand Up @@ -51,10 +51,10 @@
class TuningType(str, Enum):
PROMPT_TUNING = "PROMPT_TUNING"
MULTITASK_PROMPT_TUNING = "MULTITASK_PROMPT_TUNING"
LORA = "LORA"
# MULTITASK_PREFIX_TUNING = "MULTITASK_PREFIX_TUNING"
# P_TUNING = "P_TUNING"
# PREFIX_TUNING = "PREFIX_TUNING"
# LORA = "LORA"


def resolve_base_model(base_model, cls, torch_dtype):
Expand Down Expand Up @@ -96,12 +96,142 @@ def resolve_base_model(base_model, cls, torch_dtype):


def get_peft_config(
tuning_type, tuning_config, base_model, cls, torch_dtype, verbalizer
tuning_type,
tuning_config,
base_model,
cls=None,
torch_dtype=None,
verbalizer="{{input}}",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason these defaults are now being set here? I.e., the calling module is always going to pass them in positionally and override these defaults, right?

Copy link
Collaborator Author

@Ssukriti Ssukriti Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want to deprecate all these 3 parameters (once I move create_hf_tuning function to this file , we wont need cls either. Other 2 are unused). So I just set them here , so we can stop setting them from calling module and remove them in next major release. The 3 will not be needed for Lora either and dont have to be set from calling module.

):

if isinstance(tuning_type, str):
error.value_check(
"<NLP65714994E>",
tuning_type in TuningType._member_names_,
f"Invalid tuning type [{tuning_type}]. Allowed types: "
f"[{TuningType._member_names_}]",
)
tuning_type = TuningType(tuning_type)

error.type_check("<NLP65714993E>", TuningType, tuning_type=tuning_type)

if tuning_type not in TuningType._member_names_:
raise NotImplementedError("{} tuning type not supported!".format(tuning_type))

error.type_check("<NLP65714919E>", PretrainedModelBase, base_model=base_model)

# Validate if tuned output model type is compatible with base model or not
output_model_types = _get_output_types(tuning_config, base_model)

# NOTE: Base model is a resource at this point
task_type = base_model.TASK_TYPE

error.value_check(
"<NLP30542004E>",
len(output_model_types) <= base_model.MAX_NUM_TRANSFORMERS,
f"Too many output model types. Got {len(output_model_types)}, "
f"maximum {base_model.MAX_NUM_TRANSFORMERS}",
)

if verbalizer:
log.warning(
"<NLP21323085W>",
"verbalizer parameter is DEPRECATED for this function \
and will be removed in future. \
This parameter is also not getting used in creation of peft config",
)
# Ensure that our verbalizer is a string and
# will not render to a hardcoded string
# TODO: This check should happen in prompt tuning module and not here
error.value_check(
"<NLP83837412E>",
is_valid_verbalizer(verbalizer),
"Provided verbalizer is an invalid type or has no renderable placeholders",
)

if torch_dtype:
log.warning(
"<NLP16173085W>",
"torch_dtype parameter is DEPRECATED for this function \
and will be removed in future. \
This parameter is also not getting used in creation of peft config",
)
torch_dtype = get_torch_dtype(torch_dtype)

if tuning_type in [
TuningType.PROMPT_TUNING,
TuningType.MULTITASK_PROMPT_TUNING,
]:
peft_config = _create_prompt_tuning_config(
tuning_type, tuning_config, cls, base_model, task_type, output_model_types
)
else:
# we only have Lora besides other two for now
peft_config = _create_lora_config(tuning_config, task_type)

return task_type, output_model_types, peft_config, tuning_type


def _get_output_types(tuning_config, base_model):
"Validate and return output_model_types"
# Validate if tuned output model type is compatible with base model or not
if not tuning_config.output_model_types:
output_model_types = base_model.PROMPT_OUTPUT_TYPES
else:
# If the first element is not PromptOutputModelType, assume the entire list
# isn't and convert
if not isinstance(tuning_config.output_model_types[0], PromptOutputModelType):
output_model_types = []
for output_type in tuning_config.output_model_types:
output_model_types.append(PromptOutputModelType(output_type))
else:
output_model_types = tuning_config.output_model_types
error.value_check(
"<NLP36947542E>",
all(
output_type in base_model.PROMPT_OUTPUT_TYPES
for output_type in output_model_types
),
"{} not supported for base model type {}".format(
output_model_types, base_model.MODEL_TYPE
),
)
return output_model_types


def _filter_params_for_prompt_config(prompt_config, params):
"""Utility function to filter out required parameters for prompt_config
from `params`

Args:
prompt_config: PromptTuningConfig
Tuning config type, eg:, PromptTuningConfig
params: dict
Dictionary containing all the input training params

Returns:
dict:
Dictionary containing required params for prompt_config
"""
# Inspect the underlying dataclass fileds; we do this because the common super class
# used for multi/vanilla prompt/prefix tuning is a DataClass; we can't use __dict__
# because the dataclass fields are omitted.
allowed_keys = list(prompt_config.__dataclass_fields__.keys())
allowed_params = dict(filter(lambda x: x[0] in allowed_keys, params.items()))
log.info(
"<NLP18184771I>",
"[{}] config params not supported by provided tuning type!".format(
params.keys() - allowed_params.keys()
),
)
return allowed_params


def _create_prompt_tuning_config(
tuning_type, tuning_config, cls, base_model, task_type, output_model_types
) -> PromptTuningConfig:
"""Creates Huggingface PromptTuningConfig from Caikit tuning configuration."""

if tuning_config.prompt_tuning_init_method:
# NOTE: GK-APR-5-2023
# MultitaskPromptTuningInit and MultitaskPrefixTuningInit are same at the
Expand Down Expand Up @@ -144,62 +274,6 @@ def get_peft_config(
tuning_config.prompt_tuning_init_source_model,
)

error.type_check("<NLP65714919E>", PretrainedModelBase, base_model=base_model)

# Validate if tuned output model type is compatible with base model or not
if not tuning_config.output_model_types:
output_model_types = base_model.PROMPT_OUTPUT_TYPES
else:
# If the first element is not PromptOutputModelType, assume the entire list
# isn't and convert
if not isinstance(tuning_config.output_model_types[0], PromptOutputModelType):
output_model_types = []
for output_type in tuning_config.output_model_types:
output_model_types.append(PromptOutputModelType(output_type))
else:
output_model_types = tuning_config.output_model_types
error.value_check(
"<NLP36947542E>",
all(
output_type in base_model.PROMPT_OUTPUT_TYPES
for output_type in output_model_types
),
"{} not supported for base model type {}".format(
output_model_types, base_model.MODEL_TYPE
),
)

error.value_check(
"<NLP30542004E>",
len(output_model_types) <= base_model.MAX_NUM_TRANSFORMERS,
f"Too many output model types. Got {len(output_model_types)}, "
f"maximum {base_model.MAX_NUM_TRANSFORMERS}",
)
# Ensure that our verbalizer is a string and will not render to a hardcoded string
error.value_check(
"<NLP83837412E>",
is_valid_verbalizer(verbalizer),
"Provided verbalizer is an invalid type or has no renderable placeholders",
)

# NOTE: Base model is a resource at this point
task_type = base_model.TASK_TYPE

if isinstance(tuning_type, str):
error.value_check(
"<NLP65714994E>",
tuning_type in TuningType._member_names_,
f"Invalid tuning type [{tuning_type}]. Allowed types: "
f"[{TuningType._member_names_}]",
)
tuning_type = TuningType(tuning_type)
error.type_check("<NLP65714993E>", TuningType, tuning_type=tuning_type)

# Coerce the passed model into a resource; if we have one, this is a noop
# TODO: When splitting up this mono-module, use the configured resource
# type of the concrete class to bootstrap
torch_dtype = get_torch_dtype(torch_dtype)

# Take tokenizer name/path from the model
tokenizer_name_or_path = base_model.model.config._name_or_path

Expand All @@ -208,13 +282,23 @@ def get_peft_config(

# NOTE: We currently only support TEXT as init type, this is to later only easily
# switch to MPT
peft_config = cls.create_hf_tuning_config(
prompt_tuning_config = cls.create_hf_tuning_config(
base_model=base_model,
tuning_type=tuning_type,
task_type=task_type,
tokenizer_name_or_path=tokenizer_name_or_path,
tuning_config=tuning_config,
output_model_types=output_model_types,
)
return prompt_tuning_config

return task_type, output_model_types, peft_config, tuning_type

def _create_lora_config(tuning_config, task_type) -> LoraConfig:
"""Creates Huggingface LoraConfig from Caikit tuning configuration."""

config_kwargs = tuning_config.to_dict()
log.info("<NLP61012781I>", f"Parameters used: {config_kwargs}")
config_params = _filter_params_for_prompt_config(tuning_config, config_kwargs)
del config_params["output_model_types"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am probably missing something - what is the reason for deleting this?

Copy link
Collaborator Author

@Ssukriti Ssukriti Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have this parameter in our TuningConfig data model (it was there in prompt tuning, so I copied it to Lora Tuning. I didnt dig into why we need it) , but it is not in the HF prompttuningConfig or Loraconfig , so we have to remove it before passing it to get the HF TuningConfig. The way it is removed for prompt tuning currently in https://github.com/caikit/caikit-nlp/blob/main/caikit_nlp/modules/text_generation/peft_prompt_tuning.py#L798C16-L798C16 , is by copying all the relevant parameters in another dict which does not include output_model_types.

I chose to delete instead as only 1 parameter was different. I should have checked if key exists before deleting though to avoid key error. I will add that check, thanks for observing this

lora_config = LoraConfig(task_type=task_type, **config_params)
return lora_config
39 changes: 8 additions & 31 deletions caikit_nlp/modules/text_generation/peft_prompt_tuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@
)
from ...toolkit.trainer_utils import validate_training_data
from ...toolkit.verbalizer_utils import render_verbalizer
from .peft_config import TuningType, get_peft_config, resolve_base_model
from .peft_config import (
TuningType,
_filter_params_for_prompt_config,
get_peft_config,
resolve_base_model,
)

log = alog.use_channel("PEFT_PROMPT")
error = error_handler.get(log)
Expand All @@ -99,10 +104,10 @@ class PeftPromptTuning(ModuleBase):
tuning_type_to_huggingface = {
TuningType.PROMPT_TUNING: PeftType.PROMPT_TUNING,
TuningType.MULTITASK_PROMPT_TUNING: PeftType.MULTITASK_PROMPT_TUNING,
TuningType.LORA: PeftType.LORA,
# TuningType.MULTITASK_PREFIX_TUNING: PeftType.MULTITASK_PREFIX_TUNING,
# TuningType.P_TUNING: PeftType.P_TUNING,
# TuningType.PREFIX_TUNING: PeftType.PREFIX_TUNING,
# TuningType.LORA: PeftType.LORA,
}

RANDOM_SEED = 73
Expand Down Expand Up @@ -856,7 +861,7 @@ def create_hf_tuning_config(
elif tuning_type == TuningType.MULTITASK_PROMPT_TUNING:
tuning_config_type = MultitaskPromptTuningConfig

config_params = cls._filter_params_for_prompt_config(
config_params = _filter_params_for_prompt_config(
tuning_config_type, config_kwargs
)
log.info("<NLP41038481I>", f"Parameters used: {config_params}")
Expand Down Expand Up @@ -1150,34 +1155,6 @@ def _execute_train_loop(
)
return {"loss": training_loss_tracker}

@classmethod
def _filter_params_for_prompt_config(cls, prompt_config, params):
"""Utility function to filter out required parameters for prompt_config
Copy link
Collaborator Author

@Ssukriti Ssukriti Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this belongs in peft_config as it a utility to create any Config, so I moved it. I also think create_hf_tuning_config( in thsi file belongs there, but I did not move it because I dont know the rational behind keeping create_hf_tuning_config here in prompt_tuning.py

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more of a legacy reason than anything, all of the code for tuning config stuff was originally written in this file as part of this module, and the refactoring to pull some of it out happened later. I don't think there was a deep reason for leaving create_hf_tuning_config here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I will move it

from `params`

Args:
prompt_config: PromptTuningConfig
Tuning config type, eg:, PromptTuningConfig
params: dict
Dictionary containing all the input training params

Returns:
dict:
Dictionary containing required params for prompt_config
"""
# Inspect the underlying dataclass fileds; we do this because the common super class
# used for multi/vanilla prompt/prefix tuning is a DataClass; we can't use __dict__
# because the dataclass fields are omitted.
allowed_keys = list(prompt_config.__dataclass_fields__.keys())
allowed_params = dict(filter(lambda x: x[0] in allowed_keys, params.items()))
log.info(
"<NLP18184771I>",
"[{}] config params not supported by provided tuning type!".format(
params.keys() - allowed_params.keys()
),
)
return allowed_params

@staticmethod
def convert_peft_model_to_type(
device: str, peft_model: PeftModel, torch_dtype=Union[str, torch.dtype]
Expand Down
Loading