| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <math.h> |
| 8 |
#include <stddef.h> |
| 9 |
#include <stdint.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include "xnnpack.h" |
| 13 |
#include "xnnpack/allocator.h" |
| 14 |
#include "xnnpack/common.h" |
| 15 |
#include "xnnpack/compute.h" |
| 16 |
#include "xnnpack/config-types.h" |
| 17 |
#include "xnnpack/config.h" |
| 18 |
#include "xnnpack/log.h" |
| 19 |
#include "xnnpack/math.h" |
| 20 |
#include "xnnpack/microkernel-type.h" |
| 21 |
#include "xnnpack/microparams.h" |
| 22 |
#include "xnnpack/operator-type.h" |
| 23 |
#include "xnnpack/operator.h" |
| 24 |
#include "xnnpack/params.h" |
| 25 |
#include "pthreadpool.h" |
| 26 |
|
| 27 |
static enum xnn_status create_scaled_dot_product_attention_nhtc( |
| 28 |
enum xnn_attention_logits_cap_type cap_type, |
| 29 |
const void* cap_params, |
| 30 |
enum xnn_operator_type operator_type, |
| 31 |
const struct xnn_gemm_config* gemm_config, |
| 32 |
const struct xnn_raddstoreexpminusmax_config* raddstoreexpminusmax_config, |
| 33 |
const struct xnn_rmax_config* rmax_config, |
| 34 |
const struct xnn_binary_elementwise_config* vadd_config, |
| 35 |
const struct xnn_binary_elementwise_config* vmul_config, |
| 36 |
const struct xnn_unary_elementwise_config* vtanh_config, |
| 37 |
const void* minmax_params, |
| 38 |
size_t minmax_params_size, |
| 39 |
const void* expminus_params, |
| 40 |
size_t expminus_params_size, |
| 41 |
const void* rmax_params, |
| 42 |
size_t rmax_params_size, |
| 43 |
const void* tanh_params, |
| 44 |
size_t tanh_params_size, |
| 45 |
uint32_t flags, |
| 46 |
xnn_operator_t* attention_op_out) |
| 47 |
{ |
| 48 |
xnn_operator_t attention_op = NULL; |
| 49 |
enum xnn_status status = xnn_status_uninitialized; |
| 50 |
|
| 51 |
if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) { |
| 52 |
xnn_log_error("failed to create %s operator: XNNPACK is not initialized", |
| 53 |
xnn_operator_type_to_string(operator_type)); |
| 54 |
goto error; |
| 55 |
} |
| 56 |
|
| 57 |
status = xnn_status_out_of_memory; |
| 58 |
|
| 59 |
attention_op = xnn_allocate_zero_simd_memory(sizeof(struct xnn_operator)); |
| 60 |
if (attention_op == NULL) { |
| 61 |
xnn_log_error( |
| 62 |
"failed to allocate %zu bytes for %s operator descriptor", |
| 63 |
sizeof(struct xnn_operator), xnn_operator_type_to_string(operator_type)); |
| 64 |
goto error; |
| 65 |
} |
| 66 |
|
| 67 |
const size_t nr = gemm_config->nr; |
| 68 |
const size_t mr = gemm_config->mr; |
| 69 |
attention_op->ukernel.type = xnn_microkernel_type_gemm; |
| 70 |
attention_op->ukernel.gemm = (struct xnn_ukernel_gemm) { |
| 71 |
.mr = mr, |
| 72 |
.nr = nr, |
| 73 |
.kr = UINT32_C(1) << gemm_config->log2_kr, |
| 74 |
.sr = UINT32_C(1) << gemm_config->log2_sr, |
| 75 |
}; |
| 76 |
|
| 77 |
assert(XNN_MAX_MR >= mr); |
| 78 |
for (size_t i = 0; i < mr; i++) { |
| 79 |
attention_op->ukernel.gemm.gemm_cases[i] = gemm_config->minmax.gemm[i]; |
| 80 |
} |
| 81 |
attention_op->ukernel.gemm.packw_gemm_goi = gemm_config->pack_gemm_goi; |
| 82 |
attention_op->ukernel.gemm.packw_gemm_gio = gemm_config->pack_gemm_gio; |
| 83 |
|
| 84 |
memcpy(&attention_op->params, minmax_params, minmax_params_size); |
| 85 |
memcpy(&attention_op->params2, expminus_params, expminus_params_size); |
| 86 |
memcpy(&attention_op->params3, rmax_params, rmax_params_size); |
| 87 |
memcpy(&attention_op->params4, tanh_params, tanh_params_size); |
| 88 |
|
| 89 |
if (cap_type == xnn_attention_logits_cap_type_tanh) { |
| 90 |
const struct xnn_attention_logits_cap_tanh_params* cap_tanh_params = |
| 91 |
(const struct xnn_attention_logits_cap_tanh_params*) cap_params; |
| 92 |
memcpy(&attention_op->attention.cap_params, cap_tanh_params, sizeof(struct xnn_attention_logits_cap_tanh_params)); |
| 93 |
} |
| 94 |
|
| 95 |
attention_op->attention.raddstoreexpminusmax_config = raddstoreexpminusmax_config; |
| 96 |
attention_op->attention.rmax_config = rmax_config; |
| 97 |
attention_op->attention.vadd_config = vadd_config; |
| 98 |
attention_op->attention.vmul_config = vmul_config; |
| 99 |
attention_op->attention.vtanh_config = vtanh_config; |
| 100 |
attention_op->attention.cap_type = cap_type; |
| 101 |
|
| 102 |
attention_op->state = xnn_run_state_invalid; |
| 103 |
attention_op->type = operator_type; |
| 104 |
attention_op->flags = flags; |
| 105 |
|
| 106 |
*attention_op_out = attention_op; |
| 107 |
|
| 108 |
return xnn_status_success; |
| 109 |
|
| 110 |
error: |
| 111 |
xnn_delete_operator(attention_op); |
| 112 |
return status; |
| 113 |
} |
| 114 |
|
| 115 |
enum xnn_status xnn_create_scaled_dot_product_attention_nhtc_f16( |
| 116 |
enum xnn_attention_logits_cap_type cap_type, |
| 117 |
const void* cap_params, |
| 118 |
uint32_t flags, |
| 119 |
xnn_operator_t* attention_op_out) |
| 120 |
{ |
| 121 |
const enum xnn_operator_type operator_type = xnn_operator_type_scaled_dot_product_attention_nhtc_f16; |
| 122 |
enum xnn_status status = xnn_status_unsupported_hardware; |
| 123 |
|
| 124 |
const struct xnn_gemm_config* gemm_config = xnn_init_f16_gemm_config(); |
| 125 |
if (gemm_config == NULL) { |
| 126 |
xnn_log_error("failed to create %s operator: unsupported hardware configuration", |
| 127 |
xnn_operator_type_to_string(operator_type)); |
| 128 |
goto error; |
| 129 |
} |
| 130 |
|
| 131 |
union xnn_f16_minmax_params minmax_params; |
| 132 |
if XNN_LIKELY(gemm_config->init.f16 != NULL) { |
| 133 |
gemm_config->init.f16(&minmax_params, xnn_float16_from_float(-INFINITY), xnn_float16_from_float(INFINITY)); |
| 134 |
} |
| 135 |
|
| 136 |
const struct xnn_raddstoreexpminusmax_config* raddstoreexpminusmax_config = |
| 137 |
xnn_init_f16_raddstoreexpminusmax_config(); |
| 138 |
if (raddstoreexpminusmax_config == NULL) { |
| 139 |
xnn_log_error( |
| 140 |
"failed to create %s operator: unsupported hardware configuration", |
| 141 |
xnn_operator_type_to_string(operator_type)); |
| 142 |
goto error; |
| 143 |
} |
| 144 |
|
| 145 |
struct xnn_f16_default_params expminus_params; |
| 146 |
if (raddstoreexpminusmax_config->init.f16 != NULL) { |
| 147 |
raddstoreexpminusmax_config->init.f16(&expminus_params); |
| 148 |
} |
| 149 |
|
| 150 |
const struct xnn_rmax_config* rmax_config = xnn_init_f16_rmax_config(); |
| 151 |
if (rmax_config == NULL) { |
| 152 |
xnn_log_error( |
| 153 |
"failed to create %s operator: unsupported hardware configuration", |
| 154 |
xnn_operator_type_to_string(operator_type)); |
| 155 |
goto error; |
| 156 |
} |
| 157 |
|
| 158 |
struct xnn_f16_default_params rmax_params; |
| 159 |
if (rmax_config->init.f16 != NULL) { |
| 160 |
rmax_config->init.f16(&rmax_params); |
| 161 |
} |
| 162 |
|
| 163 |
const struct xnn_binary_elementwise_config* vadd_config = xnn_init_f16_vadd_config(); |
| 164 |
if (vadd_config == NULL) { |
| 165 |
xnn_log_error( |
| 166 |
"failed to create %s operator: unsupported hardware configuration", |
| 167 |
xnn_operator_type_to_string(operator_type)); |
| 168 |
goto error; |
| 169 |
} |
| 170 |
|
| 171 |
const struct xnn_binary_elementwise_config* vmul_config = xnn_init_f16_vmul_config(); |
| 172 |
if (vmul_config == NULL) { |
| 173 |
xnn_log_error( |
| 174 |
"failed to create %s operator: unsupported hardware configuration", |
| 175 |
xnn_operator_type_to_string(operator_type)); |
| 176 |
goto error; |
| 177 |
} |
| 178 |
|
| 179 |
const struct xnn_unary_elementwise_config* vtanh_config = xnn_init_f16_tanh_config(); |
| 180 |
if (vtanh_config == NULL) { |
| 181 |
xnn_log_error( |
| 182 |
"failed to create %s operator: unsupported hardware configuration", |
| 183 |
xnn_operator_type_to_string(operator_type)); |
| 184 |
goto error; |
| 185 |
} |
| 186 |
|
| 187 |
union xnn_unary_uparams tanh_params; |
| 188 |
if XNN_LIKELY(vtanh_config->init != NULL) { |
| 189 |
vtanh_config->init(&tanh_params, NULL, NULL, NULL); |
| 190 |
} |
| 191 |
|
| 192 |
status = xnn_status_invalid_parameter; |
| 193 |
|
| 194 |
if (cap_type == xnn_attention_logits_cap_type_tanh) { |
| 195 |
const struct xnn_attention_logits_cap_tanh_params* cap_tanh_params = |
| 196 |
(const struct xnn_attention_logits_cap_tanh_params*) cap_params; |
| 197 |
const float cap = cap_tanh_params->cap; |
| 198 |
if (cap <= 0.0f || isnan(cap) || cap < 0x1.0p-14f || cap > 65504.0f) { |
| 199 |
xnn_log_error("failed to create %s operator with Cap TanH: cap value (%f) must be greater than 0, representable " |
| 200 |
"in FP16, and not be NaN", xnn_operator_type_to_string(operator_type), cap_tanh_params->cap); |
| 201 |
goto error; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return create_scaled_dot_product_attention_nhtc( |
| 206 |
cap_type, cap_params, |
| 207 |
operator_type, |
| 208 |
gemm_config, |
| 209 |
raddstoreexpminusmax_config, |
| 210 |
rmax_config, |
| 211 |
vadd_config, |
| 212 |
vmul_config, |
| 213 |
vtanh_config, |
| 214 |
&minmax_params, sizeof(minmax_params), |
| 215 |
&expminus_params, sizeof(expminus_params), |
| 216 |
&rmax_params, sizeof(rmax_params), |
| 217 |
&tanh_params, sizeof(tanh_params), |
| 218 |
flags, |
| 219 |
attention_op_out); |
| 220 |
|
| 221 |
error: |
| 222 |
return status; |
| 223 |
} |
| 224 |
|
| 225 |
enum xnn_status xnn_create_scaled_dot_product_attention_nhtc_f32( |
| 226 |
enum xnn_attention_logits_cap_type cap_type, |
| 227 |
const void* cap_params, |
| 228 |
uint32_t flags, |
| 229 |
xnn_operator_t* attention_op_out) |
| 230 |
{ |
| 231 |
const enum xnn_operator_type operator_type = xnn_operator_type_scaled_dot_product_attention_nhtc_f32; |
| 232 |
enum xnn_status status = xnn_status_unsupported_hardware; |
| 233 |
|
| 234 |
const struct xnn_gemm_config* gemm_config = xnn_init_f32_gemm_config(); |
| 235 |
if (gemm_config == NULL) { |
| 236 |
xnn_log_error("failed to create %s operator: unsupported hardware configuration", |
| 237 |
xnn_operator_type_to_string(operator_type)); |
| 238 |
goto error; |
| 239 |
} |
| 240 |
|
| 241 |
union xnn_f32_minmax_params minmax_params; |
| 242 |
if XNN_LIKELY(gemm_config->init.f32 != NULL) { |
| 243 |
gemm_config->init.f32(&minmax_params, -INFINITY , INFINITY); |
| 244 |
} |
| 245 |
|
| 246 |
const struct xnn_raddstoreexpminusmax_config* raddstoreexpminusmax_config = |
| 247 |
xnn_init_f32_raddstoreexpminusmax_config(); |
| 248 |
if (raddstoreexpminusmax_config == NULL) { |
| 249 |
xnn_log_error( |
| 250 |
"failed to create %s operator: unsupported hardware configuration", |
| 251 |
xnn_operator_type_to_string(operator_type)); |
| 252 |
goto error; |
| 253 |
} |
| 254 |
|
| 255 |
struct xnn_f32_default_params expminus_params; |
| 256 |
|
| 257 |
const struct xnn_rmax_config* rmax_config = xnn_init_f32_rmax_config(); |
| 258 |
if (rmax_config == NULL) { |
| 259 |
xnn_log_error( |
| 260 |
"failed to create %s operator: unsupported hardware configuration", |
| 261 |
xnn_operator_type_to_string(operator_type)); |
| 262 |
goto error; |
| 263 |
} |
| 264 |
|
| 265 |
struct xnn_f32_default_params rmax_params; |
| 266 |
|
| 267 |
const struct xnn_binary_elementwise_config* vadd_config = xnn_init_f32_vadd_config(); |
| 268 |
if (vadd_config == NULL) { |
| 269 |
xnn_log_error( |
| 270 |
"failed to create %s operator: unsupported hardware configuration", |
| 271 |
xnn_operator_type_to_string(operator_type)); |
| 272 |
goto error; |
| 273 |
} |
| 274 |
|
| 275 |
const struct xnn_binary_elementwise_config* vmul_config = xnn_init_f32_vmul_config(); |
| 276 |
if (vmul_config == NULL) { |
| 277 |
xnn_log_error( |
| 278 |
"failed to create %s operator: unsupported hardware configuration", |
| 279 |
xnn_operator_type_to_string(operator_type)); |
| 280 |
goto error; |
| 281 |
} |
| 282 |
|
| 283 |
const struct xnn_unary_elementwise_config* vtanh_config = xnn_init_f32_tanh_config(); |
| 284 |
if (vtanh_config == NULL) { |
| 285 |
xnn_log_error( |
| 286 |
"failed to create %s operator: unsupported hardware configuration", |
| 287 |
xnn_operator_type_to_string(operator_type)); |
| 288 |
goto error; |
| 289 |
} |
| 290 |
|
| 291 |
union xnn_unary_uparams tanh_params; |
| 292 |
if XNN_LIKELY(vtanh_config->init != NULL) { |
| 293 |
vtanh_config->init(&tanh_params, NULL, NULL, NULL); |
| 294 |
} |
| 295 |
|
| 296 |
status = xnn_status_invalid_parameter; |
| 297 |
|
| 298 |
if (cap_type == xnn_attention_logits_cap_type_tanh) { |
| 299 |
const struct xnn_attention_logits_cap_tanh_params* cap_tanh_params = |
| 300 |
(const struct xnn_attention_logits_cap_tanh_params*) cap_params; |
| 301 |
float cap = cap_tanh_params->cap; |
| 302 |
if (cap <= 0.0f || !isnormal(cap)) { |
| 303 |
xnn_log_error("failed to create %s operator with Cap TanH: cap value (%f) must be finite and greater than 0", |
| 304 |
xnn_operator_type_to_string(operator_type), cap_tanh_params->cap); |
| 305 |
goto error; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
return create_scaled_dot_product_attention_nhtc( |
| 310 |
cap_type, cap_params, |
| 311 |
operator_type, |
| 312 |
gemm_config, |
| 313 |
raddstoreexpminusmax_config, |
| 314 |
rmax_config, |
| 315 |
vadd_config, |
| 316 |
vmul_config, |
| 317 |
vtanh_config, |
| 318 |
&minmax_params, sizeof(minmax_params), |
| 319 |
&expminus_params, sizeof(expminus_params), |
| 320 |
&rmax_params, sizeof(rmax_params), |
| 321 |
&tanh_params, sizeof(tanh_params), |
| 322 |
flags, |
| 323 |
attention_op_out); |
| 324 |
|
| 325 |
error: |
| 326 |
return status; |
| 327 |
} |
| 328 |
|
| 329 |
static void compute_reciprocal_f16( |
| 330 |
const xnn_float16 input[XNN_MIN_ELEMENTS(1)], |
| 331 |
xnn_float16 output[XNN_MIN_ELEMENTS(1)]) |
| 332 |
{ |
| 333 |
*output = xnn_float16_from_float(1.0f / xnn_float16_to_float(*input)); |
| 334 |
} |
| 335 |
|
| 336 |
static void compute_reciprocal_f32( |
| 337 |
const float input[XNN_MIN_ELEMENTS(1)], |
| 338 |
float output[XNN_MIN_ELEMENTS(1)]) |
| 339 |
{ |
| 340 |
*output = 1.0f / *input; |
| 341 |
} |
| 342 |
|
| 343 |
static enum xnn_status reshape_scaled_dot_product_attention_nhtc( |
| 344 |
xnn_operator_t attention_op, |
| 345 |
enum xnn_operator_type expected_operator_type, |
| 346 |
size_t batch_size, |
| 347 |
size_t query_heads, |
| 348 |
size_t query_tokens, |
| 349 |
size_t key_value_heads, |
| 350 |
size_t key_value_tokens, |
| 351 |
size_t query_key_channels, |
| 352 |
size_t value_channels, |
| 353 |
size_t* workspace_size, |
| 354 |
size_t* workspace_alignment, |
| 355 |
size_t log2_element_size, |
| 356 |
size_t element_size, |
| 357 |
xnn_compute_reciprocal_fn compute_reciprocal, |
| 358 |
void* cap, |
| 359 |
void* cap_reciprocal, |
| 360 |
size_t cap_size, |
| 361 |
const void* minmax_params, |
| 362 |
size_t minmax_params_size, |
| 363 |
const void* expminus_params, |
| 364 |
size_t expminus_params_size, |
| 365 |
const void* rmax_params, |
| 366 |
size_t rmax_params_size, |
| 367 |
const void* tanh_params, |
| 368 |
size_t tanh_params_size, |
| 369 |
pthreadpool_t threadpool) |
| 370 |
{ |
| 371 |
if (attention_op->type != expected_operator_type) { |
| 372 |
xnn_log_error("failed to reshape operator: operator type mismatch (expected %s, got %s)", |
| 373 |
xnn_operator_type_to_string(expected_operator_type), |
| 374 |
xnn_operator_type_to_string(attention_op->type)); |
| 375 |
return xnn_status_invalid_parameter; |
| 376 |
} |
| 377 |
attention_op->state = xnn_run_state_invalid; |
| 378 |
|
| 379 |
if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) { |
| 380 |
xnn_log_error("failed to reshape %s operator: XNNPACK is not initialized", |
| 381 |
xnn_operator_type_to_string(attention_op->type)); |
| 382 |
return xnn_status_uninitialized; |
| 383 |
} |
| 384 |
|
| 385 |
if (batch_size == 0) { |
| 386 |
xnn_log_error( |
| 387 |
"failed to create %s operator with batch size of %zu: batch size must be non-zero", |
| 388 |
xnn_operator_type_to_string(expected_operator_type), batch_size); |
| 389 |
return xnn_status_invalid_parameter; |
| 390 |
} |
| 391 |
|
| 392 |
if (query_heads == 0) { |
| 393 |
xnn_log_error( |
| 394 |
"failed to create %s operator with number of query heads %zu: number of query heads must be non-zero", |
| 395 |
xnn_operator_type_to_string(expected_operator_type), query_heads); |
| 396 |
return xnn_status_invalid_parameter; |
| 397 |
} |
| 398 |
|
| 399 |
if (key_value_heads == 0) { |
| 400 |
xnn_log_error( |
| 401 |
"failed to create %s operator with number of key/value heads %zu: number of key/value heads must be non-zero", |
| 402 |
xnn_operator_type_to_string(expected_operator_type), key_value_heads); |
| 403 |
return xnn_status_invalid_parameter; |
| 404 |
} |
| 405 |
|
| 406 |
if (key_value_heads != 1 && key_value_heads != query_heads) { |
| 407 |
xnn_log_error( |
| 408 |
"failed to create %s operator with number of key/value heads %zu: number of key/value heads must be either 1 or " |
| 409 |
"equal to number of query heads", xnn_operator_type_to_string(expected_operator_type), key_value_heads); |
| 410 |
return xnn_status_invalid_parameter; |
| 411 |
} |
| 412 |
|
| 413 |
if (query_tokens == 0) { |
| 414 |
xnn_log_error( |
| 415 |
"failed to create %s operator with query tokens of %zu: query tokens must be non-zero", |
| 416 |
xnn_operator_type_to_string(expected_operator_type), query_tokens); |
| 417 |
return xnn_status_invalid_parameter; |
| 418 |
} |
| 419 |
|
| 420 |
if (key_value_tokens == 0) { |
| 421 |
xnn_log_error( |
| 422 |
"failed to create %s operator with key/value tokens of %zu: key/value tokens must be non-zero", |
| 423 |
xnn_operator_type_to_string(expected_operator_type), query_tokens); |
| 424 |
return xnn_status_invalid_parameter; |
| 425 |
} |
| 426 |
|
| 427 |
if (query_key_channels == 0) { |
| 428 |
xnn_log_error( |
| 429 |
"failed to create %s operator with %zu channels: query/key channels must be non-zero", |
| 430 |
xnn_operator_type_to_string(expected_operator_type), query_key_channels); |
| 431 |
return xnn_status_invalid_parameter; |
| 432 |
} |
| 433 |
|
| 434 |
if (value_channels == 0) { |
| 435 |
xnn_log_error( |
| 436 |
"failed to create %s operator with %zu channels: value channels must be non-zero", |
| 437 |
xnn_operator_type_to_string(expected_operator_type), value_channels); |
| 438 |
return xnn_status_invalid_parameter; |
| 439 |
} |
| 440 |
|
| 441 |
const uint32_t mr = attention_op->ukernel.gemm.mr; |
| 442 |
const uint32_t nr = attention_op->ukernel.gemm.nr; |
| 443 |
const uint32_t kr = attention_op->ukernel.gemm.kr; |
| 444 |
const uint32_t sr = attention_op->ukernel.gemm.sr; |
| 445 |
|
| 446 |
const size_t num_threads = pthreadpool_get_threads_count(threadpool); |
| 447 |
const size_t size_using_threads = num_threads * mr; |
| 448 |
const size_t size_using_batch = batch_size * query_heads * query_tokens; |
| 449 |
const bool use_threads_workspace_size = size_using_threads < size_using_batch; |
| 450 |
const size_t workspace_multiplier = use_threads_workspace_size ? size_using_threads : size_using_batch; |
| 451 |
|
| 452 |
|
| 453 |
const size_t scaled_query_size = |
| 454 |
round_up_po2(workspace_multiplier * query_key_channels * element_size + XNN_EXTRA_BYTES, XNN_ALLOCATION_ALIGNMENT); |
| 455 |
|
| 456 |
|
| 457 |
const size_t key_n_stride = round_up(key_value_tokens, nr); |
| 458 |
const size_t key_k_stride = round_up_po2(query_key_channels, kr * sr); |
| 459 |
const size_t key_head_stride = key_n_stride * (element_size + (key_k_stride << log2_element_size)); |
| 460 |
|
| 461 |
const size_t packed_key_size = round_up_po2(batch_size * key_value_heads * key_head_stride, XNN_ALLOCATION_ALIGNMENT); |
| 462 |
|
| 463 |
|
| 464 |
const size_t value_n_stride = round_up(value_channels, nr); |
| 465 |
const size_t value_k_stride = round_up_po2(key_value_tokens, kr * sr); |
| 466 |
const size_t value_head_stride = value_n_stride * (element_size + (value_k_stride << log2_element_size)); |
| 467 |
|
| 468 |
const size_t packed_value_size = round_up_po2(batch_size * key_value_heads * value_head_stride, XNN_ALLOCATION_ALIGNMENT); |
| 469 |
|
| 470 |
|
| 471 |
const size_t logits_size = |
| 472 |
round_up_po2(workspace_multiplier * key_value_tokens * element_size + XNN_EXTRA_BYTES, XNN_ALLOCATION_ALIGNMENT); |
| 473 |
|
| 474 |
const size_t total_workspace_size = scaled_query_size + packed_key_size + packed_value_size + logits_size; |
| 475 |
|
| 476 |
*workspace_size = total_workspace_size; |
| 477 |
*workspace_alignment = XNN_ALLOCATION_ALIGNMENT; |
| 478 |
|
| 479 |
|
| 480 |
attention_op->context.gemm.packw_gemm_goi = (struct packw_gemm_goi_context) { |
| 481 |
.kc = query_key_channels, |
| 482 |
.nr = nr, |
| 483 |
.kr = kr, |
| 484 |
.sr = sr, |
| 485 |
.k_stride = query_key_channels << log2_element_size, |
| 486 |
|
| 487 |
.w_stride = element_size + (key_k_stride << log2_element_size), |
| 488 |
.packw_gemm_goi = attention_op->ukernel.gemm.packw_gemm_goi, |
| 489 |
.gk_stride = key_value_tokens * (query_key_channels << log2_element_size), |
| 490 |
.gc_stride = key_head_stride, |
| 491 |
}; |
| 492 |
attention_op->compute[0].type = xnn_parallelization_type_2d_tile_1d; |
| 493 |
attention_op->compute[0].task_2d_tile_1d = (pthreadpool_task_2d_tile_1d_t) xnn_compute_batched_packw_gemm_goi; |
| 494 |
attention_op->compute[0].context_offset = |
| 495 |
offsetof(struct xnn_operator, context.gemm.packw_gemm_goi) - offsetof(struct xnn_operator, context); |
| 496 |
attention_op->compute[0].range[0] = batch_size * key_value_heads; |
| 497 |
attention_op->compute[0].range[1] = key_value_tokens; |
| 498 |
|
| 499 |
|
| 500 |
attention_op->compute[0].tile[0] = key_value_tokens; |
| 501 |
|
| 502 |
|
| 503 |
attention_op->context.gemm.packw_gemm_gio = (struct packw_gemm_gio_context) { |
| 504 |
.kc = key_value_tokens, |
| 505 |
.nr = nr, |
| 506 |
.kr = kr, |
| 507 |
.sr = sr, |
| 508 |
.n_stride = 1 << log2_element_size, |
| 509 |
.k_stride_elements = value_channels, |
| 510 |
|
| 511 |
.w_stride = element_size + (value_k_stride << log2_element_size), |
| 512 |
.packw_gemm_gio = attention_op->ukernel.gemm.packw_gemm_gio, |
| 513 |
.gk_stride = key_value_tokens * (value_channels << log2_element_size), |
| 514 |
.gb_stride = value_channels * element_size, |
| 515 |
.gc_stride = value_head_stride, |
| 516 |
}; |
| 517 |
attention_op->compute[1].type = xnn_parallelization_type_2d_tile_1d; |
| 518 |
attention_op->compute[1].task_2d_tile_1d = (pthreadpool_task_2d_tile_1d_t) xnn_compute_batched_packw_gemm_gio; |
| 519 |
attention_op->compute[1].context_offset = |
| 520 |
offsetof(struct xnn_operator, context.gemm.packw_gemm_gio) - offsetof(struct xnn_operator, context); |
| 521 |
attention_op->compute[1].range[0] = batch_size * key_value_heads; |
| 522 |
attention_op->compute[1].range[1] = value_channels; |
| 523 |
attention_op->compute[1].tile[0] = value_channels; |
| 524 |
|
| 525 |
struct xnn_hmp_gemm_ukernel gemm_ukernel = attention_op->ukernel.gemm.gemm_cases[mr - 1]; |
| 526 |
|
| 527 |
attention_op->context.gemm.gemm.attention = (struct scaled_dot_product_attention_context){ |
| 528 |
.key_value_tokens = key_value_tokens, |
| 529 |
.key_value_tokens_scaled = key_value_tokens * element_size, |
| 530 |
.query_key_channels = query_key_channels, |
| 531 |
.query_key_scaled_channels = query_key_channels * element_size, |
| 532 |
.value_channels = value_channels, |
| 533 |
.value_scaled_channels = value_channels * element_size, |
| 534 |
.cn_stride = nr << log2_element_size, |
| 535 |
.query_batch_stride = query_heads * query_tokens * query_key_channels * element_size, |
| 536 |
.query_head_stride = query_tokens * query_key_channels * element_size, |
| 537 |
.key_batch_stride = key_value_heads * key_head_stride, |
| 538 |
.key_head_stride = key_value_heads == 1 ? 0 : key_head_stride, |
| 539 |
.value_batch_stride = key_value_heads * value_head_stride, |
| 540 |
.value_head_stride = key_value_heads == 1 ? 0 : value_head_stride, |
| 541 |
.logits_batch_stride = query_heads * query_tokens * key_value_tokens * element_size, |
| 542 |
.logits_head_stride = query_tokens * key_value_tokens * element_size, |
| 543 |
.output_batch_stride = query_heads * query_tokens * value_channels * element_size, |
| 544 |
.output_head_stride = query_tokens * value_channels * element_size, |
| 545 |
.scaled_query_thread_stride = mr * query_key_channels * element_size, |
| 546 |
.logits_thread_stride = mr * key_value_tokens * element_size, |
| 547 |
.gemm_ukernel = gemm_ukernel, |
| 548 |
.compute_reciprocal = compute_reciprocal, |
| 549 |
.raddstoreexpminusmax_ukernel = attention_op->attention.raddstoreexpminusmax_config->ukernel, |
| 550 |
.rmax_ukernel = attention_op->attention.rmax_config->ukernel, |
| 551 |
.vadd_ukernel = attention_op->attention.vadd_config->op_ukernel, |
| 552 |
.vmul_ukernel = attention_op->attention.vmul_config->op_ukernel, |
| 553 |
.vmulc_ukernel = attention_op->attention.vmul_config->opc_ukernel, |
| 554 |
.vtanh_ukernel = attention_op->attention.vtanh_config->ukernel, |
| 555 |
}; |
| 556 |
|
| 557 |
if (attention_op->attention.cap_type == xnn_attention_logits_cap_type_tanh) { |
| 558 |
attention_op->context.gemm.gemm.attention.logits_cap.type = xnn_attention_logits_cap_type_tanh; |
| 559 |
memcpy(&attention_op->context.gemm.gemm.attention.logits_cap.cap, cap, cap_size); |
| 560 |
memcpy(&attention_op->context.gemm.gemm.attention.logits_cap.cap_reciprocal, cap_reciprocal, cap_size); |
| 561 |
} |
| 562 |
|
| 563 |
#if XNN_MAX_UARCH_TYPES > 1 |
| 564 |
if (xnn_is_hmp_gemm_ukernel(gemm_ukernel)) { |
| 565 |
if (use_threads_workspace_size) { |
| 566 |
attention_op->compute[2].type = xnn_parallelization_type_3d_tile_1d_with_uarch_with_thread; |
| 567 |
attention_op->compute[2].task_3d_tile_1d_with_id_with_thread = |
| 568 |
(pthreadpool_task_3d_tile_1d_with_id_with_thread_t) xnn_compute_hmp_scaled_dot_product_attention_with_thread; |
| 569 |
} else { |
| 570 |
attention_op->compute[2].type = xnn_parallelization_type_3d_tile_1d_with_uarch; |
| 571 |
attention_op->compute[2].task_3d_tile_1d_with_id = |
| 572 |
(pthreadpool_task_3d_tile_1d_with_id_t) xnn_compute_hmp_scaled_dot_product_attention; |
| 573 |
} |
| 574 |
} else { |
| 575 |
if (use_threads_workspace_size) { |
| 576 |
attention_op->compute[2].type = xnn_parallelization_type_3d_tile_1d_with_thread; |
| 577 |
attention_op->compute[2].task_3d_tile_1d_with_thread = |
| 578 |
(pthreadpool_task_3d_tile_1d_with_thread_t) xnn_compute_scaled_dot_product_attention_with_thread; |
| 579 |
} else { |
| 580 |
attention_op->compute[2].type = xnn_parallelization_type_3d_tile_1d; |
| 581 |
attention_op->compute[2].task_3d_tile_1d = |
| 582 |
(pthreadpool_task_3d_tile_1d_t) xnn_compute_scaled_dot_product_attention; |
| 583 |
} |
| 584 |
} |
| 585 |
#else |
| 586 |
if (use_threads_workspace_size) { |
| 587 |
attention_op->compute[2].type = xnn_parallelization_type_3d_tile_1d_with_thread; |
| 588 |
attention_op->compute[2].task_3d_tile_1d_with_thread = |
| 589 |
(pthreadpool_task_3d_tile_1d_with_thread_t) xnn_compute_scaled_dot_product_attention_with_thread; |
| 590 |
} else { |
| 591 |
attention_op->compute[2].type = xnn_parallelization_type_3d_tile_1d; |
| 592 |
attention_op->compute[2].task_3d_tile_1d = |
| 593 |
(pthreadpool_task_3d_tile_1d_t) xnn_compute_scaled_dot_product_attention; |
| 594 |
} |
| 595 |
#endif |
| 596 |
|
| 597 |
attention_op->compute[2].range[0] = batch_size; |
| 598 |
attention_op->compute[2].range[1] = query_heads; |
| 599 |
attention_op->compute[2].range[2] = query_tokens; |
| 600 |
attention_op->compute[2].tile[0] = mr; |
| 601 |
|
| 602 |
attention_op->context.gemm.gemm.attention.scaled_query_offset = 0; |
| 603 |
attention_op->context.gemm.gemm.attention.packed_k_offset = scaled_query_size; |
| 604 |
attention_op->context.gemm.gemm.attention.packed_v_offset = scaled_query_size + packed_key_size; |
| 605 |
attention_op->context.gemm.gemm.attention.logits_offset = scaled_query_size + packed_key_size + packed_value_size; |
| 606 |
|
| 607 |
memcpy(&attention_op->context.gemm.gemm.attention.minmax_params, minmax_params, minmax_params_size); |
| 608 |
memcpy(&attention_op->context.gemm.gemm.attention.expminus_params, expminus_params, expminus_params_size); |
| 609 |
memcpy(&attention_op->context.gemm.gemm.attention.rmax_params, rmax_params, rmax_params_size); |
| 610 |
memcpy(&attention_op->context.gemm.gemm.attention.tanh_params, tanh_params, tanh_params_size); |
| 611 |
|
| 612 |
attention_op->state = xnn_run_state_needs_setup; |
| 613 |
|
| 614 |
return xnn_status_success; |
| 615 |
|
| 616 |
} |
| 617 |
|
| 618 |
enum xnn_status xnn_reshape_scaled_dot_product_attention_nhtc_f16( |
| 619 |
xnn_operator_t attention_op, |
| 620 |
size_t batch_size, |
| 621 |
size_t heads, |
| 622 |
size_t query_tokens, |
| 623 |
size_t key_value_heads, |
| 624 |
size_t key_value_tokens, |
| 625 |
size_t query_key_channels, |
| 626 |
size_t value_channels, |
| 627 |
size_t* workspace_size, |
| 628 |
size_t* workspace_alignment, |
| 629 |
pthreadpool_t threadpool) |
| 630 |
{ |
| 631 |
xnn_float16 cap = xnn_float16_from_float(attention_op->attention.cap_params.cap); |
| 632 |
xnn_float16 cap_reciprocal = xnn_float16_from_float(1.0f / attention_op->attention.cap_params.cap); |
| 633 |
|
| 634 |
return reshape_scaled_dot_product_attention_nhtc( |
| 635 |
attention_op, |
| 636 |
xnn_operator_type_scaled_dot_product_attention_nhtc_f16, |
| 637 |
batch_size, |
| 638 |
heads, |
| 639 |
query_tokens, |
| 640 |
key_value_heads, |
| 641 |
key_value_tokens, |
| 642 |
query_key_channels, |
| 643 |
value_channels, |
| 644 |
workspace_size, workspace_alignment, |
| 645 |
XNN_LOG2_SIZEOF_UINT16_T, |
| 646 |
sizeof(uint16_t), |
| 647 |
(xnn_compute_reciprocal_fn) compute_reciprocal_f16, |
| 648 |
&cap, &cap_reciprocal, sizeof(uint16_t), |
| 649 |
&attention_op->params.f16_minmax, sizeof(attention_op->params.f16_minmax), |
| 650 |
&attention_op->params2.f16_default, sizeof(attention_op->params2.f16_default), |
| 651 |
&attention_op->params3.f16_rmax, sizeof(attention_op->params3.f16_rmax), |
| 652 |
&attention_op->params4.unary, sizeof(attention_op->params4.unary), |
| 653 |
threadpool); |
| 654 |
} |
| 655 |
|
| 656 |
enum xnn_status xnn_reshape_scaled_dot_product_attention_nhtc_f32( |
| 657 |
xnn_operator_t attention_op, |
| 658 |
size_t batch_size, |
| 659 |
size_t query_heads, |
| 660 |
size_t query_tokens, |
| 661 |
size_t key_value_heads, |
| 662 |
size_t key_value_tokens, |
| 663 |
size_t query_key_channels, |
| 664 |
size_t value_channels, |
| 665 |
size_t* workspace_size, |
| 666 |
size_t* workspace_alignment, |
| 667 |
pthreadpool_t threadpool) |
| 668 |
{ |
| 669 |
float cap = attention_op->attention.cap_params.cap; |
| 670 |
float cap_reciprocal = 1 / attention_op->attention.cap_params.cap; |
| 671 |
|
| 672 |
return reshape_scaled_dot_product_attention_nhtc( |
| 673 |
attention_op, |
| 674 |
xnn_operator_type_scaled_dot_product_attention_nhtc_f32, |
| 675 |
batch_size, |
| 676 |
query_heads, |
| 677 |
query_tokens, |
| 678 |
key_value_heads, |
| 679 |
key_value_tokens, |
| 680 |
query_key_channels, |
| 681 |
value_channels, |
| 682 |
workspace_size, workspace_alignment, |
| 683 |
XNN_LOG2_SIZEOF_FLOAT, |
| 684 |
sizeof(float), |
| 685 |
(xnn_compute_reciprocal_fn) compute_reciprocal_f32, |
| 686 |
&cap, &cap_reciprocal, sizeof(float), |
| 687 |
&attention_op->params.f32_minmax, sizeof(attention_op->params.f32_minmax), |
| 688 |
&attention_op->params2.f32_default, sizeof(attention_op->params2.f32_default), |
| 689 |
&attention_op->params3.f32_rmax, sizeof(attention_op->params3.f32_rmax), |
| 690 |
&attention_op->params4.unary, sizeof(attention_op->params4.unary), |
| 691 |
threadpool); |
| 692 |
} |
| 693 |
|
| 694 |
static enum xnn_status setup_scaled_dot_product_attention_nhtc( |
| 695 |
xnn_operator_t attention_op, |
| 696 |
enum xnn_operator_type expected_operator_type, |
| 697 |
void* workspace, |
| 698 |
const float* query, |
| 699 |
const float* key, |
| 700 |
const float* value, |
| 701 |
const float* scale, |
| 702 |
const float* mask, |
| 703 |
float* output) |
| 704 |
{ |
| 705 |
if (attention_op->type != expected_operator_type) { |
| 706 |
xnn_log_error( |
| 707 |
"failed to setup operator: operator type mismatch (expected %s, got %s)", |
| 708 |
xnn_operator_type_to_string(expected_operator_type), |
| 709 |
xnn_operator_type_to_string(attention_op->type)); |
| 710 |
return xnn_status_invalid_parameter; |
| 711 |
} |
| 712 |
|
| 713 |
switch (attention_op->state) { |
| 714 |
case xnn_run_state_skip: |
| 715 |
return xnn_status_success; |
| 716 |
case xnn_run_state_invalid: |
| 717 |
xnn_log_error( |
| 718 |
"failed to setup %s operator: operator has not been reshaped yet", |
| 719 |
xnn_operator_type_to_string(attention_op->type)); |
| 720 |
return xnn_status_invalid_state; |
| 721 |
case xnn_run_state_needs_setup: |
| 722 |
|
| 723 |
case xnn_run_state_ready: |
| 724 |
|
| 725 |
break; |
| 726 |
} |
| 727 |
|
| 728 |
attention_op->context.gemm.packw_gemm_goi.kernel = key; |
| 729 |
attention_op->context.gemm.packw_gemm_goi.packed_weights = |
| 730 |
(void*) ((uintptr_t) workspace + attention_op->context.gemm.gemm.attention.packed_k_offset); |
| 731 |
attention_op->context.gemm.packw_gemm_goi.bias = NULL; |
| 732 |
|
| 733 |
attention_op->context.gemm.packw_gemm_gio.kernel = value; |
| 734 |
attention_op->context.gemm.packw_gemm_gio.packed_weights = |
| 735 |
(void*) ((uintptr_t) workspace + attention_op->context.gemm.gemm.attention.packed_v_offset); |
| 736 |
attention_op->context.gemm.packw_gemm_gio.bias = NULL; |
| 737 |
|
| 738 |
attention_op->context.gemm.gemm.attention.scaled_query = |
| 739 |
(void*) ((uintptr_t) workspace + attention_op->context.gemm.gemm.attention.scaled_query_offset); |
| 740 |
attention_op->context.gemm.gemm.attention.logits_buffer = |
| 741 |
(void*) ((uintptr_t) workspace + attention_op->context.gemm.gemm.attention.logits_offset); |
| 742 |
attention_op->context.gemm.gemm.attention.query = query; |
| 743 |
attention_op->context.gemm.gemm.attention.key = attention_op->context.gemm.packw_gemm_goi.packed_weights; |
| 744 |
attention_op->context.gemm.gemm.attention.value = attention_op->context.gemm.packw_gemm_gio.packed_weights; |
| 745 |
attention_op->context.gemm.gemm.attention.scale = scale; |
| 746 |
attention_op->context.gemm.gemm.attention.mask = mask; |
| 747 |
attention_op->context.gemm.gemm.attention.output = output; |
| 748 |
attention_op->state = xnn_run_state_ready; |
| 749 |
|
| 750 |
return xnn_status_success; |
| 751 |
} |
| 752 |
|
| 753 |
enum xnn_status xnn_setup_scaled_dot_product_attention_nhtc_f16( |
| 754 |
xnn_operator_t attention_op, |
| 755 |
void* workspace, |
| 756 |
const void* query, |
| 757 |
const void* key, |
| 758 |
const void* value, |
| 759 |
const void* scale, |
| 760 |
const void* mask, |
| 761 |
void* output) |
| 762 |
{ |
| 763 |
return setup_scaled_dot_product_attention_nhtc( |
| 764 |
attention_op, xnn_operator_type_scaled_dot_product_attention_nhtc_f16, |
| 765 |
workspace, |
| 766 |
query, key, value, |
| 767 |
scale, mask, |
| 768 |
output); |
| 769 |
} |
| 770 |
|
| 771 |
enum xnn_status xnn_setup_scaled_dot_product_attention_nhtc_f32( |
| 772 |
xnn_operator_t attention_op, |
| 773 |
void* workspace, |
| 774 |
const float* query, |
| 775 |
const float* key, |
| 776 |
const float* value, |
| 777 |
const float* scale, |
| 778 |
const float* mask, |
| 779 |
float* output) |
| 780 |
{ |
| 781 |
return setup_scaled_dot_product_attention_nhtc( |
| 782 |
attention_op, xnn_operator_type_scaled_dot_product_attention_nhtc_f32, |
| 783 |
workspace, |
| 784 |
query, key, value, |
| 785 |
scale, mask, |
| 786 |
output); |
| 787 |
} |