Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tests pass on stable + support Pool2D & Conv{1,2}d #927

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions dfdx-core/src/shapes/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,81 @@ where
}
}

macro_rules! add_impl {
($X1:expr, $X2:expr, $Y:expr) => {
#[cfg(not(feature = "nightly"))]
impl core::ops::Add<Const<$X2>> for Const<$X1> {
type Output = Const<$Y>;
fn add(self, _: Const<$X2>) -> Self::Output {
Const
}
}
};
}

macro_rules! add_impl_2 {
($X1:expr, $X2:expr, $Y:expr) => {
add_impl!($X1, $X2, $Y);
add_impl!($X2, $X1, $Y);
};
}

add_impl!(1, 1, 2);
add_impl!(2, 2, 4);
add_impl!(3, 3, 6);
add_impl_2!(1, 2, 3);
add_impl_2!(1, 3, 4);
add_impl_2!(1, 4, 5);
add_impl_2!(1, 5, 6);
add_impl_2!(1, 6, 7);
add_impl_2!(2, 3, 5);
add_impl_2!(2, 4, 6);
add_impl_2!(2, 5, 7);
add_impl_2!(2, 6, 8);

macro_rules! mul_div_impl {
($X1:expr, $X2:expr, $Y:expr) => {
#[cfg(not(feature = "nightly"))]
impl core::ops::Mul<Const<$X2>> for Const<$X1> {
type Output = Const<$Y>;
fn mul(self, _: Const<$X2>) -> Self::Output {
Const
}
}

#[cfg(not(feature = "nightly"))]
impl core::ops::Div<Const<$X2>> for Const<$Y> {
type Output = Const<$X1>;
fn div(self, _: Const<$X2>) -> Self::Output {
Const
}
}
};
}

macro_rules! mul_div_impl_2 {
($X1:expr, $X2:expr, $Y:expr) => {
mul_div_impl!($X1, $X2, $Y);
mul_div_impl!($X2, $X1, $Y);
};
}

mul_div_impl!(1, 1, 1);
mul_div_impl!(2, 2, 4);
mul_div_impl!(3, 3, 9);
mul_div_impl!(4, 4, 16);
mul_div_impl!(5, 5, 25);
mul_div_impl_2!(1, 2, 2);
mul_div_impl_2!(1, 3, 3);
mul_div_impl_2!(2, 3, 6);
mul_div_impl_2!(1, 4, 4);
mul_div_impl_2!(2, 4, 8);
mul_div_impl_2!(3, 4, 12);
mul_div_impl_2!(1, 5, 5);
mul_div_impl_2!(2, 5, 10);
mul_div_impl_2!(3, 5, 15);
mul_div_impl_2!(4, 5, 20);

/// Represents either `[T; N]` or `Vec<T>`
pub trait Array<T>: IntoIterator<Item = T> {
type Dim: Dim;
Expand Down
34 changes: 34 additions & 0 deletions dfdx-core/src/tensor_ops/conv1d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,40 @@ where
}
}

macro_rules! const_try_conv {
($Dim:expr, $Kernel:expr, $Stride:expr, $Padding:expr, $Dilation:expr, out=$Out_dim:expr) => {
#[cfg(not(feature = "nightly"))]
impl<Groups: Dim> TryConv1D<Const<$Stride>, Const<$Padding>, Const<$Dilation>, Groups>
for (Const<$Dim>, Const<$Kernel>)
{
// ($Dim + 2 * $Padding - $Dilation * ($Kernel - 1) - 1) / $Stride + 1
// def compute_output_size(dim, kernel_size, stride, padding, dilation):
// output_size = int(int(dim + 2 * padding - dilation * (kernel_size - 1) - 1) / stride + 1)
// return output_size
type Convolved = Const<$Out_dim>;

fn try_conv1d(
self,
_: Const<$Stride>,
_: Const<$Padding>,
_: Const<$Dilation>,
_: Groups,
) -> Result<Self::Convolved, Error> {
Ok(Const)
}
}
};
}

const_try_conv!(1, 2, 1, 0, 1, out = 0);
const_try_conv!(1, 2, 1, 2, 1, out = 4);
const_try_conv!(2, 2, 1, 0, 1, out = 1);
const_try_conv!(3, 3, 1, 0, 1, out = 1);
const_try_conv!(3, 3, 1, 1, 1, out = 3);
const_try_conv!(5, 2, 2, 1, 2, out = 3);

const_try_conv!(28, 6, 3, 2, 1, out = 9); // needed for a test

impl<Kernel: Dim, Stride: Dim, Padding: Dim, Dilation: Dim, Groups: Dim>
TryConv1D<Stride, Padding, Dilation, Groups> for (usize, Kernel)
{
Expand Down
61 changes: 61 additions & 0 deletions dfdx-core/src/tensor_ops/conv2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub trait TryConv2D<Stride, Padding, Dilation, Groups>: Sized {
) -> Result<Self::Convolved, Error>;
}

#[cfg(feature = "nightly")]
impl<
const KERNEL: usize,
const STRIDE: usize,
Expand All @@ -140,6 +141,66 @@ where
}
}

macro_rules! const_try_conv {
($Dim:expr, $Kernel:expr, $Stride:expr, $Padding:expr, $Dilation:expr, out=$Out_dim:expr) => {
#[cfg(not(feature = "nightly"))]
impl<Groups: Dim> TryConv2D<Const<$Stride>, Const<$Padding>, Const<$Dilation>, Groups>
for (Const<$Dim>, Const<$Kernel>)
{
// ($Dim + 2 * $Padding - $Dilation * ($Kernel - 1) - 1) / $Stride + 1
// def compute_output_size(dim, kernel_size, stride, padding, dilation):
// output_size = int(int(dim + 2 * padding - dilation * (kernel_size - 1) - 1) / stride + 1)
// return output_size
type Convolved = Const<$Out_dim>;

fn try_conv2d(
self,
_: Const<$Stride>,
_: Const<$Padding>,
_: Const<$Dilation>,
_: Groups,
) -> Result<Self::Convolved, Error> {
Ok(Const)
}
}
};
}

