Beam vs. Cog
Beam and Cog are fundamentally different products. Cog is a wrapper around Docker, whereas Beam provides a custom container runtime. Moreover, the interface for Cog isyaml, whereas the interface for Beam is pure Python.
Beam’s custom container runtime is the magic behind fast cold-start. In addition, Beam provides other utilities for developers building AI apps, like the ability to cache models in storage volumes and run development previews.
You don’t need Docker to use Beam. The only requirement is having the Beam CLI and SDK installed.
Refactoring a Cog to Beam
The structure of a Cog is two-fold. There is apredict.py, which is the entry-point to your model, and a cog.yaml which defines the libraries and dependencies to package with your app.
With Beam, your predict.py and cog.yaml will be combined into the same Python file.
You don’t necessarily need to keep your entire Beam app in a single file; feel
free to split your code into different files and import the modules
accordingly.
Migrate the logic in predict.py
The first thing we’ll do is refactor the predict.py portion of the Cog to use Beam primitives.
This is the original predict.py file:
predict.py to Beam, create a new Python file. You can call it whatever you want, but for this example, we will name it app.py.
app.py
Migrate the image and dependencies in cog.yaml
Now, we’ll move the dependencies in cog.yaml to the app.py file with your Beam app:
Image class.
app.py
Adding System Commands & Python Packages
This is a simple Hello World example, but your real-world Cog app probably has additional dependencies. Suppose yourcog.yaml looks like this:
cog.yaml
python_packages and commands field in Image():
app.py
Test Your Beam Code
To test your Beam code, you can spin up a temporary dev server:Deploy to Beam
When you are ready to deploy your app, run this command:Next Steps
After you’ve refactored your app to run on Beam, there are more optimizations you can take advantage of.Cache Models in Storage Volumes
Speed-up cold start by caching models in volumes.
Pre-Load Models
Cache models in your Beam apps to keep request response times fast.