MCPcopy Create free account
hub / github.com/Niko1221/Strata / bf16

Method bf16

engine/src/split/src/model.cpp:415–435  ·  view source on GitHub ↗

Q8_0 -> bf16, the whole tensor; F32 -> bf16 likewise

Source from the content-addressed store, hash-verified

413 }
414 // Q8_0 -> bf16, the whole tensor; F32 -> bf16 likewise
415 uint16_t* bf16(const Tensor& t) {
416 const size_t n = (size_t) (t.ne[0] * t.ne[1] * t.ne[2] * t.ne[3]);
417 std::vector<uint16_t> h(n);
418 if (t.type == T_F32) {
419 const float* f = (const float*) t.data;
420 for (size_t i = 0; i < n; ++i) h[i] = f32_to_bf16(f[i]);
421 } else if (t.type == T_Q8_0) {
422 for (size_t b = 0; b < n / 32; ++b) {
423 const uint8_t* blk = t.data + b * 34;
424 uint16_t dh;
425 std::memcpy(&dh, blk, 2);
426 const float d = f16_to_f32(dh);
427 for (int j = 0; j < 32; ++j) h[b * 32 + j] = f32_to_bf16(d * (float) (int8_t) blk[2 + j]);
428 }
429 } else if (t.type == T_BF16) {
430 std::memcpy(h.data(), t.data, n * 2);
431 } else {
432 throw std::runtime_error(t.name + ": bf16 conversion supports F32, Q8_0 and BF16");
433 }
434 return put<uint16_t>(h.data(), h.size() * 2);
435 }
436 // a dense matrix for q8s_gemv: Q8_0 slices move to the split layout (other types stay as they are)
437 DW dense(DW w) {
438 if (w.type != T_Q8_0) return w;

Callers 1

load_cardFunction · 0.45

Calls 5

memcpyFunction · 0.85
f32_to_bf16Function · 0.70
f16_to_f32Function · 0.70
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected