s1ghhh commited on
Commit
319642d
Β·
verified Β·
1 Parent(s): 399efd3

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +110 -1
README.md CHANGED
@@ -11,4 +11,113 @@ datasets:
11
  language:
12
  - en
13
  pipeline_tag: feature-extraction
14
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  language:
12
  - en
13
  pipeline_tag: feature-extraction
14
+ ---
15
+
16
+ # CoIn-Matching-Head
17
+
18
+ Pre-trained matching head models for the **CoIn** framework β€” a system for auditing hidden reasoning tokens in commercial LLM APIs.
19
+
20
+ **Paper**: [CoIn: Counting the Invisible Reasoning Tokens in Commercial Opaque LLM APIs](https://arxiv.org/abs/2505.13778)
21
+
22
+ **Code**: [GitHub](https://github.com/s1ghhh/LLM-Auditing-CoIn)
23
+
24
+ ## Model Description
25
+
26
+ This repository contains three pre-trained models used in the CoIn auditing pipeline:
27
+
28
+ ### 1. Tokens2Block Matching Head (Model A)
29
+ - **Purpose**: Verifies that sampled token IDs match their corresponding reasoning blocks
30
+ - **Architecture**: `sentence-transformers/all-MiniLM-L6-v2` base encoder + cosine similarity matching head
31
+ - **Input**: Token ID embeddings (mean-pooled) + reasoning block text embedding
32
+ - **Output**: Match probability (0-1)
33
+
34
+ ### 2. Block2Answer Matching Head (Model B)
35
+ - **Purpose**: Verifies that each reasoning block is semantically relevant to the final answer
36
+ - **Architecture**: `sentence-transformers/all-MiniLM-L6-v2` base encoder + cosine similarity matching head
37
+ - **Input**: Reasoning block text embedding + answer text embedding
38
+ - **Output**: Match probability (0-1)
39
+
40
+ ### 3. DeepSet Verifier
41
+ - **Purpose**: Aggregates per-block matching scores into a final benign/malicious prediction
42
+ - **Architecture**: DeepSet (permutation-invariant set encoding)
43
+ - **Input**: Sequence of interleaved (score_a, score_b) pairs from Model A and B
44
+ - **Output**: Probability of the sample being benign (0-1)
45
+
46
+ ## Training Details
47
+
48
+ - **Base Embedding Model**: `sentence-transformers/all-MiniLM-L6-v2`
49
+ - **Matching Head Type**: Cosine similarity head (`cos_sim`)
50
+ - **Loss Function**: Focal Loss
51
+ - **Optimizer**: Adam
52
+ - **Learning Rate**: 2e-5
53
+ - **Batch Size**: 128
54
+ - **Epochs**: 3
55
+ - **Random Seed**: 42
56
+ - **Training Data**: [CoIn-Auditing-Dataset](https://huggingface.co/datasets/s1ghhh/CoIn-Auditing-Dataset)
57
+
58
+ ## Usage
59
+
60
+ ### Quick Start
61
+
62
+ ```python
63
+ from sentence_transformers import SentenceTransformer
64
+ import torch
65
+
66
+ # Load Model B (Block2Answer, block_size=256)
67
+ model_dir = "./matching_head_BlockToAnswer/256/train_all-MiniLM-L6-v2_mixed_pos_merged_4_domain_0.5_hard_easy_mixed_neg_4_domain_limit0_cos_sim_focal_freeze"
68
+ embedding_model = SentenceTransformer(f"{model_dir}/embedding_model", trust_remote_code=True)
69
+
70
+ # Load matching head
71
+ from heads import get_matching_head
72
+ embedding_dim = embedding_model.get_sentence_embedding_dimension()
73
+ matching_head = get_matching_head("cos_sim", embedding_dim)
74
+ matching_head.load_state_dict(torch.load(f"{model_dir}/matching_head.pt"))
75
+ matching_head.eval()
76
+
77
+ # Score a (reasoning_block, answer) pair
78
+ emb_block = embedding_model.encode("The derivative of x^2 is 2x...", convert_to_tensor=True)
79
+ emb_answer = embedding_model.encode("The answer is 2x.", convert_to_tensor=True)
80
+
81
+ features = {"embedding_a": emb_block.unsqueeze(0), "embedding_b": emb_answer.unsqueeze(0)}
82
+ with torch.no_grad():
83
+ logits = matching_head(features)["logits"]
84
+ score = torch.sigmoid(logits).item()
85
+ print(f"Match score: {score:.4f}")
86
+ ```
87
+
88
+ ### Full Pipeline
89
+
90
+ See the [GitHub repository](https://github.com/s1ghhh/LLM-Auditing-CoIn) for the complete CoIn pipeline usage.
91
+
92
+ ## File Structure
93
+
94
+ ```
95
+ CoIn-Matching-Head/
96
+ β”œβ”€β”€ matching_head_TokensToBlock/ # Model A
97
+ β”‚ └── {256,512,1024}/ # Block size variants
98
+ β”‚ └── train_.../
99
+ β”‚ β”œβ”€β”€ embedding_model/ # Sentence-transformers model
100
+ β”‚ β”œβ”€β”€ matching_head.pt # Matching head weights
101
+ β”‚ └── tokenid_embedding_cache.pt
102
+ β”œβ”€β”€ matching_head_BlockToAnswer/ # Model B
103
+ β”‚ └── {256,512,1024}/
104
+ β”‚ └── train_.../
105
+ β”‚ β”œβ”€β”€ embedding_model/
106
+ β”‚ └── matching_head.pt
107
+ └── learned_verifier/
108
+ β”œβ”€β”€ DeepSet/
109
+ β”‚ β”œβ”€β”€ deepset_weight.pt # DeepSet verifier weights
110
+ β”‚ └── model_cfg.py # Model config
111
+ └── RNN/ # RNN verifier variant
112
+ ```
113
+
114
+ ## Citation
115
+
116
+ ```bibtex
117
+ @article{sun2025coin,
118
+ title={Coin: Counting the invisible reasoning tokens in commercial opaque llm apis},
119
+ author={Sun, Guoheng and Wang, Ziyao and Tian, Bowei and Liu, Meng and Shen, Zheyu and He, Shwai and He, Yexiao and Ye, Wanghao and Wang, Yiting and Li, Ang},
120
+ journal={arXiv preprint arXiv:2505.13778},
121
+ year={2025}
122
+ }
123
+ ```