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

Method weight_bytes_for

engine/src/core/native_dense.cpp:244–310  ·  view source on GitHub ↗

The byte walk `load` does for the layers [lb, le), without the allocations: the same filters in the same order, reading the GGUF headers and the canonical table only, so it needs no device. A later stage's projections are not on the card when the split search prices that stage, and `load` runs one cudaMalloc per matrix (#1238). The two have to stay in step. A repeated name is skipped rather tha

Source from the content-addressed store, hash-verified

242// on the card when the split search prices that stage, and `load` runs one cudaMalloc per matrix (#1238). The two
243// have to stay in step. A repeated name is skipped rather than refused (`load` refuses it).
244bool NativeDense::weight_bytes_for(const std::vector<std::string>& shards, WeightTable& table, bool include_ple_key,
245 int64_t lb, int64_t le, uint64_t& out, std::string& err) {
246 out = 0;
247 try {
248 std::set<std::string> seen, hc_seen;
249 uint64_t total = 0;
250 auto in_stage = [&](const std::string& name) { // a blk.<l>. tensor of layers [lb, le); the rest belongs to every stage
251 if (name.rfind("blk.", 0) != 0) return true;
252 const long l = std::strtol(name.c_str() + 4, nullptr, 10);
253 return l >= lb && l < le;
254 };
255 for (const auto& path : shards) {
256 strata::GgufFile gguf(path);
257 for (const auto& tensor : gguf.tensors()) {
258 // STRATA_HC_Q8: `load` copies the GGUF's Q8_0 hyper-connection rows of its layers (and the final mixer's)
259 // to the card, one cudaMalloc each, and the pack's bf16 rows get a Q8_0 copy below: priced as `load` makes them
260 if (hc_q8_env() && hc_q8_name(tensor.name) && tensor.shape.size() == 2) {
261 const bool f32_inject = hc_q8_inject_env() && tensor.type == 0 && tensor.name.ends_with("_inject.weight");
262 if ((tensor.type == 8 || f32_inject) && (!hc_q8_stage_local() || in_stage(tensor.name)) &&
263 hc_seen.insert(tensor.name).second) {
264 const auto found = table.table_.find(tensor.name);
265 if (found != table.table_.end() && found->second.ne0 % 32 == 0 &&
266 tensor.shape[0] == (uint64_t) found->second.ne0 && tensor.shape[1] == (uint64_t) found->second.ne1)
267 total += alloc_bytes((uint64_t) found->second.ne0 / 32 * 34 * (uint64_t) found->second.ne1);
268 }
269 }
270 if (!eligible(tensor, include_ple_key)) continue;
271 if (tensor.name.rfind("blk.", 0) == 0 && tensor.name.find("ple") == std::string::npos) { // `load` takes the PLE key on every stage
272 const long l = std::strtol(tensor.name.c_str() + 4, nullptr, 10);
273 if (l < lb || l >= le) continue;
274 }
275 if (!seen.insert(tensor.name).second) continue;
276 auto found = table.table_.find(tensor.name);
277 if (found == table.table_.end()) {
278 err = "native dense: tensor absent from canonical table: " + tensor.name;
279 return false;
280 }
281 const auto& ref = found->second;
282 if (bf16_as_q8_0(tensor, ref)) { // #4741
283 total += alloc_bytes((uint64_t) ref.ne0 / 32 * 34 * (uint64_t) ref.ne1);
284 continue;
285 }
286 if (!strata::kernels::native_mmvq_supported(tensor.type)) continue;
287 if (tensor.name == "blk.1.ple_key.weight" && !ref.quantized()) continue;
288 if (!ref.quantized() || tensor.shape.size() != 2 ||
289 ref.ne0 <= 0 || ref.ne0 > INT_MAX || ref.ne1 <= 0 || ref.ne1 > INT_MAX ||
290 tensor.shape[0] != (uint64_t) ref.ne0 || tensor.shape[1] != (uint64_t) ref.ne1) {
291 err = "native dense: incompatible matrix " + tensor.name;
292 return false;
293 }
294 total += alloc_bytes(strata::kernels::native_mmvq_weight_bytes(tensor.type, (int) ref.ne0, (int) ref.ne1));
295 }
296 }
297 if (hc_q8_env()) // a bf16 pack: `load` quantizes the layers' down / up projections that the GGUF does not carry as Q8_0
298 for (const auto& [name, ref] : table.table_) {
299 if (name.rfind("blk.", 0) != 0 || !in_stage(name) || !hc_q8_name(name) || hc_seen.count(name) ||
300 !(name.ends_with("_down.weight") || name.ends_with("_up.weight")))
301 continue;

Callers

nothing calls this directly

Calls 15

hc_q8_inject_envFunction · 0.85
hc_q8_stage_localFunction · 0.85
alloc_bytesFunction · 0.85
bf16_as_q8_0Function · 0.85
quantizedMethod · 0.80
hc_q8_envFunction · 0.70
hc_q8_nameFunction · 0.70
eligibleFunction · 0.70
native_mmvq_supportedFunction · 0.50
native_mmvq_weight_bytesFunction · 0.50
stringFunction · 0.50
tensorsMethod · 0.45

Tested by

no test coverage detected