opsml.model.interfaces.vowpal

  1from pathlib import Path
  2from typing import Any, Dict, Optional
  3
  4from pydantic import model_validator
  5
  6from opsml.helpers.utils import get_class_name
  7from opsml.model.interfaces.base import ModelInterface
  8from opsml.types import CommonKwargs, ModelReturn, Suffix, TrainedModelType
  9
 10try:
 11    import vowpalwabbit as vw
 12
 13    class VowpalWabbitModel(ModelInterface):
 14        """Model interface for VowPal Wabbit model.
 15
 16        Args:
 17            model:
 18                vowpal wabbit workspace
 19            sample_data:
 20                Sample data to be used for type inference.
 21                For vowpal wabbit models this should be a string.
 22            arguments:
 23                Vowpal Wabbit arguments. This will be inferred automatically from the workspace
 24
 25        Returns:
 26            VowpalWabbitModel
 27        """
 28
 29        model: Optional[vw.Workspace] = None
 30        sample_data: Optional[str] = None
 31        arguments: str = ""
 32
 33        @property
 34        def model_class(self) -> str:
 35            return TrainedModelType.VOWPAL.value
 36
 37        @classmethod
 38        def _get_sample_data(cls, sample_data: str) -> str:
 39            """Check sample data and returns one record to be used
 40            during type inference and ONNX conversion/validation.
 41
 42            Returns:
 43                Sample data with only one record
 44            """
 45
 46            assert isinstance(sample_data, str), "Sample data must be a string"
 47            return sample_data
 48
 49        @model_validator(mode="before")
 50        @classmethod
 51        def check_model(cls, model_args: Dict[str, Any]) -> Dict[str, Any]:
 52            model = model_args.get("model")
 53
 54            if model_args.get("modelcard_uid", False):
 55                return model_args
 56
 57            assert model is not None, "Model must not be None"
 58
 59            sample_data = cls._get_sample_data(sample_data=model_args[CommonKwargs.SAMPLE_DATA.value])
 60            model_args[CommonKwargs.SAMPLE_DATA.value] = sample_data
 61            model_args[CommonKwargs.DATA_TYPE.value] = get_class_name(sample_data)
 62            model_args[CommonKwargs.VOWPAL_ARGS.value] = model.get_arguments()
 63
 64            return model_args
 65
 66        def save_model(self, path: Path) -> None:
 67            """Saves vowpal model to ".model" file.
 68
 69            Args:
 70                path:
 71                    base path to save model to
 72            """
 73            assert self.model is not None, "No model found"
 74            self.model.save(path.as_posix())
 75
 76        def load_model(self, path: Path, **kwargs: Any) -> None:
 77            """Loads a vowpal model from ".model" file with arguments.
 78
 79            Args:
 80                path:
 81                    base path to load from
 82                **kwargs:
 83                    Additional arguments to be passed to the workspace. This is supplied via
 84                    "arguments" key.
 85
 86                    Example:
 87                        arguments="--cb 4"
 88                        or
 89                        arguments=["--cb", "4"]
 90
 91                    There is no need to specify "-i" argument. This is done automatically during loading.
 92            """
 93            args = kwargs.get("arguments", self.arguments)
 94
 95            if args is None:
 96                args = self.arguments
 97
 98            elif isinstance(args, str):
 99                args = args + f" -i {path.as_posix()}"
