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

Function make_weights

engine/src/quant/selfcheck.cpp:32–72  ·  view source on GitHub ↗

A random weight row of n elements whose scales are powers of two, so every float operation of every contract is exact.

Source from the content-addressed store, hash-verified

30
31// A random weight row of n elements whose scales are powers of two, so every float operation of every contract is exact.
32std::vector<uint8_t> make_weights(Type t, int n, Rng& r) {
33 const TypeInfo* ti = type_info(t);
34 const size_t nb = (size_t) (n / ti->blck);
35 std::vector<uint8_t> row(nb * ti->bytes);
36 for (uint8_t& b : row) b = (uint8_t) r.next();
37 for (size_t i = 0; i < nb; ++i) {
38 switch (t) {
39 case Type::Q2_0: blk<block_q2_0>(row, i)->d = kH1; break;
40 case Type::Q4_0: blk<block_q4_0>(row, i)->d = kH1; break;
41 case Type::Q4_1: blk<block_q4_1>(row, i)->d = kH1, blk<block_q4_1>(row, i)->m = kH05; break;
42 case Type::Q5_0: blk<block_q5_0>(row, i)->d = kH1; break;
43 case Type::Q5_1: blk<block_q5_1>(row, i)->d = kH1, blk<block_q5_1>(row, i)->m = kH05; break;
44 case Type::Q8_0: blk<block_q8_0>(row, i)->d = kH05; break;
45 case Type::IQ4_NL: blk<block_iq4_nl>(row, i)->d = kH1; break;
46 case Type::MXFP4: blk<block_mxfp4>(row, i)->e = 127; break;
47 case Type::NVFP4: for (int j = 0; j < 4; ++j) blk<block_nvfp4>(row, i)->d[j] = 0x38; break;
48 case Type::Q2_K: blk<block_q2_K>(row, i)->d = kH1, blk<block_q2_K>(row, i)->dmin = kH05; break;
49 case Type::Q3_K: blk<block_q3_K>(row, i)->d = kH1; break;
50 case Type::Q4_K: blk<block_q4_K>(row, i)->d = kH1, blk<block_q4_K>(row, i)->dmin = kH05; break;
51 case Type::Q5_K: blk<block_q5_K>(row, i)->d = kH1, blk<block_q5_K>(row, i)->dmin = kH05; break;
52 case Type::Q6_K: blk<block_q6_K>(row, i)->d = kH1; break;
53 case Type::IQ2_XXS: blk<block_iq2_xxs>(row, i)->d = kH1; break;
54 case Type::IQ2_XS: blk<block_iq2_xs>(row, i)->d = kH1; break;
55 case Type::IQ2_S: blk<block_iq2_s>(row, i)->d = kH1; break;
56 case Type::IQ3_XXS: blk<block_iq3_xxs>(row, i)->d = kH1; break;
57 case Type::IQ3_S: blk<block_iq3_s>(row, i)->d = kH1; break;
58 case Type::IQ1_S: blk<block_iq1_s>(row, i)->d = kH1; break;
59 case Type::IQ4_XS: blk<block_iq4_xs>(row, i)->d = kH1; break;
60 case Type::IQ1_M: { // the fp16 scale is spread over the top nibbles of the four 16-bit scale words: 0x3C00 = nibbles 0, 0, C, 3
61 uint16_t* sc = reinterpret_cast<uint16_t*>(blk<block_iq1_m>(row, i)->scales);
62 sc[0] = (uint16_t) ((sc[0] & 0x0FFF) | 0x0000);
63 sc[1] = (uint16_t) ((sc[1] & 0x0FFF) | 0x0000);
64 sc[2] = (uint16_t) ((sc[2] & 0x0FFF) | 0xC000);
65 sc[3] = (uint16_t) ((sc[3] & 0x0FFF) | 0x3000);
66 break;
67 }
68 default: break;
69 }
70 }
71 return row;
72}
73
74// A random activation row: values in [-3, 3], scale 1.
75std::vector<uint8_t> make_act(Type act, int n, Rng& r) {

Callers 1

check_dotFunction · 0.70

Calls 2

type_infoFunction · 0.70
nextMethod · 0.45

Tested by

no test coverage detected