-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable running a single step on the active stack (#2942)
* POC to run single steps on stack * Run sync if possible * Some cleanup * Rename env variable * Invert logic to make it easier * Docs * Fix tests * Fix unit tests * Add note on unlisted pipeline runs * Update docs/book/how-to/build-pipelines/run-an-individual-step.md Co-authored-by: Hamza Tahir <[email protected]> * Apply review comments --------- Co-authored-by: Hamza Tahir <[email protected]>
- Loading branch information
Showing
11 changed files
with
260 additions
and
27 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
docs/book/how-to/build-pipelines/run-an-individual-step.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,71 @@ | ||
# Run an individual step on your stack | ||
|
||
If you want to run just an invividual step on your stack, you can simply call the step | ||
as you would with a normal Python function. ZenML will internally create a pipeline with just your step | ||
and run it on the active stack. | ||
|
||
{% hint style="info" %} | ||
The pipeline run that will happen to execute your step will be `unlisted`, which means it | ||
will not be associated with any pipeline. You can still see it in the "Runs" tab of the | ||
dashboard. | ||
{% endhint %} | ||
|
||
```python | ||
from zenml import step | ||
import pandas as pd | ||
from sklearn.base import ClassifierMixin | ||
from sklearn.svm import SVC | ||
|
||
# Configure the step to use a step operator. If you're not using | ||
# a step operator, you can remove this and the step will run on | ||
# your orchestrator instead. | ||
@step(step_operator="<STEP_OPERATOR_NAME>") | ||
def svc_trainer( | ||
X_train: pd.DataFrame, | ||
y_train: pd.Series, | ||
gamma: float = 0.001, | ||
) -> Tuple[ | ||
Annotated[ClassifierMixin, "trained_model"], | ||
Annotated[float, "training_acc"], | ||
]: | ||
"""Train a sklearn SVC classifier.""" | ||
|
||
model = SVC(gamma=gamma) | ||
model.fit(X_train.to_numpy(), y_train.to_numpy()) | ||
|
||
train_acc = model.score(X_train.to_numpy(), y_train.to_numpy()) | ||
print(f"Train accuracy: {train_acc}") | ||
|
||
return model, train_acc | ||
|
||
|
||
X_train = pd.DataFrame(...) | ||
y_train = pd.Series(...) | ||
|
||
# Call the step directly. This will internally create a | ||
# pipeline with just this step, which will be executed on | ||
# the active stack. | ||
model, train_acc = svc_trainer(X_train=X_train, y_train=y_train) | ||
``` | ||
|
||
## Run the underlying step function directly | ||
|
||
If you instead want to run your step function without ZenML getting involved, you | ||
can use the `entrypoint(...)` method of a step: | ||
|
||
```python | ||
X_train = pd.DataFrame(...) | ||
y_train = pd.Series(...) | ||
|
||
model, train_acc = svc_trainer.entrypoint(X_train=X_train, y_train=y_train) | ||
``` | ||
|
||
{% hint style="info" %} | ||
If you want to make this the default behavior when calling a step, you | ||
can set the `ZENML_RUN_SINGLE_STEPS_WITHOUT_STACK` environment variable to `True`. | ||
Once you do that, calling `svc_trainer(...)` will simply call the underlying function and | ||
not use your ZenML stack. | ||
{% endhint %} | ||
|
||
<!-- For scarf --> | ||
<figure><img alt="ZenML Scarf" referrerpolicy="no-referrer-when-downgrade" src="https://static.scarf.sh/a.png?x-pxid=f0b4f458-0a54-4fcd-aa95-d5ee424815bc" /></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
3 changes: 1 addition & 2 deletions
3
...tebooks/define-steps-in-notebook-cells.md → ...ns-of-defining-steps-in-notebook-cells.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
52 changes: 52 additions & 0 deletions
52
...-remote-steps-and-pipelines-from-notebooks/run-a-single-step-from-a-notebook.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,52 @@ | ||
# Run a single step from a notebook | ||
|
||
If you want to run just a single step remotely from a notebook, you can simply call the step | ||
as you would with a normal Python function. ZenML will internally create a pipeline with just your step | ||
and run it on the active stack. | ||
|
||
{% hint style="warning" %} | ||
When defining a step that should be run remotely in a notebook, make sure you're | ||
aware of all the [limitations](limitations-of-defining-steps-in-notebook-cells.md) that apply. | ||
{% endhint %} | ||
|
||
|
||
```python | ||
from zenml import step | ||
import pandas as pd | ||
from sklearn.base import ClassifierMixin | ||
from sklearn.svm import SVC | ||
|
||
# Configure the step to use a step operator. If you're not using | ||
# a step operator, you can remove this and the step will run on | ||
# your orchestrator instead. | ||
@step(step_operator="<STEP_OPERATOR_NAME>") | ||
def svc_trainer( | ||
X_train: pd.DataFrame, | ||
y_train: pd.Series, | ||
gamma: float = 0.001, | ||
) -> Tuple[ | ||
Annotated[ClassifierMixin, "trained_model"], | ||
Annotated[float, "training_acc"], | ||
]: | ||
"""Train a sklearn SVC classifier.""" | ||
|
||
model = SVC(gamma=gamma) | ||
model.fit(X_train.to_numpy(), y_train.to_numpy()) | ||
|
||
train_acc = model.score(X_train.to_numpy(), y_train.to_numpy()) | ||
print(f"Train accuracy: {train_acc}") | ||
|
||
return model, train_acc | ||
|
||
|
||
X_train = pd.DataFrame(...) | ||
y_train = pd.Series(...) | ||
|
||
# Call the step directly. This will internally create a | ||
# pipeline with just this step, which will be executed on | ||
# the active stack. | ||
model, train_acc = svc_trainer(X_train=X_train, y_train=y_train) | ||
``` | ||
|
||
<!-- For scarf --> | ||
<figure><img alt="ZenML Scarf" referrerpolicy="no-referrer-when-downgrade" src="https://static.scarf.sh/a.png?x-pxid=f0b4f458-0a54-4fcd-aa95-d5ee424815bc" /></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
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.