11 #include "../../FaissAssert.h"
12 #include "../utils/ConversionOperators.cuh"
13 #include "../utils/DeviceDefs.cuh"
14 #include "../utils/DeviceUtils.h"
15 #include "../utils/Float16.cuh"
16 #include "../utils/MathOperators.cuh"
17 #include "../utils/PtxUtils.cuh"
18 #include "../utils/StaticUtils.h"
19 #include "../utils/Reductions.cuh"
21 namespace faiss {
namespace gpu {
33 template <
typename T,
typename TVec,
typename int64_t,
34 int RowTileSize,
bool NormLoop,
bool NormSquared>
35 __global__
void l2Norm(Tensor<TVec, 2, true, int64_t> input,
36 Tensor<T, 1, true, int64_t> output) {
37 extern __shared__
char smemByte[];
38 T* smem = (T*) smemByte;
40 int64_t numWarps = utils::divUp(blockDim.x, kWarpSize);
41 int64_t laneId = getLaneId();
42 int64_t warpId = threadIdx.x / kWarpSize;
44 bool lastRowTile = (blockIdx.x == (gridDim.x - 1));
45 int64_t rowStart = RowTileSize * blockIdx.x;
46 T rowNorm[RowTileSize];
50 for (int64_t row = 0; row < input.getSize(0) - rowStart; ++row) {
52 rowNorm[0] = Math<T>::zero();
54 for (int64_t col = threadIdx.x;
55 col < input.getSize(1); col += blockDim.x) {
56 TVec val = input[rowStart + row][col];
57 val = Math<TVec>::mul(val, val);
58 rowNorm[0] = Math<T>::add(rowNorm[0], Math<TVec>::reduceAdd(val));
61 TVec val = input[rowStart + row][threadIdx.x];
62 val = Math<TVec>::mul(val, val);
66 rowNorm[0] = warpReduceAllSum(rowNorm[0]);
68 smem[row * numWarps + warpId] = rowNorm[0];
78 TVec tmp[RowTileSize];
81 for (
int row = 0; row < RowTileSize; ++row) {
82 rowNorm[row] = Math<T>::zero();
85 for (int64_t col = threadIdx.x;
86 col < input.getSize(1); col += blockDim.x) {
88 for (
int row = 0; row < RowTileSize; ++row) {
89 tmp[row] = input[rowStart + row][col];
93 for (
int row = 0; row < RowTileSize; ++row) {
94 tmp[row] = Math<TVec>::mul(tmp[row], tmp[row]);
98 for (
int row = 0; row < RowTileSize; ++row) {
99 rowNorm[row] = Math<T>::add(rowNorm[row],
100 Math<TVec>::reduceAdd(tmp[row]));
104 TVec tmp[RowTileSize];
108 for (
int row = 0; row < RowTileSize; ++row) {
109 tmp[row] = input[rowStart + row][threadIdx.x];
113 for (
int row = 0; row < RowTileSize; ++row) {
114 tmp[row] = Math<TVec>::mul(tmp[row], tmp[row]);
118 for (
int row = 0; row < RowTileSize; ++row) {
125 for (
int row = 0; row < RowTileSize; ++row) {
126 rowNorm[row] = warpReduceAllSum(rowNorm[row]);
131 for (
int row = 0; row < RowTileSize; ++row) {
132 smem[row * numWarps + warpId] = rowNorm[row];
142 for (
int row = 0; row < RowTileSize; ++row) {
143 rowNorm[row] = laneId < numWarps ?
144 smem[row * numWarps + laneId] : Math<T>::zero();
148 for (
int row = 0; row < RowTileSize; ++row) {
149 rowNorm[row] = warpReduceAllSum(rowNorm[row]);
155 for (
int row = 0; row < RowTileSize; ++row) {
156 int outCol = rowStart + row;
159 if (outCol < output.getSize(0)) {
161 NormSquared ? rowNorm[row] :
163 sqrtf(ConvertTo<float>::to(rowNorm[row])));
167 NormSquared ? rowNorm[row] :
169 sqrtf(ConvertTo<float>::to(rowNorm[row])));
176 template <
typename T,
typename TVec,
typename int64_t>
177 void runL2Norm(Tensor<T, 2, true, int64_t>& input,
178 Tensor<T, 1, true, int64_t>& output,
180 cudaStream_t stream) {
181 FAISS_ASSERT(input.getSize(0) == output.getSize(0));
183 int64_t maxThreads = (int64_t) getMaxThreadsCurrentDevice();
184 constexpr
int rowTileSize = 8;
186 #define RUN_L2(TYPE_T, TYPE_TVEC, INPUT) \
190 l2Norm<TYPE_T, TYPE_TVEC, int64_t, rowTileSize, true, true> \
191 <<<grid, block, smem, stream>>>(INPUT, output); \
193 l2Norm<TYPE_T, TYPE_TVEC, int64_t, rowTileSize, true, false> \
194 <<<grid, block, smem, stream>>>(INPUT, output); \
198 l2Norm<TYPE_T, TYPE_TVEC, int64_t, rowTileSize, false, true> \
199 <<<grid, block, smem, stream>>>(INPUT, output); \
201 l2Norm<TYPE_T, TYPE_TVEC, int64_t, rowTileSize, false, false> \
202 <<<grid, block, smem, stream>>>(INPUT, output); \
207 if (input.template canCastResize<TVec>()) {
209 auto inputV = input.template castResize<TVec>();
211 auto dim = inputV.getSize(1);
212 bool normLoop = dim > maxThreads;
213 auto numThreads = min(dim, maxThreads);
215 auto grid = dim3(utils::divUp(inputV.getSize(0), rowTileSize));
216 auto block = dim3(numThreads);
218 auto smem =
sizeof(T) * rowTileSize * utils::divUp(numThreads, kWarpSize);
220 RUN_L2(T, TVec, inputV);
224 auto dim = input.getSize(1);
225 bool normLoop = dim > maxThreads;
226 auto numThreads = min(dim, maxThreads);
228 auto grid = dim3(utils::divUp(input.getSize(0), rowTileSize));
229 auto block = dim3(numThreads);
231 auto smem =
sizeof(T) * rowTileSize * utils::divUp(numThreads, kWarpSize);
241 void runL2Norm(Tensor<float, 2, true>& input,
242 Tensor<float, 1, true>& output,
244 cudaStream_t stream) {
245 if (input.canUseIndexType<
int>()) {
246 runL2Norm<float, float4, int>(input, output, normSquared, stream);
248 auto inputCast = input.castIndexType<
long>();
249 auto outputCast = output.castIndexType<
long>();
250 runL2Norm<float, float4, long>(inputCast, outputCast, normSquared, stream);
254 #ifdef FAISS_USE_FLOAT16
255 void runL2Norm(Tensor<half, 2, true>& input,
256 Tensor<half, 1, true>& output,
258 cudaStream_t stream) {
259 if (input.canUseIndexType<
int>()) {
260 runL2Norm<half, half2, int>(input, output, normSquared, stream);
262 auto inputCast = input.castIndexType<
long>();
263 auto outputCast = output.castIndexType<
long>();
264 runL2Norm<half, half2, long>(inputCast, outputCast, normSquared, stream);
static __device__ T reduceAdd(T v)
For a vector type, this is a horizontal add, returning sum(v_i)