Esempio n. 1
0
 def test_job_file_with_init_passes(self):
     run_config = CompiledOperationSpecification.read([
         os.path.abspath("tests/fixtures/plain/job_file_with_init.yml"),
         {
             "kind": "compiled_operation"
         },
     ])
     run_config = CompiledOperationSpecification.apply_operation_contexts(
         run_config)
     assert run_config.version == 1.1
     assert isinstance(run_config.plugins, V1Plugins)
     assert run_config.plugins.log_level == "INFO"
     assert run_config.plugins.auth is True
     assert run_config.plugins.shm is True
     assert run_config.plugins.docker is True
     assert run_config.plugins.collect_artifacts is True
     assert run_config.plugins.collect_logs is None
     assert isinstance(run_config.run.environment, V1Environment)
     assert run_config.run.environment.labels == {"key": "value"}
     assert isinstance(run_config.run.init, list)
     assert len(run_config.run.connections) == 2
     assert run_config.run.connections == ["data1", "data2"]
     assert len(run_config.run.volumes) == 2
     assert run_config.run.volumes[0].name == "my_ssh_secret"
     assert run_config.run.volumes[0].secret == {"secretName": "mysecret"}
     assert run_config.run.volumes[1].name == "config_map"
     assert run_config.run.volumes[1].config_map == {
         "configName": "config_map2"
     }
Esempio n. 2
0
    def test_joins_pipeline(self):
        run_config = V1CompiledOperation.read([
            os.path.abspath(
                "tests/fixtures/pipelines/simple_joins_pipeline.yml"),
            {
                "kind": "compiled_operation"
            },
        ])

        run_config = CompiledOperationSpecification.apply_operation_contexts(
            run_config)
        assert len(run_config.run.operations) == 5
        assert run_config.run.operations[0].name == "job1"
        assert run_config.run.operations[0].dependencies is None
        assert run_config.run.operations[1].name == "job2"
        assert run_config.run.operations[1].dependencies is None
        assert run_config.run.operations[2].name == "experiment1"
        assert run_config.run.operations[2].dependencies is None
        assert run_config.run.operations[3].name == "experiment2"
        assert run_config.run.operations[3].dependencies is None
        assert run_config.run.operations[4].name == "reduce"
        assert run_config.run.operations[4].dependencies is None
        dag_strategy = run_config.run
        assert set(dag_strategy.sort_topologically(dag_strategy.dag)[0]) == {
            "job1",
            "job2",
            "experiment1",
            "experiment2",
            "reduce",
        }
        assert run_config.run.concurrency == 2
        assert run_config.schedule is None
Esempio n. 3
0
    def test_simple_file_passes(self):
        run_config = CompiledOperationSpecification.read(
            [
                os.path.abspath("tests/fixtures/plain/simple_job.yml"),
                {"kind": "compiled_operation"},
            ]
        )

        run_config = CompiledOperationSpecification.apply_operation_contexts(run_config)
        assert run_config.version == 1.1
        assert run_config.tags is None
        assert len(run_config.run.volumes) == 1
        assert run_config.run.to_dict()["volumes"][0] == {
            "name": "foo",
            "secret": {"secretName": "mysecret"},
        }
        assert run_config.run.container.image == "python-with-boto3"
        assert run_config.run.container.command == "python download-s3-bucket"
        assert run_config.run.container.resources == {
            "requests": {"nvidia.com/gpu": 1},
            "limits": {"nvidia.com/gpu": 1},
        }
        assert run_config.run.container.volume_mounts == [
            {"name": "foo", "mount_path": "~/.aws/credentials", "readOnly": True}
        ]
Esempio n. 4
0
    def test_matrix_file_passes_int_float_types(self):
        plx_file = check_polyaxonfile(
            polyaxonfile=os.path.abspath(
                "tests/fixtures/plain/matrix_job_file_with_int_float_types.yml"
            ),
            is_cli=False,
        )
        # Get compiled_operation data
        run_config = OperationSpecification.compile_operation(plx_file)

        run_config = CompiledOperationSpecification.apply_operation_contexts(
            run_config)
        assert run_config.version == 1.1
        assert isinstance(run_config.matrix, V1GridSearch)
        assert isinstance(run_config.matrix.params["param1"], V1HpChoice)
        assert isinstance(run_config.matrix.params["param2"], V1HpChoice)
        assert run_config.matrix.params["param1"].to_dict() == {
            "kind": "choice",
            "value": [1, 2],
        }
        assert run_config.matrix.params["param2"].to_dict() == {
            "kind": "choice",
            "value": [3.3, 4.4],
        }
        assert run_config.matrix.concurrency == 2
        assert isinstance(run_config.matrix, V1GridSearch)
        assert run_config.matrix.kind == V1GridSearch.IDENTIFIER
        assert run_config.matrix.early_stopping is None
