MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / exec_internal

Method exec_internal

dnn/src/cuda/concat/opr_impl.cpp:19–107  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17
18template <typename T>
19void ConcatForwardImpl::exec_internal(
20 _megdnn_in const TensorNDArray& srcs, _megdnn_tensor_out dst,
21 _megdnn_workspace workspace) {
22 auto srcs_layout = apply_vector<TensorLayout>(m_get_layout, srcs);
23 auto srcs_shape = apply_vector<TensorShape>(m_get_shape, srcs_layout);
24 check_exec(srcs_layout, dst.layout, workspace.size);
25 size_t A, B, C;
26 auto stream = cuda_stream(this->handle());
27
28 // Pre-calculate B to determine cpu-side workspace size.
29 B = dst.layout.shape[param().axis];
30
31 // workspace_cpu will be freed by cuda callback.
32 SmallVector<size_t> workspace_sizes{
33 sizeof(const T*) * srcs.size(),
34 sizeof(size_t) * srcs.size(),
35 sizeof(size_t) * B,
36 sizeof(size_t) * B,
37 };
38
39 // What do we need:
40 // 1. An const T * array of length src.size(), the i-th element of
41 // which stores the address of the i-th srcs.
42 // 2. A size_t array of length srcs.size(), the i-th element of which
43 // stores the shape of the param().axis-th axis of the i-th src.
44 // 3. A size_t array of length B, the i-th element of which stores the
45 // index of the src tensor that the i-th element along the
46 // param().axis-th axis of dst belongs to.
47 // 4. A size_t array of length B, the i-th element of which stores the
48 // intra-offset inside the corresponding src tensor of the i-th element
49 // along the param().axis-th axis of dst.
50 //
51 // These temporary spaces reside in the device side.
52 // The actually work is delegated to concat::forward_proxy.
53 WorkspaceBundle workspace_cpu(nullptr, workspace_sizes),
54 workspace_gpu(nullptr, workspace_sizes);
55 auto total_workspace_size = workspace_cpu.total_size_in_bytes();
56 void* workspace_cpu_raw = malloc(total_workspace_size);
57 megdnn_assert_internal(workspace_cpu_raw);
58 void* workspace_gpu_raw = workspace.raw_ptr;
59 workspace_cpu = WorkspaceBundle(workspace_cpu_raw, workspace_sizes);
60 workspace_gpu = WorkspaceBundle(workspace_gpu_raw, workspace_sizes);
61 // srcs
62 auto srcs_cpu = static_cast<const T**>(workspace_cpu.get(0));
63 auto srcs_gpu = static_cast<const T**>(workspace_gpu.get(0));
64 for (size_t i = 0; i < srcs.size(); ++i) {
65 srcs_cpu[i] = srcs[i].ptr<T>();
66 }
67
68 // Bv
69 auto Bv_cpu = static_cast<size_t*>(workspace_cpu.get(1));
70 auto Bv_gpu = static_cast<size_t*>(workspace_gpu.get(1));
71 get_ABC(srcs_shape, A, Bv_cpu, C);
72
73 // table_outer
74 auto table_outer_cpu = static_cast<size_t*>(workspace_cpu.get(2));
75 auto table_outer_gpu = static_cast<size_t*>(workspace_gpu.get(2));
76 auto table_inner_cpu = static_cast<size_t*>(workspace_cpu.get(3));

Callers

nothing calls this directly

Calls 10

cuda_streamFunction · 0.85
WorkspaceBundleClass · 0.85
get_ABCFunction · 0.85
total_size_in_bytesMethod · 0.80
nr_workspaceMethod · 0.80
paramFunction · 0.50
handleMethod · 0.45
sizeMethod · 0.45
getMethod · 0.45
get_sizeMethod · 0.45

Tested by

no test coverage detected