Arthurizijar/Text_aligns_tokens

The official code implementation of the ACL2025 paper “A Text is Worth Several Tokens: Text Embedding from LLMs Secretly Aligns Well with The Key Tokens”. Text Embedding from LLMs Secretly Aligns Well with The Key Tokens”.

18

stars

4

commits

Python

primary language

Jul 12, 2025

updated

README

A Text is Worth Several Tokens

The official code implementation of the ACL2025 paper “A Text is Worth Several Tokens: Text Embedding from LLMs Secretly Aligns Well with The Key Tokens”.

Overview

overview

Environment Configuration

conda env create -f environment.yml -n your_new_environment_name

⚠️ If you are in Mainland China, you need to configure a mirror to access Huggingface

export HF_ENDPOINT=https://hf-mirror.com

Quick Start

Run the following command to produce an .xlsx file that lists each original sentence alongside the tokens aligned to its embeddings.

python aligned_token.py

Set $model and $dataset with different values to evaluate different embedders across various datasets.

python aligned_token.py --model=$model --dataset=$dataset

Note: download the wiki and nli datasets in advance from the link if you need them. The placement path is data/wiki/wiki1m_for_simcse.txt and daya/nli/nli_for_simcse.csv .

Support LLM-based Embedders

--modelCorresponding Model
gpt_neoEleutherAI/gpt-neo-1.3B
sgpt_nliMuennighoff/SGPT-1.3B-weightedmean-nli
sgpt_msmarcoMuennighoff/SGPT-1.3B-weightedmean-msmarco-specb-bitfit
optfacebook/opt-1.3b
opt_eolfacebook/opt-1.3b
opt_eol_cseroyokong/prompteol-opt-1.3b
llamaFill out Meta's official form for weights
llama_eolFill out Meta's official form for weights
llama_eol_cseroyokong/prompteol-llama-7b
llama2meta-llama/Llama-2-7b-chat-hf
llama2_eolmeta-llama/Llama-2-7b-chat-hf
mistralmistralai/Mistral-7B-Instruct-v0.2
gritlmGritLM/GritLM-7B
llm2vec_mistral_supMcGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp+
McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp-supervised
llm2vec_mistral_unsupMcGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp+
McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp-unsup-simcse
e5_mistralintfloat/e5-mistral-7b-instruct

We also offer support for several embedders built on non-Transformer decoder architectures. However, as noted in the Limitations section, not every lightweight embedder exhibits the “alignment to key tokens” phenomenon. Accordingly, the final column records the behavior we actually observed for each model.

--modelCorresponding Model in HFAligned with Key Tokens?
bertbert-base-uncased
prompt_bertbert-base-uncased
prompt_bert_suproyokong/sup-PromptBERT
prompt_bert_unsuproyokong/unsup-PromptBERT
simcseprinceton-nlp/sup-simcse-bert-base-uncased
sentence_t5sentence-transformers/sentence-t5-large
contrieverfacebook/contriever
dprfacebook/dpr-question_encoder-single-nq-base+
facebook/dpr-ctx_encoder-single-nq-base
gtethenlper/gte-base
e5intfloat/e5-base-v2
mpnetsentence-transformers/all-mpnet-base-v2
spidertau/spider

Support Dataset

--datasetCorresponding Dataset in HF
stsprinceton-nlp/datasets-for-simcse
nliprinceton-nlp/datasets-for-simcse
msmarcomicrosoft/ms_marco
wikiprinceton-nlp/datasets-for-simcse

Spectral Analysis

1. Variation in each principal component

For a quick start, you can run:

python spectral_analyze.py --analyze_type=variation
gpt_neo_sgpt_nli_wiki_variation

This figure is slightly different from the one reported in the paper because we only used 100 samples for the SVD decomposition.

If more precise results are needed, a larger sample size can be selected using the data_size field.

python spectral_analyze.py --analyze_type=variation --data_size=10000
gpt_neo_sgpt_nli_wiki10k_variation

2. Contribution of different components to aligned tokens

For a quick start, you can run:

python spectral_analyze.py --analyze_type=contribution

Then you can get a figure as follows:

gpt_neo_contribution

This image draws the text YMCA in South Australia by default.

The text field can be used to modify the text to be analyzed:

python spectral_analyze.py --analyze_type=contribution --text="I like apple and banana"
gpt_neo_contribution2

3. Subtraction of the certain amount on the first principal component

For a quick start, you can run:

python spectral_analyze.py --analyze_type=change

Then you can get a figure as follows:

gpt_neo_change1st_v1_-50.02

This image draws the text YMCA in South Australia and $v_1$ calculated by the first step by default

The text, lambda_type and lambda_value field can be used to modify the default setting.

python spectral_analyze.py --analyze_type=change --text="I like apple and banana" --lambda_type=custom --lambda_value=-48.0
gpt_neo_change1st_custom_-48.00

Contributors

Arthurizijar

4 commits

Arthurizijar/Text_aligns_tokens

