-
Notifications
You must be signed in to change notification settings - Fork 3
feat(04_dependencies): add mixed_worker and GPU vs CPU packaging docs #42
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d433dd5
feat(04_dependencies): add mixed_worker example and GPU vs CPU packag…
deanq bd62c17
fix(review): address PR feedback for #42
deanq 967f8bf
fix(05_load_balancer): use typed param for gpu_lb compute endpoint
deanq 67c5194
fix(05_load_balancer): fix gpu_lb endpoint typing and torch import
deanq 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
This file contains hidden or 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 hidden or 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,82 @@ | ||
| # GPU and CPU workers sharing a common dependency (numpy). | ||
| # Demonstrates that dependencies work correctly across both runtime environments: | ||
| # - GPU images (runpod/pytorch:*) have numpy pre-installed | ||
| # - CPU images (python-slim) install numpy from the build artifact | ||
| # | ||
| # run with: flash run | ||
| # test directly: python mixed_worker.py | ||
| from runpod_flash import CpuInstanceType, Endpoint, GpuType | ||
|
|
||
|
|
||
| @Endpoint( | ||
| name="01_04_deps_gpu_numpy", | ||
| gpu=GpuType.NVIDIA_GEFORCE_RTX_4090, | ||
| workers=(0, 3), | ||
| dependencies=["numpy"], | ||
| ) | ||
| async def gpu_matrix_multiply(input_data: dict) -> dict: | ||
| """GPU worker using numpy for matrix operations. | ||
|
|
||
| On GPU images, numpy is pre-installed in the base image. The build | ||
| artifact also includes it, so both paths work. The GPU image's | ||
| pre-installed copy takes precedence via Python's import resolution. | ||
|
deanq marked this conversation as resolved.
Outdated
|
||
| """ | ||
| import numpy as np | ||
|
|
||
| size = input_data.get("size", 100) | ||
| a = np.random.rand(size, size) | ||
| b = np.random.rand(size, size) | ||
| result = np.dot(a, b) | ||
|
deanq marked this conversation as resolved.
Outdated
|
||
|
|
||
| return { | ||
| "status": "success", | ||
| "worker_type": "GPU", | ||
| "matrix_size": size, | ||
| "result_shape": list(result.shape), | ||
| "result_trace": float(np.trace(result)), | ||
| "numpy_version": np.__version__, | ||
| } | ||
|
|
||
|
|
||
| @Endpoint( | ||
| name="01_04_deps_cpu_numpy", | ||
| cpu=CpuInstanceType.CPU3C_1_2, | ||
| workers=(0, 3), | ||
| dependencies=["numpy"], | ||
| ) | ||
| async def cpu_statistics(input_data: dict) -> dict: | ||
| """CPU worker using numpy for statistical computations. | ||
|
|
||
| On CPU images (python-slim), numpy is NOT pre-installed. The build | ||
| artifact must include it. Flash's build pipeline ships numpy in the | ||
| tarball for CPU endpoints. | ||
| """ | ||
| import numpy as np | ||
|
|
||
| values = input_data.get("values", [1.0, 2.0, 3.0, 4.0, 5.0]) | ||
| arr = np.array(values) | ||
|
|
||
| return { | ||
| "status": "success", | ||
| "worker_type": "CPU", | ||
| "count": len(values), | ||
| "mean": float(np.mean(arr)), | ||
| "std": float(np.std(arr)), | ||
| "median": float(np.median(arr)), | ||
| "numpy_version": np.__version__, | ||
| } | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import asyncio | ||
|
|
||
| async def test(): | ||
| print("\n=== Testing GPU numpy (matrix multiply) ===") | ||
| gpu_result = await gpu_matrix_multiply({"size": 50}) | ||
| print(f"Result: {gpu_result}\n") | ||
|
|
||
| print("=== Testing CPU numpy (statistics) ===") | ||
| cpu_result = await cpu_statistics({"values": [10, 20, 30, 40, 50]}) | ||
| print(f"Result: {cpu_result}\n") | ||
|
|
||
| asyncio.run(test()) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.