MCPcopy Create free account
hub / github.com/bitsandbytes-foundation/bitsandbytes / dequantizeBlockwise8bitCpu

Function dequantizeBlockwise8bitCpu

csrc/cpu_ops.cpp:437–486  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

435
436template <typename T>
437void dequantizeBlockwise8bitCpu(
438 float* code, unsigned char* A, const float* absmax, T* out, long long blocksize, long long n
439) {
440 if (blocksize <= 0 || n <= 0)
441 return;
442 // 8-bit path
443 BNB_OMP_PARALLEL_FOR
444 for (long long block_idx = 0; block_idx < n; block_idx += blocksize) {
445 long long valid_items = (n - block_idx >= blocksize ? blocksize : n - block_idx);
446 long long block_end = block_idx + valid_items;
447 float scale = absmax[block_idx / blocksize];
448#if defined(_M_ARM64) || defined(__aarch64__)
449 {
450 float32x4_t vscale = vdupq_n_f32(scale);
451 long long i = block_idx;
452 for (; i + 4 <= block_end; i += 4) {
453 float tmp[4] = {code[A[i]], code[A[i + 1]], code[A[i + 2]], code[A[i + 3]]};
454 float32x4_t v = vmulq_f32(vld1q_f32(tmp), vscale);
455 if constexpr (std::is_same<T, float>::value)
456 vst1q_f32(reinterpret_cast<float*>(out + i), v);
457 else if constexpr (std::is_same<T, bf16_t>::value)
458 neon_f32_to_bf16x4(v, out + i);
459 else
460 neon_f32_to_fp16x4(v, out + i);
461 }
462 for (; i < block_end; ++i) {
463 float v = code[A[i]] * scale;
464 if constexpr (std::is_same<T, bf16_t>::value)
465 out[i] = float_to_bf16(v);
466 else if constexpr (std::is_same<T, fp16_t>::value)
467 out[i] = float_to_fp16(v);
468 else
469 out[i] = static_cast<T>(v);
470 }
471 }
472#else
473#pragma omp simd
474 for (long long i = block_idx; i < block_end; ++i) {
475 float v = code[A[i]] * scale;
476 if constexpr (std::is_same<T, bf16_t>::value) {
477 out[i] = float_to_bf16(v);
478 } else if constexpr (std::is_same<T, fp16_t>::value) {
479 out[i] = float_to_fp16(v);
480 } else {
481 out[i] = static_cast<T>(v);
482 }
483 }
484#endif
485 }
486}
487
488// Prevent GCC/Clang from emitting EVEX-encoded AVX512 instructions in plain scalar code.
489// The global -mavx512vl flag can cause GCC to fold broadcasts into EVEX encoding (e.g. vmulps {1to4})

Callers

nothing calls this directly

Calls 4

neon_f32_to_bf16x4Function · 0.85
neon_f32_to_fp16x4Function · 0.85
float_to_bf16Function · 0.85
float_to_fp16Function · 0.85

Tested by

no test coverage detected