geopres 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- geopres-0.1.0/.gitignore +30 -0
- geopres-0.1.0/AGENTS.md +115 -0
- geopres-0.1.0/CITATION.cff +47 -0
- geopres-0.1.0/LICENSE +21 -0
- geopres-0.1.0/PKG-INFO +383 -0
- geopres-0.1.0/README.md +320 -0
- geopres-0.1.0/analysis/average_results_multi_plot.py +280 -0
- geopres-0.1.0/analysis/calculate_intrinsic_correlations.py +90 -0
- geopres-0.1.0/analysis/compare_multilingual_retention.py +241 -0
- geopres-0.1.0/analysis/create_average_methods_plot.py +202 -0
- geopres-0.1.0/analysis/create_result_plots_main_method.py +241 -0
- geopres-0.1.0/analysis/create_scatter_plot.py +238 -0
- geopres-0.1.0/analysis/generate_extrinsic_tables_2d.py +111 -0
- geopres-0.1.0/analysis/generate_extrinsic_tables_alternative_loss_functions.py +87 -0
- geopres-0.1.0/analysis/generate_extrinsic_tables_raw_mteb.py +265 -0
- geopres-0.1.0/analysis/generate_intrinsic_tables.py +100 -0
- geopres-0.1.0/analysis/run_bootstrap.py +108 -0
- geopres-0.1.0/geopres/__init__.py +52 -0
- geopres-0.1.0/geopres/api.py +909 -0
- geopres-0.1.0/geopres/baselines/__init__.py +1 -0
- geopres-0.1.0/geopres/baselines/eval_autoencoder.py +120 -0
- geopres-0.1.0/geopres/baselines/eval_pca.py +128 -0
- geopres-0.1.0/geopres/baselines/eval_random_projection.py +111 -0
- geopres-0.1.0/geopres/baselines/eval_random_selection.py +114 -0
- geopres-0.1.0/geopres/baselines/eval_truncation.py +114 -0
- geopres-0.1.0/geopres/baselines/fit_pca.py +112 -0
- geopres-0.1.0/geopres/baselines/train_autoencoder.py +393 -0
- geopres-0.1.0/geopres/compare_results.py +165 -0
- geopres-0.1.0/geopres/config.py +79 -0
- geopres-0.1.0/geopres/encode.py +78 -0
- geopres-0.1.0/geopres/eval_backbone.py +76 -0
- geopres-0.1.0/geopres/eval_model.py +146 -0
- geopres-0.1.0/geopres/eval_utils.py +396 -0
- geopres-0.1.0/geopres/geopres_trainer.py +174 -0
- geopres-0.1.0/geopres/losses.py +237 -0
- geopres-0.1.0/geopres/precalculate_embeddings.py +242 -0
- geopres-0.1.0/geopres/projection.py +61 -0
- geopres-0.1.0/geopres/py.typed +0 -0
- geopres-0.1.0/geopres/reduced_sentence_transformer.py +136 -0
- geopres-0.1.0/geopres/train_model.py +370 -0
- geopres-0.1.0/pyproject.toml +95 -0
- geopres-0.1.0/tests/test_api.py +273 -0
- geopres-0.1.0/tests/test_hub.py +56 -0
- geopres-0.1.0/tests/test_losses.py +72 -0
- geopres-0.1.0/uv.lock +3738 -0
geopres-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
.venv/
|
|
3
|
+
**/__pycache__/
|
|
4
|
+
*.egg-info/
|
|
5
|
+
build/
|
|
6
|
+
dist/
|
|
7
|
+
.pytest_cache/
|
|
8
|
+
.ruff_cache/
|
|
9
|
+
|
|
10
|
+
# Local secrets / config (no longer read by the code, but keep them out of git)
|
|
11
|
+
.env
|
|
12
|
+
|
|
13
|
+
# Experiment artefacts
|
|
14
|
+
storage/optuna_results/
|
|
15
|
+
storage/precalculated_embeddings/
|
|
16
|
+
storage/evaluation_results/
|
|
17
|
+
storage/evaluation_results_trained_models_bad_checkpoints/
|
|
18
|
+
storage/trained_autoencoders/
|
|
19
|
+
storage/trained_models/
|
|
20
|
+
storage/pca/
|
|
21
|
+
storage/plots/
|
|
22
|
+
storage/available_tasks.txt
|
|
23
|
+
trainer_output/
|
|
24
|
+
nohup.out
|
|
25
|
+
optuna_study.db
|
|
26
|
+
|
|
27
|
+
# Scratch
|
|
28
|
+
notes.md
|
|
29
|
+
scratch/
|
|
30
|
+
.vscode/
|
geopres-0.1.0/AGENTS.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# AGENTS.md — GeoPres
|
|
2
|
+
|
|
3
|
+
GeoPres trains a linear projection that compresses high-dimensional sentence
|
|
4
|
+
embeddings while preserving pairwise Euclidean distances, then evaluates it on
|
|
5
|
+
MTEB against PCA / random projection / random selection / truncation /
|
|
6
|
+
autoencoder baselines. Single-author thesis research code.
|
|
7
|
+
|
|
8
|
+
The repository is both an installable package (`geopres/`) and the paper's
|
|
9
|
+
experiment pipeline.
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
uv sync --extra research # package + paper dependencies
|
|
15
|
+
uv run pytest # fast, CPU-only, no network
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
All artefacts go to `<repository>/storage`; the paths are defined in
|
|
19
|
+
`geopres/config.py`. There is no `.env` file to copy and no `PYTHONPATH` setup any
|
|
20
|
+
more.
|
|
21
|
+
|
|
22
|
+
## Layout
|
|
23
|
+
|
|
24
|
+
- `geopres/api.py` — public API (`GeoPres.fit` / `.embed` / `.save` /
|
|
25
|
+
`GeoPres.load_path` / `.push_to_hub` / `GeoPres.from_pretrained`, plus
|
|
26
|
+
`Projection`, the backbone-free matrix). Keep it free
|
|
27
|
+
of `mteb`, `datasets`, `torchsort` and `huggingface_hub` module-level imports:
|
|
28
|
+
`import geopres` must stay light and work with core dependencies only.
|
|
29
|
+
- `geopres/projection.py` — the linear map `W`, a bias-free `nn.Linear`, so
|
|
30
|
+
existing `best_model.pt` files load unchanged.
|
|
31
|
+
- `geopres/losses.py` — positional / angular / Spearman losses and
|
|
32
|
+
`geopres_loss`, the single place where the loss is combined. `torchsort` is
|
|
33
|
+
imported lazily inside the Spearman functions.
|
|
34
|
+
- `geopres/encode.py` — backbone loading and text encoding;
|
|
35
|
+
`sentence_transformers` is imported lazily.
|
|
36
|
+
- `geopres/geopres_trainer.py` — Hugging Face `Trainer` used by the paper
|
|
37
|
+
pipeline (checkpoints, early stopping).
|
|
38
|
+
- `geopres/train_model.py`, `precalculate_embeddings.py`, `eval_model.py`,
|
|
39
|
+
`eval_backbone.py` — CLI entry points (`geopres-train`,
|
|
40
|
+
`geopres-precalculate`, `geopres-eval`, `geopres-eval-backbone`). Each exposes
|
|
41
|
+
`main()` and also runs with `python -m`.
|
|
42
|
+
- `geopres/compare_results.py` — merges the MTEB caches into
|
|
43
|
+
`comparison_results.csv` (entry point `geopres-compare`).
|
|
44
|
+
- `geopres/baselines/` — the baseline methods.
|
|
45
|
+
- `analysis/` — paper tables, plots and bootstrap tests. Not part of the
|
|
46
|
+
package, needs the cached results in `storage/`.
|
|
47
|
+
|
|
48
|
+
## Imports
|
|
49
|
+
|
|
50
|
+
Always use absolute package imports (`from geopres.config import ...`). Never
|
|
51
|
+
rely on `PYTHONPATH` or bare imports (`from config import ...`).
|
|
52
|
+
|
|
53
|
+
## Workflow: precalculate → train → evaluate
|
|
54
|
+
|
|
55
|
+
1. **Precalculate embeddings** (one time per backbone; GPU-heavy):
|
|
56
|
+
`geopres-precalculate --model_name <hf-id> --train --val_test --batch_size 8192`.
|
|
57
|
+
Writes `$STORAGE_PATH/precalculated_embeddings/{c4|sentence-paraphrases}/<backbone>/{train,validation,test}_embeddings.pt`.
|
|
58
|
+
Encodes in chunks every `--encode_every` (default 100k), skips existing
|
|
59
|
+
chunks, then concatenates and deletes them.
|
|
60
|
+
2. **Train**:
|
|
61
|
+
`geopres-train --backbone_model ... --source_dim 512 --target_dim 32 --train_batch_size 20000 --val_batch_size 20000 --positional_loss_factor 1 --weighted_loss`.
|
|
62
|
+
Defaults: AdamW lr=1e-2, weight decay 0.1, 10 epochs, early stopping
|
|
63
|
+
patience 3, linear schedule with 0.1 warmup. `--spearman` swaps in the
|
|
64
|
+
Spearman loss, `--positional_loss_factor < 1` mixes in the angular loss,
|
|
65
|
+
`--resume_from_checkpoint` resumes from the highest `checkpoint-N`.
|
|
66
|
+
3. **Evaluate**: `geopres-eval` (trained projection) or `geopres-eval-backbone`
|
|
67
|
+
(raw backbone). Both run MTEB through `eval_utils.evaluate_mteb`, which
|
|
68
|
+
caches under `$EVALUATION_RESULTS_PATH`.
|
|
69
|
+
4. **Collect**: `geopres-compare` writes `comparison_results.csv` into
|
|
70
|
+
`$EVALUATION_RESULTS_PATH`. The scripts in `analysis/` turn that into the
|
|
71
|
+
paper's tables and figures.
|
|
72
|
+
|
|
73
|
+
## Pinned / fragile dependencies
|
|
74
|
+
|
|
75
|
+
- `transformers==4.57.6` — upgrading breaks
|
|
76
|
+
`Alibaba-NLP/gte-multilingual-base` (mGTE) at encode time. Do not bump
|
|
77
|
+
without retesting.
|
|
78
|
+
- `sentence-transformers==5.3.0` — the version behind every paper number.
|
|
79
|
+
- `mteb==2.12.26` — the version behind the result caches.
|
|
80
|
+
`eval_utils.evaluate_sts` is tested against it; re-run the evaluation if you
|
|
81
|
+
bump it.
|
|
82
|
+
- `torchsort` is an optional extra (`--extra spearman`). It builds from source
|
|
83
|
+
and needs a CUDA toolchain; pre-built wheels are linked from `README.md`
|
|
84
|
+
("Common issues").
|
|
85
|
+
|
|
86
|
+
## Devices
|
|
87
|
+
|
|
88
|
+
`config.default_device()` returns `cuda` when a GPU is visible, otherwise
|
|
89
|
+
`cpu`. Scripts log a warning instead of failing on CPU; the paper configuration
|
|
90
|
+
still needs a GPU. The library API works on CPU.
|
|
91
|
+
|
|
92
|
+
## Tests
|
|
93
|
+
|
|
94
|
+
`tests/` holds small pytest tests for the API, the save/load round trip, the
|
|
95
|
+
Hub helpers and the losses. They must stay CPU-only, network-free and fast: no
|
|
96
|
+
model downloads, no MTEB runs, no real `push_to_hub` (mock
|
|
97
|
+
`huggingface_hub.snapshot_download` instead).
|
|
98
|
+
|
|
99
|
+
## Conventions
|
|
100
|
+
|
|
101
|
+
- `torch.manual_seed(42)` at module top of `eval_utils.py` and
|
|
102
|
+
`geopres_trainer.py`.
|
|
103
|
+
- `TOKENIZERS_PARALLELISM=false` in the training and precalculate scripts.
|
|
104
|
+
- `HF_HUB_DOWNLOAD_TIMEOUT=300` in `precalculate_embeddings.py`.
|
|
105
|
+
- Model names contain the backbone, target dim, batch size and method suffix;
|
|
106
|
+
adding a suffix requires updating `_get_extrinsic_model_names` in
|
|
107
|
+
`analysis/calculate_intrinsic_correlations.py`.
|
|
108
|
+
|
|
109
|
+
## What not to do
|
|
110
|
+
|
|
111
|
+
- Do not bump `transformers` or `sentence-transformers` without checking mGTE.
|
|
112
|
+
- Do not import `mteb`, `datasets` or `torchsort` at module level in library
|
|
113
|
+
modules (it breaks core-only installs).
|
|
114
|
+
- Do not re-introduce `.env` / `python-dotenv` or hard-coded `cuda`.
|
|
115
|
+
- Do not `pip install` globally — use `uv`.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
cff-version: 1.2.0
|
|
2
|
+
message: "If you use this software, please cite the paper below."
|
|
3
|
+
title: "GeoPres"
|
|
4
|
+
type: software
|
|
5
|
+
version: 0.1.0
|
|
6
|
+
license: MIT
|
|
7
|
+
repository-code: "https://github.com/konstantinoskalyfommatos/geopres"
|
|
8
|
+
abstract: >-
|
|
9
|
+
GeoPres learns a single linear map that reduces the dimensionality of text
|
|
10
|
+
embeddings while preserving pairwise Euclidean distances, and evaluates it
|
|
11
|
+
against PCA, random projection, random selection, truncation and
|
|
12
|
+
autoencoder baselines on MTEB.
|
|
13
|
+
authors:
|
|
14
|
+
- family-names: Kalyfommatos
|
|
15
|
+
given-names: Konstantinos
|
|
16
|
+
- family-names: Michail
|
|
17
|
+
given-names: Andrianos
|
|
18
|
+
- family-names: Huber
|
|
19
|
+
given-names: Marius
|
|
20
|
+
- family-names: Psomas
|
|
21
|
+
given-names: Bill
|
|
22
|
+
- family-names: Clematide
|
|
23
|
+
given-names: Simon
|
|
24
|
+
- family-names: Opitz
|
|
25
|
+
given-names: Juri
|
|
26
|
+
preferred-citation:
|
|
27
|
+
type: conference-paper
|
|
28
|
+
title: "On Post-Hoc Dimensionality Reduction of Text Embeddings"
|
|
29
|
+
year: 2027
|
|
30
|
+
collection-title: "Proceedings of the 2027 Conference of the European Chapter of the Association for Computational Linguistics (EACL 2027)"
|
|
31
|
+
url: "https://openreview.net/forum?id=vAWo3CIzqp"
|
|
32
|
+
authors:
|
|
33
|
+
- family-names: Kalyfommatos
|
|
34
|
+
given-names: Konstantinos
|
|
35
|
+
- family-names: Michail
|
|
36
|
+
given-names: Andrianos
|
|
37
|
+
- family-names: Huber
|
|
38
|
+
given-names: Marius
|
|
39
|
+
- family-names: Psomas
|
|
40
|
+
given-names: Bill
|
|
41
|
+
- family-names: Clematide
|
|
42
|
+
given-names: Simon
|
|
43
|
+
- family-names: Opitz
|
|
44
|
+
given-names: Juri
|
|
45
|
+
notes: >-
|
|
46
|
+
Accepted at EACL 2027. Page numbers, DOI and publisher are to be added once
|
|
47
|
+
the proceedings are published.
|
geopres-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Konstantinos Kalyfommatos and contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
geopres-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: geopres
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Post-hoc dimensionality reduction of text embeddings that preserves pairwise geometry.
|
|
5
|
+
Project-URL: Repository, https://github.com/konstantinoskalyfommatos/geopres
|
|
6
|
+
Project-URL: Issues, https://github.com/konstantinoskalyfommatos/geopres/issues
|
|
7
|
+
Author: Konstantinos Kalyfommatos
|
|
8
|
+
License: MIT License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2025 Konstantinos Kalyfommatos and contributors
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Keywords: dimensionality-reduction,embeddings,linear-projection,mteb,sentence-transformers
|
|
31
|
+
Classifier: Intended Audience :: Science/Research
|
|
32
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
33
|
+
Classifier: Operating System :: OS Independent
|
|
34
|
+
Classifier: Programming Language :: Python :: 3
|
|
35
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
39
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
40
|
+
Classifier: Typing :: Typed
|
|
41
|
+
Requires-Python: >=3.10
|
|
42
|
+
Requires-Dist: numpy>=1.24
|
|
43
|
+
Requires-Dist: safetensors>=0.4
|
|
44
|
+
Requires-Dist: sentence-transformers==5.3.0
|
|
45
|
+
Requires-Dist: torch>=2.0
|
|
46
|
+
Requires-Dist: tqdm>=4.60
|
|
47
|
+
Requires-Dist: transformers==4.57.6
|
|
48
|
+
Provides-Extra: dev
|
|
49
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
50
|
+
Provides-Extra: research
|
|
51
|
+
Requires-Dist: accelerate; extra == 'research'
|
|
52
|
+
Requires-Dist: datasets; extra == 'research'
|
|
53
|
+
Requires-Dist: huggingface-hub; extra == 'research'
|
|
54
|
+
Requires-Dist: matplotlib; extra == 'research'
|
|
55
|
+
Requires-Dist: mteb==2.12.26; extra == 'research'
|
|
56
|
+
Requires-Dist: pandas; extra == 'research'
|
|
57
|
+
Requires-Dist: scikit-learn; extra == 'research'
|
|
58
|
+
Requires-Dist: scipy; extra == 'research'
|
|
59
|
+
Requires-Dist: statsmodels>=0.14.6; extra == 'research'
|
|
60
|
+
Provides-Extra: spearman
|
|
61
|
+
Requires-Dist: torchsort; extra == 'spearman'
|
|
62
|
+
Description-Content-Type: text/markdown
|
|
63
|
+
|
|
64
|
+
# GeoPres
|
|
65
|
+
|
|
66
|
+
GeoPres compresses high-dimensional text embeddings with a **single linear map**
|
|
67
|
+
that is trained to keep pairwise Euclidean distances. It is *post-hoc*: the
|
|
68
|
+
embedding model stays frozen, and at inference time the whole "reduction" is one
|
|
69
|
+
matrix multiply. The method is described in our [**EACL paper: "On Post-Hoc Dimensionality Reduction of Text Embeddings"**](https://openreview.net/forum?id=vAWo3CIzqp).
|
|
70
|
+
|
|
71
|
+
This repository contains features two parts:
|
|
72
|
+
|
|
73
|
+
1. **`geopres`, a small library** — train a projection on your own embeddings,
|
|
74
|
+
save it, reload it, and use it on new text (no GPU required).
|
|
75
|
+
2. **The paper's experimental pipeline** — pre-encode 10M C4 passages, train,
|
|
76
|
+
run MTEB, compare against PCA / random projection / random selection /
|
|
77
|
+
truncation / autoencoder baselines, and rebuild the paper's tables and plots.
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Install
|
|
82
|
+
|
|
83
|
+
Python 3.10 or newer is required (tested on 3.10, 3.11, 3.12 and 3.13).
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
git clone https://github.com/konstantinoskalyfommatos/geopres
|
|
87
|
+
cd geopres
|
|
88
|
+
|
|
89
|
+
# recommended: uv, exactly how the paper runs were made
|
|
90
|
+
uv sync --extra research # package + paper dependencies
|
|
91
|
+
uv sync --extra research --extra spearman # ... plus torchsort, for --spearman
|
|
92
|
+
|
|
93
|
+
# or with plain pip
|
|
94
|
+
pip install -e . # library only (torch + sentence-transformers)
|
|
95
|
+
pip install -e ".[research]" # + datasets, mteb, matplotlib, ...
|
|
96
|
+
pip install -e ".[spearman]" # + torchsort, only for the --spearman loss
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
`torchsort` is not installed by default because it has to be compiled against
|
|
100
|
+
your CUDA toolchain; see [Common issues](#common-issues).
|
|
101
|
+
|
|
102
|
+
> **PyPI.** `pip install geopres` installs the library only. Add `[research]`
|
|
103
|
+
> or `[spearman]` for the paper pipeline: `pip install "geopres[research]"`.
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## Quick start (library)
|
|
108
|
+
|
|
109
|
+
Fitting a dimension-reduced embedding model:
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from geopres import GeoPres
|
|
113
|
+
|
|
114
|
+
model = GeoPres("sentence-transformers/all-mpnet-base-v2", dim=32)
|
|
115
|
+
model.fit(texts) # texts: list[str]
|
|
116
|
+
reduced = model.embed(texts) # numpy array, shape (len(texts), 32)
|
|
117
|
+
|
|
118
|
+
model.save("geopres-mpnet-32")
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Note that `fit` also accepts embeddings you already have, which allows much cheaper training:
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
import torch
|
|
125
|
+
from geopres import GeoPres
|
|
126
|
+
|
|
127
|
+
embeddings = torch.load("my_embeddings.pt") # (n, 768)
|
|
128
|
+
model = GeoPres("sentence-transformers/all-mpnet-base-v2", dim=32)
|
|
129
|
+
model.fit(embeddings) # no encoding, just the projection
|
|
130
|
+
reduced = model.embed(embeddings) # (n, 32)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Load a trained projection and use it on new text:
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from geopres import GeoPres
|
|
137
|
+
|
|
138
|
+
model = GeoPres.load_path("geopres-mpnet-32") # backbone id comes from config.json
|
|
139
|
+
reduced = model.embed(["a sentence I have never seen before"])
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
The projection is a plain matrix, therefore it can be applied on embeddings
|
|
143
|
+
created elsewhere (e.g. Hugging Face's
|
|
144
|
+
[TEI](https://huggingface.co/docs/text-embeddings-inference)), as long as the
|
|
145
|
+
underlying vector-generator function (e.g. embedding model) is the same during
|
|
146
|
+
training and inference. "Same" includes the pooling, the normalisation and, for
|
|
147
|
+
instruction-based models such as Qwen3-Embedding, the same query/passage
|
|
148
|
+
prefixes.
|
|
149
|
+
|
|
150
|
+
`Projection` is the API for exactly that: the matrix and nothing else. No
|
|
151
|
+
backbone is loaded, downloaded or needed.
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
from geopres import Projection
|
|
155
|
+
|
|
156
|
+
projection = Projection.load_path("geopres-mpnet-32")
|
|
157
|
+
# or: Projection.from_pretrained("kkalyfo/geopres-mpnet-32")
|
|
158
|
+
projection.source_dim, projection.dim # (768, 32)
|
|
159
|
+
|
|
160
|
+
# any vector from the same embedding model: TEI, a vector DB, another service
|
|
161
|
+
vector = get_embedding(embedding_model_name="sentence-transformers/all-mpnet-base-v2")
|
|
162
|
+
reduced = projection.project(vector) # (32,)
|
|
163
|
+
|
|
164
|
+
vectors = ... # (1000, 768)
|
|
165
|
+
reduced = projection.project(vectors) # (1000, 32)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
If you would rather not depend on `geopres` at inference time, the matrix is
|
|
169
|
+
just `projection.weight`, of shape `(dim, source_dim)`: one `vectors @ weight.T`.
|
|
170
|
+
It is read-only, so a trained projection cannot be changed by accident.
|
|
171
|
+
|
|
172
|
+
Numpy in, numpy out; a tensor in, a tensor out on the same device and dtype.
|
|
173
|
+
Vectors that already live on a GPU are therefore never copied to the host:
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
vectors = torch.load("gpu_embeddings.pt").cuda() # stays on the GPU
|
|
177
|
+
reduced = projection.project(vectors) # torch.Tensor, cuda
|
|
178
|
+
|
|
179
|
+
projection.to_torch(device="cuda") # frozen nn.Linear, e.g. for torch.compile / ONNX
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
The projection is one `768 x 32` matrix, so it is memory-bound: on CPU it costs
|
|
183
|
+
about the same as the copy it would take to move the vectors to a GPU. Keep it
|
|
184
|
+
on whichever device already holds the vectors.
|
|
185
|
+
|
|
186
|
+
Upload your own projection, or load one from the Hub:
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
model.push_to_hub("kkalyfo/geopres-mpnet-32") # config + weights + model card
|
|
190
|
+
|
|
191
|
+
from geopres import GeoPres
|
|
192
|
+
model = GeoPres.from_pretrained("kkalyfo/geopres-mpnet-32")
|
|
193
|
+
reduced = model.embed(["a sentence I have never seen before"])
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
Trained projections released with the paper live under
|
|
197
|
+
[huggingface.co/kkalyfo](https://huggingface.co/kkalyfo).
|
|
198
|
+
|
|
199
|
+
### API summary
|
|
200
|
+
|
|
201
|
+
| Call | What it does |
|
|
202
|
+
| --- | --- |
|
|
203
|
+
| `GeoPres(model, dim=32)` | `model` is a sentence-transformers id, a loaded `SentenceTransformer`, or `None` (embeddings only). |
|
|
204
|
+
| `.fit(data, ...)` | Train on texts or on an `(n, source_dim)` array/tensor. Returns `self`. |
|
|
205
|
+
| `.embed(data, ...)` | Project texts or embeddings to `dim`. Returns `(n, dim)` numpy by default, `(dim,)` for a single vector. |
|
|
206
|
+
| `.save(path)` | Writes `config.json` + `projection.safetensors`. |
|
|
207
|
+
| `GeoPres.load_path(path)` | Reloads a saved directory (or a bare `.pt` / `.safetensors` weight file). |
|
|
208
|
+
| `.to_sentence_transformer()` | Backbone + projection as a `SentenceTransformer`, e.g. for MTEB. |
|
|
209
|
+
| `.push_to_hub(repo_id, private=False)` | Upload `config.json`, weights and a model card to the HF Hub. |
|
|
210
|
+
| `GeoPres.from_pretrained(repo_id)` | Download and load a projection from the HF Hub. |
|
|
211
|
+
| `Projection.load_path(path)` / `.from_pretrained(repo_id)` | The projection alone, for embeddings you already have. `.project(vectors)`, no backbone. `.to_torch()` for the `nn.Module`. |
|
|
212
|
+
|
|
213
|
+
`fit` defaults follow the paper (10 epochs, AdamW, lr 1e-2, weight decay 0.1,
|
|
214
|
+
linear warmup 0.1, early stopping patience 3, batch size 20 000). Batch size is
|
|
215
|
+
a hyperparameter, not only a memory setting: the loss compares all pairs inside
|
|
216
|
+
a batch, so a smaller batch trains on a different signal.
|
|
217
|
+
|
|
218
|
+
Data size matters less than batch size. The paper trained on 10M passages
|
|
219
|
+
because more data kept helping, but a few hundred thousand text samples are
|
|
220
|
+
enough to get a good projection (see [Data](#reproducing-the-paper)).
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
## Reproducing the paper
|
|
225
|
+
|
|
226
|
+
The pipeline is **precalculate → train → evaluate → collect**.
|
|
227
|
+
|
|
228
|
+
```bash
|
|
229
|
+
# 0) environment
|
|
230
|
+
uv sync --extra research
|
|
231
|
+
|
|
232
|
+
# 1) pre-encode the corpora with a backbone (one-time per backbone, GPU, slow)
|
|
233
|
+
uv run geopres-precalculate \
|
|
234
|
+
--model_name jinaai/jina-embeddings-v2-small-en \
|
|
235
|
+
--train --val_test --batch_size 8192
|
|
236
|
+
|
|
237
|
+
# 2) train the projection
|
|
238
|
+
uv run geopres-train \
|
|
239
|
+
--backbone_model jinaai/jina-embeddings-v2-small-en \
|
|
240
|
+
--source_dim 512 --target_dim 32 \
|
|
241
|
+
--train_batch_size 20000 --val_batch_size 20000 \
|
|
242
|
+
--positional_loss_factor 1 --weighted_loss
|
|
243
|
+
|
|
244
|
+
# 3) evaluate on MTEB (this is also run automatically at the end of step 2)
|
|
245
|
+
uv run geopres-eval --backbone_model jinaai/jina-embeddings-v2-small-en --source_dim 512 --target_dim 32
|
|
246
|
+
|
|
247
|
+
# 4) merge all MTEB caches into one CSV
|
|
248
|
+
uv run geopres-compare
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
Everything is also runnable as a module (`python -m geopres.train_model ...`)
|
|
252
|
+
if you prefer not to use the console scripts.
|
|
253
|
+
|
|
254
|
+
**Data.** Step 1 uses `allenai/c4` (`en` split, 10M passages longer than 200
|
|
255
|
+
words) for training and `agentlans/sentence-paraphrases` for validation/test.
|
|
256
|
+
No dataset is downloaded by the library; only step 1 needs the `datasets`
|
|
257
|
+
package.
|
|
258
|
+
|
|
259
|
+
We used 10M training samples because results kept improving as we added more
|
|
260
|
+
data, but this is not a requirement: a few hundred thousand text samples are
|
|
261
|
+
already enough for a good projection, and they make the one-time encoding step
|
|
262
|
+
much cheaper. Pass `--max_train_examples` to cap the training set.
|
|
263
|
+
|
|
264
|
+
**Baselines** read the same cached embeddings:
|
|
265
|
+
|
|
266
|
+
```bash
|
|
267
|
+
uv run python -m geopres.baselines.fit_pca --model_name <backbone> --target_dim 32
|
|
268
|
+
uv run python -m geopres.baselines.eval_pca --backbone_model <backbone> --target_dim 32
|
|
269
|
+
uv run python -m geopres.baselines.train_autoencoder --backbone_model <backbone> --target_dim 32
|
|
270
|
+
# ... eval_random_projection, eval_random_selection, eval_truncation
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
**Tables and plots.** `analysis/` contains the scripts that regenerate the
|
|
274
|
+
paper's figures, LaTeX tables and bootstrap tests from the cached results. They
|
|
275
|
+
are not part of the installed package, e.g.
|
|
276
|
+
`uv run python analysis/create_scatter_plot.py`.
|
|
277
|
+
|
|
278
|
+
### Where files go
|
|
279
|
+
|
|
280
|
+
Everything lands under `storage/` in the repository:
|
|
281
|
+
|
|
282
|
+
```
|
|
283
|
+
storage/
|
|
284
|
+
├── precalculated_embeddings/<dataset>/<backbone>/{train,validation,test}_embeddings.pt
|
|
285
|
+
├── trained_models/<backbone>/<model_name>/checkpoint-N/ + best_model.pt
|
|
286
|
+
├── trained_autoencoders/<backbone>/<model_name>/...
|
|
287
|
+
├── pca/<backbone>/<target_dim>/
|
|
288
|
+
└── evaluation_results/{backbone,trained_models,autoencoders}/...
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
`HF_TOKEN` is picked up from the environment by `huggingface_hub` if you need
|
|
292
|
+
gated datasets.
|
|
293
|
+
|
|
294
|
+
---
|
|
295
|
+
|
|
296
|
+
## Repository layout
|
|
297
|
+
|
|
298
|
+
```
|
|
299
|
+
geopres/
|
|
300
|
+
├── api.py # public API: GeoPres (fit / embed / save) and Projection
|
|
301
|
+
├── projection.py # the linear map W (a bias-free nn.Linear)
|
|
302
|
+
├── losses.py # positional / angular / Spearman losses
|
|
303
|
+
├── encode.py # loading a backbone and encoding text
|
|
304
|
+
├── config.py # paths + dtype helpers
|
|
305
|
+
├── train_model.py # paper training entry point (HF Trainer)
|
|
306
|
+
├── geopres_trainer.py # HF Trainer subclass with the pairwise loss
|
|
307
|
+
├── reduced_sentence_transformer.py # backbone + projection as a SentenceTransformer
|
|
308
|
+
├── precalculate_embeddings.py # one-time encoding of C4 / sentence-paraphrases
|
|
309
|
+
├── eval_model.py # MTEB + intrinsic eval of a trained projection
|
|
310
|
+
├── eval_backbone.py # MTEB eval of the raw backbone (reference point)
|
|
311
|
+
├── eval_utils.py # shared MTEB runner and intrinsic metrics
|
|
312
|
+
├── compare_results.py # merges MTEB caches into comparison_results.csv
|
|
313
|
+
├── py.typed # this package ships type hints
|
|
314
|
+
└── baselines/ # PCA, random projection, random selection, truncation, autoencoder
|
|
315
|
+
analysis/ # paper tables, plots, bootstrap tests (not shipped)
|
|
316
|
+
tests/ # pytest: API, persistence, losses, hub (no GPU, no network)
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
321
|
+
## Common issues
|
|
322
|
+
|
|
323
|
+
### `torchsort` fails to install or build
|
|
324
|
+
|
|
325
|
+
`torchsort` is only needed for `--spearman`. It builds from source, so it needs
|
|
326
|
+
a CUDA toolchain. If that fails, install a pre-built wheel from the
|
|
327
|
+
[torchsort releases](https://github.com/teddykoker/torchsort/releases):
|
|
328
|
+
|
|
329
|
+
```bash
|
|
330
|
+
export TORCHSORT=0.1.10
|
|
331
|
+
export TORCH=pt26 # your torch version: pt26, pt25, pt24, pt21, pt20, pt113
|
|
332
|
+
export CUDA=cu126 # cpu, cu113, cu117, cu118, cu121, cu124, cu126
|
|
333
|
+
export PYTHON=cp312 # cp310, cp311, cp312
|
|
334
|
+
|
|
335
|
+
uv pip install https://github.com/teddykoker/torchsort/releases/download/v${TORCHSORT}/torchsort-${TORCHSORT}+${TORCH}${CUDA}-${PYTHON}-${PYTHON}-linux_x86_64.whl
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
### `Alibaba-NLP/gte-multilingual-base` (mGTE) breaks with newer transformers
|
|
339
|
+
|
|
340
|
+
The paper's environment pins `transformers==4.57.6`. Newer versions break mGTE
|
|
341
|
+
at encode time. If you change the pin, re-test mGTE.
|
|
342
|
+
|
|
343
|
+
### Out of memory during training
|
|
344
|
+
|
|
345
|
+
Memory is dominated by the pairwise loss, which compares every pair of
|
|
346
|
+
embeddings in a batch, so it grows quadratically with the batch size. The paper
|
|
347
|
+
trains with `--train_batch_size 20000`, and that is the configuration the
|
|
348
|
+
reported numbers come from: keep it if you want comparable results.
|
|
349
|
+
|
|
350
|
+
Shrinking the batch is not a free fix. Fewer pairs per step means a different
|
|
351
|
+
training signal, so a smaller batch produces a different model, not the same
|
|
352
|
+
model on less memory. If 20000 does not fit on your GPU, treat the smaller batch
|
|
353
|
+
as a new configuration and re-check the intrinsic validation loss (ideally also
|
|
354
|
+
a task or two) instead of assuming the published numbers carry over.
|
|
355
|
+
|
|
356
|
+
---
|
|
357
|
+
|
|
358
|
+
## Development
|
|
359
|
+
|
|
360
|
+
```bash
|
|
361
|
+
uv sync --extra research
|
|
362
|
+
uv run pytest # fast, CPU-only, no downloads
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
## Citation
|
|
366
|
+
|
|
367
|
+
```bibtex
|
|
368
|
+
@inproceedings{kalyfommatos2027geopres,
|
|
369
|
+
title = {On Post-Hoc Dimensionality Reduction of Text Embeddings},
|
|
370
|
+
author = {Kalyfommatos, Konstantinos and Michail, Andrianos and Huber, Marius
|
|
371
|
+
and Psomas, Bill and Clematide, Simon and Opitz, Juri},
|
|
372
|
+
booktitle = {Proceedings of the 2027 Conference of the European Chapter of the
|
|
373
|
+
Association for Computational Linguistics (EACL 2027)},
|
|
374
|
+
year = {2027},
|
|
375
|
+
url = {https://openreview.net/forum?id=vAWo3CIzqp}
|
|
376
|
+
}
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
See also [`CITATION.cff`](https://github.com/konstantinoskalyfommatos/geopres/blob/main/CITATION.cff).
|
|
380
|
+
|
|
381
|
+
## License
|
|
382
|
+
|
|
383
|
+
MIT — see [`LICENSE`](https://github.com/konstantinoskalyfommatos/geopres/blob/main/LICENSE).
|