MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / dot

Function dot

pocs/vdot/vdot.cpp:61–85  ·  view source on GitHub ↗

"Scalar" dot product between the quantized vector x and float vector y

Source from the content-addressed store, hash-verified

59
60// "Scalar" dot product between the quantized vector x and float vector y
61inline double dot(int n, const block_q4_0* x, const float* y) {
62 const static float kValues[16] = {-8.f, -7.f, -6.f, -5.f, -4.f, -3.f, -2.f, -1.f, 0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f};
63 constexpr uint32_t kMask1 = 0x0f0f0f0f;
64 uint32_t u1, u2;
65 auto q1 = (const uint8_t*)&u1;
66 auto q2 = (const uint8_t*)&u2;
67 double sum = 0;
68 for (int i=0; i<n; ++i) {
69 float d = x->d;
70 auto u = (const uint32_t*)x->qs;
71 float s = 0;
72 for (int k=0; k<4; ++k) {
73 u1 = u[k] & kMask1;
74 u2 = (u[k] >> 4) & kMask1;
75 s += y[0]*kValues[q1[0]] + y[1]*kValues[q2[0]] +
76 y[2]*kValues[q1[1]] + y[3]*kValues[q2[1]] +
77 y[4]*kValues[q1[2]] + y[5]*kValues[q2[2]] +
78 y[6]*kValues[q1[3]] + y[7]*kValues[q2[3]];
79 y += 8;
80 }
81 sum += s*d;
82 ++x;
83 }
84 return sum;
85}
86// Alternative version of the above. Faster on my Mac (~45 us vs ~55 us per dot product),
87// but about the same on X86_64 (Ryzen 7950X CPU).
88inline double dot3(int n, const block_q4_0* x, const float* y) {

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected