Esempio n. 1
0
def prepare_parameters(kwargs: Dict[str, Any],
                       method: Callable,
                       is_init: bool = False):
    """Prepares paramters passed into components before calling SDK.

    1. Determines the annotation type that should used with the parameter
    2. Reads input values if needed
    3. Deserializes thos value where appropriate
    4. Or casts to the correct type.

    Args:
        kwargs (Dict[str, Any]): The kwargs that will be passed into method. Mutates in place.
        method (Callable): The method the kwargs used to invoke the method.
        is_init (bool): Whether this method is a constructor
    """
    for key, param in inspect.signature(method).parameters.items():
        if key in kwargs:
            value = kwargs[key]
            param_type = utils.resolve_annotation(param.annotation)
            value = resolve_init_args(
                key, value) if is_init else resolve_input_args(
                    value, param_type)
            deserializer = utils.get_deserializer(param_type)
            if deserializer:
                value = deserializer(value)
            else:
                value = cast(value, param_type)
            kwargs[key] = value
Esempio n. 2
0
    def test_custom_training_typed_dataset_annotation_defaults_to_using_base_dataset(
            self):

        dataset_annotation = signature(
            aiplatform.CustomTrainingJob.run).parameters['dataset'].annotation

        assert utils.resolve_annotation(
            dataset_annotation) is aiplatform.datasets.dataset._Dataset

        dataset_annotation = signature(aiplatform.CustomContainerTrainingJob.
                                       run).parameters['dataset'].annotation

        assert utils.resolve_annotation(
            dataset_annotation) is aiplatform.datasets.dataset._Dataset

        dataset_annotation = signature(
            aiplatform.CustomPythonPackageTrainingJob.run
        ).parameters['dataset'].annotation

        assert utils.resolve_annotation(
            dataset_annotation) is aiplatform.datasets.dataset._Dataset
Esempio n. 3
0
def prepare_parameters(kwargs: Dict[str, Any],
                       method: Callable,
                       is_init: bool = False):
    """Prepares parameters passed into components before calling SDK.

    1. Determines the annotation type that should used with the parameter
    2. Reads input values if needed
    3. Deserializes thos value where appropriate
    4. Or casts to the correct type.

    Args:
        kwargs (Dict[str, Any]): The kwargs that will be passed into method. Mutates in place.
        method (Callable): The method the kwargs used to invoke the method.
        is_init (bool): Whether this method is a constructor
    """
    for key, param in inspect.signature(method).parameters.items():
        if key in kwargs:
            value = kwargs[key]
            param_type = utils.resolve_annotation(param.annotation)
            value = resolve_init_args(
                key, value) if is_init else resolve_input_args(
                    value, param_type)
            deserializer = utils.get_deserializer(param_type)
            if deserializer:
                value = deserializer(value)
            else:
                value = cast(value, param_type)

            try:
                # Attempt at converting String to list:
                # Some parameters accept union[str, sequence[str]]
                # For these serialization with json is not possible as
                # component yaml conversion swaps double and single
                # quotes resulting in `JSON.Loads` loading such a list as
                # a String. Using ast.literal_eval to attempt to convert the
                # str back to a python List.
                value = ast.literal_eval(value)
                print(f"Conversion for value succeeded for value: {value}")
            except:
                # The input was actually a String and not a List,
                # no additional transformations are required.
                pass

            kwargs[key] = value
Esempio n. 4
0
    def test_resolve_annotation_with_annotation_type_empty(self):
        annotation = None

        results = utils.resolve_annotation(annotation)
        self.assertEqual(results, None)
Esempio n. 5
0
    def test_resolve_annotation_with_annotation_type_union(self):
        annotation = Union[Dict, None]

        results = utils.resolve_annotation(annotation)
        self.assertEqual(results, Dict)
Esempio n. 6
0
    def test_resolve_annotation_with_annotation_foward_typed_reference(self):
        annotation = ForwardRef(aiplatform.Model.__name__)

        results = utils.resolve_annotation(annotation)
        self.assertEqual(results, aiplatform.Model)
Esempio n. 7
0
    def test_resolve_annotation_with_annotation_class(self):
        annotation = aiplatform.Model

        results = utils.resolve_annotation(annotation)
        self.assertEqual(results, annotation)