Esempio n. 5
0
    def test_sequential_pipeline(self):
        run_config = V1CompiledOperation.read([
            os.path.abspath(
                "tests/fixtures/pipelines/simple_sequential_pipeline.yml"),
            {
                "kind": "compiled_operation"
            },
        ])

        run_config = CompiledOperationSpecification.apply_operation_contexts(
            run_config)
        assert run_config.run is not None
        assert len(run_config.run.operations) == 4
        assert run_config.run.operations[0].name == "job1"
        assert run_config.run.operations[1].name == "job2"
        assert run_config.run.operations[1].dependencies == ["job1"]
        assert run_config.run.operations[2].name == "experiment1"
        assert run_config.run.operations[2].dependencies == ["job2"]
        assert run_config.run.operations[3].name == "experiment2"
        assert run_config.run.operations[3].dependencies == ["experiment1"]
        dag_strategy = run_config.run
        assert dag_strategy.sort_topologically(dag_strategy.dag) == [
            ["job1"],
            ["job2"],
            ["experiment1"],
            ["experiment2"],
        ]
        assert run_config.schedule is None
Esempio n. 6
0
    def test_mpi_passes(self):
        run_config = CompiledOperationSpecification.read(
            [
                os.path.abspath("tests/fixtures/plain/distributed_mpi_file.yml"),
                {"kind": "compiled_operation"},
            ]
        )

        run_config = CompiledOperationSpecification.apply_operation_contexts(run_config)
        assert run_config.version == 1.1
        assert run_config.is_mpi_job_run
        assert run_config.termination is None
        assert run_config.run.launcher.replicas == 1
        assert run_config.run.launcher.to_dict() == {
            "replicas": 1,
            "container": {
                "name": "polyaxon-main",
                "image": "mpioperator/tensorflow-benchmarks:latest",
                "command": ["mpirun", "python", "run.py"],
            },
        }
        assert run_config.run.launcher.environment is None

        assert run_config.run.worker.replicas == 2
        assert run_config.run.worker.environment.affinity is None
        assert run_config.run.worker.environment.node_selector is not None
        assert isinstance(run_config.run.worker.environment.tolerations, list)
        assert run_config.run.worker.to_dict()["container"] == {
            "name": "polyaxon-main",
            "image": "mpioperator/tensorflow-benchmarks:latest",
            "command": ["mpirun", "python", "run.py"],
            "resources": {"limits": {"nvidia.com/gpu": 1}},
        }
Esempio n. 7
0
    def test_matrix_early_stopping_file_passes(self):
        plx_file = check_polyaxonfile(
            polyaxonfile=os.path.abspath(
                "tests/fixtures/plain/matrix_job_file_early_stopping.yml"),
            is_cli=False,
            to_op=False,
        )
        # Get compiled_operation data
        run_config = OperationSpecification.compile_operation(plx_file)

        run_config = CompiledOperationSpecification.apply_operation_contexts(
            run_config)
        assert run_config.version == 1.1
        assert isinstance(run_config.matrix, V1RandomSearch)
        assert isinstance(run_config.matrix.params["lr"], V1HpLinSpace)
        assert isinstance(run_config.matrix.params["loss"], V1HpChoice)
        assert run_config.matrix.params["lr"].to_dict() == {
            "kind": "linspace",
            "value": {
                "start": 0.01,
                "stop": 0.1,
                "num": 5
            },
        }
        assert run_config.matrix.params["loss"].to_dict() == {
            "kind": "choice",
            "value": ["MeanSquaredError", "AbsoluteDifference"],
        }
        assert run_config.matrix.concurrency == 2
        assert run_config.matrix.num_runs == 300
        assert isinstance(run_config.matrix, V1RandomSearch)
        assert run_config.matrix.kind == V1RandomSearch.IDENTIFIER
        assert len(run_config.matrix.early_stopping) == 1
        assert isinstance(run_config.matrix.early_stopping[0],
                          V1MetricEarlyStopping)
