lib.rs 3508 -> 938 lines, ffi.rs 1897 -> small, qwen_dispatch.rs 2003 and qwen_ffi.rs 1696 lose their inline test and installed-proof modules (scripts/dev/extract_modules.py, 5 self-tests; split_module.py --allow-root for lib.rs). Module paths, visibility and test ids are unchanged: 544 test ids before and after, cuda-driver tests 433 passed / 78 ignored before and after (Linux), host check clean. GPU identity and A/B follow with the combined batch 1+2 gate.
197 lines
8.1 KiB
Rust
197 lines
8.1 KiB
Rust
//! Moved out of the parent module by STRA-338: impl DeviceBuffer, impl Drop_for_DeviceBuffer, fn chain_selects_mtp, fn chain_selects_mtp, fn set_chain_down_override.
|
|
|
|
#[allow(unused_imports)]
|
|
use super::*;
|
|
|
|
impl DeviceBuffer<'_> {
|
|
pub fn len(&self) -> usize {
|
|
self.bytes
|
|
}
|
|
pub fn is_empty(&self) -> bool {
|
|
false
|
|
}
|
|
pub(super) fn staging(&self, actual: usize) -> Result<Vec<u8>> {
|
|
if actual != self.bytes {
|
|
return Err(Error::ShapeMismatch {
|
|
expected: self.bytes,
|
|
actual,
|
|
});
|
|
}
|
|
if self.context.poisoned.get() {
|
|
return Err(Error::ContextPoisoned);
|
|
}
|
|
let mut staging = Vec::new();
|
|
staging
|
|
.try_reserve_exact(self.bytes)
|
|
.map_err(|_| Error::HostAllocation)?;
|
|
staging.resize(self.bytes, 0);
|
|
Ok(staging)
|
|
}
|
|
pub fn copy_from(&mut self, input: &[u8]) -> Result<()> {
|
|
let mut staging = self.staging(input.len())?;
|
|
staging.copy_from_slice(input);
|
|
self.copy_staged(staging, false).map(|_| ())
|
|
}
|
|
/// Consume genuine whole-buffer host staging without an intermediate clone.
|
|
/// Byte ownership is not a source, finite or device-completion certificate.
|
|
pub fn copy_from_owned(&mut self, input: Vec<u8>) -> Result<()> {
|
|
// Preserve the original staging() shape-before-poison admission order.
|
|
if input.len() != self.bytes {
|
|
return Err(Error::ShapeMismatch {
|
|
expected: self.bytes,
|
|
actual: input.len(),
|
|
});
|
|
}
|
|
if self.context.poisoned.get() {
|
|
return Err(Error::ContextPoisoned);
|
|
}
|
|
// Exactly the existing attempted-copy/synchronize/restore and retained
|
|
// staging rule. No source storage is released while work is uncertain.
|
|
self.copy_staged(input, false).map(|_| ())
|
|
}
|
|
/// Copy a nonempty checked subspan. Owned staging remains alive through
|
|
/// synchronization, with the same poisoning/leak rule as whole-buffer copies.
|
|
pub fn copy_from_range(&mut self, offset: usize, input: &[u8]) -> Result<()> {
|
|
self.copy_from_range_shared(offset, input)
|
|
}
|
|
/// The same transfer through a shared borrow, for arenas their single
|
|
/// owner thread shares between read-only views (`Rc<DeviceBuffer>`).
|
|
/// Ranges are checked and the copy is synchronised exactly like
|
|
/// `copy_from_range`; callers keep writes to disjoint spans.
|
|
pub(crate) fn copy_from_range_shared(&self, offset: usize, input: &[u8]) -> Result<()> {
|
|
if self.context.poisoned.get() {
|
|
return Err(Error::ContextPoisoned);
|
|
}
|
|
let handle = self.handle.ok_or(Error::ContextPoisoned)?.checked_subspan(
|
|
self.bytes,
|
|
offset,
|
|
input.len(),
|
|
)?;
|
|
let mut staging = Vec::new();
|
|
staging
|
|
.try_reserve_exact(input.len())
|
|
.map_err(|_| Error::HostAllocation)?;
|
|
staging.extend_from_slice(input);
|
|
let guard = self.context.enter()?;
|
|
let copy = self.context.driver.api.copy_from(handle, &staging);
|
|
let sync = self.context.driver.api.synchronize();
|
|
if sync.is_err() {
|
|
self.context.poisoned.set(true);
|
|
std::mem::forget(staging);
|
|
return combine(combine(copy, sync), guard.finish());
|
|
}
|
|
combine(copy, guard.finish())
|
|
}
|
|
/// Read back a nonempty checked subspan. Owned staging stays alive through
|
|
/// synchronization with the same poisoning/leak rule as whole-buffer reads;
|
|
/// a failed synchronization never returns its bytes.
|
|
pub fn copy_to_range(&self, offset: usize, output: &mut [u8]) -> Result<()> {
|
|
if self.context.poisoned.get() {
|
|
return Err(Error::ContextPoisoned);
|
|
}
|
|
let handle = self.handle.ok_or(Error::ContextPoisoned)?.checked_subspan(
|
|
self.bytes,
|
|
offset,
|
|
output.len(),
|
|
)?;
|
|
let mut staging = Vec::new();
|
|
staging
|
|
.try_reserve_exact(output.len())
|
|
.map_err(|_| Error::HostAllocation)?;
|
|
staging.resize(output.len(), 0);
|
|
let guard = self.context.enter()?;
|
|
let copy = self.context.driver.api.copy_to(handle, &mut staging);
|
|
let sync = self.context.driver.api.synchronize();
|
|
if sync.is_err() {
|
|
self.context.poisoned.set(true);
|
|
std::mem::forget(staging);
|
|
return combine(combine(copy, sync), guard.finish());
|
|
}
|
|
combine(copy, guard.finish())?;
|
|
output.copy_from_slice(&staging);
|
|
Ok(())
|
|
}
|
|
pub fn copy_to(&self, output: &mut [u8]) -> Result<()> {
|
|
let staging = self.staging(output.len())?;
|
|
let staging = self.copy_staged(staging, true)?;
|
|
output.copy_from_slice(&staging);
|
|
Ok(())
|
|
}
|
|
/// Return the completed whole-buffer readback without a second host copy.
|
|
/// The staging remains owned through the same synchronization and poison
|
|
/// handling as `copy_to`; a failed synchronization never returns its bytes.
|
|
pub fn copy_to_owned(&self, expected_bytes: usize) -> Result<Vec<u8>> {
|
|
let staging = self.staging(expected_bytes)?;
|
|
self.copy_staged(staging, true)
|
|
}
|
|
pub(super) fn copy_staged(&self, mut staging: Vec<u8>, to_host: bool) -> Result<Vec<u8>> {
|
|
let handle = self.handle.ok_or(Error::ContextPoisoned)?;
|
|
let guard = self.context.enter()?;
|
|
let copy = if to_host {
|
|
self.context.driver.api.copy_to(handle, &mut staging)
|
|
} else {
|
|
self.context.driver.api.copy_from(handle, &staging)
|
|
};
|
|
// Even copy errors can report earlier asynchronous work. Synchronize
|
|
// on every attempted copy before considering the staging reclaimable.
|
|
let sync = self.context.driver.api.synchronize();
|
|
if sync.is_err() {
|
|
self.context.poisoned.set(true);
|
|
// CUDA may still reference these owned host bytes. A poisoned
|
|
// context admits no further copy, limiting this leak to one buffer
|
|
// (at most its context budget), regardless of caller lifetimes.
|
|
std::mem::forget(staging);
|
|
return combine(combine(copy, sync), guard.finish()).map(|()| Vec::new());
|
|
}
|
|
combine(copy, guard.finish())?;
|
|
Ok(staging)
|
|
}
|
|
pub fn release(mut self) -> Result<()> {
|
|
self.free()
|
|
}
|
|
pub(super) fn free(&mut self) -> Result<()> {
|
|
let Some(handle) = self.handle.take() else {
|
|
return Ok(());
|
|
};
|
|
if self.borrowed {
|
|
return Ok(());
|
|
}
|
|
vram_ledger::note_free(handle.qwen_address());
|
|
if compute_drain::background() != 0 && !self.context.poisoned.get() {
|
|
// a chunk expert stream is in flight: do not synchronize the device now
|
|
return self.context.defer_free(handle, self.bytes);
|
|
}
|
|
let guard = self.context.enter()?;
|
|
let free = self.context.driver.api.free(handle);
|
|
if free.is_ok() {
|
|
self.context
|
|
.allocated
|
|
.set(self.context.allocated.get() - self.bytes);
|
|
}
|
|
combine(free, guard.finish())
|
|
}
|
|
}
|
|
impl Drop for DeviceBuffer<'_> {
|
|
fn drop(&mut self) {
|
|
let _ = self.free();
|
|
}
|
|
}
|
|
|
|
/// `LELEKA_CHAIN` selects the chain-kernel group `mtp` (claude/chain-kernels): the draft step reads the hyper-connections with the
|
|
/// staged variant the Original runs on this card (bit-identical to the plain one) and uses the load-hoisted attention kernels.
|
|
#[cfg(all(feature = "cuda-driver", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))]
|
|
pub fn chain_selects_mtp() -> bool {
|
|
original_verify::chain::flags().mtp
|
|
}
|
|
#[cfg(not(all(feature = "cuda-driver", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64")))]
|
|
pub fn chain_selects_mtp() -> bool {
|
|
false
|
|
}
|
|
/// Without the CUDA driver there is no chain window: the override is a no-op.
|
|
#[cfg(not(all(
|
|
feature = "cuda-driver",
|
|
any(target_os = "linux", target_os = "windows"),
|
|
target_pointer_width = "64"
|
|
)))]
|
|
pub fn set_chain_down_override(_: i8) {}
|