Skip to content

Commit

Permalink
Merge branch 'master' into nikki/docs/flyte-docs-overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
nikki everett committed Sep 27, 2024
2 parents fecbcc1 + c255605 commit eeaf027
Show file tree
Hide file tree
Showing 19 changed files with 223 additions and 183 deletions.
4 changes: 2 additions & 2 deletions docker/sandbox-bundled/manifests/complete-agent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ type: Opaque
---
apiVersion: v1
data:
haSharedSecret: aVh1N3lZb0F1c2l0NHVuRg==
haSharedSecret: ZXlJVkhWYjdIMHhjamZadA==
proxyPassword: ""
proxyUsername: ""
kind: Secret
Expand Down Expand Up @@ -1413,7 +1413,7 @@ spec:
metadata:
annotations:
checksum/config: 8f50e768255a87f078ba8b9879a0c174c3e045ffb46ac8723d2eedbe293c8d81
checksum/secret: 042e6b21a3852a65952e0701cd9667e53bfef57590eea4d116b261472f29a882
checksum/secret: 94a4c448ea7ad0892283bc4cfc6c506c83c9c5fe998587f4b2c55194c6a674e3
labels:
app: docker-registry
release: flyte-sandbox
Expand Down
4 changes: 2 additions & 2 deletions docker/sandbox-bundled/manifests/complete.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ type: Opaque
---
apiVersion: v1
data:
haSharedSecret: Q2EyanRtd1JjWmVKS2tHMw==
haSharedSecret: OW1PbDdRY0t4RllhM3Nybg==
proxyPassword: ""
proxyUsername: ""
kind: Secret
Expand Down Expand Up @@ -1362,7 +1362,7 @@ spec:
metadata:
annotations:
checksum/config: 8f50e768255a87f078ba8b9879a0c174c3e045ffb46ac8723d2eedbe293c8d81
checksum/secret: 01201329c98a1417f04feeef00dc21e67cf73d99ac9b99486ce5788eca0c282c
checksum/secret: 1f30487909a5b2db21b8f92a734fcb321ab30f01694f4257333026e00d512053
labels:
app: docker-registry
release: flyte-sandbox
Expand Down
4 changes: 2 additions & 2 deletions docker/sandbox-bundled/manifests/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ metadata:
---
apiVersion: v1
data:
haSharedSecret: N3dIemE2TnF1b3l1SWdNTw==
haSharedSecret: MWVqaUwzWDZtUWY4TDdscA==
proxyPassword: ""
proxyUsername: ""
kind: Secret
Expand Down Expand Up @@ -934,7 +934,7 @@ spec:
metadata:
annotations:
checksum/config: 8f50e768255a87f078ba8b9879a0c174c3e045ffb46ac8723d2eedbe293c8d81
checksum/secret: ca7423805c2fd3a98507c790af575a6e5389f50d6baa09bd8c49cb59c4452340
checksum/secret: 53219c6f309435a180b4635448e130a2ec19b63b379a881dde73bf8ae957a1ad
labels:
app: docker-registry
release: flyte-sandbox
Expand Down
185 changes: 150 additions & 35 deletions docs/user_guide/customizing_dependencies/imagespec.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,178 @@
.. tags:: Containerization, Intermediate
```

`ImageSpec` is a way to specify how to build a container image without a Dockerfile. The `ImageSpec` by default will be
converted to an [Envd](https://envd.tensorchord.ai/) config, and the [Envd builder](https://github.com/flyteorg/flytekit/blob/master/plugins/flytekit-envd/flytekitplugins/envd/image_builder.py#L12-L34) will build the image for you. However, you can also register your own builder to build
the image using other tools.

`ImageSpec` allows you to customize the container image for your Flyte tasks without a Dockerfile.
`ImageSpec` speeds up the build process by allowing you to reuse previously downloaded packages from the PyPI and APT caches.

By default, the `ImageSpec` will be built using the `default` builder associated with Flytekit, but you can register your own builder.

For example, [flytekitplugins-envd](https://github.com/flyteorg/flytekit/blob/c06ef30518dec2057e554fbed375dfa43b985c60/plugins/flytekit-envd/flytekitplugins/envd/image_builder.py#L25) is another image builder that uses envd to build the ImageSpec.

For every {py:class}`flytekit.PythonFunctionTask` task or a task decorated with the `@task` decorator,
you can specify rules for binding container images. By default, flytekit binds a single container image, i.e.,
the [default Docker image](https://ghcr.io/flyteorg/flytekit), to all tasks. To modify this behavior,
use the `container_image` parameter available in the {py:func}`flytekit.task` decorator, and pass an
`ImageSpec`.

Before building the image, Flytekit checks the container registry first to see if the image already exists. By doing so, it avoids having to rebuild the image over and over again. If the image does not exist, flytekit will build the image before registering the workflow, and replace the image name in the task template with the newly built image name.

```{note}
To clone and run the example code on this page, see the [Flytesnacks repo][flytesnacks].
```

```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/customizing_dependencies/customizing_dependencies/image_spec.py
:caption: customizing_dependencies/image_spec.py
:lines: 1-4
```
Before building the image, Flytekit checks the container registry to see if the image already exists.
If the image does not exist,
Flytekit will build the image before registering the workflow and replace the image name in the task template with the newly built image name.

:::{admonition} Prerequisites
:class: important

- Install [flytekitplugins-envd](https://github.com/flyteorg/flytekit/tree/master/plugins/flytekit-envd) to build the `ImageSpec`.
- To build the image on remote machine, check this [doc](https://envd.tensorchord.ai/teams/context.html#start-remote-buildkitd-on-builder-machine).
- Make sure `docker` is running on your local machine.
- When using a registry in ImageSpec, `docker login` is required to push the image
:::

You can specify python packages, apt packages, and environment variables in the `ImageSpec`.
## Install Python or APT packages
You can specify Python packages and APT packages in the `ImageSpec`.
These specified packages will be added on top of the [default image](https://github.com/flyteorg/flytekit/blob/master/Dockerfile), which can be found in the Flytekit Dockerfile.
More specifically, flytekit invokes [DefaultImages.default_image()](https://github.com/flyteorg/flytekit/blob/f2cfef0ec098d4ae8f042ab915b0b30d524092c6/flytekit/configuration/default_images.py#L26-L27) function.
This function determines and returns the default image based on the Python version and flytekit version. For example, if you are using python 3.8 and flytekit 0.16.0, the default image assigned will be `ghcr.io/flyteorg/flytekit:py3.8-1.6.0`.
If desired, you can also override the default image by providing a custom `base_image` parameter when using the `ImageSpec`.

```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/customizing_dependencies/customizing_dependencies/image_spec.py
:caption: customizing_dependencies/image_spec.py
:lines: 6-19
```
This function determines and returns the default image based on the Python version and flytekit version.
For example, if you are using Python 3.8 and flytekit 1.6.0, the default image assigned will be `ghcr.io/flyteorg/flytekit:py3.8-1.6.0`.

:::{important}
Replace `ghcr.io/flyteorg` with a container registry you can publish to.
To upload the image to the local registry in the demo cluster, indicate the registry as `localhost:30000`.
:::

`is_container` is used to determine whether the task is utilizing the image constructed from the `ImageSpec`.
If the task is indeed using the image built from the `ImageSpec`, it will then import Tensorflow.
```python
from flytekit import ImageSpec

