+
Skip to content

Minifying conv impls #213

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

Merged
merged 1 commit into from
Oct 4, 2022
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
133 changes: 40 additions & 93 deletions src/nn/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,144 +36,91 @@ pub struct Conv2D<
pub bias: Tensor1D<OUT_CHAN>,
}

impl<
const IN_CHAN: usize,
const OUT_CHAN: usize,
const KERNEL_SIZE: usize,
const STRIDE: usize,
const PADDING: usize,
> CanUpdateWithGradients for Conv2D<IN_CHAN, OUT_CHAN, KERNEL_SIZE, STRIDE, PADDING>
impl<const I: usize, const O: usize, const K: usize, const S: usize, const P: usize>
CanUpdateWithGradients for Conv2D<I, O, K, S, P>
{
fn update<G: GradientProvider>(&mut self, grads: &mut G, unused: &mut UnusedTensors) {
self.weight.update(grads, unused);
self.bias.update(grads, unused);
}
}

impl<
const IN_CHAN: usize,
const OUT_CHAN: usize,
const KERNEL_SIZE: usize,
const STRIDE: usize,
const PADDING: usize,
> ResetParams for Conv2D<IN_CHAN, OUT_CHAN, KERNEL_SIZE, STRIDE, PADDING>
impl<const I: usize, const O: usize, const K: usize, const S: usize, const P: usize> ResetParams
for Conv2D<I, O, K, S, P>
{
fn reset_params<R: Rng>(&mut self, rng: &mut R) {
let k = (IN_CHAN * KERNEL_SIZE * KERNEL_SIZE) as f32;
let k = (I * K * K) as f32;
let bound = 1.0 / k.sqrt();
let dist = Uniform::new(-bound, bound);
self.weight.randomize(rng, &dist);
self.bias.randomize(rng, &dist);
}
}

