Open3D (C++ API)  0.20.0
Loading...
Searching...
No Matches
TrilinearDevoxelizeSYCL.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 implementation of TrilinearDevoxelize(Grad) — ports
9// TrilinearDevoxelize.cu. Forward is a per-point trilinear gather (no shared
10// state between points, so we use a flat nd_range over (batch, point),
11// flattened to 1-D via core::ParallelFor, rather than the CUDA
12// grid-stride-over-one-block-per-batch layout or a bare sycl::range.
13// Backward scatters into the voxel grid via atomic_ref::fetch_add (proven
14// pattern from InvertNeighborsList / InterpolatePoints).
15
16#pragma once
17
18#include <sycl/sycl.hpp>
19
21
22namespace open3d {
23namespace ml {
24namespace contrib {
25
30inline void TrilinearDevoxelizeSYCL(sycl::queue& queue,
31 int b,
32 int c,
33 int n,
34 int r,
35 int r2,
36 int r3,
37 bool is_training,
38 const float* const coords,
39 const float* const feat,
40 int* const inds,
41 float* const wgts,
42 float* const outs) {
43 if (b <= 0 || n <= 0) return;
44
45 core::ParallelFor(queue, static_cast<int64_t>(b) * n, [=](int64_t idx) {
46 const int batch_index = static_cast<int>(idx / n);
47 const int i = static_cast<int>(idx % n);
48
49 const float* const crd = coords + batch_index * n * 3;
50 int* const ind = inds + batch_index * n * 8;
51 float* const wgt = wgts + batch_index * n * 8;
52 const float* const ft = feat + batch_index * c * r3;
53 float* const out = outs + batch_index * c * n;
54
55 const float x = crd[i];
56 const float y = crd[i + n];
57 const float z = crd[i + n + n];
58 const float x_lo_f = sycl::floor(x);
59 const float y_lo_f = sycl::floor(y);
60 const float z_lo_f = sycl::floor(z);
61
62 const float x_d_1 = x - x_lo_f;
63 const float y_d_1 = y - y_lo_f;
64 const float z_d_1 = z - z_lo_f;
65 const float x_d_0 = 1.0f - x_d_1;
66 const float y_d_0 = 1.0f - y_d_1;
67 const float z_d_0 = 1.0f - z_d_1;
68
69 const float wgt000 = x_d_0 * y_d_0 * z_d_0;
70 const float wgt001 = x_d_0 * y_d_0 * z_d_1;
71 const float wgt010 = x_d_0 * y_d_1 * z_d_0;
72 const float wgt011 = x_d_0 * y_d_1 * z_d_1;
73 const float wgt100 = x_d_1 * y_d_0 * z_d_0;
74 const float wgt101 = x_d_1 * y_d_0 * z_d_1;
75 const float wgt110 = x_d_1 * y_d_1 * z_d_0;
76 const float wgt111 = x_d_1 * y_d_1 * z_d_1;
77
78 const int x_lo = static_cast<int>(x_lo_f);
79 const int y_lo = static_cast<int>(y_lo_f);
80 const int z_lo = static_cast<int>(z_lo_f);
81 const int x_hi = (x_d_1 > 0) ? -1 : 0;
82 const int y_hi = (y_d_1 > 0) ? -1 : 0;
83 const int z_hi = (z_d_1 > 0) ? 1 : 0;
84
85 const int idx000 = x_lo * r2 + y_lo * r + z_lo;
86 const int idx001 = idx000 + z_hi;
87 const int idx010 = idx000 + (y_hi & r);
88 const int idx011 = idx010 + z_hi;
89 const int idx100 = idx000 + (x_hi & r2);
90 const int idx101 = idx100 + z_hi;
91 const int idx110 = idx100 + (y_hi & r);
92 const int idx111 = idx110 + z_hi;
93
94 if (is_training) {
95 wgt[i] = wgt000;
96 wgt[i + n] = wgt001;
97 wgt[i + n * 2] = wgt010;
98 wgt[i + n * 3] = wgt011;
99 wgt[i + n * 4] = wgt100;
100 wgt[i + n * 5] = wgt101;
101 wgt[i + n * 6] = wgt110;
102 wgt[i + n * 7] = wgt111;
103 ind[i] = idx000;
104 ind[i + n] = idx001;
105 ind[i + n * 2] = idx010;
106 ind[i + n * 3] = idx011;
107 ind[i + n * 4] = idx100;
108 ind[i + n * 5] = idx101;
109 ind[i + n * 6] = idx110;
110 ind[i + n * 7] = idx111;
111 }
112
113 for (int j = 0; j < c; ++j) {
114 const int jr3 = j * r3;
115 out[j * n + i] =
116 wgt000 * ft[jr3 + idx000] + wgt001 * ft[jr3 + idx001] +
117 wgt010 * ft[jr3 + idx010] + wgt011 * ft[jr3 + idx011] +
118 wgt100 * ft[jr3 + idx100] + wgt101 * ft[jr3 + idx101] +
119 wgt110 * ft[jr3 + idx110] + wgt111 * ft[jr3 + idx111];
120 }
121 });
122}
123
126inline void TrilinearDevoxelizeGradSYCL(sycl::queue& queue,
127 int b,
128 int c,
129 int n,
130 int r3,
131 const int* const inds,
132 const float* const wgts,
133 const float* const grad_y,
134 float* const grad_x) {
135 if (b <= 0 || n <= 0) return;
136
137 core::ParallelFor(queue, static_cast<int64_t>(b) * n, [=](int64_t idx64) {
138 const int batch_index = static_cast<int>(idx64 / n);
139 const int i = static_cast<int>(idx64 % n);
140
141 const int* const ind = inds + batch_index * n * 8;
142 const float* const wgt = wgts + batch_index * n * 8;
143 float* const gx = grad_x + batch_index * c * r3;
144 const float* const gy = grad_y + batch_index * c * n;
145
146 const int idx[8] = {ind[i], ind[i + n], ind[i + n * 2],
147 ind[i + n * 3], ind[i + n * 4], ind[i + n * 5],
148 ind[i + n * 6], ind[i + n * 7]};
149 const float w[8] = {wgt[i], wgt[i + n], wgt[i + n * 2],
150 wgt[i + n * 3], wgt[i + n * 4], wgt[i + n * 5],
151 wgt[i + n * 6], wgt[i + n * 7]};
152
153 for (int j = 0; j < c; ++j) {
154 const int jr3 = j * r3;
155 const float g = gy[j * n + i];
156 for (int l = 0; l < 8; ++l) {
157 sycl::atomic_ref<float, sycl::memory_order::relaxed,
158 sycl::memory_scope::device,
159 sycl::access::address_space::global_space>
160 ref(gx[jr3 + idx[l]]);
161 ref.fetch_add(w[l] * g);
162 }
163 }
164 });
165}
166
167} // namespace contrib
168} // namespace ml
169} // namespace open3d
std::int64_t y
Definition NormalDistributionsTransform.cpp:43
std::int64_t x
Definition NormalDistributionsTransform.cpp:42
std::int64_t z
Definition NormalDistributionsTransform.cpp:44
sycl::queue queue
Definition SYCLContext.cpp:88
void ParallelFor(const Device &device, int64_t n, const func_t &func)
Definition ParallelFor.h:190
void TrilinearDevoxelizeGradSYCL(sycl::queue &queue, int b, int c, int n, int r3, const int *const inds, const float *const wgts, const float *const grad_y, float *const grad_x)
Definition TrilinearDevoxelizeSYCL.h:126
void TrilinearDevoxelizeSYCL(sycl::queue &queue, int b, int c, int n, int r, int r2, int r3, bool is_training, const float *const coords, const float *const feat, int *const inds, float *const wgts, float *const outs)
Definition TrilinearDevoxelizeSYCL.h:30
Definition PinholeCameraIntrinsic.cpp:16