+
Skip to content

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

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载