diff --git a/paddle/framework/tensor_util.h b/paddle/framework/tensor_util.h index f541d2ba693a169d074c070dd794a2dd4e52aabf..091b63bf0f907a5449f08f0e36abb6577fa5e43e 100644 --- a/paddle/framework/tensor_util.h +++ b/paddle/framework/tensor_util.h @@ -116,8 +116,8 @@ inline void Copy(const Tensor& src, const platform::Place& dst_place, * @param[in] src The external tensor. * @param[in] ctx The device context contains device resources. * - * * @note CopyFromVector assumes that the tensor has been resized - * before invoking. + * * @note CopyFromVector will resize dst to an 1D tensor with the same + * size as src. */ template inline void CopyFromVector(const std::vector& src, diff --git a/paddle/operators/assign_value_op.cc b/paddle/operators/assign_value_op.cc index 7f6d351f505278a2c887dac1bcefc9728b933b64..d5671c1183a0f58d2aedb0723bd462684ac5636e 100644 --- a/paddle/operators/assign_value_op.cc +++ b/paddle/operators/assign_value_op.cc @@ -63,20 +63,11 @@ $$Out = values$$ } }; -template -class AssignValueCPUKernel : public AssignValueKernel { - protected: - virtual void Copy(void *dst, const void *src, size_t size, - const framework::ExecutionContext &ctx) const { - std::memcpy(dst, src, size); - } -}; - } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OPERATOR(assign_value, ops::AssignValueOp, ops::AssignValueOpMaker); -REGISTER_OP_CPU_KERNEL(assign_value, ops::AssignValueCPUKernel, - ops::AssignValueCPUKernel) +REGISTER_OP_CPU_KERNEL(assign_value, ops::AssignValueKernel, + ops::AssignValueKernel); diff --git a/paddle/operators/assign_value_op.cu.cc b/paddle/operators/assign_value_op.cu.cc index 8afb032037fe415a128919939fd14b5ac46faa4b..b17e20150053cea4c6b9ed6a5f222f77f4a4bd36 100644 --- a/paddle/operators/assign_value_op.cu.cc +++ b/paddle/operators/assign_value_op.cu.cc @@ -14,23 +14,6 @@ limitations under the License. */ #include "paddle/operators/assign_value_op.h" -namespace paddle { -namespace operators { - -template -class AssignValueGPUKernel : public AssignValueKernel { - protected: - virtual void Copy(void* dst, const void* src, size_t size, - const framework::ExecutionContext& ctx) const { - auto& dev_ctx = ctx.template device_context(); - paddle::platform::GpuMemcpyAsync(dst, src, size, cudaMemcpyHostToDevice, - dev_ctx.stream()); - } -}; - -} // namespace operators -} // namespace paddle - namespace ops = paddle::operators; -REGISTER_OP_CUDA_KERNEL(assign_value, ops::AssignValueGPUKernel, - ops::AssignValueGPUKernel); +REGISTER_OP_CUDA_KERNEL(assign_value, ops::AssignValueKernel, + ops::AssignValueKernel); diff --git a/paddle/operators/assign_value_op.h b/paddle/operators/assign_value_op.h index bdb5bce272fb997e5fb1fa82540f56c42117b9e4..db2e43077999fa0f9aaada74026dd701ab2bf464 100644 --- a/paddle/operators/assign_value_op.h +++ b/paddle/operators/assign_value_op.h @@ -27,8 +27,6 @@ class AssignValueKernel : public framework::OpKernel { virtual void Compute(const framework::ExecutionContext& ctx) const { auto shape = ctx.Attr>("shape"); auto* out = ctx.Output("Out"); - out->Resize(framework::make_ddim(shape)); - auto* dst = out->mutable_data(ctx.GetPlace()); int dtype = ctx.Attr("dtype"); const char* value_name = nullptr; switch (dtype) { @@ -43,12 +41,9 @@ class AssignValueKernel : public framework::OpKernel { break; } auto values = ctx.Attr>(value_name); - Copy(dst, values.data(), sizeof(T) * values.size(), ctx); + framework::CopyFromVector(values, ctx.device_context(), out); + out->Resize(framework::make_ddim(shape)); } - - protected: - virtual void Copy(void* dst, const void* src, size_t size, - const framework::ExecutionContext& ctx) const = 0; }; } // namespace operators