Skip to content

Plugins

DeciClient

A client to deci platform and model zoo. requires credentials for connection

Source code in common/plugins/deci_client.py
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
class DeciClient:
    """
    A client to deci platform and model zoo.
    requires credentials for connection
    """

    def __init__(self):
        if not client_enabled:
            logger.error(
                "deci-lab-client or deci-common are not installed. Model cannot be loaded from deci lab."
                "Please install deci-lab-client>=2.55.0 and deci-common>=3.4.1"
            )
            return

        self.api_host = env_variables.DECI_API_HOST
        self.lab_client = DeciPlatformClient(api_host=self.api_host)
        self.lab_client.login(token=env_variables.DECI_PLATFORM_TOKEN)

    def _get_file(self, model_name: str, file_name: str) -> Optional[str]:
        """Get a file from the DeciPlatform if it exists, otherwise returns None
        :param model_name:      Name of the model to download from, as saved in the platform.
        :param file_name:       Name of the file to download
        :return:            Path were the downloaded file was saved to. None if not found.
        """
        try:
            response = self.lab_client.get_autonac_model_file_link(
                model_name=model_name, file_name=file_name, super_gradients_version=super_gradients.__version__
            )
            download_link = response.data
        except ApiException as e:
            if e.status == 401:
                logger.error(
                    "Unauthorized. wrong token or token was not defined. please login to deci-lab-client " "by calling DeciPlatformClient().login(<token>)"
                )
            elif e.status == 400 and e.body is not None and "message" in e.body:
                logger.error(f"Deci client: {json.loads(e.body)['message']}")
            else:
                logger.debug(e.body)
            return None

        file_path = FilesDataInterface.download_temporary_file(file_url=download_link)

        return file_path

    def get_model_arch_params(self, model_name: str) -> Optional[DictConfig]:
        """Get the model arch_params from DeciPlatform.
        :param model_name:  Name of the model as saved in the platform.
        :return:            arch_params. None if arch_params were not found for this specific model on this SG version."""
        arch_params_file = self._get_file(model_name, AutoNACFileName.STRUCTURE_YAML)
        if arch_params_file is None:
            return None

        config_name = os.path.basename(arch_params_file)
        download_dir = os.path.dirname(arch_params_file)

        # The arch_params config files need to be saved inside an "arch_params" folder
        _move_file_to_folder(src_file_path=arch_params_file, dest_dir_name="arch_params")

        return load_arch_params(config_name=config_name, recipes_dir_path=download_dir)

    def get_model_recipe(self, model_name: str) -> Optional[DictConfig]:
        """Get the model recipe from DeciPlatform.
        :param model_name:  Name of the model as saved in the platform.
        :return:            recipe. None if recipe were not found for this specific model on this SG version."""
        recipe_file = self._get_file(model_name, AutoNACFileName.RECIPE_YAML)
        if recipe_file is None:
            return None

        config_name = os.path.basename(recipe_file)
        download_dir = os.path.dirname(recipe_file)

        return load_recipe(config_name=config_name, recipes_dir_path=download_dir)

    def get_model_weights(self, model_name: str) -> Optional[str]:
        """Get the path to model weights (downloaded locally).
        :param model_name:  Name of the model as saved in the platform.
        :return:            model_weights path. None if weights were not found for this specific model on this SG version."""
        return self._get_file(model_name=model_name, file_name=AutoNACFileName.WEIGHTS_PTH)

    def download_and_load_model_additional_code(self, model_name: str, target_path: str, package_name: str = "deci_model_code") -> None:
        """
        try to download code files for this model.
        if found, code files will be placed in the target_path/package_name and imported dynamically
        """

        file = self._get_file(model_name=model_name, file_name=AutoNACFileName.CODE_ZIP)

        package_path = os.path.join(target_path, package_name)
        if file is not None:
            # crete the directory
            os.makedirs(package_path, exist_ok=True)

            # extract code files
            with ZipFile(file) as zipfile:
                zipfile.extractall(package_path)

            # add an init file that imports all code files
            with open(os.path.join(package_path, "__init__.py"), "w") as init_file:
                all_str = "\n\n__all__ = ["
                for code_file in os.listdir(path=package_path):
                    if code_file.endswith(".py") and not code_file.startswith("__init__"):
                        init_file.write(f'import {code_file.replace(".py", "")}\n')
                        all_str += f'"{code_file.replace(".py", "")}", '

                all_str += "]\n\n"
                init_file.write(all_str)

            # include in path and import
            sys.path.insert(1, package_path)
            importlib.import_module(package_name)

            logger.info(
                f"*** IMPORTANT ***: files required for the model {model_name} were downloaded and added to your code in:\n{package_path}\n"
                f"These files will be downloaded to the same location each time the model is fetched from the deci-client.\n"
                f"you can override this by passing models.get(... download_required_code=False) and importing the files yourself"
            )

    def upload_model(self, model: nn.Module, model_meta_data, optimization_request_form):
        """
        This function will upload the trained model to the Deci Lab

        :param model:                     The resulting model from the training process
        :param model_meta_data:           Metadata to accompany the model
        :param optimization_request_form: The optimization parameters
        """
        self.lab_client.add_model(
            add_model_request=model_meta_data,
            optimization_request=optimization_request_form,
            local_loaded_model=model,
        )

    def is_model_benchmarking(self, name: str) -> bool:
        """Check if a given model is still benchmarking or not.
        :param name: The mode name.
        """
        benchmark_state = self.lab_client.get_model_by_name(name=name).data.benchmark_state
        return benchmark_state in [ModelBenchmarkState.IN_PROGRESS, ModelBenchmarkState.PENDING]

    def register_experiment(self, name: str, model_name: str, resume: bool):
        """Registers a training experiment in Deci's backend.
        :param name:        Name of the experiment to register
        :param model_name:  Name of the model architecture to connect the experiment to
        """
        try:
            self.lab_client.register_user_architecture(BodyRegisterUserArchitecture(architecture_name=model_name))
        except ApiException as e:
            if e.status == 422:
                logger.debug(f"The model was already registered, or validation error: {e.body}")
            else:
                raise e

        self.lab_client.register_experiment(name=name, model_name=model_name, resume=resume)

    def save_experiment_file(self, file_path: str):
        """
        Uploads a training related file to Deci's location in S3. This can be a TensorBoard file or a log
        :params file_path: The local path of the file to be uploaded
        """
        self.lab_client.save_experiment_file(file_path=file_path)

    def upload_file_to_s3(self, tag: str, level: str, from_path: str):
        """Upload a file to the platform S3 bucket.

        :param tag:         Tag that will be associated to the file.
        :param level:       Logging level that will be used to notify the monitoring system that the file was uploaded.
        :param from_path:   Path of the file to upload.
        """
        data = self.lab_client.upload_log_url(tag=tag, level=level)
        signed_url = S3SignedUrl(**data.data)
        self.lab_client.upload_file_to_s3(from_path=from_path, s3_signed_url=signed_url)

    def add_model(
        self,
        model_metadata,
        hardware_types: List[str],
        model_path: Optional[str] = None,
        model: Optional[nn.Module] = None,
        **kwargs: Any,
    ):
        """Adds a new model to the company's model repository.
        :param model_metadata: The model metadata.
        :param hardware_types: The hardware types you want to benchmark the model on.
        :param model_path:      The path of the model on the local operating system.
        :param model:           Pytorch loaded model object.
                                If your model's framework is pytorch you may pass the following parameters as kwargs in order to control the conversion to onnx
        :param kwargs: Extra arguments to be passed to the PyTorch to ONNX conversion, for example:
            opset_version
            do_constant_folding
            dynamic_axes
            input_names
            output_names
        """

        self.lab_client.add_model_v2(model_metadata=model_metadata, hardware_types=hardware_types, model_path=model_path, model=model, **kwargs)

