# google-ai-edge/ml-drift architecture

> Architecture diagram of the GitHub repository google-ai-edge/ml-drift, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/google-ai-edge/ml-drift
- Repository: https://github.com/google-ai-edge/ml-drift

## Overview

ML Drift is a cross-platform on-device GPU inference engine. The principal workflow is to represent a model as a graph, build a backend-neutral GpuModel of GPU operations, then execute it through a selected GPU backend using device-specific kernels and managed tensor storage. The README documents graph optimization, runtime shader generation, memory reuse, and four backends; the source index confirms key model, operation, and backend modules. Some backend internals and wiring were not excerpted, so those links are shown only where documented at a high level.

Main entry points: [`ml_drift/common/gpu_model.h`](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/common/gpu_model.h) (GPU model), [`ml_drift/common/gpu_model_builder.h`](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/common/gpu_model_builder.h) (GPU model builder), [`ml_drift/common/task/gpu_operation.h`](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/common/task/gpu_operation.h) (GPU operations), [`ml_drift/cl/inference_context.cc`](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/cl/inference_context.cc) (OpenCL inference)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_model["Model and graph"]
  node_public["Public API<br/>[ml_drift.h]"]
  node_irgraph["Model graph<br/>[model.h]"]
  node_transform["Graph transforms"]
  node_builder["GPU model builder"]
  node_gpumodel["GPU model<br/>[gpu_model.h]"]
end

subgraph group_compute["GPU compute"]
  node_operation["GPU operations<br/>[gpu_operation.h]"]
  node_kernels["Operator kernels"]
  node_tensor["Tensor descriptors<br/>[tensor_desc.h]"]
  node_ucl["Unified compute language"]
end

subgraph group_cl["OpenCL backend"]
  node_clruntime["OpenCL inference"]
  node_clenv["OpenCL environment<br/>[environment.h]"]
  node_clmemory["OpenCL memory<br/>[memory_manager.cc]"]
end

subgraph group_gl["OpenGL backend"]
  node_glruntime["OpenGL inference"]
  node_glmemory["OpenGL memory<br/>[memory_manager.cc]"]
end

subgraph group_webgpu["WebGPU backend"]
  node_webgpuruntime["WebGPU inference"]
  node_webgpuenv["WebGPU environment<br/>[environment.h]"]
  node_webgpumemory["WebGPU memory<br/>[memory_manager.cc]"]
end

subgraph group_metal["Metal backend"]
  node_metalruntime["Metal inference"]
  node_metalmemory["Metal memory<br/>[memory_manager.h]"]
end

node_developer(("Application developer"))
node_device(("GPU device"))

node_developer -->|"uses"| node_public
node_public -->|"defines models"| node_irgraph
node_irgraph -->|"optimizes graph"| node_transform
node_builder -.->|"builds from"| node_irgraph
node_builder -->|"creates"| node_gpumodel
node_gpumodel -->|"contains operations"| node_operation
node_builder -.->|"selects implementations"| node_kernels
node_operation -->|"uses shader language"| node_ucl
node_operation -->|"describes tensors"| node_tensor
node_device -.->|"provides GPU"| node_clenv
node_device -.->|"provides GPU"| node_glruntime
node_device -.->|"provides GPU"| node_webgpuenv
node_device -.->|"provides GPU"| node_metalruntime
node_gpumodel -->|"executes on"| node_clruntime
node_gpumodel -->|"executes on"| node_glruntime
node_gpumodel -->|"executes on"| node_webgpuruntime
node_gpumodel -->|"executes on"| node_metalruntime
node_clruntime -->|"uses environment"| node_clenv
node_clruntime -->|"allocates tensors"| node_clmemory
node_glruntime -.->|"allocates tensors"| node_glmemory
node_webgpuruntime -.->|"uses environment"| node_webgpuenv
node_webgpuruntime -.->|"allocates tensors"| node_webgpumemory
node_metalruntime -.->|"allocates tensors"| node_metalmemory