Esempio n. 8
0
    def test_tf_passes(self):
        run_config = CompiledOperationSpecification.read(
            [
                os.path.abspath("tests/fixtures/plain/distributed_tensorflow_file.yml"),
                {"kind": "compiled_operation"},
            ]
        )

        run_config = CompiledOperationSpecification.apply_operation_contexts(run_config)
        assert run_config.version == 1.1
        assert run_config.termination is not None
        assert run_config.termination.ttl == 12
        assert run_config.is_tf_job_run
        assert run_config.run.worker.replicas == 5
        assert run_config.run.worker.environment.affinity is not None
        assert run_config.run.worker.environment.restart_policy == "OnFailure"
        assert run_config.run.worker.container.resources == {
            "requests": {"memory": "300Mi"},
            "limits": {"memory": "300Mi"},
        }
        assert run_config.run.ps.replicas == 10
        assert run_config.run.ps.environment.affinity is None
        assert isinstance(run_config.run.ps.environment.tolerations, list)
        assert run_config.run.ps.environment.restart_policy == "OnFailure"
        assert run_config.run.ps.container.resources == {
            "requests": {"cpu": 3, "memory": "256Mi"},
            "limits": {"cpu": 3, "memory": "256Mi"},
        }
Esempio n. 9
0
    def test_pytorch_passes(self):
        run_config = CompiledOperationSpecification.read(
            [
                os.path.abspath("tests/fixtures/plain/distributed_pytorch_file.yml"),
                {"kind": "compiled_operation"},
            ]
        )
        run_config = CompiledOperationSpecification.apply_operation_contexts(run_config)
        assert run_config.version == 1.1

        assert run_config.termination is not None
        assert run_config.termination.ttl == 12

        assert run_config.is_pytorch_job_run
        assert run_config.run.master.replicas == 5
        assert run_config.run.master.environment.to_dict() == {
            "restartPolicy": "OnFailure",
            "nodeName": "foo",
            "serviceAccountName": "sa1",
        }
        assert run_config.run.master.container.image == "my_image"
        assert run_config.run.master.container.resources == {
            "requests": {"memory": "300Mi"},
            "limits": {"memory": "300Mi"},
        }
        assert run_config.run.worker.replicas == 10
        assert run_config.run.worker.environment.affinity is None
        assert run_config.run.worker.environment.restart_policy == "OnFailure"
        assert isinstance(run_config.run.worker.environment.tolerations, list)
        assert run_config.run.worker.container.resources == {
            "requests": {"cpu": 3, "memory": "256Mi"},
            "limits": {"cpu": 3, "memory": "256Mi"},
        }
Esempio n. 10
0
 def test_job_file_with_environment_passes(self):
     run_config = CompiledOperationSpecification.read(
         [
             os.path.abspath("tests/fixtures/plain/job_file_with_environment.yml"),
             {"kind": "compiled_operation"},
         ]
     )
     run_config = CompiledOperationSpecification.apply_operation_contexts(run_config)
     assert run_config.version == 1.1
     assert isinstance(run_config.run.environment, V1Environment)
     assert run_config.run.environment.node_selector == {"polyaxon.com": "core"}
     assert run_config.run.container.resources == {
         "requests": {"cpu": 1, "memory": 200},
         "limits": {"cpu": 2, "memory": 200},
     }
     assert run_config.run.environment.affinity.node_affinity == {
         "requiredDuringSchedulingIgnoredDuringExecution": {}
     }
     assert run_config.run.environment.tolerations[0].key == "key"
     assert run_config.run.environment.tolerations[0].operator == "Exists"
     assert run_config.run.environment.labels == {
         "label_key1": "val1",
         "label_key2": "val2",
     }
     assert run_config.run.environment.annotations == {
         "annotation_key1": "val1",
         "annotation_key2": "val2",
     }
     assert run_config.run.environment.service_account_name == "new_sa"
     assert run_config.run.environment.image_pull_secrets == ["secret1", "secret2"]
     assert run_config.run.environment.security_context.run_as_user == 1000
     assert run_config.run.environment.security_context.run_as_group == 3000
     assert isinstance(run_config.plugins, V1Plugins)
     assert run_config.plugins.log_level == "DEBUG"
Esempio n. 11
0
    def test_mapping_early_stopping_file_passes(self):
        plx_file = check_polyaxonfile(
            polyaxonfile=os.path.abspath(
                "tests/fixtures/plain/mapping_job_file_early_stopping.yml"),
            is_cli=False,
            to_op=False,
        )
        # Get compiled_operation data
        config_run = OperationSpecification.compile_operation(plx_file)

        config_run = CompiledOperationSpecification.apply_operation_contexts(
            config_run)
        assert config_run.version == 1.1
        assert isinstance(config_run.matrix, V1Mapping)
        assert config_run.matrix.values == [
            {
                "lr": 0.001,
                "loss": "MeanSquaredError"
            },
            {
                "lr": 0.1,
                "loss": "AbsoluteDifference"
            },
        ]
        assert config_run.matrix.concurrency == 2
        assert isinstance(config_run.matrix, V1Mapping)
        assert config_run.matrix.kind == V1Mapping.IDENTIFIER
        assert len(config_run.matrix.early_stopping) == 1
        assert isinstance(config_run.matrix.early_stopping[0],
                          V1MetricEarlyStopping)