add_model(model_metadata, hardware_types, model_path=None, model=None, **kwargs)

Adds a new model to the company's model repository.

Parameters:

Name Type Description Default
model_metadata

The model metadata.

required
hardware_types List[str]

The hardware types you want to benchmark the model on.

required
model_path Optional[str]

The path of the model on the local operating system.

None
model Optional[nn.Module]

Pytorch loaded model object. If your model's framework is pytorch you may pass the following parameters as kwargs in order to control the conversion to onnx

None
kwargs Any

Extra arguments to be passed to the PyTorch to ONNX conversion, for example: opset_version do_constant_folding dynamic_axes input_names output_names

{}
Source code in common/plugins/deci_client.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
def add_model(
    self,
    model_metadata,
    hardware_types: List[str],
    model_path: Optional[str] = None,
    model: Optional[nn.Module] = None,
    **kwargs: Any,
):
    """Adds a new model to the company's model repository.
    :param model_metadata: The model metadata.
    :param hardware_types: The hardware types you want to benchmark the model on.
    :param model_path:      The path of the model on the local operating system.
    :param model:           Pytorch loaded model object.
                            If your model's framework is pytorch you may pass the following parameters as kwargs in order to control the conversion to onnx
    :param kwargs: Extra arguments to be passed to the PyTorch to ONNX conversion, for example:
        opset_version
        do_constant_folding
        dynamic_axes
        input_names
        output_names
    """

    self.lab_client.add_model_v2(model_metadata=model_metadata, hardware_types=hardware_types, model_path=model_path, model=model, **kwargs)

