créé moi un script qui permet de savoir si il y a des nombre premier optimisez pour modal cuda en python :
optimise le script pour modal :
Modal logo
Examples
Guide
Reference
Dashboard
Introduction
Custom container images
Custom containers
Private registries
GPUs and other resources
GPU acceleration
Reserving CPU and memory
Scaling out
Scaling out
Dicts and queues
Job processing
Concurrent inputs on a single container (beta)
Secrets and environment variables
Secrets
Environment variables
Deployment
Managing deployments
Invoke deployed functions
Continuous deployment
Scheduling and cron jobs
Web endpoints
Web endpoints
Streaming endpoints
Web endpoint URLs
Request timeouts
Tunnels (beta)
Data sharing and storage
Passing local data
Volumes
Mounting local files and directories
Storing model weights
Cloud bucket mounts (Beta)
Network file systems (superseded)
Dynamic sandboxes
Performance
Cold start performance
Memory Snapshot (beta)
Geographic latency
Reliability and robustness
Failures and retries
Preemption
Timeouts
Troubleshooting
Security and privacy
Integrations
Connecting Modal to your Vercel account
Okta SSO
Other topics
File and project structure
Developing and debugging
Modal user account setup
Workspaces
Environments
Jupyter notebooks
Asynchronous API usage
Global variables
Container lifecycle and parameters
Apps, Stubs, and entrypoints
Introduction to Modal
Modal lets you run code in the cloud without having to think about infrastructure.
Features
Run any code remotely within seconds.
Define container environments in code (or use one of our pre-built backends).
Scale up horizontally to thousands of containers.
Deploy and monitor persistent cron jobs.
Attach GPUs with a single line of code.
Serve your functions as web endpoints.
Use powerful primitives like distributed dictionaries and queues.
Getting started
The nicest thing about all of this is that you don’t have to set up any infrastructure. Just:
Create an account at modal.com
Install the modal Python package
Set up a token
…and you can start running jobs right away.
Modal is currently Python-only, but we may support other languages in the future.
How does it work?
Modal takes your code, puts it in a container, and executes it in the cloud.
Where does it run? Modal runs it in its own cloud environment. The benefit is that we solve all the hard infrastructure problems for you, so you don’t have to do anything. You don’t need to mess with Kubernetes, Docker or even an AWS account.
Modal logo
Examples
Guide
Reference
Dashboard
Introduction
Custom container images
Custom containers
Private registries
GPUs and other resources
GPU acceleration
Reserving CPU and memory
Scaling out
Scaling out
Dicts and queues
Job processing
Concurrent inputs on a single container (beta)
Secrets and environment variables
Secrets
Environment variables
Deployment
Managing deployments
Invoke deployed functions
Continuous deployment
Scheduling and cron jobs
Web endpoints
Web endpoints
Streaming endpoints
Web endpoint URLs
Request timeouts
Tunnels (beta)
Data sharing and storage
Passing local data
Volumes
Mounting local files and directories
Storing model weights
Cloud bucket mounts (Beta)
Network file systems (superseded)
Dynamic sandboxes
Performance
Cold start performance
Memory Snapshot (beta)
Geographic latency
Reliability and robustness
Failures and retries
Preemption
Timeouts
Troubleshooting
Security and privacy
Integrations
Connecting Modal to your Vercel account
Okta SSO
Other topics
File and project structure
Developing and debugging
Modal user account setup
Workspaces
Environments
Jupyter notebooks
Asynchronous API usage
Global variables
Container lifecycle and parameters
Apps, Stubs, and entrypoints
Custom containers
This guide walks you through how to define the environment your Modal functions and applications run in.
Assistant
Pour créer un script Python optimisé pour CUDA afin de déterminer les nombres premiers, nous pouvons utiliser numba, une bibliothèque qui permet d'accélérer les fonctions Python en les compilant juste-à-temps avec LLVM. supporte spécifiquement CUDA pour optimiser les calculs à l'échelle du GPU.
These environments are called containers. Containers are like light-weight virtual machines — container engines use operating system tricks to isolate programs from each other (“containing” them), making them work as though they were running on their own hardware with their own filesystem. This makes execution environments more reproducible, for example by preventing accidental cross-contamination of environments on the same machine. For added security, Modal runs containers using the sandboxed gVisor container runtime.
Containers are started up from a stored “snapshot” of their filesystem state called an image. Producing the image for a container is called building the image.
By default, Modal functions are executed in a Debian Linux container with a basic Python installation of the same minor version v3.x as your local Python interpreter.
Customizing this environment is critical. To make your apps and functions useful, you will probably need some third party system packages or Python libraries. To make them start up faster, you can bake data like model weights into the container image, taking advantage of Modal’s optimized filesystem for serving containers.
Modal provides a number of options to customize your container images at different levels of abstraction and granularity, from high-level convenience methods like pip_install through wrappers of core container image build features like RUN and ENV to full on “bring-your-own-Dockerfile”. We’ll cover each of these in this guide, along with tips and tricks for building images effectively when using each tool.
The typical flow for defining an image in Modal is method chaining starting from a base image, like this:
Copy
In addition to being Pythonic and clean, this also matches the onion-like layerwise build process of container images.
Add Python packages with pip_install
The simplest and most common container modification is to add some third party Python package, like pandas.
You can add Python packages to the environment by passing all the packages you need to the pip_install method of an image.
You can include typical Python dependency version specifiers, like "torch <= 2.0", in the arguments. But we recommend pinning dependencies tightly, like "torch == 1.9.1", to improve the reproducibility and robustness of your builds.
Of course, that means you need to start from some image. Below, we use the recommended debian_slim image as our base.
@app.function(image=datascience_image)
def my_function():
import pandas as pd
import numpy as np
code
df = pd.DataFrame()
...
Copy
Note that because you can define a different environment for each and every Modal function if you so choose, you don’t need to worry about virtual environment management. Containers make for much better separation of concerns!
If you want to run a specific version of Python remotely rather than just matching the one you’re running locally, provide the python_version as a string when constructing the base image, like we did above.
What if I have different Python packages locally and remotely?
You might want to use packages inside your Modal code that you don’t have on your local computer. In the example above, we build a container that uses pandas. But if we don’t have pandas locally, on the computer launching the Modal job, we can’t put import pandas at the top of the script, since it would cause an ImportError.
The easiest solution to this is to put import pandas in the function body instead, as you can see above. This means that pandas is only imported when running inside the remote Modal container, which has pandas installed.
Be careful about what you return from Modal functions that have different packages installed than the ones you have locally! Modal functions return Python objects, like pandas.DataFrames, and if your local machine doesn’t have pandas installed, it won’t be able to handle a pandas object (the error message you see will mention serialization/deserialization).
If you have a lot of functions and a lot of Python packages, you might want to keep the imports in the global scope so that every function can use the same imports. In that case, you can use the imports() context manager:
Copy
Run a Modal function during your build with .run_function (beta)
Instead of using shell commands, you can also run a Python function as an image build step using the Image.run_function method. For example, you can use this to download model parameters from Hugging Face into your image, massively speeding up function starts:
Copy
Any kwargs accepted by @app.function (such as Mounts, NetworkFileSystems, and specifications of resources like GPUs) can be supplied here.
Essentially, this is equivalent to running a Modal function and snapshotting the resulting filesystem as an image.
Whenever you change other features of your image, like the base image or the version of a Python package, the image will automatically be rebuilt the next time it is used. This is a bit more complicated when changing the contents of Modal functions. See the reference documentation for details.
Attach GPUs during setup
If a step in the setup of your container image should be run on an instance with a GPU (e.g., so that a package can be linked against CUDA libraries), pass a desired GPU type when defining that step:
Copy
Use mamba instead of pip with micromamba_install
pip installs Python packages, but some Python workloads require the coordinated installation of system packages as well. The mamba package manager can install both. Modal provides a pre-built Micromamba base image that makes it easy to work with micromamba:
from modal import Image, App
app = App("bayes-pgm") # Note: prior to April 2024, "app" was called "stub"
@app.function(image=numpyro_pymc_image)
def sample():
import pymc as pm
import numpyro as np
code
print(f"Running on PyMC v{pm.__version__} with JAX/numpyro v{np.__version__} backend")
...
Copy
Use an existing container image with .from_registry
You don’t always need to start from scratch! Public registries like Docker Hub have many pre-built container images for common software packages.
You can use any public image in your function using Image.from_registry, so long as:
Python 3.8 or above is present, and is available as python
pip is installed correctly
The image is built for the linux/amd64 platform
The image has a valid ENTRYPOINT
from modal import Image
@app.function(image=sklearn_image)
def fit_knn():
from sklearn.neighbors import KNeighborsClassifier
...
Copy
If an existing image does not have either python or pip set up properly, you can still use it. Just provide a version number as the add_python argument to install a reproducible, standalone build of Python:
Copy
The from_registry method can load images from all public registries, such as Nvidia’s nvcr.io, AWS ECR, and GitHub’s ghcr.io.
We also support access to private AWS ECR and GCP Artifact Registry images.
Bring your own image definition with .from_dockerfile
Sometimes, you might be working in a setting where the environment is already defined as a container image in the form of a Dockerfile.
Modal supports defining a container image directly from a Dockerfile via the Image.from_dockerfile function. It takes a path to an existing Dockerfile.
For instance, we might write a Dockerfile based on the official Python image and adding scikit-learn:
FROM python:3.9
RUN pip install sklearn
Copy
and then define an image for Modal based on it:
Copy
Note that you can still do method chaining to extend this image!
Dockerfile command compatibility
Since Modal doesn’t use Docker to build containers, we have our own implementation of the Dockerfile specification. Most Dockerfiles should work out of the box, but there are some differences to be aware of.
First, a few minor Dockerfile commands and flags have not been implemented yet. Please reach out to us if your use case requires any of these.
Next, there are some command-specific things that may be useful when porting a Dockerfile to Modal.
ENTRYPOINT
While the ENTRYPOINT command is supported, there is an additional constraint to the entrypoint script provided: it must also exec the arguments passed to it at some point. This is so that Modal’s own Python entrypoint can run after your own. Most entrypoint scripts in Docker containers are wrappers over other scripts, so this is likely already the case.
If you wish to write your own entrypoint script, you can use the following as a template:
#!/usr/bin/env bash
Your custom startup commands here.
exec "$@" # Runs the command passed to the entrypoint script.
Copy
If the above file is saved as /usr/bin/my_entrypoint.sh in your container, then you can register it as an entrypoint with ENTRYPOINT ["/usr/bin/my_entrypoint.sh"] in your Dockerfile, or with dockerfile_commands as an Image build step.
Copy
ENV
We currently don’t support Default value in Interpolation, such as ${VAR:-default}
Forcing an image to rebuild
Modal uses the definition of an image to determine whether it needs to be rebuilt. In some cases, you may want to force an image to rebuild, even if the definition hasn’t changed. You can do this by adding the force_build=True argument to any of the image build steps.
Copy
In the above example, both pip_install("slack-sdk") and run_commands("echo hi") will run again, but apt_install("git") will not. Remember to remove force_build=True after you’ve rebuilt the image, otherwise it will rebuild every time you run your code.
Modal logo
Examples
Guide
Reference
Dashboard
Introduction
Custom container images
Custom containers
Private registries
GPUs and other resources
GPU acceleration
Reserving CPU and memory
Scaling out
Scaling out
Dicts and queues
Job processing
Concurrent inputs on a single container (beta)
Secrets and environment variables
Secrets
Environment variables
Deployment
Managing deployments
Invoke deployed functions
Continuous deployment
Scheduling and cron jobs
Web endpoints
Web endpoints
Streaming endpoints
Web endpoint URLs
Request timeouts
Tunnels (beta)
Data sharing and storage
Passing local data
Volumes
Mounting local files and directories
Storing model weights
Cloud bucket mounts (Beta)
Network file systems (superseded)
Dynamic sandboxes
Performance
Cold start performance
Memory Snapshot (beta)
Geographic latency
Reliability and robustness
Failures and retries
Preemption
Timeouts
Troubleshooting
Security and privacy
Integrations
Connecting Modal to your Vercel account
Okta SSO
Other topics
File and project structure
Developing and debugging
Modal user account setup
Workspaces
Environments
Jupyter notebooks
Asynchronous API usage
Global variables
Container lifecycle and parameters
Apps, Stubs, and entrypoints
Custom containers
This guide walks you through how to define the environment your Modal functions and applications run in.
These environments are called containers. Containers are like light-weight virtual machines — container engines use operating system tricks to isolate programs from each other (“containing” them), making them work as though they were running on their own hardware with their own filesystem. This makes execution environments more reproducible, for example by preventing accidental cross-contamination of environments on the same machine. For added security, Modal runs containers using the sandboxed gVisor container runtime.
Containers are started up from a stored “snapshot” of their filesystem state called an image. Producing the image for a container is called building the image.
By default, Modal functions are executed in a Debian Linux container with a basic Python installation of the same minor version v3.x as your local Python interpreter.
Customizing this environment is critical. To make your apps and functions useful, you will probably need some third party system packages or Python libraries. To make them start up faster, you can bake data like model weights into the container image, taking advantage of Modal’s optimized filesystem for serving containers.
Modal provides a number of options to customize your container images at different levels of abstraction and granularity, from high-level convenience methods like pip_install through wrappers of core container image build features like RUN and ENV to full on “bring-your-own-Dockerfile”. We’ll cover each of these in this guide, along with tips and tricks for building images effectively when using each tool.
The typical flow for defining an image in Modal is method chaining starting from a base image, like this:
Copy
In addition to being Pythonic and clean, this also matches the onion-like layerwise build process of container images.
Add Python packages with pip_install
The simplest and most common container modification is to add some third party Python package, like pandas.
You can add Python packages to the environment by passing all the packages you need to the pip_install method of an image.
You can include typical Python dependency version specifiers, like "torch <= 2.0", in the arguments. But we recommend pinning dependencies tightly, like "torch == 1.9.1", to improve the reproducibility and robustness of your builds.
Of course, that means you need to start from some image. Below, we use the recommended debian_slim image as our base.
@app.function(image=datascience_image)
def my_function():
import pandas as pd
import numpy as np
code
df = pd.DataFrame()
...
Copy
Note that because you can define a different environment for each and every Modal function if you so choose, you don’t need to worry about virtual environment management. Containers make for much better separation of concerns!
If you want to run a specific version of Python remotely rather than just matching the one you’re running locally, provide the python_version as a string when constructing the base image, like we did above.
What if I have different Python packages locally and remotely?
You might want to use packages inside your Modal code that you don’t have on your local computer. In the example above, we build a container that uses pandas. But if we don’t have pandas locally, on the computer launching the Modal job, we can’t put import pandas at the top of the script, since it would cause an ImportError.
The easiest solution to this is to put import pandas in the function body instead, as you can see above. This means that pandas is only imported when running inside the remote Modal container, which has pandas installed.
Be careful about what you return from Modal functions that have different packages installed than the ones you have locally! Modal functions return Python objects, like pandas.DataFrames, and if your local machine doesn’t have pandas installed, it won’t be able to handle a pandas object (the error message you see will mention serialization/deserialization).
If you have a lot of functions and a lot of Python packages, you might want to keep the imports in the global scope so that every function can use the same imports. In that case, you can use the imports() context manager:
Copy
The from_registry method can load images from all public registries, such as Nvidia’s nvcr.io, AWS ECR, and GitHub’s ghcr.io.
numba
Voici un exemple de script Python qui utilise numba et CUDA pour vérifier les nombres premiers dans un intervalle donné:
python
import numpy as np
from numba import cuda, vectorize, int64
# Décorateur pour indiquer que cette fonction doit être exécuté sur GPU
@vectorize([int64(int64)], target='cuda')
def is_prime(n):
if n <= 1:
return 0
elif n <= 3:
return 1
elif n % 2 == 0 or n % 3 == 0:
return 0
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return 0
i += 6
return 1
def main():
# Choisissez la taille de l'array et créez une array de nombres consécutifs
N = 100000
data = np.arange(1, N + 1, dtype=np.int64)
# Appeler la fonction is_prime sur GPU
prime_flags = is_prime(data)
# Filtrer et imprimer les nombres premiers
primes = data[prime_flags == 1]
print("Primes up to", N, ":", primes)
if __name__ == "__main__":
main()
Explication du script
Décorateur @vectorize : Cela permet de vectoriser la fonction is_prime pour qu'elle soit exécutée sur chaque élément du tableau data de manière indépendante sur le GPU.
Fonction is_prime : Vérifie si un nombre est premier. Elle est conçue pour traiter chaque nombre indépendamment, ce qui est idéal pour une exécution parallèle sur un GPU.
Main Function:
Il crée un grand tableau de nombres entiers.
Utilise la fonction is_prime pour déterminer quels nombres dans ce tableau sont premiers en les exécutant sur le GPU.
Filtre et affiche les nombres premiers.
Utiliser Modal avec CUDA
Si vous utilisez Modal pour déployer ce script dans un environnement cloud avec accès GPU, assurez-vous de spécifier que votre fonction a besoin de ressources GPU lors de la configuration de votre environnement Modal. Vous pouvez également bénéficier de la scalabilité horizontale que Modal offre pour travailler avec des datasets massifs ou augmenter le nombre de calculs parallèles.