TABLE OF CONTENTS
Updated: 5 Sep 2024
Developing AI applications brings its own set of challenges - from configuring dependencies and finding the right model to designing robust systems and integrating components. But even after overcoming those hurdles, debugging and troubleshooting GPU-accelerated applications running in the cloud can be intimidating. Here's your quick guide to build AI applications in the cloud.
AI Tips and Tricks for Developers of AI Applications in the Cloud
We've covered you with some simple yet powerful techniques that can forever change your cloud debugging game and streamline your development workflow. In this blog post, we'll explore several tips and tricks with Visual Studio Code (VS Code) - one of the most popular code editors out there. Let’s get started.
1. Inspecting the GPUs in the Configured VM
Before getting into your development efforts, take the time to inspect the GPUs in your configured virtual machine. Use commands like “nvidia-smi” to retrieve information about the installed GPUs, such as their model, memory capacity, and current utilisation. This information will help you optimise your code for the available resources and ensure efficient performance. Don’t forget to monitor GPU usage during training and inference, this will help you identify potential bottlenecks and make informed decisions about scaling your resources.
Related Read: Beginner's Guide to Choosing the Right GPU
2. Connecting VS Code to the VM
When working in the cloud, you may find it more convenient to connect VS Code running on your local machine to the remote VM. This setup allows you to leverage the computational power of the cloud while retaining the familiar development environment on your local machine. You can use tools like SSH or VS Code's remote development extensions to establish the connection. Once connected, you can easily edit, debug and run your code on the remote VM, taking advantage of the cloud's resources while benefiting from the powerful features of VS Code.
3. Installing VS Code Extensions in the VM
While VS Code offers a wide range of extensions out of the box, you may need additional tools and utilities specific to your AI development workflow. Since you will be working in the cloud, it's important to ensure that your development environment is properly configured with the necessary extensions. After connecting VS Code to your remote Virtual machine, you can browse and install extensions directly from the VS Code marketplace. You must look for extensions that support popular AI frameworks like TensorFlow, PyTorch or Hugging Face, as well as tools for data visualisation, profiling, and debugging. Also consider installing extensions that improve productivity, such as code formatters, linters and autocomplete tools.
4. Installing Python Virtual Environment in the VM
When working with Python in the cloud, it's recommended to create a virtual environment for each project. Virtual environments provide isolated Python environments, ensuring that your project dependencies don't conflict with other projects or the system's Python installation. To set up a virtual environment in your VM, you can use tools like “venv”. Once created, activate the virtual environment and install the required Python packages and libraries specific to your project. This allows having an isolated Python environment with the required packages installed, without affecting the system Python or clashing with other projects.
5. Running a Python script from VS Code
After opening your Python file, you can use the built-in Python extension to execute the script with just a few clicks or keyboard shortcuts. To run a script, locate the "Run" option in the top menu bar or the editor's command palette. Alternatively, you can set up a custom build task or launch configuration to streamline the execution process. VS Code also supports debugging capabilities, allowing you to step through your code, set breakpoints, and inspect variables.
6. Leveraging Remote Debugging
Remote debugging allows you to connect your local development environment to a remotely running application so that you can inspect and debug the application as it runs in its production environment. This is particularly useful when you encounter issues that are difficult to reproduce locally or that are specific to the cloud environment. VS Code supports remote debugging through its remote development extensions, which can connect to a variety of targets, including remote machines, containers, and cloud services. With remote debugging, you can set breakpoints, inspect variables, and step through your code while it's running in the cloud, giving you a more accurate representation of the application's behaviour and making it easier to identify and resolve issues.
7. Executing a Script with Breakpoints
Debugging is an important part of the development process, and VS Code's Python extension offers powerful debugging tools to help you identify and fix issues in your code. One crucial feature is the ability to set breakpoints, which pause the execution of your script at specified lines of code. This allows you to inspect variable values, step through your code line by line, and gain a deeper understanding of the program's flow. To set a breakpoint, simply click on the gutter (the left margin of the editor) next to the line of code where you want the execution to pause. You can also use keyboard shortcuts or the debug control panel to manage breakpoints. Once your breakpoints are set, you can execute your script in debug mode and step through the code, making it easier to identify and fix bugs or unintended behaviour.
8. Fixing Bugs Interactively
When your script encounters a breakpoint or an exception, you can use the integrated debugger console to inspect and modify variables, evaluate expressions, and even execute code snippets right from the editor. This feature allows you to test hypotheses, experiment with different solutions, and rapidly iterate on your code without having to restart the entire script. The debugger console supports autocomplete and syntax highlighting, making it a powerful tool for interactive debugging and exploration. With these interactive debugging features, you can quickly identify and resolve issues for more efficient and effective development cycles.
9. Inspecting Execution State within third-party code
Working with AI applications, you often rely on third-party libraries and frameworks, such as Transformers Library by Hugging Face. While these libraries are well-tested and widely used, you may still encounter issues or unexpected behaviour during your development process. In such cases, VS Code's debugging capabilities can be invaluable in helping you understand what's happening within the third-party code.
To inspect the execution state within third-party code, you can't directly set breakpoints in the library's source code files. However, you can use a trick in your code editor or IDE to achieve this. For example, in Visual Studio Code, you can set the "justMyCode" option in the debugging settings to false. This allows you to step through the third-party code during debugging sessions. Most popular Python libraries include source code or allow you to download it, making it possible to inspect the library's code line by line. This feature can be particularly useful when you're trying to understand how a specific function or method works, or when you suspect a bug within the library itself.
10. Wrapping up the Debugging Session
After a productive debugging session, it's important to properly clean up and exit the debugging environment. VS Code provides several options for wrapping up the debugging session, ensuring that your development environment remains consistent and ready for the next task. One way to wrap up the debugging session is to use the "Stop" or "Disconnect" options in the debug control panel or the command palette. This will terminate the current debugging session and return you to the normal editing mode. You can also simply close the debug panel or the terminal window that was used for debugging.
It's a good practice to remove any breakpoints or debug configurations that are no longer needed. This can be done by using the debug control panel to manage breakpoints and configurations. But wait, if you made any temporary changes to your code during the debugging session, such as adding print statements or modifying variable values, be sure to revert those changes or create a new branch or commit before continuing with your development work.
Watch the full video on our channel below to ace AI Applications in the Cloud. The video goes into much more detail on each of these tips, with clear examples to help you follow along. Check it out now!
If you enjoyed this post, you might also like
- How to Use Oobabooga Web UI to Run LLMs on Hyperstack
- How to Run Jupyter Notebooks Like a Pro on Hyperstack
Sign up today to access the Hyperstack Portal and experience the power of AI in the Cloud.
Subscribe to Hyperstack!
Enter your email to get updates to your inbox every week
Get Started
Ready to build the next big thing in AI?