# google/sentencepiece architecture

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

- Interactive diagram: https://gitdiagram.com/google/sentencepiece
- Repository: https://github.com/google/sentencepiece

## Overview

##### Main Components

a. Core Processing Components:
- Tokenizer Engine
- Detokenizer Engine
- Model Trainer
- Processor Interface
- Normalization System

b. Model Types:
- BPE Model
- Unigram Model
- Character Model
- Word Model

c. External Interfaces:
- Python Bindings
- Command-line Interface
- C++ API

**[Python API] --- [Command Line Interface] --- [C++ API]**

**[SentencePiece Processor]**

**[Model Interface]**

**[Normalizer] --- [Trainer Interface]**

**[BPE Model] --- [Unigram Model] --- [Char Model] --- [Word Model]**

##### Key Elements to Highlight

- Input text → Normalizer → Model → Tokenized output
- Training data → Trainer → Model creation

- Blue: Core processing components
- Green: Model implementations
- Orange: External interfaces
- Gray: Support utilities

##### Component Details

**Processor {**
- Encode/Decode
- Sample/NBest generation
- Vocabulary management
**}**

**Model Interface {**
- Model loading/saving
- Tokenization rules
- Vocabulary mapping
**}**

**Trainer {**
- Vocabulary size control
- Character coverage
- Model type selection
**}**

##### Supporting Systems

Connections to:
- Protocol Buffers (for model serialization)
- Unicode processing
- File system interactions

##### Integration Points

Interfaces for:
- External tokenizers
- Custom normalization rules
- Model export/import
- Vocabulary management

Key Relationships to Emphasize:
- The modular nature of the model types
- The separation between training and inference paths
- The unified interface for different model types
- The integration between C++ core and Python bindings

This architecture should clearly show how SentencePiece achieves its key features: language independence, multiple subword algorithms, and direct vocabulary ID generation, while maintaining high performance and a small memory footprint.

## Diagram (Mermaid)

```mermaid
graph TB
    %% External Interfaces Layer
    subgraph Interfaces
        PY["Python API"]:::interface
        CLI["Command Line Interface"]:::interface
        CAPI["C++ API"]:::interface
    end

    %% Core Processing Layer
    subgraph Core
        PROC["Processor Interface"]:::core
        NORM["Normalization System"]:::core
        MI["Model Interface"]:::core
        TI["Trainer Interface"]:::core
    end

    %% Model Types Layer
    subgraph Models
        BPE["BPE Model"]:::model
        UNI["Unigram Model"]:::model
        CHAR["Character Model"]:::model
        WORD["Word Model"]:::model
    end

    %% Supporting Systems
    subgraph Support
        PROTO["Protocol Buffers"]:::support
        UNICODE["Unicode Processing"]:::support
        MF["Model Factory"]:::support
        TF["Trainer Factory"]:::support
    end

    %% Relationships
    PY --> PROC
    CLI --> PROC
    CAPI --> PROC
    
    PROC --> MI
    PROC --> NORM
    
    MI --> MF
    TI --> TF
    
    MF --> BPE
    MF --> UNI
    MF --> CHAR
    MF --> WORD
    
    TF --> BPE
    TF --> UNI
    TF --> CHAR
    TF --> WORD
    
    MI --> PROTO
    NORM --> UNICODE

    %% Styles
    classDef interface fill:#FFA500,stroke:#333,stroke-width:2px
    classDef core fill:#3498DB,stroke:#333,stroke-width:2px
    classDef model fill:#2ECC71,stroke:#333,stroke-width:2px
    classDef support fill:#95A5A6,stroke:#333,stroke-width:2px

    %% Click Events
    click PROC "https://github.com/google/sentencepiece/blob/master/src/sentencepiece_processor.h"
    click NORM "https://github.com/google/sentencepiece/blob/master/src/normalizer.h"
    click MI "https://github.com/google/sentencepiece/blob/master/src/model_interface.h"
    click TI "https://github.com/google/sentencepiece/blob/master/src/trainer_interface.h"
    click BPE "https://github.com/google/sentencepiece/blob/master/src/bpe_model.h"
    click UNI "https://github.com/google/sentencepiece/blob/master/src/unigram_model.h"
    click CHAR "https://github.com/google/sentencepiece/blob/master/src/char_model.h"
    click WORD "https://github.com/google/sentencepiece/blob/master/src/word_model.h"
    click PY "https://github.com/google/sentencepiece/blob/master/python/src/sentencepiece/sentencepiece.i"
    click PROTO "https://github.com/google/sentencepiece/blob/master/src/sentencepiece.proto"
    click UNICODE "https://github.com/google/sentencepiece/blob/master/src/unicode_script.h"
    click MF "https://github.com/google/sentencepiece/blob/master/src/model_factory.h"
    click TF "https://github.com/google/sentencepiece/blob/master/src/trainer_factory.h"
```
