diff --git a/assets/training/model_management/components/condition_block/asset.yaml b/assets/training/model_management/components/condition_block/asset.yaml new file mode 100644 index 0000000000..ea30e0bd5b --- /dev/null +++ b/assets/training/model_management/components/condition_block/asset.yaml @@ -0,0 +1,3 @@ +type: component +spec: spec.yaml +categories: ["Models"] diff --git a/assets/training/model_management/components/condition_block/spec.yaml b/assets/training/model_management/components/condition_block/spec.yaml new file mode 100644 index 0000000000..02a4786d13 --- /dev/null +++ b/assets/training/model_management/components/condition_block/spec.yaml @@ -0,0 +1,34 @@ +$schema: https://azuremlschemas.azureedge.net/latest/commandComponent.schema.json + +name: condition_block +version: 0.0.1 +type: command + +is_deterministic: True + +display_name: Condition Block +description: Component takes a condition and executes the command based on the condition. + +environment: azureml://registries/azureml/environments/model-management/versions/34 + +code: ../../src/ +command: | + set -ex + IFS=',' read -ra input_args <<< "${{inputs.args}}" + echo "Running condition block ..." + python -u run_condition_block.py --condition ${{inputs.condition}} --input-args "${{inputs.args}}" + echo "Completed condition block ... " + +inputs: + condition: + type: string + description: Input condition. + optional: false + + extra_pip_requirements: + type: string + description: | + Input arguments. + Arguments expressed as below. Do not use quotes for passing. + eg: a==1.0, b==1 + optional: false \ No newline at end of file diff --git a/assets/training/model_management/src/run_condition_block.py b/assets/training/model_management/src/run_condition_block.py new file mode 100644 index 0000000000..9366112381 --- /dev/null +++ b/assets/training/model_management/src/run_condition_block.py @@ -0,0 +1,58 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +"""Run Model preprocessor module.""" + +import argparse +import os +import json +import shutil +from azureml.model.mgmt.config import AppName, ModelFramework +from azureml.model.mgmt.processors.transformers.config import HF_CONF +from azureml.model.mgmt.processors.preprocess import run_preprocess, check_for_py_files +from azureml.model.mgmt.processors.transformers.config import SupportedTasks as TransformersSupportedTasks +from azureml.model.mgmt.processors.pyfunc.config import SupportedTasks as PyFuncSupportedTasks +from azureml.model.mgmt.utils.exceptions import swallow_all_exceptions, UnsupportedTaskType +from azureml._common.exceptions import AzureMLException +from azureml._common._error_definition.azureml_error import AzureMLError +from azureml.model.mgmt.utils.logging_utils import custom_dimensions, get_logger +from pathlib import Path +from tempfile import TemporaryDirectory +import json + + +logger = get_logger(__name__) +custom_dimensions.app_name = AppName.CONVERT_MODEL_TO_MLFLOW + + +def _get_parser(): + parser = argparse.ArgumentParser() + parser.add_argument("--condition", type=str, required=True, help="Condition") + parser.add_argument( + "--input-args", + type=str, + required=True, + help="Input args", + ) + return parser + + +@swallow_all_exceptions(logger) +def run(): + """Run preprocess.""" + parser = _get_parser() + args, _ = parser.parse_known_args() + + input_args = args.input_args + condition = args.condition + input_args = json.loads(input_args) + result = None + try: + result = eval(condition, input_args) + except Exception as e: + logger.error(f"Error evaluating condition: {e}") + result = False + + +if __name__ == "__main__": + run()