use crate::exec::context::ExecutionContext;
use crate::exec::policy::GradMode;
use crate::shapes::Dyn;
use crate::tensor::backend::StorageBackend;
use core::fmt::Debug;
use core::marker::PhantomData;
pub trait RequiresGrad:
GradJoin<Self, Output = Self> + 'static + Clone + Debug + Send + Sync + Eq + PartialEq
{
type Arg;
type Field: Clone + Debug + Send + Sync + PartialEq + Default;
fn requires_grad(grad: &Self::Field) -> bool;
fn init(arg: Self::Arg) -> Self::Field;
fn grad_mode(grad: &Self::Field) -> GradMode {
if Self::requires_grad(grad) {
GradMode::Enabled
} else {
GradMode::Disabled
}
}
}
pub(crate) fn execution_context<B, G>(grad: &G::Field) -> ExecutionContext<B>
where
B: StorageBackend + Default,
G: RequiresGrad,
{
let context = ExecutionContext::from_scope(B::default());
let mode = context.grad_mode().and(G::grad_mode(grad));
context.with_grad_mode(mode)
}
pub trait ConstRequiresGrad: RequiresGrad<Arg = ()> {
const REQUIRES_GRAD: bool;
}
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct Grad;
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct NoGrad;
impl RequiresGrad for Dyn {
type Arg = bool;
type Field = bool;
fn requires_grad(grad: &Self::Field) -> bool {
*grad
}
fn init(arg: Self::Arg) -> Self::Field {
arg
}
}
impl RequiresGrad for Grad {
type Arg = ();
type Field = PhantomData<Self>;
fn requires_grad(_: &Self::Field) -> bool {
true
}
fn init(_: Self::Arg) -> Self::Field {
PhantomData
}
}
impl RequiresGrad for NoGrad {
type Arg = ();
type Field = PhantomData<Self>;
fn requires_grad(_: &Self::Field) -> bool {
false
}
fn init(_: Self::Arg) -> Self::Field {
PhantomData
}
}
impl ConstRequiresGrad for Grad {
const REQUIRES_GRAD: bool = true;
}
impl ConstRequiresGrad for NoGrad {
const REQUIRES_GRAD: bool = false;
}
pub trait GradJoin<Rhs: RequiresGrad>: 'static + Send + Sync {
type Output: RequiresGrad;
fn join_field(
lhs: &<Self as RequiresGrad>::Field,
rhs: &Rhs::Field,
) -> <<Self as GradJoin<Rhs>>::Output as RequiresGrad>::Field
where
Self: RequiresGrad;
}
pub type JoinedGrad<L, R> = <L as GradJoin<R>>::Output;
impl<G2: RequiresGrad> GradJoin<G2> for Grad {
type Output = Grad;
fn join_field(_: &PhantomData<Grad>, _: &G2::Field) -> PhantomData<Grad> {
PhantomData
}
}
impl<G2: RequiresGrad> GradJoin<G2> for NoGrad {
type Output = G2;
fn join_field(_: &PhantomData<NoGrad>, rhs: &G2::Field) -> G2::Field {
rhs.clone()
}
}
impl GradJoin<Grad> for Dyn {
type Output = Grad;
fn join_field(_: &bool, _: &PhantomData<Grad>) -> PhantomData<Grad> {
PhantomData
}
}
impl GradJoin<NoGrad> for Dyn {
type Output = Dyn;
fn join_field(lhs: &bool, _: &PhantomData<NoGrad>) -> bool {
*lhs
}
}
impl GradJoin<Dyn> for Dyn {
type Output = Dyn;
fn join_field(lhs: &bool, rhs: &bool) -> bool {
*lhs || *rhs
}
}