在PPU上从源码构建PIP软件包

Updated at:
Copy as MD

This document explains how to build packages from source code on PPU and covers common issues.

Build an open-source library from scratch

1. Check package availability on PyPI

Search for the library name, for example, lmdeploy, on PyPI.

image

The search results show 24 packages related to the keyword lmdeploy. Find the package that corresponds to the open source library on GitHub.

image

You can see that lmdeploy on PyPI is available up to version 0.7.1.

image

2. Identify if the library is CUDA-related

  • Criterion 1: The README file contains an introduction to the open source library and describes its functionality. This is a basic check.

  • Criterion 2: Check if CUDA is listed in the Languages section on GitHub. If CUDA accounts for a high percentage of the code, it is likely a CUDA-related open source library that belongs in the PTG PIP source. If CUDA accounts for only 2%, you must use other criteria for a final decision.

    image.png

  • Criterion 3: Extract the whl package of the open source library and check all files for the .cu extension.

    image.png

    wget https://files.pythonhosted.org/packages/b8/92/fa66b0684c0eace3a480498e70ad40a0f3784890a25b480ae05fb8d7a458/lmdeploy-0.7.1-cp310-cp310-manylinux2014_x86_64.whl
    unzip lmdeploy-0.7.1-cp310-cp310-manylinux2014_x86_64.whl
    find ./ -name "*.cu" 
  • Criterion 4: Extract the whl package of the open source library, then iterate through all .so files to check if their filenames contain keywords like "cuda" or "nv_fatbin".

    find ./ -name "*.so*" | xargs readelf -a | grep 'nv_fatbin'
    find ./ -name "*.so*" | xargs readelf -a | grep 'cuda'

    image

3. Build the open-source library

3.1 Build dependencies

  • First, confirm the requirements of the open source library and install the dependencies required for the build. This information is typically found in requirement files or directories. For example, to build lmdeploy, the dependencies listed in the build.txt file within the requirements path are mandatory. You should also pay close attention to readthedocs.txt, docs.txt, lite.txt, and serve.txt, as they are often needed for the build. Dependencies with "runtime" in their names are more often used during the testing phase but may also be required for the build, depending on the open source library.

    image

    image

  • You may need to install dependencies by using apt-get or pip install, or by building them from source code. We recommend that you use a search engine, such as Google or DeepSeek, to find the correct way to install the dependencies.

  • Note: Many AI-related open source libraries depend on PyTorch. Therefore, check if your library has a PyTorch dependency.

    image

  • You must also identify if a dependency is CUDA-related. If it is, you need to build that dependency first and add it to the PTG PIP source. There are two ways to install a CUDA-related dependency. The first is to use a pre-built package from the PTG PIP source and install it with pip install. The second is to include the dependency build in your build steps: first build and install the dependency, then build the current open source library.

  • The information in requirements files may not be 100% accurate. Some listed dependencies might not be used in the actual build, while some required dependencies might be missing and are only discovered through errors during the build phase. This is expected.

  • The following are the steps to install dependencies for lmdeploy. The requirements information includes pybind11. However, rapidjson-dev and openmpi were identified as missing dependencies only after analyzing errors during the build.

    pip install torch==2.5.1+ppu1.4.2 # You must use the PyTorch from the PTG PIP source. Version 1.4.2 is for demonstration purposes only.
    pip install pybind11
    apt update
    apt-get install rapidjson-dev
    # Install Open MPI. A specific version is required according to the official documentation.
    wget https://download.open-mpi.org/release/open-mpi/v4.1/openmpi-4.1.5.tar.gz
    tar xf openmpi-4.1.5.tar.gz
    cd openmpi-4.1.5
    ./configure
    make -j
    make install
    cd ..

