File size: 12,524 Bytes
d1d4335 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <cstdint>
#include <functional>
#include "fbgemm/FbgemmBuild.h"
namespace fbgemm {
template <
typename InType,
typename IndexType,
typename OffsetType = std::int32_t,
typename OutType = float>
class EmbeddingSpMDMKernelSignature {
public:
/**
* Behavior is as the follow pseudocode
* (when use_offsets == true, lengths[i] == offsets[i + 1] - offsets[i])
* (when is_weight_positional == true, use weights[j - offsets[i]] instead of
* weights[j])
*
* for i in range(output_size):
* out[i * block_size : (i + 1) * block_size] = 0
* for j in range(offsets[i], offsets[i + 1]):
* for k in range(block_size):
* out[i * block_size + k] += input[indices[j] * block_size + k] *
* weights ? weights[j] : 1;
* if normalize_weights and lengths[i] > 0:
* out[i * block_size : (i + 1) * block_size] /= lengths[i]
*
* @param data_size the number of rows in embedding table
*/
using Type = std::function<bool(
std::int64_t output_size,
std::int64_t index_size,
std::int64_t data_size,
const InType* input,
const IndexType* indices,
const OffsetType* offsets_or_lengths,
const float* weights, // optional, can be null for non-weighted sum
OutType* out)>;
};
/**
* @tparam InType can be float, float16, or uint8_t
* @tparam IndexType can be int32_t or int64_t
* @tparam IndexType can be int32_t or int64_t
*
* @param use_offsets If true, the generated code assumes we will pass offsets
* instead of lengths that confirms PyTorch EmbeddingBag
* interface. In this case, the length of offsets array
* should be output_size + 1 and offsets[output_size] should
* be index_size.
* If false, the generate code assumes we will pass lengths
* that confirms Caffe2 SparseLengthsSum interface.
*/
template <
typename InType,
typename IndexType,
typename OffsetType = std::int32_t,
typename OutType = float,
bool THREAD_LOCAL = false>
FBGEMM_API typename EmbeddingSpMDMKernelSignature<
InType,
IndexType,
OffsetType,
OutType>::Type
GenerateEmbeddingSpMDM(
const std::int64_t block_size,
bool has_weight,
bool normalize_by_lengths,
int prefetch = 16,
bool is_weight_positional = false,
bool use_offsets = true,
bool is_bf16_out = false,
bool is_bf16_in = false);
/**
* @param output_stride If -1, output_stride is same as block_size
* @param input_stride If -1, input_stride is same as block_size
* @param scale_bias_last if false, scale and bias appear at the beginning
* of each row and are in fp16 for table batched embedding (TBE)
* in FBGEMM_GPU. If false, it can also take -1 indices (output from
* pruned embedding id mapping)
*/
template <
typename InType,
typename IndexType,
typename OffsetType = std::int32_t,
typename OutType = float,
bool THREAD_LOCAL = false>
FBGEMM_API typename EmbeddingSpMDMKernelSignature<
InType,
IndexType,
OffsetType,
OutType>::Type
GenerateEmbeddingSpMDMWithStrides(
const std::int64_t block_size,
bool has_weight,
bool normalize_by_lengths,
int prefetch = 16,
bool is_weight_positional = false,
bool use_offsets = true,
std::int64_t output_stride = -1,
std::int64_t input_stride = -1,
bool scale_bias_last = true,
bool no_bag = false,
bool is_bf16_out = false,
bool is_bf16_in = false);
/**
* @tparam IndexType can be int32_t or int64_t
* @tparam OffsetType can be int32_t or int64_t
* @param bit_rate can be 2 or 4
*/
template <
typename IndexType,
typename OffsetType = std::int32_t,
typename OutType = float>
FBGEMM_API typename EmbeddingSpMDMKernelSignature<
std::uint8_t,
IndexType,
OffsetType,
OutType>::Type
GenerateEmbeddingSpMDMNBit(
int bit_rate,
const std::int64_t block_size,
bool has_weight,
bool normalize_by_lengths,
int prefetch = 16,
bool is_weight_positional = false,
bool use_offsets = true);
/**
* @param output_stride If -1, output_stride is same as block_size
* @param input_stride in Bytes. If -1, input_stride is same as
* block_size / num_elem_per_byte + 2 * sizeof(float16)
* @param scale_bias_last if false, scale and bias appear at the beginning
* of each row and are in fp16 for table batched embedding (TBE)
* in FBGEMM_GPU. If false, it can also take -1 indices (output from
* pruned embedding id mapping)
*/
template <
typename IndexType,
typename OffsetType = std::int32_t,
typename OutType = float,
bool THREAD_LOCAL = false>
FBGEMM_API typename EmbeddingSpMDMKernelSignature<
std::uint8_t,
IndexType,
OffsetType,
OutType>::Type
GenerateEmbeddingSpMDMNBitWithStrides(
const int input_bit_rate,
const std::int64_t block_size,
bool has_weight,
bool normalize_by_lengths,
int prefetch = 16,
bool is_weight_positional = false,
bool use_offsets = true,
std::int64_t output_stride = -1,
std::int64_t input_stride = -1,
bool scale_bias_last = true,
const bool is_bf16_out = false,
const bool no_bag = false,
int output_bit_rate = -1);
/**
* @param output_stride If -1, output_stride is same as block_size
* @param input_stride in Bytes. If -1, input_stride is same as
* block_size / num_elem_per_byte + 2 * sizeof(float16)
* @param exponent_bits is the number of exponent bits in the FP8 encode
* (normally 4 or 5)
* @param exponent_bias is subtracted from the exponent to obtain the actual
* exponent for the floating-point number
*/
template <
typename IndexType,
typename OffsetType = std::int32_t,
typename OutType = float>
FBGEMM_API typename EmbeddingSpMDMKernelSignature<
std::uint8_t,
IndexType,
OffsetType,
OutType>::Type
GenerateEmbeddingSpMDMFP8WithStrides(
const std::int64_t block_size,
bool normalize_by_lengths,
bool is_weight_positional = false,
bool use_offsets = true,
std::int64_t output_stride = -1,
std::int64_t input_stride = -1,
int exponent_bits = 4,
int exponent_bias = 7,
bool is_bf16_out = false);
template <
typename InType,
typename IndexType,
typename OffsetType = std::int32_t>
class EmbeddingSpMDMRowWiseSparseKernelSignature {
public:
using Type = std::function<bool(
std::int64_t output_size,
std::int64_t index_size,
std::int64_t uncompressed_data_size,
// TODO: add compressed_data_size and check array bound
const InType* input,
const IndexType* indices,
const OffsetType* offsets_or_lengths,
const float* weights, // optional, can be null for non-weighted sum
float* out,
const std::int32_t* compressed_indices_table)>;
};
/**
* @tparam InType can be float, float16, or uint8_t
* @tparam IndexType can be int32_t or int64_t
* @tparam OffsetType can be int32_t or int64_t
*/
template <
typename InType,
typename IndexType,
typename OffsetType = std::int32_t>
FBGEMM_API typename EmbeddingSpMDMRowWiseSparseKernelSignature<
InType,
IndexType,
OffsetType>::Type
GenerateEmbeddingSpMDMRowWiseSparse(
const std::int64_t block_size,
bool has_weight,
bool normalize_by_lengths,
int prefetch = 16,
bool is_weight_positional = false,
bool use_offsets = true);
/**
* @tparam IndexType can be int32_t or int64_t
* @tparam OffsetType can be int32_t or int64_t
* @param bit_rate can be 2 or 4
*/
template <typename IndexType, typename OffsetType = std::int32_t>
FBGEMM_API typename EmbeddingSpMDMRowWiseSparseKernelSignature<
std::uint8_t,
IndexType,
OffsetType>::Type
GenerateEmbeddingSpMDMNBitRowWiseSparse(
int bit_rate,
const std::int64_t block_size,
bool has_weight,
bool normalize_by_lengths,
int prefetch = 16,
bool is_weight_positional = false,
bool use_offsets = true);
/**
* @return The number of rows processed. If smaller than num_rows, an error
* must have happened at the last row processed.
*/
template <typename IndexType>
class SparseAdaGradSignature {
public:
using Type = std::function<int(
int num_rows, // number of rows reading
std::uint64_t param_size, // total number of parameters
float* w, // input/output parameters
const float* g, // input gradients
float* h, // input/output momentums
const IndexType* indices, // indices of each row
float epsilon,
float lr,
float weight_decay,
const double* counter, // used for weight_decay adjusted for frequency
// nullptr when frequency adjustment is not used.
// ignored when the kernel is generated with
// use_weight_decay = false.
std::int64_t counter_halflife)>; // frequency adjust happens only after
};
template <typename IndexType>
FBGEMM_API typename SparseAdaGradSignature<IndexType>::Type
GenerateSparseAdaGrad(
int block_size, // number of parameters per row
bool rowwise = false,
int prefetch = 16,
bool use_weight_decay = false);
// RowWiseSparseAdaGrad fused with SLS gradient
// Weights can be either float or float16
template <
typename IndexType,
typename OffsetType = std::int32_t,
typename DataType = float>
class RowWiseSparseAdaGradFusedSignature {
public:
using Type = std::function<bool(
std::int64_t output_size,
std::int64_t index_size,
std::int64_t data_size, // number of rows in w
DataType* w, // input/output parameters
const float* g, // input gradients
float* h, // input/output momentums
const IndexType* indices, // indices of each row
const OffsetType* offsets_or_lengths,
float epsilon,
float lr)>;
};
/**
* @param grad_stride If -1, grad_stride is same as block size
*/
template <
typename IndexType,
typename OffsetType = std::int32_t,
typename DataType = float>
FBGEMM_API typename RowWiseSparseAdaGradFusedSignature<
IndexType,
OffsetType,
DataType>::Type
GenerateRowWiseSparseAdaGradFused(
int block_size, // number of parameters per row
int prefetch = 16,
bool use_offsets = true,
bool use_stochastic_rounding = true,
int grad_stride = -1);
namespace internal {
// Specialization for block size 1 internally called by GenerateEmbeddingSpMDM
template <typename InType, typename IndexType, typename OffsetType>
FBGEMM_API bool EmbeddingSpMDMBlockSize1_(
const std::int64_t output_size,
const std::int64_t index_size,
const std::int64_t data_size, // the number of rows in input
const InType* input,
const IndexType* indices,
const OffsetType* offsets_or_lengths,
const float* weights, // optional, can be null for non-weighted sum
bool normalize_by_lengths,
float* out,
bool is_weight_positional = false,
bool use_offsets = true,
bool is_bf16 = false);
template <typename IndexType, bool HAS_WEIGHTS>
void compressed_indices_remap_avx512(
std::int32_t offsets_numel,
const IndexType* indices,
const int32_t* compressed_indices_mapping,
const IndexType* offsets,
const float* weights, // optional, can be null,
IndexType* out_indices,
IndexType* out_offsets,
float* out_weights);
} // namespace internal
template <typename IndexType>
FBGEMM_API void compressed_indices_remap(
std::int32_t offsets_numel,
const IndexType* indices,
const int32_t* compressed_indices_mapping,
const IndexType* offsets,
const float* weights, // optional, can be null,
IndexType* out_indices,
IndexType* out_offsets,
float* out_weights);
} // namespace fbgemm
|