download_and_load_model_additional_code(model_name, target_path, package_name='deci_model_code')

try to download code files for this model. if found, code files will be placed in the target_path/package_name and imported dynamically

Source code in common/plugins/deci_client.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def download_and_load_model_additional_code(self, model_name: str, target_path: str, package_name: str = "deci_model_code") -> None:
    """
    try to download code files for this model.
    if found, code files will be placed in the target_path/package_name and imported dynamically
    """

    file = self._get_file(model_name=model_name, file_name=AutoNACFileName.CODE_ZIP)

    package_path = os.path.join(target_path, package_name)
    if file is not None:
        # crete the directory
        os.makedirs(package_path, exist_ok=True)

        # extract code files
        with ZipFile(file) as zipfile:
            zipfile.extractall(package_path)

        # add an init file that imports all code files
        with open(os.path.join(package_path, "__init__.py"), "w") as init_file:
            all_str = "\n\n__all__ = ["
            for code_file in os.listdir(path=package_path):
                if code_file.endswith(".py") and not code_file.startswith("__init__"):
                    init_file.write(f'import {code_file.replace(".py", "")}\n')
                    all_str += f'"{code_file.replace(".py", "")}", '

            all_str += "]\n\n"
            init_file.write(all_str)

        # include in path and import
        sys.path.insert(1, package_path)
        importlib.import_module(package_name)

        logger.info(
            f"*** IMPORTANT ***: files required for the model {model_name} were downloaded and added to your code in:\n{package_path}\n"
            f"These files will be downloaded to the same location each time the model is fetched from the deci-client.\n"
            f"you can override this by passing models.get(... download_required_code=False) and importing the files yourself"
        )

get_model_arch_params(model_name)

Get the model arch_params from DeciPlatform.

Parameters:

Name Type Description Default
model_name str

Name of the model as saved in the platform.

required

Returns:

Type Description
Optional[DictConfig]

arch_params. None if arch_params were not found for this specific model on this SG version.

Source code in common/plugins/deci_client.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def get_model_arch_params(self, model_name: str) -> Optional[DictConfig]:
    """Get the model arch_params from DeciPlatform.
    :param model_name:  Name of the model as saved in the platform.
    :return:            arch_params. None if arch_params were not found for this specific model on this SG version."""
    arch_params_file = self._get_file(model_name, AutoNACFileName.STRUCTURE_YAML)
    if arch_params_file is None:
        return None

    config_name = os.path.basename(arch_params_file)
    download_dir = os.path.dirname(arch_params_file)

    # The arch_params config files need to be saved inside an "arch_params" folder
    _move_file_to_folder(src_file_path=arch_params_file, dest_dir_name="arch_params")

    return load_arch_params(config_name=config_name, recipes_dir_path=download_dir)

get_model_recipe(model_name)

Get the model recipe from DeciPlatform.

Parameters:

Name Type Description Default
model_name str

Name of the model as saved in the platform.

required

Returns:

Type Description
Optional[DictConfig]

recipe. None if recipe were not found for this specific model on this SG version.

Source code in common/plugins/deci_client.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def get_model_recipe(self, model_name: str) -> Optional[DictConfig]:
    """Get the model recipe from DeciPlatform.
    :param model_name:  Name of the model as saved in the platform.
    :return:            recipe. None if recipe were not found for this specific model on this SG version."""
    recipe_file = self._get_file(model_name, AutoNACFileName.RECIPE_YAML)
    if recipe_file is None:
        return None

    config_name = os.path.basename(recipe_file)
    download_dir = os.path.dirname(recipe_file)

    return load_recipe(config_name=config_name, recipes_dir_path=download_dir)

get_model_weights(model_name)

Get the path to model weights (downloaded locally).

