Skip to content

Commit

Permalink
chore: add pipeline mock development guide (livepeer#115)
Browse files Browse the repository at this point in the history
This commit adds all the components needed to be able to mock the
pipelines.
  • Loading branch information
rickstaa authored and eliteprox committed Jul 26, 2024
1 parent cf4b6e3 commit a8b18e2
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dev/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Development Guide

This guide provides instructions for setting up the development environment and debugging the [AI worker](https://github.com/livepeer/ai-worker) repository.
This document offers a comprehensive guide for configuring the development environment and debugging the [worker](/worker) component within the [AI worker](https://github.com/livepeer/ai-worker) repository. For an in-depth exploration of developing the [runner](/runner), please consult the [AI runner development guide](/runner/dev/README.md).

## Debugging

Expand Down
21 changes: 21 additions & 0 deletions runner/dev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,24 @@ To debug the AI runner when it operates within a container orchestrated by exter
```bash
docker build -t livepeer/ai-runner:latest .
```

### Mocking the Pipelines

Mocking the pipelines is a practical approach for accelerating development and testing phases. This method simulates the pipeline execution, eliminating the need to run the actual model on a dedicated GPU. Follow the steps below to implement mocking:

1. **Navigate to the Correct Directory**:
Ensure you are within the `runner` directory to apply changes effectively.

2. **Applying the Mock Patch**:
Use the command below to apply the necessary code modifications for mocking the pipelines. This step introduces a mock environment for your development process.

```bash
cd .. && git apply ./runner/dev/patches/mock.patch && cd runner
```

3. **Starting the AI Runner with Mocking**: Launch the AI runner with the environment variable `MOCK_PIPELINE` set to `True`. This enables the mock mode for pipeline execution.
4. **Reverting Mock Changes**: Once testing is complete and you wish to return to the actual pipeline execution, revert the applied mock changes using the following command:

```bash
cd .. && git apply -R ./runner/dev/patches/mock.patch && cd runner
```
105 changes: 105 additions & 0 deletions runner/dev/patches/mock.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
diff --git a/runner/app/pipelines/image_to_image.py b/runner/app/pipelines/image_to_image.py
index fda2dfb..8256b64 100644
--- a/runner/app/pipelines/image_to_image.py
+++ b/runner/app/pipelines/image_to_image.py
@@ -48,6 +48,10 @@ class ImageToImagePipeline(Pipeline):
self.model_id = model_id
kwargs = {"cache_dir": get_model_dir()}

+ if os.getenv("MOCK_PIPELINE", "").strip().lower() == "true":
+ logger.info("Mocking ImageToImagePipeline for %s", model_id)
+ return
+
torch_device = get_torch_device()
folder_name = file_download.repo_folder_name(
repo_id=model_id, repo_type="model"
@@ -171,6 +175,9 @@ class ImageToImagePipeline(Pipeline):
def __call__(
self, prompt: str, image: PIL.Image, **kwargs
) -> Tuple[List[PIL.Image], List[Optional[bool]]]:
+ if os.getenv("MOCK_PIPELINE", "").strip().lower() == "true":
+ return [PIL.Image.new("RGB", (256, 256), (0, 0, 255))], [True]
+
seed = kwargs.pop("seed", None)
safety_check = kwargs.pop("safety_check", True)

diff --git a/runner/app/pipelines/image_to_video.py b/runner/app/pipelines/image_to_video.py
index c3a528e..81dab05 100644
--- a/runner/app/pipelines/image_to_video.py
+++ b/runner/app/pipelines/image_to_video.py
@@ -24,6 +24,10 @@ class ImageToVideoPipeline(Pipeline):
self.model_id = model_id
kwargs = {"cache_dir": get_model_dir()}

+ if os.getenv("MOCK_PIPELINE", "").strip().lower() == "true":
+ logger.info("Mocking ImageToVideoPipeline for %s", model_id)
+ return
+
torch_device = get_torch_device()
folder_name = file_download.repo_folder_name(
repo_id=model_id, repo_type="model"
@@ -109,6 +113,14 @@ class ImageToVideoPipeline(Pipeline):
def __call__(
self, image: PIL.Image, **kwargs
) -> Tuple[List[PIL.Image], List[Optional[bool]]]:
+ if os.getenv("MOCK_PIPELINE", "").strip().lower() == "true":
+ return [
+ [
+ PIL.Image.new("RGB", (256, 256), (0, 0, 255)),
+ PIL.Image.new("RGB", (256, 256), (0, 0, 255)),
+ ]
+ ], [True]
+
seed = kwargs.pop("seed", None)
safety_check = kwargs.pop("safety_check", True)

diff --git a/runner/app/pipelines/text_to_image.py b/runner/app/pipelines/text_to_image.py
index 278c04e..8a0c4b6 100644
--- a/runner/app/pipelines/text_to_image.py
+++ b/runner/app/pipelines/text_to_image.py
@@ -32,6 +32,10 @@ class TextToImagePipeline(Pipeline):
self.model_id = model_id
kwargs = {"cache_dir": get_model_dir()}

+ if os.getenv("MOCK_PIPELINE", "").strip().lower() == "true":
+ logger.info("Mocking TextToImagePipeline for %s", model_id)
+ return
+
torch_device = get_torch_device()
folder_name = file_download.repo_folder_name(
repo_id=model_id, repo_type="model"
@@ -167,6 +171,9 @@ class TextToImagePipeline(Pipeline):
def __call__(
self, prompt: str, **kwargs
) -> Tuple[List[PIL.Image], List[Optional[bool]]]:
+ if os.getenv("MOCK_PIPELINE", "").strip().lower() == "true":
+ return [PIL.Image.new("RGB", (256, 256), (0, 0, 255))], [True]
+
seed = kwargs.pop("seed", None)
safety_check = kwargs.pop("safety_check", True)

diff --git a/runner/app/pipelines/upscale.py b/runner/app/pipelines/upscale.py
index bd2e4f2..830d27c 100644
--- a/runner/app/pipelines/upscale.py
+++ b/runner/app/pipelines/upscale.py
@@ -27,6 +27,10 @@ class UpscalePipeline(Pipeline):
self.model_id = model_id
kwargs = {"cache_dir": get_model_dir()}

+ if os.getenv("MOCK_PIPELINE", "").strip().lower() == "true":
+ logger.info("Mocking UpscalePipeline for %s", model_id)
+ return
+
torch_device = get_torch_device()
folder_name = file_download.repo_folder_name(
repo_id=model_id, repo_type="model"
@@ -97,6 +101,9 @@ class UpscalePipeline(Pipeline):
def __call__(
self, prompt: str, image: PIL.Image, **kwargs
) -> Tuple[List[PIL.Image], List[Optional[bool]]]:
+ if os.getenv("MOCK_PIPELINE", "").strip().lower() == "true":
+ return [PIL.Image.new("RGB", (256, 256), (0, 0, 255))], [True]
+
seed = kwargs.pop("seed", None)
safety_check = kwargs.pop("safety_check", True)

0 comments on commit a8b18e2

Please sign in to comment.