# princeton-nlp/cofipruning architecture

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

- Interactive diagram: https://gitdiagram.com/princeton-nlp/cofipruning
- Repository: https://github.com/princeton-nlp/cofipruning

## Overview

CoFiPruning trains and evaluates task-specific compact BERT/RoBERTa models for GLUE classification and extractive question answering. The main workflow loads task data and a pretrained model, learns structured pruning masks (optionally with teacher distillation), trains through CoFi trainers, and evaluates or saves the resulting model. Separate utilities materialize masks as smaller model structures and handle QA answer post-processing. The README documents the supported pruning units, distillation, and post-pruning fine-tuning; some links below rely on that documentation or the source index because the relevant implementation excerpts are incomplete.

Main entry points: [`run_glue_prune.py`](https://github.com/princeton-nlp/cofipruning/blob/main/run_glue_prune.py) (GLUE runner), [`run_qa_prune.py`](https://github.com/princeton-nlp/cofipruning/blob/main/run_qa_prune.py) (QA runner), [`utils/cofi_utils.py`](https://github.com/princeton-nlp/cofipruning/blob/main/utils/cofi_utils.py) (Model materialization), [`models/modeling_bert.py`](https://github.com/princeton-nlp/cofipruning/blob/main/models/modeling_bert.py) (CoFi BERT)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Task entry points"]
  node_glue_runner["GLUE runner<br/>[run_glue_prune.py]"]
  node_qa_runner["QA runner<br/>[run_qa_prune.py]"]
  node_args["Task arguments<br/>[args.py]"]
  node_model_args["Model arguments<br/>[model_args.py]"]
end

subgraph group_data["Task data and evaluation"]
  node_datasets["Task datasets<br/>[glue.py]"]
  node_evaluation["Evaluation<br/>[evaluation.py]"]
  node_qa_utils["QA post-processing<br/>[qa_utils.py]"]
end

subgraph group_pruning["Pruning and optimization"]
  node_l0["Mask learning<br/>[l0_module.py]"]
  node_trainer["Pruning trainer<br/>[trainer.py]"]
  node_qa_trainer["QA trainer<br/>[trainer_qa.py]"]
  node_prune_utils["Model materialization<br/>[cofi_utils.py]"]
end

subgraph group_models["Model implementations"]
  node_bert["CoFi BERT<br/>[modeling_bert.py]"]
  node_roberta["CoFi RoBERTa"]
  node_metrics_utils["Parameter utilities<br/>[utils.py]"]
end

node_user(("Researcher"))
node_hf_data[("Hugging Face datasets")]
node_teacher["Teacher model"]

node_user -->|"configures task"| node_glue_runner
node_user -->|"configures task"| node_qa_runner
node_glue_runner -->|"parses arguments"| node_args
node_qa_runner -->|"parses arguments"| node_args
node_glue_runner -->|"reads model config"| node_model_args
node_qa_runner -->|"reads model config"| node_model_args
node_glue_runner -->|"loads task data"| node_hf_data
node_qa_runner -->|"loads task data"| node_hf_data
node_glue_runner -.->|"prepares GLUE data"| node_datasets
node_glue_runner -->|"sets up masks"| node_l0
node_qa_runner -->|"sets up masks"| node_l0
node_glue_runner -->|"selects task model"| node_bert
node_glue_runner -->|"selects task model"| node_roberta
node_qa_runner -->|"loads QA model"| node_bert
node_glue_runner -.->|"trains and evaluates"| node_trainer
node_qa_runner -.->|"trains and evaluates"| node_qa_trainer
node_trainer -->|"samples masks"| node_l0
node_qa_trainer -->|"extends trainer"| node_trainer
node_qa_trainer -->|"post-processes predictions"| node_qa_utils
node_glue_runner -->|"loads for distillation"| node_teacher
node_qa_runner -.->|"loads for distillation"| node_teacher
node_glue_runner -.->|"materializes pruning"| node_prune_utils
node_qa_runner -->|"materializes pruning"| node_prune_utils
node_bert -->|"uses pruning helpers"| node_prune_utils
node_roberta -->|"reuses BERT components"| node_bert
node_prune_utils -->|"counts parameters"| node_metrics_utils
node_glue_runner -.->|"runs task evaluation"| node_evaluation
node_qa_runner -.->|"runs task evaluation"| node_evaluation

click node_glue_runner "https://github.com/princeton-nlp/cofipruning/blob/main/run_glue_prune.py"
click node_qa_runner "https://github.com/princeton-nlp/cofipruning/blob/main/run_qa_prune.py"
click node_args "https://github.com/princeton-nlp/cofipruning/blob/main/args.py"
click node_model_args "https://github.com/princeton-nlp/cofipruning/blob/main/models/model_args.py"
click node_datasets "https://github.com/princeton-nlp/cofipruning/blob/main/glue.py"
click node_evaluation "https://github.com/princeton-nlp/cofipruning/blob/main/evaluation.py"
click node_qa_utils "https://github.com/princeton-nlp/cofipruning/blob/main/utils/qa_utils.py"
click node_l0 "https://github.com/princeton-nlp/cofipruning/blob/main/models/l0_module.py"
click node_trainer "https://github.com/princeton-nlp/cofipruning/blob/main/trainer/trainer.py"
click node_qa_trainer "https://github.com/princeton-nlp/cofipruning/blob/main/trainer/trainer_qa.py"
click node_prune_utils "https://github.com/princeton-nlp/cofipruning/blob/main/utils/cofi_utils.py"
click node_bert "https://github.com/princeton-nlp/cofipruning/blob/main/models/modeling_bert.py"
click node_roberta "https://github.com/princeton-nlp/cofipruning/blob/main/models/modeling_roberta.py"
click node_metrics_utils "https://github.com/princeton-nlp/cofipruning/blob/main/utils/utils.py"

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_glue_runner,node_qa_runner,node_args,node_model_args toneBlue
class node_datasets,node_evaluation,node_qa_utils,node_hf_data toneAmber
class node_l0,node_trainer,node_qa_trainer,node_prune_utils toneMint
class node_bert,node_roberta,node_metrics_utils toneRose
class node_user,node_teacher toneIndigo
```

## Components

### Task entry points

- [GLUE runner](https://github.com/princeton-nlp/cofipruning/blob/main/run_glue_prune.py): `run_glue_prune.py`
- [QA runner](https://github.com/princeton-nlp/cofipruning/blob/main/run_qa_prune.py): `run_qa_prune.py`
- [Task arguments](https://github.com/princeton-nlp/cofipruning/blob/main/args.py): `args.py`
- [Model arguments](https://github.com/princeton-nlp/cofipruning/blob/main/models/model_args.py): `models/model_args.py`

### Task data and evaluation

- [Task datasets](https://github.com/princeton-nlp/cofipruning/blob/main/glue.py): `glue.py`
- [Evaluation](https://github.com/princeton-nlp/cofipruning/blob/main/evaluation.py): `evaluation.py`
- [QA post-processing](https://github.com/princeton-nlp/cofipruning/blob/main/utils/qa_utils.py): `utils/qa_utils.py`

### Pruning and optimization

- [Mask learning](https://github.com/princeton-nlp/cofipruning/blob/main/models/l0_module.py): `models/l0_module.py`
- [Pruning trainer](https://github.com/princeton-nlp/cofipruning/blob/main/trainer/trainer.py): `trainer/trainer.py`
- [QA trainer](https://github.com/princeton-nlp/cofipruning/blob/main/trainer/trainer_qa.py): `trainer/trainer_qa.py`
- [Model materialization](https://github.com/princeton-nlp/cofipruning/blob/main/utils/cofi_utils.py): `utils/cofi_utils.py`

### Model implementations

- [CoFi BERT](https://github.com/princeton-nlp/cofipruning/blob/main/models/modeling_bert.py): `models/modeling_bert.py`
- [CoFi RoBERTa](https://github.com/princeton-nlp/cofipruning/blob/main/models/modeling_roberta.py): `models/modeling_roberta.py`
- [Parameter utilities](https://github.com/princeton-nlp/cofipruning/blob/main/utils/utils.py): `utils/utils.py`

### Other components

- **Researcher**
- **Hugging Face datasets**
- **Teacher model**

## Connections

- Researcher → GLUE runner: configures task (evidence: `README.md`)
- Researcher → QA runner: configures task (evidence: `README.md`)
- GLUE runner → Task arguments: parses arguments (evidence: `run_glue_prune.py`)
- QA runner → Task arguments: parses arguments (evidence: `run_qa_prune.py`)
- GLUE runner → Model arguments: reads model config (evidence: `run_glue_prune.py`)
- QA runner → Model arguments: reads model config (evidence: `run_qa_prune.py`)
- GLUE runner → Hugging Face datasets: loads task data (evidence: `run_glue_prune.py`)
- QA runner → Hugging Face datasets: loads task data (evidence: `run_qa_prune.py`)
- GLUE runner → Task datasets: prepares GLUE data (evidence: `README.md`)
- GLUE runner → Mask learning: sets up masks (evidence: `run_glue_prune.py`)
- QA runner → Mask learning: sets up masks (evidence: `run_qa_prune.py`)
- GLUE runner → CoFi BERT: selects task model (evidence: `run_glue_prune.py`)
- GLUE runner → CoFi RoBERTa: selects task model (evidence: `run_glue_prune.py`)
- QA runner → CoFi BERT: loads QA model (evidence: `run_qa_prune.py`)
- GLUE runner → Pruning trainer: trains and evaluates (evidence: `README.md`)
- QA runner → QA trainer: trains and evaluates (evidence: `README.md`)
- Pruning trainer → Mask learning: samples masks (evidence: `trainer/trainer.py`)
- QA trainer → Pruning trainer: extends trainer (evidence: `trainer/trainer_qa.py`)
- QA trainer → QA post-processing: post-processes predictions (evidence: `trainer/trainer_qa.py`)
- GLUE runner → Teacher model: loads for distillation (evidence: `run_glue_prune.py`)
- QA runner → Teacher model: loads for distillation (evidence: `README.md`)
- GLUE runner → Model materialization: materializes pruning (evidence: `README.md`)
- QA runner → Model materialization: materializes pruning (evidence: `run_qa_prune.py`)
- CoFi BERT → Model materialization: uses pruning helpers (evidence: `models/modeling_bert.py`)
- CoFi RoBERTa → CoFi BERT: reuses BERT components (evidence: `models/modeling_roberta.py`)
- Model materialization → Parameter utilities: counts parameters (evidence: `utils/cofi_utils.py`)
- GLUE runner → Evaluation: runs task evaluation (evidence: `README.md`)
- QA runner → Evaluation: runs task evaluation (evidence: `README.md`)
