| 38 | |
| 39 | template <typename KeyType, typename OffsetType, typename DType> |
| 40 | void EmbeddingVarGPUAdapter<KeyType, OffsetType, DType>::lookup( |
| 41 | const core23::Tensor& keys, size_t num_keys, |
| 42 | const core23::Tensor& id_space_offset, size_t num_id_space_offset, |
| 43 | const core23::Tensor& id_space, core23::Tensor& embedding_vec) { |
| 44 | id_space_offset_.resize(num_id_space_offset); |
| 45 | CUDACHECK(cudaMemcpyAsync(id_space_offset_.data(), |
| 46 | id_space_offset.data(), |
| 47 | sizeof(OffsetType) * (num_id_space_offset), |
| 48 | cudaMemcpyDeviceToHost, stream_)); |
| 49 | id_space_.resize(num_id_space_offset - 1); |
| 50 | CUDACHECK(cudaMemcpyAsync(id_space_.data(), id_space.data<int>(), |
| 51 | sizeof(int) * (num_id_space_offset - 1), |
| 52 | cudaMemcpyDeviceToHost, stream_)); |
| 53 | CUDACHECK(cudaStreamSynchronize(stream_)); |
| 54 | assert(tmp_ev_list_.size() == 0); |
| 55 | |
| 56 | const KeyType* input = keys.data<KeyType>(); |
| 57 | std::vector<DType*> lookup_res; |
| 58 | for (int i = 0; i < num_id_space_offset - 1; ++i) { |
| 59 | size_t num = id_space_offset_[i + 1] - id_space_offset_[i]; |
| 60 | |
| 61 | if (num == 0) { |
| 62 | continue; |
| 63 | } |
| 64 | int ev_size = ev_size_per_lookup_[i]; |
| 65 | Tensor evs; |
| 66 | OP_REQUIRES_OK(ctx_, ctx_->allocate_temp(DT_FLOAT, {ev_size * num}, &evs)); |
| 67 | tmp_ev_list_.push_back(evs); |
| 68 | |
| 69 | const auto& device = ctx_->eigen_device<Eigen::GpuDevice>(); |
| 70 | auto var = vars_[id_space_[i]]; |
| 71 | var->LookupOrCreate(input + id_space_offset_[i], evs.flat<DType>().data(), |
| 72 | var->GetDefaultValuePtr(), var->GetDefaultValueDim(), |
| 73 | true, num, device); |
| 74 | for (size_t i_ev = 0; i_ev < num; ++i_ev) { |
| 75 | lookup_res.push_back(evs.flat<DType>().data() + i_ev * ev_size); |
| 76 | } |
| 77 | } |
| 78 | DType** output = static_cast<DType**>(embedding_vec.data()); |
| 79 | CUDACHECK(cudaMemcpyAsync(output, lookup_res.data(), |
| 80 | sizeof(DType*) * lookup_res.size(), |
| 81 | cudaMemcpyHostToDevice, stream_)); |
| 82 | CUDACHECK(cudaStreamSynchronize(stream_)); |
| 83 | } |
| 84 | |
| 85 | template class EmbeddingVarGPUAdapter<int32, int32, float>; |
| 86 | template class EmbeddingVarGPUAdapter<int32, int64, float>; |
no test coverage detected