| 96 | } |
| 97 | |
| 98 | bool NativeHead::load(const std::vector<std::string>& shards, int64_t n_in, int64_t n_out, std::string& err, |
| 99 | int peer_device) { |
| 100 | if (loaded()) { err = "native head is already loaded"; return false; } |
| 101 | if (n_in <= 0 || n_out <= 0 || n_in > INT_MAX || n_out > INT_MAX || n_in % 256) { |
| 102 | err = "native head requires positive int32 dimensions and whole 256-value rows"; |
| 103 | return false; |
| 104 | } |
| 105 | try { |
| 106 | // The architecture is the metadata shard's; output.weight comes from whichever shard holds it (shard 2 |
| 107 | // of Unsloth's UD-Q4_K_XL, whose shard 1 holds no tensor). GgufModel refuses a duplicate across shards. |
| 108 | const strata::GgufModel model(shards); |
| 109 | err = strata::check_engine_architecture(model.meta()); |
| 110 | if (!err.empty()) return false; |
| 111 | size_t at = 0; |
| 112 | const strata::TensorInfo* tensor = model.find("output.weight", &at); |
| 113 | const strata::GgufFile& gguf = model.shard(at); |
| 114 | if (!tensor || !strata::kernels::native_mmvq_supported((int) tensor->type) || tensor->shape.size() != 2 || |
| 115 | tensor->shape[0] != (uint64_t) n_in || tensor->shape[1] != (uint64_t) n_out) { |
| 116 | err = "native head: expected a natively supported output.weight with the canonical head dimensions"; |
| 117 | return false; |
| 118 | } |
| 119 | const uint64_t bytes = strata::kernels::native_mmvq_weight_bytes((int) tensor->type, (int) n_in, (int) n_out); |
| 120 | const uint64_t payload = gguf.file_size() - gguf.data_start(); |
| 121 | if (tensor->offset > payload || bytes > payload - tensor->offset) { |
| 122 | err = "native head: truncated output.weight payload"; |
| 123 | return false; |
| 124 | } |
| 125 | void* weights = nullptr; |
| 126 | void* scratch = nullptr; |
| 127 | void* host = nullptr; |
| 128 | int dev0 = -1; |
| 129 | if (cudaGetDevice(&dev0) != cudaSuccess) { (void) cudaGetLastError(); dev0 = -1; } |
| 130 | const bool ask_host = std::getenv("STRATA_HEAD_HOST") != nullptr; |
| 131 | #if !defined(__HIPCC__) && !defined(STRATA_GPU_METAL) |
| 132 | const char* sp = std::getenv("STRATA_PEER_HEAD_SPLIT"); |
| 133 | const char* np = std::getenv("STRATA_PEER_NO_P2P"); |
| 134 | if (sp != nullptr && std::atoi(sp) != 0 && peer_device >= 0 && peer_device != dev0 && dev0 >= 0 && !ask_host && |
| 135 | !strata::kernels::native_q6_k_packed_enabled() && !(tensor->type == 8 && std::getenv("STRATA_Q8_SM60") != nullptr) && |
| 136 | n_out >= 2 && !(np != nullptr && std::atoi(np) != 0)) { |
| 137 | int p01 = 0, p10 = 0; |
| 138 | cudaDeviceCanAccessPeer(&p01, dev0, peer_device); |
| 139 | cudaDeviceCanAccessPeer(&p10, peer_device, dev0); |
| 140 | if (p01 && p10) { |
| 141 | cudaError_t e0 = cudaDeviceEnablePeerAccess(peer_device, 0); |
| 142 | if (e0 == cudaErrorPeerAccessAlreadyEnabled) { (void) cudaGetLastError(); e0 = cudaSuccess; } |
| 143 | cudaError_t e1 = cudaSuccess; |
| 144 | { |
| 145 | ScopedDev on1(peer_device); |
| 146 | e1 = cudaDeviceEnablePeerAccess(dev0, 0); |
| 147 | if (e1 == cudaErrorPeerAccessAlreadyEnabled) { (void) cudaGetLastError(); e1 = cudaSuccess; } |
| 148 | } |
| 149 | if (e0 == cudaSuccess && e1 == cudaSuccess) { |
| 150 | const int rows0 = (int) (n_out / 2), rows1 = (int) (n_out - rows0); |
| 151 | const uint64_t row_b = bytes / (uint64_t) n_out; |
| 152 | const uint64_t bytes0 = (uint64_t) rows0 * row_b, bytes1 = (uint64_t) rows1 * row_b; |
| 153 | const int qcols = (int) strata::kernels::kVerifyMaxT; |
| 154 | const int max_st = std::max((int) strata::kernels::kVerifyLongT, qcols); |
| 155 | bool ok0 = false, ok1 = false; |
nothing calls this directly
no test coverage detected