sklearn_image_spec = ImageSpec(
packages=["scikit-learn", "tensorflow==2.5.0"],
apt_packages=["curl", "wget"],
registry="ghcr.io/flyteorg",
)
```

## Install Conda packages
Define the ImageSpec to install packages from a specific conda channel.
```python
image_spec = ImageSpec(
conda_packages=["langchain"],
conda_channels=["conda-forge"], # List of channels to pull packages from.
registry="ghcr.io/flyteorg",
)
```

## Use different Python versions in the image
You can specify the Python version in the `ImageSpec` to build the image with a different Python version.

```python
image_spec = ImageSpec(
packages=["pandas"],
python_version="3.9",
registry="ghcr.io/flyteorg",
)
```

## Import modules only in a specify imageSpec environment

`is_container()` is used to determine whether the task is utilizing the image constructed from the `ImageSpec`.
If the task is indeed using the image built from the `ImageSpec`, it will return true.
This approach helps minimize module loading time and prevents unnecessary dependency installation within a single image.

```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/customizing_dependencies/customizing_dependencies/image_spec.py
:caption: customizing_dependencies/image_spec.py
:lines: 21-22
In the following example, both `task1` and `task2` will import the `pandas` module. However, `Tensorflow` will only be imported in `task2`.

```python
from flytekit import ImageSpec, task
import pandas as pd

pandas_image_spec = ImageSpec(
packages=["pandas"],
registry="ghcr.io/flyteorg",
)

tensorflow_image_spec = ImageSpec(
packages=["tensorflow", "pandas"],
registry="ghcr.io/flyteorg",
)

# Return if and only if the task is using the image built from tensorflow_image_spec.
if tensorflow_image_spec.is_container():
import tensorflow as tf

@task(container_image=pandas_image_spec)
def task1() -> pd.DataFrame:
return pd.DataFrame({"Name": ["Tom", "Joseph"], "Age": [1, 22]})


@task(container_image=tensorflow_image_spec)
def task2() -> int:
num_gpus = len(tf.config.list_physical_devices('GPU'))
print("Num GPUs Available: ", num_gpus)
return num_gpus
```

