\brief Sample the token \param x the input buffer \return the sampled token
| 348 | /// \param x the input buffer |
| 349 | /// \return the sampled token |
| 350 | int Sampler::sample(buffer<bf16>& x) { |
| 351 | // PRNG is seeded once at construction, no per-call seeding needed |
| 352 | |
| 353 | // COPY FROM `x` → `this->logits[]` |
| 354 | #if USEAVX2 |
| 355 | const int simd_width = 8; |
| 356 | int i = 0; |
| 357 | for (; i <= in_features - simd_width; i += simd_width) { |
| 358 | __m128i bf16_vals_x = _mm_loadu_si128( |
| 359 | (__m128i*)&x[i] |
| 360 | ); |
| 361 | __m256 fp32_vals_x = bf16o_fp32(bf16_vals_x); |
| 362 | _mm256_storeu_ps(&this->logits[i], fp32_vals_x); |
| 363 | } |
| 364 | for (; i < in_features; i++) { |
| 365 | this->logits[i] = x[i]; |
| 366 | } |
| 367 | #else |
| 368 | for (int i = 0; i < in_features; i++) { |
| 369 | this->logits[i] = x[i]; |
| 370 | } |
| 371 | #endif |
| 372 | |
| 373 | sampler_penalty_apply_sparse(); |
| 374 | sampler_topk_apply(this->top_k); |
| 375 | if (this->use_optimized_sampling) { |
| 376 | softmax_with_topp_minp(this->top_p, this->min_p); |
| 377 | sampler_temp_apply(this->temperature); |
| 378 | softmax_inplace(); |
| 379 | } else { |
| 380 | // Legacy behavior is kept as the default for output compatibility. |
| 381 | softmax_inplace(); |
| 382 | sampler_topp_apply(this->top_p); |
| 383 | softmax_inplace(); |
| 384 | sampler_minp_apply(this->min_p); |
| 385 | sampler_temp_apply(this->temperature); |
| 386 | softmax_inplace(); |
| 387 | } |
| 388 | |
| 389 | int sampled_index = sample_from_probs(); |
| 390 | ring_buffer_update_sparse(sampled_index); |
| 391 | |
| 392 | return sampled_index; |
| 393 | } |
no test coverage detected