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

Method open

engine/src/core/remote_experts.cpp:136–263  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

134}
135
136bool RemoteExperts::open(int device, int slots, int64_t layers, int64_t experts,
137 const std::vector<std::pair<int32_t, int32_t>>& ranked,
138 const ExpertCache& primary, ExpertSource& source,
139 std::vector<uint8_t>& claimed, std::string& err, bool auto_size) {
140 close();
141 int count = 0;
142 if (!check(cudaGetDeviceCount(&count), "cudaGetDeviceCount", err, device)) return false;
143 if (device < 0 || device >= count || slots <= 0 || ranked.empty() ||
144 layers <= 0 || experts <= 0 || claimed.size() != (size_t) layers * (size_t) experts ||
145 device == primary_device()) { // the helper cannot be the primary device (any visible ordinal)
146 err = "CUDA" + std::to_string(device) + " experts: need the device, ranked experts and positive slot count";
147 return false;
148 }
149 DeviceScope scope(device);
150 if (!scope.ok) { err = scope.error(device); return false; }
151 device_ = device;
152 n_expert_ = experts;
153 const auto& lay = strata::kernels::cpu::expert_layout();
154 size_t free_bytes = 0, total_bytes = 0;
155 if (!check(cudaMemGetInfo(&free_bytes, &total_bytes), "free memory", err, device)) { close(); return false; }
156 uint64_t needed = 0;
157 std::vector<std::pair<int32_t, int32_t>> selected;
158 selected.reserve((size_t) std::min<int64_t>(slots, layers * experts));
159 std::vector<uint8_t> picked(claimed.size(), 0);
160 for (const auto& pair : ranked) {
161 if (pair.first < 0 || pair.first >= layers || pair.second < 0 || pair.second >= experts) continue;
162 const size_t index = (size_t) pair.first * (size_t) experts + (size_t) pair.second;
163 if (primary.slot_of(pair.first, pair.second) < 0 && !claimed[index] && !picked[index]) {
164 const uint64_t bytes = lay.native ? (lay.blob_bytes(pair.first) + 255) / 256 * 256 : lay.max_blob;
165 if (auto_size && needed + bytes + (512ull << 20) > free_bytes) break;
166 selected.push_back(pair);
167 needed += bytes;
168 picked[index] = 1;
169 if ((int) selected.size() >= slots) break;
170 }
171 }
172 if (selected.empty()) {
173 err = "CUDA" + std::to_string(device) + (auto_size ? " experts: no unclaimed expert fits with 512 MiB free"
174 : " experts: no unclaimed experts remain");
175 close(); return false;
176 }
177 std::vector<int64_t> sizes;
178 if (lay.native) {
179 sizes.reserve(selected.size());
180 for (const auto& pair : selected) sizes.push_back((int64_t) lay.blob_bytes(pair.first));
181 }
182 // Leave room for the CUDA context, staging and later driver allocations, especially under WDDM.
183 if (needed + (512ull << 20) > free_bytes) {
184 err = "CUDA" + std::to_string(device) + " experts: slots leave less than 512 MiB free; reduce --expert-cache-device" + std::to_string(device);
185 close(); return false;
186 }
187 const bool cache_ok = lay.native ? cache_.open_sized(sizes, layers, experts, err)
188 : cache_.open((int64_t) selected.size(), layers, experts,
189 (int64_t) lay.max_blob, err);
190 if (!cache_ok) { err = "CUDA" + std::to_string(device) + " experts: " + err; close(); return false; }
191 for (const auto& pair : selected) {
192 const int32_t slot = cache_.admit(pair.first, pair.second);
193 const uint8_t* blob = source.blob(pair.first, pair.second);

Callers

nothing calls this directly

Calls 15

expert_layoutFunction · 0.85
hadamard_specFunction · 0.85
hadamard_signsFunction · 0.85
push_backMethod · 0.80
frontMethod · 0.80
assignMethod · 0.80
checkFunction · 0.70
closeFunction · 0.50
cudaGetDeviceCountFunction · 0.50
to_stringFunction · 0.50
cudaMemGetInfoFunction · 0.50

Tested by

no test coverage detected