# amd/zendnn-tensorflow-plugin architecture

> Architecture diagram of the GitHub repository amd/zendnn-tensorflow-plugin, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/amd/zendnn-tensorflow-plugin
- Repository: https://github.com/amd/zendnn-tensorflow-plugin

## Overview

##### Project type and purpose
- This is an open‐source plug‐in for TensorFlow (named zentf) that integrates AMD’s ZenDNN library into TensorFlow’s CPU execution path.
- It provides custom graph optimizers, op kernels, and operator libraries to accelerate inference on AMD EPYC CPUs, and exposes user interfaces in Python, Java, and C++.

##### File structure
- Top-level configuration: WORKSPACE, Bazel BUILD files, configure scripts, LICENSEs.
- Directory “tensorflow_plugin/”: the heart of the plug-in.
- python/ – Python wrapper and unit test.
- src/amd_cpu/graph – Graph-level transforms and optimizer passes (auto‐mixed precision, remapper, CPU optimizer, graph_view abstractions).
- src/amd_cpu/kernels/zendnn – Implementation of individual kernel functors using ZenDNN microkernels.
- src/amd_cpu/util – Utilities (allocators, coding, proto serialization, logging, tensor formats, etc.).
- BUILD and .bzl files – Bazel build rules tying it all together.
- scripts/ – Convenience scripts for setting up env, building Java and C++ interfaces.
- examples/ – Sample code in C++, Java, and Python demonstrating how to invoke the plugin.
- tests/ – Functional tests invoking individual ops (softmax, conv2d, matmul, etc.).
- third_party/ – Vendored dependencies (Abseil, Eigen3, ZenDNN core, Protobuf, zlib, LLVM OpenMP).

##### README for additional insights
- Confirms support matrix (TensorFlow ≥2.16), build tools (Bazel 5.3–6.5), Python ≥3.9.
- Describes three user‐facing APIs: Python, Java, C++.
- Outlines build/installation from source: configure → Bazel build → build_pip_package → pip install.

