# misc.py file contains miscellaneous utility functions import math import sys import logging import os import subprocess def pause(): """ Pauses the execution until any key is pressed. """ if sys.platform.startswith('win'): import msvcrt print("Press any key to continue...") msvcrt.getch() else: import termios import tty print("Press any key to continue...") fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(fd) sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) def install(package): import subprocess subprocess.check_call([sys.executable, "-m", "pip", "install", package]) def get_filename(file): filename = None if file is not None: filename = file.name return filename def get_extension(file): extension = None if file is not None: extension = file.name.split(".")[-1] return extension def convert_ratio_to_dimensions(ratio, height=512, rotate90=False): """ Calculate pixel dimensions based on a given aspect ratio and base height. This function computes the width and height in pixels for an image, ensuring that both dimensions are divisible by 16. The height is adjusted upwards to the nearest multiple of 16 if necessary, and the width is calculated based on the adjusted height and the provided aspect ratio. Additionally, it ensures that both width and height are at least 16 pixels to avoid extremely small dimensions. Parameters: ratio (float): The aspect ratio of the image (width divided by height). height (int, optional): The base height in pixels. Defaults to 512. Returns: tuple: A tuple containing the calculated (width, height) in pixels, both divisible by 16. """ base_height = 512 # Scale the height based on the provided height parameter # Ensure the height is at least base_height scaled_height = max(height, base_height) # Adjust the height to be divisible by 16 adjusted_height = math.ceil(scaled_height / 16) * 16 # Calculate the width based on the ratio calculated_width = int(adjusted_height * ratio) # Adjust the width to be divisible by 16 adjusted_width = math.ceil(calculated_width / 16) * 16 if rotate90: adjusted_width, adjusted_height = adjusted_height, adjusted_width return adjusted_width, adjusted_height def install_torch(): print("\nInstalling PyTorch with CUDA support...") # Define the package and index URL package = "torch==2.4.0" index_url = "https://download.pytorch.org/whl/cu124" # Construct the pip install command command = [ "pip", "install", "--force-reinstall", f"{package}", "--index-url", f"{index_url}" ] # Run the command using subprocess subprocess.run(command, check=True) print("\nPyTorch installation completed.") print("\nInstalling torchvision...") package = "torchvision==0.19.0" index_url = "https://download.pytorch.org/whl/cu124" # Construct the pip install command command = [ "pip", "install", "--force-reinstall", f"{package}", "--index-url", f"{index_url}" ] # Run the command using subprocess subprocess.run(command, check=True) print("\nPlease restart the kernel to use the newly installed PyTorch version.") def _get_output(cmd): try: return subprocess.check_output(cmd).decode("utf-8") except Exception as ex: logging.exception(ex) return None def install_cuda_toolkit(): #CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run" # CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/12.2.0/local_installers/cuda_12.2.0_535.54.03_linux.run" CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/12.4.1/local_installers/cuda_12.4.1_550.54.15_linux.run" CUDA_TOOLKIT_FILE = "/tmp/%s" % os.path.basename(CUDA_TOOLKIT_URL) print("\nDownloading CUDA Toolkit from %s" % CUDA_TOOLKIT_URL) subprocess.call(["wget", "-q", CUDA_TOOLKIT_URL, "-O", CUDA_TOOLKIT_FILE]) subprocess.call(["chmod", "+x", CUDA_TOOLKIT_FILE]) subprocess.call([CUDA_TOOLKIT_FILE, "--silent", "--toolkit"]) os.environ["CUDA_HOME"] = "/usr/local/cuda" os.environ["PATH"] = "%s/bin:%s" % (os.environ["CUDA_HOME"], os.environ["PATH"]) os.environ["LD_LIBRARY_PATH"] = "%s/lib:%s" % ( os.environ["CUDA_HOME"], "" if "LD_LIBRARY_PATH" not in os.environ else os.environ["LD_LIBRARY_PATH"], ) # Fix: arch_list[-1] += '+PTX'; IndexError: list index out of range os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0;8.6" print("\nPlease restart the kernel to use the newly installed CUDA Toolkit.") def setup_runtime_env(): from torch import cuda logging.info("Python Version: %s" % _get_output(["python", "--version"])) logging.info("CUDA Version: %s" % _get_output(["nvcc", "--version"])) logging.info("GCC Version: %s" % _get_output(["gcc", "--version"])) logging.info("CUDA is available: %s" % cuda.is_available()) logging.info("CUDA Device Capability: %s" % (cuda.get_device_capability(),)) # Install Pre-compiled CUDA extensions (Fallback to this solution on 12/31/24) # Ref: https://huggingface.co./spaces/zero-gpu-explorers/README/discussions/110 ##ext_dir = os.path.join(os.path.dirname(__file__), "wheels") ##for e in os.listdir(ext_dir): ## logging.info("Installing Extensions from %s" % e) ## subprocess.call( ## ["pip", "install", os.path.join(ext_dir, e)], stderr=subprocess.STDOUT ## ) # Compile CUDA extensions # Update on 12/31/24: No module named 'torch'. But it is installed and listed by `pip list` # ext_dir = os.path.join(os.path.dirname(__file__), "citydreamer", "extensions") # for e in os.listdir(ext_dir): # if os.path.isdir(os.path.join(ext_dir, e)): # subprocess.call(["pip", "install", "."], cwd=os.path.join(ext_dir, e)) #logging.info("Installed Python Packages: %s" % _get_output(["pip", "list"]))