---
title: "DiScoFormer: One transformer for density and score, across distributions"
description: "DiScoFormer is a transformer that takes a set of sample points and, in one forward pass, estimates both the density and the score (gradient of log-density) of the underlying distribution without per-distribution retraining."
url: "https://swe.quest/articles/discoformer-one-transformer-for-density-and-score-across-distributions-0da88f66916ae95c"
canonical: "https://huggingface.co/blog/allenai/discoformer"
source: "Hugging Face"
published: "2026-06-29"
difficulty: "hard"
kind: "Research"
reading_minutes: 4
tags: ["machine-learning","model-training"]
---

# DiScoFormer: One transformer for density and score, across distributions

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2026-06-29, 4 min read, Research. Original: [https://huggingface.co/blog/allenai/discoformer](https://huggingface.co/blog/allenai/discoformer)

## Summary

DiScoFormer is a transformer that takes a set of sample points and, in one forward pass, estimates both the density and the score (gradient of log-density) of the underlying distribution without per-distribution retraining. It uses cross-attention to query arbitrary points and a shared backbone with density and score heads, whose mismatch serves as a label-free consistency loss for test-time adaptation to out-of-distribution inputs. Attention provably generalizes kernel density estimation, since a single head approximates a Gaussian kernel, and the model is trained on freshly sampled Gaussian mixtures with closed-form targets. In 100 dimensions it cuts score error about 6.5x and density error over 37x versus the best tuned KDE and generalizes to Laplace and Student-t distributions, though KDE remains faster on small datasets.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)

## Prerequisites

- transformer attention
- kernel density estimation ([easy posts on Operating systems](https://swe.quest/tags/operating-systems.md?difficulty=easy))
- score matching
- Gaussian mixture models

## Keywords

DiScoFormer, kernel density estimation, score matching, Gaussian Mixture Models, diffusion models, Ai2

## Related posts

- [Towards demystifying the creativity of diffusion models](https://swe.quest/articles/towards-demystifying-the-creativity-of-diffusion-models-b65392995a4ab4c2.md): Google Research · 2026-07-15 · Hard · 7 min read · Machine learning, Model training
  Diffusion models generalize beyond their training data because neural networks learn a smoothed version of the score function rather than the exact empirical one, which would only reproduce training samples.
- [Finetune Stable Diffusion Models with DDPO via TRL](https://swe.quest/articles/finetune-stable-diffusion-models-with-ddpo-via-trl-08d1757d4cd09bc2.md): Hugging Face · 2023-09-29 · Hard · 9 min read · Model training, Computer vision, Machine learning, Python
  Denoising Diffusion Policy Optimization (DDPO) fine-tunes diffusion models with reinforcement learning by treating the full denoising process as a multistep MDP.
- [Nyströmformer: Approximating self-attention in linear time and memory via the Nyström method](https://swe.quest/articles/nystromformer-approximating-self-attention-in-linear-time-and-memory-via-the-fd6613fb9ac42d67.md): Hugging Face · 2022-08-02 · Hard · 6 min read · LLMs, Machine learning, Algorithms, Python
  Nyströmformer replaces quadratic softmax self-attention with an O(n) approximation built on the Nyström method, which reconstructs a matrix from a sampled subset of its rows and columns via a Moore-Penrose pseudoinverse.
- [Universal Image Segmentation with Mask2Former and OneFormer](https://swe.quest/articles/universal-image-segmentation-with-mask2former-and-oneformer-c9679decf8636d8b.md): Hugging Face · 2023-01-19 · Medium · 7 min read · Computer vision, Machine learning, Python
  Instance, semantic and panoptic segmentation have converged on "universal" architectures that treat every task as binary mask classification rather than per-pixel classification, a line running from DETR through MaskFormer to Mask2Former.
- [Multi-token Residual Prediction](https://swe.quest/articles/multi-token-residual-prediction-f7971ab59cb61cdd.md): Modal · 2026-07-01 · Hard · 13 min read · Inference & serving, LLMs, Model training, Performance engineering
  Multi-token Residual Prediction (MRP) adapts multi-token prediction to diffusion language models by training a small 3-layer transformer on a frozen backbone to predict the logit residual between adjacent denoising steps, not the full next-step distribution.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
