From a6c11a5d95e9f1b62589a42305e5a9b97a4194f5 Mon Sep 17 00:00:00 2001 From: yuyang18 Date: Wed, 30 May 2018 16:22:41 +0800 Subject: [PATCH] Fix bug in CUDA --- paddle/fluid/operators/random_crop_op.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/paddle/fluid/operators/random_crop_op.h b/paddle/fluid/operators/random_crop_op.h index e0e24a7d1fa..f3261cbdc98 100644 --- a/paddle/fluid/operators/random_crop_op.h +++ b/paddle/fluid/operators/random_crop_op.h @@ -67,7 +67,7 @@ HOSTDEVICE inline void StridedMemcpy(const T* x, const size_t* x_dims, T* out, } } else { x += offset_i * x_stride; - for (size_t j = 0; j < x_dim_i; ++j) { + for (size_t j = 0; j < out_dim_i; ++j) { StridedMemcpy(x, x_dims, out, out_dims, i + 1, rank, x_stride, out_stride, offsets); x += x_stride; @@ -86,8 +86,6 @@ struct RandomCropFunctor { int rank_; int64_t seed_; - size_t prod_x_dims_; - size_t prod_out_dims_; size_t prod_batchsize_dims_; size_t prod_x_ins_dims_; size_t prod_out_ins_dims_; @@ -118,8 +116,6 @@ struct RandomCropFunctor { prod_out_ins_dims_ *= out_dim_i; } } - prod_x_dims_ = prod_batchsize_dims_ * prod_x_ins_dims_; - prod_out_dims_ = prod_batchsize_dims_ * prod_out_ins_dims_; } HOSTDEVICE void operator()(size_t ins_idx) { @@ -146,7 +142,17 @@ template class RandomCropKernel : public framework::OpKernel { public: virtual void Compute(const framework::ExecutionContext& ctx) const { - int64_t seed = *ctx.Input("Seed")->data(); + auto& seed_tensor = detail::Ref(ctx.Input("Seed")); + int64_t seed = 0; + if (platform::is_cpu_place(seed_tensor.place())) { + seed = *seed_tensor.data(); + } else { + LOG(WARNING) << "It is slow to place seed in GPU memory. Please verify " + "your program"; + framework::LoDTensor cpu_seed; + framework::TensorCopySync(seed_tensor, platform::CPUPlace(), &cpu_seed); + seed = *cpu_seed.data(); + } auto shape = ctx.Attr>("shape"); auto& x = detail::Ref(ctx.Input("X")); auto& out = detail::Ref(ctx.Output("Out")); -- GitLab