click node_public "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/include/ml_drift.h"
click node_irgraph "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/common/model.h"
click node_transform "https://github.com/google-ai-edge/ml-drift/tree/main/ml_drift/common/transformations"
click node_builder "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/common/gpu_model_builder.h"
click node_gpumodel "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/common/gpu_model.h"
click node_operation "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/common/task/gpu_operation.h"
click node_kernels "https://github.com/google-ai-edge/ml-drift/tree/main/ml_drift/common/kernels"
click node_tensor "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/common/task/tensor_desc.h"
click node_clruntime "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/cl/inference_context.cc"
click node_clenv "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/cl/environment.h"
click node_clmemory "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/cl/memory_manager.cc"
click node_glruntime "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/gl/gl_inference_context.cc"
click node_glmemory "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/gl/memory_manager.cc"
click node_webgpuruntime "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/webgpu/inference_context.h"
click node_webgpuenv "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/webgpu/environment.h"
click node_webgpumemory "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/webgpu/memory_manager.cc"
click node_metalruntime "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/metal/inference_context.h"
click node_metalmemory "https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/metal/memory_manager.h"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_public,node_irgraph,node_transform,node_builder,node_gpumodel toneBlue
class node_operation,node_kernels,node_tensor,node_ucl toneAmber
class node_clruntime,node_clenv,node_clmemory toneMint
class node_glruntime,node_glmemory toneRose
class node_webgpuruntime,node_webgpuenv,node_webgpumemory,node_developer,node_device toneIndigo
class node_metalruntime,node_metalmemory toneTeal
```

## Components

### Model and graph

- [Public API](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/include/ml_drift.h): `ml_drift/include/ml_drift.h`
- [Model graph](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/common/model.h): `ml_drift/common/model.h`
- [Graph transforms](https://github.com/google-ai-edge/ml-drift/tree/main/ml_drift/common/transformations): `ml_drift/common/transformations`
- [GPU model builder](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/common/gpu_model_builder.h): `ml_drift/common/gpu_model_builder.h`
- [GPU model](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/common/gpu_model.h): `ml_drift/common/gpu_model.h`

### GPU compute

- [GPU operations](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/common/task/gpu_operation.h): `ml_drift/common/task/gpu_operation.h`
- [Operator kernels](https://github.com/google-ai-edge/ml-drift/tree/main/ml_drift/common/kernels): `ml_drift/common/kernels`
- [Tensor descriptors](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/common/task/tensor_desc.h): `ml_drift/common/task/tensor_desc.h`
- **Unified compute language**

### OpenCL backend

- [OpenCL inference](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/cl/inference_context.cc): `ml_drift/cl/inference_context.cc`
- [OpenCL environment](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/cl/environment.h): `ml_drift/cl/environment.h`
- [OpenCL memory](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/cl/memory_manager.cc): `ml_drift/cl/memory_manager.cc`

### OpenGL backend

- [OpenGL inference](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/gl/gl_inference_context.cc): `ml_drift/gl/gl_inference_context.cc`
- [OpenGL memory](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/gl/memory_manager.cc): `ml_drift/gl/memory_manager.cc`

### WebGPU backend

- [WebGPU inference](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/webgpu/inference_context.h): `ml_drift/webgpu/inference_context.h`
- [WebGPU environment](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/webgpu/environment.h): `ml_drift/webgpu/environment.h`
- [WebGPU memory](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/webgpu/memory_manager.cc): `ml_drift/webgpu/memory_manager.cc`

### Metal backend

- [Metal inference](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/metal/inference_context.h): `ml_drift/metal/inference_context.h`
- [Metal memory](https://github.com/google-ai-edge/ml-drift/blob/main/ml_drift/metal/memory_manager.h): `ml_drift/metal/memory_manager.h`

### Other components

- **Application developer**
- **GPU device**

## Connections

- Application developer → Public API: uses (evidence: `ml_drift/include/ml_drift.h`)
- Public API → Model graph: defines models (evidence: `ml_drift/include/ml_drift.h`)
- Model graph → Graph transforms: optimizes graph (evidence: `README.md`)
- GPU model builder → Model graph: builds from (evidence: `ml_drift/common/gpu_model_builder.h`)
- GPU model builder → GPU model: creates (evidence: `ml_drift/common/gpu_model_builder.h`)
- GPU model → GPU operations: contains operations (evidence: `ml_drift/common/gpu_model.h`)
- GPU model builder → Operator kernels: selects implementations (evidence: `ml_drift/common/gpu_model_builder.h`)
- GPU operations → Unified compute language: uses shader language (evidence: `README.md`)
- GPU operations → Tensor descriptors: describes tensors (evidence: `ml_drift/common/task/gpu_operation.h`)
- GPU device → OpenCL environment: provides GPU
- GPU device → OpenGL inference: provides GPU
- GPU device → WebGPU environment: provides GPU
- GPU device → Metal inference: provides GPU
- GPU model → OpenCL inference: executes on (evidence: `README.md`)
- GPU model → OpenGL inference: executes on (evidence: `README.md`)
- GPU model → WebGPU inference: executes on (evidence: `README.md`)
- GPU model → Metal inference: executes on (evidence: `README.md`)
- OpenCL inference → OpenCL environment: uses environment (evidence: `ml_drift/cl/inference_context.cc`)
- OpenCL inference → OpenCL memory: allocates tensors (evidence: `ml_drift/cl/inference_context.cc`)
- OpenGL inference → OpenGL memory: allocates tensors
- WebGPU inference → WebGPU environment: uses environment
- WebGPU inference → WebGPU memory: allocates tensors
- Metal inference → Metal memory: allocates tensors (evidence: `ml_drift/metal/inference_context.h`)