Esempio n. 12
0
 def test_matrix_file_with_required_inputs_passes(self):
     plx_file = check_polyaxonfile(
         polyaxonfile=os.path.abspath(
             "tests/fixtures/typing/matrix_job_required_inputs_file.yml"
         ),
         is_cli=False,
     )
     run_config = OperationSpecification.compile_operation(plx_file)
     run_config = CompiledOperationSpecification.apply_operation_contexts(run_config)
     assert run_config.version == 1.1
     assert isinstance(run_config.matrix, V1Hyperband)
     assert isinstance(run_config.matrix.params["lr"], V1HpLinSpace)
     assert isinstance(run_config.matrix.params["loss"], V1HpChoice)
     assert run_config.matrix.params["lr"].to_dict() == {
         "kind": "linspace",
         "value": {"start": 0.01, "stop": 0.1, "num": 5},
     }
     assert run_config.matrix.params["loss"].to_dict() == {
         "kind": "choice",
         "value": ["MeanSquaredError", "AbsoluteDifference"],
     }
     assert run_config.matrix.concurrency == 2
     assert isinstance(run_config.matrix, V1Hyperband)
     assert run_config.matrix.kind == V1Hyperband.IDENTIFIER
     assert run_config.matrix.early_stopping is None
Esempio n. 13
0
    def test_dag_pipeline(self):
        run_config = V1CompiledOperation.read([
            os.path.abspath(
                "tests/fixtures/pipelines/simple_dag_pipeline.yml"),
            {
                "kind": "compiled_operation"
            },
        ])

        run_config = CompiledOperationSpecification.apply_operation_contexts(
            run_config)
        assert len(run_config.run.operations) == 5
        assert run_config.run.operations[0].name == "job1"
        assert run_config.run.operations[1].name == "experiment1"
        assert run_config.run.operations[1].dependencies == ["job1"]
        assert run_config.run.operations[2].name == "experiment2"
        assert run_config.run.operations[2].dependencies == ["job1"]
        assert run_config.run.operations[3].name == "experiment3"
        assert run_config.run.operations[3].dependencies == ["job1"]
        assert run_config.run.operations[4].name == "job2"
        assert run_config.run.operations[4].dependencies == [
            "experiment1",
            "experiment2",
            "experiment3",
        ]
        dag_strategy = run_config.run
        sorted_dag = dag_strategy.sort_topologically(dag_strategy.dag)
        assert sorted_dag[0] == ["job1"]
        assert set(
            sorted_dag[1]) == {"experiment1", "experiment2", "experiment3"}
        assert sorted_dag[2] == ["job2"]
        assert run_config.run.concurrency == 3
        assert run_config.schedule is None
Esempio n. 14
0
 def test_matrix_file_passes(self):
     run_config = V1CompiledOperation.read([
         os.path.abspath("tests/fixtures/pipelines/matrix_file.yml"),
         {
             "kind": "compiled_operation"
         },
     ])
     run_config = CompiledOperationSpecification.apply_operation_contexts(
         run_config)
     assert run_config.version == 1.1
     assert run_config.is_dag_run is True
     assert run_config.has_pipeline is True
     assert run_config.schedule is None
     assert run_config.run.concurrency == 4
     assert isinstance(run_config.run, V1Dag)
     assert run_config.run.early_stopping is None
     assert run_config.run.kind == V1Dag.IDENTIFIER
     assert len(run_config.run.operations) == 2
     assert len(run_config.run.components) == 1
     template_hyperband = run_config.run.operations[1].matrix
     assert isinstance(template_hyperband.params["lr"], V1HpLinSpace)
     assert isinstance(template_hyperband.params["loss"], V1HpChoice)
     assert template_hyperband.params["lr"].to_dict() == {
         "kind": "linspace",
         "value": {
             "start": 0.01,
             "stop": 0.1,
             "num": 5
         },
     }
     assert template_hyperband.params["loss"].to_dict() == {
         "kind": "choice",
         "value": ["MeanSquaredError", "AbsoluteDifference"],
     }
     assert template_hyperband.params["normal_rate"].to_dict() == {
         "kind": "normal",
         "value": {
             "loc": 0,
             "scale": 0.9
         },
     }
     assert template_hyperband.params["dropout"].to_dict() == {
         "kind": "qloguniform",
         "value": {
             "high": 0.8,
             "low": 0,
             "q": 0.1
         },
     }
     assert template_hyperband.params["activation"].to_dict() == {
         "kind": "pchoice",
         "value": [["relu", 0.1], ["sigmoid", 0.8]],
     }
     assert template_hyperband.params["model"].to_dict() == {
         "kind": "choice",
         "value": ["CDNA", "DNA", "STP"],
     }
     assert template_hyperband.concurrency == 2
     assert isinstance(template_hyperband, V1Hyperband)
