Download code/tt_dsocr/tt/mk_moe/kernels/sampler_host.cpp from changh95/schift-ocr-1-beta: direct link, hf CLI and curl.
- Browser
- Download file 3.77 kB
-
https://huggingface.co/changh95/schift-ocr-1-beta/resolve/main/code/tt_dsocr/tt/mk_moe/kernels/sampler_host.cpp
- Command line
-
hf download hf://changh95/schift-ocr-1-beta/code/tt_dsocr/tt/mk_moe/kernels/sampler_host.cpp
-
curl -L -o sampler_host.cpp https://huggingface.co/changh95/schift-ocr-1-beta/resolve/main/code/tt_dsocr/tt/mk_moe/kernels/sampler_host.cpp
3.77 kB
| // SPDX-License-Identifier: Apache-2.0 | |
| // | |
| // Host build of the shared sampler (sampler.hpp) with a C ABI for ctypes (tt/mk_common/sampler_host.py): the | |
| // bit-exact reference the device samplers are tested against. mks_sample() emulates the distributed protocol: the | |
| // vocab is cut into slices of `slice` ids (one per participant), each slice is sorted locally (cap entries), the hub | |
| // merges / selects exactly as the device does. | |
| using namespace mks; | |
| extern "C" { | |
| uint32_t mks_key(uint32_t bits) { return f32_key(bits); } | |
| int64_t mks_key_fixed(uint32_t key) { return key_fixed(key); } | |
| uint64_t mks_scaled_gap(uint64_t d, uint32_t inv_t_bits) { return scaled_gap(d, inv_t_bits); } | |
| uint32_t mks_weight_x(uint64_t x) { return weight_q30_x(x); } | |
| uint32_t mks_weight(int64_t f0, int64_t fi, uint32_t inv_t_bits) { return weight_q30(f0, fi, inv_t_bits); } | |
| uint32_t mks_top_p_q24(uint32_t top_p_bits) { return top_p_q24(top_p_bits); } | |
| void mks_philox(const uint32_t* ctr, uint32_t k0, uint32_t k1, uint32_t* out) { | |
| uint32_t c[4] = {ctr[0], ctr[1], ctr[2], ctr[3]}; | |
| philox4x32_10(c, k0, k1); | |
| memcpy(out, c, 16); | |
| } | |
| uint32_t mks_u24(uint32_t seed_lo, uint32_t seed_hi, uint32_t frame, uint32_t codebook) { | |
| return philox_u24(seed_lo, seed_hi, frame, codebook); | |
| } | |
| void mks_talker_process(uint32_t* bits, uint32_t n, uint32_t id0, const uint32_t* seen, uint32_t step, | |
| uint32_t min_new, uint32_t eos, uint32_t pen_bits) { | |
| talker_process(bits, n, id0, seen, step, min_new, eos, pen_bits); | |
| } | |
| // local list of one slice (for device-protocol tests) | |
| uint32_t mks_local_sorted(const uint32_t* bits, uint32_t n, uint32_t id0, uint32_t* out_pairs, uint32_t cap) { | |
| std::vector<Entry> e(cap); | |
| const uint32_t c = local_sorted(bits, n, id0, e.data(), cap); | |
| for (uint32_t i = 0; i < c; ++i) { | |
| out_pairs[2 * i] = e[i].key; | |
| out_pairs[2 * i + 1] = e[i].id; | |
| } | |
| return c; | |
| } | |
| // whole distributed protocol on one score vector; info_out = [kept_k, kept_p, rank, z_lo, z_hi] | |
| uint32_t mks_sample(const uint32_t* bits, uint32_t n, uint32_t slice, uint32_t do_sample, uint32_t inv_t_bits, | |
| uint32_t top_k, uint32_t top_p_bits, uint32_t u24, uint32_t k_max, uint32_t cap, | |
| uint32_t* info_out) { | |
| const uint32_t p = (n + slice - 1) / slice; | |
| if (p > MKS_MAX_LISTS) { | |
| return INVALID; | |
| } | |
| std::vector<std::vector<Entry>> lists(p, std::vector<Entry>(cap)); | |
| std::vector<const Entry*> ptrs(p); | |
| std::vector<uint32_t> counts(p); | |
| for (uint32_t i = 0; i < p; ++i) { | |
| const uint32_t n0 = i * slice, ni = (n - n0) < slice ? (n - n0) : slice; | |
| counts[i] = local_sorted(bits + n0, ni, n0, lists[i].data(), cap); | |
| ptrs[i] = lists[i].data(); | |
| } | |
| Params prm{do_sample, inv_t_bits, top_k, top_p_bits}; | |
| Info info{}; | |
| static Merger m; | |
| m.init(ptrs.data(), counts.data(), p); | |
| uint32_t tok; | |
| if (full_vocab(prm)) { | |
| const int64_t f0 = key_fixed(m.top().key); | |
| uint64_t z = 0; | |
| for (uint32_t i = 0; i < p; ++i) { | |
| z += local_weight_sum(ptrs[i], counts[i], f0, inv_t_bits); | |
| } | |
| tok = select_full(m, z, u24, inv_t_bits, &info); | |
| } else { | |
| std::vector<Entry> merged(k_max); | |
| std::vector<uint32_t> w(k_max); | |
| const uint32_t c = merge_take(m, merged.data(), k_max); | |
| tok = select_topk(merged.data(), c, prm, u24, k_max, w.data(), &info); | |
| } | |
| if (info_out) { | |
| info_out[0] = info.kept_k; | |
| info_out[1] = info.kept_p; | |
| info_out[2] = info.rank; | |
| info_out[3] = (uint32_t)info.z; | |
| info_out[4] = (uint32_t)(info.z >> 32); | |
| } | |
| return tok; | |
| } | |
| } | |