DSL (Distilling Soft Labels)
DSL (Li et al., 2026) is a way of training neural topic models on targets produced by a small language model, instead of on bag-of-words.
A causal language model reads each document inside a short prompt that asks for its theme. The label is never generated. Instead, DSL keeps two things from that single forward pass:
- The next-token distribution at the end of the prompt, restricted to the vocabulary and softened with a temperature. This soft label replaces the bag-of-words vector as the reconstruction target.
- The last hidden state at the end of the prompt. This replaces sentence embeddings as the document representation.
Bag-of-words targets can only give weight to words that occur in a document. A soft label also gives weight to theme words that the document never uses, such as "hockey" for a post about a trade rumour, which matters most for short texts.
The topic model in Turftopic's implementation is a ProdLDA-style variational autoencoder, and its loss is the KL divergence between the soft label and the reconstruction, plus the usual prior term.
Usage
from turftopic import DSL
documents = [...]
model = DSL(20)
doc_topic_matrix = model.fit_transform(documents)
model.print_topics()
The language model is run once over the corpus. A GPU is recommended for this step; the default teacher has 0.3B parameters. You can choose another causal language model from the Hugging Face Hub, or configure the teacher yourself:
from turftopic import DSL
from turftopic.encoders import SoftLabelEncoder
teacher = SoftLabelEncoder("Qwen/Qwen3.5-0.8B", batch_size=32, max_length=512)
model = DSL(20, encoder=teacher)
The teacher's outputs can be computed once and reused across models:
model = DSL(20, encoder=teacher)
model.vectorizer.fit(documents)
logits, hidden_states, _ = teacher.extract(documents, list(model.get_vocab()))
model.fit(documents, embeddings=hidden_states, logits=logits)
Note
Only vocabulary words that are a single alphabetic token for the language model are part of the soft label. The default vectorizer keeps the 2000 most frequent non-stop words; phrase vectorizers are not a good fit for this model.
Citation
Please cite the authors of the paper, and Turftopic when using the DSL model:
@inproceedings{li2026dsltopic,
title = {Improving Topic Modeling by Distilling Soft Labels from Language Models},
author = {Li, Raymond and Abaskohi, Amirhossein and Li, Chuyuan and Murray, Gabriel and Carenini, Giuseppe},
booktitle = {Proceedings of the 43rd International Conference on Machine Learning},
pages = {65815--65836},
year = {2026},
volume = {306},
series = {Proceedings of Machine Learning Research},
publisher = {PMLR},
url = {https://proceedings.mlr.press/v306/li26a.html}
}
@article{
Kardos2025,
title = {Turftopic: Topic Modelling with Contextual Representations from Sentence Transformers},
doi = {10.21105/joss.08183},
url = {https://doi.org/10.21105/joss.08183},
year = {2025},
publisher = {The Open Journal},
volume = {10},
number = {111},
pages = {8183},
author = {Kardos, Márton and Enevoldsen, Kenneth C. and Kostkan, Jan and Kristensen-McLachlan, Ross Deans and Rocca, Roberta},
journal = {Journal of Open Source Software}
}
API Reference
turftopic.models.dsl.DSL
Bases: ContextualModel
Topic model trained by Distilling Soft Labels from a language model (Li et al., 2026).
A small causal language model reads each document in a prompt that asks for its theme. Its next-token distribution over the vocabulary is the reconstruction target (instead of the bag-of-words), and its last hidden state is the document representation. The topic model itself is a ProdLDA-style autoencoder.
from turftopic import DSL
corpus: list[str] = ["some text", "more text", ...]
model = DSL(10).fit(corpus)
model.print_topics()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components |
int
|
Number of topics. |
required |
encoder |
Union[SoftLabelEncoder, str]
|
Causal language model used as the teacher, ERNIE-4.5-0.3B is the default. |
'baidu/ERNIE-4.5-0.3B-PT'
|
vectorizer |
Optional[CountVectorizer]
|
Vectorizer used for term extraction. Defaults to the 2000 most frequent non-stop words. Only words that are a single alphabetic token for the language model are part of the soft labels, so unigram vocabularies work best. |
None
|
temperature |
float
|
Softmax temperature of the soft labels. |
3.0
|
loss_weight |
float
|
Weight of the distillation term against the prior term. |
1000.0
|
hidden |
int
|
Units in each of the two encoder layers. |
200
|
dropout_rate |
float
|
Dropout in the encoder and on topic proportions. |
0.2
|
batch_size |
int
|
Batch size. |
64
|
learning_rate |
float
|
Learning rate of Adam. |
0.002
|
n_epochs |
int
|
Number of training epochs. |
100
|
random_state |
Optional[int]
|
Random state for reproducible training. |
None
|
device |
str
|
Device to train the topic model on. |
'cpu'
|
Source code in turftopic/models/dsl.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
fit_transform(raw_documents, y=None, embeddings=None, logits=None)
Fits the model and returns the document-topic matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_documents |
Documents to fit the model on. |
required | |
embeddings |
Optional[ndarray]
|
Precomputed hidden states from the teacher. |
None
|
logits |
Optional[ndarray]
|
Precomputed teacher logits over the vectorizer's vocabulary. |
None
|
Source code in turftopic/models/dsl.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
transform(raw_documents, embeddings=None)
Infers topic proportions of documents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_documents |
Documents to infer topics for. |
required | |
embeddings |
Optional[ndarray]
|
Precomputed hidden states from the teacher. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray of shape (n_documents, n_topics)
|
Document-topic matrix. |
Source code in turftopic/models/dsl.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
turftopic.encoders.soft_labels.SoftLabelEncoder
Bases: ExternalEncoder
Causal language model that yields a document embedding and a soft label.
The document is placed in a prompt that asks for its theme. The embedding is the last-layer hidden state at the end of the prompt, and the soft label is the next-token distribution at that position, restricted to a vocabulary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name |
str
|
Any causal language model on the Hugging Face Hub. |
'baidu/ERNIE-4.5-0.3B-PT'
|
instruction |
str
|
Instruction placed before the document. |
DEFAULT_INSTRUCTION
|
template |
str
|
Prompt template with |
DEFAULT_TEMPLATE
|
batch_size |
int
|
Documents per forward pass. |
16
|
max_length |
Optional[int]
|
Longer prompts have their document shortened (the end of the prompt is kept). |
512
|
device |
Optional[str]
|
Defaults to CUDA when available. |
None
|
Source code in turftopic/encoders/soft_labels.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
encode(sentences)
Document embeddings: the last hidden state at the end of the prompt.
Source code in turftopic/encoders/soft_labels.py
167 168 169 | |
extract(documents, vocabulary, progress=None)
Return (logits, hidden): arrays of shape (n_docs, n_vocab) and (n_docs, hidden_size).
Source code in turftopic/encoders/soft_labels.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |