PaddlePaddle Deep Learning Platform

repository·develop·Indexed 12 days ago

https://github.com/paddlepaddle/paddle

An industrial-grade deep learning platform providing core frameworks, model libraries, and end-to-end development kits. It features CINN (Compiler Infrastructure and Neural Network) for hardware-independent computation representation via a two-level IR model (HLIR and CINN IR), an inference analysis pipeline for program optimization, and support for both standard Layer and custom PyLayer definitions in imperative mode.

Tokens
51.2K
Snippets
129
Records
186
Agent score
97%

What's inside PaddlePaddle

  1. Overview of the Paddle High reusability operator library (PHI)

    develop

    The Paddle High reusability operator library (PHI), also known as the 'functional operator library', is a refactored operator architecture designed to replace the original Paddle Fluid Framework operator library.

    PHI provides a functional paradigm that allows developers to implement new operator kernels by combining existing kernel functions and the Kernel Primitives API (KPS). This design supports:

    • High Reusability: Implementing new operators by calling functional interfaces instead of constructing complex ExecutionContext objects.
    • Hardware Plug-ins: Support for new hardware or acceleration libraries via plug-in access.
    • Unified Training and Inference: A single library that serves training, server-side inference, and mobile-side (Paddle-Lite) inference, reducing maintenance costs and bugs caused by implementation mismatches.
    • Improved Performance: A function-based scheduling architecture that reduces the overhead of the dynamic graph and enables fine-grained execution scheduling for static graphs.
  2. Access PaddlePaddle documentation and resources

    develop

    PaddlePaddle provides extensive documentation in English and Chinese to help you implement deep learning models and tasks.

    • Guides: Learn the basics of implementing deep learning with PaddlePaddle.
    • Practices/Tutorials: Learn how to build models and execute deep learning tasks more efficiently.
    • API Reference: Detailed technical documentation for all available APIs.

    Community & Support:

  3. Understand the Paddle PHI (High reusability operator library) Design

    develop

    PHI is PaddlePaddle's High reusability operator library. It is designed as a basic component used by various runtimes (like fluid and lite) and can be compiled as a separate dynamic library.

    Key design goals include:

    • Reusability: High reuse between Operators and Operator Kernels.
    • Split Compilation: Support for compiling specifically for certain devices (CPU, GPU) or scenarios (Training vs. Inference).
    • Unified Implementation: Aiming for a single kernel implementation that adapts to various devices via a Kernel Primitive API.
    • Ease of Use: Clear directory structures and concise namespaces for developers adding new kernels.
  4. How Kernel management works in PHI

    develop

    PHI manages kernels through a centralized system designed for high reusability and efficiency:

    • KernelFactory: A global singleton that manages kernels using a two-level map. The first level maps by operator name, and the second level maps by KernelKey.
    • KernelKey: A unique identifier for a kernel instance, combining Backend (replacing the older place and library_type split) to simplify lookup.
    • Kernel: An object that holds the execution function (KernelFn) and KernelArgsDef. It stores metadata for Tensor inputs/outputs (Device, Dtype, Layout) and Attribute inputs.
  5. Best practices for implementing fusion kernels

    develop

    When implementing fusion kernels in PaddlePaddle, follow these architectural guidelines:

    • Avoid Python APIs: Do not implement Python APIs for fusion kernels. They typically involve complex input/output arguments that are difficult to use in Python. Instead, trigger fusion kernels via optimization passes in dy2static mode or in static graph mode.
    • Kernel Reuse: Do not reuse a fusion kernel as a building block for other kernels. Instead, implement fusion kernels by reusing existing individual kernels.
    • Backend Specificity: Fusion kernels are intended to accelerate combined operations on specific backends. You are not required to implement them for all devices. If a kernel is only intended for a specific backend, append the backend name as a suffix to the kernel name (e.g., fused_matmul_onednn or fused_fc_xpu).
    • Avoid Pseudo Kernels: Do not implement 'pseudo kernels' that simply throw exceptions. If a kernel is not required for a specific backend, it should simply not be implemented.
    • Namespace Requirement: All fusion kernels must reside in the phi/fusion namespace.
  6. How C++ APIs are auto-generated from YAML

    develop

    To reduce maintenance costs and ensure consistency, PHI automatically generates C++ API code by parsing YAML configuration files. This ensures that the C++ API stays in sync with the operator definitions.

    Configuration Files

    • Forward API: Defined in paddle/phi/ops/yaml/ops.yaml. This generates paddle/phi/api/include/api.h and paddle/phi/api/lib/api.cc.
    • Backward API: Defined in paddle/phi/ops/yaml/backward.yaml. This generates the necessary backward API headers and implementation files (e.g., backward_api.h, backward_api.cc).

    Developers modify the YAML files to add or update operators, and the build system handles the code generation for the C++ interfaces.

  7. How CINN works: The two-level IR model

    develop

    CINN functions by lowering a traditional Deep Neural Network (DNN) model through two levels of Intermediate Representation (IR):

    1. High-Level IR (HLIR): Used to define domain-specific computations and perform overall optimization on the IR-graph.
    2. CINN IR: Represents computation semantics and is eventually lowered to a specific hardware backend (currently targeting x86 CPUs and Nvidia GPUs).

    Both levels utilize an SSA (Static Single Assignment) graph and provide analysis and optimization facilities. Optimizations are applied via schedule transforms on the CINN IR.

  8. Understand the code checking workflow with xdoctest

    develop

    The code checking process is divided into three main stages: Interface Extraction, Example Execution, and Result Comparison.

    When using xdoctest, the responsibilities are distributed as follows:

    1. Interface Extraction: Uses sampcd_processor_utils.py to extract docstrings.
    2. Example Execution: Uses sampcd_processor_xdoctest.py (via Xdoctester) to run the extracted examples.
    3. Result Comparison: Uses sampcd_processor_xdoctest.py (via Xdoctester) to compare results.

    The execution flow follows these steps:

    1. Initialize logger via init_logger.
    2. Check test mode via check_test_mode.
    3. Get test capacity via get_test_capacity.
    4. Extract test docstrings via get_docstring.
    5. Prepare the doctester via doctester.prepare(sample_code_test_capacity).
    6. Run code checking and get results via get_test_results(doctester, docstrings_to_test).
    7. Print summary via doctester.print_summary(test_results, whl_error).
    8. (Optional) Generate documentation via exec_gen_doc().
  9. Understand the PHI Kernels Directory Organization

    develop

    The paddle/phi/kernels directory manages how operator logic is implemented across different backends:

    • Root kernels/: Contains device-independent kernel declarations (kernel.h) and implementations (kernel.cc). If a kernel uses the Primitive API or reuses other kernels, it lives here.
    • Device Subdirectories (cpu/, gpu/, xpu/, etc.): Contains the actual implementation for that specific backend. Note that gpu is used as a unified name for both cuda and hip to reduce code repetition.
    • kernels/funcs/: Contains functors and functions that support multiple backends (legacy compatibility with original fluid operators).
    • kernels/primitive/: Contains the Kernel Primitive API, which provides basic tools for unified multi-device kernel implementation.
    • kernels/impl/: A special directory for kernel implementations that are shared between CPU and GPU but are not strictly device-independent. These are stored as header files with the xxx_kernel_impl.h suffix.

    Implementation Details:

    • Backward Kernels: Implementation of backward (gradient) kernels is placed in separate files with the *_grad_kernel.* suffix to facilitate split compilation (e.g., for inference-only builds).
    • Auxiliary Functions: Functions used only by a specific kernel should be placed in the same backend folder as the kernel implementation.
    paddle/phi/kernels
    ├── (Device-independent declarations/implementations)
    ├── cpu
    ├── gpu
    ├── xpu
    ├── onednn
    ├── gpudnn
    ├── impl (Shared CPU/GPU implementations, e.g., scale_kernel_impl.h)
    ├── funcs (Multi-device functors/functions)
    └── primitive (Kernel Primitive API)
  10. Core Concepts of CINN/DSL

    develop

    The CINN/DSL (Domain Specific Language) is designed to represent computations in a hardware-independent way. It is built around several key abstractions:

    • Object: The base for all mutable elements in CINN.
    • Shared: Reference-counted containers similar to std::shared_ptr. When passing a Shared object, pass it by pointer, and the consumer should store it in a local Shared member variable.
    • Tensor: Represents input or temporary output nodes. Every Compute operation outputs a Tensor, which supports slicing.
    • PlaceHolder: A special type of Tensor that represents an input slot with a defined shape.
    • Operation: Operations performed on tensors, which include placeholder, compute, and bound inference.
    • Schedule: A mechanism to determine the order of computation (via topological sorting of the computational graph) and to transform computations.
  11. Key features of PaddlePaddle Next-Generation Framework 3.2

    develop

    The PaddlePaddle 3.2 framework introduces several advanced capabilities designed for large-scale model development and scientific computing:

    • Unified Dynamic/Static Execution with Automatic Parallelism: Automatically finds efficient distributed parallel strategies with minimal tensor splitting annotations, reducing the cost of industrial development and training.
    • Unified Training and Inference for Large Models: Supports both training and inference within the same framework, enabling code reuse and seamless transitions for the entire large model lifecycle.
    • High-Order Differentiation for Scientific Computing: Provides high-order automatic differentiation, complex number arithmetic, Fourier transforms, and compilation optimizations to accelerate differential equation solving in fields like mathematics, mechanics, and biology.
    • Neural Network Compiler: Uses an integrated framework design to balance computational flexibility and high performance for generative and scientific models, lowering the cost of performance optimization.
    • Heterogeneous Multi-core Adaptation: Provides a standardized interface to shield developers from the differences in software stacks across various hardware chips, enabling a plug-and-play architecture.
  12. JIT Kernel directory structure

    develop

    The JIT kernel implementation is organized into three main functional directories under paddle/phi/kernels/funcs/jit/:

    • gen/: Contains code generated via JIT (using the xbyak library). This directory focuses on maximum performance.
    • refer/: Contains the reference implementation. Every kernel MUST have a reference implementation here to serve as a baseline for correctness and unit testing.
    • more/: Contains additional optimized implementations, such as those using mkl, mkldnn, openblas, or custom intrinsic sets. It can also contain combinations of existing kernels.
    PaddlePaddle/Paddle/paddle/phi/kernels/
    ├── ...
    └── funcs/
        ├── .../
        └── jit/
            ├── ...
            ├── gen/
            │   └── ...
            ├── more/
            │   ├── mkl/
            │   ├── mkldnn/
            │   ├── intrinsic/
            │   └── openblas/
            └── refer/
                └── ...