AI profiling: Performance analysis examples

Updated at:

Use the visualization page of online performance detection results to analyze Compute Unified Device Architecture (CUDA) calls in PyTorch training and virtual large language model (vLLM) inference on GPU-accelerated pods in Container Service for Kubernetes (ACK) clusters, locate performance bottlenecks, find tuning directions, and improve GPU utilization and application efficiency.

Example of PyTorch training

Sample environment

  • Framework: PyTorch 2.6.0

  • Model: ResNet18

  • Data: Cifar10

  • GPU: NVIDIA A10

  • Profiling duration: 5s

  • Enabled profiling items: all

Result analysis

  1. Observe the time proportion of GPU function calls. The following example shows that the overall GPU busy time is not high. The busy time includes kernel function execution (cuda_runtime and Kernel), GPU memory operations (Memset), and CUDA synchronization (cuda_sync). The proportion of kernel function execution time meets expectations.

    image

  2. Observe the distribution of GPU kernel functions: In this example, kernel functions mainly run outside Tensor Cores, resulting in low Tensor Core utilization. At the same time, the main executed kernel functions are concentrated in vector operations of libraries such as cuDNN and cuBLAS, for example, matrix multiplication. These details can be used to evaluate the performance of custom CUDA kernel functions.

    image.png

    The GPU Kernel Function Details table shows the performance data for five kernel functions: void cask__5x_c... (run 159 times, total duration 384.80 us), _5x_cudnn_amper... (run 53 times, total duration 35041.43 us), void at::native... (run 1060 times, total duration 2385.17 us), GPU cudnn::bn_... (run 53 times, total duration 29800.92 us), and void at::native... (vector scaling, run 901 times, total duration 54144.91 us). For these kernel functions, the Use Tensor column shows "No" and the SM Utilization (%) is 0. This confirms that Tensor Cores were not effectively utilized.

  3. Analyze TimeLine: Specifically analyze a training step, including data loading, forward propagation, backward propagation, and optimizer updates. Mark the boundaries between each step. The Python part of the TimeLine is as follows, which clearly shows the execution of each part.

    image.png

    The preceding figure clearly shows that, in the entire TimeLine, the time proportion of DataLoad is higher than that of other steps, and a blank period exists in DataLoad. Further observation of the CUDA kernel function part reveals that in the first half of DataLoad, the kernel functions of the Optimizer update from the previous Step are still executing. After DataLoad is completed, data transmission (cuMemCpyH2D) begins, transferring the loaded data from the Host to the GPU Device. After data transmission is completed, corresponding calculations are performed based on different stages.

    image.png

  4. Through analysis, the key measures to improve overall GPU utilization mainly include the following aspects:

    • Optimize data loading:

      • Increase the number of Dataloaders.

      • Enable Pin Memory for Dataloaders.

      • Set up overlapping of data loading and computation.

    • Improve GPU utilization:

      • Automatic mixed precision.

      • Increase Batch Size.

Example of vLLM inference

Sample environment

  • Framework: vLLM 0.5.0

  • Model: Qwen2-7B

  • GPU: NVIDIA A10

  • Profiling duration: 5s

  • Enabled profiling items: all

Result analysis

Model loading

  1. View the overall process of model loading: The enlarged view reveals that the entire model loading process is divided into three steps: data reading, data copying, and decoding.imageimageIn Python profiling, dividing lines separate the stages, as shown in the following figure. Because this example shows the first startup, PageCache is not used. Therefore, the time required by data transmission is particularly noticeable.image

  2. Examine the profiling details for each stage. Zooming in shows that the main activity involves I/O-related system calls, such as openat, mmap, read, and ioctl. Because the model is loaded using safetensors.torch.load_file for random access reads, mmap and read calls appear frequently.image

  3. The data transmission process shows that system calls are concentrated in poll, epoll, and futex. Multiple CPUs are frequently used and switched. In terms of CUDA calls, there are as many cuMemcpyHtoD transmissions as there are model safetensors. Therefore, combined with the model loading process of vLLM, the approximate path of model data transmission is: disk or network -> memory (PageCache) -> GPU memory. Add network and disk I/O data to the figure to help you clearly observe the path. In the Syscall tracing view, a total of 433018 system calls are recorded and shown as colored blocks on the timeline to reflect concurrent calls from multiple threads. The futex calls, which wait for thread synchronization, occupy most of the area, with a small number of poll and ppoll calls that wait for I/O multiplexing. This behavior indicates extensive inter-thread synchronization and blocking waits in this phase.image

    image

  4. Observe the decoding process and focus on GPU-related library calls. The matrix multiplication executed in each batch and its upstream and downstream calls (such as cuLaunchKernel, cuMemcpyDtoD, cublasGemmEx) regularly appear and can be aligned with the corresponding Python Decoding method. Therefore, the stages they represent can be clearly distinguished.image

Model inference

Note

This example uses a manually simulated NCCL hang to show how issues are discovered and inferred.

  1. An NCCL hang usually manifests as getting trapped in kernel mode or blocking at the NCCL communication or I/O layer. To reproduce the scenario, suspend and restore different processes to simulate the interruption and recovery of NCCL communication between them:

    kill -STOP <PID>
    kill -CONT <PID>

    Normal inference duration without interruption:

    Start At: 2024-08-02/02:51:51, Duration Time: 14.090227s
    ----------------------------------------------------------------------------------
    -------------------

    Inference duration with a 5-second interruption:

    Start At: 2024-08-02/02:53:00, Duration Time: 21.281224s
    --------------------------------------------------------------------------------
    ------------------
  2. During the process of active interruption, the above steps show that each inference in the example takes about 15 seconds. With a simulated interruption of 5 seconds, the total duration for the inference request after recovery becomes 20 seconds, which aligns with the expected outcome.

    Observations show that each inference process of vLLM consists of multiple inference computation steps, as highlighted by the red box in the following figure. The general process is as follows: cuda memory copy H2D -> nccl broadcast -> cublas compute -> nccl send and recv -> cuda memory copy D2H. Therefore, NCCL may be interrupted during the broadcast and send and recv processes.image

  3. The interruption point determines the blocked NCCL action. Enable 60 seconds of GPU-only profiling and convert the profiling data to Chrome Tracing format data. The following list describes two cases of NCCL being blocked:

    1. nccl send and recv

      The visualization results are as follows:

      image.png

      First, consider the normal case. During the end time of an inference, there is usually one pnccl Send in one replica, and a corresponding pnccl Recv in another replica. When the simulation for the NCCL hang is enabled, the profiling view shows the following situation:

      image.png

      image.png

      image.png

      One replica issues an ncclSend call, but the other replica has no corresponding ncclRecv action call. The GPU-related method call then enters an idle state for a period of time. Another replica executes the corresponding ncclRecv action only after the waiting process status is restored. Without understanding the underlying logic, it can be inferred that this phenomenon is caused by an NCCL hang issue.

    2. nccl broadcast

      The following figure shows the effect of a Hang operation performed before nccl broadcast:

      image.png

      Zoom in on the method executed before the hang.

      image.png

      image.png

      The execution of cuMemcpyH2D is clearly visible. After the execution, the process hangs. Then, move to the position after the hang recovery, as shown in the following figure:

      image.png

      image.png

      Zoom in on the corresponding position. The first operation executed after the hang recovery is the NCCL broadcast, followed by subsequent standard actions. Based on this phenomenon, the problem can also be roughly inferred as being caused by an NCCL hang.