Skip to content

feat(conv): 支持无 bias 的 conv #22

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
15 changes: 10 additions & 5 deletions operators/src/conv/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub struct Args<H: Hardware> {
pub x_base: ConstPtr<H>,
pub w_layout: TensorLayout,
pub w_base: ConstPtr<H>,
pub b_layout: TensorLayout,
pub b_base: ConstPtr<H>,
pub b_layout: Option<TensorLayout>,
pub b_base: Option<ConstPtr<H>>,
pub strides: [usize; 2],
pub dilations: [usize; 2],
pub pads: [usize; 4],
Expand Down Expand Up @@ -50,12 +50,17 @@ impl<H: Hardware> Args<H> {
let &[m, ck, hk, wk] = &*w_layout.shape() else {
return Err(rank_error("w", 4, w_layout.ndim()));
};
let &[mb] = &*b_layout.shape() else {
return Err(rank_error("b", 1, b_layout.ndim()));
let (mb, b_layout_dt) = if let Some(b_layout) = b_layout {
let &[mb] = &*b_layout.shape() else {
return Err(rank_error("b", 1, b_layout.ndim()));
};
(mb, b_layout.dt)
} else {
(m, y_layout.dt)
};

Ok(Meta {
dt: type_distinct(&[y_layout.dt, x_layout.dt, w_layout.dt, b_layout.dt])?,
dt: type_distinct(&[y_layout.dt, x_layout.dt, w_layout.dt, b_layout_dt])?,
n: dim_distinct(&[n, ny]).expect("n mismatch"),
m: dim_distinct(&[m, my, mb]).expect("m mismatch"),
c: dim_distinct(&[c, ck]).expect("c mismatch"),
Expand Down
36 changes: 19 additions & 17 deletions operators/src/conv/im2col.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ where
let &[mks, cks, hks, wks] = w_layout.strides() else {
unreachable!()
};
let &[mbs] = b_layout.strides() else {
unreachable!()
};

// 计算考虑空洞的 kernel size

Expand Down Expand Up @@ -147,19 +144,24 @@ where
let b_dst = TensorLayout { dt, layout: b_dst };
let b_src = TensorLayout { dt, layout: b_src };

// b 布局广播
let b = Arr4::new(&[n, m, hy * wy], &[0, mbs, 0], 0);
// 广播 b
self.rearrange.launch(
&rearrange::Args {
dst_layout: c_y.clone(),
dst_base: *y_base,
src_layout: TensorLayout::new(dt, b.shape(), b.strides()),
src_base: *b_base,
},
workspace,
queue_alloc,
)?;
if let (Some(b_layout), Some(b_base)) = (b_layout, b_base) {
let &[mbs] = b_layout.strides() else {
unreachable!()
};
// b 布局广播
let b = Arr4::new(&[n, m, hy * wy], &[0, mbs, 0], 0);
// 广播 b
self.rearrange.launch(
&rearrange::Args {
dst_layout: c_y.clone(),
dst_base: *y_base,
src_layout: TensorLayout::new(dt, b.shape(), b.strides()),
src_base: *b_base,
},
workspace,
queue_alloc,
)?;
}

// 为 im2col 分配工作空间
let b_size = b_shape.iter().product::<usize>() * ele;
Expand All @@ -181,7 +183,7 @@ where
&mat_mul::Args {
c_layout: c_y.clone(),
c_base: *y_base,
beta: 1.,
beta: if b_layout.is_some() { 1. } else { 0. },
a_layout: a_w,
a_base: *w_base,
b_layout: b_x,
Expand Down