Ashish Ajin commited on
Commit
64ff81d
Β·
1 Parent(s): bcd0453

Compile diffvg with CUDA

Browse files
Files changed (3) hide show
  1. Dockerfile +7 -1
  2. README.md +9 -3
  3. code/main.py +8 -3
Dockerfile CHANGED
@@ -35,8 +35,14 @@ RUN pip install --upgrade pip \
35
  # β€”β€” Build diffvg from a known-good commit β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
36
  RUN git clone https://github.com/BachiLi/diffvg.git /tmp/diffvg \
37
  && cd /tmp/diffvg \
 
38
  && git submodule update --init --recursive \
39
- && USE_CUDA=1 python setup.py install --user \
 
 
 
 
 
40
  && cd /app \
41
  && rm -rf /tmp/diffvg
42
 
 
35
  # β€”β€” Build diffvg from a known-good commit β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
36
  RUN git clone https://github.com/BachiLi/diffvg.git /tmp/diffvg \
37
  && cd /tmp/diffvg \
38
+ && git checkout 7aa22d3f1ef72e22e5cfae3c8c50288f31bc9bcf \
39
  && git submodule update --init --recursive \
40
+ && mkdir build && cd build \
41
+ && cmake .. -DCMAKE_BUILD_TYPE=Release -DUSE_CUDA=ON \
42
+ -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc \
43
+ && make -j$(nproc) \
44
+ && cd ../python && TORCH_CUDA_ARCH_LIST="6.0;7.0;7.5;8.0;8.6;8.9" \
45
+ python setup.py install --user \
46
  && cd /app \
47
  && rm -rf /tmp/diffvg
48
 
README.md CHANGED
@@ -57,9 +57,9 @@ cd Word-As-Image
57
  ```
58
  2. Create a new conda environment and install the libraries:
59
  ```bash
60
- conda create --name word python=3.8.15
61
  conda activate word
62
- pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 --extra-index-url https://download.pytorch.org/whl/cu113
63
  conda install -y numpy scikit-image
64
  conda install -y -c anaconda cmake
65
  conda install -y -c conda-forge ffmpeg
@@ -79,8 +79,14 @@ pip install transformers scipy ftfy accelerate
79
  ```bash
80
  git clone https://github.com/BachiLi/diffvg.git
81
  cd diffvg
 
82
  git submodule update --init --recursive
83
- python setup.py install
 
 
 
 
 
84
  ```
85
 
86
  5. Paste your HuggingFace [access token](https://huggingface.co/settings/tokens) for StableDiffusion in the TOKEN file.
 
57
  ```
58
  2. Create a new conda environment and install the libraries:
59
  ```bash
60
+ conda create --name word python=3.10
61
  conda activate word
62
+ pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu118
63
  conda install -y numpy scikit-image
64
  conda install -y -c anaconda cmake
65
  conda install -y -c conda-forge ffmpeg
 
79
  ```bash
80
  git clone https://github.com/BachiLi/diffvg.git
81
  cd diffvg
82
+ git checkout 7aa22d3f1ef72e22e5cfae3c8c50288f31bc9bcf
83
  git submodule update --init --recursive
84
+ mkdir build && cd build
85
+ cmake .. -DCMAKE_BUILD_TYPE=Release -DUSE_CUDA=ON \
86
+ -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc
87
+ make -j$(nproc)
88
+ cd ../python && TORCH_CUDA_ARCH_LIST="6.0;7.0;7.5;8.0;8.6;8.9" \
89
+ python setup.py install
90
  ```
91
 
92
  5. Paste your HuggingFace [access token](https://huggingface.co/settings/tokens) for StableDiffusion in the TOKEN file.
code/main.py CHANGED
@@ -55,9 +55,14 @@ def generate_word_image(cfg, device: torch.device):
55
  if isinstance(cfg, dict):
56
  cfg = edict(cfg)
57
 
58
- # diffvg was compiled without GPU support in this environment. Always use
59
- # the CPU for rendering to avoid runtime errors.
60
- pydiffvg.set_use_gpu(False)
 
 
 
 
 
61
 
62
  print(f"experiment directory: {cfg.experiment_dir}")
63
 
 
55
  if isinstance(cfg, dict):
56
  cfg = edict(cfg)
57
 
58
+ # Use the GPU for rendering when available. If diffvg was compiled
59
+ # without CUDA support this call will fall back to CPU rendering.
60
+ use_gpu = torch.cuda.is_available()
61
+ try:
62
+ pydiffvg.set_use_gpu(use_gpu)
63
+ except Exception as err: # diffvg not built with CUDA
64
+ print(f"Warning: cannot enable CUDA for diffvg ({err}). Falling back to CPU.")
65
+ pydiffvg.set_use_gpu(False)
66
 
67
  print(f"experiment directory: {cfg.experiment_dir}")
68