Esempio n. 15
0
    def test_apply_params_extends_connections_and_init(self):
        content = {
            "version": 1.1,
            "kind": "compiled_operation",
            "inputs": [
                {"name": "docker_image", "type": types.IMAGE},
                {"name": "git_repo", "type": types.GIT},
            ],
            "run": {
                "kind": V1RunKind.JOB,
                "connections": ["{{ params.docker_image.connection }}"],
                "container": {
                    "name": "polyaxon-main",
                    "image": "{{ docker_image }}",
                    "command": "train",
                },
            },
        }
        run_config = V1CompiledOperation.read(content)
        # no params
        with self.assertRaises(ValidationError):
            CompiledOperationSpecification.apply_operation_contexts(run_config)

        params = {
            "docker_image": {
                "value": "destination:tag",
                "connection": "docker-registry",
            },
            "git_repo": {
                "value": V1GitType(revision="foo"),
                "connection": "repo-connection",
            },
        }

        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 params == {p.name: p.param.to_dict() for p in validated_params}
        assert run_config.inputs[0].connection == "docker-registry"
        assert run_config.inputs[1].connection == "repo-connection"
        run_config = CompiledOperationSpecification.apply_operation_contexts(run_config)
        run_config = CompiledOperationSpecification.apply_params(run_config)
        run_config = CompiledOperationSpecification.apply_runtime_contexts(run_config)
        assert run_config.run.connections == ["docker-registry"]
        assert run_config.run.container.image == "destination:tag"
Esempio n. 16
0
def generate(polyaxonfile, python_module, build_context, destination,
             copy_path, params, track):
    """Generate a dockerfile given the polyaxonfile."""
    from polyaxon.init.dockerfile import create_dockerfile_lineage
    from polyaxon.utils.hashing import hash_value

    if all([polyaxonfile, build_context]):
        Printer.print_error(
            "Only a polyaxonfile or a build context option is required.")
        sys.exit(1)

    if build_context:
        try:
            build_context = [
                V1DockerfileType.from_dict(ConfigSpec.read_from(build_context))
            ]
        except (PolyaxonSchemaError, ValidationError) as e:
            Printer.print_error("received a non valid build context.")
            Printer.print_error("Error message: {}.".format(e))
            sys.exit(1)
    else:
        specification = check_polyaxonfile(
            polyaxonfile=polyaxonfile,
            python_module=python_module,
            params=params,
            log=False,
        )

        try:
            compiled_operation = specification.compile_operation()
            compiled_operation.apply_params(params=specification.config.params)
            compiled_operation = CompiledOperationSpecification.apply_operation_contexts(
                compiled_operation)
        except PolyaxonSchemaError:
            Printer.print_error(
                "Could not run this polyaxonfile locally, "
                "a context is required to resolve it dependencies.")
            sys.exit(1)

        build_context = compiled_operation.init_dockerfiles

    for init_dockerfile in build_context:
        generator = DockerFileGenerator(build_context=init_dockerfile,
                                        destination=destination or ".")
        generator.create()
        Printer.print_success("Dockerfile was generated, path: `{}`".format(
            generator.dockerfile_path))

        dockerfile_path = generator.dockerfile_path
        if copy_path:
            dockerfile_path = copy_file(dockerfile_path, copy_path)
        if track:
            hash_content = hash_value(init_dockerfile.to_dict())
            create_dockerfile_lineage(dockerfile_path,
                                      summary={"hash": hash_content})
Esempio n. 17
0
    def test_no_params_for_required_inputs_outputs_raises(self):
        # Get compiled_operation data
        run_config = V1CompiledOperation.read(
            [
                os.path.abspath("tests/fixtures/typing/required_inputs.yml"),
                {"kind": "compiled_operation"},
            ]
        )

        # Inputs don't have delayed validation by default
        with self.assertRaises(ValidationError):
            CompiledOperationSpecification.apply_operation_contexts(run_config)

        run_config = V1CompiledOperation.read(
            [
                os.path.abspath("tests/fixtures/typing/required_outputs.yml"),
                {"kind": "compiled_operation"},
            ]
        )
        # Outputs have delayed validation by default
        CompiledOperationSpecification.apply_operation_contexts(run_config)