Parameters:

Name Type Description Default
model_name str

Name of the model as saved in the platform.

required

Returns:

Type Description
Optional[str]

model_weights path. None if weights were not found for this specific model on this SG version.

Source code in common/plugins/deci_client.py
105
106
107
108
109
def get_model_weights(self, model_name: str) -> Optional[str]:
    """Get the path to model weights (downloaded locally).
    :param model_name:  Name of the model as saved in the platform.
    :return:            model_weights path. None if weights were not found for this specific model on this SG version."""
    return self._get_file(model_name=model_name, file_name=AutoNACFileName.WEIGHTS_PTH)

is_model_benchmarking(name)

Check if a given model is still benchmarking or not.

Parameters:

Name Type Description Default
name str

The mode name.

required
Source code in common/plugins/deci_client.py
163
164
165
166
167
168
def is_model_benchmarking(self, name: str) -> bool:
    """Check if a given model is still benchmarking or not.
    :param name: The mode name.
    """
    benchmark_state = self.lab_client.get_model_by_name(name=name).data.benchmark_state
    return benchmark_state in [ModelBenchmarkState.IN_PROGRESS, ModelBenchmarkState.PENDING]

register_experiment(name, model_name, resume)

Registers a training experiment in Deci's backend.

Parameters:

Name Type Description Default
name str

Name of the experiment to register

required
model_name str

Name of the model architecture to connect the experiment to

required
Source code in common/plugins/deci_client.py
170
171
172
173
174
175
176
177
178
179
180
181
182
183
def register_experiment(self, name: str, model_name: str, resume: bool):
    """Registers a training experiment in Deci's backend.
    :param name:        Name of the experiment to register
    :param model_name:  Name of the model architecture to connect the experiment to
    """
    try:
        self.lab_client.register_user_architecture(BodyRegisterUserArchitecture(architecture_name=model_name))
    except ApiException as e:
        if e.status == 422:
            logger.debug(f"The model was already registered, or validation error: {e.body}")
        else:
            raise e

    self.lab_client.register_experiment(name=name, model_name=model_name, resume=resume)

save_experiment_file(file_path)

Uploads a training related file to Deci's location in S3. This can be a TensorBoard file or a log

Parameters:

Name Type Description Default
file_path str

The local path of the file to be uploaded

required
Source code in common/plugins/deci_client.py
185
186
187
188
189
190
def save_experiment_file(self, file_path: str):
    """
    Uploads a training related file to Deci's location in S3. This can be a TensorBoard file or a log
    :params file_path: The local path of the file to be uploaded
    """
    self.lab_client.save_experiment_file(file_path=file_path)

upload_file_to_s3(tag, level, from_path)

Upload a file to the platform S3 bucket.

Parameters:

Name Type Description Default
tag str

Tag that will be associated to the file.

required
level str

Logging level that will be used to notify the monitoring system that the file was uploaded.

required
from_path str

Path of the file to upload.

required
Source code in common/plugins/deci_client.py
192
193
194
195
196
197
198
199
200
201
def upload_file_to_s3(self, tag: str, level: str, from_path: str):
    """Upload a file to the platform S3 bucket.

    :param tag:         Tag that will be associated to the file.
    :param level:       Logging level that will be used to notify the monitoring system that the file was uploaded.
    :param from_path:   Path of the file to upload.
    """
    data = self.lab_client.upload_log_url(tag=tag, level=level)
    signed_url = S3SignedUrl(**data.data)
    self.lab_client.upload_file_to_s3(from_path=from_path, s3_signed_url=signed_url)

upload_model(model, model_meta_data, optimization_request_form)

This function will upload the trained model to the Deci Lab

Parameters:

Name Type Description Default
model nn.Module

The resulting model from the training process

required
model_meta_data

Metadata to accompany the model

required
optimization_request_form

The optimization parameters

required
Source code in common/plugins/deci_client.py
149
150
151
152
153
154
155
156
157
158
159
160
161
def upload_model(self, model: nn.Module, model_meta_data, optimization_request_form):
    """
    This function will upload the trained model to the Deci Lab

    :param model:                     The resulting model from the training process
    :param model_meta_data:           Metadata to accompany the model
    :param optimization_request_form: The optimization parameters
    """
    self.lab_client.add_model(
        add_model_request=model_meta_data,
        optimization_request=optimization_request_form,
        local_loaded_model=model,
    )