model_reference
References to custom models.
Classes
BitfountModelReference
class BitfountModelReference( model_ref: Union[Path, str], datastructure: Optional[DataStructure] = None, model_version: Optional[int] = None, schema: Optional[BitfountSchema] = None, username: Optional[str] = None, hub: Optional[BitfountHub] = None, hyperparameters: Optional[_StrAnyDict] = None, private: bool = False, new_version: bool = False, secrets: Optional[Union[APIKeys, ExternallyManagedJWT]] = None, weights: Optional[Union[Path, str]] = None, model_description: Optional[str] = None,):Describes a local or remote reference to a model implementing ModelProtocol.
To use another user's custom model, simply provide that user's username instead of
your own (along with the name of the model as the model_ref argument).
Arguments
datastructure:DataStructureto be passed to the model when initialised. This is an optional argument as it is only required forget_modelto perform validation on the model before uploading it to the hub. Ensure that you provide this argument if you want to useget_modelto upload your model.hub: Required for upload/download of model. This attribute is set after initialisation on the worker side as the hub is not serialized. Defaults to None.hyperparameters: Hyperparameters to be passed to the model constructor after it has been loaded from file or hub. Defaults to None.model_ref: Either path to model file or name of model on hub.model_version: The version of the model you wish to use. Defaults to the latest version.new_version: Whether to upload a new version of the model to the hub. Defaults to False.private: Boolean flag to set the model to be private to control useage or publicly accessible to all users.schema: TheBitfountSchemaobject associated with the datasource on which the model will be trained on.secrets: The secrets to use when creating aBitfountHubinstance. Defaults to None.username: The username of the model owner. Defaults to bitfount session username if not provided.
Attributes
fields_dict: A dictionary mapping all attributes that will be serialized in the class to their marshmallow field type. (e.g. fields_dict ={"class_name": fields.Str()}).nested_fields: A dictionary mapping all nested attributes to a registry that contains class names mapped to the respective classes. (e.g. nested_fields ={"datastructure": datastructure.registry})
Raises
ValueError: Ifusernameis not provided andhubis not provided.
Ancestors
- bitfount.models.base_models._BaseModelRegistryMixIn
- bitfount.types._BaseSerializableObjectMixIn
Variables
- static
datastructure : Optional[DataStructure]
- static
fields_dict : ClassVar[dict[str, marshmallow.fields.Field]]
- static
nested_fields : ClassVar[dict[str, collections.abc.Mapping[str, Any]]]
-
model_id : str- The full model ID (or model slug) for a model.This is of the form "owner_name/model_name:model_version".
-
model_name : str- The name of the model being referenced.If the model referenced is one already hosted in the hub then this will be the model name it was registered with.
If the model referenced is a local file, to be uploaded to the hub, then this is the file stem (e.g.
MyModel.pyhas nameMyModel).
Static methods
from_model_ref_config
def from_model_ref_config( config: BitfountModelReferenceConfig, *, hub: Optional[BitfountHub] = None, secrets: Optional[Union[APIKeys, ExternallyManagedJWT]] = None, new_version: bool = False, private: bool = False, datastructure: Optional[DataStructure] = None, schema: Optional[BitfountSchema] = None, hyperparameters: Optional[_StrAnyDict] = None,) ‑> BitfountModelReference:Builds a BitfountModelReference instance from config.
Methods
get_model_from_hub
def get_model_from_hub( self, project_id: Optional[str] = None,) ‑> type[ModelProtocol]:Gets the model referenced.
If the model is a Path to a ModelProtocol, it will upload it to
BitfountHub and return the model class. If it is a name of a model on the hub,
it will download the model from the hub and return the model class.
If the model class is a v1 PyTorchBitfountModel it will be automatically converted to v2.
Returns The model class (auto converted to pytorch v2 if necessary).
Raises
TypeError: If the model is not a Path or a string.TypeError: If the model does not implementDistributedModelProtocol,InferrableModelProtocolorEvaluableModelProtocol.ValueError: If aBitfountHubinstance has not been provided or if there was a communication error with the hub.ValueError: If a datastructure has not been provided.
get_weights
def get_weights(self, project_id: Optional[str] = None) ‑> Optional[bytes]:Gets weights file uploaded for the model if one exists.
Returns The weights file as a byte stream.
send_weights
def send_weights(self, pretrained_file: Union[Path, str]) ‑> None:Sends the model weights from a pretrained file to Hub.
Arguments
pretrained_file: The path to the pretrained model file.
Raises
ValueError: Ifmodel_versionhas not been set on BitfountModelReference instance.
upload_model_and_weights
def upload_model_and_weights( self, project_id: Optional[str] = None,) ‑> type[ModelProtocol]:Uploads model and weights to the hub.
Should be used by the modeller to upload the model and weights to the hub at the beginning of a task.
Arguments
project_id: The project ID to upload the model to. Defaults to None.
Returns The model class (auto converted to PyTorchBitfountModelv2 if needed).