| 24 | |
| 25 | template <typename T> |
| 26 | void SplitForwardImpl::exec_internal( |
| 27 | _megdnn_tensor_in src, const TensorNDArray& dsts, _megdnn_workspace workspace) { |
| 28 | // Please refer to ConcatForwardImpl. Implementations are similar. |
| 29 | auto dsts_layout = apply_vector<TensorLayout>(m_get_layout, dsts); |
| 30 | auto dsts_shape = apply_vector<TensorShape>(m_get_shape, dsts_layout); |
| 31 | check_exec(src.layout, dsts_layout, workspace.size); |
| 32 | size_t A, B, C; |
| 33 | auto stream = cuda_stream(this->handle()); |
| 34 | |
| 35 | // Pre-calculate B to determine cpu-side workspace size. |
| 36 | B = src.layout.shape[param().axis]; |
| 37 | |
| 38 | // workspace_cpu will be freed by cuda callback. |
| 39 | SmallVector<size_t> workspace_sizes{ |
| 40 | sizeof(const T*) * dsts.size(), |
| 41 | sizeof(size_t) * dsts.size(), |
| 42 | sizeof(size_t) * B, |
| 43 | sizeof(size_t) * B, |
| 44 | }; |
| 45 | |
| 46 | WorkspaceBundle workspace_cpu(nullptr, workspace_sizes), |
| 47 | workspace_gpu(nullptr, workspace_sizes); |
| 48 | |
| 49 | auto total_workspace_size = workspace_cpu.total_size_in_bytes(); |
| 50 | void* workspace_cpu_raw = malloc(total_workspace_size); |
| 51 | megdnn_assert_internal(workspace_cpu_raw); |
| 52 | void* workspace_gpu_raw = static_cast<void*>(workspace.raw_ptr); |
| 53 | workspace_cpu = WorkspaceBundle(workspace_cpu_raw, workspace_sizes); |
| 54 | workspace_gpu = WorkspaceBundle(workspace_gpu_raw, workspace_sizes); |
| 55 | |
| 56 | auto dsts_cpu = static_cast<T**>(workspace_cpu.get(0)); |
| 57 | auto dsts_gpu = static_cast<T**>(workspace_gpu.get(0)); |
| 58 | for (size_t i = 0; i < dsts.size(); ++i) { |
| 59 | dsts_cpu[i] = dsts[i].ptr<T>(); |
| 60 | } |
| 61 | |
| 62 | auto Bv_cpu = static_cast<size_t*>(workspace_cpu.get(1)); |
| 63 | auto Bv_gpu = static_cast<size_t*>(workspace_gpu.get(1)); |
| 64 | get_ABC(dsts_shape, A, Bv_cpu, C); |
| 65 | |
| 66 | auto table_outer_cpu = static_cast<size_t*>(workspace_cpu.get(2)); |
| 67 | auto table_outer_gpu = static_cast<size_t*>(workspace_gpu.get(2)); |
| 68 | auto table_inner_cpu = static_cast<size_t*>(workspace_cpu.get(3)); |
| 69 | auto table_inner_gpu = static_cast<size_t*>(workspace_gpu.get(3)); |
| 70 | { |
| 71 | size_t outer_idx = 0, inner_idx = 0; |
| 72 | |
| 73 | for (size_t i = 0; i < B; ++i) { |
| 74 | table_outer_cpu[i] = outer_idx; |
| 75 | table_inner_cpu[i] = inner_idx; |
| 76 | ++inner_idx; |
| 77 | if (inner_idx == Bv_cpu[outer_idx]) { |
| 78 | ++outer_idx; |
| 79 | inner_idx = 0; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | for (size_t i = 0; i < workspace_cpu.nr_workspace(); ++i) { |
nothing calls this directly
no test coverage detected