Esempio n. 18
0
 def test_matrix_file_passes(self):
     plx_file = check_polyaxonfile(
         polyaxonfile=os.path.abspath(
             "tests/fixtures/plain/matrix_job_file.yml"),
         is_cli=False,
     )
     run_config = OperationSpecification.compile_operation(plx_file)
     run_config = CompiledOperationSpecification.apply_operation_contexts(
         run_config)
     assert run_config.version == 1.1
     assert isinstance(run_config.matrix, V1Hyperband)
     assert isinstance(run_config.matrix.params["lr"], V1HpLinSpace)
     assert isinstance(run_config.matrix.params["loss"], V1HpChoice)
     assert run_config.matrix.params["lr"].to_dict() == {
         "kind": "linspace",
         "value": {
             "start": 0.01,
             "stop": 0.1,
             "num": 5
         },
     }
     assert run_config.matrix.params["loss"].to_dict() == {
         "kind": "choice",
         "value": ["MeanSquaredError", "AbsoluteDifference"],
     }
     assert run_config.matrix.params["normal_rate"].to_dict() == {
         "kind": "normal",
         "value": {
             "loc": 0,
             "scale": 0.9
         },
     }
     assert run_config.matrix.params["dropout"].to_dict() == {
         "kind": "qloguniform",
         "value": {
             "high": 0.8,
             "low": 0,
             "q": 0.1
         },
     }
     assert run_config.matrix.params["activation"].to_dict() == {
         "kind": "pchoice",
         "value": [["relu", 0.1], ["sigmoid", 0.8]],
     }
     assert run_config.matrix.params["model"].to_dict() == {
         "kind": "choice",
         "value": ["CDNA", "DNA", "STP"],
     }
     assert run_config.matrix.concurrency == 2
     assert isinstance(run_config.matrix, V1Hyperband)
     assert run_config.matrix.kind == V1Hyperband.IDENTIFIER
     assert run_config.matrix.early_stopping is None
Esempio n. 19
0
    def test_specification_with_quotes(self):
        run_config = CompiledOperationSpecification.read(
            [
                os.path.abspath("tests/fixtures/plain/polyaxonfile_with_quotes.yaml"),
                {"kind": "compiled_operation"},
            ]
        )

        run_config = CompiledOperationSpecification.apply_operation_contexts(run_config)
        expected_run = {
            "kind": V1RunKind.JOB,
            "container": {
                "image": "continuumio/miniconda3",
                "command": ["python"],
                "args": ["-c \"print('Tweet tweet')\""],
                "name": "polyaxon-main",
            },
        }
        assert run_config.run.to_dict() == expected_run
        run_config = V1CompiledOperation.read(run_config.to_dict())
        run_config = CompiledOperationSpecification.apply_operation_contexts(run_config)
        assert run_config.run.to_dict() == expected_run
Esempio n. 20
0
 def test_job_file_with_termination_passes(self):
     run_config = CompiledOperationSpecification.read(
         [
             os.path.abspath("tests/fixtures/plain/job_file_with_termination.yml"),
             {"kind": "compiled_operation"},
         ]
     )
     run_config = CompiledOperationSpecification.apply_operation_contexts(run_config)
     assert run_config.version == 1.1
     assert isinstance(run_config.termination, V1Termination)
     assert run_config.termination.max_retries == 5
     assert run_config.termination.timeout == 500
     assert run_config.termination.ttl == 400
     assert run_config.run.environment.restart_policy == "Never"
Esempio n. 21
0
    def test_matrix_early_stopping_file_passes(self):
        run_config = V1CompiledOperation.read([
            os.path.abspath(
                "tests/fixtures/pipelines/matrix_file_early_stopping.yml"),
            {
                "kind": "compiled_operation"
            },
        ])

        run_config = CompiledOperationSpecification.apply_operation_contexts(
            run_config)
        assert run_config.run is not None
        assert run_config.is_dag_run is True
        assert run_config.has_pipeline is True
        assert run_config.schedule is None
        assert run_config.run.concurrency == 4
        assert isinstance(run_config.run, V1Dag)
        assert run_config.run.early_stopping[
            0].kind == "failure_early_stopping"
        assert isinstance(run_config.run.early_stopping[0],
                          V1FailureEarlyStopping)
        assert len(run_config.run.early_stopping) == 1
        assert run_config.run.kind == V1Dag.IDENTIFIER
        assert len(run_config.run.operations) == 2
        assert len(run_config.run.components) == 1
        template_random = run_config.run.operations[1].matrix
        assert isinstance(template_random, V1RandomSearch)
        assert isinstance(template_random.params["lr"], V1HpLinSpace)
        assert isinstance(template_random.params["loss"], V1HpChoice)
        assert template_random.params["lr"].to_dict() == {
            "kind": "linspace",
            "value": {
                "start": 0.01,
                "stop": 0.1,
                "num": 5
            },
        }
        assert template_random.params["loss"].to_dict() == {
            "kind": "choice",
            "value": ["MeanSquaredError", "AbsoluteDifference"],
        }
        assert template_random.concurrency == 2
        assert template_random.num_runs == 300
        assert template_random.early_stopping[
            0].kind == "metric_early_stopping"
        assert len(template_random.early_stopping) == 1
        assert isinstance(template_random.early_stopping[0],
                          V1MetricEarlyStopping)
