Esempio n. 1
0
def get(path, root, type):
    public_api = PublicApi()
    entity, project, artifact_name = public_api._parse_artifact_path(path)
    if project is None:
        project = click.prompt("Enter the name of the project you want to use")

    try:
        artifact_parts = artifact_name.split(":")
        if len(artifact_parts) > 1:
            version = artifact_parts[1]
            artifact_name = artifact_parts[0]
        else:
            version = "latest"
        full_path = "{entity}/{project}/{artifact}:{version}".format(
            entity=entity, project=project, artifact=artifact_name, version=version
        )
        wandb.termlog(
            "Downloading {type} artifact {full_path}".format(
                type=type or "dataset", full_path=full_path
            )
        )
        artifact = public_api.artifact(full_path, type=type)
        path = artifact.download(root=root)
        wandb.termlog("Artifact downloaded to %s" % path)
    except ValueError:
        raise ClickException("Unable to download artifact")
Esempio n. 2
0
    def use_artifact(self, artifact, type=None, aliases=None):
        """ Declare an artifact as an input to a run, call `download` or `file` on \
        the returned object to get the contents locally.

        Args:
            artifact_or_name (str or Artifact): An artifact name. May be prefixed with entity/project. Valid names
                can be in the following forms:
                    name:version
                    name:alias
                    digest
                You can also pass an Artifact object created by calling `wandb.Artifact`
            type (str, optional): The type of artifact to use.
            aliases (list, optional): Aliases to apply to this artifact
        Returns:
            A :obj:`Artifact` object.
        """
        if self.mode == "dryrun":
            wandb.termwarn("Using artifacts in dryrun mode is currently unsupported.")
            return artifact
        self.history.ensure_jupyter_started()
        if isinstance(artifact, six.string_types):
            # Ensure we have an entity (_load_entity puts the result into api.settings())
            # then initialize PublicApi with it.
            self._load_entity(self.api, True)
            public_api = PublicApi(self.api.settings())
            artifact = public_api.artifact(name=artifact)
            if type is not None and type != artifact.type:
                raise ValueError('Supplied type {} does not match type {} of artifact {}'.format(
                    type, artifact.type, artifact.name))
            self.api.use_artifact(artifact.id)
            return artifact
        else:
            if isinstance(aliases, six.string_types):
                aliases = [aliases]
            if isinstance(artifact, wandb.Artifact):
                artifact.finalize()
                self.send_message({
                    'use_artifact': {
                        'type': artifact.type,
                        'name': artifact.name,
                        'server_manifest_entries': artifact.server_manifest.entries,
                        'manifest': artifact.manifest.to_manifest_json(include_local=True),
                        'digest': artifact.digest,
                        'metadata': artifact.metadata,
                        'aliases': aliases
                    }
                })
                return artifact
            elif isinstance(artifact, ApiArtifact):
                self.api.use_artifact(artifact.id)
                return artifact
            else:
                raise ValueError('You must pass an artifact name (e.g. "pedestrian-dataset:v1"), an instance of wandb.Artifact, or wandb.Api().artifact() to use_artifact')
Esempio n. 3
0
def test_run_in_launch_context_with_artifacts_api(runner, live_mock_server,
                                                  test_settings, capsys):
    live_mock_server.set_ctx({"swappable_artifacts": True})
    arti = {
        "name": "test:v0",
        "project": "test",
        "entity": "test",
        "_version": "v0",
        "_type": "artifactVersion",
        "id": "QXJ0aWZhY3Q6NTI1MDk4",
    }
    overrides = {
        "overrides": {
            "run_config": {
                "epochs": 10
            },
            "artifacts": {
                "old_name:v0": arti
            },
        },
    }
    with runner.isolated_filesystem():
        path = _project_spec.DEFAULT_LAUNCH_METADATA_PATH
        with open(path, "w") as fp:
            json.dump(overrides, fp)
        test_settings.update(launch=True,
                             source=wandb.sdk.wandb_settings.Source.INIT)
        test_settings.update(launch_config_path=path,
                             source=wandb.sdk.wandb_settings.Source.INIT)
        run = wandb.init(settings=test_settings,
                         config={
                             "epochs": 2,
                             "lr": 0.004
                         })
        public_api = PublicApi()
        art = public_api.artifact("old_name:v0")
        arti_inst = run.use_artifact(art)
        assert run.config.epochs == 10
        assert run.config.lr == 0.004
        run.finish()
        assert arti_inst.name == "old_name:v0"
        arti_info = live_mock_server.get_ctx()["used_artifact_info"]
        assert arti_info["used_name"] == "old_name:v0"
        _, err = capsys.readouterr()
        assert (
            "Swapping artifacts does not support swapping artifacts used as an instance of"
            in err)
