| 342 | } // namespace |
| 343 | |
| 344 | void CopyDeviceToHost(const Tensor* input, Allocator* cpu_allocator, |
| 345 | Allocator* out_allocator, StringPiece edge_name, |
| 346 | Device* src, Tensor* output, |
| 347 | DeviceContext* send_dev_context, StatusCallback done) { |
| 348 | if (input->dtype() == DT_VARIANT) { |
| 349 | Tensor copy(cpu_allocator, DT_VARIANT, input->shape()); |
| 350 | auto* status_cb = new ReffedStatusCallback(std::move(done)); |
| 351 | core::ScopedUnref status_cb_unref(status_cb); |
| 352 | |
| 353 | auto wrapped_done = [status_cb](const Status& s) { |
| 354 | status_cb->UpdateStatus(s); |
| 355 | status_cb->Unref(); |
| 356 | }; |
| 357 | auto copier = std::bind( |
| 358 | [edge_name, src, send_dev_context, out_allocator, status_cb, |
| 359 | cpu_allocator](StatusCallback wrapped_done_, |
| 360 | // Begin unbound arguments |
| 361 | const Tensor& from, Tensor* to) { |
| 362 | if (from.dtype() == DT_VARIANT) { |
| 363 | status_cb->Ref(); |
| 364 | CopyDeviceToHost(&from, cpu_allocator, out_allocator, edge_name, |
| 365 | src, to, send_dev_context, wrapped_done_); |
| 366 | return Status::OK(); |
| 367 | } else { |
| 368 | if (!DMAHelper::CanUseDMA(&from)) { |
| 369 | Status err = errors::InvalidArgument( |
| 370 | "During Variant Device->Host Copy: " |
| 371 | "non-DMA-copy attempted of tensor type: ", |
| 372 | DataTypeString(from.dtype())); |
| 373 | status_cb->UpdateStatus(err); |
| 374 | return err; |
| 375 | } |
| 376 | if (status_cb->ok()) { |
| 377 | status_cb->Ref(); |
| 378 | *to = Tensor(out_allocator, from.dtype(), from.shape()); |
| 379 | send_dev_context->CopyDeviceTensorToCPU(&from, edge_name, src, to, |
| 380 | wrapped_done_); |
| 381 | return Status::OK(); |
| 382 | } else { |
| 383 | return status_cb->status(); |
| 384 | } |
| 385 | } |
| 386 | }, |
| 387 | std::move(wrapped_done), std::placeholders::_1, std::placeholders::_2); |
| 388 | |
| 389 | const Variant* v = input->flat<Variant>().data(); |
| 390 | Variant* v_out = copy.flat<Variant>().data(); |
| 391 | Status s_copy_init; |
| 392 | for (int64 i = 0; i < input->NumElements(); ++i) { |
| 393 | s_copy_init = VariantDeviceCopy( |
| 394 | VariantDeviceCopyDirection::DEVICE_TO_HOST, v[i], &v_out[i], copier); |
| 395 | if (!s_copy_init.ok()) { |
| 396 | status_cb->UpdateStatus(s_copy_init); |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | if (s_copy_init.ok()) { |
| 401 | *output = std::move(copy); |
no test coverage detected