3.2 Build procedure

  • The library's GitHub repository provides the simplest build method. On an NVIDIA device, you can successfully build it by using the following method.

    git clone -b v0.3.0 https://github.com/InternLM/lmdeploy.git
    cd ./lmdeploy
    python setup.py bdist_wheel
  • Using setup.py is simpler, but if a build error occurs, the error logs are often incomplete, making it difficult to diagnose the problem. We recommend using CMake to build instead. When porting a new open source library to PPU, errors are likely. Therefore, you should thoroughly collect, locate, and resolve issues.

    git clone -b v0.3.0 https://github.com/InternLM/lmdeploy.git
    cd ./lmdeploy
    mkdir build && cd build
    cmake  ..
    make -j
    cd ../
    python setup.py bdist_wheel
  • The CMake build of lmdeploy exposed many errors. We resolved them using Google, DeepSeek, and our own experience. The final build script is as follows.

    git clone -b v0.3.0 https://github.com/InternLM/lmdeploy.git
    cd ./lmdeploy
    # Configure environment variables for Open MPI
    export PATH=/usr/local/bin:$PATH
    export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
    mkdir build && cd build
    cmake  .. \
        -DCMAKE_BUILD_TYPE=RelWithDebInfo \
        -DCMAKE_EXPORT_COMPILE_COMMANDS=1 \
        -DCMAKE_INSTALL_PREFIX=./install \
        -DBUILD_PY_FFI=ON \
        -DBUILD_MULTI_GPU=ON \
        -DCMAKE_CUDA_FLAGS="-lineinfo" \
        -DUSE_NVTX=ON \
        -DNCCL_LIBRARIES=/usr/local/PPU_SDK/CUDA_SDK/lib64/libnccl.so
    make -j
    cd ../
    python setup.py bdist_wheel

4. Build examples

Example of an open source library built using a .sh script

Build ONNX Runtime on PPU

A complex journey of building an open source library

Build Paddle on PPU

Common unit test issues

1. Import errors

An ImportError: cannot import name 'xxx' error is often caused by:

  • Name does not exist: The function, class, or variable you are trying to import is not defined in the module. You can usually resolve this by installing the corresponding package with pip install.

    This issue can usually be resolved by installing the corresponding package with pip install.

  • Spelling, case sensitivity, or path issues.

    First, check for spelling errors, and then verify if there are path issues with the file corresponding to that version. Python's import logic is as follows: for an import statement such as import aa.bb.cc, Python starts from the installation path of the whl package, finds the corresponding package folder, and then sequentially verifies that the aa path, bb path, and cc file are correct. Some versions of certain open-source packages inherently have this type of issue.

2. "Cannot find xxx.so" error

If you encounter this type of issue, first check the output of echo $PYTHONPATH to determine if the corresponding .so file can be found in the current path or the package installation path. If the file is not found in the current path but is found in the package installation path, this usually means that some dependencies in the current test environment are locating the corresponding Python files in the source code. In this case, move the tests folder from the source code path to a separate directory. (Do not rename the folder, because this may break some dependencies. Also, ensure that the unit tests and the built code are from the same version.)

3. Triton support issues

Triton support issues appear in various forms. Here are some common examples:

  1. Cannot find a working triton installation: Triton is not installed.

  2. cannot compiled: The Triton version is mismatched.

  3. cannot load xxx.ptx: The Triton package for CUDA was installed by mistake.

Important note: Triton can only install whl packages built internally by PTG. If the Triton version does not contain keywords such as ptg, ppu, or sail, you have likely installed the wrong Triton package.

4. cuBLAS, cuSPARSE, and cuDNN support errors

These three types of errors are caused by a lack of support in the operator library, which produces clear error messages such as in function cublas_gemm: cuBLAS Error: an unsupported value or parameter was passed to the function.

5. Test exits silently

You need to debug this issue locally. First, confirm if it is a random issue. Currently, the tests are primarily managed by using pytest. The basic command to run a test is pytest -v xxx.py::test_xxxa[abcdefg]. The -v option is added to identify exactly which test failed. The following are the steps to check when no error message is displayed:

  1. Rerun the test command to ensure the failure is reproducible and not caused by a temporary environment issue.

  2. If the failure is reproducible, check dmesg.log for runtime exceptions or ERROR messages.

  3. If dmesg provides no relevant information, run the pytest -v xxx.py::test_xxxa[abcdefg] --trace command to perform single-step debugging with pdb. This ensures that all print information is captured because sometimes relevant information is not visible without single-step debugging.

    image.png

    The preceding image shows an error that can only be seen when using step-by-step debugging. This type of output cannot be captured when the command is run directly. The error message clearly indicates that the failure is caused by a lack of cuSPARSE support, which is the same root cause as the errors described in the previous section.

6. Precision issues and incorrect test results

These two types of errors produce similar messages, which typically include information like max, min, and difference, and provide a comparison of two numbers, similar to this:

AssertionError: Outputs not close enough in tensor at idx=0. Location of the maximum difference: 2669 with 0.013854987919330597 vs 0.01314355805516243 (diff 0.0007114298641681671).

A developer should confirm whether this is a precision issue or an incorrect result.

7. Outdated CMake version

Download from: https://cmake.org/files/dev/?C=M;O=D

bash cmake-3.28.4-linux-x86_64.sh --prefix=/usr/local --exclude-subdir