+
Skip to content
Merged
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
74 changes: 34 additions & 40 deletions src/tensor_ops/conv2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ pub(super) trait Conv2DKernel<E: Dtype>: DeviceStorage {
) -> Result<(), Self::Err>;
}

pub trait ConvAlgebra<const K: usize, const S: usize, const P: usize>: ConstDim {
type Convolved: ConstDim;
pub trait ConvAlgebra<const K: usize, const S: usize, const P: usize>: Dim {
type Convolved: Dim;

fn convolve_dim(&self) -> Self::Convolved;
}

impl<const D: usize, const K: usize, const S: usize, const P: usize> ConvAlgebra<K, S, P>
Expand All @@ -88,6 +90,18 @@ where
Const<{ (D + 2 * P - K) / S + 1 }>: Sized,
{
type Convolved = Const<{ (D + 2 * P - K) / S + 1 }>;

fn convolve_dim(&self) -> Self::Convolved {
Self::Convolved::default()
}
}

impl<const K: usize, const S: usize, const P: usize> ConvAlgebra<K, S, P> for usize {
type Convolved = usize;

fn convolve_dim(&self) -> Self::Convolved {
(self.size() + 2 * P).checked_sub(K).unwrap() / S + 1
}
}

pub trait TryConv2DTo<F, const S: usize, const P: usize>: HasErr {
Expand Down Expand Up @@ -120,42 +134,33 @@ impl<T, F> TryConv2D<F> for T {}

impl<
const C: usize,
const H: usize,
const W: usize,
H: Dim + ConvAlgebra<K, S, P>,
W: Dim + ConvAlgebra<K, S, P>,
const O: usize,
const K: usize,
const S: usize,
const P: usize,
E: Dtype,
D: Conv2DKernel<E> + ZerosTensor<E>,
T: 'static + Tape<E, D>,
> TryConv2DTo<Tensor<Rank4<O, C, K, K>, E, D>, S, P> for Tensor<Rank3<C, H, W>, E, D, T>
where
Const<H>: ConvAlgebra<K, S, P>,
Const<W>: ConvAlgebra<K, S, P>,
> TryConv2DTo<Tensor<Rank4<O, C, K, K>, E, D>, S, P> for Tensor<(Const<C>, H, W), E, D, T>
{
type Output = Tensor<
(
Const<O>,
<Const<H> as ConvAlgebra<K, S, P>>::Convolved,
<Const<W> as ConvAlgebra<K, S, P>>::Convolved,
),
E,
D,
T,
>;
type Output = Tensor<(Const<O>, H::Convolved, W::Convolved), E, D, T>;

fn try_conv2d_to(
self,
filters: Tensor<Rank4<O, C, K, K>, E, D>,
) -> Result<Self::Output, Self::Err> {
let op = Conv2DOp::new(S, P, K, [1, C, H, W], O);
let h = self.shape.1;
let w = self.shape.2;

let op = Conv2DOp::new(S, P, K, [1, C, h.size(), w.size()], O);
let (lhs, ltape) = self.split_tape();
let (rhs, rtape) = filters.split_tape();
let mut tape = ltape.merge(rtape);
let mut out = lhs
.device
.alloc((Const, Default::default(), Default::default()))?;
.alloc((Const, h.convolve_dim(), w.convolve_dim()))?;
lhs.device.forward(op, &lhs, &rhs, &mut out)?;
let phantom_out = out.clone();
tape.try_alloc_grad(&lhs)?;
Expand All @@ -173,43 +178,32 @@ where
impl<
B: Dim,
const C: usize,
const H: usize,
const W: usize,
H: Dim + ConvAlgebra<K, S, P>,
W: Dim + ConvAlgebra<K, S, P>,
const O: usize,
const K: usize,
const S: usize,
const P: usize,
E: Dtype,
D: Conv2DKernel<E> + ZerosTensor<E>,
T: 'static + Tape<E, D>,
> TryConv2DTo<Tensor<Rank4<O, C, K, K>, E, D>, S, P>
for Tensor<(B, Const<C>, Const<H>, Const<W>), E, D, T>
where
Const<H>: ConvAlgebra<K, S, P>,
Const<W>: ConvAlgebra<K, S, P>,
> TryConv2DTo<Tensor<Rank4<O, C, K, K>, E, D>, S, P> for Tensor<(B, Const<C>, H, W), E, D, T>
{
type Output = Tensor<
(
B,
Const<O>,
<Const<H> as ConvAlgebra<K, S, P>>::Convolved,
<Const<W> as ConvAlgebra<K, S, P>>::Convolved,
),
E,
D,
T,
>;
type Output = Tensor<(B, Const<O>, H::Convolved, W::Convolved), E, D, T>;
fn try_conv2d_to(
self,
filters: Tensor<Rank4<O, C, K, K>, E, D>,
) -> Result<Self::Output, Self::Err> {
let batch = self.shape().0;
let op = Conv2DOp::new(S, P, K, [batch.size(), C, H, W], O);
let h = self.shape().2;
let w = self.shape().3;

let op = Conv2DOp::new(S, P, K, [batch.size(), C, h.size(), w.size()], O);
let (lhs, ltape) = self.split_tape();
let (rhs, rtape) = filters.split_tape();
let mut out = lhs
.device
.alloc((batch, Const, Default::default(), Default::default()))?;
.alloc((batch, Const, h.convolve_dim(), w.convolve_dim()))?;
let mut tape = ltape.merge(rtape);
lhs.device.forward(op, &lhs, &rhs, &mut out)?;
let phantom_out = out.clone();
Expand Down
57 changes: 19 additions & 38 deletions src/tensor_ops/pool2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,37 +85,28 @@ macro_rules! pool2d {

impl<
C: Dim,
const H: usize,
const W: usize,
H: Dim + ConvAlgebra<K, S, P>,
W: Dim + ConvAlgebra<K, S, P>,
E: Dtype,
D: $Kernel<E> + ZerosTensor<E>,
T: 'static + Tape<E, D>,
const K: usize,
const S: usize,
const P: usize,
> $ConstTrait<K, S, P> for Tensor<(C, Const<H>, Const<W>), E, D, T>
where
Const<H>: ConvAlgebra<K, S, P>,
Const<W>: ConvAlgebra<K, S, P>,
> $ConstTrait<K, S, P> for Tensor<(C, H, W), E, D, T>
{
type Output = Tensor<
(
C,
<Const<H> as ConvAlgebra<K, S, P>>::Convolved,
<Const<W> as ConvAlgebra<K, S, P>>::Convolved,
),
E,
D,
T,
>;
type Output = Tensor<(C, H::Convolved, W::Convolved), E, D, T>;

fn try_pool2d(self) -> Result<Self::Output, Self::Err> {
let h = self.shape.1;
let w = self.shape.2;

let &(chan, _, _) = self.shape();
let op = Pool2DOp::new(K, S, P, [1, chan.size(), H, W]);
let op = Pool2DOp::new(K, S, P, [1, chan.size(), h.size(), w.size()]);
let (inp, mut tape) = self.split_tape();
let mut out =
inp.device
.try_zeros_like(&(chan, Default::default(), Default::default()))?;
.try_zeros_like(&(chan, h.convolve_dim(), w.convolve_dim()))?;
inp.device.forward(op, &inp, &mut out)?;
let phantom_out = out.clone();
tape.try_alloc_grad(&inp)?;
Expand All @@ -132,40 +123,30 @@ macro_rules! pool2d {
impl<
B: Dim,
C: Dim,
const H: usize,
const W: usize,
H: Dim + ConvAlgebra<K, S, P>,
W: Dim + ConvAlgebra<K, S, P>,
E: Dtype,
D: $Kernel<E> + ZerosTensor<E>,
T: 'static + Tape<E, D>,
const K: usize,
const S: usize,
const P: usize,
> $ConstTrait<K, S, P> for Tensor<(B, C, Const<H>, Const<W>), E, D, T>
where
Const<H>: ConvAlgebra<K, S, P>,
Const<W>: ConvAlgebra<K, S, P>,
> $ConstTrait<K, S, P> for Tensor<(B, C, H, W), E, D, T>
{
type Output = Tensor<
(
B,
C,
<Const<H> as ConvAlgebra<K, S, P>>::Convolved,
<Const<W> as ConvAlgebra<K, S, P>>::Convolved,
),
E,
D,
T,
>;
type Output = Tensor<(B, C, H::Convolved, W::Convolved), E, D, T>;

fn try_pool2d(self) -> Result<Self::Output, Self::Err> {
let h = self.shape.2;
let w = self.shape.3;

let &(batch, chan, _, _) = self.shape();
let op = Pool2DOp::new(K, S, P, [batch.size(), chan.size(), H, W]);
let op = Pool2DOp::new(K, S, P, [batch.size(), chan.size(), h.size(), w.size()]);
let (inp, mut tape) = self.split_tape();
let mut out = inp.device.try_zeros_like(&(
batch,
chan,
Default::default(),
Default::default(),
h.convolve_dim(),
w.convolve_dim(),
))?;
inp.device.forward(op, &inp, &mut out)?;
let phantom_out = out.clone();
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载