100
101            elif isinstance(args, list):
102                args.append(f"-i {path.as_posix()}")
103                args = " ".join(args)
104
105            else:
106                raise ValueError("Arguments must be a string or a list")
107
108            self.model = vw.Workspace(args)
109
110        def save_onnx(self, path: Path) -> ModelReturn:  # pylint: disable=redundant-returns-doc
111            """Onnx is not supported for vowpal wabbit models.
112
113            Args:
114                path:
115                    Path to save
116
117            Returns:
118                ModelReturn
119            """
120            raise ValueError("Onnx is not supported for vowpal wabbit models")
121
122        @property
123        def model_suffix(self) -> str:
124            """Returns suffix for model storage"""
125            return Suffix.MODEL.value
126
127        @staticmethod
128        def name() -> str:
129            return VowpalWabbitModel.__name__
130
131except ModuleNotFoundError:
132    from opsml.model.interfaces.backups import (
133        VowpalWabbitModelNoModule as VowpalWabbitModel,
134    )
class VowpalWabbitModel(opsml.model.interfaces.base.ModelInterface):
 14    class VowpalWabbitModel(ModelInterface):
 15        """Model interface for VowPal Wabbit model.
 16
 17        Args:
 18            model:
 19                vowpal wabbit workspace
 20            sample_data:
 21                Sample data to be used for type inference.
 22                For vowpal wabbit models this should be a string.
 23            arguments:
 24                Vowpal Wabbit arguments. This will be inferred automatically from the workspace
 25
 26        Returns:
 27            VowpalWabbitModel
 28        """
 29
 30        model: Optional[vw.Workspace] = None
 31        sample_data: Optional[str] = None
 32        arguments: str = ""
 33
 34        @property
 35        def model_class(self) -> str:
 36            return TrainedModelType.VOWPAL.value
 37
 38        @classmethod
 39        def _get_sample_data(cls, sample_data: str) -> str:
 40            """Check sample data and returns one record to be used
 41            during type inference and ONNX conversion/validation.
 42
 43            Returns:
 44                Sample data with only one record
 45            """
 46
 47            assert isinstance(sample_data, str), "Sample data must be a string"
 48            return sample_data
 49
 50        @model_validator(mode="before")
 51        @classmethod
 52        def check_model(cls, model_args: Dict[str, Any]) -> Dict[str, Any]:
 53            model = model_args.get("model")
 54
 55            if model_args.get("modelcard_uid", False):
 56                return model_args
 57
 58            assert model is not None, "Model must not be None"
 59
 60            sample_data = cls._get_sample_data(sample_data=model_args[CommonKwargs.SAMPLE_DATA.value])
 61            model_args[CommonKwargs.SAMPLE_DATA.value] = sample_data
 62            model_args[CommonKwargs.DATA_TYPE.value] = get_class_name(sample_data)
 63            model_args[CommonKwargs.VOWPAL_ARGS.value] = model.get_arguments()
 64
 65            return model_args
 66
 67        def save_model(self, path: Path) -> None:
 68            """Saves vowpal model to ".model" file.
 69
 70            Args:
 71                path:
 72                    base path to save model to
 73            """
 74            assert self.model is not None, "No model found"
 75            self.model.save(path.as_posix())
 76
 77        def load_model(self, path: Path, **kwargs: Any) -> None:
 78            """Loads a vowpal model from ".model" file with arguments.
 79
 80            Args:
 81                path:
 82                    base path to load from
 83                **kwargs:
 84                    Additional arguments to be passed to the workspace. This is supplied via
 85                    "arguments" key.
 86
 87                    Example:
 88                        arguments="--cb 4"
 89                        or
 90                        arguments=["--cb", "4"]
 91
 92                    There is no need to specify "-i" argument. This is done automatically during loading.
 93            """
 94            args = kwargs.get("arguments", self.arguments)
 95
 96            if args is None:
 97                args = self.arguments
 98
 99            elif isinstance(args, str):
100                args = args + f" -i {path.as_posix()}"
101
102            elif isinstance(args, list):
103                args.append(f"-i {path.as_posix()}")
104                args = " ".join(args)
105
106            else:
107                raise ValueError("Arguments must be a string or a list")
108
109            self.model = vw.Workspace(args)
110
111        def save_onnx(self, path: Path) -> ModelReturn:  # pylint: disable=redundant-returns-doc
112            """Onnx is not supported for vowpal wabbit models.
113
114            Args:
115                path:
116                    Path to save
117
118            Returns:
119                ModelReturn
120            """
121            raise ValueError("Onnx is not supported for vowpal wabbit models")
122
123        @property
124        def model_suffix(self) -> str:
125            """Returns suffix for model storage"""
126            return Suffix.MODEL.value
127
128        @staticmethod
129        def name() -> str:
130            return VowpalWabbitModel.__name__

Model interface for VowPal Wabbit model.

Arguments:
  • model: vowpal wabbit workspace
  • sample_data: Sample data to be used for type inference. For vowpal wabbit models this should be a string.
  • arguments: Vowpal Wabbit arguments. This will be inferred automatically from the workspace
Returns:

VowpalWabbitModel

