# urchade/gliner architecture

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

- Interactive diagram: https://gitdiagram.com/urchade/gliner
- Repository: https://github.com/urchade/gliner

## Overview

##### Project Type and Purpose
- GLiNER is a specialized machine learning library focused on NER.
- It provides an efficient, lightweight alternative to large language models by leveraging transformer-based architecture and supporting multiple tasks (multi-task learning such as classification, open extraction, question answering, etc.).
- The README and file structure indicate that it supports training, evaluation, and deployment (including ONNX conversion) along with various example notebooks for quick start and finetuning.

##### File Structure for Architectural Components
- Top-level directories/files:
– configs: Contains configuration files (general and task-specific) for different parts of the pipeline.
– data: Scripts dedicated to data processing and preprocessing.
– gliner: The core package containing submodules:
○ data_processing: Manages tokenization, collating, and dataset preparation.
○ decoding: Contains utilities for turning model outputs into predictions.
○ evaluation: Contains evaluators and metrics for assessing NER performance.
○ modeling: Holds the core model architecture (encoder, layers, loss functions, scorers, etc.).
○ multitask: Provides support for other NLP tasks such as classification and question answering using the same core.
○ onnx: Dedicated to exporting the model to ONNX format for deployment efficiency.
○ training: Contains trainer modules to streamline the training process.
– Examples: Notebook examples demonstrating different workflows (finetuning, synthetic data generation, ONNX conversion).
– Demo, train.py, custom_train.py, eval.py: Standalone scripts for demonstration, training, or evaluation.

##### Main Components & Their Roles
- Data Ingestion and Preprocessing:
– Includes scripts and modules in “data” as well as gliner/data_processing (tokenizer, dataset, processor and collator).
- Core Model Architecture:
– gliner/model.py and gliner/modeling provide the base encoder (BERT-like) and associated layers, loss functions, and scoring modules.
- Task-Specific Modules (Multitask):
– Located in gliner/multitask supporting tasks like classification, question answering, open extraction, and summarization.
- Decoding and Post-Processing:
– Managed in gliner/decoding. These modules map raw model outputs into entity predictions.
- Training Pipeline:
– gliner/training/trainer.py along with training scripts like train.py and custom_train.py orchestrate model training using specified configurations.
- Evaluation:
– gliner/evaluation includes evaluators for measuring performance based on established NER benchmarks.
- Deployment / ONNX Conversion:
– convert_to_onnx.py and gliner/onnx provide functionality for converting models to an efficient deployment format.

##### Relationships and Data Flow
- Start from raw input text → Data processing: tokenization and dataset creation.
- Pass processed data into the core model (from gliner/modeling) for feature extraction and predictions.
- Use decoding modules to interpret model outputs into human-readable entities.
- Training scripts and trainer modules manage the loop, feeding data through preprocessing, model training, and handling validation through evaluation modules.
- Config files (in the “configs” folder) determine hyperparameters, model architecture details, and task-specific settings at each stage.
- Example notebooks illustrate how the components interconnect, from initialization (using GLiNER.from_pretrained) to prediction.

##### Architectural Patterns and Design Principles
- Modularity: Each aspect (data, models, training, evaluation, decoding) is cleanly separated into its own submodule, promoting reusability and ease of maintenance.
- Extensibility: The multitask directory illustrates the design for scaling and extending the base model’s functionality to other tasks.
- Config-driven behavior: Use of YAML config files allows dynamic changes to experimental settings without code modifications.
- Deployment Readiness: ONNX conversion emphasizes efficient deployment pipelines and platform compatibility.

##### Specific Elements
- Each component (Data Processing, Modeling, Training, Evaluation, Decoding, ONNX conversion).
- Their interactions with clear labels (e.g., “tokenizer → dataset creation”, “encoder → feature extraction”, “trainer → model updates”).
- Configuration management (linking the “configs” directory to all modules where hyperparameters are set).
- Example integration points demonstrating how external users can interact with the GLiNER library (via demo.py or notebooks).

## Diagram (Mermaid)