impl<
const IN_CHAN: usize,
const OUT_CHAN: usize,
const KERNEL_SIZE: usize,
const STRIDE: usize,
const PADDING: usize,
> SaveToNpz for Conv2D<IN_CHAN, OUT_CHAN, KERNEL_SIZE, STRIDE, PADDING>
impl<const I: usize, const O: usize, const K: usize, const S: usize, const P: usize> SaveToNpz
for Conv2D<I, O, K, S, P>
{
/// Saves [Self::weight] to `{pre}weight.npy` and [Self::bias] to `{pre}bias.npy`
/// using [npz_fwrite()].
fn write<W>(&self, pre: &str, w: &mut ZipWriter<W>) -> ZipResult<()>
where
W: Write + Seek,
{
fn write<W: Write + Seek>(&self, pre: &str, w: &mut ZipWriter<W>) -> ZipResult<()> {
npz_fwrite(w, format!("{pre}weight.npy"), self.weight.data())?;
npz_fwrite(w, format!("{pre}bias.npy"), self.bias.data())?;
Ok(())
}
}

impl<
const IN_CHAN: usize,
const OUT_CHAN: usize,
const KERNEL_SIZE: usize,
const STRIDE: usize,
const PADDING: usize,
> LoadFromNpz for Conv2D<IN_CHAN, OUT_CHAN, KERNEL_SIZE, STRIDE, PADDING>
impl<const I: usize, const O: usize, const K: usize, const S: usize, const P: usize> LoadFromNpz
for Conv2D<I, O, K, S, P>
{
/// Reads [Self::weight] from `{pre}weight.npy` and [Self::bias] from `{pre}bias.npy`
/// using [npz_fread()].
fn read<R>(&mut self, pre: &str, r: &mut ZipArchive<R>) -> Result<(), NpzError>
where
R: Read + Seek,
{
fn read<R: Read + Seek>(&mut self, pre: &str, r: &mut ZipArchive<R>) -> Result<(), NpzError> {
npz_fread(r, format!("{pre}weight.npy"), self.weight.mut_data())?;
npz_fread(r, format!("{pre}bias.npy"), self.bias.mut_data())?;
Ok(())
}
}

impl<
TAPE: 'static + Tape,
const IN_CHAN: usize,
const OUT_CHAN: usize,
const KERNEL_SIZE: usize,
const STRIDE: usize,
const PADDING: usize,
const IN_HEIGHT: usize,
const IN_WIDTH: usize,
> Module<Tensor3D<IN_CHAN, IN_HEIGHT, IN_WIDTH, TAPE>>
for Conv2D<IN_CHAN, OUT_CHAN, KERNEL_SIZE, STRIDE, PADDING>
T: Tape,
const I: usize,
const O: usize,
const K: usize,
const S: usize,
const P: usize,
const H: usize,
const W: usize,
> Module<Tensor3D<I, H, W, T>> for Conv2D<I, O, K, S, P>
where
[(); (IN_WIDTH + 2 * PADDING - KERNEL_SIZE) / STRIDE + 1]:,
[(); (IN_HEIGHT + 2 * PADDING - KERNEL_SIZE) / STRIDE + 1]:,
[(); (W + 2 * P - K) / S + 1]:,
[(); (H + 2 * P - K) / S + 1]:,
{
type Output = Tensor3D<
OUT_CHAN,
{ (IN_HEIGHT + 2 * PADDING - KERNEL_SIZE) / STRIDE + 1 },
{ (IN_WIDTH + 2 * PADDING - KERNEL_SIZE) / STRIDE + 1 },
TAPE,
>;
type Output = Tensor3D<O, { (H + 2 * P - K) / S + 1 }, { (W + 2 * P - K) / S + 1 }, T>;

fn forward(&self, x: Tensor3D<IN_CHAN, IN_HEIGHT, IN_WIDTH, TAPE>) -> Self::Output {
conv2d::<TAPE, IN_CHAN, OUT_CHAN, KERNEL_SIZE, STRIDE, PADDING, IN_HEIGHT, IN_WIDTH>(
x,
&self.weight,
&self.bias,
)
fn forward(&self, x: Tensor3D<I, H, W, T>) -> Self::Output {
conv2d::<T, I, O, K, S, P, H, W>(x, &self.weight, &self.bias)
}
}

impl<
TAPE: 'static + Tape,
const BATCH_SIZE: usize,
const IN_CHAN: usize,
const OUT_CHAN: usize,
const KERNEL_SIZE: usize,
const STRIDE: usize,
const PADDING: usize,
const IN_HEIGHT: usize,
const IN_WIDTH: usize,
> Module<Tensor4D<BATCH_SIZE, IN_CHAN, IN_HEIGHT, IN_WIDTH, TAPE>>
for Conv2D<IN_CHAN, OUT_CHAN, KERNEL_SIZE, STRIDE, PADDING>
T: Tape,
const B: usize,
const I: usize,
const O: usize,
const K: usize,
const S: usize,
const P: usize,
const H: usize,
const W: usize,
> Module<Tensor4D<B, I, H, W, T>> for Conv2D<I, O, K, S, P>
where
[(); (IN_WIDTH + 2 * PADDING - KERNEL_SIZE) / STRIDE + 1]:,
[(); (IN_HEIGHT + 2 * PADDING - KERNEL_SIZE) / STRIDE + 1]:,
[(); (W + 2 * P - K) / S + 1]:,
[(); (H + 2 * P - K) / S + 1]:,
{
type Output = Tensor4D<
BATCH_SIZE,
OUT_CHAN,
{ (IN_HEIGHT + 2 * PADDING - KERNEL_SIZE) / STRIDE + 1 },
{ (IN_WIDTH + 2 * PADDING - KERNEL_SIZE) / STRIDE + 1 },
TAPE,
>;
type Output = Tensor4D<B, O, { (H + 2 * P - K) / S + 1 }, { (W + 2 * P - K) / S + 1 }, T>;

fn forward(&self, x: Tensor4D<BATCH_SIZE, IN_CHAN, IN_HEIGHT, IN_WIDTH, TAPE>) -> Self::Output {
conv2d_batched::<
TAPE,
BATCH_SIZE,
IN_CHAN,
OUT_CHAN,
KERNEL_SIZE,
STRIDE,
PADDING,
IN_HEIGHT,
IN_WIDTH,
>(x, &self.weight, &self.bias)
fn forward(&self, x: Tensor4D<B, I, H, W, T>) -> Self::Output {
conv2d_batched::<T, B, I, O, K, S, P, H, W>(x, &self.weight, &self.bias)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/tensor_ops/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::prelude::*;
///
/// TODO docstring
pub fn conv2d<
TAPE: 'static + Tape,
TAPE: Tape,
const IN_CHAN: usize,
const OUT_CHAN: usize,
const KERNEL: usize,
Expand Down Expand Up @@ -48,7 +48,7 @@ pub fn conv2d<
///
/// TODO docstring
pub fn conv2d_batched<
TAPE: 'static + Tape,
TAPE: Tape,
const BATCH_SIZE: usize,
const IN_CHAN: usize,
const OUT_CHAN: usize,
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载