Skip to content

Commit

Permalink
Merge pull request #1 from deciduus/main
Browse files Browse the repository at this point in the history
Incorporate changes from deciduus agent-zero fork [~ IDE terminal compatibility, docker quality of life, prompt optimizations, execution handling & style encoding, NVIDIA CUDA (GPU support) ~ frdel#19]
  • Loading branch information
DanozWorld authored Aug 19, 2024
2 parents 3bc8d3f + 061ddfc commit 4587fa0
Show file tree
Hide file tree
Showing 16 changed files with 880 additions and 180 deletions.
278 changes: 278 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,281 @@ pip install -r requirements.txt
python main.py
~~~
- Or run it in debug mode in VS Code using the **debug** button in the top right corner of the editor. I have provided config files for VS Code for this purpose.

# Comprehensive NVIDIA Docker Setup on Ubuntu WSL2

This guide provides detailed instructions for setting up NVIDIA Docker on Ubuntu within WSL2 (Windows Subsystem for Linux 2). It's designed for users of all experience levels, including those new to Ubuntu and Docker.

## Prerequisites

Before you start, ensure you have:

- **Windows 11** with WSL2 enabled
- An **NVIDIA GPU** with the latest drivers installed on Windows
- **Docker Desktop** installed and configured to use WSL2

## Installation Steps

### 1. Install Ubuntu on WSL2

Install Ubuntu from the Microsoft Store if you haven't already.

### 2. Install NVIDIA Docker Toolkit

#### Add NVIDIA Docker Repository Key

```bash
sudo curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
```

#### Set Up the CUDA Repository

Due to issues with the 22.04 repository, use the 18.04 repository:

```bash
echo "deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/amd64 /" | sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
```

#### Update and Install NVIDIA Docker

```bash
sudo apt-get update
sudo apt-get install -y nvidia-docker2
```

#### Restart Docker Daemon

```bash
sudo systemctl restart docker
```

#### Add User to Docker Group

```bash
sudo usermod -aG docker $USER
```

Log out and back in for this change to take effect.

### 3. Configure Docker for NVIDIA Runtime

Edit the Docker daemon configuration:

```bash
sudo nano /etc/docker/daemon.json
```

Add or update the following configuration:

```json
{
"default-runtime": "nvidia",
"runtimes": {
"nvidia": {
"path": "nvidia-container-runtime",
"runtimeArgs": []
}
}
}
```

Save and exit, then restart Docker:

```bash
sudo systemctl restart docker
```

### 4. Verify NVIDIA Docker Installation

Run this command to check if NVIDIA Docker is set up correctly:

```bash
docker run --rm --gpus all nvidia/cuda:11.0.3-base-ubuntu20.04 nvidia-smi
```

You should see information about your NVIDIA GPU.

### 5. Enable Docker to Start on Boot

```bash
sudo systemctl enable docker
```

### 6. Ensure Persistence of NVIDIA Runtime

To make sure the NVIDIA runtime remains the default across restarts:

#### Create a Startup Script

```bash
nano ~/.docker_startup.sh
```

Add the following content:

```bash
#!/bin/bash
sudo ln -sf /usr/libexec/docker/cli-plugins/docker-buildx /usr/local/lib/docker/cli-plugins/
sudo ln -sf /usr/libexec/docker/cli-plugins/docker-compose /usr/local/lib/docker/cli-plugins/
sudo service docker restart
```

#### Make the Script Executable

```bash
chmod +x ~/.docker_startup.sh
```

#### Edit .bashrc

```bash
nano ~/.bashrc
```

Add this line at the end:

```bash
[ -f ~/.docker_startup.sh ] && ~/.docker_startup.sh
```

### 7. Configure WSL2 for Systemd

Edit or create the WSL configuration file:

```bash
sudo nano /etc/wsl.conf
```

Add the following content:

```
[boot]
systemd=true
```

Save and exit the editor.

### 8. Configure Docker Desktop

To prevent Docker Desktop from overriding your WSL2 settings:

1. Open Docker Desktop settings
2. Go to the "Docker Engine" configuration tab
3. Update the configuration to include:

```json
{
"builder": {
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"runtimes": {
"nvidia": {
"path": "/usr/bin/nvidia-container-runtime",
"runtimeArgs": []
}
},
"default-runtime": "nvidia"
}
```

4. Apply changes and restart Docker Desktop

## Verifying Setup

After completing all steps:

1. Restart your WSL2 instance:
In a Windows PowerShell (run as administrator):
```
wsl --shutdown
```
Then reopen your Ubuntu terminal.

2. Check Docker runtime:
```bash
docker info | grep -i runtime
```

3. Verify NVIDIA is still the default runtime and no warnings appear about missing plugins.

## Troubleshooting

If you encounter issues during setup or operation, follow these steps:

### 1. Check Current Docker Runtime Settings

```bash
docker info | grep -i runtime
```

Ensure `nvidia` is listed as a runtime and set as default.

### 2. Reinstall NVIDIA Docker Components

If runtime settings are incorrect, try reinstalling:

```bash
sudo apt-get remove --purge docker-ce docker-ce-cli containerd.io nvidia-docker2
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io nvidia-docker2
```

### 3. Update Docker Daemon Configuration

Ensure `/etc/docker/daemon.json` contains the correct configuration as shown in step 3 of the installation process.

### 4. Restart Docker and Verify

After making changes:

```bash
sudo systemctl restart docker
docker info | grep -i runtime
```

### 5. Test GPU Access

```bash
docker run --rm --gpus all nvidia/cuda:11.0.3-base-ubuntu20.04 nvidia-smi
```

### 6. Resolving Plugin Warnings

If you see warnings about missing Docker plugins:

```bash
ls -l /usr/local/lib/docker/cli-plugins/
ls -l /usr/libexec/docker/cli-plugins/
```

If the symlinks are incorrect or missing, recreate them:

```bash
sudo rm /usr/local/lib/docker/cli-plugins/docker-buildx
sudo rm /usr/local/lib/docker/cli-plugins/docker-compose
sudo ln -s /usr/libexec/docker/cli-plugins/docker-buildx /usr/local/lib/docker/cli-plugins/
sudo ln -s /usr/libexec/docker/cli-plugins/docker-compose /usr/local/lib/docker/cli-plugins/
```

### 7. Docker Desktop and WSL2 Sync Issues

If Docker Desktop isn't syncing properly with Ubuntu WSL2:

1. Verify WSL2 integration is enabled in Docker Desktop settings.
2. Restart Docker Desktop and WSL2 (`wsl --shutdown` in Windows PowerShell).
3. Ensure Docker Desktop is set to use the WSL2 backend.
4. Disable any local Docker distributions, leaving only the Ubuntu WSL2 distribution enabled.
5. Check file sharing permissions for accessed directories.
6. Consider reinstalling Docker Desktop if issues persist.

## Additional Resources

- [NVIDIA Docker Documentation](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/overview.html)
- [Docker Documentation](https://docs.docker.com/)
- [Microsoft WSL2 Documentation](https://docs.microsoft.com/en-us/windows/wsl/)

Remember to always check for the latest updates and best practices in the official documentation.
10 changes: 5 additions & 5 deletions agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class AgentConfig:
msgs_keep_start: int = 5
msgs_keep_end: int = 10
response_timeout_seconds: int = 60
max_tool_response_length: int = 3000
max_tool_response_length: int = 100000
code_exec_docker_enabled: bool = True
code_exec_docker_name: str = "agent-zero-exe"
code_exec_docker_image: str = "frdel/agent-zero-exe:latest"
code_exec_docker_name: str = "docker-agent-zero-exe"
code_exec_docker_image: str = "docker-agent-zero-exe:latest"
code_exec_docker_ports: dict[str,int] = field(default_factory=lambda: {"22/tcp": 50022})
code_exec_docker_volumes: dict[str, dict[str, str]] = field(default_factory=lambda: {files.get_abs_path("work_dir"): {"bind": "/root", "mode": "rw"}})
code_exec_ssh_enabled: bool = True
Expand Down Expand Up @@ -75,9 +75,9 @@ def message_loop(self, msg: str):
memories = self.fetch_memories(True)

while True: # let the agent iterate on his thoughts until he stops by using a tool
Agent.streaming_agent = self #mark self as current streamer
Agent.streaming_agent = self # mark self as current streamer
agent_response = ""
self.intervention_status = False # reset interventon status
self.intervention_status = False # reset intervention status

try:

Expand Down
3 changes: 0 additions & 3 deletions docker/.bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

# Activate the virtual environment
source /opt/venv/bin/activate
35 changes: 24 additions & 11 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,43 +1,56 @@
# Use the latest slim version of Debian
FROM --platform=$TARGETPLATFORM debian:bookworm-slim
# FROM --platform=$TARGETPLATFORM debian:bookworm-slim

# Specify the base image with CUDA support
FROM --platform=$TARGETPLATFORM nvidia/cuda:11.0.3-base-ubuntu20.04

# Set ARG for platform-specific commands
ARG TARGETPLATFORM

# Update and install necessary packages
# Set environment variables
ENV TZ=America/Chicago
ENV DEBIAN_FRONTEND=noninteractive
ENV PATH="/usr/bin:$PATH"

# Update and install necessary packages, including tzdata
RUN apt-get update && apt-get install -y \
tzdata \
python3 \
python3-pip \
python3-venv \
nodejs \
npm \
openssh-server \
sudo \
cmake \
&& rm -rf /var/lib/apt/lists/*

# Set up timezone
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
&& dpkg-reconfigure -f noninteractive tzdata

# Set up SSH
RUN mkdir /var/run/sshd && \
echo 'root:toor' | chpasswd && \
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# Create and activate Python virtual environment
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV

# Copy initial .bashrc with virtual environment activation to a temporary location
COPY .bashrc /etc/skel/.bashrc

# Copy the script to ensure .bashrc is in the root directory
COPY initialize.sh /usr/local/bin/initialize.sh
RUN chmod +x /usr/local/bin/initialize.sh

# Ensure the virtual environment and pip setup
RUN $VIRTUAL_ENV/bin/pip install --upgrade pip
# Ensure pip is upgraded and install Python packages globally
RUN python3 -m pip install --upgrade pip

# Install required Python packages globally with no cache and to the target directory
RUN python3 -m pip install --target=/usr/local/lib/python3.8/dist-packages \
numpy scipy pandas torch torchvision torchaudio \
tensorflow scikit-learn transformers accelerate diffusers \
opencv-python matplotlib seaborn

# Expose SSH port
EXPOSE 22

# Init .bashrc
CMD ["/usr/local/bin/initialize.sh"]


2 changes: 1 addition & 1 deletion docker/build.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
docker buildx build --platform linux/amd64,linux/arm64 -t frdel/agent-zero-exe:latest --push .
docker buildx build --platform linux/amd64,linux/arm64 -t docker-agent-zero-exe:latest --push .
Loading

0 comments on commit 4587fa0

Please sign in to comment.