1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Fused elementwise kernel entry points
//!
//! - fused_mul_add: out = a * b + c
//! - fused_add_mul: out = (a + b) * c
//! - fused_mul_add_scalar: out = a * scale + bias
use super::binary_int::binary_int_fused_elem;
use crate::dtype::Element;
use crate::ops::BinaryOp;
/// Fused multiply-add: `out[i] = a[i] * b[i] + c[i]`
///
/// # Safety
/// - `a`, `b`, `c`, and `out` must be valid pointers to `len` elements
#[inline]
pub unsafe fn fused_mul_add_kernel<T: Element>(
a: *const T,
b: *const T,
c: *const T,
out: *mut T,
len: usize,
) {
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
{
use super::simd::fused_elementwise;
use crate::dtype::DType;
match T::DTYPE {
DType::F32 => {
fused_elementwise::fused_mul_add_f32(
a as *const f32,
b as *const f32,
c as *const f32,
out as *mut f32,
len,
);
return;
}
DType::F64 => {
fused_elementwise::fused_mul_add_f64(
a as *const f64,
b as *const f64,
c as *const f64,
out as *mut f64,
len,
);
return;
}
#[cfg(feature = "f16")]
DType::F16 => {
fused_elementwise::fused_mul_add_f16(
a as *const half::f16,
b as *const half::f16,
c as *const half::f16,
out as *mut half::f16,
len,
);
return;
}
#[cfg(feature = "f16")]
DType::BF16 => {
fused_elementwise::fused_mul_add_bf16(
a as *const half::bf16,
b as *const half::bf16,
c as *const half::bf16,
out as *mut half::bf16,
len,
);
return;
}
_ => {}
}
}
fused_ternary_scalar(
a,
b,
c,
out,
len,
BinaryOp::Mul,
BinaryOp::Add,
|x, y, z| x * y + z,
);
}
/// Fused add-multiply: `out[i] = (a[i] + b[i]) * c[i]`
///
/// # Safety
/// - `a`, `b`, `c`, and `out` must be valid pointers to `len` elements
#[inline]
pub unsafe fn fused_add_mul_kernel<T: Element>(
a: *const T,
b: *const T,
c: *const T,
out: *mut T,
len: usize,
) {
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
{
use super::simd::fused_elementwise;
use crate::dtype::DType;
match T::DTYPE {
DType::F32 => {
fused_elementwise::fused_add_mul_f32(
a as *const f32,
b as *const f32,
c as *const f32,
out as *mut f32,
len,
);
return;
}
DType::F64 => {
fused_elementwise::fused_add_mul_f64(
a as *const f64,
b as *const f64,
c as *const f64,
out as *mut f64,
len,
);
return;
}
#[cfg(feature = "f16")]
DType::F16 => {
fused_elementwise::fused_add_mul_f16(
a as *const half::f16,
b as *const half::f16,
c as *const half::f16,
out as *mut half::f16,
len,
);
return;
}
#[cfg(feature = "f16")]
DType::BF16 => {
fused_elementwise::fused_add_mul_bf16(
a as *const half::bf16,
b as *const half::bf16,
c as *const half::bf16,
out as *mut half::bf16,
len,
);
return;
}
_ => {}
}
}
fused_ternary_scalar(
a,
b,
c,
out,
len,
BinaryOp::Add,
BinaryOp::Mul,
|x, y, z| (x + y) * z,
);
}
/// Fused multiply-add scalar: `out[i] = a[i] * scale + bias`
///
/// # Safety
/// - `a` and `out` must be valid pointers to `len` elements
#[inline]
pub unsafe fn fused_mul_add_scalar_kernel<T: Element>(
a: *const T,
scale: f64,
bias: f64,
out: *mut T,
len: usize,
) {
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
{
use super::simd::fused_elementwise;
use crate::dtype::DType;
match T::DTYPE {
DType::F32 => {
fused_elementwise::fused_mul_add_scalar_f32_kernel(
a as *const f32,
scale as f32,
bias as f32,
out as *mut f32,
len,
);
return;
}
DType::F64 => {
fused_elementwise::fused_mul_add_scalar_f64_kernel(
a as *const f64,
scale,
bias,
out as *mut f64,
len,
);
return;
}
#[cfg(feature = "f16")]
DType::F16 => {
fused_elementwise::fused_mul_add_scalar_f32_f16(
a as *const half::f16,
scale as f32,
bias as f32,
out as *mut half::f16,
len,
);
return;
}
#[cfg(feature = "f16")]
DType::BF16 => {
fused_elementwise::fused_mul_add_scalar_f32_bf16(
a as *const half::bf16,
scale as f32,
bias as f32,
out as *mut half::bf16,
len,
);
return;
}
_ => {}
}
}
// Scalar fallback
let a_slice = std::slice::from_raw_parts(a, len);
let out_slice = std::slice::from_raw_parts_mut(out, len);
// An integer row must equal `add_scalar(mul_scalar(a, scale), bias)`, which
// wraps at each step. Computing in f64 and converting once saturates
// instead, so the fused and unfused paths would disagree exactly where the
// product leaves the dtype.
if T::DTYPE.is_int() {
let scale_t = T::from_f64(scale);
let bias_t = T::from_f64(bias);
for i in 0..len {
// `None` only for a non-integer dtype, which the guard excluded.
if let Some(v) =
binary_int_fused_elem(BinaryOp::Mul, BinaryOp::Add, a_slice[i], scale_t, bias_t)
{
out_slice[i] = v;
}
}
return;
}
for i in 0..len {
let val = a_slice[i].to_f64();
out_slice[i] = T::from_f64(val * scale + bias);
}
}
/// Generic scalar fallback for ternary fused ops.
///
/// `op1` and `op2` name the same computation as `op` for the integer dtypes,
/// which take the wrapping element path instead of `op`'s f64 one: a fused
/// integer op must answer exactly what the unfused sequence answers, and that
/// sequence wraps at each step rather than saturating once at the end.
#[inline]
unsafe fn fused_ternary_scalar<T: Element, F: Fn(f64, f64, f64) -> f64>(
a: *const T,
b: *const T,
c: *const T,
out: *mut T,
len: usize,
op1: BinaryOp,
op2: BinaryOp,
op: F,
) {
let a_slice = std::slice::from_raw_parts(a, len);
let b_slice = std::slice::from_raw_parts(b, len);
let c_slice = std::slice::from_raw_parts(c, len);
let out_slice = std::slice::from_raw_parts_mut(out, len);
if T::DTYPE.is_int() {
for i in 0..len {
// `None` only for a non-integer dtype, which the guard excluded.
if let Some(v) = binary_int_fused_elem(op1, op2, a_slice[i], b_slice[i], c_slice[i]) {
out_slice[i] = v;
}
}
return;
}
for i in 0..len {
let x = a_slice[i].to_f64();
let y = b_slice[i].to_f64();
let z = c_slice[i].to_f64();
out_slice[i] = T::from_f64(op(x, y, z));
}
}