Transformers predictors

Huggingface Transformers predictors

Module: implementation.predictors



TransformersModel

Transformers model predictor. Subclass of Predictor.

When executed calls provided model.


__init__

Arguments:

  • cfg (TransformersModelConfig): Configuration for predictor.

  • input_class (Type[Input]): Class for input validation.

  • output_class (Type[Output]): Class for output validation.

  • name (Optional[str], optional): Name for identification. If equals to None, class name will be used. Defaults to None.


invoke

Call model

Arguments:

  • input_data (Input): Validated input.

  • evaluator (Evaluator): Evaluator in context of which executed.

Returns:

  • Dict[str, Any]: Result of execution.


config (Any)

Model configuration




TransformersGenerativeModel

Transformers generative model wrapper. Subclass of TransformersModel.

When executed calls generate method of provided model.


invoke

Call generate method of the model

Arguments:

  • input_data (Input): Validated input.

  • evaluator (Evaluator): Evaluator in context of which executed.

Returns:

  • Dict[str, Any]: Result of execution.




TransformersModelConfig

Transformers model configuration. Subclass of Config.


__init__

Arguments:

  • kwargs (Optional[Dict[str, Any]], optional): Extra model parameters.


config (Any)

Model configuration




TransformersPipeline

Transformers pipeline predictor. Subclass of Predictor.


__init__

Arguments:

  • cfg (TransformersPipelineConfig): Configuration for predictor.

  • input_class (Type[Input]): Class for input validation.

  • output_class (Type[Output]): Class for output validation.

  • name (Optional[str], optional): Name for identification. If equals to None, class name will be used. Defaults to None.


invoke

Call pipeline

Arguments:

  • input_data (Input): Validated input.

  • evaluator (Evaluator): Evaluator in context of which executed.

Returns:

  • Dict[str, Any]: Result of execution.


config (Any)

Model configuration




TransformersPipelineConfig

Transformers pipeline configuration. Subclass of Config.


__init__

Arguments:

  • task (Optional[str], optional): The task defining which pipeline will be returned. Defaults to None. Currently accepted tasks are:

  • model (Optional[Union[str, PreTrainedModel, TFPreTrainedModel]], optional): The model that will be used by the pipeline to make predictions. This can be a model identifier or an actual instance of a pretrained model inheriting from PreTrainedModel (for PyTorch) or TFPreTrainedModel (for TensorFlow). Defaults to None.

  • config (Optional[Union[str, PretrainedConfig]], optional): The configuration that will be used by the pipeline to instantiate the model. This can be a model identifier or an actual pretrained model configuration inheriting from PretrainedConfig. Defaults to None.

  • tokenizer (Optional[Union[str, PreTrainedTokenizer]], optional): The tokenizer that will be used by the pipeline to encode data for the model. This can be a model identifier or an actual pretrained tokenizer inheriting from PreTrainedTokenizer. Defaults to None.

  • feature_extractor (Optional[Any], optional): The feature extractor that will be used by the pipeline to encode data for the model. This can be a model identifier or an actual pretrained feature extractor inheriting from PreTrainedFeatureExtractor. Defaults to None. Feature extractors are used for non-NLP models, such as Speech or Vision models as well as multi-modal models. Multi-modal models will also require a tokenizer to be passed.

    If not provided, the default feature extractor for the given model will be loaded (if it is a string). If model is not specified or not a string, then the default feature extractor for config is loaded (if it is a string). However, if config is also not given or not a string, then the default feature extractor for the given task will be loaded.

  • image_processor (Optional[Union[str, BaseImageProcessor]], optional): Defaults to None.

  • framework (Optional[str], optional): The framework to use, either "pt" for PyTorch or "tf" for TensorFlow. The specified framework must be installed.

    If no framework is specified, will default to the one currently installed. If no framework is specified and both frameworks are installed, will default to the framework of the model, or to PyTorch if no model is provided.

  • revision (Optional[str], optional): When passing a task name or a string model identifier: The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a git-based system for storing models and other artifacts on huggingface.co, so revision can be any identifier allowed by git. If equals to None, "main" will be used. Defaults to None.

  • use_fast (bool, optional): Whether or not to use a Fast tokenizer if possible (a PreTrainedTokenizerFast). Defaults to True.

  • token (Optional[Union[str, bool]], optional): The token to use as HTTP bearer authorization for remote files. If True, will use the token generated when running huggingface-cli login (stored in ~/.huggingface). Defaults to None.

  • device (Optional[Union[int, str, torch.device]], optional): Defines the device (e.g., "cpu", "cuda:1", "mps", or a GPU ordinal rank like 1) on which this pipeline will be allocated. Defaults to None.

  • device_map (Optional[Union[str, Dict[str, Union[int, str, torch.device]]]], optional): Sent directly as model_kwargs (just a simpler shortcut). When accelerate library is present, set device_map="auto" to compute the most optimized device_map automatically (see here for more information). Defaults to None.

Do not use device_map and device at the same time as they will conflict

  • torch_dtype (Optional[Union[str, torch.dtype]], optional): Sent directly as model_kwargs (just a simpler shortcut) to use the available precision for this model (torch.float16, torch.bfloat16, … or "auto"). Defaults to None.

  • trust_remote_code (bool, optional): Whether or not to allow for custom code defined on the Hub in their own modeling, configuration, tokenization or even pipeline files. This option should only be set to True for repositories you trust and in which you have read the code, as it will execute code present on the Hub on your local machine. Defaults to False.

  • model_kwargs (Optional[Dict[str, Any]], optional): Additional dictionary of keyword arguments passed along to the model’s from_pretrained(..., **model_kwargs) function. Defaults to None.

  • pipeline_class (Optional[Any], optional): Defaults to None.

  • kwargs (Optional[Dict[str, Any]], optional): Additional keyword arguments passed along to the specific pipeline init (see the documentation for the corresponding pipeline class for possible values). Defaults to None.

For more, see:



Last updated