mirror of
https://github.com/huggingface/candle.git
synced 2025-06-18 03:28:50 +00:00
Support for groups in conv-transpose1d. (#1731)
* Groups support in conv-transpose-1d. * Remove dangling file.
This commit is contained in:
@ -250,6 +250,7 @@ impl Tensor {
|
|||||||
out_padding,
|
out_padding,
|
||||||
*stride,
|
*stride,
|
||||||
*dilation,
|
*dilation,
|
||||||
|
/* groups */ 1,
|
||||||
)?;
|
)?;
|
||||||
let sum_grad = grads.or_insert(arg)?;
|
let sum_grad = grads.or_insert(arg)?;
|
||||||
*sum_grad = sum_grad.add(&grad_arg)?;
|
*sum_grad = sum_grad.add(&grad_arg)?;
|
||||||
|
@ -187,36 +187,16 @@ impl Tensor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Applies a 1D transposed convolution over the input tensor.
|
fn conv_transpose1d_single_group(
|
||||||
pub fn conv_transpose1d(
|
|
||||||
&self,
|
&self,
|
||||||
kernel: &Self,
|
kernel: &Self,
|
||||||
padding: usize,
|
params: &ParamsConvTranspose1D,
|
||||||
output_padding: usize,
|
|
||||||
stride: usize,
|
|
||||||
dilation: usize,
|
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
let (b_size, c_in, l_in) = self.dims3()?;
|
|
||||||
let (c_in_k, c_out, k_size) = kernel.dims3()?;
|
|
||||||
if c_in != c_in_k {
|
|
||||||
crate::bail!("in_channel mismatch between input ({c_in}) and kernel ({c_in_k})")
|
|
||||||
}
|
|
||||||
let params = ParamsConvTranspose1D {
|
|
||||||
b_size,
|
|
||||||
l_in,
|
|
||||||
k_size,
|
|
||||||
c_out,
|
|
||||||
c_in,
|
|
||||||
padding,
|
|
||||||
output_padding,
|
|
||||||
stride,
|
|
||||||
dilation,
|
|
||||||
};
|
|
||||||
let storage = self.storage().conv_transpose1d(
|
let storage = self.storage().conv_transpose1d(
|
||||||
self.layout(),
|
self.layout(),
|
||||||
&kernel.storage(),
|
&kernel.storage(),
|
||||||
kernel.layout(),
|
kernel.layout(),
|
||||||
¶ms,
|
params,
|
||||||
)?;
|
)?;
|
||||||
let op = BackpropOp::new2(self, kernel, |arg, kernel| Op::ConvTranspose1D {
|
let op = BackpropOp::new2(self, kernel, |arg, kernel| Op::ConvTranspose1D {
|
||||||
arg,
|
arg,
|
||||||
@ -230,6 +210,49 @@ impl Tensor {
|
|||||||
Ok(crate::tensor::from_storage(storage, out_dims, op, false))
|
Ok(crate::tensor::from_storage(storage, out_dims, op, false))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Applies a 1D transposed convolution over the input tensor.
|
||||||
|
pub fn conv_transpose1d(
|
||||||
|
&self,
|
||||||
|
kernel: &Self,
|
||||||
|
padding: usize,
|
||||||
|
output_padding: usize,
|
||||||
|
stride: usize,
|
||||||
|
dilation: usize,
|
||||||
|
groups: usize,
|
||||||
|
) -> Result<Self> {
|
||||||
|
let (c_in_k, c_out, k_size) = kernel.dims3()?;
|
||||||
|
let (b_size, c_in, l_in) = self.dims3()?;
|
||||||
|
if c_in != c_in_k {
|
||||||
|
crate::bail!("in_channel mismatch between input ({c_in}) and kernel ({c_in_k})")
|
||||||
|
}
|
||||||
|
if c_in % groups != 0 {
|
||||||
|
crate::bail!("in_channel {c_in} is not divisible by the number of groups")
|
||||||
|
}
|
||||||
|
let params = ParamsConvTranspose1D {
|
||||||
|
b_size,
|
||||||
|
l_in,
|
||||||
|
k_size,
|
||||||
|
c_out,
|
||||||
|
c_in: c_in / groups,
|
||||||
|
padding,
|
||||||
|
output_padding,
|
||||||
|
stride,
|
||||||
|
dilation,
|
||||||
|
};
|
||||||
|
if groups == 1 {
|
||||||
|
self.conv_transpose1d_single_group(kernel, ¶ms)
|
||||||
|
} else {
|
||||||
|
let blocks = self.chunk(groups, 1)?;
|
||||||
|
let kernel = kernel.chunk(groups, 0)?;
|
||||||
|
let blocks = blocks
|
||||||
|
.iter()
|
||||||
|
.zip(&kernel)
|
||||||
|
.map(|(block, kernel)| block.conv_transpose1d_single_group(kernel, ¶ms))
|
||||||
|
.collect::<Result<Vec<_>>>()?;
|
||||||
|
Tensor::cat(&blocks, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn conv2d_single_group(&self, kernel: &Self, params: &ParamsConv2D) -> Result<Self> {
|
fn conv2d_single_group(&self, kernel: &Self, params: &ParamsConv2D) -> Result<Self> {
|
||||||
let storage =
|
let storage =
|
||||||
self.storage()
|
self.storage()
|
||||||
|
@ -50,7 +50,7 @@ fn conv1d(dev: &Device) -> Result<()> {
|
|||||||
test_utils::to_vec1_round(&res.flatten_all()?, 4)?,
|
test_utils::to_vec1_round(&res.flatten_all()?, 4)?,
|
||||||
[2.4509, 2.6357, -1.3336, 4.1393, 0.5657, 1.8091, -1.1784, 3.5675, 0.5069, 3.3352]
|
[2.4509, 2.6357, -1.3336, 4.1393, 0.5657, 1.8091, -1.1784, 3.5675, 0.5069, 3.3352]
|
||||||
);
|
);
|
||||||
let res = t.conv_transpose1d(&w.transpose(0, 1)?, 0, 0, 1, 1)?;
|
let res = t.conv_transpose1d(&w.transpose(0, 1)?, 0, 0, 1, 1, 1)?;
|
||||||
assert_eq!(res.dims(), [1, 2, 7]);
|
assert_eq!(res.dims(), [1, 2, 7]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
test_utils::to_vec1_round(&res.flatten_all()?, 4)?,
|
test_utils::to_vec1_round(&res.flatten_all()?, 4)?,
|
||||||
|
@ -76,7 +76,7 @@ pub struct ConvTranspose1dConfig {
|
|||||||
pub output_padding: usize,
|
pub output_padding: usize,
|
||||||
pub stride: usize,
|
pub stride: usize,
|
||||||
pub dilation: usize,
|
pub dilation: usize,
|
||||||
// TODO: support groups.
|
pub groups: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ConvTranspose1dConfig {
|
impl Default for ConvTranspose1dConfig {
|
||||||
@ -86,6 +86,7 @@ impl Default for ConvTranspose1dConfig {
|
|||||||
output_padding: 0,
|
output_padding: 0,
|
||||||
stride: 1,
|
stride: 1,
|
||||||
dilation: 1,
|
dilation: 1,
|
||||||
|
groups: 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -127,6 +128,7 @@ impl crate::Module for ConvTranspose1d {
|
|||||||
self.config.output_padding,
|
self.config.output_padding,
|
||||||
self.config.stride,
|
self.config.stride,
|
||||||
self.config.dilation,
|
self.config.dilation,
|
||||||
|
self.config.groups,
|
||||||
)?;
|
)?;
|
||||||
match &self.bias {
|
match &self.bias {
|
||||||
None => Ok(x),
|
None => Ok(x),
|
||||||
@ -346,7 +348,11 @@ pub fn conv_transpose1d(
|
|||||||
lo: -bound,
|
lo: -bound,
|
||||||
up: bound,
|
up: bound,
|
||||||
};
|
};
|
||||||
let ws = vb.get_with_hints((in_channels, out_channels, kernel_size), "weight", init)?;
|
let ws = vb.get_with_hints(
|
||||||
|
(in_channels, out_channels / cfg.groups, kernel_size),
|
||||||
|
"weight",
|
||||||
|
init,
|
||||||
|
)?;
|
||||||
let bs = vb.get_with_hints(out_channels, "bias", init)?;
|
let bs = vb.get_with_hints(out_channels, "bias", init)?;
|
||||||
Ok(ConvTranspose1d::new(ws, Some(bs), cfg))
|
Ok(ConvTranspose1d::new(ws, Some(bs), cfg))
|
||||||
}
|
}
|
||||||
@ -363,7 +369,11 @@ pub fn conv_transpose1d_no_bias(
|
|||||||
lo: -bound,
|
lo: -bound,
|
||||||
up: bound,
|
up: bound,
|
||||||
};
|
};
|
||||||
let ws = vb.get_with_hints((in_channels, out_channels, kernel_size), "weight", init)?;
|
let ws = vb.get_with_hints(
|
||||||
|
(in_channels, out_channels / cfg.groups, kernel_size),
|
||||||
|
"weight",
|
||||||
|
init,
|
||||||
|
)?;
|
||||||
Ok(ConvTranspose1d::new(ws, None, cfg))
|
Ok(ConvTranspose1d::new(ws, None, cfg))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user