```mermaid
flowchart TD
    U("Raw Input"):::data

    subgraph "Data Ingestion & Preprocessing"
        A("Data Processing Module"):::data
        B1("Data Script: process_nuner"):::data
        B2("Data Script: process_pilener"):::data
    end

    subgraph "Core Model Architecture"
        C("Model Definition"):::model
        D("Modeling Components"):::model
    end

    subgraph "Task-Specific Modules"
        F("Task-Specific Modules"):::multitask
    end

    subgraph "Decoding & Post-Processing"
        E("Decoding Utilities"):::decoding
    end

    subgraph "Training Pipeline"
        G("Trainer Module"):::training
        H1("Train Script: train.py"):::training
        H2("Train Script: custom_train.py"):::training
    end

    subgraph "Evaluation"
        I("Evaluation Modules"):::evaluation
        J("Evaluation Script"):::evaluation
    end

    subgraph "Deployment / ONNX Conversion"
        K1("ONNX Conversion Script"):::deployment
        K2("ONNX Module"):::deployment
    end

    subgraph "Configuration Management"
        L1("Config Files"):::config
        L2("Config Utility"):::config
    end

    subgraph "Examples & Demos"
        M1("Example Notebooks"):::examples
        M2("Demo Script"):::examples
    end

    %% Connections
    U -->|"tokenization"| A
    A -->|"processed_data"| C
    C -->|"builds_model"| D
    D -->|"feature_extraction"| E
    D -->|"multitask_support"| F
    A -->|"training_input"| G
    H1 -->|"orchestrates"| G
    H2 -->|"orchestrates"| G
    G -->|"trains_model"| C
    G -->|"validation"| I
    I -->|"evaluation_results"| J
    C -->|"export_model"| K1
    C -->|"export_model"| K2
    L1 --> A
    L1 --> C
    L1 --> G
    L1 --> F
    L2 --> A
    L2 --> C
    L2 --> G
    L2 --> F
    M1 -->|"usage_examples"| C
    M2 -->|"demonstrates_deployment"| K1

    %% Click Events
    click A "https://github.com/urchade/gliner/tree/main/gliner/data_processing"
    click B1 "https://github.com/urchade/gliner/blob/main/data/process_nuner.py"
    click B2 "https://github.com/urchade/gliner/blob/main/data/process_pilener.py"
    click C "https://github.com/urchade/gliner/blob/main/gliner/model.py"
    click D "https://github.com/urchade/gliner/tree/main/gliner/modeling"
    click F "https://github.com/urchade/gliner/tree/main/gliner/multitask"
    click E "https://github.com/urchade/gliner/tree/main/gliner/decoding"
    click G "https://github.com/urchade/gliner/blob/main/gliner/training/trainer.py"
    click H1 "https://github.com/urchade/gliner/blob/main/train.py"
    click H2 "https://github.com/urchade/gliner/blob/main/custom_train.py"
    click I "https://github.com/urchade/gliner/tree/main/gliner/evaluation"
    click J "https://github.com/urchade/gliner/blob/main/eval.py"
    click K1 "https://github.com/urchade/gliner/blob/main/convert_to_onnx.py"
    click K2 "https://github.com/urchade/gliner/tree/main/gliner/onnx"
    click L1 "https://github.com/urchade/gliner/tree/main/configs/"
    click L2 "https://github.com/urchade/gliner/blob/main/gliner/config.py"
    click M1 "https://github.com/urchade/gliner/tree/main/examples/"
    click M2 "https://github.com/urchade/gliner/blob/main/demo.py"

    %% Styles
    classDef data fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef model fill:#FAD7A0,stroke:#7D6608,stroke-width:2px;
    classDef multitask fill:#D5F5E3,stroke:#196F3D,stroke-width:2px;
    classDef decoding fill:#F5B7B1,stroke:#922B21,stroke-width:2px;
    classDef training fill:#D6EAF8,stroke:#2874A6,stroke-width:2px;
    classDef evaluation fill:#FADBD8,stroke:#C0392B,stroke-width:2px;
    classDef deployment fill:#E8DAEF,stroke:#6C3483,stroke-width:2px;
    classDef config fill:#FCF3CF,stroke:#B7950B,stroke-width:2px;
    classDef examples fill:#D1F2EB,stroke:#0E6251,stroke-width:2px;
```
