| 827 | } |
| 828 | |
| 829 | bool Window::init(const strata::core::ModelGeometry& g, strata::core::SessionState& ss, const std::string& shard1, |
| 830 | int max_t, int64_t max_cells, std::string& err) { |
| 831 | ss_ = &ss; |
| 832 | main_ss_ = &ss; |
| 833 | max_t_ = std::min(max_t, MAXT); |
| 834 | max_cells_ = max_cells; |
| 835 | // The 19 fields the CUDA guard compares with the compiled shape (strata::geometry_unsupported, + rms_eps); the two it leaves free are |
| 836 | // checked here: the routed experts per layer (the router kernels take NL = 16, i.e. 512, and 8, i.e. 256 for the expert-pruned |
| 837 | // Coder) and the experts per token (the kernels are built for TOPK). |
| 838 | const bool experts_ok = g.n_expert == NEXP || (NEXP == 512 && g.n_expert == 256); |
| 839 | if (!strata::geometry_unsupported(g).empty() || !experts_ok || g.n_expert_used != TOPK) { |
| 840 | err = "metal window: the geometry differs from Qwen3.8-Flash-Next's"; |
| 841 | return false; |
| 842 | } |
| 843 | nexp_ = (int) g.n_expert; // 512, or 256 (the expert-pruned Coder); the routers' logits rows keep a 512-float stride either way |
| 844 | eps_ = 1e-6f; |
| 845 | const double t0 = now_ms(); |
| 846 | if (const long rp = repack_pid_running(shard1); rp > 0 && !(std::getenv("STRATA_METAL_IGNORE_REPACK") && std::string(std::getenv("STRATA_METAL_IGNORE_REPACK")) == "1")) { |
| 847 | err = "metal window: a model repack or setup's preparation is running here (pid " + std::to_string(rp) + "): the engine is not started " |
| 848 | "beside it (it froze a 128 GB Mac twice); wait for it to finish, or set STRATA_METAL_IGNORE_REPACK=1"; |
| 849 | return false; |
| 850 | } |
| 851 | if (!f_.open(shard1, err)) return false; |
| 852 | cx_ = std::make_unique<mtl::Context>(); |
| 853 | if (const auto it = f_.meta_str.find("qwen4exp.strata_experts"); it != f_.meta_str.end()) { |
| 854 | sx_on_ = true; |
| 855 | if (!sx_open(it->second, G_NL, sx4_, sxd_, sx_map_, sx_len_, err)) return false; |
| 856 | if (const auto dr = f_.meta_str.find("qwen4exp.dense_rot"); dr != f_.meta_str.end() && dr->second == "1") { |
| 857 | if (loop_adapter_wanted()) { err = "metal window: this GGUF keeps the dense weights in the .strata's rotated domain (qwen4exp.dense_rot); the Loop adapter reads the raw activations: convert with Q4S_ROT=0 for the Loop"; return false; } |
| 858 | rot_ = true; |
| 859 | } |
| 860 | } |
| 861 | stream_ = ExpertStream::wanted(f_, *cx_); |
| 862 | g_f16_auto = f16_auto_for(f_.path, stream_); // STRATA_METAL_F16 unset: a resident IQ3_S / Q2_0 pack takes the fp16 decode windows |
| 863 | if (stream_) { |
| 864 | // the model does not fit: the dense weights are read into memory below, the experts stream from the SSD |
| 865 | st_ = std::make_unique<ExpertStream>(); |
| 866 | { // the compiled shape's layers (Flash-Next 48; q4_lite 40: ExpertStream::open(f, err) assumes 48 and stops at "blk.40. has no routed experts") |
| 867 | std::vector<std::string> pfx((size_t) G_NL); |
| 868 | for (int l = 0; l < G_NL; ++l) pfx[(size_t) l] = "blk." + std::to_string(l) + "."; |
| 869 | if (!st_->open(f_, pfx, err)) return false; |
| 870 | } |
| 871 | } else { |
| 872 | // a Gyro file holds the PLE table (54 GB of the 92) in the same GGUF: the host code reads it with direct I/O, |
| 873 | // no kernel touches it, so it is neither wrapped nor read ahead (it would take its 54 GB of file pages and |
| 874 | // wired GPU residency); the rest is wrapped as the ranges around it |
| 875 | const gguf::Tensor* ple = f_.find("per_layer_token_embd.weight"); |
| 876 | const size_t pg = 16384, maxlen = (size_t) cx_->max_buffer_length() / pg * pg; |
| 877 | std::vector<std::pair<size_t, size_t>> ranges; |
| 878 | size_t head = 0, tail = 0; |
| 879 | if (ple && ple->bytes > 0) { |
| 880 | head = std::min<size_t>(ple->offset / pg * pg, f_.map_bytes); |
| 881 | tail = std::min<size_t>((ple->offset + ple->bytes + pg - 1) / pg * pg, f_.map_bytes); |
| 882 | if (head > 0) ranges.push_back({0, head}); |
| 883 | if (tail < f_.map_bytes) ranges.push_back({tail, f_.map_bytes}); |
| 884 | } |
| 885 | bool split = !ranges.empty() && !(std::getenv("STRATA_METAL_WRAP_PLE") && std::string(std::getenv("STRATA_METAL_WRAP_PLE")) == "1"); |
| 886 | for (const auto& rg : ranges) |
no test coverage detected