Skip to content

Commit

Permalink
fix trace offset out of shape (#33922)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff41404 committed Jul 2, 2021
1 parent fcdbc8d commit b9e4aaa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions paddle/fluid/operators/trace_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/operators/reduce_ops/cub_reduce.h"
#include "paddle/fluid/operators/trace_op.h"

Expand Down Expand Up @@ -50,6 +51,9 @@ class TraceCUDAKernel : public framework::OpKernel<T> {
TensorReduce<T, T, cub::Sum, IdentityFunctor>(
diag, out, reduce_dims, static_cast<T>(0), cub::Sum(),
IdentityFunctor(), stream);
} else {
math::SetConstant<DeviceContext, T> functor;
functor(context.device_context<DeviceContext>(), out, static_cast<T>(0));
}
}
};
Expand Down
4 changes: 3 additions & 1 deletion paddle/fluid/operators/trace_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class TraceKernel : public framework::OpKernel<T> {

auto output_dims = out->dims();

out->mutable_data<T>(context.GetPlace());
T* out_data = out->mutable_data<T>(context.GetPlace());

const framework::Tensor diag =
Diagonal<DeviceContext, T>(context, input, offset, dim1, dim2);
Expand All @@ -191,6 +191,8 @@ class TraceKernel : public framework::OpKernel<T> {
auto reduce_dim = Eigen::array<int, 1>({1});
output.device(place) = x.sum(reduce_dim);
out->Resize(output_dims);
} else {
std::fill(out_data, out_data + out->numel(), static_cast<T>(0));
}
}
};
Expand Down

0 comments on commit b9e4aaa

Please sign in to comment.