flytekitplugins-neptune 1.13.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ Metadata-Version: 2.1
2
+ Name: flytekitplugins-neptune
3
+ Version: 1.13.5
4
+ Summary: This package enables seamless use of Neptune within Flyte
5
+ Author: flyteorg
6
+ Author-email: admin@flyte.org
7
+ License: apache2
8
+ Classifier: Intended Audience :: Science/Research
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: License :: OSI Approved :: Apache Software License
11
+ Classifier: Programming Language :: Python :: 3.8
12
+ Classifier: Programming Language :: Python :: 3.9
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Topic :: Scientific/Engineering
17
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
18
+ Classifier: Topic :: Software Development
19
+ Classifier: Topic :: Software Development :: Libraries
20
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
+ Requires-Python: >=3.8
22
+ Requires-Dist: flytekit>=1.13.3
23
+ Requires-Dist: neptune>=1.10.4
@@ -0,0 +1,36 @@
1
+ # Flytekit Neptune Plugin
2
+
3
+ Neptune is the MLOps stack component for experiment tracking. It offers a single place to log, compare, store, and collaborate on experiments and models. This plugin integrates Flyte with Neptune by configuring links between the two platforms.
4
+
5
+ To install the plugin, run:
6
+
7
+ ```bash
8
+ pip install flytekitplugins-neptune
9
+ ```
10
+
11
+ Neptune requires an API key to authenticate with their platform. This Flyte plugin requires a `flytekit` `Secret` to be configured using [Flyte's Secrets manager](https://docs.flyte.org/en/latest/user_guide/productionizing/secrets.html).
12
+
13
+ ```python
14
+ from flytekit import Secret, current_context
15
+
16
+ neptune_api_token = Secret(key="neptune_api_token", group="neptune_group")
17
+
18
+ @task
19
+ @neptune_init_run(project="flytekit/project", secret=neptune_api_token)
20
+ def neptune_task() -> bool:
21
+ ctx = current_context()
22
+ run = ctx.neptune_run
23
+ run["algorithm"] = "my_algorithm"
24
+ ...
25
+ ```
26
+
27
+ To enable linking from the Flyte side panel to Neptune, add the following to Flyte's configuration:
28
+
29
+ ```yaml
30
+ plugins:
31
+ logs:
32
+ dynamic-log-links:
33
+ - neptune-run-id:
34
+ displayName: Neptune
35
+ templateUris: "{{ .taskConfig.host }}/{{ .taskConfig.project }}?query=(%60flyte%2Fexecution_id%60%3Astring%20%3D%20%22{{ .executionName }}-{{ .nodeId }}-{{ .taskRetryAttempt }}%22)&lbViewUnpacked=true"
36
+ ```
@@ -0,0 +1,3 @@
1
+ from .tracking import neptune_init_run
2
+
3
+ __all__ = ["neptune_init_run"]
@@ -0,0 +1,119 @@
1
+ import os
2
+ from functools import partial
3
+ from typing import Callable, Union
4
+
5
+ import neptune
6
+ from flytekit import Secret
7
+ from flytekit.core.context_manager import FlyteContext, FlyteContextManager
8
+ from flytekit.core.utils import ClassDecorator
9
+
10
+ NEPTUNE_RUN_VALUE = "neptune-run-id"
11
+
12
+
13
+ def neptune_init_run(
14
+ project: str,
15
+ secret: Union[Secret, Callable],
16
+ host: str = "https://app.neptune.ai",
17
+ **init_run_kwargs: dict,
18
+ ):
19
+ """Neptune plugin.
20
+
21
+ Args:
22
+ project (str): Name of the project where the run should go, in the form `workspace-name/project_name`.
23
+ (Required)
24
+ secret (Secret or Callable): Secret with your `NEPTUNE_API_KEY` or a callable that returns the API key.
25
+ The callable takes no arguments and returns a string. (Required)
26
+ host (str): URL to Neptune. Defaults to "https://app.neptune.ai".
27
+ **init_run_kwargs (dict):
28
+ """
29
+ return partial(
30
+ _neptune_init_run_class,
31
+ project=project,
32
+ secret=secret,
33
+ host=host,
34
+ **init_run_kwargs,
35
+ )
36
+
37
+
38
+ class _neptune_init_run_class(ClassDecorator):
39
+ NEPTUNE_HOST_KEY = "host"
40
+ NEPTUNE_PROJECT_KEY = "project"
41
+
42
+ def __init__(
43
+ self,
44
+ task_function: Callable,
45
+ project: str,
46
+ secret: Union[Secret, Callable],
47
+ host: str = "https://app.neptune.ai",
48
+ **init_run_kwargs: dict,
49
+ ):
50
+ """Neptune plugin. See `neptune_init_run` for documentation on the parameters.
51
+
52
+ `neptune_init_run` is the public interface to enforce that `project` and `secret`
53
+ must be passed in.
54
+ """
55
+ self.project = project
56
+ self.secret = secret
57
+ self.host = host
58
+ self.init_run_kwargs = init_run_kwargs
59
+
60
+ super().__init__(task_function, project=project, secret=secret, host=host, **init_run_kwargs)
61
+
62
+ def _is_local_execution(self, ctx: FlyteContext) -> bool:
63
+ return ctx.execution_state.is_local_execution()
64
+
65
+ def _get_secret(self, ctx: FlyteContext) -> str:
66
+ if isinstance(self.secret, Secret):
67
+ secrets = ctx.user_space_params.secrets
68
+ return secrets.get(key=self.secret.key, group=self.secret.group)
69
+ else:
70
+ # Callable
71
+ return self.secret()
72
+
73
+ def execute(self, *args, **kwargs):
74
+ ctx = FlyteContextManager.current_context()
75
+ is_local_execution = self._is_local_execution(ctx)
76
+
77
+ init_run_kwargs = {"project": self.project, **self.init_run_kwargs}
78
+
79
+ if not is_local_execution:
80
+ init_run_kwargs["api_token"] = self._get_secret(ctx)
81
+
82
+ run = neptune.init_run(**init_run_kwargs)
83
+
84
+ if not is_local_execution:
85
+ # The HOSTNAME is set to {.executionName}-{.nodeID}-{.taskRetryAttempt}
86
+ # If HOSTNAME is not defined, use the execution name as a fallback
87
+ hostname = os.environ.get("HOSTNAME", ctx.user_space_params.execution_id.name)
88
+ # Execution specific metadata
89
+ run["flyte/execution_id"] = hostname
90
+ run["flyte/project"] = ctx.user_space_params.execution_id.project
91
+ run["flyte/domain"] = ctx.user_space_params.execution_id.domain
92
+ run["flyte/name"] = ctx.user_space_params.execution_id.name
93
+ run["flyte/raw_output_prefix"] = ctx.user_space_params.raw_output_prefix
94
+ run["flyte/output_metadata_prefix"] = ctx.user_space_params.output_metadata_prefix
95
+ run["flyte/working_directory"] = ctx.user_space_params.working_directory
96
+
97
+ # Task specific metadata
98
+ run["flyte/task/name"] = ctx.user_space_params.task_id.name
99
+ run["flyte/task/project"] = ctx.user_space_params.task_id.project
100
+ run["flyte/task/domain"] = ctx.user_space_params.task_id.domain
101
+ run["flyte/task/version"] = ctx.user_space_params.task_id.version
102
+
103
+ if (execution_url := os.getenv("FLYTE_EXECUTION_URL")) is not None:
104
+ run["flyte/execution_url"] = execution_url
105
+
106
+ new_user_params = ctx.user_space_params.builder().add_attr("NEPTUNE_RUN", run).build()
107
+ with FlyteContextManager.with_context(
108
+ ctx.with_execution_state(ctx.execution_state.with_params(user_space_params=new_user_params))
109
+ ):
110
+ output = self.task_function(*args, **kwargs)
111
+ run.stop()
112
+ return output
113
+
114
+ def get_extra_config(self):
115
+ return {
116
+ self.NEPTUNE_HOST_KEY: self.host,
117
+ self.NEPTUNE_PROJECT_KEY: self.project,
118
+ self.LINK_TYPE_KEY: NEPTUNE_RUN_VALUE,
119
+ }
@@ -0,0 +1,23 @@
1
+ Metadata-Version: 2.1
2
+ Name: flytekitplugins-neptune
3
+ Version: 1.13.5
4
+ Summary: This package enables seamless use of Neptune within Flyte
5
+ Author: flyteorg
6
+ Author-email: admin@flyte.org
7
+ License: apache2
8
+ Classifier: Intended Audience :: Science/Research
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: License :: OSI Approved :: Apache Software License
11
+ Classifier: Programming Language :: Python :: 3.8
12
+ Classifier: Programming Language :: Python :: 3.9
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Topic :: Scientific/Engineering
17
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
18
+ Classifier: Topic :: Software Development
19
+ Classifier: Topic :: Software Development :: Libraries
20
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
+ Requires-Python: >=3.8
22
+ Requires-Dist: flytekit>=1.13.3
23
+ Requires-Dist: neptune>=1.10.4
@@ -0,0 +1,11 @@
1
+ README.md
2
+ setup.py
3
+ flytekitplugins/neptune/__init__.py
4
+ flytekitplugins/neptune/tracking.py
5
+ flytekitplugins_neptune.egg-info/PKG-INFO
6
+ flytekitplugins_neptune.egg-info/SOURCES.txt
7
+ flytekitplugins_neptune.egg-info/dependency_links.txt
8
+ flytekitplugins_neptune.egg-info/namespace_packages.txt
9
+ flytekitplugins_neptune.egg-info/requires.txt
10
+ flytekitplugins_neptune.egg-info/top_level.txt
11
+ tests/test_neptune_init_run.py
@@ -0,0 +1,2 @@
1
+ flytekit>=1.13.3
2
+ neptune>=1.10.4
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,38 @@
1
+ from setuptools import setup
2
+
3
+ PLUGIN_NAME = "neptune"
4
+
5
+
6
+ microlib_name = f"flytekitplugins-{PLUGIN_NAME}"
7
+
8
+ plugin_requires = ["flytekit>=1.13.3", "neptune>=1.10.4"]
9
+
10
+ __version__ = "1.13.5"
11
+
12
+ setup(
13
+ name=microlib_name,
14
+ version=__version__,
15
+ author="flyteorg",
16
+ author_email="admin@flyte.org",
17
+ description="This package enables seamless use of Neptune within Flyte",
18
+ namespace_packages=["flytekitplugins"],
19
+ packages=[f"flytekitplugins.{PLUGIN_NAME}"],
20
+ install_requires=plugin_requires,
21
+ license="apache2",
22
+ python_requires=">=3.8",
23
+ classifiers=[
24
+ "Intended Audience :: Science/Research",
25
+ "Intended Audience :: Developers",
26
+ "License :: OSI Approved :: Apache Software License",
27
+ "Programming Language :: Python :: 3.8",
28
+ "Programming Language :: Python :: 3.9",
29
+ "Programming Language :: Python :: 3.10",
30
+ "Programming Language :: Python :: 3.11",
31
+ "Programming Language :: Python :: 3.12",
32
+ "Topic :: Scientific/Engineering",
33
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
34
+ "Topic :: Software Development",
35
+ "Topic :: Software Development :: Libraries",
36
+ "Topic :: Software Development :: Libraries :: Python Modules",
37
+ ],
38
+ )
@@ -0,0 +1,101 @@
1
+ from unittest.mock import patch, Mock
2
+
3
+ from flytekit import Secret, task, current_context
4
+ from flytekit.core.context_manager import FlyteContextManager
5
+ from flytekitplugins.neptune import neptune_init_run
6
+ from flytekitplugins.neptune.tracking import _neptune_init_run_class
7
+
8
+ neptune_api_token = Secret(key="neptune_api_token", group="neptune_group")
9
+
10
+
11
+ def test_get_extra_config():
12
+
13
+ @neptune_init_run(project="flytekit/project", secret=neptune_api_token, tags=["my-tag"])
14
+ def my_task() -> bool:
15
+ ...
16
+
17
+ config = my_task.get_extra_config()
18
+ assert config[my_task.NEPTUNE_HOST_KEY] == "https://app.neptune.ai"
19
+ assert config[my_task.NEPTUNE_PROJECT_KEY] == "flytekit/project"
20
+
21
+
22
+ @task
23
+ @neptune_init_run(project="flytekit/project", secret=neptune_api_token, tags=["my-tag"])
24
+ def neptune_task() -> bool:
25
+ ctx = current_context()
26
+ return ctx.neptune_run is not None
27
+
28
+
29
+ @patch("flytekitplugins.neptune.tracking.neptune")
30
+ def test_local_project_and_init_run_kwargs(neptune_mock):
31
+ neptune_exists = neptune_task()
32
+ assert neptune_exists
33
+
34
+ neptune_mock.init_run.assert_called_with(
35
+ project="flytekit/project", tags=["my-tag"]
36
+ )
37
+
38
+
39
+ class RunObjectMock(dict):
40
+ def __init__(self):
41
+ self._stop_called = False
42
+
43
+ def stop(self):
44
+ self._stop_called = True
45
+
46
+
47
+ @patch.object(_neptune_init_run_class, "_get_secret")
48
+ @patch.object(_neptune_init_run_class, "_is_local_execution")
49
+ @patch("flytekitplugins.neptune.tracking.neptune")
50
+ def test_remote_project_and_init_run_kwargs(
51
+ neptune_mock,
52
+ mock_is_local_execution,
53
+ mock_get_secret,
54
+ monkeypatch,
55
+ ):
56
+ # Pretend that the execution is remote
57
+ mock_is_local_execution.return_value = False
58
+ api_token = "this-is-my-api-token"
59
+ mock_get_secret.return_value = api_token
60
+
61
+ host_name = "ff59abade1e7f4758baf-mainmytask-0"
62
+ execution_url = "https://my-host.com/execution_url"
63
+ monkeypatch.setenv("HOSTNAME", host_name)
64
+ monkeypatch.setenv("FLYTE_EXECUTION_URL", execution_url)
65
+
66
+ run_mock = RunObjectMock()
67
+ init_run_mock = Mock(return_value=run_mock)
68
+ neptune_mock.init_run = init_run_mock
69
+
70
+ neptune_task()
71
+
72
+ init_run_mock.assert_called_with(project="flytekit/project", tags=["my-tag"], api_token=api_token)
73
+ assert run_mock["flyte/execution_id"] == host_name
74
+ assert run_mock["flyte/execution_url"] == execution_url
75
+
76
+
77
+ def test_get_secret_callable():
78
+ def get_secret():
79
+ return "abc-123"
80
+
81
+ @neptune_init_run(project="flytekit/project", secret=get_secret, tags=["my-tag"])
82
+ def my_task():
83
+ pass
84
+
85
+ ctx_mock = Mock()
86
+ assert my_task._get_secret(ctx_mock) == "abc-123"
87
+
88
+
89
+ def test_get_secret_object():
90
+ secret_obj = Secret(key="my_key", group="my_group")
91
+
92
+ @neptune_init_run(project="flytekit/project", secret=secret_obj, tags=["my-tag"])
93
+ def my_task():
94
+ pass
95
+
96
+ get_secret_mock = Mock(return_value="my-secret-value")
97
+ ctx_mock = Mock()
98
+ ctx_mock.user_space_params.secrets.get = get_secret_mock
99
+
100
+ assert my_task._get_secret(ctx_mock) == "my-secret-value"
101
+ get_secret_mock.assert_called_with(key="my_key", group="my_group")