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.

Attributes#

Classes#

HFDecoderModel

Initializes a HFDecoderModel instance.

Module Contents#

lmflow.models.hf_decoder_model.logger[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.hf_model_mixin.HFModelMixin, 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) lmflow.datasets.dataset.Dataset[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, release_gpu: bool = False, use_vllm: bool = False, **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. When using vllm inference, this should be a string or a list of strings. When using normal inference, this should be a tensor.

release_gpubool, optional

Whether to release the GPU resource after inference, by default False.

use_vllmbool, optional

Whether to use VLLM for inference, by default False.

kwargsOptional.

Keyword arguments.

Returns:
outputs

The generated sequence output

__inference(inputs, *args, **kwargs)[source]#

Perform generation process of the model.

Parameters:
inputs

The tokenized 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

__vllm_inference(inputs: str | List[str], sampling_params: vllm.SamplingParams | None = None, **kwargs) List[lmflow.utils.data_utils.VLLMInferenceResultWithInput][source]#

Perform VLLM inference process of the model.

Parameters:
inputsUnion[str, List[str]]

Prompt(s), string or a list of strings.

sampling_paramsOptional[SamplingParams], optional

vllm SamplingParams object, by default None.

Returns:
List[VLLMInferenceResultWithInput]

Return a list of VLLMInferenceResultWithInput, where each element contains the input prompt and the corresponding output.

When sampling_params.detokenize = True, the output would be a list of strings, contains sampling_params.n samples for the corresponding prompt.

When sampling_params.detokenize = False, return a list of list of ints (token ids, no decoding after generation).

prepare_inputs_for_inference(dataset: lmflow.datasets.dataset.Dataset, apply_chat_template: bool = True, enable_distributed_inference: bool = False, use_vllm: bool = False, **kwargs) List[str] | ray.data.Dataset | Dict[str, torch.Tensor][source]#

Prepare inputs for inference.

Parameters:
datasetlmflow.datasets.Dataset.

The dataset used for inference.

argsOptional.

Positional arguments.

kwargsOptional.

Keyword arguments.

Returns:
outputs

The prepared inputs for inference.

__prepare_inputs_for_vllm_inference(dataset: lmflow.datasets.dataset.Dataset, apply_chat_template: bool = True, enable_distributed_inference: bool = False) List[str] | ray.data.Dataset[source]#
abstract __prepare_inputs_for_inference(dataset: lmflow.datasets.dataset.Dataset, **kwargs)[source]#
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