| 165 | |
| 166 | private: |
| 167 | void Compute(user_op::KernelComputeContext* ctx, user_op::OpKernelState*, |
| 168 | const user_op::OpKernelCache* cache) const override { |
| 169 | auto* kernel_cache = dynamic_cast<const EagerSToPOpKernelCache*>(cache); |
| 170 | CHECK(kernel_cache != nullptr); |
| 171 | const user_op::Tensor* in = ctx->Tensor4ArgNameAndIndex("in", 0); |
| 172 | user_op::Tensor* out = ctx->Tensor4ArgNameAndIndex("out", 0); |
| 173 | user_op::Tensor* tmp_buffer = ctx->Tensor4ArgNameAndIndex("tmp_buffer", 0); |
| 174 | const void* in_ptr = in->dptr(); |
| 175 | void* out_ptr = out->mut_dptr(); |
| 176 | void* tmp_buffer_ptr = tmp_buffer->mut_dptr(); |
| 177 | |
| 178 | const int64_t total_elem_cnt = ctx->Attr<Shape>("shape").elem_cnt(); |
| 179 | |
| 180 | DeviceType device_type = ctx->device_type(); |
| 181 | |
| 182 | std::unique_ptr<ep::primitive::Memset> memset_primitive = |
| 183 | ep::primitive::NewPrimitive<ep::primitive::MemsetFactory>(device_type); |
| 184 | CHECK(memset_primitive) << "Can not create Memset primitive for device type " << device_type; |
| 185 | memset_primitive->Launch(ctx->stream(), out->mut_dptr(), 0, |
| 186 | total_elem_cnt * GetSizeOfDataType(out->data_type())); |
| 187 | |
| 188 | const auto& sorted_elem_cnt2in_tensor_slice_copier_pair = |
| 189 | kernel_cache->sorted_elem_cnt2in_tensor_slice_copier_pair(); |
| 190 | const auto& sorted_elem_cnt2out_tensor_slice_copier_pair = |
| 191 | kernel_cache->sorted_elem_cnt2out_tensor_slice_copier_pair(); |
| 192 | const auto& sorted_p2p_pair = kernel_cache->sorted_p2p_pair(); |
| 193 | CHECK_EQ(sorted_elem_cnt2in_tensor_slice_copier_pair.size(), sorted_p2p_pair.size()); |
| 194 | CHECK_EQ(sorted_elem_cnt2out_tensor_slice_copier_pair.size(), sorted_p2p_pair.size()); |
| 195 | |
| 196 | for (int64_t i = 0; i < sorted_p2p_pair.size(); ++i) { |
| 197 | const auto& p2p_pair = sorted_p2p_pair.at(i); |
| 198 | int64_t src = p2p_pair.first; |
| 199 | int64_t dst = p2p_pair.second; |
| 200 | if (src == dst && src == GlobalProcessCtx::Rank()) { |
| 201 | const auto& elem_cnt2tensor_slice_copier_pair = |
| 202 | sorted_elem_cnt2in_tensor_slice_copier_pair.at(i); |
| 203 | const auto& tensor_slice_copier = elem_cnt2tensor_slice_copier_pair.second; |
| 204 | tensor_slice_copier->Copy(ctx->stream(), out_ptr, in_ptr); |
| 205 | continue; |
| 206 | } |
| 207 | if (GlobalProcessCtx::Rank() == src) { |
| 208 | const auto& elem_cnt2tensor_slice_copier_pair = |
| 209 | sorted_elem_cnt2in_tensor_slice_copier_pair.at(i); |
| 210 | const auto& elem_cnt = elem_cnt2tensor_slice_copier_pair.first; |
| 211 | const auto& tensor_slice_copier = elem_cnt2tensor_slice_copier_pair.second; |
| 212 | tensor_slice_copier->Copy(ctx->stream(), tmp_buffer_ptr, in_ptr); |
| 213 | CHECK_JUST(Send(reinterpret_cast<const void*>(tmp_buffer_ptr), elem_cnt, in->data_type(), |
| 214 | dst, device_type, ctx->stream())); |
| 215 | } |
| 216 | if (GlobalProcessCtx::Rank() == dst) { |
| 217 | const auto& elem_cnt2tensor_slice_copier_pair = |
| 218 | sorted_elem_cnt2out_tensor_slice_copier_pair.at(i); |
| 219 | const auto& elem_cnt = elem_cnt2tensor_slice_copier_pair.first; |
| 220 | const auto& tensor_slice_copier = elem_cnt2tensor_slice_copier_pair.second; |
| 221 | CHECK_JUST( |
| 222 | Recv(tmp_buffer_ptr, elem_cnt, out->data_type(), src, device_type, ctx->stream())); |
| 223 | tensor_slice_copier->Copy(ctx->stream(), out_ptr, |
| 224 | reinterpret_cast<const void*>(tmp_buffer_ptr)); |
nothing calls this directly
no test coverage detected