Esempio n. 22
0
    def test_cron_pipeline(self):
        plx_file = check_polyaxonfile(
            polyaxonfile=os.path.abspath(
                "tests/fixtures/pipelines/simple_cron_pipeline.yml"),
            is_cli=False,
            to_op=False,
        )
        # Get compiled_operation data
        run_config = OperationSpecification.compile_operation(plx_file)

        run_config = CompiledOperationSpecification.apply_operation_contexts(
            run_config)
        assert run_config.run is not None
        assert len(run_config.run.operations) == 1
        assert run_config.run.operations[0].name == "cron-task"
        assert run_config.schedule is not None
        assert run_config.schedule.kind == "cron"
        assert run_config.schedule.cron == "0 0 * * *"
Esempio n. 23
0
 def test_refs_pipeline(self):
     run_config = V1CompiledOperation.read([
         os.path.abspath("tests/fixtures/pipelines/ref_pipeline.yml"),
         {
             "kind": "compiled_operation"
         },
     ])
     with patch(
             "polyaxon.config_reader.spec.ConfigSpec.read") as config_read:
         config_read.return_value = V1Component(
             kind="component",
             version=" 1.1",
             inputs=[V1IO(name="str-input", iotype="str")],
             run=V1Job(container=V1Container(name="test")),
         ).to_dict()
         compiled_op = CompiledOperationSpecification.apply_operation_contexts(
             run_config)
     assert compiled_op.run is not None
     assert len(compiled_op.run.operations) == 2
     assert compiled_op.run.operations[0].name == "ref-path-op"
     assert compiled_op.run.operations[1].name == "ref-url-op"
Esempio n. 24
0
    def test_interval_pipeline(self):
        plx_file = check_polyaxonfile(
            polyaxonfile=os.path.abspath(
                "tests/fixtures/pipelines/simple_recurrent_pipeline.yml"),
            is_cli=False,
            to_op=False,
        )
        # Get compiled_operation data
        run_config = OperationSpecification.compile_operation(plx_file)

        run_config = CompiledOperationSpecification.apply_operation_contexts(
            run_config)
        assert run_config.run is not None
        assert len(run_config.run.operations) == 1
        assert run_config.run.operations[0].name == "recurrent-task"
        assert run_config.schedule is not None
        assert run_config.schedule.kind == "interval"
        assert run_config.schedule.start_at.year == 2019
        assert run_config.schedule.frequency.seconds == 120
        assert run_config.schedule.depends_on_past is True
        assert run_config.schedule is not None
Esempio n. 25
0
    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)
Esempio n. 26
0
    def test_matrix_file_passes_int_float_types(self):
        run_config = V1CompiledOperation.read([
            os.path.abspath(
                "tests/fixtures/pipelines/matrix_file_with_int_float_types.yml"
            ),
            {
                "kind": "compiled_operation"
            },
        ])

        run_config = CompiledOperationSpecification.apply_operation_contexts(
            run_config)
        assert run_config.version == 1.1
        assert run_config.is_dag_run is True
        assert run_config.has_pipeline is True
        assert run_config.schedule is None
        assert run_config.run.concurrency == 4
        assert isinstance(run_config.run, V1Dag)
        assert run_config.run.early_stopping is None
        assert run_config.run.kind == V1Dag.IDENTIFIER
        assert len(run_config.run.operations) == 2
        assert len(run_config.run.components) == 1
        template_grid = run_config.run.operations[1].matrix
        assert isinstance(template_grid, V1GridSearch)
        assert isinstance(template_grid.params["param1"], V1HpChoice)
        assert isinstance(template_grid.params["param2"], V1HpChoice)
        assert template_grid.params["param1"].to_dict() == {
            "kind": "choice",
            "value": [1, 2],
        }
        assert template_grid.params["param2"].to_dict() == {
            "kind": "choice",
            "value": [3.3, 4.4],
        }
        assert template_grid.concurrency == 2
        assert template_grid.early_stopping is None