## Install CUDA in the image
There are few ways to install CUDA in the image.

### Use Nvidia docker image
CUDA is pre-installed in the Nvidia docker image. You can specify the base image in the `ImageSpec`.
```python
image_spec = ImageSpec(
base_image="nvidia/cuda:12.6.1-cudnn-devel-ubuntu22.04",
packages=["tensorflow", "pandas"],
python_version="3.9",
registry="ghcr.io/flyteorg",
)
```

To enable tasks to utilize the images built with `ImageSpec`, you can specify the `container_image` parameter for those tasks.
### Install packages from extra index
CUDA can be installed by specifying the `pip_extra_index_url` in the `ImageSpec`.
```python
image_spec = ImageSpec(
name="pytorch-mnist",
packages=["torch", "torchvision", "flytekitplugins-kfpytorch"],
pip_extra_index_url=["https://download.pytorch.org/whl/cu118"],
registry="ghcr.io/flyteorg",
)
```

```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/customizing_dependencies/customizing_dependencies/image_spec.py
:caption: customizing_dependencies/image_spec.py
:lines: 27-56
## Build an image in different architecture
You can specify the platform in the `ImageSpec` to build the image in a different architecture, such as `linux/arm64` or `darwin/arm64`.
```python
image_spec = ImageSpec(
packages=["pandas"],
platform="linux/arm64",
registry="ghcr.io/flyteorg",
)
```

There exists an option to override the container image by providing an Image Spec YAML file to the `pyflyte run` or `pyflyte register` command.
## Install flytekit from GitHub
When you update the flytekit, you may want to test the changes with your tasks.
You can install the flytekit from a specific commit hash in the `ImageSpec`.

```python
new_flytekit = "git+https://github.com/flyteorg/flytekit@90a4455c2cc2b3e171dfff69f605f47d48ea1ff1"
new_spark_plugins = f"git+https://github.com/flyteorg/flytekit.git@90a4455c2cc2b3e171dfff69f605f47d48ea1ff1#subdirectory=plugins/flytekit-spark"

image_spec = ImageSpec(
apt_packages=["git"],
packages=[new_flytekit, new_spark_plugins],
registry="ghcr.io/flyteorg",
)
```

## Customize the tag of the image
You can customize the tag of the image by specifying the `tag_format` in the `ImageSpec`.
In the following example, the full qualified image name will be `ghcr.io/flyteorg/my-image:<spec_hash>-dev`.

```python
image_spec = ImageSpec(
name="my-image",
packages=["pandas"],
tag_format="{spec_hash}-dev",
registry="ghcr.io/flyteorg",
)
```

## Define ImageSpec in a YAML File

You can override the container image by providing an ImageSpec YAML file to the `pyflyte run` or `pyflyte register` command.
This allows for greater flexibility in specifying a custom container image. For example:

```yaml
Expand All @@ -85,19 +195,24 @@ env:
pyflyte run --remote --image image.yaml image_spec.py wf
```

## Build the image without registering the workflow

If you only want to build the image without registering the workflow, you can use the `pyflyte build` command.

```
pyflyte build --remote image_spec.py wf
```

In some cases, you may want to force an image to rebuild, even if the image spec hasn’t changed. If you want to overwrite an existing image, you can pass the `FLYTE_FORCE_PUSH_IMAGE_SPEC=True` to `pyflyte` command or add `force_push()` to the ImageSpec.
## Force push an image

In some cases, you may want to force an image to rebuild, even if the ImageSpec hasn’t changed.
To overwrite an existing image, pass the `FLYTE_FORCE_PUSH_IMAGE_SPEC=True` to the `pyflyte` command.

```bash
FLYTE_FORCE_PUSH_IMAGE_SPEC=True pyflyte run --remote image_spec.py wf
```

or
You can also force push an image in the Python code by calling the `force_push()` method.

```python
image = ImageSpec(registry="ghcr.io/flyteorg", packages=["pandas"]).force_push()
Expand Down
24 changes: 12 additions & 12 deletions docs/user_guide/development_lifecycle/decks.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ We create a new deck named `pca` and render Markdown content along with a
You can begin by initializing an {ref}`ImageSpec <image_spec_example>` object to encompass all the necessary dependencies.
This approach automatically triggers a Docker build, alleviating the need for you to manually create a Docker image.

