Skip to content

Wrappers

Reference information for the model Wrappers API.

eva.models.wrappers.BaseModel

Bases: Module

Base class for model wrappers.

Parameters:

Name Type Description Default
tensor_transforms Callable | None

The transforms to apply to the output tensor produced by the model.

None
Source code in src/eva/core/models/wrappers/base.py
def __init__(self, tensor_transforms: Callable | None = None) -> None:
    """Initializes the model.

    Args:
        tensor_transforms: The transforms to apply to the output
            tensor produced by the model.
    """
    super().__init__()

    self._output_transforms = tensor_transforms

    self._model: Callable[..., torch.Tensor] | nn.Module

load_model abstractmethod

Loads the model.

Source code in src/eva/core/models/wrappers/base.py
@abc.abstractmethod
def load_model(self) -> Callable[..., torch.Tensor]:
    """Loads the model."""
    raise NotImplementedError

model_forward

Implements the forward pass of the model.

Parameters:

Name Type Description Default
tensor Tensor

The input tensor to the model.

required
Source code in src/eva/core/models/wrappers/base.py
def model_forward(self, tensor: torch.Tensor) -> torch.Tensor:
    """Implements the forward pass of the model.

    Args:
        tensor: The input tensor to the model.
    """
    return self._model(tensor)

eva.models.wrappers.ModelFromFunction

Bases: BaseModel

Wrapper class for models which are initialized from functions.

This is helpful for initializing models in a .yaml configuration file.

Parameters:

Name Type Description Default
path Callable[..., Module]

The path to the callable object (class or function).

required
arguments Dict[str, Any] | None

The extra callable function / class arguments.

None
checkpoint_path str | None

The path to the checkpoint to load the model weights from. This is currently only supported for torch model checkpoints. For other formats, the checkpoint loading should be handled within the provided callable object in .

None
tensor_transforms Callable | None

The transforms to apply to the output tensor produced by the model.

None
Source code in src/eva/core/models/wrappers/from_function.py
def __init__(
    self,
    path: Callable[..., nn.Module],
    arguments: Dict[str, Any] | None = None,
    checkpoint_path: str | None = None,
    tensor_transforms: Callable | None = None,
) -> None:
    """Initializes and constructs the model.

    Args:
        path: The path to the callable object (class or function).
        arguments: The extra callable function / class arguments.
        checkpoint_path: The path to the checkpoint to load the model
            weights from. This is currently only supported for torch
            model checkpoints. For other formats, the checkpoint loading
            should be handled within the provided callable object in <path>.
        tensor_transforms: The transforms to apply to the output tensor
            produced by the model.
    """
    super().__init__(tensor_transforms=tensor_transforms)

    self._path = path
    self._arguments = arguments
    self._checkpoint_path = checkpoint_path

    self.load_model()

eva.models.wrappers.HuggingFaceModel

Bases: BaseModel

Wrapper class for loading HuggingFace transformers models.

Parameters:

Name Type Description Default
model_name_or_path str

The model name or path to load the model from. This can be a local path or a model name from the HuggingFace model hub.

required
tensor_transforms Callable | None

The transforms to apply to the output tensor produced by the model.

None
model_kwargs Dict[str, Any] | None

The arguments used for instantiating the model.

None
Source code in src/eva/core/models/wrappers/huggingface.py
def __init__(
    self,
    model_name_or_path: str,
    tensor_transforms: Callable | None = None,
    model_kwargs: Dict[str, Any] | None = None,
) -> None:
    """Initializes the model.

    Args:
        model_name_or_path: The model name or path to load the model from.
            This can be a local path or a model name from the `HuggingFace`
            model hub.
        tensor_transforms: The transforms to apply to the output tensor
            produced by the model.
        model_kwargs: The arguments used for instantiating the model.
    """
    super().__init__(tensor_transforms=tensor_transforms)

    self._model_name_or_path = model_name_or_path
    self._model_kwargs = model_kwargs or {}

    self.load_model()

eva.models.wrappers.ONNXModel

Bases: BaseModel

Wrapper class for loading ONNX models.

Parameters:

Name Type Description Default
path str

The path to the .onnx model file.

required
device Literal['cpu', 'cuda'] | None

The device to run the model on. This can be either "cpu" or "cuda".

'cpu'
tensor_transforms Callable | None

The transforms to apply to the output tensor produced by the model.

None
Source code in src/eva/core/models/wrappers/onnx.py
def __init__(
    self,
    path: str,
    device: Literal["cpu", "cuda"] | None = "cpu",
    tensor_transforms: Callable | None = None,
):
    """Initializes the model.

    Args:
        path: The path to the .onnx model file.
        device: The device to run the model on. This can be either "cpu" or "cuda".
        tensor_transforms: The transforms to apply to the output tensor produced by the model.
    """
    super().__init__(tensor_transforms=tensor_transforms)

    self._path = path
    self._device = device

    self.load_model()