Open3D (C++ API)  0.20.0
Loading...
Searching...
No Matches
ContinuousConvSYCL.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// Copyright (c) 2018-2026 www.open3d.org
5// SPDX-License-Identifier: MIT
6// ----------------------------------------------------------------------------
7
8// SYCL port of ContinuousConv.cuh. Structurally identical to the CUDA
9// version: same two-pass temp-size query (see MemoryAllocation.h, reused
10// verbatim - it is pure host-side bookkeeping, no CUDA/SYCL dependency),
11// same chunked-GEMM loop over `num_cols_per_run` output points at a time.
12// Only device-specific pieces changed: cudaStream_t -> sycl::queue&,
13// cudaMemsetAsync -> queue.fill, CUTLASS 2.x device::Gemm -> the sycl-tla
14// backed GemmColumnMajorSYCL shim (see GemmSYCL.h), FillColumn ->
15// FillColumnSYCL (ContinuousConvSYCLKernels.h).
16#pragma once
17
22
24
25namespace open3d {
26namespace ml {
27namespace impl {
28
31template <class TFeat, class TOut, class TReal, class TIndex>
33 void* temp,
34 size_t& temp_size,
35 size_t& max_temp_size,
36 int texture_alignment,
37 TOut* out_features,
38 const std::vector<int>& filter_dims,
39 const TFeat* filter,
40 TIndex num_out,
41 const TReal* out_positions,
42 TIndex num_inp,
43 const TReal* inp_positions,
44 const TFeat* inp_features,
45 const TFeat* inp_importance,
46 size_t neighbors_index_size,
47 const TIndex* neighbors_index,
48 const TFeat* neighbors_importance,
49 const int64_t* neighbors_row_splits,
50 const TReal* extents,
51 const TReal* offsets,
52 InterpolationMode interpolation,
53 CoordinateMapping coordinate_mapping,
54 bool align_corners,
55 bool individual_extent,
56 bool isotropic_extent,
57 bool normalize,
58 bool allow_tf32) {
59 const bool get_temp_size = !temp;
60
61 if (get_temp_size) {
62 temp = (char*)1; // worst case alignment
63 temp_size = std::numeric_limits<int64_t>::max();
64 }
65
66 MemoryAllocation mem_temp(temp, temp_size, texture_alignment);
67
68 const int in_channels = filter_dims[filter_dims.size() - 2];
69 const int out_channels = filter_dims[filter_dims.size() - 1];
70
71 int spatial_filter_size = 1;
72 for (int i = 0; i < 3; ++i) spatial_filter_size *= filter_dims[i];
73
74 // this defines how much temporary storage we need at least.
75 // we want to allocate memory for at least 32 output points.
76 const size_t min_num_cols_per_run = std::min(size_t(num_out), size_t(32));
77 const size_t max_num_cols_per_run = num_out;
78 const size_t bytes_per_column =
79 sizeof(TFeat) * (spatial_filter_size * in_channels);
80 const size_t min_temp_size_bytes = min_num_cols_per_run * bytes_per_column;
81 const size_t max_temp_size_bytes = max_num_cols_per_run * bytes_per_column;
82
83 if (get_temp_size) {
84 std::pair<char*, size_t> tmp =
85 mem_temp.Alloc<char>(min_temp_size_bytes);
86 temp_size = mem_temp.MaxUsed();
87 mem_temp.Free(tmp);
88 mem_temp.Alloc<char>(max_temp_size_bytes);
89 max_temp_size = mem_temp.MaxUsed();
90 return;
91 }
92
93 // Request segment using all of the temporary memory
94 std::pair<void*, size_t> mem_columns = mem_temp.AllocLargestSegment();
95
96 if (mem_columns.second < min_temp_size_bytes) {
97 std::stringstream ss;
98 ss << "temp is too small " << mem_columns.second
99 << " bytes. Expected at least " << min_temp_size_bytes << " bytes\n";
100 throw std::runtime_error(ss.str());
101 }
102
103 // init output
104 sycl::event out_features_fill_event =
105 queue.fill(out_features, TOut(0), size_t(num_out) * out_channels);
106
107 size_t num_cols_per_run =
108 std::min(mem_columns.second / bytes_per_column, size_t(num_out));
109
110 // this is the pointer to the patch matrix
111 TFeat* columns = (TFeat*)mem_columns.first;
112
113 // if we cannot process all data at once we need multiple runs. Since
114 // GemmColumnMajorSYCL returns a completion event without blocking
115 // (GemmSYCL.h); chunk N+1's FillColumn (which
116 // overwrites the shared `columns` scratch buffer) must explicitly depend
117 // on chunk N's GEMM event -- otherwise FillColumn could start
118 // overwriting `columns` while the previous GEMM is still reading it.
119 const size_t num_runs = DivUp(num_out, num_cols_per_run);
120 sycl::event prev_gemm_event;
121 for (size_t run_i = 0; run_i < num_runs; ++run_i) {
122 const TIndex begin_idx = TIndex(run_i * num_cols_per_run);
123 const TIndex end_idx = TIndex(
124 std::min(size_t(num_out), (run_i + 1) * num_cols_per_run));
125 const size_t num_cols_this_run = end_idx - begin_idx;
126
127 // compute the patch matrix
128 sycl::event fill_column_event = FillColumnSYCL<TFeat, TReal, TIndex>(
129 queue, columns, in_channels, begin_idx, end_idx, num_out,
130 out_positions, num_inp, inp_positions, inp_features,
131 inp_importance, neighbors_index_size, neighbors_index,
132 neighbors_importance, neighbors_row_splits, extents, offsets,
133 filter_dims, interpolation, coordinate_mapping, align_corners,
134 individual_extent, isotropic_extent, normalize,
135 run_i == 0 ? std::vector<sycl::event>{out_features_fill_event}
136 : std::vector<sycl::event>{prev_gemm_event});
137
138 // C is MxN
139 // B is KxN
140 // A is MxK
141 const int m = out_channels;
142 const int k = spatial_filter_size * in_channels;
143 const int n = static_cast<int>(num_cols_this_run);
144 const float alpha = 1;
145 const float* const A = filter;
146 const int lda = m;
147 const float* const B = columns;
148 const int ldb = k;
149 const float beta = 1;
150 float* C = out_features + (run_i * num_cols_per_run * out_channels);
151 const int ldc = m;
152
153 prev_gemm_event = GemmColumnMajorSYCL<cutlass::layout::ColumnMajor,
154 cutlass::layout::ColumnMajor>(
155 queue, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc, allow_tf32,
156 {fill_column_event});
157 }
158}
159
160} // namespace impl
161} // namespace ml
162} // namespace open3d
const NeighborOffsets & offsets
Definition NormalDistributionsTransform.cpp:254
Eigen::Matrix3d B
Definition PointCloudPlanarPatchDetection.cpp:523
sycl::queue queue
Definition SYCLContext.cpp:88
A class for managing memory segments within a memory allocation.
Definition MemoryAllocation.h:21
void Free(const std::pair< T *, size_t > &segment)
Frees a previously returned segment.
Definition MemoryAllocation.h:85
std::pair< void *, size_t > AllocLargestSegment()
Returns the largest free segment.
Definition MemoryAllocation.h:75
size_t MaxUsed() const
Returns the peak memory usage in bytes.
Definition MemoryAllocation.h:145
std::pair< T *, size_t > Alloc(size_t size)
Definition MemoryAllocation.h:48
void CConvComputeFeaturesSYCL(sycl::queue &queue, void *temp, size_t &temp_size, size_t &max_temp_size, int texture_alignment, TOut *out_features, const std::vector< int > &filter_dims, const TFeat *filter, TIndex num_out, const TReal *out_positions, TIndex num_inp, const TReal *inp_positions, const TFeat *inp_features, const TFeat *inp_importance, size_t neighbors_index_size, const TIndex *neighbors_index, const TFeat *neighbors_importance, const int64_t *neighbors_row_splits, const TReal *extents, const TReal *offsets, InterpolationMode interpolation, CoordinateMapping coordinate_mapping, bool align_corners, bool individual_extent, bool isotropic_extent, bool normalize, bool allow_tf32)
Definition ContinuousConvSYCL.h:32
InterpolationMode
Definition ContinuousConvTypes.h:18
CoordinateMapping
Definition ContinuousConvTypes.h:26
sycl::event GemmColumnMajorSYCL(sycl::queue &queue, int m, int n, int k, float alpha, const float *A, int64_t lda, const float *B, int64_t ldb, float beta, float *C, int64_t ldc, bool allow_tf32, const std::vector< sycl::event > &deps)
Definition GemmSYCL.cpp:462
int DivUp(int x, int y)
Computes the quotient of x/y with rounding up.
Definition Helper.h:176
Definition PinholeCameraIntrinsic.cpp:16