How does finite scalar quantization (FSQ) work for speech tokens?
- Authors
- Name
- Amit Shekhar
- Published on
Finite Scalar Quantization (FSQ) turns speech into tokens by squeezing each sound frame into a few numbers and rounding each number to one of a few fixed values, and the combination of those values is the token. It needs no learned list of codes, so almost every possible token gets used.
In this blog, we will learn about how finite scalar quantization (FSQ) works for speech tokens. We will also see why speech models need tokens at all, where the older vector quantization approach fails, how FSQ bounds and rounds a few numbers into a single token ID, how the model still learns through rounding, how CosyVoice 2 uses FSQ for its speech tokens, and how VQ and FSQ differ.
We will cover the following:
- Why do speech models need tokens?
- What is quantization?
- The older way: Vector Quantization
- The problem with Vector Quantization
- What is Finite Scalar Quantization?
- How FSQ works step by step
- How the rounded numbers become a token ID
- How the model learns through rounding
- How CosyVoice 2 uses FSQ for speech tokens
- VQ vs FSQ
I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
I teach AI and Machine Learning at Outcome School.
Let's get started.
Why do speech models need tokens?
An LLM (Large Language Model) works with tokens. A token is a small piece of input, like a word or a part of a word, and each token has an ID number from a fixed list. The LLM reads a sequence of token IDs and predicts the next token ID.
Now, many speech models, for example text-to-speech models, are built on top of LLMs. So, the question is: how can an LLM read or generate speech?
Speech is a sound wave. It is a long, smooth stream of numbers, often 16,000 or 24,000 numbers for every second of audio. An LLM cannot pick the "next token" from an endless range of smooth values. It needs a fixed list of choices.
So, we first break the audio into short frames, for example 25 frames per second. A neural network called an encoder turns each frame into a list of numbers that describes that frame. This list of numbers is called a vector. For example:
frame 1 -> [0.83, -1.92, 0.10, 2.45, ...]
frame 2 -> [0.79, -1.40, 0.31, 2.02, ...]
These values can be any number, like 0.83 or -1.92. We need to convert each vector into one token ID from a fixed list. This conversion is called quantization.
What is quantization?
Quantization is the process of mapping a value that can be anything into one value from a small, fixed set of choices.
The best way to learn this is by taking an example.
Let's say we have a thermometer that shows 22.37 degrees. If we round it to the nearest whole number, we get 22. We have quantized the value. Instead of infinite possible readings, we now have only whole numbers.
For speech tokens, we do the same thing, but for a whole vector instead of a single number. The output is a token ID that an LLM can read and predict.
Now, let's see the older way to do this.
The older way: Vector Quantization
Vector Quantization (VQ) keeps a list of learned vectors called a codebook. Each vector in the codebook is called a code, and its position in the list is the token ID.
For example, a codebook can have 4,096 codes:
code 0 -> [0.12, -0.50, 1.30, ...]
code 1 -> [1.80, 0.20, -0.70, ...]
...
code 4095 -> [-0.90, 2.10, 0.40, ...]
When a new frame vector comes in, VQ compares it with every code in the codebook and picks the closest one. The ID of that closest code becomes the token.
Here, the codebook is not written by hand. It is learned during training, together with the encoder. The codes move little by little so that they sit close to the vectors that the encoder produces.
We have a detailed blog on low frame-rate codecs that explains how audio codecs stack several codebooks for each frame using Residual Vector Quantization.
This works, but it comes with problems.
The problem with Vector Quantization
The biggest problem with VQ is called codebook collapse.
During training, some codes get picked early. Because they get picked, they get updated and move closer to the data. Because they move closer, they get picked again. Meanwhile, other codes are never picked, so they are never updated, and they stay unused.
So, we may have a codebook with 4,096 codes, but only a small part of them is ever used. The rest is wasted space. Fewer used codes means fewer different sounds that the tokens can tell apart.
In the CosyVoice 2 paper, a VQ tokenizer with 4,096 codes used only 963 of them, which is about 23%.
To fight this, VQ training often needs extra tricks. It adds extra loss terms, which are extra error scores during training, to pull the encoder output and the codes toward each other. It resets dead codes, which are codes that are never picked. And it needs careful tuning. This makes training complex and fragile.
Planning your move into AI?
No matter which tech domain you work in today, with your effort, you can transition to these roles:
- AI Engineer
- Forward Deployed Engineer
- AI Agent Engineer
- Harness Engineer
- Voice AI Engineer
- AI Architect
- LLM Engineer
- LLM Inference Engineer
- Gen AI Engineer
- MLOps Engineer
- ML Engineer
- Deep Learning Engineer
Our AI and Machine Learning Program at Outcome School takes you there.
Now, the question is: can we get tokens without learning a codebook at all? The answer is yes. Here comes Finite Scalar Quantization into the picture.
What is Finite Scalar Quantization?
Finite Scalar Quantization (FSQ) is a quantization method that squeezes a vector down to a few numbers, limits each number to a fixed range, and rounds each number to one of a few allowed values. The combination of the rounded values is the token.
FSQ was introduced by researchers at Google in the 2023 paper "Finite Scalar Quantization: VQ-VAE Made Simple".
Let's understand the two words that describe how it works:
- Scalar means a single number. FSQ works on each number of the vector on its own, not on the whole vector at once.
- Finite means each number can take only a limited set of values, for example -1, 0, or 1.
Let's say we have a switch with three positions: down, middle, and up. If we have 8 such switches, every unique combination of switch positions is a different setting. The total number of settings is 3 * 3 * 3 * 3 * 3 * 3 * 3 * 3 = 3^8 = 6,561.
FSQ works exactly like this. Each number is a switch. The rounded value is the switch position. The full combination is the token. There is no list of codes to learn, because the list is simply "every possible combination". This is called an implicit codebook. Implicit means the codebook is not stored anywhere. It is defined by the rule itself.
How FSQ works step by step
Now, let's see the steps that FSQ follows for one frame.
Step 1: We project the vector down to a few numbers.
The encoder gives a long vector, for example with hundreds of numbers. FSQ first uses a small learned layer to project it down to a few numbers, for example 8. For the sake of understanding, let's use only 3 numbers in our example:
after projection -> [2.31, 0.04, -0.87]
Step 2: We bound each number to a fixed range.
These numbers can still be anything, like 2.31 or -15.6. So, FSQ passes each number through a squashing function that pulls every value into a fixed range. A common choice is tanh, which turns any number into a value between -1 and 1. Big positive numbers come close to 1, big negative numbers come close to -1, and numbers near 0 stay near 0.
after bounding -> [0.98, 0.04, -0.70]
Step 3: We round each number to the nearest allowed value.
Let's say each number has 3 allowed values: -1, 0, and 1. Now, we round each bounded number to the nearest one:
0.98 -> 1
0.04 -> 0
-0.70 -> -1
after rounding -> [1, 0, -1]
Here, we can see that every number now has only 3 possible values. With 3 numbers and 3 values each, the total number of possible tokens is 3 * 3 * 3 = 27.
The number of allowed values per number is called the levels. Each number can have its own level count. For example, the FSQ paper uses levels like [8, 5, 5, 5], which gives 8 * 5 * 5 * 5 = 1,000 possible tokens. For more levels, FSQ stretches the bounded range before rounding. For example, with 5 levels, the range becomes -2 to 2, and the rounded values are -2, -1, 0, 1, and 2.
Step 4: We project back up.
The rest of the model expects a full-size vector. So, FSQ uses another small learned layer to project the rounded numbers back to the original size. This rounded and projected vector is what the next part of the model uses.
Let's see the full flow for our example in a diagram:
frame vector from the encoder (hundreds of numbers)
↓
project down
↓
[2.31, 0.04, -0.87]
↓
bound with tanh
↓
[0.98, 0.04, -0.70]
↓
round
↓
[1, 0, -1]
↓
project back up
↓
full-size vector for the rest of the model
This is how FSQ turns one frame into a set of rounded numbers.
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
How the rounded numbers become a token ID
Now, we have the rounded numbers. But an LLM needs one single number as the token ID, not a list like [1, 0, -1]. So, we must convert the list into one number.
The idea is the same as reading digits in a number system. In our normal decimal system, the number 352 means 3 hundreds, 5 tens, and 2 ones. Each position to the left is worth 10 times more than the one on its right.
With FSQ, each position has 3 values, so we use base 3. Let's see the steps for [1, 0, -1].
First, we shift the values so that they start from 0. We add 1 to each value:
[1, 0, -1] -> [2, 1, 0]
Now, each digit is 0, 1, or 2. Then, we read it as a base-3 number. Here, we treat the first position as the ones place. So, the first position is worth 1, the second is worth 3, and the third is worth 9:
token ID = 2 * 1 + 1 * 3 + 0 * 9
= 2 + 3 + 0
= 5
So, the frame becomes token 5.
Here, we can notice two things:
- Every combination gives a different ID, from 0 to 26. No two combinations collide.
- We can go back from the ID to the rounded numbers by reversing the steps.
This is how FSQ produces token IDs without any codebook table.
How the model learns through rounding
Now, the next big question is: how does the model learn if there is a rounding step in the middle?
A neural network learns by checking how a small change in each of its internal numbers changes the final error, and then nudging the numbers in the right direction. But rounding breaks this. If we change 0.04 to 0.05, the rounded value is still 0. The change has no effect, so the model gets no signal about which direction to move.
So, here comes the straight-through estimator to the rescue.
It works like below:
- In the forward pass, when the model produces the output, we use the real rounded value.
- In the backward pass, when the model learns, we pretend the rounding step was not there and pass the learning signal straight through, as if the rounded value were equal to the value before rounding.
In code, this trick is often written in a single line as below:
h_rounded = h + (h.round() - h).detach()
Here, detach() hides the part inside the brackets from learning. So, in the forward pass, h_rounded is equal to h.round(). In the backward pass, the model only sees h, so the learning signal passes straight through.
This way, the encoder still gets a useful signal to move its numbers up or down, and the rounding still happens when producing tokens. VQ also uses this trick, but VQ needs extra loss terms on top. FSQ does not.
Since there is no learned codebook, there are no dead codes. Any combination can be reached by moving the numbers a little. This is why FSQ gives very high codebook usage without any special tricks.
To learn Neural Networks, Backpropagation, and Gradient Descent from the ground up, check out our AI and Machine Learning Program at Outcome School.
How CosyVoice 2 uses FSQ for speech tokens
Now, let's take a real use case in speech models.
CosyVoice 2 is a text-to-speech model from Alibaba. It uses an LLM to generate speech tokens from text, and then turns those tokens back into audio.
We have a detailed blog on streaming TTS that explains how CosyVoice 2 mixes text tokens and speech tokens so that it can start speaking early.
For its speech tokens, CosyVoice 2 takes the encoder of a speech recognition model called SenseVoice-Large and inserts an FSQ module inside it. The steps are as below:
- The audio goes through the first part of the encoder and becomes a sequence of vectors.
- Each vector is projected down to 8 numbers.
- Each number is bounded and rounded to one of 3 values: -1, 0, or 1.
- The 8 rounded values are read as a base-3 number to give one token ID.
- The rounded values are projected back up and continue through the rest of the encoder.
With 8 numbers and 3 values each, the total number of tokens is 3^8 = 6,561. The tokenizer produces 25 tokens per second of speech.
An open-source PyTorch version of this tokenizer does the FSQ part in a few lines. Here, I have kept only the important lines of the code and removed other lines for brevity:
h = project_down(x) # 8 numbers per frame
h = h.tanh() # bound each number between -1 and 1
h = h.round() + 1 # round to -1, 0, 1 and shift to 0, 1, 2
powers = 3 ** torch.arange(8) # 1, 3, 9, 27, ...
token_id = (h * powers).sum(-1) # read as a base-3 number
Here, we can see that the code follows exactly the steps we have learned: project down, bound with tanh, round, shift by 1, and read the digits as a base-3 number.
Because the tokenizer is trained as part of a speech recognition model, the tokens learn to carry what is being said. So, these tokens are often called semantic speech tokens.
Earlier, we saw that the VQ tokenizer in the CosyVoice 2 paper used only about 23% of its 4,096 codes. With FSQ, the tokenizer used 100% of its 6,561 codes.
Similarly, X-codec2, a codec that turns audio into tokens and back, is used by the Llasa text-to-speech model. It uses a single layer of FSQ to turn speech into one stream of tokens that an LLM can model directly.
Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
VQ vs FSQ
Let me tabulate the differences between VQ and FSQ for your better understanding.
| Point | VQ | FSQ |
|---|---|---|
| Codebook | Learned table of code vectors | No table, every combination is a code |
| How a token is found | Find the nearest code in the table | Bound and round each number |
| Codebook usage | Often low because of collapse | Close to 100% |
| Extra training loss | Needed to pull codes and data closer | Not needed |
| Training stability | Needs tricks like resetting codes | Simple and stable |
| Codebook size | Number of codes in the table | Product of the levels, like 3^8 = 6,561 |
Now we must have understood how finite scalar quantization (FSQ) works for speech tokens, how it bounds and rounds a few numbers, how those numbers become a token ID, and why speech models like CosyVoice 2 use it.
Frequently Asked Questions
What is an implicit codebook in FSQ?
An implicit codebook is a codebook that is not stored anywhere. In FSQ, the list of codes is simply every possible combination of the rounded values, so it is defined by the rule itself. For example, 8 numbers with 3 allowed values each give 3^8 = 6,561 possible tokens, and there is no table of codes to learn.
What is the straight-through estimator?
The straight-through estimator is a trick that lets a model learn through a rounding step. In the forward pass, the model uses the real rounded value. In the backward pass, it pretends the rounding was not there and passes the learning signal straight through. In code, it is often written as h + (h.round() - h).detach().
Who introduced Finite Scalar Quantization?
Finite Scalar Quantization was introduced by researchers at Google in the 2023 paper "Finite Scalar Quantization: VQ-VAE Made Simple". The paper uses levels like [8, 5, 5, 5], which gives 8 * 5 * 5 * 5 = 1,000 possible tokens. Today, speech models like CosyVoice 2 use FSQ to make their speech tokens.
How many of its codes does the CosyVoice 2 tokenizer use?
With FSQ, the CosyVoice 2 tokenizer used 100% of its 6,561 codes. In the same paper, a VQ tokenizer with 4,096 codes used only 963 of them, which is about 23%. This happens because FSQ has no learned codebook, so there are no dead codes, and any combination can be reached by moving the numbers a little.
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
