High-Performance GPU-to-CPU Transpilation and Optimization via High-Level Parallel Constructs

William S. Moses, Ivan R. Ivanov, Jens Domke, Toshio Endo, Johannes Doerfert, Oleksandr ZinenkoPpopp 2022

TL;DR

This paper presents a novel approach to automatically translate GPU programs (CUDA) into CPU-executable code using the MLIR compiler infrastructure. By introducing a high-level representation of parallel constructs and their semantics, the framework enables significant performance improvements over hand-written CPU code and allows GPU-native applications like PyTorch to run efficiently on CPU-only supercomputers.

What Problem Does This Paper Address?

The increasing reliance on specialized hardware like GPUs for high-performance computing presents a significant challenge for performance portability. As hardware evolves, applications often require costly manual re-engineering to adapt to new architectures or programming models. While many tools exist for performance portability, they often demand manual code porting. This paper tackles the problem of automatically migrating GPU code to CPUs, aiming to preserve performance and reduce the burden of manual porting, especially for modern frameworks like PyTorch that are not well-supported on CPU-only supercomputers.

Methods

The core of the proposed solution lies in a compiler framework built on MLIR (Multi-Level Intermediate Representation). The key innovations include:

  • High-Level Parallel Construct Representation: Instead of treating GPU parallelism as an opaque "black box," the system represents parallel constructs (like thread blocks, grids, and synchronization) using MLIR's dialect system. This allows for transparent application of conventional compiler transformations.
  • Semantic Barrier Definition: A novel polygeist.barrier operation is introduced. Unlike traditional compiler barriers, this operation is defined by its memory semantics, allowing optimizations to reason about and move code across barriers more effectively, as long as external behavior is preserved.
  • Polygeist Frontend Extension: The Polygeist C/C++ frontend is extended to directly emit MLIR from CUDA code, preserving high-level parallelism and program structure.
  • End-to-End Transpilation and Optimization: The framework supports transpiling CUDA kernels to CPU threads, leveraging OpenMP for parallel execution on the CPU. It incorporates optimizations like barrier elimination, memory-to-register promotion, and parallel loop transformations.
  • MocCUDA Integration: A compatibility layer, MocCUDA, is developed to enable PyTorch to run on CPU-only systems by intercepting CUDA runtime calls and transpiling PyTorch's CUDA kernels using the Polygeist/MLIR framework.

Datasets

The paper evaluates its approach using:

  • Rodinia CUDA Benchmark Suite: A subset of this suite was used to compare transpiled CUDA code against native OpenMP versions and GPU execution.
  • PyTorch ResNet-50: A PyTorch implementation of the ResNet-50 neural network was used to demonstrate the end-to-end integration and performance on the CPU-only Fugaku supercomputer.

Key Insights and Ideas

  • Semantic Barrier Definition is Crucial: Defining barriers by their memory effects, rather than as opaque optimization barriers, unlocks significant opportunities for compiler optimizations that were previously impossible.
  • MLIR's Role in Heterogeneous Compilation: MLIR's ability to represent code at multiple abstraction levels and its dialect system are instrumental in handling the complexities of GPU-to-CPU transpilation and optimization.
  • Performance Portability is Achievable: The framework demonstrates that high-performance GPU code can be automatically translated to efficient CPU code, achieving competitive or even superior performance compared to hand-tuned CPU implementations.
  • Bridging the Gap for Frameworks: The MocCUDA integration shows a viable path for running complex deep learning frameworks like PyTorch on CPU-only hardware, addressing a critical gap in current HPC ecosystems.

Key Results

  • Rodinia Benchmarks: The transpiled CUDA code achieved a 76% geomean performance improvement over hand-written OpenMP code on a commodity CPU when inner serialization optimization was enabled. Without inner serialization, a 43.7% geomean speedup was observed.
  • PyTorch ResNet-50 on Fugaku: The MocCUDA integration, using Polygeist-generated kernels, achieved 2.7x throughput increase compared to Fujitsu's tuned oneDNN backend on the CPU-only Fugaku supercomputer.
  • Comparison to MCUDA: The proposed approach achieved performance within 1.3% of MCUDA on a matrix multiplication kernel, and with inner loop serialization, it achieved a 14.9% geomean speedup over MCUDA.

Limitations and Future Work

  • Unsupported CUDA Features: The current Polygeist frontend supports a subset of CUDA and C++ features. Benchmarks using unsupported features (e.g., virtual functions, texture memory, certain C++ constructs) were excluded.
  • Nested Parallelism Overhead: While the framework can represent nested parallel regions for shared memory, it notes that this can sometimes lead to overhead. Future work could explore optimizing the generation of nested OpenMP constructs.
  • Rescheduling for CPU Hierarchies: The authors suggest that future work could involve advanced rescheduling of the transpiled code to better leverage CPU-style memory hierarchies, potentially leading to further performance gains.
  • Broader Benchmark Coverage: While Rodinia and PyTorch ResNet-50 are good demonstrations, extending the coverage to a wider range of benchmarks and applications would be beneficial.

Summary updated

BibTeX
@inproceedings{moses2022high,
  author = {William S. Moses and Ivan R. Ivanov and Jens Domke and Toshio Endo and Johannes Doerfert and Oleksandr Zinenko},
  title = {{High-Performance GPU-to-CPU Transpilation and Optimization via High-Level Parallel Constructs}},
  booktitle = {Ppopp},
  year = {2022},
}
Figure