use crate::{Error, Result, runtime::RuntimeHandle};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum StoragePrecision {
F32,
F16,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PrecisionPolicy {
pub storage: StoragePrecision,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct EffectivePrecision {
pub requested: PrecisionPolicy,
pub activation_storage: StoragePrecision,
pub accumulation: StoragePrecision,
pub optimizer_state: StoragePrecision,
pub scaler_required: bool,
}
pub struct PrecisionScope {
runtime: RuntimeHandle,
previous: Option<StoragePrecision>,
effective: EffectivePrecision,
active: bool,
}
impl PrecisionScope {
#[inline]
#[must_use]
pub const fn effective(&self) -> EffectivePrecision {
self.effective
}
pub fn close(&mut self) {
if self.active {
self.runtime.replace_precision_override(self.previous);
self.active = false;
}
}
}
impl Drop for PrecisionScope {
fn drop(&mut self) {
self.close();
}
}
impl PrecisionPolicy {
#[inline]
#[must_use]
pub const fn f32() -> Self {
Self {
storage: StoragePrecision::F32,
}
}
#[inline]
#[must_use]
pub const fn f16_storage() -> Self {
Self {
storage: StoragePrecision::F16,
}
}
pub fn resolve(&self, runtime: &crate::runtime::Runtime) -> Result<EffectivePrecision> {
match self.storage {
StoragePrecision::F32 => Ok(EffectivePrecision {
requested: *self,
activation_storage: StoragePrecision::F32,
accumulation: StoragePrecision::F32,
optimizer_state: StoragePrecision::F32,
scaler_required: false,
}),
StoragePrecision::F16 => {
if !runtime.handle().fp16_storage_capable() {
return Err(Error::invalid_argument(
"F16-storage precision is not admitted on this device: the enabled \
capability surface lacks shaderFloat16 + 16-bit storage-buffer access",
));
}
Ok(EffectivePrecision {
requested: *self,
activation_storage: StoragePrecision::F16,
accumulation: StoragePrecision::F32,
optimizer_state: StoragePrecision::F32,
scaler_required: true,
})
}
}
}
pub fn enter(&self, runtime: &crate::runtime::Runtime) -> Result<PrecisionScope> {
let effective = self.resolve(runtime)?;
let handle = runtime.handle();
let previous = handle.replace_precision_override(Some(self.storage));
Ok(PrecisionScope {
runtime: handle,
previous,
effective,
active: true,
})
}
pub fn current(runtime: &crate::runtime::Runtime) -> EffectivePrecision {
if let Some(storage) = runtime.handle().precision_override() {
let requested = Self { storage };
return requested.resolve(runtime).unwrap_or(EffectivePrecision {
requested,
activation_storage: StoragePrecision::F32,
accumulation: StoragePrecision::F32,
optimizer_state: StoragePrecision::F32,
scaler_required: false,
});
}
let requested =
match crate::core::env_flag::EnvFlag::get_string("OA_FORCE_PRECISION", "").as_str() {
"FP16" | "BF16" | "Q8" => Self::f16_storage(),
_ => Self::f32(),
};
match requested.resolve(runtime) {
Ok(effective) => effective,
Err(_) => EffectivePrecision {
requested,
activation_storage: StoragePrecision::F32,
accumulation: StoragePrecision::F32,
optimizer_state: StoragePrecision::F32,
scaler_required: false,
},
}
}
}
impl EffectivePrecision {
#[inline]
#[must_use]
pub const fn is_exact(&self) -> bool {
matches!(
(self.activation_storage, self.requested.storage),
(StoragePrecision::F32, StoragePrecision::F32)
| (StoragePrecision::F16, StoragePrecision::F16)
)
}
}