Esempio n. 4
0
 def use_artifact(self, artifact, type=None, aliases=None):
     if self.mode == "dryrun":
         wandb.termwarn(
             "Using artifacts in dryrun mode is currently unsupported.")
         return artifact
     self.history.ensure_jupyter_started()
     if isinstance(artifact, str):
         if type is None:
             raise ValueError('type required')
         public_api = PublicApi()
         artifact = public_api.artifact(type=type, name=artifact)
         self.api.use_artifact(artifact.id)
         return artifact
     else:
         if type is not None:
             raise ValueError(
                 'cannot specify type when passing Artifact object')
         if isinstance(aliases, str):
             aliases = [aliases]
         if isinstance(artifact, wandb.Artifact):
             artifact.finalize()
             self.send_message({
                 'use_artifact': {
                     'type':
                     artifact.type,
                     'name':
                     artifact.name,
                     'server_manifest_entries':
                     artifact.server_manifest.entries,
                     'manifest':
                     artifact.manifest.to_manifest_json(include_local=True),
                     'digest':
                     artifact.digest,
                     'metadata':
                     artifact.metadata,
                     'aliases':
                     aliases
                 }
             })
         elif isinstance(artifact, ApiArtifact):
             self.api.use_artifact(artifact.id)
             return artifact
         else:
             raise ValueError(
                 'You must pass an artifact name (e.g. "pedestrian-dataset:v1"), an instance of wandb.Artifact, or wandb.Api().artifact() to use_artifact'
             )
Esempio n. 5
0
 def use_artifact(self, artifact, type=None, aliases=None):
     if self.mode == "dryrun":
         wandb.termwarn("Using artifacts in dryrun mode is currently unsupported.")
         return artifact
     self.history.ensure_jupyter_started()
     if isinstance(artifact, str):
         # Ensure we have an entity (_load_entity puts the result into api.settings())
         # then initialize PublicApi with it.
         self._load_entity(self.api, True)
         public_api = PublicApi(self.api.settings())
         artifact = public_api.artifact(name=artifact)
         if type is not None and type != artifact.type:
             raise ValueError('Supplied type {} does not match type {} of artifact {}'.format(
                 type, artifact.type, artifact.name))
         self.api.use_artifact(artifact.id)
         return artifact
     else:
         if isinstance(aliases, str):
             aliases = [aliases]
         if isinstance(artifact, wandb.Artifact):
             artifact.finalize()
             self.send_message({
                 'use_artifact': {
                     'type': artifact.type,
                     'name': artifact.name,
                     'server_manifest_entries': artifact.server_manifest.entries,
                     'manifest': artifact.manifest.to_manifest_json(include_local=True),
                     'digest': artifact.digest,
                     'metadata': artifact.metadata,
                     'aliases': aliases
                 }
             })
         elif isinstance(artifact, ApiArtifact):
             self.api.use_artifact(artifact.id)
             return artifact
         else:
             raise ValueError('You must pass an artifact name (e.g. "pedestrian-dataset:v1"), an instance of wandb.Artifact, or wandb.Api().artifact() to use_artifact')