-
Notifications
You must be signed in to change notification settings - Fork 475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow returning existing artifacts from steps #3347
Open
schustmi
wants to merge
4
commits into
develop
Choose a base branch
from
feature/return-raw-artifact
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...ta-artifact-management/complex-usecases/return-existing-artifacts-from-steps.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
description: Return existing artifacts from steps. | ||
--- | ||
|
||
# Return existing artifacts from steps | ||
|
||
ZenML allows you to return existing artifact versions that are already registered in the ZenML server from your pipeline steps. This is particularly useful when you want to optimize caching behavior in your pipelines. | ||
|
||
## Understanding caching behavior | ||
|
||
ZenML's caching mechanism uses the IDs of the step input artifacts (among other things) to determine whether a step needs to be re-run or can be cached. By default, when a step produces an output artifact, a new artifact version is registered - even if the output data is identical to a previous run. | ||
|
||
This means that steps downstream of a non-cached step will also need to be re-run, since their input artifact IDs will be different, even if the underlying data hasn't changed. To enable better caching, you can return existing artifact versions from your steps instead of always creating new ones. This is useful if you want to do some computation in early parts of the pipeline that decides whether the remaining steps of the pipeline can be cached. | ||
|
||
```python | ||
from zenml import pipeline, step, log_metadata | ||
from zenml.client import Client | ||
from typing import Annotated | ||
|
||
# We want to always run this step to decide whether the | ||
# downstream steps can be cached, so we disable caching for it | ||
@step(enable_cache=False) | ||
def compute_cache() -> Annotated[int, "cache_key"]: | ||
# Replace this with your custom logic, for example compute a key | ||
# from the date of the latest available data point | ||
cache_key = 27 | ||
|
||
artifact_versions = Client().list_artifact_versions( | ||
sort_by="desc:created", | ||
size=1, | ||
name="cache_key", | ||
run_metadata={"cache_key_value": cache_key}, | ||
) | ||
|
||
if artifact_versions: | ||
return artifact_versions[0] | ||
else: | ||
# Log the cache key as metadata on the artifact version so we easily | ||
# fetch it later in subsequent runs | ||
log_metadata(metadata={"cache_key_value": cache_key}, infer_artifact=True) | ||
return cache_key | ||
|
||
|
||
@step | ||
def downstream_step(cache_key: int): | ||
... | ||
|
||
# Enable caching for the pipeline | ||
@pipeline(enable_cache=True) | ||
def my_pipeline(): | ||
cache_key = compute_cache() | ||
downstream_step(cache_key) | ||
``` | ||
|
||
|
||
<figure><img src="https://static.scarf.sh/a.png?x-pxid=f0b4f458-0a54-4fcd-aa95-d5ee424815bc" alt="ZenML Scarf"><figcaption></figcaption></figure> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.