STRA-338 size: cuda-driver MmqExperiment::new/enable_original_formats/observed_admit end <= 30 lines, complexity <= 10

This commit is contained in:
Pavlo Ratushnyi
2026-11-06 15:39:23 +01:00
parent 82df6cd43b
commit fbb6c87ed3
+110 -80
View File
@@ -21,56 +21,66 @@ impl<'ctx> MmqExperiment<'ctx> {
};
let guard = context.enter()?;
crate::arch_ptx::latch_stream_k_policy();
let result = (|| {
owner.sms = context.driver.api.mmq_capabilities()?;
owner.modules[0] = Some(context.driver.api.load_mmq(0)?);
owner.modules[1] = Some(context.driver.api.load_mmq(1)?);
owner.modules[2] = Some(context.driver.api.load_mmq(2)?);
owner.modules[3] = Some(context.driver.api.load_mmq(3)?);
owner.modules[4] = Some(context.driver.api.load_mmq(4)?);
let mut fixups = Vec::new();
fixups.try_reserve_exact(28).map_err(|_| Error::HostAllocation)?;
let mut functions = Vec::new();
functions.try_reserve_exact(28).map_err(|_| Error::HostAllocation)?;
for format in [MmqFormat::Iq3XxsGateUp, MmqFormat::Iq2SGateUp, MmqFormat::Iq4NlDown, MmqFormat::Q2Down] {
for tile in Tile::ALL {
let module = if tile == Tile::J64 { 0 } else { 3 };
let fixup_module = if tile == Tile::J64 { 2 } else { 4 };
let f = context.driver.api.mmq_function(
owner.modules[module].ok_or(Error::ContextPoisoned)?,
Some((format, tile)),
false,
)?;
let mut l = format.layout(1)?;
l.tile = tile;
let j = tile.width();
let stride = if format == MmqFormat::Iq2SGateUp { 84 } else { 76 };
l.shared = 128 * stride * 4 + j * 4 + (j * 144).div_ceil(1024) * 1024;
context.driver.api.mmq_shared(f, l.shared)?;
functions.push(f);
fixups.push(context.driver.api.mmq_function(
owner.modules[fixup_module].ok_or(Error::ContextPoisoned)?,
Some((format, tile)),
true,
)?);
}
}
let functions: [ffi::FunctionHandle; 28] =
functions.try_into().map_err(|_| Error::InvalidProjectionShape)?;
let fixups: [ffi::FunctionHandle; 28] = fixups.try_into().map_err(|_| Error::InvalidProjectionShape)?;
owner.functions = Some(std::array::from_fn(|f| std::array::from_fn(|j| functions[f * 7 + j])));
owner.fixups = Some(std::array::from_fn(|f| std::array::from_fn(|j| fixups[f * 7 + j])));
owner.quantizer =
Some(context.driver.api.mmq_function(owner.modules[1].ok_or(Error::ContextPoisoned)?, None, false)?);
Ok(())
})();
let result = owner.load_native();
let result = combine(result, guard.finish());
if let Err(error) = result {
return combine(Err(error), owner.close());
}
Ok(owner)
}
/// Capabilities, the five modules, the native tile functions and fixups, and the activation quantizer.
fn load_native(&mut self) -> Result<()> {
let api = &self.context.driver.api;
self.sms = api.mmq_capabilities()?;
for kind in 0..5 {
self.modules[kind as usize] = Some(api.load_mmq(kind)?);
}
let (functions, fixups) = self.native_tables()?;
self.functions = Some(functions);
self.fixups = Some(fixups);
self.quantizer = Some(api.mmq_function(self.modules[1].ok_or(Error::ContextPoisoned)?, None, false)?);
Ok(())
}
/// The projection and fixup function of every (format, tile) of the four native formats.
fn native_tables(&self) -> Result<(TileFunctions, TileFunctions)> {
let mut fixups = Vec::new();
fixups.try_reserve_exact(28).map_err(|_| Error::HostAllocation)?;
let mut functions = Vec::new();
functions.try_reserve_exact(28).map_err(|_| Error::HostAllocation)?;
for format in [MmqFormat::Iq3XxsGateUp, MmqFormat::Iq2SGateUp, MmqFormat::Iq4NlDown, MmqFormat::Q2Down] {
for tile in Tile::ALL {
let (f, fixup) = self.native_tile(format, tile)?;
functions.push(f);
fixups.push(fixup);
}
}
let functions: [ffi::FunctionHandle; 28] = functions.try_into().map_err(|_| Error::InvalidProjectionShape)?;
let fixups: [ffi::FunctionHandle; 28] = fixups.try_into().map_err(|_| Error::InvalidProjectionShape)?;
Ok((
std::array::from_fn(|f| std::array::from_fn(|j| functions[f * 7 + j])),
std::array::from_fn(|f| std::array::from_fn(|j| fixups[f * 7 + j])),
))
}
/// One tile's projection function (with its shared-memory size set) and fixup function.
fn native_tile(&self, format: MmqFormat, tile: Tile) -> Result<(ffi::FunctionHandle, ffi::FunctionHandle)> {
let api = &self.context.driver.api;
let module = if tile == Tile::J64 { 0 } else { 3 };
let fixup_module = if tile == Tile::J64 { 2 } else { 4 };
let f = api.mmq_function(self.modules[module].ok_or(Error::ContextPoisoned)?, Some((format, tile)), false)?;
let mut l = format.layout(1)?;
l.tile = tile;
let j = tile.width();
let stride = if format == MmqFormat::Iq2SGateUp { 84 } else { 76 };
l.shared = 128 * stride * 4 + j * 4 + (j * 144).div_ceil(1024) * 1024;
api.mmq_shared(f, l.shared)?;
let fixup =
api.mmq_function(self.modules[fixup_module].ok_or(Error::ContextPoisoned)?, Some((format, tile)), true)?;
Ok((f, fixup))
}
pub(super) fn enable_original_formats(&mut self) -> Result<()> {
self.check()?;
if self.original_modules.iter().any(Option::is_some) {
@@ -79,39 +89,42 @@ impl<'ctx> MmqExperiment<'ctx> {
original_formats_asset(5)?;
original_formats_asset(6)?;
let guard = self.context.enter()?;
let result = (|| {
self.original_modules[0] = Some(self.context.driver.api.load_mmq(5)?);
self.original_modules[1] = Some(self.context.driver.api.load_mmq(6)?);
let mut functions = Vec::new();
let mut fixups = Vec::new();
functions.try_reserve_exact(14).map_err(|_| Error::HostAllocation)?;
fixups.try_reserve_exact(14).map_err(|_| Error::HostAllocation)?;
for format in [MmqFormat::Iq3SGateUp, MmqFormat::Iq4XsGateUp] {
for tile in Tile::ALL {
let f = self.context.driver.api.mmq_function(
self.original_modules[0].ok_or(Error::ContextPoisoned)?,
Some((format, tile)),
false,
)?;
let l = format.adaptive_layout(tile.width(), MmqProfile::OriginalAdaptive)?;
self.context.driver.api.mmq_shared(f, l.shared)?;
functions.push(f);
fixups.push(self.context.driver.api.mmq_function(
self.original_modules[1].ok_or(Error::ContextPoisoned)?,
Some((format, tile)),
true,
)?);
}
}
let functions: [ffi::FunctionHandle; 14] =
functions.try_into().map_err(|_| Error::InvalidProjectionShape)?;
let fixups: [ffi::FunctionHandle; 14] = fixups.try_into().map_err(|_| Error::InvalidProjectionShape)?;
self.original_functions = Some(std::array::from_fn(|i| std::array::from_fn(|j| functions[i * 7 + j])));
self.original_fixups = Some(std::array::from_fn(|i| std::array::from_fn(|j| fixups[i * 7 + j])));
Ok(())
})();
let result = self.load_original();
combine(result, guard.finish())
}
fn load_original(&mut self) -> Result<()> {
self.original_modules[0] = Some(self.context.driver.api.load_mmq(5)?);
self.original_modules[1] = Some(self.context.driver.api.load_mmq(6)?);
let mut functions = Vec::new();
let mut fixups = Vec::new();
functions.try_reserve_exact(14).map_err(|_| Error::HostAllocation)?;
fixups.try_reserve_exact(14).map_err(|_| Error::HostAllocation)?;
for format in [MmqFormat::Iq3SGateUp, MmqFormat::Iq4XsGateUp] {
for tile in Tile::ALL {
let (f, fixup) = self.original_tile(format, tile)?;
functions.push(f);
fixups.push(fixup);
}
}
let functions: [ffi::FunctionHandle; 14] = functions.try_into().map_err(|_| Error::InvalidProjectionShape)?;
let fixups: [ffi::FunctionHandle; 14] = fixups.try_into().map_err(|_| Error::InvalidProjectionShape)?;
self.original_functions = Some(std::array::from_fn(|i| std::array::from_fn(|j| functions[i * 7 + j])));
self.original_fixups = Some(std::array::from_fn(|i| std::array::from_fn(|j| fixups[i * 7 + j])));
Ok(())
}
/// One original-format tile's projection function (shared size set) and fixup function.
fn original_tile(&self, format: MmqFormat, tile: Tile) -> Result<(ffi::FunctionHandle, ffi::FunctionHandle)> {
let api = &self.context.driver.api;
let module = self.original_modules[0].ok_or(Error::ContextPoisoned)?;
let f = api.mmq_function(module, Some((format, tile)), false)?;
let l = format.adaptive_layout(tile.width(), MmqProfile::OriginalAdaptive)?;
api.mmq_shared(f, l.shared)?;
let fixup_module = self.original_modules[1].ok_or(Error::ContextPoisoned)?;
Ok((f, api.mmq_function(fixup_module, Some((format, tile)), true)?))
}
pub(super) fn function(&self, format: MmqFormat, tile: Tile, fixup: bool) -> Result<ffi::FunctionHandle> {
self.check()?;
if format.index() < 4 {
@@ -182,14 +195,14 @@ impl<'ctx> MmqExperiment<'ctx> {
combine(result, combine(cleanup, self.release_buffer(packed)))
}
/// Context, size and alias admission of the three buffers; returns their handles `[weights, input, output]`.
fn observed_admit(
/// All buffers belong to this context and have the layout's sizes; returns the weight buffer.
fn observed_contexts<'w, 'c>(
&self,
weights: &MmqWeights<'_>,
weights: &'w MmqWeights<'c>,
input: &DeviceBuffer<'_>,
output: &DeviceBuffer<'_>,
l: &Layout,
) -> Result<[ffi::BufferHandle; 3]> {
) -> Result<&'w DeviceBuffer<'c>> {
let w = weights.buffer.as_ref().ok_or(Error::ContextPoisoned)?;
if !std::ptr::eq(self.context, weights.context)
|| !std::ptr::eq(self.context, w.context)
@@ -201,19 +214,36 @@ impl<'ctx> MmqExperiment<'ctx> {
if w.bytes != l.weight || input.bytes != l.input || output.bytes != l.output {
return Err(Error::InvalidProjectionShape);
}
Ok(w)
}
/// Context, size and alias admission of the three buffers; returns their handles `[weights, input, output]`.
fn observed_admit(
&self,
weights: &MmqWeights<'_>,
input: &DeviceBuffer<'_>,
output: &DeviceBuffer<'_>,
l: &Layout,
) -> Result<[ffi::BufferHandle; 3]> {
let w = self.observed_contexts(weights, input, output, l)?;
let wh = w.handle.ok_or(Error::ContextPoisoned)?;
let ih = input.handle.ok_or(Error::ContextPoisoned)?;
let oh = output.handle.ok_or(Error::ContextPoisoned)?;
wh.validate_projection_extent(l.weight, 2)?;
ih.validate_projection_extent(l.input, 16)?;
oh.validate_projection_extent(l.output, 4)?;
Self::observed_reject_aliases(l, [wh, ih, oh])?;
Ok([wh, ih, oh])
}
fn observed_reject_aliases(l: &Layout, [wh, ih, oh]: [ffi::BufferHandle; 3]) -> Result<()> {
if wh.overlaps(l.weight, ih, l.input)
|| oh.overlaps(l.output, wh, l.weight)
|| oh.overlaps(l.output, ih, l.input)
{
return Err(Error::OutputAliasesInput);
}
Ok([wh, ih, oh])
Ok(())
}
fn observed_host_input(&self, input: &DeviceBuffer<'_>, l: &Layout) -> Result<()> {