Published at

Automated Nunchaku Wheel URL Generation for Seamless Installation

Automated Nunchaku Wheel URL Generation for Seamless Installation

This blog post explains how to automatically determine the correct Nunchaku wheel URL based on system specifications, ensuring a smooth installation process. It caters to intermediate learners.

Authors
  • avatar
    Name
    James Lau
    Twitter
  • Indie App Developer at Self-employed
Sharing is caring!
Table of Contents

Introduction

The nunchaku library is a powerful tool, but installing it can sometimes be tricky due to its dependency on specific system configurations and PyTorch versions. This post dives into the code that automatically generates the correct wheel URL for Nunchaku, simplifying the installation process.

The get_nunchaku_wheel_url Function

The core of this automation lies in the get_nunchaku_wheel_url function. Let’s break down how it works:

1. Detecting Python Version

The code first extracts the Python version using sys.version_info. It formats this into a tag like cp310 for compatibility with PyPI.

python_version = f"cp{sys.version_info.major}{sys.version_info.minor}"

2. Detecting PyTorch Version

The PyTorch version is obtained using torch.__version__. The CUDA suffix (e.g., +cu126) is removed to isolate the major and minor versions, creating a tag like torch2.6.

torch_version = torch.__version__.split('+')[0]
torch_major_minor = '.'.join(torch_version.split('.')[:2])
torch_tag = f"torch{torch_major_minor}"

3. Detecting Platform Information

The platform module is used to determine the operating system and machine architecture. This information is crucial for selecting the correct wheel.

system = platform.system().lower()
machine = platform.machine()

For Windows, it checks if the machine is amd64 or x86_64 to determine whether to use win_amd64 or win32 as the platform tag.

4. Constructing the Wheel URL

The function combines all the detected information (version, PyTorch tag, and platform) to construct a unique wheel filename. It then prepends this filename to a base Hugging Face URL to create the complete download link.

wheel_name = f"nunchaku-{version}+{torch_tag}-{python_version}-{python_version}-{platform_tag}.whl"
base_url = "https://huggingface.co/mit-han-lab/nunchaku/resolve/main/"
wheel_url = base_url + wheel_name

Automated Installation

The code includes a try...except block to handle cases where Nunchaku is not already installed. If an import error occurs, it calls get_nunchaku_wheel_url to obtain the correct URL and then uses subprocess.run to install Nunchaku using pip.

try:
    import nunchaku
except:
    # Get the correct wheel URL
    wheel_url = get_nunchaku_wheel_url()

    # Install Nunchaku with the auto-detected wheel
    print("\nInstalling Nunchaku...")
    subprocess.run([sys.executable, "-m", "pip", "install", wheel_url], check=True)

    print("Installation complete!")

Real-World Context

This automated approach is invaluable in environments where manual wheel selection is cumbersome or error-prone. It ensures that users, regardless of their system configuration, can easily install Nunchaku and leverage its capabilities.

Conclusion

The get_nunchaku_wheel_url function provides a robust solution for automatically generating the correct Nunchaku wheel URL, simplifying installation and promoting wider adoption of this powerful library. This code exemplifies how to automate dependency management based on system-specific details.

Sharing is caring!