mirror of
https://github.com/kvcache-ai/ktransformers.git
synced 2026-10-16 21:01:14 +08:00
22 lines
742 B
C++
22 lines
742 B
C++
#include "bf16_sumsq.h"
|
|
|
|
#include <bit>
|
|
#include <cstdint>
|
|
#include <stdexcept>
|
|
|
|
double bf16_sumsq_ptr(std::uintptr_t data_ptr, std::int64_t numel) {
|
|
if (numel < 0 || (numel != 0 && data_ptr == 0)) {
|
|
throw std::invalid_argument("BF16 sum of squares requires a valid pointer and non-negative length");
|
|
}
|
|
|
|
const auto* values = reinterpret_cast<const std::uint16_t*>(data_ptr);
|
|
double total = 0.0;
|
|
#pragma omp parallel for schedule(static) reduction(+ : total)
|
|
for (std::int64_t index = 0; index < numel; ++index) {
|
|
const auto bits = static_cast<std::uint32_t>(values[index]) << 16;
|
|
const float value = std::bit_cast<float>(bits);
|
|
total += static_cast<double>(value) * static_cast<double>(value);
|
|
}
|
|
return total;
|
|
}
|