Files
leleka/crates/cuda-driver/src/native_batch_workspace.rs
T

177 lines
6.1 KiB
Rust

//! Model-lifetime original-prefill GEMM workspace and handle. Closed producers
//! exclusively borrow this actual Context-affine owner through each full drain.
use crate::ple_batch_ffi::Blas;
use crate::{Context, DeviceBuffer, Error, Result, combine};
pub const NATIVE_BATCH_WORKSPACE_BYTES: usize = 32 * 1024 * 1024;
const RESERVE: usize = 128 * 1024 * 1024;
pub struct NativeBatchWorkspace<'ctx> {
context: &'ctx Context<'ctx>,
buffer: Option<DeviceBuffer<'ctx>>,
blas: Option<Blas>,
failed: bool,
}
impl Context<'_> {
pub fn native_batch_workspace(&self) -> Result<NativeBatchWorkspace<'_>> {
NativeBatchWorkspace::new(self)
}
}
impl<'ctx> NativeBatchWorkspace<'ctx> {
fn new(context: &'ctx Context<'ctx>) -> Result<Self> {
if context.is_poisoned() || context.handle.is_none() {
return Err(Error::ContextPoisoned);
}
let required = NATIVE_BATCH_WORKSPACE_BYTES + RESERVE;
if context.allocated_bytes().checked_add(required).is_none_or(|n| n > context.budget)
|| context.memory_info()?.0 < required
{
return Err(Error::BudgetExceeded);
}
let mut owner = Self { context, buffer: None, blas: None, failed: false };
if let Err(error) = owner.open() {
return combine(Err(error), owner.close());
}
Ok(owner)
}
fn open(&mut self) -> Result<()> {
let context = self.context;
self.buffer = Some(context.allocate(NATIVE_BATCH_WORKSPACE_BYTES)?);
self.attach_blas()?;
self.validate_context(context)?;
if context.memory_info()?.0 < RESERVE {
return Err(Error::BudgetExceeded);
}
Ok(())
}
fn attach_blas(&mut self) -> Result<()> {
let context = self.context;
let guard = context.enter()?;
let load = Blas::load(
self.buffer.as_ref().ok_or(Error::ContextPoisoned)?.handle.ok_or(Error::ContextPoisoned)?,
NATIVE_BATCH_WORKSPACE_BYTES,
);
let pop = guard.finish();
if pop.is_err() {
context.poisoned.set(true);
}
match load {
Ok(blas) => {
self.blas = Some(blas);
pop
}
Err(error) => {
context.poisoned.set(true);
combine(Err(error), pop)
}
}
}
pub fn allocated_bytes(&self) -> usize {
self.buffer.as_ref().map_or(0, DeviceBuffer::len)
}
pub fn is_failed(&self) -> bool {
self.failed || self.context.is_poisoned() || self.context.handle.is_none()
}
pub fn validate_context(&self, context: &Context<'_>) -> Result<()> {
if !std::ptr::eq(context, self.context) {
return Err(Error::ContextMismatch);
}
if self.is_failed() || self.blas.is_none() {
return Err(Error::ContextPoisoned);
}
let buffer = self.buffer.as_ref().ok_or(Error::ContextPoisoned)?;
if !std::ptr::eq(buffer.context, self.context) {
return Err(Error::ContextMismatch);
}
if buffer.bytes != NATIVE_BATCH_WORKSPACE_BYTES {
return Err(Error::InvalidSize);
}
buffer.handle.ok_or(Error::ContextPoisoned)?.validate_projection_extent(NATIVE_BATCH_WORKSPACE_BYTES, 256)?;
if context.allocated_bytes() > context.budget {
return Err(Error::BudgetExceeded);
}
Ok(())
}
pub(crate) fn blas(&self) -> Result<&Blas> {
self.validate_context(self.context)?;
self.blas.as_ref().ok_or(Error::ContextPoisoned)
}
pub(crate) fn mark_failed(&mut self) {
self.failed = true;
self.context.poisoned.set(true);
}
pub(crate) fn reject_alias(&self, handle: crate::ffi::BufferHandle, bytes: usize) -> Result<()> {
let h = self.buffer.as_ref().ok_or(Error::ContextPoisoned)?.handle.ok_or(Error::ContextPoisoned)?;
if handle.overlaps(bytes, h, NATIVE_BATCH_WORKSPACE_BYTES) {
return Err(Error::OutputAliasesInput);
}
Ok(())
}
pub fn close(mut self) -> Result<()> {
self.close_inner()
}
fn close_inner(&mut self) -> Result<()> {
self.failed = true;
let context = self.context;
let mut result = Ok(());
if let Some(blas) = self.blas.take() {
result = combine(result, Self::close_blas(context, blas));
}
if let Some(buffer) = self.buffer.take() {
result = combine(result, Self::release_buffer(context, buffer));
}
if context.is_poisoned() {
context.retain_driver();
}
result
}
fn close_blas(context: &Context<'_>, blas: Blas) -> Result<()> {
let close = if context.is_poisoned() {
std::mem::forget(blas);
Err(Error::ContextPoisoned)
} else {
match context.enter() {
Ok(g) => {
let close = blas.close();
let pop = g.finish();
combine(close, pop)
}
Err(e) => {
std::mem::forget(blas);
Err(e)
}
}
};
if close.is_err() {
context.poisoned.set(true);
}
close
}
fn release_buffer(context: &Context<'_>, buffer: DeviceBuffer<'ctx>) -> Result<()> {
let close = if context.is_poisoned() {
std::mem::forget(buffer);
Err(Error::ContextPoisoned)
} else {
buffer.release()
};
if close.is_err() {
context.poisoned.set(true);
}
close
}
}
impl Drop for NativeBatchWorkspace<'_> {
fn drop(&mut self) {
let _ = self.close_inner();
}
}
// Root registers the closed gdn_batch_gemm module alongside Claude's Option-B
// producer. This does not change the existing PLE/GR workspace methods.
impl<'ctx> NativeBatchWorkspace<'ctx> {
pub fn gdn_gemm(&mut self) -> Result<crate::gdn_batch_gemm::NativeGdnGemm<'_, 'ctx>> {
crate::gdn_batch_gemm::NativeGdnGemm::new(self)
}
pub(crate) fn context_for_gdn(&self) -> &'ctx Context<'ctx> {
self.context
}
}