Esempio n. 27
0
    def test_specification_with_context_requirement(self):
        context_root = container_contexts.CONTEXT_ROOT
        contexts = {
            "globals": {
                "owner_name": "user",
                "project_name": "project",
                "project_unique_name": "user.project",
                "project_uuid": "uuid",
                "run_info": "user.project.runs.uuid",
                "name": "run",
                "uuid": "uuid",
                "context_path": "/plx-context",
                "artifacts_path": "{}/artifacts".format(context_root),
                "run_artifacts_path": "{}/artifacts/test".format(context_root),
                "run_outputs_path": "{}/artifacts/test/outputs".format(context_root),
                "namespace": "test",
                "iteration": 12,
                "ports": [1212, 1234],
                "base_url": "/services/v1/test/user/project/runs/uuid",
                "created_at": None,
                "compiled_at": None,
                "cloning_kind": None,
                "original_uuid": None,
            },
            "init": {},
            "connections": {"foo": {"key": "connection-value"}},
        }
        run_config = CompiledOperationSpecification.read(
            [
                os.path.abspath(
                    "tests/fixtures/plain/polyaxonfile_with_contexts_requirements.yaml"
                ),
                {"kind": "compiled_operation"},
            ]
        )

        run_config = CompiledOperationSpecification.apply_operation_contexts(run_config)
        expected_run = {
            "kind": V1RunKind.JOB,
            "init": [
                {
                    "artifacts": {"files": ["{{globals.run_outputs_path}}/foo"]},
                    "connection": "{{connections['foo']['key']}}",
                }
            ],
            "container": {
                "image": "continuumio/miniconda3",
                "command": ["python"],
                "workingDir": "{{ globals.artifacts_path }}/repo",
                "args": ["-c \"print('Tweet tweet')\""],
                "name": "polyaxon-main",
            },
        }
        assert run_config.run.to_dict() == expected_run
        run_config = V1CompiledOperation.read(run_config.to_dict())
        run_config = CompiledOperationSpecification.apply_operation_contexts(run_config)
        assert run_config.run.to_dict() == expected_run

        expected_run = {
            "kind": V1RunKind.JOB,
            "init": [
                {
                    "artifacts": {
                        "files": [
                            "{}/artifacts/test/outputs/foo".format(
                                container_contexts.CONTEXT_ROOT
                            )
                        ],
                    },
                    "connection": "connection-value",
                }
            ],
            "container": {
                "image": "continuumio/miniconda3",
                "command": ["python"],
                "workingDir": "{}/artifacts/repo".format(
                    container_contexts.CONTEXT_ROOT
                ),
                "args": ["-c \"print('Tweet tweet')\""],
                "name": "polyaxon-main",
            },
        }
        run_config = CompiledOperationSpecification.apply_runtime_contexts(
            run_config, contexts=contexts
        )
        assert run_config.run.to_dict() == expected_run
Esempio n. 28
0
    def test_spec_with_optional_inputs(self):
        content = {
            "version":
            1.1,
            "kind":
            "compiled_operation",
            "inputs": [
                {
                    "name": "lr",
                    "type": types.FLOAT,
                    "value": 0.6,
                    "isOptional": True
                },
                {
                    "name": "num_steps",
                    "type": types.INT,
                    "value": 16,
                    "isOptional": True,
                },
            ],
            "run": {
                "kind": V1RunKind.JOB,
                "container": {
                    "name": "polyaxon-main",
                    "image": "test/test:latest",
                    "command": "train",
                },
            },
        }
        config = V1CompiledOperation.read(content)
        assert config.inputs[0].value == 0.6
        assert config.inputs[1].value == 16
        config = CompiledOperationSpecification.apply_operation_contexts(
            config)
        validated_params = config.validate_params()
        assert {
            "lr": 0.6,
            "num_steps": 16
        } == {p.name: p.param.value
              for p in validated_params}
        assert config.inputs[0].value == 0.6
        assert config.inputs[1].value == 16

        # Passing params
        content["params"] = {"lr": 0.1}
        with self.assertRaises(ValidationError):  # not valid
            V1CompiledOperation.read(content)

        content.pop("params", None)

        # Add env
        assert config.run.container.resources is None
        content["run"]["container"]["resources"] = {
            "requests": {
                "gpu": 1,
                "tpu": 1
            },
            "limits": {
                "gpu": 1,
                "tpu": 1
            },
        }
        config = V1CompiledOperation.read(content)
        assert config.run.container.resources == {
            "requests": {
                "gpu": 1,
                "tpu": 1
            },
            "limits": {
                "gpu": 1,
                "tpu": 1
            },
        }

        # Passing unsupported spec
        content["hptuning"] = {"params": {"lr": {"values": [0.1, 0.2]}}}
        with self.assertRaises(ValidationError):
            V1CompiledOperation.read(content)

        content.pop("hptuning", None)

        # Patch with unsupported spec
        with self.assertRaises(ValidationError):
            content["lr"] = {"values": [0.1, 0.2]}
            V1CompiledOperation.read(content)
Esempio n. 29
0
    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"])
Esempio n. 30
0
    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)