# nlpoptimize/flash-tokenizer architecture

> Architecture diagram of the GitHub repository nlpoptimize/flash-tokenizer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nlpoptimize/flash-tokenizer
- Repository: https://github.com/nlpoptimize/flash-tokenizer

## Overview

##### Main Components
- Core Tokenizer Engine (C++): This is the heart of the project. It includes the implementation of multiple tokenizers (BasicTokenizer, BertTokenizer variants, WordPieceTokenizer, Bidirectional variants) found in the prj/src directory and its associated headers in prj/include.
- Python Binding Layer: Under prj/python, you have the binding code (e.g., bindings.cpp, flash_tokenizer package) that wraps the C++ tokenizer into a Python module. This allows users to call the fast C++ code directly from Python.
- Configuration and Data Management: The dataset directory holds configuration files and vocabularies for various pretrained BERT models (e.g., bert-base-uncased, bert-base-multilingual-cased, KR-BERT, etc.) which are used by the tokenizer engine.
- Build and Integration Infrastructure: The CMakeLists.txt files (at the root and within prj) manage the build process of the C++ code. Additionally, there are Python setup files and a requirements.txt for handling dependencies.
- Documentation & Examples: The docs folder (with BUILD_whl.md, SETUP.md, etc.) and sample/ folder (with usage examples) provide support for installation, usage, and dissemination of the tool.

##### Relationships and Interactions
- Incoming Text Input – Component for Preprocessing: Users provide raw text (via sample scripts or through APIs). The text input is first passed into the tokenization pipeline.
- Tokenization Process – Core Engine: The text then flows to the C++ tokenizer engine. Inside, tokenizers apply different stages (as described in the README: preprocessing, inference, postprocessing) and use techniques like parallel processing using OPENMP for batch encoding.
- Python Binding – API Exposure: The C++ engine is wrapped in Python via the pybind11 binding layer. This layer exposes classes (e.g., BertTokenizerFlash) that users import from the Python package.
- Configurations and Vocab Management: The engine uses the configurations (JSON files, vocab.txt, tokenizer_config.json) from the dataset directory to correctly instantiate the appropriate tokenizer for the corresponding BERT model.
- Build/Deployment: The build system (CMake) compiles the C++ source; then, the Python packaging (using pyproject.toml in prj/python or MANIFEST.in) ensures the distribution of the resulting wheel file on multiple platforms.

##### Architectural Patterns & Design Principles
- Layered Architecture: The project cleanly separates the C++ core (performance-critical tokenization logic) from its Python bindings, allowing for efficient cross-language integration.
- Modularity: The project is split into multiple subdirectories: core logic (prj/src and prj/include), bindings (prj/python), configuration (dataset/config), and examples (sample/). This modular separation makes it easy to update or extend one part (like adding another tokenization variant) without impacting the others.
- Parallelism and Performance: By incorporating OPENMP (and even detailed notes about thread pool and SIMD optimizations), the architecture shows that performance is a design priority. This could be represented by annotating the core engine with notes on parallel batch encoding.

- Component is clearly named with labels.
- Notes next to components to mention technologies (e.g., “C++17 with OPENMP”, “pybind11 wrapper”, “CMake-based build”).

## Diagram (Mermaid)

```mermaid
graph TD
    %% Global Nodes
    rawInput["User Text Input / Application"]:::external
    configData["Configuration Files & Vocab (dataset/config)"]:::yellow
    performance["Performance Optimizations (OPENMP,C++17)"]:::purpleNote

    %% Core Tokenizer Engine (C++)
    subgraph "Core Tokenizer Engine (C++)"
        srcEngine["Tokenizer Source Code"]:::blue
        headerEngine["Tokenizer Headers"]:::blue
    end

    %% Tokenization Pipeline
    subgraph "Tokenization Pipeline"
        preproc["Preprocessing"]:::purple
        inference["Inference"]:::purple
        postproc["Postprocessing"]:::purple
    end

    %% Python Binding Layer (pybind11)
    subgraph "Python Binding Layer (pybind11)"
        bindingCPP["Bindings (bindings.cpp)"]:::green
        flashPkg["Flash Tokenizer Package"]:::green
    end

    %% Build & Deployment Infrastructure
    subgraph "Build & Deployment Infrastructure"
        rootCMake["CMakeLists.txt (root)"]:::gray
        prjCMake["CMakeLists.txt (prj)"]:::gray
        pyproject["pyproject.toml (prj/python)"]:::gray
        manifest["MANIFEST.in"]:::gray
        requirements["requirements.txt"]:::gray
    end

    %% Documentation & Examples
    subgraph "Documentation & Examples"
        docsFolder["Documentation (docs)"]:::orange
        sampleFolder["Examples (sample)"]:::orange
    end

    %% Connections
    rawInput -->|"RawTextInput"| srcEngine
    configData -->|"ProvidesConfiguration"| srcEngine
    srcEngine -->|"BeginPipeline"| preproc
    preproc -->|"Infer"| inference
    inference -->|"PostProcess"| postproc
    postproc -->|"TokenIDsOutput"| bindingCPP
    bindingCPP -->|"ExposesAPI"| flashPkg
    rootCMake -->|"Build"| srcEngine
    prjCMake -->|"Build"| srcEngine
    pyproject -->|"Package"| bindingCPP
    manifest -->|"Package"| bindingCPP
    requirements -->|"Dependencies"| bindingCPP
    docsFolder -->|"UsageDocumentation"| bindingCPP
    sampleFolder -->|"Examples"| bindingCPP
    performance --- srcEngine

    %% Styles
    classDef blue fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef green fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef yellow fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef gray fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
    classDef orange fill:#ffe5b4,stroke:#ff8c00,stroke-width:2px;
    classDef purple fill:#e6ccff,stroke:#800080,stroke-width:2px;
    classDef external fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef purpleNote fill:#f5e6ff,stroke:#800080,stroke-width:2px,stroke-dasharray: 5, 5;

    %% Click Events
    click srcEngine "https://github.com/nlpoptimize/flash-tokenizer/tree/main/prj/src"
    click headerEngine "https://github.com/nlpoptimize/flash-tokenizer/tree/main/prj/include"
    click bindingCPP "https://github.com/nlpoptimize/flash-tokenizer/blob/main/prj/python/src/bindings.cpp"
    click flashPkg "https://github.com/nlpoptimize/flash-tokenizer/tree/main/prj/python/flash_tokenizer"
    click configData "https://github.com/nlpoptimize/flash-tokenizer/tree/main/dataset/config"
    click rootCMake "https://github.com/nlpoptimize/flash-tokenizer/blob/main/CMakeLists.txt"
    click prjCMake "https://github.com/nlpoptimize/flash-tokenizer/blob/main/prj/CMakeLists.txt"
    click pyproject "https://github.com/nlpoptimize/flash-tokenizer/blob/main/prj/pyproject.toml"
    click manifest "https://github.com/nlpoptimize/flash-tokenizer/blob/main/MANIFEST.in"
    click requirements "https://github.com/nlpoptimize/flash-tokenizer/blob/main/requirements.txt"
    click docsFolder "https://github.com/nlpoptimize/flash-tokenizer/tree/main/docs"
    click sampleFolder "https://github.com/nlpoptimize/flash-tokenizer/tree/main/sample"
```
