Bug Summary

File:nnc/cmd/scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c
Warning:line 283, column 28
Array access (from variable 'amp2') results in a null pointer dereference

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name ccv_nnc_scaled_dot_product_attention_cpu_ref.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -target-feature +sse2 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/liu/actions-runner/_work/ccv/ccv/lib/nnc/cmd -fcoverage-compilation-dir=/home/liu/actions-runner/_work/ccv/ccv/lib/nnc/cmd -resource-dir /usr/local/lib/clang/19 -I ../../ -I .. -I /usr/local/cuda/include -D HAVE_CBLAS -D HAVE_LIBPNG -D HAVE_LIBJPEG -D HAVE_FFTW3 -D HAVE_PTHREAD -D HAVE_LIBLINEAR -D HAVE_TESSERACT -D HAVE_AVCODEC -D HAVE_AVFORMAT -D HAVE_AVUTIL -D HAVE_SWSCALE -D HAVE_SSE2 -D HAVE_GSL -D HAVE_CUDA -D HAVE_CUDNN -D HAVE_NCCL -D USE_SYSTEM_CUB -I /usr/local/include -internal-isystem /usr/local/lib/clang/19/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/liu/actions-runner/_work/ccv/ccv/_analyze/2026-11-27-161249-4069681-1 -x c scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c
1#include "ccv.h"
2#include "ccv_internal.h"
3#include "nnc/ccv_nnc.h"
4#include "nnc/ccv_nnc_easy.h"
5#include "nnc/ccv_nnc_internal.h"
6#ifdef USE_OPENMP
7#include <omp.h>
8#endif
9#ifdef USE_DISPATCH
10#include <dispatch/dispatch.h>
11#endif
12
13// Shared methods.
14#include "../_ccv_nnc_cpu_ref.h"
15
16static int _ccv_nnc_scaled_dot_product_attention_forw_cpu_ref(const ccv_nnc_cmd_t cmd, const ccv_nnc_hint_t hint, const int flags, ccv_nnc_tensor_t* const* const inputs, const int input_size, ccv_nnc_tensor_t* const* const outputs, const int output_size, ccv_nnc_stream_context_t* const stream_context)
17{
18 assert(input_size >= 3)((void) sizeof ((input_size >= 3) ? 1 : 0), __extension__ (
{ if (input_size >= 3) ; else __assert_fail ("input_size >= 3"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 18, __extension__ __PRETTY_FUNCTION__); }))
;
1
Assuming 'input_size' is >= 3
2
Taking true branch
19 assert(output_size >= 1)((void) sizeof ((output_size >= 1) ? 1 : 0), __extension__
({ if (output_size >= 1) ; else __assert_fail ("output_size >= 1"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 19, __extension__ __PRETTY_FUNCTION__); }))
;
3
Assuming 'output_size' is >= 1
4
Taking true branch
20 const int is_varlen = cmd.info.scaled_dot_product_attention.is_varlen;
21 ccv_nnc_tensor_view_t* const q = (ccv_nnc_tensor_view_t*)inputs[0];
22 ccv_nnc_tensor_view_t* const k = (ccv_nnc_tensor_view_t*)inputs[1];
23 ccv_nnc_tensor_view_t* const v = (ccv_nnc_tensor_view_t*)inputs[2];
24 ccv_nnc_tensor_view_t* const attn_mask = input_size > 3 ? (ccv_nnc_tensor_view_t*)inputs[3] : 0;
5
Assuming 'input_size' is > 3
6
'?' condition is true
25 ccv_nnc_tensor_view_t* const w = input_size > 4 ? (ccv_nnc_tensor_view_t*)inputs[4] : 0;
7
Assuming 'input_size' is > 4
8
'?' condition is true
26 ccv_nnc_tensor_view_t* const bias = input_size > 5 ? (ccv_nnc_tensor_view_t*)inputs[5] : 0;
9
Assuming 'input_size' is > 5
10
'?' condition is true
27 ccv_nnc_tensor_view_t* const q_seq_offsets = is_varlen && input_size > 6 ? (ccv_nnc_tensor_view_t*)inputs[6] : 0;
11
Assuming 'is_varlen' is 0
28 ccv_nnc_tensor_view_t* const kv_seq_offsets = is_varlen
11.1
'is_varlen' is 0
&& input_size > 7 ? (ccv_nnc_tensor_view_t*)inputs[7] : 0;
29 const int attention_sinks = cmd.info.scaled_dot_product_attention.attention_sinks;
30 const int sliding_window = cmd.info.scaled_dot_product_attention.sliding_window;
31 ccv_nnc_tensor_view_t* const sinks = attention_sinks && input_size > 8 ? (ccv_nnc_tensor_view_t*)inputs[8] : 0;
12
Assuming 'attention_sinks' is not equal to 0
13
Assuming 'input_size' is > 8
14
'?' condition is true
32 // [batch, R, Hq, extent]: a query's own key is at column row + C - R, and the key d positions
33 // before it, for 0 <= d < extent, adds the query's bias at d to its score.
34 ccv_nnc_tensor_view_t* const relative_bias = cmd.info.scaled_dot_product_attention.relative_bias && input_size > 9 ? (ccv_nnc_tensor_view_t*)inputs[9] : 0;
15
Assuming field 'relative_bias' is not equal to 0
16
Assuming 'input_size' is > 9
17
'?' condition is true
35 if (cmd.info.scaled_dot_product_attention.relative_bias
17.1
Field 'relative_bias' is not equal to 0
&& (!relative_bias || is_varlen
18.1
'is_varlen' is 0
))
18
Assuming 'relative_bias' is non-null
19
Taking false branch
36 return CCV_NNC_EXEC_INVALID;
37 if (bias) // bias always requires a weight matrix.
20
Assuming 'bias' is null
38 { assert(w)((void) sizeof ((w) ? 1 : 0), __extension__ ({ if (w) ; else __assert_fail
("w", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 38, __extension__ __PRETTY_FUNCTION__); }))
; }
39 if (sliding_window < 0 || (sliding_window > 0 && (!cmd.info.scaled_dot_product_attention.is_causal || is_varlen)))
21
Assuming 'sliding_window' is >= 0
22
Assuming 'sliding_window' is <= 0
40 return CCV_NNC_EXEC_INVALID;
41 if (is_varlen
22.1
'is_varlen' is 0
&& (attn_mask || w || bias || !q_seq_offsets || !kv_seq_offsets))
42 return CCV_NNC_EXEC_INVALID;
43 if (attention_sinks
22.2
'attention_sinks' is not equal to 0
&& !sinks)
23
Assuming 'sinks' is non-null
44 return CCV_NNC_EXEC_INVALID;
45 ccv_nnc_tensor_view_t* const c = (w) ? (ccv_nnc_tensor_view_t*)outputs[2] : (ccv_nnc_tensor_view_t*)outputs[0];
24
Taking false branch
25
Assuming 'w' is non-null
26
'?' condition is true
46 const int q_nd = ccv_nnc_tensor_nd(q->info.dim);
47 assert(q_nd == 3 || q_nd == 4)((void) sizeof ((q_nd == 3 || q_nd == 4) ? 1 : 0), __extension__
({ if (q_nd == 3 || q_nd == 4) ; else __assert_fail ("q_nd == 3 || q_nd == 4"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 47, __extension__ __PRETTY_FUNCTION__); }))
;
27
Assuming 'q_nd' is not equal to 3
28
Assuming 'q_nd' is equal to 4
29
Taking true branch
48 const int k_nd = ccv_nnc_tensor_nd(k->info.dim);
49 assert(k_nd == 3 || k_nd == 4)((void) sizeof ((k_nd == 3 || k_nd == 4) ? 1 : 0), __extension__
({ if (k_nd == 3 || k_nd == 4) ; else __assert_fail ("k_nd == 3 || k_nd == 4"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 49, __extension__ __PRETTY_FUNCTION__); }))
;
30
Assuming 'k_nd' is not equal to 3
31
Assuming 'k_nd' is equal to 4
32
Taking true branch
50 const int v_nd = ccv_nnc_tensor_nd(v->info.dim);
51 assert(v_nd == 3 || v_nd == 4)((void) sizeof ((v_nd == 3 || v_nd == 4) ? 1 : 0), __extension__
({ if (v_nd == 3 || v_nd == 4) ; else __assert_fail ("v_nd == 3 || v_nd == 4"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 51, __extension__ __PRETTY_FUNCTION__); }))
;
33
Assuming 'v_nd' is not equal to 3
34
Assuming 'v_nd' is equal to 4
35
Taking true branch
52 const int c_nd = ccv_nnc_tensor_nd(c->info.dim);
53 assert(c_nd == 3 || c_nd == 4)((void) sizeof ((c_nd == 3 || c_nd == 4) ? 1 : 0), __extension__
({ if (c_nd == 3 || c_nd == 4) ; else __assert_fail ("c_nd == 3 || c_nd == 4"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 53, __extension__ __PRETTY_FUNCTION__); }))
;
36
Assuming 'c_nd' is not equal to 3
37
Assuming 'c_nd' is equal to 4
38
Taking true branch
54 assert(q_nd == k_nd && k_nd == v_nd && v_nd == c_nd)((void) sizeof ((q_nd == k_nd && k_nd == v_nd &&
v_nd == c_nd) ? 1 : 0), __extension__ ({ if (q_nd == k_nd &&
k_nd == v_nd && v_nd == c_nd) ; else __assert_fail (
"q_nd == k_nd && k_nd == v_nd && v_nd == c_nd"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 54, __extension__ __PRETTY_FUNCTION__); }))
;
39
Taking true branch
55 if (is_varlen
39.1
'is_varlen' is 0
&& q_nd != 4)
56 return CCV_NNC_EXEC_INVALID;
57 // Assuming this is float 32.
58 int qdim[CCV_NNC_MAX_DIM_ALLOC(12)];
59 int kdim[CCV_NNC_MAX_DIM_ALLOC(12)];
60 int vdim[CCV_NNC_MAX_DIM_ALLOC(12)];
61 int cdim[CCV_NNC_MAX_DIM_ALLOC(12)];
62 int amdim[CCV_NNC_MAX_DIM_ALLOC(12)];
63 ccv_nnc_tensor_view_get_dim(q, qdim);
64 ccv_nnc_tensor_view_get_dim(k, kdim);
65 ccv_nnc_tensor_view_get_dim(v, vdim);
66 ccv_nnc_tensor_view_get_dim(c, cdim);
67 if (is_varlen
39.2
'is_varlen' is 0
)
40
Taking false branch
68 {
69 assert(q_seq_offsets->info.datatype == CCV_32S)((void) sizeof ((q_seq_offsets->info.datatype == CCV_32S) ?
1 : 0), __extension__ ({ if (q_seq_offsets->info.datatype
== CCV_32S) ; else __assert_fail ("q_seq_offsets->info.datatype == CCV_32S"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 69, __extension__ __PRETTY_FUNCTION__); }))
;
70 assert(kv_seq_offsets->info.datatype == CCV_32S)((void) sizeof ((kv_seq_offsets->info.datatype == CCV_32S)
? 1 : 0), __extension__ ({ if (kv_seq_offsets->info.datatype
== CCV_32S) ; else __assert_fail ("kv_seq_offsets->info.datatype == CCV_32S"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 70, __extension__ __PRETTY_FUNCTION__); }))
;
71 assert(CCV_IS_TENSOR_CONTIGUOUS(q_seq_offsets))((void) sizeof (((!((*(int*)(q_seq_offsets)) & CCV_TENSOR_VIEW
) || (((ccv_nnc_tensor_view_t*)q_seq_offsets)->contiguous ==
1))) ? 1 : 0), __extension__ ({ if ((!((*(int*)(q_seq_offsets
)) & CCV_TENSOR_VIEW) || (((ccv_nnc_tensor_view_t*)q_seq_offsets
)->contiguous == 1))) ; else __assert_fail ("CCV_IS_TENSOR_CONTIGUOUS(q_seq_offsets)"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 71, __extension__ __PRETTY_FUNCTION__); }))
;
72 assert(CCV_IS_TENSOR_CONTIGUOUS(kv_seq_offsets))((void) sizeof (((!((*(int*)(kv_seq_offsets)) & CCV_TENSOR_VIEW
) || (((ccv_nnc_tensor_view_t*)kv_seq_offsets)->contiguous
== 1))) ? 1 : 0), __extension__ ({ if ((!((*(int*)(kv_seq_offsets
)) & CCV_TENSOR_VIEW) || (((ccv_nnc_tensor_view_t*)kv_seq_offsets
)->contiguous == 1))) ; else __assert_fail ("CCV_IS_TENSOR_CONTIGUOUS(kv_seq_offsets)"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 72, __extension__ __PRETTY_FUNCTION__); }))
;
73 }
74 if (q_nd
40.1
'q_nd' is not equal to 3
== 3)
41
Taking false branch
75 {
76 qdim[0] = qdim[1], qdim[1] = qdim[2], qdim[2] = 1;
77 kdim[0] = kdim[1], kdim[1] = kdim[2], kdim[2] = 1;
78 vdim[0] = vdim[1], vdim[1] = vdim[2], vdim[2] = 1;
79 cdim[0] = cdim[1], cdim[1] = cdim[2], cdim[2] = 1;
80 }
81 assert(qdim[0] == kdim[0] && kdim[0] == vdim[0] && vdim[0] == cdim[0])((void) sizeof ((qdim[0] == kdim[0] && kdim[0] == vdim
[0] && vdim[0] == cdim[0]) ? 1 : 0), __extension__ ({
if (qdim[0] == kdim[0] && kdim[0] == vdim[0] &&
vdim[0] == cdim[0]) ; else __assert_fail ("qdim[0] == kdim[0] && kdim[0] == vdim[0] && vdim[0] == cdim[0]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 81, __extension__ __PRETTY_FUNCTION__); }))
;
42
Assuming the condition is true
43
Assuming the condition is true
44
Assuming the condition is true
45
Taking true branch
82 assert(qdim[2] == cdim[2])((void) sizeof ((qdim[2] == cdim[2]) ? 1 : 0), __extension__ (
{ if (qdim[2] == cdim[2]) ; else __assert_fail ("qdim[2] == cdim[2]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 82, __extension__ __PRETTY_FUNCTION__); }))
;
46
Assuming the condition is true
47
Taking true branch
83 assert(kdim[2] == vdim[2])((void) sizeof ((kdim[2] == vdim[2]) ? 1 : 0), __extension__ (
{ if (kdim[2] == vdim[2]) ; else __assert_fail ("kdim[2] == vdim[2]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 83, __extension__ __PRETTY_FUNCTION__); }))
;
48
Assuming the condition is true
49
Taking true branch
84 assert(qdim[2] % kdim[2] == 0)((void) sizeof ((qdim[2] % kdim[2] == 0) ? 1 : 0), __extension__
({ if (qdim[2] % kdim[2] == 0) ; else __assert_fail ("qdim[2] % kdim[2] == 0"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 84, __extension__ __PRETTY_FUNCTION__); }))
;
50
Assuming the condition is true
51
Taking true branch
85 assert(qdim[2] >= kdim[2])((void) sizeof ((qdim[2] >= kdim[2]) ? 1 : 0), __extension__
({ if (qdim[2] >= kdim[2]) ; else __assert_fail ("qdim[2] >= kdim[2]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 85, __extension__ __PRETTY_FUNCTION__); }))
;
52
Assuming the condition is true
53
Taking true branch
86 assert(qdim[3] == kdim[3])((void) sizeof ((qdim[3] == kdim[3]) ? 1 : 0), __extension__ (
{ if (qdim[3] == kdim[3]) ; else __assert_fail ("qdim[3] == kdim[3]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 86, __extension__ __PRETTY_FUNCTION__); }))
;
54
Assuming the condition is true
55
Taking true branch
87 assert(kdim[1] == vdim[1])((void) sizeof ((kdim[1] == vdim[1]) ? 1 : 0), __extension__ (
{ if (kdim[1] == vdim[1]) ; else __assert_fail ("kdim[1] == vdim[1]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 87, __extension__ __PRETTY_FUNCTION__); }))
;
56
Assuming the condition is true
57
Taking true branch
88 assert(cdim[1] == qdim[1])((void) sizeof ((cdim[1] == qdim[1]) ? 1 : 0), __extension__ (
{ if (cdim[1] == qdim[1]) ; else __assert_fail ("cdim[1] == qdim[1]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 88, __extension__ __PRETTY_FUNCTION__); }))
;
58
Assuming the condition is true
59
Taking true branch
89 assert(cdim[3] == vdim[3])((void) sizeof ((cdim[3] == vdim[3]) ? 1 : 0), __extension__ (
{ if (cdim[3] == vdim[3]) ; else __assert_fail ("cdim[3] == vdim[3]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 89, __extension__ __PRETTY_FUNCTION__); }))
;
60
Assuming the condition is true
61
Taking true branch
90 const float* const sinkp = sinks
61.1
'sinks' is non-null
? sinks->data.f32 : 0;
62
'?' condition is true
91 int sink_head_stride = 0;
92 int sink_query_stride = 0;
93 if (attention_sinks
62.1
'attention_sinks' is not equal to 0
)
63
Taking true branch
94 {
95 assert(CCV_IS_TENSOR_CONTIGUOUS(sinks))((void) sizeof (((!((*(int*)(sinks)) & CCV_TENSOR_VIEW) ||
(((ccv_nnc_tensor_view_t*)sinks)->contiguous == 1))) ? 1 :
0), __extension__ ({ if ((!((*(int*)(sinks)) & CCV_TENSOR_VIEW
) || (((ccv_nnc_tensor_view_t*)sinks)->contiguous == 1))) ;
else __assert_fail ("CCV_IS_TENSOR_CONTIGUOUS(sinks)", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 95, __extension__ __PRETTY_FUNCTION__); }))
;
64
Assuming the condition is true
96 const int sink_count = ccv_nnc_tensor_count(sinks->info);
97 assert(sink_count == 1 || sink_count == qdim[2] || (q_nd == 4 && sink_count == qdim[0] * qdim[1] * qdim[2]))((void) sizeof ((sink_count == 1 || sink_count == qdim[2] || (
q_nd == 4 && sink_count == qdim[0] * qdim[1] * qdim[2
])) ? 1 : 0), __extension__ ({ if (sink_count == 1 || sink_count
== qdim[2] || (q_nd == 4 && sink_count == qdim[0] * qdim
[1] * qdim[2])) ; else __assert_fail ("sink_count == 1 || sink_count == qdim[2] || (q_nd == 4 && sink_count == qdim[0] * qdim[1] * qdim[2])"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 97, __extension__ __PRETTY_FUNCTION__); }))
;
65
Assuming 'sink_count' is equal to 1
98 sink_head_stride = (sink_count
65.1
'sink_count' is equal to 1
== 1) ? 0 : 1;
66
'?' condition is true
99 // One sink for each query and head, [batch, R, Hq].
100 sink_query_stride = (sink_count
66.1
'sink_count' is equal to 1
== 1 || sink_count == qdim[2]) ? 0 : qdim[2];
101 if (is_varlen
66.2
'is_varlen' is 0
&& sink_query_stride)
102 return CCV_NNC_EXEC_INVALID;
103 }
104 const float* const relative_biasp = relative_bias
66.3
'relative_bias' is non-null
? relative_bias->data.f32 : 0;
67
'?' condition is true
105 int relative_extent = 0;
106 if (relative_bias
67.1
'relative_bias' is non-null
)
68
Taking true branch
107 {
108 assert(q_nd == 4)((void) sizeof ((q_nd == 4) ? 1 : 0), __extension__ ({ if (q_nd
== 4) ; else __assert_fail ("q_nd == 4", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 108, __extension__ __PRETTY_FUNCTION__); }))
;
69
Taking true branch
109 assert(CCV_IS_TENSOR_CONTIGUOUS(relative_bias))((void) sizeof (((!((*(int*)(relative_bias)) & CCV_TENSOR_VIEW
) || (((ccv_nnc_tensor_view_t*)relative_bias)->contiguous ==
1))) ? 1 : 0), __extension__ ({ if ((!((*(int*)(relative_bias
)) & CCV_TENSOR_VIEW) || (((ccv_nnc_tensor_view_t*)relative_bias
)->contiguous == 1))) ; else __assert_fail ("CCV_IS_TENSOR_CONTIGUOUS(relative_bias)"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 109, __extension__ __PRETTY_FUNCTION__); }))
;
70
Assuming the condition is false
71
Assuming field 'contiguous' is equal to 1
72
Taking true branch
110 relative_extent = ccv_nnc_tensor_count(relative_bias->info) / (qdim[0] * qdim[1] * qdim[2]);
111 assert(relative_extent > 0 && ccv_nnc_tensor_count(relative_bias->info) == (size_t)qdim[0] * qdim[1] * qdim[2] * relative_extent)((void) sizeof ((relative_extent > 0 && ccv_nnc_tensor_count
(relative_bias->info) == (size_t)qdim[0] * qdim[1] * qdim[
2] * relative_extent) ? 1 : 0), __extension__ ({ if (relative_extent
> 0 && ccv_nnc_tensor_count(relative_bias->info
) == (size_t)qdim[0] * qdim[1] * qdim[2] * relative_extent) ;
else __assert_fail ("relative_extent > 0 && ccv_nnc_tensor_count(relative_bias->info) == (size_t)qdim[0] * qdim[1] * qdim[2] * relative_extent"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 111, __extension__ __PRETTY_FUNCTION__); }))
;
73
Assuming 'relative_extent' is > 0
74
Assuming the condition is true
75
Taking true branch
112 }
113 assert(CCV_NNC_MAX_DIM == 2)((void) sizeof (((2) == 2) ? 1 : 0), __extension__ ({ if ((2)
== 2) ; else __assert_fail ("CCV_NNC_MAX_DIM == 2", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 113, __extension__ __PRETTY_FUNCTION__); }))
; // Need to change this logic for CCV_NNC_MAX_DIM == other number.
76
Taking true branch
114 int qstride[CCV_NNC_MAX_DIM_ALLOC(12)];
115 int kstride[CCV_NNC_MAX_DIM_ALLOC(12)];
116 int vstride[CCV_NNC_MAX_DIM_ALLOC(12)];
117 int cstride[CCV_NNC_MAX_DIM_ALLOC(12)];
118 int amstride[CCV_NNC_MAX_DIM_ALLOC(12)];
119 ccv_nnc_tensor_view_get_stride(q, qstride);
120 ccv_nnc_tensor_view_get_stride(k, kstride);
121 ccv_nnc_tensor_view_get_stride(v, vstride);
122 ccv_nnc_tensor_view_get_stride(c, cstride);
123 if (q_nd
76.1
'q_nd' is not equal to 3
== 3)
77
Taking false branch
124 {
125 qstride[0] = qstride[1], qstride[1] = qstride[2], qstride[2] = qstride[3];
126 kstride[0] = kstride[1], kstride[1] = kstride[2], kstride[2] = kstride[3];
127 vstride[0] = vstride[1], vstride[1] = vstride[2], vstride[2] = vstride[3];
128 cstride[0] = cstride[1], cstride[1] = cstride[2], cstride[2] = cstride[3];
129 }
130 if (attn_mask)
78
Assuming 'attn_mask' is non-null
79
Taking true branch
131 {
132 ccv_nnc_tensor_view_get_dim(attn_mask, amdim);
133 ccv_nnc_tensor_view_get_stride(attn_mask, amstride);
134 assert(amdim[0] == qdim[0] || amdim[0] == 1)((void) sizeof ((amdim[0] == qdim[0] || amdim[0] == 1) ? 1 : 0
), __extension__ ({ if (amdim[0] == qdim[0] || amdim[0] == 1)
; else __assert_fail ("amdim[0] == qdim[0] || amdim[0] == 1"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 134, __extension__ __PRETTY_FUNCTION__); }))
;
80
Assuming the condition is true
135 assert(amdim[1] == qdim[2] || amdim[1] == 1)((void) sizeof ((amdim[1] == qdim[2] || amdim[1] == 1) ? 1 : 0
), __extension__ ({ if (amdim[1] == qdim[2] || amdim[1] == 1)
; else __assert_fail ("amdim[1] == qdim[2] || amdim[1] == 1"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 135, __extension__ __PRETTY_FUNCTION__); }))
;
81
Assuming the condition is true
136 assert(amdim[2] == qdim[1])((void) sizeof ((amdim[2] == qdim[1]) ? 1 : 0), __extension__
({ if (amdim[2] == qdim[1]) ; else __assert_fail ("amdim[2] == qdim[1]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 136, __extension__ __PRETTY_FUNCTION__); }))
;
82
Assuming the condition is true
83
Taking true branch
137 assert(amdim[3] == kdim[1])((void) sizeof ((amdim[3] == kdim[1]) ? 1 : 0), __extension__
({ if (amdim[3] == kdim[1]) ; else __assert_fail ("amdim[3] == kdim[1]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 137, __extension__ __PRETTY_FUNCTION__); }))
;
84
Assuming the condition is true
85
Taking true branch
138 }
139 int i[CCV_NNC_MAX_DIM(2) + 2];
140 float* qk = ccv_nnc_stream_context_get_workspace(stream_context, sizeof(float) * qdim[1] * kdim[1], CCV_TENSOR_CPU_MEMORY);
141 const float* const qp = q->data.f32;
142 const float* const kp = k->data.f32;
143 const float* const vp = v->data.f32;
144 const float* const amp = attn_mask
85.1
'attn_mask' is non-null
? attn_mask->data.f32 : 0;
86
'?' condition is true
145 float* const cp = c->data.f32;
146 const float scale = cmd.info.scaled_dot_product_attention.scale;
147 const int is_causal = cmd.info.scaled_dot_product_attention.is_causal;
148 const int h_h_k_ratio = qdim[2] / kdim[2];
149 assert(kdim[2] == vdim[2])((void) sizeof ((kdim[2] == vdim[2]) ? 1 : 0), __extension__ (
{ if (kdim[2] == vdim[2]) ; else __assert_fail ("kdim[2] == vdim[2]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 149, __extension__ __PRETTY_FUNCTION__); }))
;
87
Taking true branch
150 assert(qdim[2] >= kdim[2])((void) sizeof ((qdim[2] >= kdim[2]) ? 1 : 0), __extension__
({ if (qdim[2] >= kdim[2]) ; else __assert_fail ("qdim[2] >= kdim[2]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 150, __extension__ __PRETTY_FUNCTION__); }))
;
88
Taking true branch
151 assert(qdim[2] % kdim[2] == 0)((void) sizeof ((qdim[2] % kdim[2] == 0) ? 1 : 0), __extension__
({ if (qdim[2] % kdim[2] == 0) ; else __assert_fail ("qdim[2] % kdim[2] == 0"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 151, __extension__ __PRETTY_FUNCTION__); }))
;
89
Taking true branch
152 if (is_varlen
89.1
'is_varlen' is 0
)
90
Taking false branch
153 {
154 const int batch_size = ccv_nnc_tensor_count(q_seq_offsets->info) - 1;
155 assert(batch_size > 0)((void) sizeof ((batch_size > 0) ? 1 : 0), __extension__ (
{ if (batch_size > 0) ; else __assert_fail ("batch_size > 0"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 155, __extension__ __PRETTY_FUNCTION__); }))
;
156 assert(ccv_nnc_tensor_count(kv_seq_offsets->info) == batch_size + 1)((void) sizeof ((ccv_nnc_tensor_count(kv_seq_offsets->info
) == batch_size + 1) ? 1 : 0), __extension__ ({ if (ccv_nnc_tensor_count
(kv_seq_offsets->info) == batch_size + 1) ; else __assert_fail
("ccv_nnc_tensor_count(kv_seq_offsets->info) == batch_size + 1"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 156, __extension__ __PRETTY_FUNCTION__); }))
;
157 assert(qdim[0] == 1)((void) sizeof ((qdim[0] == 1) ? 1 : 0), __extension__ ({ if (
qdim[0] == 1) ; else __assert_fail ("qdim[0] == 1", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 157, __extension__ __PRETTY_FUNCTION__); }))
;
158 assert(kdim[0] == 1)((void) sizeof ((kdim[0] == 1) ? 1 : 0), __extension__ ({ if (
kdim[0] == 1) ; else __assert_fail ("kdim[0] == 1", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 158, __extension__ __PRETTY_FUNCTION__); }))
;
159 assert(vdim[0] == 1)((void) sizeof ((vdim[0] == 1) ? 1 : 0), __extension__ ({ if (
vdim[0] == 1) ; else __assert_fail ("vdim[0] == 1", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 159, __extension__ __PRETTY_FUNCTION__); }))
;
160 assert(cdim[0] == 1)((void) sizeof ((cdim[0] == 1) ? 1 : 0), __extension__ ({ if (
cdim[0] == 1) ; else __assert_fail ("cdim[0] == 1", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 160, __extension__ __PRETTY_FUNCTION__); }))
;
161 assert(cdim[1] == qdim[1])((void) sizeof ((cdim[1] == qdim[1]) ? 1 : 0), __extension__ (
{ if (cdim[1] == qdim[1]) ; else __assert_fail ("cdim[1] == qdim[1]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 161, __extension__ __PRETTY_FUNCTION__); }))
;
162 assert(cdim[2] == qdim[2])((void) sizeof ((cdim[2] == qdim[2]) ? 1 : 0), __extension__ (
{ if (cdim[2] == qdim[2]) ; else __assert_fail ("cdim[2] == qdim[2]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 162, __extension__ __PRETTY_FUNCTION__); }))
;
163 assert(cdim[3] == vdim[3])((void) sizeof ((cdim[3] == vdim[3]) ? 1 : 0), __extension__ (
{ if (cdim[3] == vdim[3]) ; else __assert_fail ("cdim[3] == vdim[3]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 163, __extension__ __PRETTY_FUNCTION__); }))
;
164 assert(kdim[1] == vdim[1])((void) sizeof ((kdim[1] == vdim[1]) ? 1 : 0), __extension__ (
{ if (kdim[1] == vdim[1]) ; else __assert_fail ("kdim[1] == vdim[1]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 164, __extension__ __PRETTY_FUNCTION__); }))
;
165 const int* const q_offset = q_seq_offsets->data.i32;
166 const int* const kv_offset = kv_seq_offsets->data.i32;
167 assert(q_offset[0] == 0)((void) sizeof ((q_offset[0] == 0) ? 1 : 0), __extension__ ({
if (q_offset[0] == 0) ; else __assert_fail ("q_offset[0] == 0"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 167, __extension__ __PRETTY_FUNCTION__); }))
;
168 assert(kv_offset[0] == 0)((void) sizeof ((kv_offset[0] == 0) ? 1 : 0), __extension__ (
{ if (kv_offset[0] == 0) ; else __assert_fail ("kv_offset[0] == 0"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 168, __extension__ __PRETTY_FUNCTION__); }))
;
169 assert(q_offset[batch_size] == qdim[1])((void) sizeof ((q_offset[batch_size] == qdim[1]) ? 1 : 0), __extension__
({ if (q_offset[batch_size] == qdim[1]) ; else __assert_fail
("q_offset[batch_size] == qdim[1]", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 169, __extension__ __PRETTY_FUNCTION__); }))
;
170 assert(kv_offset[batch_size] == kdim[1])((void) sizeof ((kv_offset[batch_size] == kdim[1]) ? 1 : 0), __extension__
({ if (kv_offset[batch_size] == kdim[1]) ; else __assert_fail
("kv_offset[batch_size] == kdim[1]", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 170, __extension__ __PRETTY_FUNCTION__); }))
;
171 for (i[0] = 0; i[0] < batch_size; i[0]++)
172 {
173 const int q_start = q_offset[i[0]];
174 const int q_end = q_offset[i[0] + 1];
175 const int k_start = kv_offset[i[0]];
176 const int k_end = kv_offset[i[0] + 1];
177 assert(q_start <= q_end)((void) sizeof ((q_start <= q_end) ? 1 : 0), __extension__
({ if (q_start <= q_end) ; else __assert_fail ("q_start <= q_end"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 177, __extension__ __PRETTY_FUNCTION__); }))
;
178 assert(k_start <= k_end)((void) sizeof ((k_start <= k_end) ? 1 : 0), __extension__
({ if (k_start <= k_end) ; else __assert_fail ("k_start <= k_end"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 178, __extension__ __PRETTY_FUNCTION__); }))
;
179 const int R = q_end - q_start;
180 const int K = k_end - k_start;
181 assert(R > 0)((void) sizeof ((R > 0) ? 1 : 0), __extension__ ({ if (R >
0) ; else __assert_fail ("R > 0", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 181, __extension__ __PRETTY_FUNCTION__); }))
;
182 assert(K > 0)((void) sizeof ((K > 0) ? 1 : 0), __extension__ ({ if (K >
0) ; else __assert_fail ("K > 0", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 182, __extension__ __PRETTY_FUNCTION__); }))
;
183 assert(R <= cmd.info.scaled_dot_product_attention.max_seqlen_q)((void) sizeof ((R <= cmd.info.scaled_dot_product_attention
.max_seqlen_q) ? 1 : 0), __extension__ ({ if (R <= cmd.info
.scaled_dot_product_attention.max_seqlen_q) ; else __assert_fail
("R <= cmd.info.scaled_dot_product_attention.max_seqlen_q"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 183, __extension__ __PRETTY_FUNCTION__); }))
;
184 assert(K <= cmd.info.scaled_dot_product_attention.max_seqlen_kv)((void) sizeof ((K <= cmd.info.scaled_dot_product_attention
.max_seqlen_kv) ? 1 : 0), __extension__ ({ if (K <= cmd.info
.scaled_dot_product_attention.max_seqlen_kv) ; else __assert_fail
("K <= cmd.info.scaled_dot_product_attention.max_seqlen_kv"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 184, __extension__ __PRETTY_FUNCTION__); }))
;
185 const float* const qp0 = qp + q_start * qstride[1];
186 const float* const kp0 = kp + k_start * kstride[1];
187 const float* const vp0 = vp + k_start * vstride[1];
188 float* const cp0 = cp + q_start * cstride[1];
189 for (i[1] = 0; i[1] < qdim[2]; i[1]++)
190 {
191 const float* const qp1 = qp0 + i[1] * qstride[2];
192 const float* const kp1 = kp0 + (i[1] / h_h_k_ratio) * kstride[2];
193 const float* const vp1 = vp0 + (i[1] / h_h_k_ratio) * vstride[2];
194 float* const cp1 = cp0 + i[1] * cstride[2];
195 const double sink = attention_sinks ? (double)sinkp[i[1] * sink_head_stride] : 0;
196 parallel_for(x, R){ int x; for ((x) = 0; (x) < (R); (x)++) { {
197 int y, k;
198 const float* const qp2 = qp1 + x * qstride[1];
199 float* const cp2 = cp1 + x * cstride[1];
200 float* const qk0 = qk + x * K;
201 for (y = 0; y < K; y++)
202 {
203 const float* const kp2 = kp1 + y * kstride[1];
204 float v = 0;
205 for (k = 0; k < qdim[3]; k++)
206 v += qp2[k * qstride[3]] * kp2[k * kstride[3]];
207 qk0[y] = scale * v;
208 }
209 if (is_causal)
210 {
211 const int x_end = ccv_max(x - R + K + 1, 0)({ typeof (x - R + K + 1) _a = (x - R + K + 1); typeof (0) _b
= (0); (_a > _b) ? _a : _b; })
;
212 const int x_start = sliding_window > 0 ? ccv_max(x_end - sliding_window, 0)({ typeof (x_end - sliding_window) _a = (x_end - sliding_window
); typeof (0) _b = (0); (_a > _b) ? _a : _b; })
: 0;
213 for (y = 0; y < x_start; y++)
214 qk0[y] = 0;
215 for (y = x_end; y < K; y++)
216 qk0[y] = 0;
217 double maxval = attention_sinks ? sink : qk0[x_start];
218 for (y = attention_sinks ? x_start : x_start + 1; y < x_end; y++)
219 if (qk0[y] > maxval)
220 maxval = qk0[y];
221 double sumval = attention_sinks ? expf(sink - maxval) : 0;
222 for (y = x_start; y < x_end; y++)
223 sumval += (qk0[y] = expf(qk0[y] - maxval));
224 sumval = 1.0 / sumval;
225 for (y = x_start; y < x_end; y++)
226 qk0[y] *= sumval;
227 } else {
228 double maxval = attention_sinks ? sink : qk0[0];
229 for (y = attention_sinks ? 0 : 1; y < K; y++)
230 if (qk0[y] > maxval)
231 maxval = qk0[y];
232 double sumval = attention_sinks ? expf(sink - maxval) : 0;
233 for (y = 0; y < K; y++)
234 sumval += (qk0[y] = expf(qk0[y] - maxval));
235 sumval = 1.0 / sumval;
236 for (y = 0; y < K; y++)
237 qk0[y] *= sumval;
238 }
239 for (k = 0; k < vdim[3]; k++)
240 cp2[k * cstride[3]] = 0;
241 for (y = 0; y < K; y++)
242 {
243 const float* const vp2 = vp1 + y * vstride[1];
244 const float v = qk0[y];
245 for (k = 0; k < vdim[3]; k++)
246 cp2[k * cstride[3]] += v * vp2[k * vstride[3]];
247 }
248 } parallel_endfor} }
249 }
250 }
251 return CCV_NNC_EXEC_SUCCESS;
252 }
253 for (i[0] = 0; i[0] < qdim[0]; i[0]++)
91
Assuming the condition is true
92
Loop condition is true. Entering loop body
254 {
255 const float* const qp0 = qp + i[0] * qstride[0];
256 const float* const kp0 = kp + i[0] * kstride[0];
257 const float* const vp0 = vp + i[0] * vstride[0];
258 const float* const amp0 = amp && amdim[0] > 1 ? amp + i[0] * amstride[0] : amp;
93
Assuming 'amp' is null
259 float* const cp0 = cp + i[0] * cstride[0];
260 for (i[1] = 0; i[1] < qdim[2]; i[1]++)
94
Assuming the condition is true
95
Loop condition is true. Entering loop body
261 {
262 const float* const qp1 = qp0 + i[1] * qstride[2];
263 const float* const kp1 = kp0 + (i[1] / h_h_k_ratio) * kstride[2];
264 const float* const vp1 = vp0 + (i[1] / h_h_k_ratio) * vstride[2];
265 const float* const amp1 = amp
95.1
'amp' is null
&& amdim[1] > 1 ? amp0 + i[1] * amstride[1] : amp0;
266 float* const cp1 = cp0 + i[1] * cstride[2];
267 // Compute Q @ K^T
268 parallel_for(x, qdim[1]){ int x; for ((x) = 0; (x) < (qdim[1]); (x)++) { {
96
Assuming the condition is true
97
Loop condition is true. Entering loop body
269 int y, k;
270 const double sink = attention_sinks
97.1
'attention_sinks' is not equal to 0
? (double)sinkp[(i[0] * qdim[1] + x) * sink_query_stride + i[1] * sink_head_stride] : 0;
98
'?' condition is true
271 const float* const qp2 = qp1 + x * qstride[1];
272 float* const cp2 = cp1 + x * cstride[1];
273 float* const qk0 = qk + x * kdim[1];
274 const float* const amp2 = amp1
98.1
'amp1' is null
? amp1 + x * amstride[2] : 0;
99
'?' condition is false
100
'amp2' initialized to a null pointer value
275 if (attn_mask
100.1
'attn_mask' is non-null
)
101
Taking true branch
276 {
277 for (y = 0; y < kdim[1]; y++)
102
Assuming the condition is true
103
Loop condition is true. Entering loop body
278 {
279 const float* const kp2 = kp1 + y * kstride[1];
280 float v = 0;
281 for (k = 0; k < qdim[3]; k++)
104
Assuming the condition is false
105
Loop condition is false. Execution continues on line 283
282 v += qp2[k * qstride[3]] * kp2[k * kstride[3]];
283 qk0[y] = scale * v + amp2[y * amstride[3]];
106
Array access (from variable 'amp2') results in a null pointer dereference
284 }
285 } else {
286 for (y = 0; y < kdim[1]; y++)
287 {
288 const float* const kp2 = kp1 + y * kstride[1];
289 float v = 0;
290 for (k = 0; k < qdim[3]; k++)
291 v += qp2[k * qstride[3]] * kp2[k * kstride[3]];
292 qk0[y] = scale * v;
293 }
294 }
295 if (relative_bias)
296 {
297 const float* const rbp = relative_biasp + ((size_t)(i[0] * qdim[1] + x) * qdim[2] + i[1]) * relative_extent;
298 // The query's own key, which may be before the keys or after them.
299 const int own = x - qdim[1] + kdim[1];
300 const int y_end = ccv_min(own + 1, kdim[1])({ typeof (own + 1) _a = (own + 1); typeof (kdim[1]) _b = (kdim
[1]); (_a < _b) ? _a : _b; })
;
301 for (y = ccv_max(own - relative_extent + 1, 0)({ typeof (own - relative_extent + 1) _a = (own - relative_extent
+ 1); typeof (0) _b = (0); (_a > _b) ? _a : _b; })
; y < y_end; y++)
302 qk0[y] += rbp[own - y];
303 }
304 // Compute softmax on qk.
305 if (is_causal)
306 {
307 const int x_end = ccv_max(x - qdim[1] + kdim[1] + 1, 0)({ typeof (x - qdim[1] + kdim[1] + 1) _a = (x - qdim[1] + kdim
[1] + 1); typeof (0) _b = (0); (_a > _b) ? _a : _b; })
;
308 const int x_start = sliding_window > 0 ? ccv_max(x_end - sliding_window, 0)({ typeof (x_end - sliding_window) _a = (x_end - sliding_window
); typeof (0) _b = (0); (_a > _b) ? _a : _b; })
: 0;
309 for (y = 0; y < x_start; y++)
310 qk0[y] = 0;
311 for (y = x_end; y < kdim[1]; y++)
312 qk0[y] = 0;
313 double maxval = attention_sinks ? sink : qk0[x_start];
314 for (y = attention_sinks ? x_start : x_start + 1; y < x_end; y++)
315 if (qk0[y] > maxval)
316 maxval = qk0[y];
317 double sumval = attention_sinks ? expf(sink - maxval) : 0;
318 for (y = x_start; y < x_end; y++)
319 sumval += (qk0[y] = expf(qk0[y] - maxval));
320 sumval = 1.0 / sumval;
321 for (y = x_start; y < x_end; y++)
322 qk0[y] *= sumval;
323 } else {
324 double maxval = attention_sinks ? sink : qk0[0];
325 for (y = attention_sinks ? 0 : 1; y < kdim[1]; y++)
326 if (qk0[y] > maxval)
327 maxval = qk0[y];
328 double sumval = attention_sinks ? expf(sink - maxval) : 0;
329 for (y = 0; y < kdim[1]; y++)
330 sumval += (qk0[y] = expf(qk0[y] - maxval));
331 sumval = 1.0 / sumval;
332 for (y = 0; y < kdim[1]; y++)
333 qk0[y] *= sumval;
334 }
335 for (k = 0; k < vdim[3]; k++)
336 cp2[k * cstride[3]] = 0;
337 for (y = 0; y < kdim[1]; y++)
338 {
339 const float* const vp2 = vp1 + y * vstride[1];
340 const float v = qk0[y];
341 for (k = 0; k < vdim[3]; k++)
342 cp2[k * cstride[3]] += v * vp2[k * vstride[3]];
343 }
344 } parallel_endfor} }
345 }
346 }
347 if (w)
348 {
349 const int num_heads = cdim[2];
350 ccv_nnc_tensor_view_t* const d = (ccv_nnc_tensor_view_t*)outputs[0];
351 const int w_nd = ccv_nnc_tensor_nd(w->info.dim);
352 assert(w_nd == 2)((void) sizeof ((w_nd == 2) ? 1 : 0), __extension__ ({ if (w_nd
== 2) ; else __assert_fail ("w_nd == 2", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 352, __extension__ __PRETTY_FUNCTION__); }))
;
353 assert(CCV_IS_TENSOR_CONTIGUOUS(w))((void) sizeof (((!((*(int*)(w)) & CCV_TENSOR_VIEW) || ((
(ccv_nnc_tensor_view_t*)w)->contiguous == 1))) ? 1 : 0), __extension__
({ if ((!((*(int*)(w)) & CCV_TENSOR_VIEW) || (((ccv_nnc_tensor_view_t
*)w)->contiguous == 1))) ; else __assert_fail ("CCV_IS_TENSOR_CONTIGUOUS(w)"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 353, __extension__ __PRETTY_FUNCTION__); }))
;
354 const int d_nd = ccv_nnc_tensor_nd(d->info.dim);
355 assert(d_nd == 3)((void) sizeof ((d_nd == 3) ? 1 : 0), __extension__ ({ if (d_nd
== 3) ; else __assert_fail ("d_nd == 3", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 355, __extension__ __PRETTY_FUNCTION__); }))
;
356 int ddim[CCV_NNC_MAX_DIM_ALLOC(12)];
357 int dstride[CCV_NNC_MAX_DIM_ALLOC(12)];
358 ccv_nnc_tensor_view_get_dim(d, ddim);
359 ccv_nnc_tensor_view_get_stride(d, dstride);
360 assert(ddim[2] == cdim[1])((void) sizeof ((ddim[2] == cdim[1]) ? 1 : 0), __extension__ (
{ if (ddim[2] == cdim[1]) ; else __assert_fail ("ddim[2] == cdim[1]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 360, __extension__ __PRETTY_FUNCTION__); }))
;
361 assert(ddim[3] == num_heads * cdim[3])((void) sizeof ((ddim[3] == num_heads * cdim[3]) ? 1 : 0), __extension__
({ if (ddim[3] == num_heads * cdim[3]) ; else __assert_fail (
"ddim[3] == num_heads * cdim[3]", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 361, __extension__ __PRETTY_FUNCTION__); }))
;
362 assert(w->info.dim[1] == ddim[3])((void) sizeof ((w->info.dim[1] == ddim[3]) ? 1 : 0), __extension__
({ if (w->info.dim[1] == ddim[3]) ; else __assert_fail ("w->info.dim[1] == ddim[3]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 362, __extension__ __PRETTY_FUNCTION__); }))
;
363 assert(w->info.dim[0] == ddim[3])((void) sizeof ((w->info.dim[0] == ddim[3]) ? 1 : 0), __extension__
({ if (w->info.dim[0] == ddim[3]) ; else __assert_fail ("w->info.dim[0] == ddim[3]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 363, __extension__ __PRETTY_FUNCTION__); }))
;
364 float* const dp = d->data.f32;
365 const float* const wp = w->data.f32;
366 const float* const cp = c->data.f32;
367 if (bias)
368 {
369 assert(ccv_nnc_tensor_count(bias->info) == ddim[3])((void) sizeof ((ccv_nnc_tensor_count(bias->info) == ddim[
3]) ? 1 : 0), __extension__ ({ if (ccv_nnc_tensor_count(bias->
info) == ddim[3]) ; else __assert_fail ("ccv_nnc_tensor_count(bias->info) == ddim[3]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 369, __extension__ __PRETTY_FUNCTION__); }))
;
370 assert(CCV_IS_TENSOR_CONTIGUOUS(bias))((void) sizeof (((!((*(int*)(bias)) & CCV_TENSOR_VIEW) ||
(((ccv_nnc_tensor_view_t*)bias)->contiguous == 1))) ? 1 :
0), __extension__ ({ if ((!((*(int*)(bias)) & CCV_TENSOR_VIEW
) || (((ccv_nnc_tensor_view_t*)bias)->contiguous == 1))) ;
else __assert_fail ("CCV_IS_TENSOR_CONTIGUOUS(bias)", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 370, __extension__ __PRETTY_FUNCTION__); }))
;
371 const float* const biasp = bias->data.f32;
372 for (i[0] = 0; i[0] < ddim[1]; i[0]++)
373 {
374 const float* const cp0 = cp + i[0] * cstride[0];
375 float* const dp0 = dp + i[0] * dstride[1];
376 parallel_for(y, ddim[2]){ int y; for ((y) = 0; (y) < (ddim[2]); (y)++) { {
377 int x, j, k;
378 const float* const cp1 = cp0 + y * cstride[1];
379 float* const dp1 = dp0 + y * dstride[2];
380 for (x = 0; x < ddim[3]; x++)
381 {
382 const float* const wp0 = wp + x * ddim[3];
383 float v = biasp[x];
384 for (j = 0; j < num_heads; j++)
385 {
386 const float* const cp2 = cp1 + j * cstride[2];
387 for (k = 0; k < cdim[3]; k++)
388 v += wp0[j * cdim[3] + k] * cp2[k * cstride[3]];
389 }
390 dp1[x * dstride[3]] = v;
391 }
392 } parallel_endfor} }
393 }
394 } else {
395 for (i[0] = 0; i[0] < ddim[1]; i[0]++)
396 {
397 const float* const cp0 = cp + i[0] * cstride[0];
398 float* const dp0 = dp + i[0] * dstride[1];
399 parallel_for(y, ddim[2]){ int y; for ((y) = 0; (y) < (ddim[2]); (y)++) { {
400 int x, j, k;
401 const float* const cp1 = cp0 + y * cstride[1];
402 float* const dp1 = dp0 + y * dstride[2];
403 for (x = 0; x < ddim[3]; x++)
404 {
405 const float* const wp0 = wp + x * ddim[3];
406 float v = 0;
407 for (j = 0; j < num_heads; j++)
408 {
409 const float* const cp2 = cp1 + j * cstride[2];
410 for (k = 0; k < cdim[3]; k++)
411 v += wp0[j * cdim[3] + k] * cp2[k * cstride[3]];
412 }
413 dp1[x * dstride[3]] = v;
414 }
415 } parallel_endfor} }
416 }
417 }
418 }
419 return CCV_NNC_EXEC_SUCCESS;
420}
421
422static int _ccv_nnc_scaled_dot_product_attention_back(const ccv_nnc_cmd_t cmd, const ccv_nnc_hint_t hint, const int flags, ccv_nnc_tensor_t* const* const inputs, const int input_size, ccv_nnc_tensor_t* const* const outputs, const int output_size, ccv_nnc_stream_context_t* const stream_context)
423{
424 // Assuming no saved_softmax, we need to recompute from q, k, v.
425 // We cannot do this with masks (yet).
426 assert(input_size >= 6)((void) sizeof ((input_size >= 6) ? 1 : 0), __extension__ (
{ if (input_size >= 6) ; else __assert_fail ("input_size >= 6"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 426, __extension__ __PRETTY_FUNCTION__); }))
;
427 if (cmd.info.scaled_dot_product_attention.is_varlen)
428 return CCV_NNC_EXEC_INVALID;
429 if (cmd.info.scaled_dot_product_attention.attention_sinks)
430 return CCV_NNC_EXEC_INVALID;
431 if (cmd.info.scaled_dot_product_attention.sliding_window != 0)
432 return CCV_NNC_EXEC_INVALID;
433 if (cmd.info.scaled_dot_product_attention.relative_bias)
434 return CCV_NNC_EXEC_INVALID;
435 ccv_nnc_tensor_view_t* const g = (ccv_nnc_tensor_view_t*)inputs[0];
436 ccv_nnc_tensor_view_t* const q = (ccv_nnc_tensor_view_t*)inputs[3];
437 ccv_nnc_tensor_view_t* const k = (ccv_nnc_tensor_view_t*)inputs[4];
438 ccv_nnc_tensor_view_t* const v = (ccv_nnc_tensor_view_t*)inputs[5];
439 ccv_nnc_tensor_view_t* const dq = (ccv_nnc_tensor_view_t*)outputs[0];
440 ccv_nnc_tensor_view_t* const dk = (ccv_nnc_tensor_view_t*)outputs[1];
441 ccv_nnc_tensor_view_t* const dv = (ccv_nnc_tensor_view_t*)outputs[2];
442 const int q_nd = ccv_nnc_tensor_nd(q->info.dim);
443 assert(q_nd == 3 || q_nd == 4)((void) sizeof ((q_nd == 3 || q_nd == 4) ? 1 : 0), __extension__
({ if (q_nd == 3 || q_nd == 4) ; else __assert_fail ("q_nd == 3 || q_nd == 4"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 443, __extension__ __PRETTY_FUNCTION__); }))
;
444 const int k_nd = ccv_nnc_tensor_nd(k->info.dim);
445 assert(k_nd == 3 || k_nd == 4)((void) sizeof ((k_nd == 3 || k_nd == 4) ? 1 : 0), __extension__
({ if (k_nd == 3 || k_nd == 4) ; else __assert_fail ("k_nd == 3 || k_nd == 4"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 445, __extension__ __PRETTY_FUNCTION__); }))
;
446 const int v_nd = ccv_nnc_tensor_nd(v->info.dim);
447 assert(v_nd == 3 || v_nd == 4)((void) sizeof ((v_nd == 3 || v_nd == 4) ? 1 : 0), __extension__
({ if (v_nd == 3 || v_nd == 4) ; else __assert_fail ("v_nd == 3 || v_nd == 4"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 447, __extension__ __PRETTY_FUNCTION__); }))
;
448 const int g_nd = ccv_nnc_tensor_nd(g->info.dim);
449 assert(g_nd == 3 || g_nd == 4)((void) sizeof ((g_nd == 3 || g_nd == 4) ? 1 : 0), __extension__
({ if (g_nd == 3 || g_nd == 4) ; else __assert_fail ("g_nd == 3 || g_nd == 4"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 449, __extension__ __PRETTY_FUNCTION__); }))
;
450 const int dq_nd = ccv_nnc_tensor_nd(dq->info.dim);
451 assert(dq_nd == 3 || dq_nd == 4)((void) sizeof ((dq_nd == 3 || dq_nd == 4) ? 1 : 0), __extension__
({ if (dq_nd == 3 || dq_nd == 4) ; else __assert_fail ("dq_nd == 3 || dq_nd == 4"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 451, __extension__ __PRETTY_FUNCTION__); }))
;
452 assert(dq_nd == q_nd)((void) sizeof ((dq_nd == q_nd) ? 1 : 0), __extension__ ({ if
(dq_nd == q_nd) ; else __assert_fail ("dq_nd == q_nd", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 452, __extension__ __PRETTY_FUNCTION__); }))
;
453 const int dk_nd = ccv_nnc_tensor_nd(dk->info.dim);
454 assert(dk_nd == 3 || dk_nd == 4)((void) sizeof ((dk_nd == 3 || dk_nd == 4) ? 1 : 0), __extension__
({ if (dk_nd == 3 || dk_nd == 4) ; else __assert_fail ("dk_nd == 3 || dk_nd == 4"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 454, __extension__ __PRETTY_FUNCTION__); }))
;
455 assert(dk_nd == k_nd)((void) sizeof ((dk_nd == k_nd) ? 1 : 0), __extension__ ({ if
(dk_nd == k_nd) ; else __assert_fail ("dk_nd == k_nd", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 455, __extension__ __PRETTY_FUNCTION__); }))
;
456 const int dv_nd = ccv_nnc_tensor_nd(dv->info.dim);
457 assert(dv_nd == 3 || dv_nd == 4)((void) sizeof ((dv_nd == 3 || dv_nd == 4) ? 1 : 0), __extension__
({ if (dv_nd == 3 || dv_nd == 4) ; else __assert_fail ("dv_nd == 3 || dv_nd == 4"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 457, __extension__ __PRETTY_FUNCTION__); }))
;
458 assert(dv_nd == v_nd)((void) sizeof ((dv_nd == v_nd) ? 1 : 0), __extension__ ({ if
(dv_nd == v_nd) ; else __assert_fail ("dv_nd == v_nd", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 458, __extension__ __PRETTY_FUNCTION__); }))
;
459 assert(q_nd == k_nd && k_nd == v_nd && v_nd == g_nd)((void) sizeof ((q_nd == k_nd && k_nd == v_nd &&
v_nd == g_nd) ? 1 : 0), __extension__ ({ if (q_nd == k_nd &&
k_nd == v_nd && v_nd == g_nd) ; else __assert_fail (
"q_nd == k_nd && k_nd == v_nd && v_nd == g_nd"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 459, __extension__ __PRETTY_FUNCTION__); }))
;
460 // Assuming this is float 32.
461 int qdim[CCV_NNC_MAX_DIM_ALLOC(12)];
462 int kdim[CCV_NNC_MAX_DIM_ALLOC(12)];
463 int vdim[CCV_NNC_MAX_DIM_ALLOC(12)];
464 int gdim[CCV_NNC_MAX_DIM_ALLOC(12)];
465 int dqdim[CCV_NNC_MAX_DIM_ALLOC(12)];
466 int dkdim[CCV_NNC_MAX_DIM_ALLOC(12)];
467 int dvdim[CCV_NNC_MAX_DIM_ALLOC(12)];
468 ccv_nnc_tensor_view_get_dim(q, qdim);
469 ccv_nnc_tensor_view_get_dim(k, kdim);
470 ccv_nnc_tensor_view_get_dim(v, vdim);
471 ccv_nnc_tensor_view_get_dim(g, gdim);
472 ccv_nnc_tensor_view_get_dim(dq, dqdim);
473 ccv_nnc_tensor_view_get_dim(dk, dkdim);
474 ccv_nnc_tensor_view_get_dim(dv, dvdim);
475 if (q_nd == 3)
476 {
477 qdim[0] = qdim[1], qdim[1] = qdim[2], qdim[2] = 1;
478 kdim[0] = kdim[1], kdim[1] = kdim[2], kdim[2] = 1;
479 vdim[0] = vdim[1], vdim[1] = vdim[2], vdim[2] = 1;
480 gdim[0] = gdim[1], gdim[1] = gdim[2], gdim[2] = 1;
481 dqdim[0] = dqdim[1], dqdim[1] = dqdim[2], dqdim[2] = 1;
482 dkdim[0] = dkdim[1], dkdim[1] = dkdim[2], dkdim[2] = 1;
483 dvdim[0] = dvdim[1], dvdim[1] = dvdim[2], dvdim[2] = 1;
484 }
485 assert(qdim[0] == kdim[0] && kdim[0] == vdim[0] && vdim[0] == gdim[0])((void) sizeof ((qdim[0] == kdim[0] && kdim[0] == vdim
[0] && vdim[0] == gdim[0]) ? 1 : 0), __extension__ ({
if (qdim[0] == kdim[0] && kdim[0] == vdim[0] &&
vdim[0] == gdim[0]) ; else __assert_fail ("qdim[0] == kdim[0] && kdim[0] == vdim[0] && vdim[0] == gdim[0]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 485, __extension__ __PRETTY_FUNCTION__); }))
;
486 assert(qdim[2] == gdim[2])((void) sizeof ((qdim[2] == gdim[2]) ? 1 : 0), __extension__ (
{ if (qdim[2] == gdim[2]) ; else __assert_fail ("qdim[2] == gdim[2]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 486, __extension__ __PRETTY_FUNCTION__); }))
;
487 assert(kdim[2] == vdim[2])((void) sizeof ((kdim[2] == vdim[2]) ? 1 : 0), __extension__ (
{ if (kdim[2] == vdim[2]) ; else __assert_fail ("kdim[2] == vdim[2]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 487, __extension__ __PRETTY_FUNCTION__); }))
;
488 assert(qdim[2] % kdim[2] == 0)((void) sizeof ((qdim[2] % kdim[2] == 0) ? 1 : 0), __extension__
({ if (qdim[2] % kdim[2] == 0) ; else __assert_fail ("qdim[2] % kdim[2] == 0"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 488, __extension__ __PRETTY_FUNCTION__); }))
;
489 assert(qdim[2] >= kdim[2])((void) sizeof ((qdim[2] >= kdim[2]) ? 1 : 0), __extension__
({ if (qdim[2] >= kdim[2]) ; else __assert_fail ("qdim[2] >= kdim[2]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 489, __extension__ __PRETTY_FUNCTION__); }))
;
490 assert(qdim[3] == kdim[3])((void) sizeof ((qdim[3] == kdim[3]) ? 1 : 0), __extension__ (
{ if (qdim[3] == kdim[3]) ; else __assert_fail ("qdim[3] == kdim[3]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 490, __extension__ __PRETTY_FUNCTION__); }))
;
491 assert(kdim[1] == vdim[1])((void) sizeof ((kdim[1] == vdim[1]) ? 1 : 0), __extension__ (
{ if (kdim[1] == vdim[1]) ; else __assert_fail ("kdim[1] == vdim[1]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 491, __extension__ __PRETTY_FUNCTION__); }))
;
492 assert(gdim[1] == qdim[1])((void) sizeof ((gdim[1] == qdim[1]) ? 1 : 0), __extension__ (
{ if (gdim[1] == qdim[1]) ; else __assert_fail ("gdim[1] == qdim[1]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 492, __extension__ __PRETTY_FUNCTION__); }))
;
493 assert(gdim[3] == vdim[3])((void) sizeof ((gdim[3] == vdim[3]) ? 1 : 0), __extension__ (
{ if (gdim[3] == vdim[3]) ; else __assert_fail ("gdim[3] == vdim[3]"
, "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 493, __extension__ __PRETTY_FUNCTION__); }))
;
494 assert(CCV_NNC_MAX_DIM == 2)((void) sizeof (((2) == 2) ? 1 : 0), __extension__ ({ if ((2)
== 2) ; else __assert_fail ("CCV_NNC_MAX_DIM == 2", "scaled_dot_product_attention/ccv_nnc_scaled_dot_product_attention_cpu_ref.c"
, 494, __extension__ __PRETTY_FUNCTION__); }))
; // Need to change this logic for CCV_NNC_MAX_DIM == other number.
495 int qstride[CCV_NNC_MAX_DIM_ALLOC(12)];
496 int kstride[CCV_NNC_MAX_DIM_ALLOC(12)];
497 int vstride[CCV_NNC_MAX_DIM_ALLOC(12)];
498 int gstride[CCV_NNC_MAX_DIM_ALLOC(12)];
499 int dqstride[CCV_NNC_MAX_DIM_ALLOC(12)];
500 int dkstride[CCV_NNC_MAX_DIM_ALLOC(12)];
501 int dvstride[CCV_NNC_MAX_DIM_ALLOC(12)];
502 ccv_nnc_tensor_view_get_stride(q, qstride);
503 ccv_nnc_tensor_view_get_stride(k, kstride);
504 ccv_nnc_tensor_view_get_stride(v, vstride);
505 ccv_nnc_tensor_view_get_stride(g, gstride);
506 ccv_nnc_tensor_view_get_stride(dq, dqstride);
507 ccv_nnc_tensor_view_get_stride(dk, dkstride);
508 ccv_nnc_tensor_view_get_stride(dv, dvstride);
509 if (q_nd == 3)
510 {
511 qstride[0] = qstride[1], qstride[1] = qstride[2], qstride[2] = qstride[3];
512 kstride[0] = kstride[1], kstride[1] = kstride[2], kstride[2] = kstride[3];
513 vstride[0] = vstride[1], vstride[1] = vstride[2], vstride[2] = vstride[3];
514 gstride[0] = gstride[1], gstride[1] = gstride[2], gstride[2] = gstride[3];
515 dqstride[0] = dqstride[1], dqstride[1] = dqstride[2], dqstride[2] = dqstride[3];
516 dkstride[0] = dkstride[1], dkstride[1] = dkstride[2], dkstride[2] = dkstride[3];
517 dvstride[0] = dvstride[1], dvstride[1] = dvstride[2], dvstride[2] = dvstride[3];
518 }
519 int i[CCV_NNC_MAX_DIM(2) + 2];
520 float* qk = ccv_nnc_stream_context_get_workspace(stream_context, sizeof(float) * 2 * kdim[1], CCV_TENSOR_CPU_MEMORY);
521 const float* const qp = q->data.f32;
522 const float* const kp = k->data.f32;
523 const float* const vp = v->data.f32;
524 const float* const gp = g->data.f32;
525 float* const dqp = dq->data.f32;
526 float* const dkp = dk->data.f32;
527 float* const dvp = dv->data.f32;
528 const float scale = cmd.info.scaled_dot_product_attention.scale;
529 const int is_causal = cmd.info.scaled_dot_product_attention.is_causal;
530 const int h_h_k_ratio = qdim[2] / kdim[2];
531 for (i[0] = 0; i[0] < qdim[0]; i[0]++)
532 {
533 const float* const qp0 = qp + i[0] * qstride[0];
534 const float* const kp0 = kp + i[0] * kstride[0];
535 const float* const vp0 = vp + i[0] * vstride[0];
536 const float* const gp0 = gp + i[0] * gstride[0];
537 float* const dqp0 = dqp + i[0] * dqstride[0];
538 float* const dkp0 = dkp + i[0] * dkstride[0];
539 float* const dvp0 = dvp + i[0] * dvstride[0];
540 for (i[1] = 0; i[1] < qdim[2]; i[1]++)
541 {
542 const float* const qp1 = qp0 + i[1] * qstride[2];
543 const float* const kp1 = kp0 + (i[1] / h_h_k_ratio) * kstride[2];
544 const float* const vp1 = vp0 + (i[1] / h_h_k_ratio) * vstride[2];
545 const float* const gp1 = gp0 + i[1] * gstride[2];
546 float* const dqp1 = dqp0 + i[1] * dqstride[2];
547 float* const dkp1 = dkp0 + (i[1] / h_h_k_ratio) * dkstride[2];
548 float* const dvp1 = dvp0 + (i[1] / h_h_k_ratio) * dvstride[2];
549 // Compute Q @ K^T
550 int x, y, k;
551 for (x = 0; x < qdim[1]; x++)
552 {
553 float* const dqp2 = dqp1 + x * dqstride[1];
554 for (k = 0; k < qdim[3]; k++)
555 dqp2[k * dqstride[3]] = 0;
556 }
557 // Only zero out when it is at 0-index.
558 if (i[1] % h_h_k_ratio == 0)
559 for (y = 0; y < kdim[1]; y++)
560 {
561 float* const dkp2 = dkp1 + y * dkstride[1];
562 for (k = 0; k < qdim[3]; k++)
563 dkp2[k * dkstride[3]] = 0;
564 }
565 // Only zero out when it is at 0-index.
566 if (i[1] % h_h_k_ratio == 0)
567 for (y = 0; y < kdim[1]; y++)
568 {
569 float* const dvp2 = dvp1 + y * dvstride[1];
570 for (k = 0; k < vdim[3]; k++)
571 dvp2[k * dvstride[3]] = 0;
572 }
573 for (x = 0; x < qdim[1]; x++)
574 {
575 const float* const qp2 = qp1 + x * qstride[1];
576 const float* const gp2 = gp1 + x * gstride[1];
577 float* const qk0 = qk;
578 float* const qks0 = qk + kdim[1];
579 for (y = 0; y < kdim[1]; y++)
580 {
581 const float* const kp2 = kp1 + y * kstride[1];
582 float v = 0;
583 for (k = 0; k < qdim[3]; k++)
584 v += qp2[k * qstride[3]] * kp2[k * kstride[3]];
585 qk0[y] = scale * v;
586 }
587 // Compute softmax on qk.
588 if (is_causal)
589 {
590 const int x_end = ccv_max(x - qdim[1] + kdim[1] + 1, 0)({ typeof (x - qdim[1] + kdim[1] + 1) _a = (x - qdim[1] + kdim
[1] + 1); typeof (0) _b = (0); (_a > _b) ? _a : _b; })
;
591 for (y = x_end; y < kdim[1]; y++)
592 qk0[y] = 0;
593 double maxval = qk0[0];
594 for (y = 1; y < x_end; y++)
595 if (qk0[y] > maxval)
596 maxval = qk0[y];
597 double sumval = 0;
598 for (y = 0; y < x_end; y++)
599 sumval += (qk0[y] = expf(qk0[y] - maxval));
600 sumval = 1.0 / sumval;
601 for (y = 0; y < x_end; y++)
602 qk0[y] *= sumval;
603 } else {
604 double maxval = qk0[0];
605 for (y = 1; y < kdim[1]; y++)
606 if (qk0[y] > maxval)
607 maxval = qk0[y];
608 double sumval = 0;
609 for (y = 0; y < kdim[1]; y++)
610 sumval += (qk0[y] = expf(qk0[y] - maxval));
611 sumval = 1.0 / sumval;
612 for (y = 0; y < kdim[1]; y++)
613 qk0[y] *= sumval;
614 }
615 for (y = 0; y < kdim[1]; y++)
616 {
617 float* const dvp2 = dvp1 + y * dvstride[1];
618 const float v = qk0[y];
619 for (k = 0; k < vdim[3]; k++)
620 dvp2[k * dvstride[3]] += v * gp2[k * gstride[3]];
621 }
622 double sumval = 0;
623 for (y = 0; y < kdim[1]; y++)
624 {
625 const float* const vp2 = vp1 + y * vstride[1];
626 float v = 0;
627 for (k = 0; k < vdim[3]; k++)
628 v += gp2[k * gstride[3]] * vp2[k * vstride[3]];
629 qks0[y] = v;
630 sumval += v * qk0[y];
631 }
632 for (y = 0; y < kdim[1]; y++)
633 qk0[y] = (qks0[y] - sumval) * qk0[y];
634 float* const dqp2 = dqp1 + x * dqstride[1];
635 for (y = 0; y < kdim[1]; y++)
636 {
637 const float* const kp2 = kp1 + y * kstride[1];
638 float* const dkp2 = dkp1 + y * dkstride[1];
639 const float v = scale * qk0[y];
640 for (k = 0; k < qdim[3]; k++)
641 {
642 dqp2[k * dqstride[3]] += v * kp2[k * kstride[3]];
643 dkp2[k * dkstride[3]] += v * qp2[k * qstride[3]];
644 }
645 }
646 }
647 }
648 }
649 return CCV_NNC_EXEC_SUCCESS;
650}
651
652REGISTER_COMMAND_BACKEND(CCV_NNC_SCALED_DOT_PRODUCT_ATTENTION_FORWARD, CCV_NNC_BACKEND_CPU_REF)void _register_command_CCV_NNC_SCALED_DOT_PRODUCT_ATTENTION_FORWARD_backend_CCV_NNC_BACKEND_CPU_REF(ccv_nnc_cmd_backend_registry_t* const registry)
653{
654 registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC;
655 registry->tensor_datatypes = CCV_32F | CCV_32S;
656 registry->tensor_memory = CCV_TENSOR_CPU_MEMORY;
657 registry->algorithms = 1;
658 registry->exec = _ccv_nnc_scaled_dot_product_attention_forw_cpu_ref;
659}
660
661REGISTER_COMMAND_BACKEND(CCV_NNC_SCALED_DOT_PRODUCT_ATTENTION_BACKWARD, CCV_NNC_BACKEND_CPU_REF)void _register_command_CCV_NNC_SCALED_DOT_PRODUCT_ATTENTION_BACKWARD_backend_CCV_NNC_BACKEND_CPU_REF(ccv_nnc_cmd_backend_registry_t* const registry)
662{
663 registry->tensor_formats = CCV_TENSOR_FORMAT_NHWC;
664 registry->tensor_datatypes = CCV_32F;
665 registry->tensor_memory = CCV_TENSOR_CPU_MEMORY;
666 registry->algorithms = 1;
667 registry->exec = _ccv_nnc_scaled_dot_product_attention_back;
668}