-
Notifications
You must be signed in to change notification settings - Fork 732
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
fix_bug_in_broadcast_min_max_grad_and_broadcast_like #8379
Changes from 1 commit
e444f31
9f3f01c
4fb7bf2
325f2fa
63c1444
2eb6472
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -429,25 +429,36 @@ class BroadcastLikeFunctor { | |
Maybe<Tensor> operator()(const std::shared_ptr<one::Tensor>& x, | ||
const std::shared_ptr<one::Tensor>& like, | ||
const std::vector<int32_t>& broadcast_axes) const { | ||
const Shape& x_shape = *x->shape(); | ||
const Shape& like_shape = *like->shape(); | ||
if (x_shape == like_shape) { return x; } | ||
MutableAttrMap attrs; | ||
if (broadcast_axes.empty()) { | ||
int64_t like_ndim = like->shape()->NumAxes(); | ||
int64_t x_ndim = x->shape()->NumAxes(); | ||
int64_t like_ndim = like_shape.NumAxes(); | ||
int64_t x_ndim = x_shape.NumAxes(); | ||
int64_t num_prepend = like_ndim - x_ndim; | ||
std::vector<int64_t> prepend_shape(num_prepend, 1); | ||
std::vector<int64_t> broadcast_axes; | ||
for (int i = 0; i < x_ndim; ++i) { prepend_shape.emplace_back(x->shape()->At(i)); } | ||
std::vector<int32_t> broadcast_axes; | ||
for (int i = 0; i < x_ndim; ++i) { prepend_shape.emplace_back(x_shape.At(i)); } | ||
for (int i = 0; i < num_prepend; ++i) { broadcast_axes.emplace_back(i); } | ||
for (int i = num_prepend; i < prepend_shape.size(); ++i) { | ||
if (prepend_shape[i] != like->shape()->At(i)) { | ||
if (prepend_shape[i] == 1) { broadcast_axes.emplace_back(i); } | ||
CHECK_GE_OR_RETURN(prepend_shape[i], 1) | ||
<< Error::RuntimeError() << "output with shape " << x->shape()->ToString() | ||
<< " doesn't match the broadcast shape " << like->shape()->ToString(); | ||
if (prepend_shape[i] != like_shape.At(i)) { | ||
if (prepend_shape[i] == 1) { | ||
broadcast_axes.emplace_back(i); | ||
} else { | ||
return Error::RuntimeError() << "The expanded size of the tensor " | ||
<< "(" << like_shape.At(i) << ")" | ||
<< " must match the existing size (" << prepend_shape[i] | ||
<< ") at non-singleton dimension " << i | ||
<< ". Target sizes: " << like_shape.ToString() | ||
<< ". Tensor sizes: " << x_shape.ToString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里是不是多了个空格 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
是的,已修改 |
||
} | ||
} | ||
} | ||
JUST(attrs.SetAttr<std::vector<int32_t>>("broadcast_axes", broadcast_axes)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里主要改动是这里,broadcast_axes在本作用域中有效,需要在if里边 set attr |
||
} else { | ||
JUST(attrs.SetAttr<std::vector<int32_t>>("broadcast_axes", broadcast_axes)); | ||
} | ||
JUST(attrs.SetAttr<std::vector<int32_t>>("broadcast_axes", broadcast_axes)); | ||
return OpInterpUtil::Dispatch<Tensor>(*op_, {x, JUST(like->detach())}, attrs); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bug根源就是这里,如果left_extended_x_shape与out_shape一致,则无需BroadcastLike,直接reshape即可