Note:
- To calculate the prefill we need to know our max inference time in samples rounded to whole callbacks.
- My worst case: 18 ms
- Click: in samples, at 44.1 kHz: 18 ms are 794 samples
- Whole callbacks, because the audio thread only picks results up once per callback
- Click: rounded up to whole 512-sample callbacks: 1024, two callbacks
- Click: You don't need to calculate this yourself, it's given at the top of `prepare`.
---
Task: main.cpp
## The task
Fill in the TODOs in `steps/06_threading/exercise/main.cpp`
```bash
cmake --build --preset release --target step06_exercise
./build/release/bin/step06_exercise
```
3The worker thread
model
✓ the model's stream, the prefill late
Note:
- First: make sure your number from step 4 into `k_max_inference_ms` in `steps/common/inference_time.h`, if not done yet
- Then define the prefill in `prepare`. This is step 3's, plus the max inference time
- Set up the audio thread.
- Then set up the worker thread.
- `main.cpp` in steps/06_threading/exercise, build commands on the slide
- The biggest task so far, 10 minutes
- Solution is in the `solution` folder
---
Solution
## TODO 1: The prefill
```cpp
m_prefill = «(m_model.m_input_size - host_block_size)» // <- solution: step 3's prefill
+ «rounded_max_inference_samples»; // plus the worker's time
// given: the sizes, the silence goes in, and the worker thread starts
m_input = ThreadSafeRingBuffer(m_model.m_input_size);
m_output = ThreadSafeRingBuffer(rounded_max_inference_samples + m_model.m_input_size);
m_worker.start([this] { process_model(); });
```
Note:
- The prefill is the sum of:
- the same prefill as in step 3, so the buffer adaptation: one model block minus one host block
- in our asynchronous set up we also need to take inference time into account, so we need an to add `rounded_max_inference_samples`: the block still has to go through the model.
- The sizes of the ring buffers are given: `m_input` one model block, `m_output` the rounded max inference samples plus one model block, so the prefill fits
---
Solution
## TODO 2: The audio thread
```cpp
for (size_t i = 0; i < num_samples; ++i) {
const bool pushed = «m_input.try_push(samples[i])»; // <- solution
if (!pushed) { m_unpushed += num_samples - i; break; } // given: count the rest
}
const size_t zeros = std::min(m_unpushed, num_samples); // given: zeros for those
std::fill_n(samples, zeros, 0.0f);
m_unpushed -= zeros;
while (m_debt > 0) { // pay the debt first
if (m_output.try_pop(late)) { --m_debt; } else { break; } // given
}
for (size_t i = zeros; i < num_samples; ++i) {
const bool popped = m_debt == 0 && «m_output.try_pop(samples[i])»; // <- solution
if (!popped) { // late: the rest is zeros, all owed
std::fill_n(samples + i, num_samples - i, 0.0f);
m_debt += num_samples - i; // given
break;
}
}
```
Note:
- Same job as step 3's `process_block`, minus the engine
- host samples are pushed into `m_input` with try_push, one sample at a time
- model samples popped out of `m_output`, try_pop, one sample at a time
- If `try_push` fails: The rest of the block is lost. We count it in `m_unpushed` and write as many zeros into the output buffer.
- If `try_pop` fails: The model is late. We need to catch up on that time later, so we count it in `m_debt`
- This makes sure we stay time aligned, even if the worker is late.
---
Solution
## TODO 3: The worker thread
```cpp
// in process_model(), called over and over by m_worker
for (size_t i = 0; i < m_model.m_input_size; ++i) {
while («!m_input.try_pop(m_model_block[i])») { // <- solution: spin
if (!m_worker.running()) { return; } // given
}
}
m_engine.process(m_model_block.data(), m_model.m_input_size,
m_state.data(), m_model.m_state_size);
for (size_t i = 0; i < m_model.m_input_size; ++i) {
while («!m_output.try_push(m_model_block[i])») { // <- solution: spin
if (!m_worker.running()) { return; } // given
}
}
```
Note:
- On the worker thread we process our samples
- We try to pop samples from the input ring buffer all the time without waiting until we have one model block.
- Once we have one model block we process the samples and push the results onto the output ring buffer.
- Be careful, this is not the best way to do it. This spins on one CPU core until a full model block has arrived, wasting that core while it waits; part 2 does it better
---
Done
## The audio thread is real-time safe
The model runs beside it.
Note:
- So we keep the same stream as step 3, but the engine is off the audio thread
- Audio thread is real-time safe, the model runs beside it