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
use super::{tile_loop_generic, validate_gemm_sizes, TiledGemm};
#[cfg(target_arch = "x86_64")]
use crate::cpu::{AmxSupport, Avx512Support};
use eunomia::{Bf16, F32};
use hermes_simd_core::view::{SimdError, TileMatrixMultiply};
use hermes_simd_intrinsics::Scalar;
#[cfg(target_arch = "x86_64")]
use hermes_simd_intrinsics::{AmxBf16, Avx512};
impl TiledGemm<Bf16, Bf16, F32> for (Bf16, Bf16, F32) {
#[inline]
unsafe fn dispatch_tile_matmul(
c: *mut F32,
c_stride: usize,
a: *const Bf16,
a_stride: usize,
b: *const Bf16,
b_stride: usize,
) {
#[cfg(target_arch = "x86_64")]
{
if <Bf16 as AmxSupport>::has_amx() && hermes_simd_intrinsics::AmxSession::is_active() {
return <AmxBf16 as TileMatrixMultiply<
Bf16,
Bf16,
F32,
AmxBf16,
AmxBf16,
16,
16,
32,
>>::tile_matmul(c, c_stride, a, a_stride, b, b_stride);
}
if <Bf16 as Avx512Support>::has_avx512() {
return <Avx512 as TileMatrixMultiply<
Bf16,
Bf16,
F32,
Avx512,
Avx512,
16,
16,
32,
>>::tile_matmul(c, c_stride, a, a_stride, b, b_stride);
}
}
<Scalar as TileMatrixMultiply<Bf16, Bf16, F32, Scalar, Scalar, 16, 16, 32>>::tile_matmul(
c, c_stride, a, a_stride, b, b_stride,
);
}
#[inline]
unsafe fn gemm(
m: usize,
n: usize,
k: usize,
a: &[Bf16],
a_stride: usize,
b: &[Bf16],
b_stride: usize,
c: &mut [F32],
c_stride: usize,
) -> Result<(), SimdError> {
validate_gemm_sizes(
a.len(),
b.len(),
c.len(),
m,
n,
k,
a_stride,
b_stride,
c_stride,
)?;
#[cfg(target_arch = "x86_64")]
{
let decision = crate::dispatcher::AdaptiveDispatcher::select_backend(
m,
n,
k,
a.as_ptr(),
a.len(),
b.as_ptr(),
b.len(),
);
match decision {
crate::dispatcher::DispatchDecision::Amx => {
<AmxBf16 as hermes_simd_intrinsics::x86_64::amx::AmxGemm<
Bf16,
Bf16,
F32,
>>::amx_gemm(
m,
n,
k,
a.as_ptr(),
a_stride,
b.as_ptr(),
b_stride,
c.as_mut_ptr(),
c_stride,
);
return Ok(());
}
crate::dispatcher::DispatchDecision::Avx512 => {
tile_loop_generic::<Bf16, Bf16, F32, Avx512, 16, 16, 32>(
m,
n,
k,
a.as_ptr(),
a_stride,
b.as_ptr(),
b_stride,
c.as_mut_ptr(),
c_stride,
);
let amx_m_bound = (m / 16) * 16;
let amx_n_bound = (n / 16) * 16;
let amx_k_bound = (k / 32) * 32;
for r in 0..m {
for col in 0..n {
if r >= amx_m_bound || col >= amx_n_bound {
let mut sum = 0.0f32;
for kk in 0..k {
sum += a[r * a_stride + kk].to_f32()
* b[kk * b_stride + col].to_f32();
}
c[r * c_stride + col] = F32(c[r * c_stride + col].0 + sum);
} else if amx_k_bound < k {
let mut sum = 0.0f32;
for kk in amx_k_bound..k {
sum += a[r * a_stride + kk].to_f32()
* b[kk * b_stride + col].to_f32();
}
c[r * c_stride + col] = F32(c[r * c_stride + col].0 + sum);
}
}
}
return Ok(());
}
crate::dispatcher::DispatchDecision::AvxVnni
| crate::dispatcher::DispatchDecision::Scalar => {}
}
}
tile_loop_generic::<Bf16, Bf16, F32, Scalar, 16, 16, 32>(
m,
n,
k,
a.as_ptr(),
a_stride,
b.as_ptr(),
b_stride,
c.as_mut_ptr(),
c_stride,
);
let amx_m_bound = (m / 16) * 16;
let amx_n_bound = (n / 16) * 16;
let amx_k_bound = (k / 32) * 32;
for r in 0..m {
for col in 0..n {
if r >= amx_m_bound || col >= amx_n_bound {
let mut sum = 0.0f32;
for kk in 0..k {
sum += a[r * a_stride + kk].to_f32() * b[kk * b_stride + col].to_f32();
}
c[r * c_stride + col] = F32(c[r * c_stride + col].0 + sum);
} else if amx_k_bound < k {
let mut sum = 0.0f32;
for kk in amx_k_bound..k {
sum += a[r * a_stride + kk].to_f32() * b[kk * b_stride + col].to_f32();
}
c[r * c_stride + col] = F32(c[r * c_stride + col].0 + sum);
}
}
}
Ok(())
}
}