```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/development_lifecycle/development_lifecycle/decks.py
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/b431ae399def3a749833fe81c2c291b016cf3213/examples/development_lifecycle/development_lifecycle/decks.py
:caption: development_lifecycle/decks.py
:lines: 15-19
:lines: 15-27
```

:::{important}
Expand Down Expand Up @@ -96,9 +96,9 @@ When the task connected with a deck object is executed, these objects employ ren

Creates a profile report from a Pandas DataFrame.

```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/development_lifecycle/development_lifecycle/decks.py
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/b431ae399def3a749833fe81c2c291b016cf3213/examples/development_lifecycle/development_lifecycle/decks.py
:caption: development_lifecycle/decks.py
:lines: 44-51
:lines: 56-63
```

:::{figure} https://raw.githubusercontent.com/flyteorg/static-resources/main/flytesnacks/user_guide/flyte_decks_frame_renderer.png
Expand All @@ -113,9 +113,9 @@ Creates a profile report from a Pandas DataFrame.
Renders DataFrame as an HTML table.
This renderer doesn't necessitate plugin installation since it's accessible within the flytekit library.

```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/development_lifecycle/development_lifecycle/decks.py
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/b431ae399def3a749833fe81c2c291b016cf3213/examples/development_lifecycle/development_lifecycle/decks.py
:caption: development_lifecycle/decks.py
:lines: 57-64
:lines: 69-76
```

:::{figure} https://raw.githubusercontent.com/flyteorg/static-resources/main/flytesnacks/user_guide/flyte_decks_top_frame_renderer.png
Expand Down Expand Up @@ -147,9 +147,9 @@ The median (Q2) is indicated by a line within the box.
Typically, the whiskers extend to the edges of the box,
plus or minus 1.5 times the interquartile range (IQR: Q3-Q1).

```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/development_lifecycle/development_lifecycle/decks.py
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/b431ae399def3a749833fe81c2c291b016cf3213/examples/development_lifecycle/development_lifecycle/decks.py
:caption: development_lifecycle/decks.py
:lines: 85-91
:lines: 97-103
```

:::{figure} https://raw.githubusercontent.com/flyteorg/static-resources/main/flytesnacks/user_guide/flyte_decks_box_renderer.png
Expand All @@ -162,9 +162,9 @@ plus or minus 1.5 times the interquartile range (IQR: Q3-Q1).
Converts a {ref}`FlyteFile <files>` or `PIL.Image.Image` object into an HTML string,
where the image data is encoded as a base64 string.

```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/development_lifecycle/development_lifecycle/decks.py
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/b431ae399def3a749833fe81c2c291b016cf3213/examples/development_lifecycle/development_lifecycle/decks.py
:caption: development_lifecycle/decks.py
:lines: 97-111
:lines: 109-123
```

:::{figure} https://raw.githubusercontent.com/flyteorg/static-resources/main/flytesnacks/user_guide/flyte_decks_image_renderer.png
Expand All @@ -176,9 +176,9 @@ where the image data is encoded as a base64 string.

Converts a Pandas dataframe into an HTML table.

```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/development_lifecycle/development_lifecycle/decks.py
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/b431ae399def3a749833fe81c2c291b016cf3213/examples/development_lifecycle/development_lifecycle/decks.py
:caption: development_lifecycle/decks.py
:lines: 115-123
:lines: 127-135
```

:::{figure} https://raw.githubusercontent.com/flyteorg/static-resources/main/flytesnacks/user_guide/flyte_decks_table_renderer.png
Expand Down
1 change: 0 additions & 1 deletion flyteadmin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
github.com/wI2L/jsondiff v0.5.0
github.com/wolfeidau/humanhash v1.1.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.47.0
go.opentelemetry.io/otel v1.24.0
golang.org/x/net v0.27.0
Expand Down
2 changes: 0 additions & 2 deletions flyteadmin/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1301,8 +1301,6 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/wI2L/jsondiff v0.5.0 h1:RRMTi/mH+R2aXcPe1VYyvGINJqQfC3R+KSEakuU1Ikw=
github.com/wI2L/jsondiff v0.5.0/go.mod h1:qqG6hnK0Lsrz2BpIVCxWiK9ItsBCpIZQiv0izJjOZ9s=
github.com/wolfeidau/humanhash v1.1.0 h1:06KgtyyABJGBbrfMONrW7S+b5TTYVyrNB/jss5n7F3E=
github.com/wolfeidau/humanhash v1.1.0/go.mod h1:jkpynR1bfyfkmKEQudIC0osWKynFAoayRjzH9OJdVIg=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=
github.com/xdg/stringprep v1.0.0/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=
Expand Down
Loading

0 comments on commit eeaf027

Please sign in to comment.