##### Elements
- Blue for TensorFlow core components
- Green for zentf plug-in submodules
- Orange for ZenDNN and third-party libs
- Gray for build system

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User Interface Layer
    subgraph "User Interfaces"
        direction TB
        PythonInit["Python API (__init__.py)"]:::ui
        PythonTest["Python Tests (test.py)"]:::ui
        CppExample["C++ Example (sample_inference.cpp)"]:::ui
        CppScript["C++ Setup Script (zentf_cc_api_setup.sh)"]:::ui
        JavaExample["Java Example (RunWideDeeplarge.java)"]:::ui
        JavaScript["Java Build Script (build_tf_java.sh)"]:::ui
    end

    %% TensorFlow Core
    TensorFlowCore["TensorFlow Core\n(Graph Construction & Execution)"]:::tf

    %% zentf Plugin
    subgraph "zentf Plugin" 
        direction TB
        subgraph "Graph Optimizer Passes" 
            direction TB
            autoMixedPrecision["auto_mixed_precision"]:::plugin
            remapper["remapper"]:::plugin
            cpuOptimizer["cpu_optimizer.cc/.h"]:::plugin
            graphView["graph_view abstractions"]:::plugin
            graphUtils["graph/utils"]:::plugin
            zendnnGraph["zendnn transforms"]:::plugin
        end
        subgraph "Op Kernels"
            direction TB
            KernelFunctors["Custom Kernel Functors"]:::plugin
        end
        subgraph "Ops"
            direction TB
            OpRegistration["Op Registration & Shape-Inference"]:::plugin
        end
        UtilLib["Utility Library"]:::plugin
    end

    %% ZenDNN Library
    ZenDNNCore["ZenDNN Core Library"]:::zen

    %% Third-Party Dependencies
    subgraph "Third-Party Dependencies"
        direction TB
        Abseil["Abseil"]:::third
        Eigen3["Eigen3"]:::third
        Protobuf["Protobuf"]:::third
        OpenMP["OpenMP"]:::third
        Zlib["zlib"]:::third
    end

    %% Build System
    subgraph "Build & Packaging"
        direction TB
        BazelWorkspace["Bazel Build Configuration"]:::build
        PipPackage["Pip Packaging Tools"]:::build
        EnvSetup["Env Setup Script"]:::build
        SetupScript["Build Setup Script"]:::build
        FunctionalTests["Functional Tests"]:::build
    end

    %% Runtime Flow
    PythonInit -->|"calls"| TensorFlowCore
    PythonTest --> TensorFlowCore
    CppExample --> TensorFlowCore
    JavaExample --> TensorFlowCore
    TensorFlowCore -->|"Graph rewrite"| autoMixedPrecision
    autoMixedPrecision --> remapper --> cpuOptimizer
    cpuOptimizer --> zendnnGraph
    zendnnGraph -->|"uses"| graphView
    zendnnGraph --> graphUtils
    TensorFlowCore -->|"Op dispatch"| KernelFunctors
    KernelFunctors --> OpRegistration
    KernelFunctors --> UtilLib
    KernelFunctors -->|"calls Microkernels"| ZenDNNCore
    UtilLib --> Abseil
    UtilLib --> Protobuf
    KernelFunctors --> OpenMP
    OpRegistration --> Eigen3
    OpRegistration --> Zlib

    %% Build Flow
    BazelWorkspace --> PipPackage
    BazelWorkspace --> PythonInit
    BazelWorkspace --> CppScript
    BazelWorkspace --> JavaScript
    EnvSetup --> BazelWorkspace
    SetupScript --> BazelWorkspace
    FunctionalTests --> KernelFunctors

    %% Click Events
    click PythonInit "https://github.com/amd/zendnn-tensorflow-plugin/blob/main/tensorflow_plugin/python/__init__.py"
    click PythonTest "https://github.com/amd/zendnn-tensorflow-plugin/blob/main/tensorflow_plugin/python/test.py"
    click CppExample "https://github.com/amd/zendnn-tensorflow-plugin/blob/main/examples/c++/sample_inference.cpp"
    click CppScript "https://github.com/amd/zendnn-tensorflow-plugin/blob/main/scripts/c++/zentf_cc_api_setup.sh"
    click JavaExample "https://github.com/amd/zendnn-tensorflow-plugin/blob/main/examples/java/src/main/java/org/tensorflow/benchmark/RunWideDeeplarge.java"
    click JavaScript "https://github.com/amd/zendnn-tensorflow-plugin/blob/main/scripts/java/build_tf_java.sh"
    click autoMixedPrecision "https://github.com/amd/zendnn-tensorflow-plugin/tree/main/tensorflow_plugin/src/amd_cpu/graph/auto_mixed_precision/"
    click remapper "https://github.com/amd/zendnn-tensorflow-plugin/tree/main/tensorflow_plugin/src/amd_cpu/graph/remapper/"
    click cpuOptimizer "https://github.com/amd/zendnn-tensorflow-plugin/blob/main/tensorflow_plugin/src/amd_cpu/graph/cpu_optimizer.cc"
    click graphView "https://github.com/amd/zendnn-tensorflow-plugin/tree/main/tensorflow_plugin/src/amd_cpu/graph/graph_view/"
    click graphUtils "https://github.com/amd/zendnn-tensorflow-plugin/tree/main/tensorflow_plugin/src/amd_cpu/graph/utils/"
    click zendnnGraph "https://github.com/amd/zendnn-tensorflow-plugin/tree/main/tensorflow_plugin/src/amd_cpu/graph/zendnn/"
    click KernelFunctors "https://github.com/amd/zendnn-tensorflow-plugin/tree/main/tensorflow_plugin/src/amd_cpu/kernels/zendnn/"
    click OpRegistration "https://github.com/amd/zendnn-tensorflow-plugin/tree/main/tensorflow_plugin/src/amd_cpu/ops/zendnn/"
    click UtilLib "https://github.com/amd/zendnn-tensorflow-plugin/tree/main/tensorflow_plugin/src/amd_cpu/util/"
    click ZenDNNCore "https://github.com/amd/zendnn-tensorflow-plugin/tree/main/third_party/zen_dnn/"
    click BazelWorkspace "https://github.com/amd/zendnn-tensorflow-plugin/tree/main/WORKSPACE"
    click PipPackage "https://github.com/amd/zendnn-tensorflow-plugin/tree/main/tensorflow_plugin/tools/pip_package/"
    click FunctionalTests "https://github.com/amd/zendnn-tensorflow-plugin/tree/main/tests/"
    click EnvSetup "https://github.com/amd/zendnn-tensorflow-plugin/blob/main/scripts/zentf_env_setup.sh"
    click SetupScript "https://github.com/amd/zendnn-tensorflow-plugin/blob/main/scripts/zentf_setup.sh"

    %% Styles
    classDef ui fill:#ccffcc,stroke:#33aa33;
    classDef tf fill:#cce5ff,stroke:#3399ff;
    classDef plugin fill:#e6ffe6,stroke:#33cc33;
    classDef zen fill:#ffe6cc,stroke:#ff9933;
    classDef third fill:#ffe6cc,stroke:#ff6600;
    classDef build fill:#eeeeee,stroke:#888888;
```
