fromconfig-mlflow


Namefromconfig-mlflow JSON
Version 0.4.0 PyPI version JSON
download
home_pagehttps://github.com/criteo/fromconfig-mlflow
Summary# FromConfig MlFlow <!-- {docsify-ignore} -->
upload_time2022-12-22 11:34:20
maintainer
docs_urlNone
authorCriteo
requires_python
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # FromConfig MlFlow <!-- {docsify-ignore} -->
[![pypi](https://img.shields.io/pypi/v/fromconfig-mlflow.svg)](https://pypi.python.org/pypi/fromconfig-mlflow)
[![ci](https://github.com/criteo/fromconfig-mlflow/workflows/Continuous%20integration/badge.svg)](https://github.com/criteo/fromconfig-mlflow/actions?query=workflow%3A%22Continuous+integration%22)

A [fromconfig](https://github.com/criteo/fromconfig) `Launcher` for [MlFlow](https://www.mlflow.org) support.


<!-- MarkdownTOC -->

- [Install](#install)
- [Quickstart](#quickstart)
- [MlFlow server](#mlflow-server)
- [Configure MlFlow](#configure-mlflow)
- [Artifacts and Parameters](#artifacts-and-parameters)
- [Usage-Reference](#usage-reference)
  - [`StartRunLauncher`](#startrunlauncher)
  - [`LogArtifactsLauncher`](#logartifactslauncher)
  - [`LogParamsLauncher`](#logparamslauncher)

<!-- /MarkdownTOC -->


<a id="install"></a>
## Install

```bash
pip install fromconfig_mlflow
```

<a id="quickstart"></a>
## Quickstart

To activate `MlFlow` login, simply add `--launcher.log=mlflow` to your command

```bash
fromconfig config.yaml params.yaml --launcher.log=mlflow - model - train
```

With

`model.py`

```python
"""Dummy Model."""

import mlflow


class Model:
    def __init__(self, learning_rate: float):
        self.learning_rate = learning_rate

    def train(self):
        print(f"Training model with learning_rate {self.learning_rate}")
        if mlflow.active_run():
            mlflow.log_metric("learning_rate", self.learning_rate)
```

`config.yaml`

```yaml
model:
  _attr_: model.Model
  learning_rate: "${params.learning_rate}"
```

`params.yaml`

```yaml
params:
  learning_rate: 0.001
```

It should print

```
Started run: http://127.0.0.1:5000/experiments/0/runs/7fe650dd99574784aec1e4b18fceb73f
Training model with learning_rate 0.001
```

If you navigate to `http://127.0.0.1:5000/experiments/0/runs/7fe650dd99574784aec1e4b18fceb73f` you should see your the logged `learning_rate` metric.

<a id="mlflow-server"></a>
## MlFlow server

To setup a local MlFlow tracking server, run

```bash
mlflow server
```

which should print

```
[INFO] Starting gunicorn 20.0.4
[INFO] Listening at: http://127.0.0.1:5000
```

We will assume that the tracking URI is `http://127.0.0.1:5000` from now on.


<a id="configure-mlflow"></a>
## Configure MlFlow

You can set the tracking URI either via an environment variable or via the config.

To set the `MLFLOW_TRACKING_URI` environment variable

```bash
export MLFLOW_TRACKING_URI=http://127.0.0.1:5000
```

Alternatively, you can set the `mlflow.tracking_uri` config key either via command line with

```bash
fromconfig config.yaml params.yaml --launcher.log=mlflow --mlflow.tracking_uri="http://127.0.0.1:5000" - model - train
```

or in a config file with

`launcher.yaml`

```yaml
# Configure mlflow
mlflow:
  # tracking_uri: "http://127.0.0.1:5000"  # Or set env variable MLFLOW_TRACKING_URI
  # experiment_name: "test-experiment"  # Which experiment to use
  # run_id: 12345  # To restore a previous run
  # run_name: test  # To give a name to your new run
  # artifact_location: "path/to/artifacts"  # Used only when creating a new experiment

# Configure launcher
launcher:
  log: mlflow
```

and run

```bash
fromconfig config.yaml params.yaml launcher.yaml - model - train
```

<a id="artifacts-and-parameters"></a>
## Artifacts and Parameters

In this example, we add logging of the config and parameters.

Re-using the [quickstart](#quickstart) code, modify the `launcher.yaml` file

```yaml
# Configure logging
logging:
  level: 20

# Configure mlflow
mlflow:
  # tracking_uri: "http://127.0.0.1:5000"  # Or set env variable MLFLOW_TRACKING_URI
  # experiment_name: "test-experiment"  # Which experiment to use
  # run_id: 12345  # To restore a previous run
  # run_name: test  # To give a name to your new run
  # artifact_location: "path/to/artifacts"  # Used only when creating a new experiment
  # include_keys:  # Only log params that match *model*
  #   - model

# Configure launcher
launcher:
  log:
    - logging
    - mlflow
  parse:
    - mlflow.log_artifacts
    - parser
    - mlflow.log_params
```

and run

```bash
fromconfig config.yaml params.yaml launcher.yaml - model - train
```

which prints

```
INFO:fromconfig_mlflow.launcher:Started run: http://127.0.0.1:5000/experiments/0/runs/<MLFLOW_RUN_ID>
Training model with learning_rate 0.001
```

If you navigate to the MlFlow run URL, you should see
- the parameters, a flattened version of the *parsed* config (`model.learning_rate` is `0.001` and not `${params.learning_rate}`)
- the original config, saved as `config.yaml`
- the parsed config, saved as `parsed.yaml`


<a id="usage-reference"></a>
## Usage-Reference

<a id="startrunlauncher"></a>
### `StartRunLauncher`

To configure MlFlow, add a `mlflow` entry to your config and set the following parameters

- `run_id`: if you wish to restart an existing run
- `run_name`: if you wish to give a name to your new run
- `tracking_uri`: to configure the tracking remote
- `experiment_name`: to use a different experiment than the custom
  experiment
- `artifact_location`: the location of the artifacts (config files)

Additionally, the launcher can be initialized with the following attributes

- `set_env_vars`: if True (default is `True`), set `MLFLOW_RUN_ID` and `MLFLOW_TRACKING_URI`
- `set_run_id`: if True (default is `False`), set `mlflow.run_id` in config.

For example,

```yaml
# Configure logging
logging:
  level: 20

# Configure mlflow
mlflow:
  # tracking_uri: "http://127.0.0.1:5000"  # Or set env variable MLFLOW_TRACKING_URI
  # experiment_name: "test-experiment"  # Which experiment to use
  # run_id: 12345  # To restore a previous run
  # run_name: test  # To give a name to your new run
  # artifact_location: "path/to/artifacts"  # Used only when creating a new experiment

# Configure Launcher
launcher:
  log:
    - logging
    - _attr_: mlflow
      set_env_vars: true
      set_run_id: true
```


<a id="logartifactslauncher"></a>
### `LogArtifactsLauncher`

The launcher can be initialized with the following attributes

- `path_command`: Name for the command file. If `None`, don't log the command.
- `path_config`: Name for the config file. If `None`, don't log the config.

For example,

```yaml
# Configure logging
logging:
  level: 20

# Configure mlflow
mlflow:
  # tracking_uri: "http://127.0.0.1:5000"  # Or set env variable MLFLOW_TRACKING_URI
  # experiment_name: "test-experiment"  # Which experiment to use
  # run_id: 12345  # To restore a previous run
  # run_name: test  # To give a name to your new run
  # artifact_location: "path/to/artifacts"  # Used only when creating a new experiment

# Configure launcher
launcher:
  log:
    - logging
    - mlflow
  parse:
    - _attr_: mlflow.log_artifacts
      path_command: launch.sh
      path_config: config.yaml
    - parser
    - _attr_: mlflow.log_artifacts
      path_command: null
      path_config: parsed.yaml
```


<a id="logparamslauncher"></a>
### `LogParamsLauncher`

The launcher will use `include_keys` and `ignore_keys`  if present in the config in the `mlflow` key.

- `ignore_keys` : If given, don't log some parameters that have some substrings.
- `include_keys` : If given, only log some parameters that have some substrings. Also shorten the flattened parameter to start at the first match. For example, if the config is `{"foo": {"bar": 1}}` and `include_keys=("bar",)`, then the logged parameter will be `"bar"`.

For example,

```yaml
# Configure logging
logging:
  level: 20

# Configure mlflow
mlflow:
  # tracking_uri: "http://127.0.0.1:5000"  # Or set env variable MLFLOW_TRACKING_URI
  # experiment_name: "test-experiment"  # Which experiment to use
  # run_id: 12345  # To restore a previous run
  # run_name: test  # To give a name to your new run
  # artifact_location: "path/to/artifacts"  # Used only when creating a new experiment
  include_keys:  # Only log params that match *model*
    - model

# Configure launcher
launcher:
  log:
    - logging
    - mlflow
  parse:
    - parser
    - mlflow.log_params
```



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/criteo/fromconfig-mlflow",
    "name": "fromconfig-mlflow",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Criteo",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/74/54/93a1129ec92d05fe06cfe2903ae69fae43c741c160576e98b69c17551fc4/fromconfig_mlflow-0.4.0.tar.gz",
    "platform": null,
    "description": "# FromConfig MlFlow <!-- {docsify-ignore} -->\n[![pypi](https://img.shields.io/pypi/v/fromconfig-mlflow.svg)](https://pypi.python.org/pypi/fromconfig-mlflow)\n[![ci](https://github.com/criteo/fromconfig-mlflow/workflows/Continuous%20integration/badge.svg)](https://github.com/criteo/fromconfig-mlflow/actions?query=workflow%3A%22Continuous+integration%22)\n\nA [fromconfig](https://github.com/criteo/fromconfig) `Launcher` for [MlFlow](https://www.mlflow.org) support.\n\n\n<!-- MarkdownTOC -->\n\n- [Install](#install)\n- [Quickstart](#quickstart)\n- [MlFlow server](#mlflow-server)\n- [Configure MlFlow](#configure-mlflow)\n- [Artifacts and Parameters](#artifacts-and-parameters)\n- [Usage-Reference](#usage-reference)\n  - [`StartRunLauncher`](#startrunlauncher)\n  - [`LogArtifactsLauncher`](#logartifactslauncher)\n  - [`LogParamsLauncher`](#logparamslauncher)\n\n<!-- /MarkdownTOC -->\n\n\n<a id=\"install\"></a>\n## Install\n\n```bash\npip install fromconfig_mlflow\n```\n\n<a id=\"quickstart\"></a>\n## Quickstart\n\nTo activate `MlFlow` login, simply add `--launcher.log=mlflow` to your command\n\n```bash\nfromconfig config.yaml params.yaml --launcher.log=mlflow - model - train\n```\n\nWith\n\n`model.py`\n\n```python\n\"\"\"Dummy Model.\"\"\"\n\nimport mlflow\n\n\nclass Model:\n    def __init__(self, learning_rate: float):\n        self.learning_rate = learning_rate\n\n    def train(self):\n        print(f\"Training model with learning_rate {self.learning_rate}\")\n        if mlflow.active_run():\n            mlflow.log_metric(\"learning_rate\", self.learning_rate)\n```\n\n`config.yaml`\n\n```yaml\nmodel:\n  _attr_: model.Model\n  learning_rate: \"${params.learning_rate}\"\n```\n\n`params.yaml`\n\n```yaml\nparams:\n  learning_rate: 0.001\n```\n\nIt should print\n\n```\nStarted run: http://127.0.0.1:5000/experiments/0/runs/7fe650dd99574784aec1e4b18fceb73f\nTraining model with learning_rate 0.001\n```\n\nIf you navigate to `http://127.0.0.1:5000/experiments/0/runs/7fe650dd99574784aec1e4b18fceb73f` you should see your the logged `learning_rate` metric.\n\n<a id=\"mlflow-server\"></a>\n## MlFlow server\n\nTo setup a local MlFlow tracking server, run\n\n```bash\nmlflow server\n```\n\nwhich should print\n\n```\n[INFO] Starting gunicorn 20.0.4\n[INFO] Listening at: http://127.0.0.1:5000\n```\n\nWe will assume that the tracking URI is `http://127.0.0.1:5000` from now on.\n\n\n<a id=\"configure-mlflow\"></a>\n## Configure MlFlow\n\nYou can set the tracking URI either via an environment variable or via the config.\n\nTo set the `MLFLOW_TRACKING_URI` environment variable\n\n```bash\nexport MLFLOW_TRACKING_URI=http://127.0.0.1:5000\n```\n\nAlternatively, you can set the `mlflow.tracking_uri` config key either via command line with\n\n```bash\nfromconfig config.yaml params.yaml --launcher.log=mlflow --mlflow.tracking_uri=\"http://127.0.0.1:5000\" - model - train\n```\n\nor in a config file with\n\n`launcher.yaml`\n\n```yaml\n# Configure mlflow\nmlflow:\n  # tracking_uri: \"http://127.0.0.1:5000\"  # Or set env variable MLFLOW_TRACKING_URI\n  # experiment_name: \"test-experiment\"  # Which experiment to use\n  # run_id: 12345  # To restore a previous run\n  # run_name: test  # To give a name to your new run\n  # artifact_location: \"path/to/artifacts\"  # Used only when creating a new experiment\n\n# Configure launcher\nlauncher:\n  log: mlflow\n```\n\nand run\n\n```bash\nfromconfig config.yaml params.yaml launcher.yaml - model - train\n```\n\n<a id=\"artifacts-and-parameters\"></a>\n## Artifacts and Parameters\n\nIn this example, we add logging of the config and parameters.\n\nRe-using the [quickstart](#quickstart) code, modify the `launcher.yaml` file\n\n```yaml\n# Configure logging\nlogging:\n  level: 20\n\n# Configure mlflow\nmlflow:\n  # tracking_uri: \"http://127.0.0.1:5000\"  # Or set env variable MLFLOW_TRACKING_URI\n  # experiment_name: \"test-experiment\"  # Which experiment to use\n  # run_id: 12345  # To restore a previous run\n  # run_name: test  # To give a name to your new run\n  # artifact_location: \"path/to/artifacts\"  # Used only when creating a new experiment\n  # include_keys:  # Only log params that match *model*\n  #   - model\n\n# Configure launcher\nlauncher:\n  log:\n    - logging\n    - mlflow\n  parse:\n    - mlflow.log_artifacts\n    - parser\n    - mlflow.log_params\n```\n\nand run\n\n```bash\nfromconfig config.yaml params.yaml launcher.yaml - model - train\n```\n\nwhich prints\n\n```\nINFO:fromconfig_mlflow.launcher:Started run: http://127.0.0.1:5000/experiments/0/runs/<MLFLOW_RUN_ID>\nTraining model with learning_rate 0.001\n```\n\nIf you navigate to the MlFlow run URL, you should see\n- the parameters, a flattened version of the *parsed* config (`model.learning_rate` is `0.001` and not `${params.learning_rate}`)\n- the original config, saved as `config.yaml`\n- the parsed config, saved as `parsed.yaml`\n\n\n<a id=\"usage-reference\"></a>\n## Usage-Reference\n\n<a id=\"startrunlauncher\"></a>\n### `StartRunLauncher`\n\nTo configure MlFlow, add a `mlflow` entry to your config and set the following parameters\n\n- `run_id`: if you wish to restart an existing run\n- `run_name`: if you wish to give a name to your new run\n- `tracking_uri`: to configure the tracking remote\n- `experiment_name`: to use a different experiment than the custom\n  experiment\n- `artifact_location`: the location of the artifacts (config files)\n\nAdditionally, the launcher can be initialized with the following attributes\n\n- `set_env_vars`: if True (default is `True`), set `MLFLOW_RUN_ID` and `MLFLOW_TRACKING_URI`\n- `set_run_id`: if True (default is `False`), set `mlflow.run_id` in config.\n\nFor example,\n\n```yaml\n# Configure logging\nlogging:\n  level: 20\n\n# Configure mlflow\nmlflow:\n  # tracking_uri: \"http://127.0.0.1:5000\"  # Or set env variable MLFLOW_TRACKING_URI\n  # experiment_name: \"test-experiment\"  # Which experiment to use\n  # run_id: 12345  # To restore a previous run\n  # run_name: test  # To give a name to your new run\n  # artifact_location: \"path/to/artifacts\"  # Used only when creating a new experiment\n\n# Configure Launcher\nlauncher:\n  log:\n    - logging\n    - _attr_: mlflow\n      set_env_vars: true\n      set_run_id: true\n```\n\n\n<a id=\"logartifactslauncher\"></a>\n### `LogArtifactsLauncher`\n\nThe launcher can be initialized with the following attributes\n\n- `path_command`: Name for the command file. If `None`, don't log the command.\n- `path_config`: Name for the config file. If `None`, don't log the config.\n\nFor example,\n\n```yaml\n# Configure logging\nlogging:\n  level: 20\n\n# Configure mlflow\nmlflow:\n  # tracking_uri: \"http://127.0.0.1:5000\"  # Or set env variable MLFLOW_TRACKING_URI\n  # experiment_name: \"test-experiment\"  # Which experiment to use\n  # run_id: 12345  # To restore a previous run\n  # run_name: test  # To give a name to your new run\n  # artifact_location: \"path/to/artifacts\"  # Used only when creating a new experiment\n\n# Configure launcher\nlauncher:\n  log:\n    - logging\n    - mlflow\n  parse:\n    - _attr_: mlflow.log_artifacts\n      path_command: launch.sh\n      path_config: config.yaml\n    - parser\n    - _attr_: mlflow.log_artifacts\n      path_command: null\n      path_config: parsed.yaml\n```\n\n\n<a id=\"logparamslauncher\"></a>\n### `LogParamsLauncher`\n\nThe launcher will use `include_keys` and `ignore_keys`  if present in the config in the `mlflow` key.\n\n- `ignore_keys` : If given, don't log some parameters that have some substrings.\n- `include_keys` : If given, only log some parameters that have some substrings. Also shorten the flattened parameter to start at the first match. For example, if the config is `{\"foo\": {\"bar\": 1}}` and `include_keys=(\"bar\",)`, then the logged parameter will be `\"bar\"`.\n\nFor example,\n\n```yaml\n# Configure logging\nlogging:\n  level: 20\n\n# Configure mlflow\nmlflow:\n  # tracking_uri: \"http://127.0.0.1:5000\"  # Or set env variable MLFLOW_TRACKING_URI\n  # experiment_name: \"test-experiment\"  # Which experiment to use\n  # run_id: 12345  # To restore a previous run\n  # run_name: test  # To give a name to your new run\n  # artifact_location: \"path/to/artifacts\"  # Used only when creating a new experiment\n  include_keys:  # Only log params that match *model*\n    - model\n\n# Configure launcher\nlauncher:\n  log:\n    - logging\n    - mlflow\n  parse:\n    - parser\n    - mlflow.log_params\n```\n\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "# FromConfig MlFlow <!-- {docsify-ignore} -->",
    "version": "0.4.0",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "0a65d49455c60e6ce3969dd9f643d30d",
                "sha256": "7d2b883fbec7f7c1c4ffd26ba0cb383eecf4b5d69819672476b40fbe388d6c84"
            },
            "downloads": -1,
            "filename": "fromconfig_mlflow-0.4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "0a65d49455c60e6ce3969dd9f643d30d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 8855,
            "upload_time": "2022-12-22T11:34:20",
            "upload_time_iso_8601": "2022-12-22T11:34:20.796510Z",
            "url": "https://files.pythonhosted.org/packages/74/54/93a1129ec92d05fe06cfe2903ae69fae43c741c160576e98b69c17551fc4/fromconfig_mlflow-0.4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-22 11:34:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "criteo",
    "github_project": "fromconfig-mlflow",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "fromconfig-mlflow"
}
        
Elapsed time: 0.02769s