Builds stable Python 3.11 TensorFlow GPU environments on modal.com using Micromamba.
Project description
Modal TensorFlow GPU Environment Builder (Python 3.11)
Author: Osvald Nigola (ozzuke)
Version: 0.2.2
Problem Solved
Setting up a reliable environment for running TensorFlow with GPU acceleration on Modal.io can be tricky due to complex dependencies. This tool automates the creation of a stable Python 3.11 Modal Image and App, specifically tailored for university assignments or projects needing a functional TF+GPU setup quickly.
Features
- Creates stable, reproducible Python 3.11 TF+GPU environments on Modal using Micromamba.
- Based on a known working combination (TF 2.14, CUDA 11.8, Python 3.11).
- Easily add custom
apt,micromamba, andpippackages to the base image. - Includes optional verification tests to confirm GPU functionality and provide timing context.
- Provides both a Command Line Interface (CLI) for testing and a Python function for easy integration into your Modal scripts.
- Designed for straightforward setup and usage via
pip install.
Default Environment
The base image created by this builder includes the following core components (installed via Micromamba):
- Python: 3.11.x
- CUDA Toolkit: 11.8.x (
cudatoolkit=11.8) - TensorFlow (GPU): 2.14.0 (
tensorflow-gpu=2.14.0) - NumPy: 1.26.4 (
numpy=1.26.4)
It also includes the latest compatible versions (at build time) of these common libraries:
cuda-nvcc(CUDA compiler, version tied to cudatoolkit)cudnnkeras(Usually managed by TensorFlow)scipypandaspyarrowmatplotlibseabornscikit-learnPillow(PIL fork for image processing)tqdm(Progress bars)transformers(Hugging Face)datasets(Hugging Face)
System libraries installed via apt-get:
libquadmath0,libgomp1,libgfortran5(Required by NumPy/SciPy)
You can add more packages or override versions using the provided options (see Usage).
Prerequisites (Strict)
- Python 3.11: You MUST have Python 3.11.x installed locally and use it in your environment. This is critical because Modal requires matching Python major.minor versions between your local machine and the remote container to avoid errors when transferring data.
- pip: Comes with Python 3.11.
- Git: For cloning the repository or installing directly.
- Modal Account: A configured Modal account (
modal token set ...).
Installation / Setup
Choose one of the following methods. Both require an active Python 3.11 environment.
Method 1: Direct Install from GitHub (Recommended for Users)
This is the simplest way to use the builder in your projects.
- Create & Activate Python 3.11 Environment:
- Using
venv:# Make sure python3.11 points to your Python 3.11 installation python3.11 -m venv my_ai_project_env source my_ai_project_env/bin/activate # On Windows use my_ai_project_env\Scripts\activate
- Using
conda:conda create -n my_ai_project_env python=3.11 -y conda activate my_ai_project_env
- Using
- Install the Builder (includes dependencies
modalandtblib):pip install git+https://github.com/ozzuke/modal-tf-gpu-builder.git
Method 2: Editable Install (Recommended for Development/Contribution)
Use this if you want to modify the builder's code.
- Clone the Repository:
git clone https://github.com/ozzuke/modal-tf-gpu-builder.git cd modal-tf-gpu-builder
- Create & Activate Python 3.11 Environment (inside the repo dir):
python3.11 -m venv .venv source .venv/bin/activate # Or use conda create -n modal-builder-dev python=3.11 && conda activate modal-builder-dev
- Install in Editable Mode (includes dependencies):
pip install -e .
Usage in Python Scripts
- Activate your Python 3.11 environment where you installed the builder.
- Import and use the
setup_modal_tf_gpufunction in your Modal scripts. - Run the script with Modal using
modal run my_tf_assignment.py.
# Example: my_tf_assignment.py
import modal
import sys
# Import the installed builder package
from modal_tf_builder import setup_modal_tf_gpu
# --- Configuration ---
# Builder uses Python 3.11 by default
GPU_TYPE = "T4" # Or "A10G", etc.
builder_config = {
"app_name": "my-tf-assignment-app",
"gpu_type": GPU_TYPE,
# Add packages needed for YOUR assignment (beyond the defaults)
"add_pip_packages": ["wandb", "tensorflow_datasets"], # Example: Add experiment tracking and TFDS
"add_mamba_packages": {"scikit-image": None} # Example: Add scikit-image
}
# --- Setup Modal App ---
# This uses the builder to configure an app with a Python 3.11 TF+GPU image
print(f"Configuring Modal App: {builder_config['app_name']}")
setup_data = setup_modal_tf_gpu(**builder_config)
app = setup_data["app"] # Get the configured app object
# --- Define Your Modal Function(s) ---
@app.function(gpu=GPU_TYPE, timeout=600)
def run_assignment_task(data_url: str):
import tensorflow as tf
import pandas as pd # Already included by default
import wandb # Added via add_pip
import tensorflow_datasets as tfds # Added via add_pip
from skimage import io # Added via add_mamba
print(f"Running task with TF {tf.__version__} in Python {sys.version}")
print(f"Wandb version: {wandb.__version__}")
# ... download data using data_url ...
# ... load data with pandas ...
# ... process images with skimage ...
# ... load TFDS dataset ...
# ... build/train your TF model ...
# ... log metrics with wandb ...
# ... return results ...
return {"status": "completed"}
# --- Local Entrypoint ---
@app.local_entrypoint()
def main(data_source: str = "http://example.com/my_data.csv"):
print("Starting Modal task...")
# Ensure you are running this local entrypoint using your Python 3.11 env!
result = run_assignment_task.remote(data_source)
print(f"Modal task finished: {result}")
Usage via Command Line (CLI)
The CLI is primarily for testing the build process for a given configuration. It builds the image (if not cached) and optionally runs the verification tests.
Make sure your Python 3.11 environment (where the builder is installed) is active before running these commands.
Basic Test: Build the default image and run verification tests with detailed output.
python -m modal_tf_builder.builder --run-tests --verbose-tests
Test Adding Packages: Build an image with additional packages (requests via pip, opencv via mamba) and run tests.
python -m modal_tf_builder.builder --add-pip requests --add-mamba opencv --run-tests
Test with a Specific GPU: Build for an A10G GPU and run tests.
python -m modal_tf_builder.builder --gpu-type A10G --run-tests
Force Rebuild: Ignore the cache and rebuild the image layers from scratch (useful for debugging build issues).
python -m modal_tf_builder.builder --force-rebuild --run-tests
Build Only (No Tests): Just trigger the image build process without running verification.
python -m modal_tf_builder.builder
Usage in Jupyter / IPython
You can use the builder interactively within a Jupyter Notebook or IPython session.
Important:
- Start Jupyter Lab/Notebook or IPython from your activated Python 3.11 environment.
- Modal functions (
@app.function) must generally be defined in the global scope of a cell. - Crucially, remote function calls (
.remote()) must be wrapped inwith app.run():to execute within an active Modal app context in interactive sessions. Usewith modal.enable_output():as well to see logs.
# Cell 1: Imports and Setup
import modal
import sys
import time
# Import the installed builder package
from modal_tf_builder import setup_modal_tf_gpu
print("Configuring Modal app for interactive use...")
# Configure the builder
interactive_config = {
"app_name": "interactive-tf-session",
"gpu_type": "T4",
"add_pip_packages": ["ipywidgets"], # Add notebook specific things if needed
}
# Call the builder - this defines the image and app object
setup_data = setup_modal_tf_gpu(**interactive_config)
app = setup_data["app"]
print(f"Modal App '{app.name}' configured with Python 3.11 TF+GPU image.")
# Cell 2: Define a Modal Function
# This function will run remotely on Modal using the image defined above
@app.function(gpu=interactive_config["gpu_type"], timeout=300)
def check_tf_version_remote():
import tensorflow as tf
import platform
import time
# Simulate some work
print("Remote function started...")
time.sleep(5)
print("Remote function finishing.")
return {
"tf_version": tf.__version__,
"python_version": platform.python_version(),
"cuda_build_info": tf.sysconfig.get_build_info()
}
# Cell 3: Run the Modal Function Remotely
print("Calling remote function within app.run()...")
# Use .remote() INSIDE the app.run() context manager
# Use modal.enable_output() to see logs from the remote function
result = None # Define outside the block
with modal.enable_output():
with app.run():
result = check_tf_version_remote.remote()
print("Remote function finished.")
# Cell 4: Display Results
if result:
print("\nResults from Modal:")
print(f" TensorFlow Version: {result.get('tf_version', 'N/A')}")
print(f" Python Version (in container): {result.get('python_version', 'N/A')}")
print(f" CUDA Version (TF build): {result.get('cuda_build_info', {}).get('cuda_version', 'N/A')}")
print(f" cuDNN Version (TF build): {result.get('cuda_build_info', {}).get('cudnn_version', 'N/A')}")
else:
print("Failed to get results.")
# Cell 5: Define another function (e.g., simple GPU task)
@app.function(gpu=interactive_config["gpu_type"])
def simple_gpu_task(size=1000):
import tensorflow as tf
import time
print(f"Starting GPU task with size {size}x{size}...")
start = time.time()
with tf.device('/GPU:0'):
a = tf.random.normal((size, size))
b = tf.random.normal((size, size))
c = tf.matmul(a, b).numpy() # Force execution
end = time.time()
print("GPU task complete.")
return {"time_taken": end - start, "output_shape": c.shape}
# Cell 6: Run the GPU task (Corrected)
print("Running simple GPU task within app.run()...")
gpu_result = None # Define outside the block
with modal.enable_output():
with app.run():
gpu_result = simple_gpu_task.remote(size=2000)
if gpu_result:
print(f"GPU task finished in {gpu_result.get('time_taken', -1):.4f}s")
else:
print("Failed to get GPU task results.")
Configuration Options
When calling setup_modal_tf_gpu in Python or using the CLI:
app_name(str): Name for the Modal App (default: "tf-gpu-app").gpu_type(str): GPU type for build and execution (e.g., "T4", "A10G", "H100", default: "T4").add_apt_packages(List[str]): Additional apt packages to install (CLI:--add-apt pkg1 pkg2).add_mamba_packages(Dict[str, Optional[str]]): Additional micromamba packages. Use{"pkg": "version"}or{"pkg": None}for latest compatible (CLI:--add-mamba pkg1=1.2.3 pkg2). These override defaults if the package name matches.add_pip_packages(List[str]): Additional pip packages (CLI:--add-pip pkg1 pkg2).mamba_channels(List[str]): Override default micromamba channels (CLI:--mamba-channels channel1 channel2).run_tests(bool): Run GPU verification tests after setup (CLI:--run-tests). Only applicable when run via CLI.verbose_tests(bool): Show verbose output during tests (CLI:--verbose-tests). Only applicable when run via CLI.force_rebuild(bool): Force Modal to rebuild image layers, ignoring cache (CLI:--force-rebuild).
License
Distributed under the MIT License. See LICENSE for more information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file modal_tf_gpu_builder-0.2.2.tar.gz.
File metadata
- Download URL: modal_tf_gpu_builder-0.2.2.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58a57762dc5c7715e6a3dc869b959d3db1b3523979458ffd1c62db8198a20c70
|
|
| MD5 |
c33002c88ba4f61bcd4cc859ee513c16
|
|
| BLAKE2b-256 |
fb5946a43783e3945700c01089d6a667c9b8a5ce77ca396e38a3f1743f5aeddd
|
File details
Details for the file modal_tf_gpu_builder-0.2.2-py3-none-any.whl.
File metadata
- Download URL: modal_tf_gpu_builder-0.2.2-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
073941ed76da2650b0853d69469b503beef4cd80bc26a43689345f159c949a17
|
|
| MD5 |
dcbdab539e8a782ab370d8666ac28394
|
|
| BLAKE2b-256 |
20dc4b0c7c89736e223305af3fc9e5d76d58a6e7c769b21b7889c4abeb55831b
|