gpu_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package gpu
  2. import (
  3. "runtime"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestBasicGetGPUInfo(t *testing.T) {
  9. info := GetGPUInfo()
  10. assert.NotEmpty(t, len(info))
  11. assert.Contains(t, "cuda rocm cpu metal", info[0].Library)
  12. if info[0].Library != "cpu" {
  13. assert.Greater(t, info[0].TotalMemory, uint64(0))
  14. assert.Greater(t, info[0].FreeMemory, uint64(0))
  15. }
  16. }
  17. func TestCPUMemInfo(t *testing.T) {
  18. info, err := GetCPUMem()
  19. require.NoError(t, err)
  20. switch runtime.GOOS {
  21. case "darwin":
  22. t.Skip("CPU memory not populated on darwin")
  23. case "linux", "windows":
  24. assert.Greater(t, info.TotalMemory, uint64(0))
  25. assert.Greater(t, info.FreeMemory, uint64(0))
  26. default:
  27. return
  28. }
  29. }
  30. func TestByLibrary(t *testing.T) {
  31. type testCase struct {
  32. input []GpuInfo
  33. expect int
  34. }
  35. testCases := map[string]*testCase{
  36. "empty": {input: []GpuInfo{}, expect: 0},
  37. "cpu": {input: []GpuInfo{{Library: "cpu"}}, expect: 1},
  38. "cpu + GPU": {input: []GpuInfo{{Library: "cpu"}, {Library: "cuda"}}, expect: 2},
  39. "cpu + 2 GPU no variant": {input: []GpuInfo{{Library: "cpu"}, {Library: "cuda"}, {Library: "cuda"}}, expect: 2},
  40. "cpu + 2 GPU same variant": {input: []GpuInfo{{Library: "cpu"}, {Library: "cuda", Variant: "v11"}, {Library: "cuda", Variant: "v11"}}, expect: 2},
  41. "cpu + 2 GPU diff variant": {input: []GpuInfo{{Library: "cpu"}, {Library: "cuda", Variant: "v11"}, {Library: "cuda", Variant: "v12"}}, expect: 3},
  42. }
  43. for k, v := range testCases {
  44. t.Run(k, func(t *testing.T) {
  45. resp := (GpuInfoList)(v.input).ByLibrary()
  46. if len(resp) != v.expect {
  47. t.Fatalf("expected length %d, got %d => %+v", v.expect, len(resp), resp)
  48. }
  49. })
  50. }
  51. }
  52. // TODO - add some logic to figure out card type through other means and actually verify we got back what we expected