cpufeat.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package cpu
  2. import "golang.org/x/sys/cpu"
  3. // Features captures the SIMD capabilities we care about for dispatching
  4. // CPU kernels. Values are populated once at init using the runtime cpuid
  5. // information from x/sys/cpu.
  6. type Features struct {
  7. FMA bool
  8. AVX2 bool
  9. AVX512F bool
  10. AVX512DQ bool
  11. AVX512BW bool
  12. AVX512VL bool
  13. AVX512VNNI bool
  14. AMXTile bool
  15. AMXInt8 bool
  16. AMXBF16 bool
  17. }
  18. var detected Features
  19. func init() {
  20. detected = Features{
  21. FMA: cpu.X86.HasFMA,
  22. AVX2: cpu.X86.HasAVX2,
  23. AVX512F: cpu.X86.HasAVX512F,
  24. AVX512DQ: cpu.X86.HasAVX512DQ,
  25. AVX512BW: cpu.X86.HasAVX512BW,
  26. AVX512VL: cpu.X86.HasAVX512VL,
  27. AVX512VNNI: cpu.X86.HasAVX512VNNI,
  28. AMXTile: cpu.X86.HasAMXTile,
  29. AMXInt8: cpu.X86.HasAMXInt8,
  30. AMXBF16: cpu.X86.HasAMXBF16,
  31. }
  32. }
  33. // CPUFeatures returns the detected SIMD feature set.
  34. func CPUFeatures() Features {
  35. return detected
  36. }
  37. // SupportsAVX512 reports whether the CPU supports the AVX-512 subset we use.
  38. // We require F, DQ, BW and VL to match ggml-style kernels.
  39. func SupportsAVX512() bool {
  40. return detected.FMA && detected.AVX512F && detected.AVX512DQ && detected.AVX512BW && detected.AVX512VL
  41. }
  42. // SupportsAVX2 reports whether AVX2 is available.
  43. func SupportsAVX2() bool {
  44. return detected.FMA && detected.AVX2
  45. }
  46. // SupportsAVX512VNNI reports whether the CPU supports AVX-512 VNNI instructions.
  47. func SupportsAVX512VNNI() bool {
  48. return SupportsAVX512() && detected.AVX512VNNI
  49. }
  50. // SupportsAMXInt8 reports whether the CPU supports AMX INT8 tile ops.
  51. func SupportsAMXInt8() bool {
  52. return detected.AMXTile && detected.AMXInt8
  53. }
  54. // SupportsAMXBF16 reports whether the CPU supports AMX BF16 tile ops.
  55. func SupportsAMXBF16() bool {
  56. return detected.AMXTile && detected.AMXBF16
  57. }
  58. // SIMDLevel represents the best available SIMD tier.
  59. type SIMDLevel int
  60. const (
  61. SIMDNone SIMDLevel = iota
  62. SIMDAVX2
  63. SIMDAVX512
  64. )
  65. // BestSIMD returns the highest SIMD level supported (AVX-512 over AVX2).
  66. func BestSIMD() SIMDLevel {
  67. switch {
  68. case SupportsAVX512():
  69. return SIMDAVX512
  70. case SupportsAVX2():
  71. return SIMDAVX2
  72. default:
  73. return SIMDNone
  74. }
  75. }