charmed-ai / tilemapgen

A tool for generating isometric dungeon tiles using stable diffusion
MIT License
66 stars 3 forks source link

Installation problems #1

Closed ahrnbom closed 7 months ago

ahrnbom commented 7 months ago

Hi!

I have spent a few days trying to install tilemapgen, but failed. I think the Readme.md instructions might be out of date, or that the requirements.txt or environment.yml needs to be updated? Or maybe I'm just doing something wrong.

I am using Docker to try to install this, to allow me to try multiple different approaches without breaking my system. I have been using continuumio/miniconda3 as a base, but perhaps that is a bad idea. Do you have any suggestions? Any help is much appreciated. If there's anything I can help with, like updating documentation or configuration files, I will gladly help and make a PR or something (assuming I can solve it, at least).

Here is a summary of a handful of the things I have tried:

Attempt 1

Trying to install from pip. First, I install pytorch3d by

conda install -y pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
conda install -y pytorch3d::pytorch3d -c nvidia -c pytorch -c conda-forge

which, in itself seems to work, my pytorch is now CUDA enabled. It does install torch 2.1.0 though, which is not the same as what tilemapgen wants.

Regardless, I try installing via pip as

python3 -m pip install tilemapgen

This overwrites PyTorch with 2.0.1 (still CUDA enabled). PyTorch3d 0.7.5 is still installed.

Trying to run it gives the following error:

Traceback (most recent call last):
  File "/opt/conda/bin/tilemapgen", line 3, in <module>
    from tilemapgen import commands
  File "/opt/conda/lib/python3.11/site-packages/tilemapgen/commands.py", line 2, in <module>
    from tilemapgen.command_definitions import swatch, render_tile, generate_tile, tilemap
ModuleNotFoundError: No module named 'tilemapgen.command_definitions'

This is somewhat cryptic, but I assume the pip package is incorrectly configured somehow. It doesn't seem to find its command_definitions module.

Attempt 2

Trying to install from source code on GitHub. Running conda env create -f environment.yml does seemingly work, but it installs a version of PyTorch not compiled with CUDA support. Everything seems to work, except it runs on the CPU, so it takes forever to render anything. Presumably, something is wrong in environment.yml that makes conda grab a CPU-only build of PyTorch from a "priority channel".

Attempt 3

Like attempt 1, but I try a more compact installation of pytorch3d. The idea being that if I can install everything in one line, I can make a modification to environment.yml to get it to install.

conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch3d -c pytorch -c nvidia -c conda-forge

This does not work, as PyTorch will be CPU only (not sure why?)

I guess this is somewhat similar to what conda env create -f environment.yml does under the hood, trying to install it all at once. I can cofirm that removing "pytorch3d" from this command will indeed give you the CUDA version of PyTorch.

Attempt 4

Trying to single-line install both pytorch (CUDA) and PyTorch3d. The idea is, if I can do this, I can modify environment.yml to actually work.

conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch3d==0.7.4 -c pytorch -c nvidia -c pytorch3d -c fvcore -c iopath

