1- //! Traits and methods for CPU-backed Tensors
2-
31pub mod erf;
42pub mod kernels;
53
@@ -38,16 +36,33 @@ trait CpuF16<const ARR: usize> {
3836 unsafe fn from_f32 ( v : f32 ) -> Self :: Unit ;
3937 unsafe fn vec_store ( mem_addr : * mut f16 , a : Self :: Unit ) ;
4038}
41- #[ cfg( not( target_feature = "avx" ) ) ]
42- use half:: bf16;
43- use half:: f16;
39+
40+ #[ allow( unused) ]
41+ trait CpuBF16 < const ARR : usize > {
42+ type Unit ;
43+ type Array ;
44+ const STEP : usize ;
45+ const EPR : usize ;
46+
47+ fn n ( ) -> usize ;
48+ unsafe fn zero ( ) -> Self :: Unit ;
49+ unsafe fn zero_array ( ) -> Self :: Array ;
50+ unsafe fn load ( mem_addr : * const bf16 ) -> Self :: Unit ;
51+ unsafe fn vec_add ( a : Self :: Unit , b : Self :: Unit ) -> Self :: Unit ;
52+ unsafe fn vec_fma ( a : Self :: Unit , b : Self :: Unit , c : Self :: Unit ) -> Self :: Unit ;
53+ unsafe fn vec_reduce ( x : Self :: Array , y : * mut f32 ) ;
54+ unsafe fn from_f32 ( v : f32 ) -> Self :: Unit ;
55+ unsafe fn vec_store ( mem_addr : * mut bf16 , a : Self :: Unit ) ;
56+ }
57+
58+ use half:: { bf16, f16} ;
4459
4560#[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
4661#[ cfg( target_feature = "avx" ) ]
4762pub mod avx;
4863#[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
4964#[ cfg( target_feature = "avx" ) ]
50- pub use avx:: { CurrentCpu , CurrentCpuF16 } ;
65+ pub use avx:: { CurrentCpu , CurrentCpuBF16 , CurrentCpuF16 } ;
5166
5267#[ cfg( target_arch = "wasm32" ) ]
5368#[ cfg( target_feature = "simd128" ) ]
@@ -148,24 +163,24 @@ pub(crate) unsafe fn vec_sum(row: *const f32, b: *mut f32, k: usize) {
148163
149164#[ cfg( target_feature = "avx" ) ]
150165#[ inline( always) ]
151- pub ( crate ) unsafe fn vec_dot_bf16 ( a_row : * const bf16 , b_row : * const bf16 , c : * mut f32 , k : usize ) {
166+ pub ( crate ) unsafe fn vec_dot_f16 ( a_row : * const f16 , b_row : * const f16 , c : * mut f32 , k : usize ) {
152167 let mut sumf = 0.0f32 ;
153- let np = k & !( CurrentCpuBF16 :: STEP - 1 ) ;
168+ let np = k & !( CurrentCpuF16 :: STEP - 1 ) ;
154169
155- let mut sum = CurrentCpuBF16 :: zero_array ( ) ;
156- let mut ax = CurrentCpuBF16 :: zero_array ( ) ;
157- let mut ay = CurrentCpuBF16 :: zero_array ( ) ;
170+ let mut sum = CurrentCpuF16 :: zero_array ( ) ;
171+ let mut ax = CurrentCpuF16 :: zero_array ( ) ;
172+ let mut ay = CurrentCpuF16 :: zero_array ( ) ;
158173
159- for i in ( 0 ..np) . step_by ( CurrentCpuBF16 :: STEP ) {
160- for j in 0 ..CurrentCpuBF16 :: n ( ) {
161- ax[ j] = CurrentCpuBF16 :: load ( a_row. add ( i + j * CurrentCpuBF16 :: EPR ) ) ;
162- ay[ j] = CurrentCpuBF16 :: load ( b_row. add ( i + j * CurrentCpuBF16 :: EPR ) ) ;
174+ for i in ( 0 ..np) . step_by ( CurrentCpuF16 :: STEP ) {
175+ for j in 0 ..CurrentCpuF16 :: n ( ) {
176+ ax[ j] = CurrentCpuF16 :: load ( a_row. add ( i + j * CurrentCpuF16 :: EPR ) ) ;
177+ ay[ j] = CurrentCpuF16 :: load ( b_row. add ( i + j * CurrentCpuF16 :: EPR ) ) ;
163178
164- sum[ j] = CurrentCpuBF16 :: vec_fma ( sum[ j] , ax[ j] , ay[ j] ) ;
179+ sum[ j] = CurrentCpuF16 :: vec_fma ( sum[ j] , ax[ j] , ay[ j] ) ;
165180 }
166181 }
167182
168- CurrentCpuBF16 :: vec_reduce ( sum, & mut sumf) ;
183+ CurrentCpuF16 :: vec_reduce ( sum, & mut sumf) ;
169184
170185 // leftovers
171186 for i in np..k {
@@ -176,24 +191,24 @@ pub(crate) unsafe fn vec_dot_bf16(a_row: *const bf16, b_row: *const bf16, c: *mu
176191
177192#[ cfg( target_feature = "avx" ) ]
178193#[ inline( always) ]
179- pub ( crate ) unsafe fn vec_dot_f16 ( a_row : * const f16 , b_row : * const f16 , c : * mut f32 , k : usize ) {
194+ pub ( crate ) unsafe fn vec_dot_bf16 ( a_row : * const bf16 , b_row : * const bf16 , c : * mut f32 , k : usize ) {
180195 let mut sumf = 0.0f32 ;
181- let np = k & !( CurrentCpuF16 :: STEP - 1 ) ;
196+ let np = k & !( CurrentCpuBF16 :: STEP - 1 ) ;
182197
183- let mut sum = CurrentCpuF16 :: zero_array ( ) ;
184- let mut ax = CurrentCpuF16 :: zero_array ( ) ;
185- let mut ay = CurrentCpuF16 :: zero_array ( ) ;
198+ let mut sum = CurrentCpuBF16 :: zero_array ( ) ;
199+ let mut ax = CurrentCpuBF16 :: zero_array ( ) ;
200+ let mut ay = CurrentCpuBF16 :: zero_array ( ) ;
186201
187- for i in ( 0 ..np) . step_by ( CurrentCpuF16 :: STEP ) {
188- for j in 0 ..CurrentCpuF16 :: n ( ) {
189- ax[ j] = CurrentCpuF16 :: load ( a_row. add ( i + j * CurrentCpuF16 :: EPR ) ) ;
190- ay[ j] = CurrentCpuF16 :: load ( b_row. add ( i + j * CurrentCpuF16 :: EPR ) ) ;
202+ for i in ( 0 ..np) . step_by ( CurrentCpuBF16 :: STEP ) {
203+ for j in 0 ..CurrentCpuBF16 :: n ( ) {
204+ ax[ j] = CurrentCpuBF16 :: load ( a_row. add ( i + j * CurrentCpuBF16 :: EPR ) ) ;
205+ ay[ j] = CurrentCpuBF16 :: load ( b_row. add ( i + j * CurrentCpuBF16 :: EPR ) ) ;
191206
192- sum[ j] = CurrentCpuF16 :: vec_fma ( sum[ j] , ax[ j] , ay[ j] ) ;
207+ sum[ j] = CurrentCpuBF16 :: vec_fma ( sum[ j] , ax[ j] , ay[ j] ) ;
193208 }
194209 }
195210
196- CurrentCpuF16 :: vec_reduce ( sum, & mut sumf) ;
211+ CurrentCpuBF16 :: vec_reduce ( sum, & mut sumf) ;
197212
198213 // leftovers
199214 for i in np..k {
0 commit comments