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

Adding nn builder structs, dtype generics, and remove device defaults. #433

Merged
merged 13 commits into from
Feb 7, 2023

Conversation

coreylowman
Copy link
Owner

Resolves #419

In order to support multiple dtypes, the nn api needs to have a generic over dtype, similar to tensors. Additionally, the device generic being defaulted to Cpu was causing issues with other APIs, so this PR removes it.

The above two changes meant that any specification of a nn module required also specifying the dtype & device, which could quickly grow out of hand.

This PR thus introduces a number of "builder" unit structs that implement the BuildOnDevice trait, so you don't have to specify either dtype or device.

Here is Conv2D as an example:

pub mod builder {
    #[derive(Debug)]
    pub struct Conv2D<
        const IN_CHAN: usize,
        const OUT_CHAN: usize,
        const KERNEL_SIZE: usize,
        const STRIDE: usize = 1,
        const PADDING: usize = 0,
    >;
}

impl<const I: usize, const O: usize, const K: usize, const S: usize, const P: usize, D>
    BuildOnDevice<D, f32> for builder::Conv2D<I, O, K, S, P>
where
    D: Device<f32>,
{
    type Built = Conv2D<I, O, K, S, P, f32, D>;
    fn try_build_on_device(device: &D) -> Result<Self::Built, <D>::Err> {
        Self::Built::try_build(device)
    }
}

Here we see that we can get default generics for free on builder structs, and utilize BuildOnDevice to build the actual struct.

Considerations for future

  • we now have nn::conv::builder::Conv2D and nn::conv::Conv2D, which have the same name. this could cause confusion
  • nn now exposes two sub-modules: modules and builders. The prior should be used when building a new network layer, and the second should be used when declaring a network structure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove Device default value for layers in src/nn
1 participant