model: Optional[vowpalwabbit.pyvw.Workspace]
sample_data: Optional[str]
arguments: str
model_class: str
34        @property
35        def model_class(self) -> str:
36            return TrainedModelType.VOWPAL.value
@model_validator(mode='before')
@classmethod
def check_model(cls, model_args: Dict[str, Any]) -> Dict[str, Any]:
50        @model_validator(mode="before")
51        @classmethod
52        def check_model(cls, model_args: Dict[str, Any]) -> Dict[str, Any]:
53            model = model_args.get("model")
54
55            if model_args.get("modelcard_uid", False):
56                return model_args
57
58            assert model is not None, "Model must not be None"
59
60            sample_data = cls._get_sample_data(sample_data=model_args[CommonKwargs.SAMPLE_DATA.value])
61            model_args[CommonKwargs.SAMPLE_DATA.value] = sample_data
62            model_args[CommonKwargs.DATA_TYPE.value] = get_class_name(sample_data)
63            model_args[CommonKwargs.VOWPAL_ARGS.value] = model.get_arguments()
64
65            return model_args
def save_model(self, path: pathlib.Path) -> None:
67        def save_model(self, path: Path) -> None:
68            """Saves vowpal model to ".model" file.
69
70            Args:
71                path:
72                    base path to save model to
73            """
74            assert self.model is not None, "No model found"
75            self.model.save(path.as_posix())

Saves vowpal model to ".model" file.

Arguments:
  • path: base path to save model to
def load_model(self, path: pathlib.Path, **kwargs: Any) -> None:
 77        def load_model(self, path: Path, **kwargs: Any) -> None:
 78            """Loads a vowpal model from ".model" file with arguments.
 79
 80            Args:
 81                path:
 82                    base path to load from
 83                **kwargs:
 84                    Additional arguments to be passed to the workspace. This is supplied via
 85                    "arguments" key.
 86
 87                    Example:
 88                        arguments="--cb 4"
 89                        or
 90                        arguments=["--cb", "4"]
 91
 92                    There is no need to specify "-i" argument. This is done automatically during loading.
 93            """
 94            args = kwargs.get("arguments", self.arguments)
 95
 96            if args is None:
 97                args = self.arguments
 98
 99            elif isinstance(args, str):
100                args = args + f" -i {path.as_posix()}"
101
102            elif isinstance(args, list):
103                args.append(f"-i {path.as_posix()}")
104                args = " ".join(args)
105
106            else:
107                raise ValueError("Arguments must be a string or a list")
108
109            self.model = vw.Workspace(args)

Loads a vowpal model from ".model" file with arguments.

Arguments:
  • path: base path to load from
  • **kwargs: Additional arguments to be passed to the workspace. This is supplied via "arguments" key.

    Example: arguments="--cb 4" or arguments=["--cb", "4"]

    There is no need to specify "-i" argument. This is done automatically during loading.

def save_onnx(self, path: pathlib.Path) -> opsml.types.model.ModelReturn:
111        def save_onnx(self, path: Path) -> ModelReturn:  # pylint: disable=redundant-returns-doc
112            """Onnx is not supported for vowpal wabbit models.
113
114            Args:
115                path:
116                    Path to save
117
118            Returns:
119                ModelReturn
120            """
121            raise ValueError("Onnx is not supported for vowpal wabbit models")

Onnx is not supported for vowpal wabbit models.

Arguments:
  • path: Path to save
Returns:

ModelReturn

model_suffix: str
123        @property
124        def model_suffix(self) -> str:
125            """Returns suffix for model storage"""
126            return Suffix.MODEL.value

Returns suffix for model storage

@staticmethod
def name() -> str:
128        @staticmethod
129        def name() -> str:
130            return VowpalWabbitModel.__name__
model_config = {'protected_namespaces': ('protect_',), 'arbitrary_types_allowed': True, 'validate_assignment': False, 'validate_default': True, 'extra': 'allow'}
model_fields = {'model': FieldInfo(annotation=Union[Workspace, NoneType], required=False), 'sample_data': FieldInfo(annotation=Union[str, NoneType], required=False), 'onnx_model': FieldInfo(annotation=Union[OnnxModel, NoneType], required=False), 'task_type': FieldInfo(annotation=str, required=False, default='undefined'), 'model_type': FieldInfo(annotation=str, required=False, default='undefined'), 'data_type': FieldInfo(annotation=str, required=False, default='undefined'), 'modelcard_uid': FieldInfo(annotation=str, required=False, default=''), 'arguments': FieldInfo(annotation=str, required=False, default='')}
model_computed_fields = {}
Inherited Members
pydantic.main.BaseModel
BaseModel
model_extra
model_fields_set
model_construct
model_copy
model_dump
model_dump_json
model_json_schema
model_parametrized_name
model_post_init
model_rebuild
model_validate
model_validate_json
model_validate_strings
dict
json
parse_obj
parse_raw
parse_file
from_orm
construct
copy
schema
schema_json
validate
update_forward_refs
opsml.model.interfaces.base.ModelInterface
onnx_model
task_type
model_type
data_type
modelcard_uid
check_modelcard_uid
convert_to_onnx
load_onnx_model
save_sample_data
load_sample_data
get_sample_prediction
data_suffix