def test_apply_context_raises_with_required_inputs(self): content = { "version": 1.1, "kind": "component", "inputs": [ {"name": "lr", "type": types.FLOAT}, {"name": "num_steps", "type": types.INT}, ], "run": { "kind": V1RunKind.JOB, "container": { "name": "polyaxon-main", "image": "test/test:latest", "command": "train", }, }, } component_config = V1Component.read(content) assert component_config.to_dict() == content content = { "version": 1.1, "kind": "compiled_operation", "inputs": [ {"name": "lr", "type": types.FLOAT}, {"name": "num_steps", "type": types.INT}, ], "run": { "kind": V1RunKind.JOB, "container": { "name": "polyaxon-main", "image": "test/test:latest", "command": "train", }, }, } run_config = V1CompiledOperation.read(content) # Raise because required inputs are not met with self.assertRaises(ValidationError): CompiledOperationSpecification.apply_operation_contexts(run_config) # Validation for template should pass validated_params = run_config.validate_params() assert {"lr": None, "num_steps": None} == { p.name: p.param.value for p in validated_params } # Validation for non template should raise with self.assertRaises(ValidationError): run_config.validate_params(is_template=False)
def test_spec_without_io_and_params_raises(self): content = { "version": 1.1, "kind": "component", "run": { "kind": V1RunKind.JOB, "container": { "name": "polyaxon-main", "image": "test/test:latest", "command": "train", }, }, } config = V1Component.read(content) assert config.to_dict() == content content = { "version": 1.1, "kind": "compiled_operation", "run": { "kind": V1RunKind.JOB, "container": { "name": "polyaxon-main", "image": "test/test:latest", "command": "train", }, }, } config = V1CompiledOperation.read(content) config = CompiledOperationSpecification.apply_operation_contexts( config) assert config.to_dict() == content # Add params content["params"] = {"lr": 0.1} with self.assertRaises(ValidationError): V1CompiledOperation.read(content)
def test_apply_context_passes_with_required_inputs_and_params(self): content = { "version": 1.1, "kind": "component", "inputs": [ { "name": "lr", "type": types.FLOAT }, { "name": "num_steps", "type": types.INT }, ], "run": { "kind": V1RunKind.JOB, "container": { "name": "polyaxon-main", "image": "test/test:latest", "command": "train", }, }, } component_config = V1Component.read(content) assert component_config.to_dict() == content content = { "version": 1.1, "kind": "compiled_operation", "inputs": [ { "name": "lr", "type": types.FLOAT }, { "name": "num_steps", "type": types.INT }, ], "run": { "kind": V1RunKind.JOB, "container": { "name": "polyaxon-main", "image": "test/test:latest", "command": "train", }, }, } run_config = V1CompiledOperation.read(content) # no params with self.assertRaises(ValidationError): CompiledOperationSpecification.apply_operation_contexts(run_config) params = { "lr": V1Param(value=0.1), "num_steps": V1Param.from_dict({"value": 100}), } assert run_config.inputs[0].value is None assert run_config.inputs[1].value is None validated_params = run_config.validate_params(params=params) run_config.apply_params(params=params) assert {k: v.to_dict() for k, v in params.items() } == {p.name: p.param.to_dict() for p in validated_params} assert run_config.inputs[0].value == 0.1 assert run_config.inputs[1].value == 100 run_config = CompiledOperationSpecification.apply_operation_contexts( run_config) updated_content = { "version": 1.1, "kind": "compiled_operation", "inputs": [ { "name": "lr", "type": types.FLOAT, "isOptional": True, "value": 0.1 }, { "name": "num_steps", "type": types.INT, "isOptional": True, "value": 100, }, ], "run": { "kind": V1RunKind.JOB, "container": { "name": "polyaxon-main", "image": "test/test:latest", "command": "train", }, }, } assert run_config.to_dict() == updated_content updated_content["run"]["container"]["resources"] = { "requests": { "gpu": 1, "tpu": 1 }, "limits": { "gpu": 1, "tpu": 1 }, } run_config = V1CompiledOperation.read(updated_content) assert (run_config.run.container.resources == updated_content["run"] ["container"]["resources"])