Skip to content

Commit

Permalink
feat: GPU in WSL
Browse files Browse the repository at this point in the history
bug: WSL fails to start sometimes because cp fails
  • Loading branch information
htngr committed Nov 21, 2024
1 parent b6145f0 commit a410d0a
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 193 deletions.
16 changes: 7 additions & 9 deletions codchi/src/platform/windows/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,12 @@ impl Host for HostImpl {

let fragment_path = fragment_dir.join(format!("{machine_name}.json"));

// omit icon for now because if its missing, the fragment doesn't work. Also, after
// each codchi update the icon path changes until the machine is rebuilt
// let codchi_icon = env::current_exe()?
// .parent()
// .context("Failed to access codchi.exe install dir.")?
// .join("Assets")
// .join("favicon150.png");
// let codchi_icon = codchi_icon.display();
// "icon": "{codchi_icon}",
// format! with Debug {codchi_icon:?} because Windows Terminal wants double backspaces
let codchi_icon = env::current_exe()?
.parent()
.context("Failed to access codchi.exe install dir.")?
.join("Assets")
.join("favicon.ico");

fs::write(
fragment_path,
Expand All @@ -214,6 +211,7 @@ impl Host for HostImpl {
"updates": "{{{guid}}}",
"commandline": "codchi.exe exec {machine_name}",
"name": "Codchi - {machine_name}",
"icon": {codchi_icon:?},
}}
]
}}"#
Expand Down
1 change: 1 addition & 0 deletions docs/rspress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ sidebar.push({
"/docs/config/environments/jvm",
"/docs/config/environments/javascript",
"/docs/config/environments/python",
"/docs/config/environments/gpu",
// "/docs/config/examples/c",
// "/docs/config/examples/rust",
// "/docs/config/examples/go",
Expand Down
Empty file.
52 changes: 52 additions & 0 deletions docs/src/docs/config/environments/gpu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# GPU

::: tip
Try it out with `codchi init <NAME> https://github.com/aformatik/codchi nixosModules.gpu`.
:::

GPU usage should work by default inside a code machine. Codchi uses the GPU driver from your host and therefore only works when the driver is installed on your host. Currently only Nvidia/CUDA is tested.

## Nvidia

### Check if it works

```bash
codchi init gpu_test # create an empty machine
codchi exec gpu_test nvidia-smi
# Output should look similar to this:
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.157 Driver Version: 538.18 CUDA Version: 12.2 |
|-----------------------------------------+----------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+======================+======================|
| 0 Quadro P500 On | 00000000:02:00.0 Off | N/A |
| N/A 55C P0 N/A / ERR! | 0MiB / 2048MiB | 1% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+

+---------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=======================================================================================|
| No running processes found |
+---------------------------------------------------------------------------------------+
```

### nvidia-container-toolkit

To use your GPU inside Docker inside a code machine, enable `codchi.docker.enableNvidia`. This also requires requires `nixpkgs.config.allowUnfree = true`.

For example, here is how you can run [InvokeAI](https://github.com/invoke-ai/InvokeAI) inside a code machine:
```bash
codchi init invokeai https://github.com/aformatik/codchi nixosModules.gpu
codchi exec invokeai
# on NixOS>=24.11
docker run --runtime=nvidia --device nvidia.com/gpu=all --publish 9090:9090 ghcr.io/invoke-ai/invokeai
# on NixOS<24.11
docker run --runtime=nvidia --gpus=all --publish 9090:9090 ghcr.io/invoke-ai/invokeai
# open web ui in your windows browser (in another terminal)
xdg-open "http://$(ip route | awk '/^default/{print $3; exit}'):9090"
```
7 changes: 4 additions & 3 deletions docs/src/docs/contrib/roadmap.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# Roadmap

- Docs
- [ ] Demo Video
- [ ] Architecture
- [ ] Landing Page
- Demo Video

- Bugs
- [ ] shortcuts / terminal fragments win
- update path on every update => autostart codchi tray => run "migration"

- Future
- Announcement Post (Discourse, HN?, Product Hunt?)
- [/] GPU
- [ ] GPU

- Features
- [ ] codchi status on windows is slow
- [ ] codchiw.exe
- [ ] `codchi status` on windows is slow
=> move wsl status checking / store container starting to scheduled / time based task
- [ ] `codchi recover` => fs tar export
- [ ] `codchi debug` => machine / store debug shell
Expand Down
2 changes: 2 additions & 0 deletions nix/container/pkgs/make-tarball.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ let
# Copy relative symlinks as is
for f in $rel_links; do
mkdir -p "$(dirname "$f")"
[ -e "$f" ] && rm -f "$f"
cp -af "$pkg/$f" "$f"
echo "$f" >> .files
done
# Copy files and derefenrence absolute symlinks as is
for f in $files; do
mkdir -p "$(dirname "$f")"
[ -e "$f" ] && rm -f "$f"
cp -afL "$pkg/$f" "$f"
echo "$f" >> .files
done
Expand Down
7 changes: 7 additions & 0 deletions nix/examples/gpu/configuration.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ pkgs, ... }: {

nixpkgs.config.allowUnfree = true;
codchi.docker.enable = true;
codchi.docker.enableNvidia = true;
environment.systemPackages = [ pkgs.nvtop ];
}
6 changes: 6 additions & 0 deletions nix/examples/gpu/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = { ... }: {
nixosModules.default = import ./configuration.nix;
};
}
1 change: 1 addition & 0 deletions nix/nixos/driver/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ in
./lxd
./wsl
./secrets.nix
./host-integration.nix
];

options.codchi.driver = {
Expand Down
10 changes: 10 additions & 0 deletions nix/nixos/driver/host-integration.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{ lib, ... }:
let
inherit (lib) mkEnableOption;
in
{
options.codchi = {
gpu.enable = mkEnableOption "OpenGL / GPU driver from the host" // { default = true; };
};

}
Loading

0 comments on commit a410d0a

Please sign in to comment.