The official code implementation of the ACL2025 paper “A Text is Worth Several Tokens: Text Embedding from LLMs Secretly Aligns Well with The Key Tokens”. Text Embedding from LLMs Secretly Aligns Well with The Key Tokens”.

18

stars

4

commits

Python

primary language

Jul 12, 2025

updated

README

A Text is Worth Several Tokens

The official code implementation of the ACL2025 paper “A Text is Worth Several Tokens: Text Embedding from LLMs Secretly Aligns Well with The Key Tokens”.

Overview

overview

Environment Configuration

conda env create -f environment.yml -n your_new_environment_name

⚠️ If you are in Mainland China, you need to configure a mirror to access Huggingface

export HF_ENDPOINT=https://hf-mirror.com

Quick Start

Run the following command to produce an .xlsx file that lists each original sentence alongside the tokens aligned to its embeddings.

python aligned_token.py

Set $model and $dataset with different values to evaluate different embedders across various datasets.

python aligned_token.py --model=$model --dataset=$dataset

Note: download the wiki and nli datasets in advance from the link if you need them. The placement path is data/wiki/wiki1m_for_simcse.txt and daya/nli/nli_for_simcse.csv .

Support LLM-based Embedders

--modelCorresponding Model
gpt_neoEleutherAI/gpt-neo-1.3B
sgpt_nliMuennighoff/SGPT-1.3B-weightedmean-nli
sgpt_msmarcoMuennighoff/SGPT-1.3B-weightedmean-msmarco-specb-bitfit
optfacebook/opt-1.3b
opt_eolfacebook/opt-1.3b
opt_eol_cseroyokong/prompteol-opt-1.3b
llamaFill out Meta's official form for weights
llama_eolFill out Meta's official form for weights
llama_eol_cseroyokong/prompteol-llama-7b
llama2meta-llama/Llama-2-7b-chat-hf
llama2_eolmeta-llama/Llama-2-7b-chat-hf
mistralmistralai/Mistral-7B-Instruct-v0.2
gritlmGritLM/GritLM-7B
llm2vec_mistral_supMcGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp+
McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp-supervised
llm2vec_mistral_unsupMcGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp+
McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp-unsup-simcse
e5_mistralintfloat/e5-mistral-7b-instruct

We also offer support for several embedders built on non-Transformer decoder architectures. However, as noted in the Limitations section, not every lightweight embedder exhibits the “alignment to key tokens” phenomenon. Accordingly, the final column records the behavior we actually observed for each model.

--modelCorresponding Model in HFAligned with Key Tokens?
bertbert-base-uncased
prompt_bertbert-base-uncased
prompt_bert_suproyokong/sup-PromptBERT
prompt_bert_unsuproyokong/unsup-PromptBERT
simcseprinceton-nlp/sup-simcse-bert-base-uncased
sentence_t5sentence-transformers/sentence-t5-large
contrieverfacebook/contriever
dprfacebook/dpr-question_encoder-single-nq-base+
facebook/dpr-ctx_encoder-single-nq-base
gtethenlper/gte-base
e5intfloat/e5-base-v2
mpnetsentence-transformers/all-mpnet-base-v2
spidertau/spider

Support Dataset

--datasetCorresponding Dataset in HF
stsprinceton-nlp/datasets-for-simcse
nliprinceton-nlp/datasets-for-simcse
msmarcomicrosoft/ms_marco
wikiprinceton-nlp/datasets-for-simcse

Spectral Analysis

1. Variation in each principal component

For a quick start, you can run:

python spectral_analyze.py --analyze_type=variation
gpt_neo_sgpt_nli_wiki_variation

This figure is slightly different from the one reported in the paper because we only used 100 samples for the SVD decomposition.

If more precise results are needed, a larger sample size can be selected using the data_size field.

python spectral_analyze.py --analyze_type=variation --data_size=10000
gpt_neo_sgpt_nli_wiki10k_variation

2. Contribution of different components to aligned tokens

For a quick start, you can run:

python spectral_analyze.py --analyze_type=contribution

Then you can get a figure as follows:

gpt_neo_contribution

This image draws the text YMCA in South Australia by default.

The text field can be used to modify the text to be analyzed:

python spectral_analyze.py --analyze_type=contribution --text="I like apple and banana"
gpt_neo_contribution2

3. Subtraction of the certain amount on the first principal component

For a quick start, you can run:

python spectral_analyze.py --analyze_type=change

Then you can get a figure as follows:

gpt_neo_change1st_v1_-50.02

This image draws the text YMCA in South Australia and $v_1$ calculated by the first step by default

The text, lambda_type and lambda_value field can be used to modify the default setting.

python spectral_analyze.py --analyze_type=change --text="I like apple and banana" --lambda_type=custom --lambda_value=-48.0
gpt_neo_change1st_custom_-48.00

Contributors

Arthurizijar

4 commits

Languages

Python

95.7%

Perl

4.3%