#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MathMode {
#[default]
Precise,
Fast,
}
impl MathMode {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Precise => "precise",
Self::Fast => "fast",
}
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Determinism {
#[default]
Permitted,
Required,
}
impl Determinism {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Permitted => "permitted",
Self::Required => "required",
}
}
#[must_use]
pub const fn is_required(self) -> bool {
matches!(self, Self::Required)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FallbackPolicy {
Deny,
#[default]
AllowComposition,
AllowTransfer,
}
impl FallbackPolicy {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Deny => "deny",
Self::AllowComposition => "allow-composition",
Self::AllowTransfer => "allow-transfer",
}
}
#[must_use]
pub const fn allows_composition(self) -> bool {
matches!(self, Self::AllowComposition | Self::AllowTransfer)
}
#[must_use]
pub const fn allows_transfer(self) -> bool {
matches!(self, Self::AllowTransfer)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum GradMode {
#[default]
Enabled,
Disabled,
}
impl GradMode {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Enabled => "enabled",
Self::Disabled => "disabled",
}
}
#[must_use]
pub const fn records(self) -> bool {
matches!(self, Self::Enabled)
}
#[must_use]
pub const fn and(self, other: Self) -> Self {
match (self, other) {
(Self::Enabled, Self::Enabled) => Self::Enabled,
_ => Self::Disabled,
}
}
#[must_use]
pub fn current() -> Self {
#[cfg(feature = "std")]
{
ExecutionPolicy::current().grad_mode
}
#[cfg(not(feature = "std"))]
{
Self::Enabled
}
}
#[inline]
pub fn restrict<R>(self, body: impl FnOnce() -> R) -> R {
#[cfg(feature = "std")]
{
match self {
Self::Enabled => body(),
Self::Disabled => self.scope(body),
}
}
#[cfg(not(feature = "std"))]
{
body()
}
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum NanPolicy {
#[default]
Permit,
Reject,
}
impl NanPolicy {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Permit => "permit",
Self::Reject => "reject",
}
}
#[must_use]
pub const fn checks(self) -> bool {
matches!(self, Self::Reject)
}
#[must_use]
pub fn current() -> Self {
#[cfg(feature = "std")]
{
ExecutionPolicy::current().nan_policy
}
#[cfg(not(feature = "std"))]
{
Self::Permit
}
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ExecutionPolicy {
pub math_mode: MathMode,
pub fallback: FallbackPolicy,
pub training: bool,
pub grad_mode: GradMode,
pub nan_policy: NanPolicy,
pub precision: crate::exec::RuntimePrecisionPolicy,
}
impl ExecutionPolicy {
#[must_use]
pub const fn new() -> Self {
Self {
math_mode: MathMode::Precise,
fallback: FallbackPolicy::AllowComposition,
training: false,
grad_mode: GradMode::Enabled,
nan_policy: NanPolicy::Permit,
precision: crate::exec::RuntimePrecisionPolicy::fp32(),
}
}
#[must_use]
pub const fn with_math_mode(mut self, math_mode: MathMode) -> Self {
self.math_mode = math_mode;
self
}
#[must_use]
pub const fn with_fallback(mut self, fallback: FallbackPolicy) -> Self {
self.fallback = fallback;
self
}
#[must_use]
pub const fn with_training(mut self, training: bool) -> Self {
self.training = training;
self
}
#[must_use]
pub const fn with_grad_mode(mut self, grad_mode: GradMode) -> Self {
self.grad_mode = grad_mode;
self
}
#[must_use]
pub const fn with_nan_policy(mut self, nan_policy: NanPolicy) -> Self {
self.nan_policy = nan_policy;
self
}
#[must_use]
pub const fn with_precision(mut self, precision: crate::exec::RuntimePrecisionPolicy) -> Self {
self.precision = precision;
self
}
#[must_use]
pub const fn precision(&self) -> crate::exec::RuntimePrecisionPolicy {
self.precision
}
}
#[cfg(feature = "std")]
mod scope {
use super::{ExecutionPolicy, GradMode, NanPolicy};
use core::cell::Cell;
std::thread_local! {
static CURRENT: Cell<ExecutionPolicy> = const { Cell::new(ExecutionPolicy::new()) };
}
struct Restore(ExecutionPolicy);
impl Drop for Restore {
fn drop(&mut self) {
CURRENT.with(|current| current.set(self.0));
}
}
impl ExecutionPolicy {
#[must_use]
pub fn current() -> Self {
CURRENT.with(Cell::get)
}
pub fn scope<R>(self, body: impl FnOnce() -> R) -> R {
let _restore = Restore(CURRENT.with(|current| current.replace(self)));
body()
}
}
impl NanPolicy {
pub fn scope<R>(self, body: impl FnOnce() -> R) -> R {
ExecutionPolicy::current().with_nan_policy(self).scope(body)
}
}
pub fn check_gradients<R>(body: impl FnOnce() -> R) -> R {
NanPolicy::Reject.scope(body)
}
impl GradMode {
pub fn scope<R>(self, body: impl FnOnce() -> R) -> R {
ExecutionPolicy::current().with_grad_mode(self).scope(body)
}
}
}
#[cfg(feature = "std")]
pub use scope::check_gradients;