| 109 | } |
| 110 | |
| 111 | void XlaDeviceContext::CopyCPUTensorToDevice(const Tensor* cpu_tensor, |
| 112 | Device* device, |
| 113 | Tensor* device_tensor, |
| 114 | StatusCallback done, |
| 115 | bool sync_dst_compute) const { |
| 116 | if (cpu_tensor->NumElements() == 0) { |
| 117 | VLOG(2) << "CopyCPUTensorToDevice empty tensor"; |
| 118 | done(Status::OK()); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | VLOG(2) << "CopyCPUTensorToDevice use_fast_mem " << use_fast_mem_ << " " |
| 123 | << this << " " |
| 124 | << reinterpret_cast<const void*>(cpu_tensor->tensor_data().data()) |
| 125 | << " " |
| 126 | << reinterpret_cast<const void*>(device_tensor->tensor_data().data()) |
| 127 | << " " << cpu_tensor->NumElements() << " " |
| 128 | << cpu_tensor->shape().DebugString() << " " |
| 129 | << device_tensor->shape().DebugString(); |
| 130 | |
| 131 | XlaTensor* xla_tensor = XlaTensor::FromTensor(device_tensor); |
| 132 | CHECK(xla_tensor); |
| 133 | |
| 134 | Status status = [&]() -> Status { |
| 135 | TF_ASSIGN_OR_RETURN( |
| 136 | xla::Shape shape, |
| 137 | shape_representation_fn_(device_tensor->shape(), device_tensor->dtype(), |
| 138 | use_fast_mem_)); |
| 139 | |
| 140 | // The device tensor should always be fresh. |
| 141 | TF_RET_CHECK(!xla_tensor->has_shaped_buffer()); |
| 142 | |
| 143 | xla_tensor->set_host_tensor(*cpu_tensor); |
| 144 | TF_RETURN_IF_ERROR( |
| 145 | xla_tensor->AllocateShapedBuffer(device_tensor->dtype(), shape, client_, |
| 146 | stream_->parent()->device_ordinal())); |
| 147 | |
| 148 | // The cpu_tensor and literal that we created here hold the data of host |
| 149 | // tensor in descending layout. The layout could be different from layout in |
| 150 | // device_tensor (but the logical shape has to be the same). The |
| 151 | // transfer_manager is responsible to do corresponding transposing when |
| 152 | // transferring the data to device. |
| 153 | xla::BorrowingLiteral literal( |
| 154 | static_cast<const char*>(DMAHelper::base(cpu_tensor)), |
| 155 | xla::ShapeUtil::MakeShape(shape.element_type(), |
| 156 | xla::AsInt64Slice(shape.dimensions()))); |
| 157 | |
| 158 | VLOG(2) << "Transfer to device as literal: " << literal.ToString() << " " |
| 159 | << xla_tensor->shaped_buffer().ToString(); |
| 160 | if (UseMultipleStreams() && |
| 161 | !transfer_manager_->CanShapedBufferBeAccessedNow( |
| 162 | stream_->parent(), xla_tensor->shaped_buffer())) { |
| 163 | // Initially wait for the compute stream so that memory allocations are |
| 164 | // synchronized. |
| 165 | host_to_device_stream_->ThenWaitFor(stream_.get()); |
| 166 | } |
| 167 | |
| 168 | TF_RETURN_IF_ERROR(transfer_manager_->TransferLiteralToDeviceAsync( |