#pragma once #include "strata/core/expert_cache.hpp" #include "strata/core/expert_source.hpp" #include #include #include #include #include namespace strata::core { class RemoteExpertOpt; /// A static, profile-filled expert tier on another CUDA device. CUDA0 keeps all /// dense weights and state; results return through the existing pinned CPU rows. class RemoteExperts { public: RemoteExperts() = default; ~RemoteExperts(); RemoteExperts(const RemoteExperts&) = delete; RemoteExperts& operator=(const RemoteExperts&) = delete; /// Initialise the device before the host expert arena registers /// tens of GiB of portable mapped memory with CUDA. static bool preflight(int device, double& free_gib, std::string& err); bool open(int device, int slots, int64_t layers, int64_t experts, const std::vector>& ranked, const ExpertCache& primary, ExpertSource& source, std::vector& claimed, std::string& err, bool auto_size = false); void close(); /// `kind` is the primary verifier's classification (-1 = CPU candidate), /// or null on the one-token path. Entries already served on CUDA0 are excluded. bool begin(int64_t layer, const float* x, const int32_t* ids, int64_t n_tok, int64_t k, const int32_t* kind, const int32_t* primary_res, std::string& err); bool owns(int64_t index) const { return owned_[(size_t) index] != 0; } bool optimized_decode() const { return remote_opt_ != nullptr; } bool finish(float* out, std::string& err); int64_t resident() const { return cache_.resident(); } int64_t computed() const { return computed_; } int64_t launched_layers() const { return launched_layers_; } double gib() const { return cache_.gib(); } uint64_t returned_bytes() const { return returned_bytes_; } uint64_t full_row_bytes() const { return full_row_bytes_; } /// host time spent in begin() (staging + launches) and in finish() (waiting for this GPU), cumulative double ms_begin() const { return ms_begin_; } double ms_wait() const { return ms_wait_; } private: friend class RemoteExpertOpt; RemoteExpertOpt* remote_opt_ = nullptr; int device_ = -1; int64_t n_expert_ = 0; int32_t groups_ = 0; int64_t computed_ = 0; int64_t launched_layers_ = 0; uint64_t returned_bytes_ = 0; uint64_t full_row_bytes_ = 0; double ms_begin_ = 0, ms_wait_ = 0; ExpertCache cache_; cudaStream_t stream_ = nullptr; float* h_x_ = nullptr; float* h_out_ = nullptr; void* h_meta_ = nullptr; float* d_x_ = nullptr; float* z_x_ = nullptr; ///< h_x_ as the helper GPU sees it (zero-copy: no input copy per layer) float* z_out_ = nullptr; ///< h_out_ as the helper GPU sees it (zero-copy: no result copy) bool zero_copy_ = false; float* d_out_ = nullptr; uint8_t* d_q8_ = nullptr; float* d_scales_ = nullptr; void* d_scratch_ = nullptr; void* d_meta_ = nullptr; ///< one contiguous upload of grouped indices, instead of five small copies int32_t* d_start_ = nullptr; int32_t* d_dst_ = nullptr; int32_t* d_tok_ = nullptr; int32_t* d_count_ = nullptr; unsigned long long* d_ptr_ = nullptr; std::vector owned_; std::vector layers_present_; std::vector group_of_, group_id_; std::vector start_, dst_, tok_, original_row_; std::vector ptr_; }; } // namespace strata::core