Skip to content

Commit

Permalink
Xが4D、Yが1D、axisが0の場合に対応
Browse files Browse the repository at this point in the history
  • Loading branch information
Masato Hori committed Apr 20, 2018
1 parent 0b31de3 commit ee8b37f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
5 changes: 4 additions & 1 deletion python/test/function/test_broadcast_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def ref_broadcast_to(x, y, axis):
elif xs == 4:
if ys == 1:
if axis == 0:
pass
t = y[:, np.newaxis, np.newaxis, np.newaxis]
t.transpose()
return np.broadcast_to(t, x.shape)
elif axis == 1:
t = y[np.newaxis, :, np.newaxis, np.newaxis]
t.transpose()
Expand All @@ -72,6 +74,7 @@ def ref_broadcast_to(x, y, axis):
((2, 3, 4), (4), 2),
((2, 3, 4), (2, 3), 0),
((2, 3, 4), (3, 4), 1),
((2, 3, 4, 5), (2), 0),
((2, 3, 4, 5), (3), 1),
((2, 3, 4, 5), (5), 3),
((2, 3, 4, 5), (4, 5), 2),
Expand Down
20 changes: 10 additions & 10 deletions src/nbla/function/broadcast_to.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,14 @@ static void copy_block_to_tail(
}
}

// Copy each Y value to each channel
// Copy each Y value to each block
template <typename T>
static void copy_value_to_channel(
T* z, const T* y, Size_t chan, Size_t height, Size_t width) {
const Size_t size = height*width;
for (Size_t c=0; c<chan; c++) {
T val = y[c];
T* zchan = &z[c*size];
std::fill(zchan, zchan+size, val);
static void copy_value_to_block(
T* z, const T* y, Size_t y_num, Size_t block_size) {
for (Size_t i=0; i<y_num; i++) {
T val = y[i];
T* zblock = &z[i*block_size];
std::fill(zblock, zblock+block_size, val);
}
}

Expand Down Expand Up @@ -152,7 +151,7 @@ void BroadcastTo<T>::forward_impl(const Variables &inputs, const Variables &outp
case 0:
{
// X: (2,3,4) Y: (2) axis=0
copy_value_to_channel(z, y, xs[0], xs[1], xs[2]);
copy_value_to_block(z, y, xs[0], xs[1]*xs[2]);
}
break;
case 1:
Expand Down Expand Up @@ -221,6 +220,7 @@ void BroadcastTo<T>::forward_impl(const Variables &inputs, const Variables &outp
switch(axis_) {
case 0:
// X: (2,3,4,5) Y: (2) axis=0
copy_value_to_block(z, y, xs[0], xs[1]*xs[2]*xs[3]);
break;
case 1:
{
Expand All @@ -229,7 +229,7 @@ void BroadcastTo<T>::forward_impl(const Variables &inputs, const Variables &outp
const Size_t z_slice_size = xs[1]*xs[2]*xs[3];
for (Size_t i=0; i<xs[0]; i++) {
T* z_slice = &z[i*z_slice_size];
copy_value_to_channel(z_slice, y, xs[1], xs[2], xs[3]);
copy_value_to_block(z_slice, y, xs[1], xs[2]*xs[3]);
}
}
break;
Expand Down

0 comments on commit ee8b37f

Please sign in to comment.