backprop.models¶
Submodules¶
backprop.models.auto_model¶
-
class
AutoModel
[source]¶ Bases:
object
-
static
from_pretrained
(model_name: str, aliases: Optional[Dict] = None, device: Optional[str] = None)[source]¶ Loads a model by name
- Parameters
model_name – unique name of the model
aliases – dictionary that maps aliases to model_name
device – device to use model on. Defaults to “cuda” if available
- Returns
Initialised model object
Example:
import backprop model = backprop.models.AutoModel.from_pretrained("t5_small")
-
static
list_models
(task=None, return_dict=False, display=False, limit=None, aliases: Optional[Dict] = None)[source]¶ Lists available models
- Parameters
task – filter by task identifier
return_dict – whether to return dictionary instead of a list
display – print instead of returning
limit – maximum number of models to include
aliases – dict that maps aliases to model_name
Example:
import backprop backprop.models.AutoModel.list_models(task="text-vectorisation", display=True) > Name clip Description Model by OpenAI ...
-
static
backprop.models.generic_models¶
-
class
BaseModel
(model, name: Optional[str] = None, description: Optional[str] = None, tasks: Optional[List[str]] = None, details: Optional[Dict] = None)[source]¶ Bases:
torch.nn.modules.module.Module
The base class for a model.
-
model
¶ Your model that takes some args, kwargs and returns an output. Must be callable.
-
name
¶ string identifier for the model. Lowercase letters and numbers. No spaces/special characters except dashes.
-
description
¶ String description of the model.
-
tasks
¶ List of supported task strings
-
details
¶ Dictionary of additional details about the model
-
eval
()[source]¶ Sets the module in evaluation mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout
,BatchNorm
, etc.This is equivalent with
self.train(False)
.- Returns
self
- Return type
Module
-
to
(device)[source]¶ Moves and/or casts the parameters and buffers.
This can be called as
Its signature is similar to
torch.Tensor.to()
, but only accepts floating point desireddtype
s. In addition, this method will only cast the floating point parameters and buffers todtype
(if given). The integral parameters and buffers will be moveddevice
, if that is given, but with dtypes unchanged. Whennon_blocking
is set, it tries to convert/move asynchronously with respect to the host if possible, e.g., moving CPU Tensors with pinned memory to CUDA devices.See below for examples.
Note
This method modifies the module in-place.
- Parameters
device (
torch.device
) – the desired device of the parameters and buffers in this moduledtype (
torch.dtype
) – the desired floating point type of the floating point parameters and buffers in this moduletensor (torch.Tensor) – Tensor whose dtype and device are the desired dtype and device for all parameters and buffers in this module
memory_format (
torch.memory_format
) – the desired memory format for 4D parameters and buffers in this module (keyword only argument)
- Returns
self
- Return type
Module
Example:
>>> linear = nn.Linear(2, 2) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]]) >>> linear.to(torch.double) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]], dtype=torch.float64) >>> gpu1 = torch.device("cuda:1") >>> linear.to(gpu1, dtype=torch.half, non_blocking=True) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1') >>> cpu = torch.device("cpu") >>> linear.to(cpu) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16)
-
train
(mode: bool = True)[source]¶ Sets the module in training mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout
,BatchNorm
, etc.- Parameters
mode (bool) – whether to set training mode (
True
) or evaluation mode (False
). Default:True
.- Returns
self
- Return type
Module
-
training
: bool¶
-
-
class
HFModel
(model_path, tokenizer_path=None, name: Optional[str] = None, description: Optional[str] = None, tasks: Optional[List[str]] = None, details: Optional[Dict] = None, model_class=<class 'transformers.models.auto.modeling_auto.AutoModelForPreTraining'>, tokenizer_class=<class 'transformers.models.auto.tokenization_auto.AutoTokenizer'>, device=None)[source]¶ Bases:
backprop.models.generic_models.PathModel
Class for huggingface models
-
model_path
¶ Local or huggingface.co path to the model
-
name
¶ string identifier for the model. Lowercase letters and numbers. No spaces/special characters except dashes.
-
description
¶ String description of the model.
-
tasks
¶ List of supported task strings
-
details
¶ Dictionary of additional details about the model
-
init_model
¶ Callable to initialise model from path Defaults to AutoModelForPreTraining from huggingface
-
tokenizer_path
¶ Path to the tokenizer
- Type
optional
-
init_tokenizer
¶ Callable to initialise tokenizer from path Defaults to AutoTokenizer from huggingface.
- Type
optional
-
device
¶ Device for inference. Defaults to “cuda” if available.
- Type
optional
-
training
: bool¶
-
-
class
HFTextGenerationModel
(model_path, tokenizer_path=None, name: Optional[str] = None, description: Optional[str] = None, tasks: Optional[List[str]] = None, details: Optional[Dict] = None, model_class=<class 'transformers.models.auto.modeling_auto.AutoModelForPreTraining'>, tokenizer_class=<class 'transformers.models.auto.tokenization_auto.AutoTokenizer'>, device=None)[source]¶ Bases:
backprop.models.generic_models.HFModel
Class for huggingface models that implement the .generate method.
-
\*args and **kwargs are passed to HFModel's __init__
-
generate
(text, variant='seq2seq', **kwargs)[source]¶ Generate according to the model’s generate method.
-
training
: bool¶
-
-
class
PathModel
(model_path, init_model, name: Optional[str] = None, description: Optional[str] = None, tasks: Optional[List[str]] = None, details: Optional[Dict] = None, tokenizer_path=None, init_tokenizer=None, device=None)[source]¶ Bases:
backprop.models.generic_models.BaseModel
Class for models which are initialised from a path.
-
model_path
¶ Path to the model
-
name
¶ string identifier for the model. Lowercase letters and numbers. No spaces/special characters except dashes.
-
description
¶ String description of the model.
-
tasks
¶ List of supported task strings
-
details
¶ Dictionary of additional details about the model
-
init_model
¶ Callable to initialise model from path
-
tokenizer_path
¶ Path to the tokenizer
- Type
optional
-
init_tokenizer
¶ Callable to initialise tokenizer from path
- Type
optional
-
device
¶ Device for inference. Defaults to “cuda” if available.
- Type
optional
-
training
: bool¶
-