const_try_conv!(1, 2, 1, 0, 1, out = 0);
const_try_conv!(2, 2, 1, 0, 1, out = 1);
const_try_conv!(3, 2, 1, 0, 1, out = 2);

const_try_conv!(1, 2, 1, 2, 1, out = 4);

const_try_conv!(1, 1, 1, 1, 1, out = 3);
const_try_conv!(1, 2, 1, 1, 1, out = 2);
const_try_conv!(2, 2, 1, 1, 1, out = 3);
const_try_conv!(1, 3, 1, 1, 1, out = 1);
const_try_conv!(2, 3, 1, 1, 1, out = 2);
const_try_conv!(3, 2, 1, 1, 1, out = 4);

const_try_conv!(5, 3, 1, 0, 1, out = 3);

const_try_conv!(2, 2, 2, 0, 1, out = 1);
const_try_conv!(3, 2, 2, 0, 1, out = 1);
const_try_conv!(4, 2, 2, 0, 1, out = 2);

const_try_conv!(4, 2, 1, 0, 2, out = 2);
const_try_conv!(5, 2, 1, 0, 2, out = 3);

const_try_conv!(2, 3, 3, 4, 1, out = 3);
const_try_conv!(4, 3, 3, 4, 1, out = 4);

const_try_conv!(6, 2, 4, 3, 1, out = 3);
const_try_conv!(7, 2, 4, 3, 1, out = 3);

const_try_conv!(14, 3, 1, 0, 1, out = 12);
const_try_conv!(28, 6, 3, 2, 1, out = 9);

const_try_conv!(3, 3, 1, 0, 1, out = 1);
const_try_conv!(3, 3, 1, 1, 1, out = 3);
const_try_conv!(5, 2, 2, 1, 2, out = 3);

impl<Kernel: Dim, Stride: Dim, Padding: Dim, Dilation: Dim, Groups: Dim>
TryConv2D<Stride, Padding, Dilation, Groups> for (usize, Kernel)
{
Expand Down
4 changes: 0 additions & 4 deletions dfdx-core/src/tensor_ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,13 @@ pub use var_to::VarTo;
mod conv1d;
pub use conv1d::TryConv1D;

#[cfg(feature = "nightly")]
mod conv2d;
#[cfg(feature = "nightly")]
pub use conv2d::TryConv2D;

#[cfg(feature = "nightly")]
mod convtrans2d;
#[cfg(feature = "nightly")]
pub use convtrans2d::TryConvTrans2D;

#[cfg(feature = "nightly")]
mod pool2d;
#[cfg(feature = "nightly")]
pub use pool2d::{Pool2DKind, TryPool2D};
45 changes: 45 additions & 0 deletions dfdx-core/src/tensor_ops/pool2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub trait TryPool2D<Kernel, Stride, Padding, Dilation>: Sized {
) -> Result<Self::Pooled, Error>;
}

#[cfg(feature = "nightly")]
impl<
const KERNEL: usize,
const STRIDE: usize,
Expand All @@ -100,6 +101,50 @@ where
}
}

macro_rules! const_try_pool {
($Dim:expr, $Kernel:expr, $Stride:expr, $Padding:expr, $Dilation:expr, out=$Out_dim:expr) => {
#[cfg(not(feature = "nightly"))]
impl TryPool2D<Const<$Kernel>, Const<$Stride>, Const<$Padding>, Const<$Dilation>>
for Const<$Dim>
{
// ($Dim + 2 * $Padding - $Dilation * ($Kernel - 1) - 1) / $Stride + 1
// def compute_output_size(dim, kernel_size, stride, padding, dilation):
// output_size = int(int(dim + 2 * padding - dilation * (kernel_size - 1) - 1) / stride + 1)
// return output_size
type Pooled = Const<$Out_dim>;

fn try_pool2d(
self,
_: Pool2DKind,
_: Const<$Kernel>,
_: Const<$Stride>,
_: Const<$Padding>,
_: Const<$Dilation>,
) -> Result<Self::Pooled, Error> {
Ok(Const)
}
}
};
}

const_try_pool!(1, 2, 1, 0, 1, out = 0);
const_try_pool!(2, 2, 1, 0, 1, out = 1);
const_try_pool!(3, 2, 1, 0, 1, out = 2);
const_try_pool!(4, 2, 1, 0, 1, out = 3);

const_try_pool!(1, 2, 2, 0, 1, out = 0);
const_try_pool!(2, 2, 2, 0, 1, out = 1);
const_try_pool!(3, 2, 2, 0, 1, out = 1);
const_try_pool!(4, 2, 2, 0, 1, out = 2);

const_try_pool!(1, 1, 2, 0, 1, out = 1);
const_try_pool!(2, 1, 2, 0, 1, out = 1);
const_try_pool!(3, 1, 2, 0, 1, out = 2);
const_try_pool!(4, 1, 2, 0, 1, out = 2);

const_try_pool!(4, 2, 1, 0, 2, out = 2);
const_try_pool!(5, 2, 1, 0, 2, out = 3);

impl<Kernel: Dim, Stride: Dim, Padding: Dim, Dilation: Dim>
TryPool2D<Kernel, Stride, Padding, Dilation> for usize
{
Expand Down
Loading