We've released our memory-efficient finetuning algorithm LISA, check out [Paper][User Guide] for more details!

lmflow.models.hf_decoder_model#

This is a class called HFDecoderModel which is a wrapper around transformers model and tokenizer classes. It has several methods such as __init__, tokenize, and train that are used for training and fine-tuning the model. The __init__ method takes in several arguments such as model_args, tune_strategy, and ds_config, which are used to load the pretrained model and tokenizer, and initialize the training settings.

The tokenize method is used to tokenize the input text and return the input IDs and attention masks that can be fed to the model for training or inference.

This class supports different tune_strategy options such as ‘normal’, ‘none’, ‘lora’, and ‘adapter’, which allow for different fine-tuning settings of the model. However, the ‘lora’ and ‘adapter’ strategies are not yet implemented.

Overall, this class provides a convenient interface for loading and fine-tuning transformer models and can be used for various NLP tasks such as language modeling, text classification, and question answering.

Module Contents#

Classes#

HFDecoderModel

Initializes a HFDecoderModel instance.

Attributes#

logger

MODELS_SUPPORT_FLASH_ATTENTION

GPU_SUPPORT_FLASH_ATTENTION

GPU_SUPPORT_FLASH_ATTENTION

lmflow.models.hf_decoder_model.logger[source]#
lmflow.models.hf_decoder_model.MODELS_SUPPORT_FLASH_ATTENTION = ['LlamaForCausalLM', 'GPTNeoForCausalLM', 'GPT2ForCausalLM', 'BloomForCausalLM'][source]#
lmflow.models.hf_decoder_model.GPU_SUPPORT_FLASH_ATTENTION[source]#
lmflow.models.hf_decoder_model.GPU_SUPPORT_FLASH_ATTENTION[source]#
class lmflow.models.hf_decoder_model.HFDecoderModel(model_args, tune_strategy='normal', ds_config=None, device='gpu', use_accelerator=False, *args, **kwargs)[source]#

Bases: lmflow.models.decoder_model.DecoderModel, lmflow.models.interfaces.tunable.Tunable

Initializes a HFDecoderModel instance.

Parameters:
model_args

Model arguments such as model name, path, revision, etc.

tune_strategystr or none, default=”normal”.

A string representing the dataset backend. Defaults to “huggingface”.

ds_config

Deepspeed configuations.

argsOptional.

Positional arguments.

kwargsOptional.

Keyword arguments.

tokenize(dataset, add_special_tokens=True, *args, **kwargs)[source]#

Tokenize the full dataset.

Parameters:
datasetlmflow.datasets.Dataset.
argsOptional.

Positional arguments.

kwargsOptional.

Keyword arguments.

Returns:
tokenized_datasets

The tokenized dataset, without any leading or trailing special tokens (normally they are Begin-Of-Sentence or End-Of-Sentence tokens).

encode(input: str | List[str], *args, **kwargs) List[int] | List[List[int]][source]#

Perform encoding process of the tokenizer.

Parameters:
inputsstr or list.

The text sequence.

argsOptional.

Positional arguments.

kwargsOptional.

Keyword arguments.

Returns:
outputs

if string input,return the tokenized inputs. “Hello,world!”-> [101, 7592, 1010, 2088, 102] if batch input,return {input_ids,attention_mask,token_type_ids} [“Hello,world!”,”Hello!”]-> {‘input_ids’: tensor([[ 101, 7592, 1010, 2088, 102],…),’attention_mask’: tensor([[1, 1, 1, 1, 1],[0,0,1,1,1]])}

decode(input, *args, **kwargs) str | List[str][source]#

Perform decoding process of the tokenizer.

Parameters:
inputslist or tensor.

The token sequence.

argsOptional.

Positional arguments.

kwargsOptional.

Keyword arguments.

Returns:
outputs

The text decoded from the token inputs. if batch input,return the list of text [[101, 7592, 1010, 2088, 102],[101, 7592, 1010, 2088, 102]]-> [“Hello,world!”,”Hello,world!” if single input,return the text [101, 7592, 1010, 2088, 102]-> “Hello,world!”

inference(inputs, use_accelerator=False, *args, **kwargs)[source]#

Perform generation process of the model.

Parameters:
inputs

The sequence used as a prompt for the generation or as model inputs to the model.

argsOptional.

Positional arguments.

kwargsOptional.

Keyword arguments.

Returns:
outputs

The generated sequence output

merge_lora_weights()[source]#
get_peft_without_qlora()[source]#
save(dir, save_full_model=False, *args, **kwargs)[source]#

Perform generation process of the model.

Parameters:
dir

The directory to save model and tokenizer

save_full_modelOptional.

Whether to save full model.

kwargsOptional.

Keyword arguments.

Returns:
outputs

The generated sequence output

get_max_length()[source]#

Return max acceptable input length in terms of tokens.

get_tokenizer()[source]#

Return the tokenizer of the model.

get_backend_model()[source]#

Return the backend model.