| 29 | |
| 30 | template <typename T, typename Context> |
| 31 | void StridedCopyKernel(const Context& dev_ctx, |
| 32 | const DenseTensor& input, |
| 33 | const std::vector<int64_t>& dims, |
| 34 | const std::vector<int64_t>& out_stride, |
| 35 | int64_t offset, |
| 36 | DenseTensor* out) { |
| 37 | #if defined(PADDLE_WITH_CUDA) |
| 38 | // not support Windows |
| 39 | #if !defined(_WIN32) |
| 40 | if (FLAGS_use_stride_kernel && |
| 41 | input.place().GetType() == AllocationType::CPU && |
| 42 | out->place().GetType() == AllocationType::GPU && |
| 43 | input.dtype() == out->dtype() && |
| 44 | (!input.meta().is_contiguous() || !out->meta().is_contiguous())) { |
| 45 | DenseTensor dst_gpu; |
| 46 | if (out->meta().is_contiguous()) { |
| 47 | dst_gpu = *out; |
| 48 | } else { |
| 49 | auto meta_dst = dst_gpu.meta(); |
| 50 | meta_dst.dims = out->dims(); |
| 51 | meta_dst.strides = meta_dst.calc_strides(out->dims()); |
| 52 | dst_gpu.set_meta(meta_dst); |
| 53 | dev_ctx.Alloc(&dst_gpu, input.dtype()); |
| 54 | } |
| 55 | |
| 56 | auto src_cpu_place = input.place(); |
| 57 | auto dst_gpu_place = out->place(); |
| 58 | auto& pool = DeviceContextPool::Instance(); |
| 59 | auto* gpu_dev_ctx = static_cast<GPUContext*>(pool.Get(out->place())); |
| 60 | auto stream = gpu_dev_ctx->stream(); |
| 61 | |
| 62 | if (input.meta().is_contiguous()) { |
| 63 | auto src_cpu_place = input.place(); |
| 64 | auto dst_gpu_place = out->place(); |
| 65 | auto size = SizeOf(input.dtype()) * input.numel(); |
| 66 | void* dst_ptr = gpu_dev_ctx->Alloc( |
| 67 | &dst_gpu, |
| 68 | dst_gpu.dtype(), |
| 69 | 0, |
| 70 | dst_gpu_place.GetType() == AllocationType::GPUPINNED); |
| 71 | |
| 72 | memory_utils::Copy( |
| 73 | dst_gpu_place, dst_ptr, src_cpu_place, input.data<T>(), size, stream); |
| 74 | |
| 75 | } else { |
| 76 | DenseTensor cpu_out; |
| 77 | ContiguousKernel<T, Context>(dev_ctx, input, &cpu_out); |
| 78 | auto* src_ptr = cpu_out.data<T>(); |
| 79 | auto size = SizeOf(input.dtype()) * cpu_out.numel(); |
| 80 | void* dst_ptr = gpu_dev_ctx->Alloc( |
| 81 | &dst_gpu, |
| 82 | dst_gpu.dtype(), |
| 83 | 0, |
| 84 | dst_gpu_place.GetType() == AllocationType::GPUPINNED); |
| 85 | |
| 86 | memory_utils::Copy( |
| 87 | dst_gpu_place, dst_ptr, src_cpu_place, src_ptr, size, stream); |
| 88 | } |
nothing calls this directly
no test coverage detected