# deepseek-ai/deepselect architecture

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

- Interactive diagram: https://gitdiagram.com/deepseek-ai/deepselect
- Repository: https://github.com/deepseek-ai/deepselect

## Overview

DeepSelect is a PyTorch TopK operator for sparse-attention indexing and sampling, with CUDA and Ascend backends. The user calls the Python interface with a tensor and selection options; the native API validates tensor constraints, builds shared arguments, and dispatches supported dtype/platform cases to a specialized kernel, which produces values and/or indices. Major branches include CUDA bfloat16, its cluster variant, CUDA float32, and Ascend bfloat16/float32. Edges to kernel implementations follow explicit API dispatch or README capability statements; some internal launch details are omitted because the sampled excerpts do not show them.

Main entry points: [`csrc/api.cpp`](https://github.com/deepseek-ai/deepselect/blob/main/csrc/api.cpp) (Tensor validation and dispatch), [`csrc/cuda_kernels/v3`](https://github.com/deepseek-ai/deepselect/tree/main/csrc/cuda_kernels/v3) (CUDA bfloat16 TopK), [`csrc/cuda_kernels/v3_cluster`](https://github.com/deepseek-ai/deepselect/tree/main/csrc/cuda_kernels/v3_cluster) (CUDA cluster TopK), [`csrc/cuda_kernels/v3_fp32`](https://github.com/deepseek-ai/deepselect/tree/main/csrc/cuda_kernels/v3_fp32) (CUDA float32 TopK)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interface["Public interface"]
  node_python_api["Python TopK API<br/>[interface.py]"]
end

subgraph group_native["Native dispatch"]
  node_native_api["Tensor validation and dispatch<br/>[api.cpp]"]
end

subgraph group_cuda["CUDA kernels"]
  node_cuda_bf16["CUDA bfloat16 TopK"]
  node_cuda_cluster["CUDA cluster TopK"]
  node_cuda_fp32["CUDA float32 TopK"]
end

subgraph group_ascend["Ascend kernels"]
  node_ascend_bf16["Ascend bfloat16 TopK<br/>[kernel_body.h]"]
  node_ascend_fp32["Ascend float32 TopK<br/>[kernel_body.h]"]
end

subgraph group_support["Shared support"]
  node_tensor_helpers["Tensor checks and pointers<br/>[torch_tensors.h]"]
  node_kernel_args["Kernel argument contract<br/>[structs.h]"]
  node_cuda_config["CUDA configuration<br/>[config.h]"]
end

node_user(("PyTorch caller"))
node_cuda_runtime(("CUDA device"))
node_ascend_runtime(("Ascend NPU"))

node_user -->|"calls"| node_python_api
node_python_api -.->|"invokes backend"| node_native_api
node_native_api -->|"checks tensors"| node_tensor_helpers
node_native_api -->|"builds arguments"| node_kernel_args
node_native_api -->|"dispatches"| node_cuda_cluster
node_native_api -->|"dispatches"| node_ascend_bf16
node_native_api -->|"dispatches"| node_ascend_fp32
node_native_api -->|"selects kernel"| node_cuda_bf16
node_native_api -->|"selects kernel"| node_cuda_fp32
node_cuda_bf16 -.->|"uses parameters"| node_cuda_config
node_cuda_cluster -.->|"uses parameters"| node_cuda_config
node_cuda_fp32 -.->|"uses parameters"| node_cuda_config
node_cuda_bf16 -.->|"runs on"| node_cuda_runtime
node_cuda_cluster -.->|"runs on"| node_cuda_runtime
node_cuda_fp32 -.->|"runs on"| node_cuda_runtime
node_ascend_bf16 -.->|"runs on"| node_ascend_runtime
node_ascend_fp32 -.->|"runs on"| node_ascend_runtime

click node_python_api "https://github.com/deepseek-ai/deepselect/blob/main/deep_select/interface.py"
click node_native_api "https://github.com/deepseek-ai/deepselect/blob/main/csrc/api.cpp"
click node_tensor_helpers "https://github.com/deepseek-ai/deepselect/blob/main/csrc/3rdparty/kerutils/include/kerutils/supplemental/torch_tensors.h"
click node_kernel_args "https://github.com/deepseek-ai/deepselect/blob/main/csrc/structs.h"
click node_cuda_config "https://github.com/deepseek-ai/deepselect/blob/main/csrc/cuda_kernels/config.h"
click node_cuda_bf16 "https://github.com/deepseek-ai/deepselect/tree/main/csrc/cuda_kernels/v3"
click node_cuda_cluster "https://github.com/deepseek-ai/deepselect/tree/main/csrc/cuda_kernels/v3_cluster"
click node_cuda_fp32 "https://github.com/deepseek-ai/deepselect/tree/main/csrc/cuda_kernels/v3_fp32"
click node_ascend_bf16 "https://github.com/deepseek-ai/deepselect/blob/main/csrc/ascend_kernels/bf16/kernel_body.h"
click node_ascend_fp32 "https://github.com/deepseek-ai/deepselect/blob/main/csrc/ascend_kernels/fp32/kernel_body.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_python_api toneBlue
class node_native_api toneAmber
class node_cuda_bf16,node_cuda_cluster,node_cuda_fp32 toneMint
class node_ascend_bf16,node_ascend_fp32 toneRose
class node_tensor_helpers,node_kernel_args,node_cuda_config,node_user,node_cuda_runtime,node_ascend_runtime toneIndigo
```

## Components

### Public interface

- [Python TopK API](https://github.com/deepseek-ai/deepselect/blob/main/deep_select/interface.py): `deep_select/interface.py`

### Native dispatch

- [Tensor validation and dispatch](https://github.com/deepseek-ai/deepselect/blob/main/csrc/api.cpp): `csrc/api.cpp`

### CUDA kernels

- [CUDA bfloat16 TopK](https://github.com/deepseek-ai/deepselect/tree/main/csrc/cuda_kernels/v3): `csrc/cuda_kernels/v3`
- [CUDA cluster TopK](https://github.com/deepseek-ai/deepselect/tree/main/csrc/cuda_kernels/v3_cluster): `csrc/cuda_kernels/v3_cluster`
- [CUDA float32 TopK](https://github.com/deepseek-ai/deepselect/tree/main/csrc/cuda_kernels/v3_fp32): `csrc/cuda_kernels/v3_fp32`

### Ascend kernels

- [Ascend bfloat16 TopK](https://github.com/deepseek-ai/deepselect/blob/main/csrc/ascend_kernels/bf16/kernel_body.h): `csrc/ascend_kernels/bf16/kernel_body.h`
- [Ascend float32 TopK](https://github.com/deepseek-ai/deepselect/blob/main/csrc/ascend_kernels/fp32/kernel_body.h): `csrc/ascend_kernels/fp32/kernel_body.h`

### Shared support

- [Tensor checks and pointers](https://github.com/deepseek-ai/deepselect/blob/main/csrc/3rdparty/kerutils/include/kerutils/supplemental/torch_tensors.h): `csrc/3rdparty/kerutils/include/kerutils/supplemental/torch_tensors.h`
- [Kernel argument contract](https://github.com/deepseek-ai/deepselect/blob/main/csrc/structs.h): `csrc/structs.h`
- [CUDA configuration](https://github.com/deepseek-ai/deepselect/blob/main/csrc/cuda_kernels/config.h): `csrc/cuda_kernels/config.h`

### Other components

- **PyTorch caller**
- **CUDA device**
- **Ascend NPU**

## Connections

- PyTorch caller → Python TopK API: calls (evidence: `README.md`)
- Python TopK API → Tensor validation and dispatch: invokes backend
- Tensor validation and dispatch → Tensor checks and pointers: checks tensors (evidence: `csrc/api.cpp`)
- Tensor validation and dispatch → Kernel argument contract: builds arguments (evidence: `csrc/api.cpp`)
- Tensor validation and dispatch → CUDA cluster TopK: dispatches (evidence: `csrc/api.cpp`)
- Tensor validation and dispatch → Ascend bfloat16 TopK: dispatches (evidence: `csrc/api.cpp`)
- Tensor validation and dispatch → Ascend float32 TopK: dispatches (evidence: `csrc/api.cpp`)
- Tensor validation and dispatch → CUDA bfloat16 TopK: selects kernel (evidence: `csrc/api.cpp`)
- Tensor validation and dispatch → CUDA float32 TopK: selects kernel (evidence: `csrc/api.cpp`)
- CUDA bfloat16 TopK → CUDA configuration: uses parameters
- CUDA cluster TopK → CUDA configuration: uses parameters
- CUDA float32 TopK → CUDA configuration: uses parameters
- CUDA bfloat16 TopK → CUDA device: runs on
- CUDA cluster TopK → CUDA device: runs on
- CUDA float32 TopK → CUDA device: runs on
- Ascend bfloat16 TopK → Ascend NPU: runs on
- Ascend float32 TopK → Ascend NPU: runs on
