Spaces:
Runtime error
Runtime error
[Admin maintenance] Support new ZeroGPU hardware
#20
by multimodalart HF Staff - opened
Files changed (9)
- README.md+1-1
- app.py+202-16
- gradio_app/all_models.py+1-1
- gradio_app/gradio_3dgen.py+2-2
- packages.txtAdded+4
- requirements.txt+4-8
- scripts/load_onnx.py+3-14
- scripts/sd_model_zoo.py+1-1
- scripts/utils.py+3-8
4 unmodified lines5678910114 unmodified linescolorTo: purplesdk: gradiopython_version: 3.10.8sdk_version: 4.12.0app_file: app.pypinned: trueshort_description: Create a 1M faces 3D colored model from an image!4 unmodified lines5678910114 unmodified linescolorTo: purplesdk: gradiopython_version: 3.10.8sdk_version: 5.49.1app_file: app.pypinned: trueshort_description: Create a 1M faces 3D colored model from an image!
12345678910111213141516171819202122232426 unmodified lines515253545556575 unmodified lines636465666768import shleximport subprocesssubprocess.run(shlex.split("pip install package/onnxruntime_gpu-1.17.0-cp310-cp310-manylinux_2_28_x86_64.whl --force-reinstall --no-deps"))subprocess.run(shlex.split("pip install package/nvdiffrast-0.3.1.torch-cp310-cp310-linux_x86_64.whl --force-reinstall --no-deps"))if __name__ == "__main__":import osfrom huggingface_hub import loginhf_token = os.environ.get("HF_TOKEN")login(token=hf_token)import osimport syssys.path.append(os.curdir)import torchtorch.set_float32_matmul_precision('medium')26 unmodified linesdef launch():model_zoo.init_models()with gr.Blocks(title=_TITLE,# theme=gr.themes.Monochrome(),5 unmodified linescreate_3d_ui("wkl")demo.queue().launch(share=True)if __name__ == '__main__':fire.Fire(launch)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921026 unmodified lines2372382392402412422435 unmodified lines249250251252253254import osimport sysimport subprocessimport tempfileimport spaces# Build pytorch3d and nvdiffrast against the installed torch the first time a GPU is allocated.# Both packages publish no prebuilt wheels for torch >= 2.8 (Blackwell / CUDA 13 stack).CUDA_HOME = "/cuda-image/usr/local/cuda-13.0"def _build_env():env = os.environ.copy()env["CUDA_HOME"] = CUDA_HOMEenv["CUDA_PATH"] = CUDA_HOMEenv["PATH"] = os.path.join(CUDA_HOME, "bin") + os.pathsep + env.get("PATH", "")env["TORCH_CUDA_ARCH_LIST"] = "12.0"return envdef _ensure_pkg(name, install_args):try:__import__(name)return Trueexcept ImportError:passsubprocess.check_call([sys.executable, "-m", "pip", "install","--no-build-isolation","--no-deps",*install_args],env=_build_env(),)def _strip_pulsar_from_pytorch3d_src(src):"""Drop pulsar from a cloned pytorch3d source tree.Pulsar fails to link on modern GCC because the explicit templateinstantiations of pulsar::Renderer::fill_bg<true>/forward<true>/...end up as hidden symbols and ld errors with:hidden symbol `..._fill_bg<true>...' isn't definedfinal link failed: bad valueUnique3D never imports pulsar (only pytorch3d.renderer.{cameras,mesh,...}and pytorch3d.structures). Strip pulsar entirely before building."""import shutil# 1. Remove the C++/CUDA sources so setup.py's glob skips them.pulsar_csrc = os.path.join(src, "pytorch3d", "csrc", "pulsar")if os.path.isdir(pulsar_csrc):shutil.rmtree(pulsar_csrc)# 2. Remove the python-side pulsar package.pulsar_py = os.path.join(src, "pytorch3d", "renderer", "points", "pulsar")if os.path.isdir(pulsar_py):shutil.rmtree(pulsar_py)# 3. Rewrite ext.cpp to drop every pulsar-related include and the entire# pulsar pybind11 registration block. Use the well-defined "Pulsar." comment# marker that starts the registration block and run to the matching #endif.ext_cpp = os.path.join(src, "pytorch3d", "csrc", "ext.cpp")if os.path.isfile(ext_cpp):with open(ext_cpp, "r") as f:lines = f.readlines()# Phase A: drop the three pulsar include lines (they sit inside# `#if !defined(USE_ROCM)` blocks together with non-pulsar includes? No —# check the file: the top of ext.cpp has two #if !defined(USE_ROCM)# blocks that only contain pulsar includes. Drop the whole #if/#endif# pairs that contain only pulsar includes.out = []i = 0while i < len(lines):ln = lines[i]if ln.strip().startswith("#if !defined(USE_ROCM)"):# Look ahead for the matching #endif. Collect block body.j = i + 1block = []while j < len(lines) and not lines[j].strip().startswith("#endif"):block.append(lines[j])j += 1# Decide: if every non-blank/non-comment line in block mentions# "pulsar", drop the whole #if..#endif. Otherwise keep as-is.meaningful = [b for b in blockif b.strip() and not b.strip().startswith("//")]if meaningful and all("pulsar" in b.lower() for b in meaningful):# Drop block (and matching #endif).i = j + 1continueout.append(ln)i += 1# Phase B: drop the pulsar pybind registration block.# Find the "// Pulsar." comment, walk back to its preceding blank line,# then forward to the matching #endif (the last #endif before `}`).text = "".join(out)marker = " // Pulsar."idx = text.find(marker)if idx >= 0:# Find the start of the line containing the marker.line_start = text.rfind("\n", 0, idx) + 1# Find the trailing `#endif` of the pulsar block. Walk forward; the# block ends with a `#endif` on a line by itself, right before `}`.end_marker = "\n#endif\n}"end_idx = text.find(end_marker, line_start)if end_idx < 0:# Fall back: just drop to the next `}` on its own line.end_idx = text.find("\n}\n", line_start)if end_idx < 0:raise RuntimeError("could not locate pulsar block end in ext.cpp")# Keep the `}`.text = text[:line_start] + text[end_idx + 1:]else:# Drop the `#endif` line but keep the `}`.text = text[:line_start] + text[end_idx + len("\n#endif"):]with open(ext_cpp, "w") as f:f.write(text)# 4. Patch renderer/points/__init__.py AND renderer/__init__.py: both have a# `if not torch.version.hip:` block that imports PulsarPointsRenderer; replace# the import with `pass` so the symbol is just absent.for init_path in [os.path.join(src, "pytorch3d", "renderer", "points", "__init__.py"),os.path.join(src, "pytorch3d", "renderer", "__init__.py"),]:if not os.path.isfile(init_path):continuewith open(init_path, "r") as f:init_lines = f.readlines()new_lines = []skip_block = Falsefor ln in init_lines:if "torch.version.hip" in ln and ln.lstrip().startswith("if "):indent = " " * (len(ln) - len(ln.lstrip()))new_lines.append(ln)new_lines.append(indent + " pass # pulsar disabled (Blackwell linker)\n")skip_block = Truecontinueif skip_block:# Drop any indented continuation lines of the if-block, including# multi-line `from .points import (` parenthesized imports.stripped = ln.strip()if stripped == "" or ln.startswith(" ") or ln.startswith("\t"):if "pulsar" in ln.lower() or stripped == "" or stripped.startswith(")") or stripped.startswith("from ") or stripped.startswith("import "):# part of the pulsar import block — dropcontinueskip_block = Falseelse:skip_block = Falsenew_lines.append(ln)with open(init_path, "w") as f:f.writelines(new_lines)@spaces.GPU(duration=1500)def _build_pytorch3d():# Probe the actual import path Unique3D uses; if the previously installed# pytorch3d still has a broken `renderer/__init__.py` (e.g. left a dangling# `from .points import PulsarPointsRenderer`), uninstall and rebuild.try:from pytorch3d.renderer import TexturesVertex # noqa: F401returnexcept Exception:passsubprocess.call([sys.executable, "-m", "pip", "uninstall", "-y", "pytorch3d"],)src = tempfile.mkdtemp(prefix="pytorch3d_src_")subprocess.check_call(["git", "clone", "--depth=1", "--branch=stable","https://github.com/facebookresearch/pytorch3d.git", src],)_strip_pulsar_from_pytorch3d_src(src)# Force a fresh wheel build (don't reuse the cached broken wheel from a# previous run, which still has the pulsar import in renderer/__init__.py).subprocess.check_call([sys.executable, "-m", "pip", "install","--no-build-isolation", "--no-deps","--force-reinstall", "--no-cache-dir", src],env=_build_env(),)@spaces.GPU(duration=600)def _build_nvdiffrast():_ensure_pkg("nvdiffrast", ["git+https://github.com/NVlabs/nvdiffrast.git"])# Run setup before importing anything that needs these packages._build_pytorch3d()_build_nvdiffrast()if __name__ == "__main__":from huggingface_hub import loginhf_token = os.environ.get("HF_TOKEN")if hf_token:login(token=hf_token)sys.path.append(os.curdir)import torchtorch.set_float32_matmul_precision('medium')26 unmodified linesdef launch():model_zoo.init_models()with gr.Blocks(title=_TITLE,# theme=gr.themes.Monochrome(),5 unmodified linescreate_3d_ui("wkl")demo.queue().launch(share=True)if __name__ == '__main__':fire.Fire(launch)
5 unmodified lines67891011125 unmodified linesclass MyModelZoo:_pipe_disney_controlnet_lineart_ipadapter_i2i: StableDiffusionControlNetImg2ImgPipeline = Nonebase_model = "benjamin-paine/stable-diffusion-v1-5"def __init__(self, base_model=None) -> None:if base_model is not None:5 unmodified lines67891011125 unmodified linesclass MyModelZoo:_pipe_disney_controlnet_lineart_ipadapter_i2i: StableDiffusionControlNetImg2ImgPipeline = Nonebase_model = "stable-diffusion-v1-5/stable-diffusion-v1-5"def __init__(self, base_model=None) -> None:if base_model is not None:
10 unmodified lines111213141516171819202122232410 unmodified lines# from scripts.multiview_inference import geo_reconstructfrom scripts.multiview_inference import geo_reconstruct_part1, geo_reconstruct_part2, geo_reconstruct_part3@spaces.GPU(duration=100)def run_mv(preview_img, input_processing, seed):if preview_img.size[0] <= 512:preview_img = run_sr_fast([preview_img])[0]rgb_pils, front_pil = run_mvprediction(preview_img, remove_bg=input_processing, seed=int(seed)) # 6sreturn rgb_pils, front_pil@spaces.GPU(duration=100) # seems split into multiple part will leads to `RuntimeError`, before fix it, still initialize heredef generate3dv2(preview_img, input_processing, seed, render_video=True, do_refine=True, expansion_weight=0.1, init_type="std"):if preview_img is None:raise gr.Error("The input image is none!")10 unmodified lines111213141516171819202122232410 unmodified lines# from scripts.multiview_inference import geo_reconstructfrom scripts.multiview_inference import geo_reconstruct_part1, geo_reconstruct_part2, geo_reconstruct_part3@spaces.GPU(duration=600)def run_mv(preview_img, input_processing, seed):if preview_img.size[0] <= 512:preview_img = run_sr_fast([preview_img])[0]rgb_pils, front_pil = run_mvprediction(preview_img, remove_bg=input_processing, seed=int(seed)) # 6sreturn rgb_pils, front_pil@spaces.GPU(duration=600) # seems split into multiple part will leads to `RuntimeError`, before fix it, still initialize heredef generate3dv2(preview_img, input_processing, seed, render_video=True, do_refine=True, expansion_weight=0.1, init_type="std"):if preview_img is None:raise gr.Error("The input image is none!")
1234libopengl0libegl1libxkbcommon-x11-0libdbus-1-312345671 unmodified line91011121314152 unmodified lines181920212223242526272829pytorch3d @ https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/py310_cu121_pyt221/pytorch3d-0.7.6-cp310-cp310-linux_x86_64.whlort_nightly_gpu @ https://aiinfra.pkgs.visualstudio.com/2692857e-05ef-43b4-ba9c-ccf1c22c437c/_packaging/d3daa2b0-aa56-45ac-8145-2c3dc0661c87/pypi/download/ort-nightly-gpu/1.17.dev20240118002/ort_nightly_gpu-1.17.0.dev20240118002-cp310-cp310-manylinux_2_28_x86_64.whlonnxruntime_gpu @ https://pkgs.dev.azure.com/onnxruntime/2a773b67-e88b-4c7f-9fc0-87d31fea8ef2/_packaging/7fa31e42-5da1-4e84-a664-f2b4129c7d45/pypi/download/onnxruntime-gpu/1.17/onnxruntime_gpu-1.17.0-cp310-cp310-manylinux_2_28_x86_64.whltorch==2.2.0acceleratedatasetsdiffusers>=0.26.31 unmodified linegradiojaxtypingnumbanumpyomegaconf>=2.3.0opencv_pythonopencv_python_headless2 unmodified linespygltflibpymeshlab>=2023.12rembg[gpu]torch>=2.0.1torch_scatter @ https://data.pyg.org/whl/torch-2.2.0%2Bcu121/torch_scatter-2.1.2%2Bpt22cu121-cp310-cp310-linux_x86_64.whltqdmtransformerstrimeshtypeguardwandbxformersninja123451 unmodified line789101112132 unmodified lines16171819202122232425onnxruntime-gputorch_scatter @ https://data.pyg.org/whl/torch-2.11.0+cu130/torch_scatter-2.1.2+pt211cu130-cp310-cp310-linux_x86_64.whlacceleratedatasetsdiffusers>=0.26.31 unmodified linegradiojaxtypingnumbanumpy<2omegaconf>=2.3.0opencv_pythonopencv_python_headless2 unmodified linespygltflibpymeshlab>=2023.12rembg[gpu]tqdmtransformerstrimeshtypeguardwandbxformersninja
1234567891011121314151617181920import onnxruntimeimport torchproviders = [# ('TensorrtExecutionProvider', {# 'device_id': 0,# 'trt_max_workspace_size': 8 * 1024 * 1024 * 1024,# 'trt_fp16_enable': True,# 'trt_engine_cache_enable': True,# }),('CUDAExecutionProvider', {'device_id': 0,'arena_extend_strategy': 'kSameAsRequested','gpu_mem_limit': 8 * 1024 * 1024 * 1024,'cudnn_conv_algo_search': 'HEURISTIC',})]def load_onnx(file_path: str):assert file_path.endswith(".onnx")123456789import onnxruntimeimport torch# ZeroGPU containers ship CUDA 13; the onnxruntime-gpu wheel links against# libcublasLt.so.12 (CUDA 12) → CUDAExecutionProvider fails to load. Use CPU.providers = ['CPUExecutionProvider']def load_onnx(file_path: str):assert file_path.endswith(".onnx")
3 unmodified lines456789103 unmodified linesfrom copy import deepcopyENABLE_CPU_CACHE = FalseDEFAULT_BASE_MODEL = "benjamin-paine/stable-diffusion-v1-5"cached_models = {} # cache for models to avoid repeated loading, key is model namedef cache_model(func):3 unmodified lines456789103 unmodified linesfrom copy import deepcopyENABLE_CPU_CACHE = FalseDEFAULT_BASE_MODEL = "stable-diffusion-v1-5/stable-diffusion-v1-5"cached_models = {} # cache for models to avoid repeated loading, key is model namedef cache_model(func):
12 unmodified lines131415161718192021222324252612 unmodified linesfrom PIL import Imageimport trimeshproviders = [('CUDAExecutionProvider', {'device_id': 0,'arena_extend_strategy': 'kSameAsRequested','gpu_mem_limit': 8 * 1024 * 1024 * 1024,'cudnn_conv_algo_search': 'HEURISTIC',})]session = new_session(providers=providers)12 unmodified lines13141516171819202112 unmodified linesfrom PIL import Imageimport trimesh# onnxruntime-gpu wheel links against CUDA 12 libs; ZeroGPU containers ship# CUDA 13 → CUDAExecutionProvider can't load. Use CPU for rembg.providers = ['CPUExecutionProvider']session = new_session(providers=providers)
README.md+1-1
app.py+202-16
gradio_app/all_models.py+1-1
gradio_app/gradio_3dgen.py+2-2
packages.txtAdded+4
requirements.txt+4-8
scripts/load_onnx.py+3-14
scripts/sd_model_zoo.py+1-1
scripts/utils.py+3-8