summaryrefslogtreecommitdiff
path: root/candle-nn/src/conv.rs
diff options
context:
space:
mode:
authorLaurent Mazare <laurent.mazare@gmail.com>2023-08-06 18:49:43 +0200
committerGitHub <noreply@github.com>2023-08-06 17:49:43 +0100
commitd34039e35267b3f4de83770f8da4ea31491bcec5 (patch)
tree6efc4796859a04223d2211303cc12b3a97321fcc /candle-nn/src/conv.rs
parent93cfe5642f473889d1df62ccb8f1740f77523dd3 (diff)
downloadcandle-d34039e35267b3f4de83770f8da4ea31491bcec5.tar.gz
candle-d34039e35267b3f4de83770f8da4ea31491bcec5.tar.bz2
candle-d34039e35267b3f4de83770f8da4ea31491bcec5.zip
Add a stable diffusion example (#328)
* Start adding a stable-diffusion example. * Proper computation of the causal mask. * Add the chunk operation. * Work in progress: port the attention module. * Add some dummy modules for conv2d and group-norm, get the attention module to compile. * Re-enable the 2d convolution. * Add the embeddings module. * Add the resnet module. * Add the unet blocks. * Add the unet. * And add the variational auto-encoder. * Use the pad function from utils.
Diffstat (limited to 'candle-nn/src/conv.rs')
-rw-r--r--candle-nn/src/conv.rs81
1 files changed, 81 insertions, 0 deletions
diff --git a/candle-nn/src/conv.rs b/candle-nn/src/conv.rs
index 8fbe7659..6e1fcf51 100644
--- a/candle-nn/src/conv.rs
+++ b/candle-nn/src/conv.rs
@@ -48,3 +48,84 @@ impl Conv1d {
}
}
}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct Conv2dConfig {
+ pub padding: usize,
+ pub stride: usize,
+}
+
+impl Default for Conv2dConfig {
+ fn default() -> Self {
+ Self {
+ padding: 0,
+ stride: 1,
+ }
+ }
+}
+
+#[allow(dead_code)]
+#[derive(Debug)]
+pub struct Conv2d {
+ weight: Tensor,
+ bias: Option<Tensor>,
+ config: Conv2dConfig,
+}
+
+impl Conv2d {
+ pub fn new(weight: Tensor, bias: Option<Tensor>, config: Conv2dConfig) -> Self {
+ Self {
+ weight,
+ bias,
+ config,
+ }
+ }
+
+ pub fn config(&self) -> &Conv2dConfig {
+ &self.config
+ }
+
+ pub fn forward(&self, _x: &Tensor) -> Result<Tensor> {
+ todo!()
+ }
+}
+
+pub fn conv1d(
+ in_channels: usize,
+ out_channels: usize,
+ kernel_size: usize,
+ cfg: Conv1dConfig,
+ vs: crate::VarBuilder,
+) -> Result<Conv1d> {
+ let init_ws = crate::init::DEFAULT_KAIMING_NORMAL;
+ let ws = vs.get_or_init((out_channels, in_channels, kernel_size), "weight", init_ws)?;
+ let bound = 1. / (in_channels as f64).sqrt();
+ let init_bs = crate::Init::Uniform {
+ lo: -bound,
+ up: bound,
+ };
+ let bs = vs.get_or_init(out_channels, "bias", init_bs)?;
+ Ok(Conv1d::new(ws, Some(bs), cfg))
+}
+
+pub fn conv2d(
+ in_channels: usize,
+ out_channels: usize,
+ kernel_size: usize,
+ cfg: Conv2dConfig,
+ vs: crate::VarBuilder,
+) -> Result<Conv2d> {
+ let init_ws = crate::init::DEFAULT_KAIMING_NORMAL;
+ let ws = vs.get_or_init(
+ (out_channels, in_channels, kernel_size, kernel_size),
+ "weight",
+ init_ws,
+ )?;
+ let bound = 1. / (in_channels as f64).sqrt();
+ let init_bs = crate::Init::Uniform {
+ lo: -bound,
+ up: bound,
+ };
+ let bs = vs.get_or_init(out_channels, "bias", init_bs)?;
+ Ok(Conv2d::new(ws, Some(bs), cfg))
+}