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

Method reserve_exchanges

engine/src/core/expert_source.cpp:4858–4925  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4856}
4857
4858bool FileExpertSource::reserve_exchanges(int64_t n, std::string& err, int64_t bounce) {
4859 err.clear();
4860 if (n <= xstage_cap_) return true;
4861 if (!staged_.empty()) { err = "FileExpertSource: exchange buffers are in use"; return false; }
4862 if (exchange_storage_.active()) {
4863 err = "FileExpertSource: reserve maximum exchange capacity before enabling rotation";
4864 return false; // the original exchange allocation can now contain live resident experts
4865 }
4866 uint64_t blob = 0;
4867 for (const uint64_t b : layer_blob_bytes_) blob = std::max(blob, b);
4868 if (blob == 0 || n <= 0) { err = "FileExpertSource: no expert geometry for the exchange buffers"; return false; }
4869 if (xstage_ != nullptr) {
4870 if (xstage_pinned_) (void) cudaFreeHost(xstage_);
4871 else std::free(xstage_);
4872 xstage_ = nullptr;
4873 xstage_cap_ = 0;
4874 }
4875 if ((uint64_t)n > std::numeric_limits<size_t>::max() / blob) {
4876 err = "FileExpertSource: exchange allocation size overflow"; return false;
4877 }
4878 const char* rotate_env = std::getenv("STRATA_EXCHANGE_ROTATE");
4879 const bool requested = rotate_env && std::strcmp(rotate_env, "1") == 0;
4880 // layers of several block sizes (a GGUF's mixed i-quants): spares of every size, see ExchangeStorage
4881 bounce = std::max<int64_t>(0, std::min(bounce, n));
4882 const uint64_t rotate_total = detail::ExchangeStorage::exchange_bytes_needed(layer_blob_bytes_, (size_t) n, (size_t) bounce);
4883 const bool eligible = requested && rotate_total != 0 && rotate_total <= std::numeric_limits<size_t>::max() &&
4884 complement_pinned_ && !complement_partial_ &&
4885 complement_host_ && complement_device_ && complement_bytes_ > 0;
4886 const size_t total = eligible ? (size_t) rotate_total : (size_t) n * (size_t) blob;
4887 void* p = nullptr;
4888 if (cudaHostAlloc(&p, total, eligible ? cudaHostAllocMapped : cudaHostAllocDefault) == cudaSuccess && p != nullptr) {
4889 xstage_pinned_ = true;
4890 } else {
4891 (void) cudaGetLastError();
4892 p = std::malloc(total);
4893 xstage_pinned_ = false;
4894 if (p == nullptr) { err = "FileExpertSource: cannot allocate the exchange buffers"; return false; }
4895 }
4896 xstage_ = (uint8_t*) p;
4897 xstage_cap_ = n;
4898 xstage_blob_ = blob;
4899 if (requested) {
4900 std::string reason = "requires a fully mapped/pinned RAM complement";
4901 void* device = nullptr;
4902 if (eligible && xstage_pinned_ && cudaHostGetDevicePointer(&device, p, 0) == cudaSuccess && device) {
4903 try {
4904 std::vector<uint64_t> sizes(complement_offsets_.size(), 0);
4905 for (size_t i = 0; i < sizes.size(); ++i) {
4906 const size_t layer = n_expert_ > 0 ? i / (size_t) n_expert_ : 0;
4907 sizes[i] = layer < layer_blob_bytes_.size() ? layer_blob_bytes_[layer] : 0;
4908 }
4909 if (exchange_storage_.initialize_sized(complement_offsets_, sizes, (uint8_t*)complement_host_, complement_device_,
4910 complement_bytes_, xstage_, (const uint8_t*)device, (uint64_t)total, (size_t)n, (size_t)bounce, reason)) {
4911 std::fprintf(stderr, "FileExpertSource: exchange buffer rotation enabled: %lld buffers in %zu block sizes "
4912 "(largest %llu bytes, %.2f GiB page-locked); no host commit memcpy\n",
4913 (long long)n, exchange_storage_.block_sizes(), (unsigned long long)blob,
4914 (double)total / 1073741824.0);
4915 }

Callers 3

test_rotating_sourceFunction · 0.45
test_foresight_exchangeFunction · 0.45
test_replace_residentFunction · 0.45

Calls 14

initialize_sizedMethod · 0.80
block_sizesMethod · 0.80
maxFunction · 0.50
cudaFreeHostFunction · 0.50
freeFunction · 0.50
minFunction · 0.50
cudaHostAllocFunction · 0.50
cudaGetLastErrorFunction · 0.50
mallocFunction · 0.50
cudaHostGetDevicePointerFunction · 0.50
clearMethod · 0.45
emptyMethod · 0.45

Tested by 3

test_rotating_sourceFunction · 0.36
test_foresight_exchangeFunction · 0.36
test_replace_residentFunction · 0.36