seems like the right approach but conflicting versions of Python? I have tried this on 3.12 (didn't work) 3.11 (didn't work) 3.10 (didn't work) 3.9 (didn't work) 3.8 (didn't work)

If I add -c conda-forge, it is always able to find a solution, but it also always picks CPU build of PyTorch.

conda install pytorch==2.0.1=*cu* torchvision==0.15.2 torchaudio==2.0.2 pytorch3d==0.7.4 -c pytorch -c nvidia -c pytorch3d -c fvcore -c iopath -c conda-forge

Should enforce CUDA-built PyTorch, but it doesn't work as-is on 3.11 at least. On 3.10 it gets stuck on "solving environment" forever (multiple hours)

conda install pytorch==2.*.*=*cu* pytorch3d -c pytorch -c nvidia -c pytorch3d -c fvcore -c iopath -c conda-forge

does kind of work, but from pytorch3d import _C does crash with a

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: libc10.so: cannot open shared object file: No such file or directory

so it seems unlikely that tilemapgen will work. Nonetheless, let's try. Modified requirements.txt to only be

transformers
diffusers
accelerate
huggingface-hub
iopath
numpy
pillow
safetensors
pyrallis

and then ran pip install -e .. It does seem to download torch 2.0.1, which is worrying.

Result is

opt/conda/bin/tilemapgen:4: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  __import__('pkg_resources').require('tilemapgen==0.0.1')
The cache for model files in Transformers v4.22.0 has been updated. Migrating your old cache. This is a one-time only operation. You can interrupt this and resume the migration later on by calling `transformers.utils.move_cache()`.
0it [00:00, ?it/s]
Traceback (most recent call last):
  File "/opt/conda/bin/tilemapgen", line 7, in <module>
    exec(compile(f.read(), __file__, 'exec'))
  File "/tilemapgen/scripts/tilemapgen", line 3, in <module>
    from tilemapgen import commands
  File "/tilemapgen/src/tilemapgen/commands.py", line 2, in <module>
    from tilemapgen.command_definitions import swatch, render_tile, generate_tile, tilemap
  File "/tilemapgen/src/tilemapgen/command_definitions/render_tile.py", line 10, in <module>
    from pytorch3d.io.mtl_io import make_mesh_texture_atlas
  File "/opt/conda/lib/python3.11/site-packages/pytorch3d/io/__init__.py", line 8, in <module>
    from .obj_io import load_obj, load_objs_as_meshes, save_obj
  File "/opt/conda/lib/python3.11/site-packages/pytorch3d/io/obj_io.py", line 22, in <module>
    from pytorch3d.renderer import TexturesAtlas, TexturesUV
  File "/opt/conda/lib/python3.11/site-packages/pytorch3d/renderer/__init__.py", line 7, in <module>
    from .blending import (
  File "/opt/conda/lib/python3.11/site-packages/pytorch3d/renderer/blending.py", line 10, in <module>
    from pytorch3d import _C
ImportError: /opt/conda/lib/python3.11/site-packages/pytorch3d/_C.cpython-311-x86_64-linux-gnu.so: undefined symbol: _ZN3c106detail14torchCheckFailEPKcS2_jRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

Attempt 5

Try to not actually install tilemapgen, but instead only install requirements, manually through conda and/or pip, and then try to just run tilemapgen.

conda install pytorch==2.0.1=*cuda* pytorch3d==0.7.4=*cuda* transformers diffusers accelerate iopath numpy pillow safetensors -c pytorch -c nvidia -c pytorch3d -c fvcore -c iopath -c conda-forge

missing only huggingface-hub and pyrallis Tried some variants, but conda wasn't able to figure it out

conda install pytorch==2.1.0=*cuda* pytorch3d==0.7.5=*cuda* transformers diffusers accelerate iopath numpy pillow safetensors -c pytorch -c nvidia -c pytorch3d -c fvcore -c iopath -c conda-forge

Installer runs, but cannot even import torch

Traceback (most recent call last):
  File "/mounted/test_torch_gpu.py", line 1, in <module>
    import torch
  File "/opt/conda/lib/python3.11/site-packages/torch/__init__.py", line 235, in <module>
    from torch._C import *  # noqa: F403
    ^^^^^^^^^^^^^^^^^^^^^^
ImportError: libnvJitLink.so.12: cannot open shared object file: No such file or directory
ahrnbom commented 7 months ago

After a lot more experimentation, it seems that tilemapgen can indeed be installed, but only if you install pytorch3d version 0.7.4 via pip, in the way that is described for Google Colab on Pytorch3d's installation instructions, and then modify tilemapgen to not require an outdated version of torch and torchvision. I will explore this a bit further and build something that's easy to use. Do you want me to make a PR with my fixes? Otherwise I might just make a fork

ahrnbom commented 7 months ago

I can at least confirm that the following setup does work. I will do some work to clean this up in a few days, not sure how to distribute it. Since there hasn't been any response here, I think a fork probably makes sense.

Anyway, my current hacky fix is to create the following files in some folder: Dockerfile:

FROM pytorch/pytorch:2.2.1-cuda12.1-cudnn8-runtime

# Install dependencies of pytorch3d and tilemapgen
RUN python3 -m pip install fvcore iopath transformers diffusers accelerate huggingface-hub numpy pillow safetensors pyrallis

# Figure out which build of pytorch3d to install
COPY pytorch3d-version.py /pt3dv.py
RUN VER=$(python3 /pt3dv.py) && pip install --no-index --no-cache-dir pytorch3d==0.7.6 -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/$VER/download.html

# Install apt stuff
RUN apt-get update && apt-get install -y git && apt-get clean

# Fix and then install tilemapgen
COPY fix-tilemapgen.py /fix-tmg.py
RUN git clone https://github.com/charmed-ai/tilemapgen.git /tmp/tmg && python3 /fix-tmg.py && pip install -e /tmp/tmg

fix-tilemapgen.py:

from pathlib import Path
reqs = Path("/tmp/tmg/requirements.txt")
assert reqs.is_file()

Path("/tmp/tmg/environment.yml").unlink()

new_reqs_text = f"""
transformers
diffusers
accelerate
huggingface-hub
iopath
numpy
pillow
safetensors
pyrallis
"""
reqs.write_text(new_reqs_text)

setup = Path("/tmp/tmg/setup.py")
assert setup.is_file()

new_setup_text="""
#!/usr/bin/env python

from distutils.core import setup

setup(name='tilemapgen',
      version='0.0.1',
      description='Generate isometric tilemaps from text prompts',
      author='Jeremy Tryba',
      author_email='jeremy@charmed.ai',
      url='https://github.com/charmed-ai/tilemap-generator/',
      packages=['tilemapgen'],
      package_dir={'': 'src'},
      py_modules=['tilemapgen'],
      install_requires=[
          'torch',
          'torchvision',
          'transformers',
          'diffusers',
          'pyrallis',
          'pytorch3d'
      ],
      scripts=['scripts/tilemapgen'],
     )
"""
setup.write_text(new_setup_text)

pytorch3d-version:

import sys
import torch
pyt_version_str=torch.__version__.split("+")[0].replace(".", "")
version_str="".join([
    f"py3{sys.version_info.minor}_cu",
    torch.version.cuda.replace(".",""),
    f"_pyt{pyt_version_str}"
])
print(version_str)

run.sh:

docker build --tag tmg:latest . && \
docker run --rm --gpus all --name tilemapgen --mount type=bind,source="$(pwd)"/mounted,target=/mounted -it tmg:latest bash
ahrnbom commented 7 months ago

For anyone interested, the fork is available here: https://github.com/ahrnbom/tilemapgen-good