concurrency_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //go:build integration
  2. package integration
  3. import (
  4. "context"
  5. "log/slog"
  6. "os"
  7. "strconv"
  8. "sync"
  9. "testing"
  10. "time"
  11. "github.com/ollama/ollama/api"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func TestMultiModelConcurrency(t *testing.T) {
  15. var (
  16. req = [2]api.GenerateRequest{
  17. {
  18. Model: "orca-mini",
  19. Prompt: "why is the ocean blue?",
  20. Stream: &stream,
  21. Options: map[string]interface{}{
  22. "seed": 42,
  23. "temperature": 0.0,
  24. },
  25. }, {
  26. Model: "tinydolphin",
  27. Prompt: "what is the origin of the us thanksgiving holiday?",
  28. Stream: &stream,
  29. Options: map[string]interface{}{
  30. "seed": 42,
  31. "temperature": 0.0,
  32. },
  33. },
  34. }
  35. resp = [2][]string{
  36. []string{"sunlight"},
  37. []string{"england", "english", "massachusetts", "pilgrims"},
  38. }
  39. )
  40. var wg sync.WaitGroup
  41. wg.Add(len(req))
  42. ctx, cancel := context.WithTimeout(context.Background(), time.Second*120)
  43. defer cancel()
  44. for i := 0; i < len(req); i++ {
  45. go func(i int) {
  46. defer wg.Done()
  47. GenerateTestHelper(ctx, t, req[i], resp[i])
  48. }(i)
  49. }
  50. wg.Wait()
  51. }
  52. func TestIntegrationConcurrentPredictOrcaMini(t *testing.T) {
  53. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) // GTX 750 2G card takes ~9 minutes
  54. defer cancel()
  55. client, _, cleanup := InitServerConnection(ctx, t)
  56. defer cleanup()
  57. req, resp := GenerateRequests()
  58. // Get the server running (if applicable) warm the model up with a single initial request
  59. DoGenerate(ctx, t, client, req[0], resp[0], 60*time.Second, 5*time.Second)
  60. var wg sync.WaitGroup
  61. wg.Add(len(req))
  62. for i := 0; i < len(req); i++ {
  63. go func(i int) {
  64. defer wg.Done()
  65. for j := 0; j < 5; j++ {
  66. slog.Info("Starting", "req", i, "iter", j)
  67. // On slower GPUs it can take a while to process the 4 concurrent requests
  68. // so we allow a much longer initial timeout
  69. DoGenerate(ctx, t, client, req[i], resp[i], 90*time.Second, 5*time.Second)
  70. }
  71. }(i)
  72. }
  73. wg.Wait()
  74. }
  75. // Stress the system if we know how much VRAM it has, and attempt to load more models than will fit
  76. func TestMultiModelStress(t *testing.T) {
  77. vram := os.Getenv("OLLAMA_MAX_VRAM")
  78. if vram == "" {
  79. t.Skip("OLLAMA_MAX_VRAM not specified, can't pick the right models for the stress test")
  80. }
  81. max, err := strconv.ParseUint(vram, 10, 64)
  82. require.NoError(t, err)
  83. const MB = uint64(1024 * 1024)
  84. type model struct {
  85. name string
  86. size uint64 // Approximate amount of VRAM they typically use when fully loaded in VRAM
  87. }
  88. smallModels := []model{
  89. {
  90. name: "orca-mini",
  91. size: 2992 * MB,
  92. },
  93. {
  94. name: "phi",
  95. size: 2616 * MB,
  96. },
  97. {
  98. name: "gemma:2b",
  99. size: 2364 * MB,
  100. },
  101. {
  102. name: "stable-code:3b",
  103. size: 2608 * MB,
  104. },
  105. {
  106. name: "starcoder2:3b",
  107. size: 2166 * MB,
  108. },
  109. }
  110. mediumModels := []model{
  111. {
  112. name: "llama2",
  113. size: 5118 * MB,
  114. },
  115. {
  116. name: "mistral",
  117. size: 4620 * MB,
  118. },
  119. {
  120. name: "orca-mini:7b",
  121. size: 5118 * MB,
  122. },
  123. {
  124. name: "dolphin-mistral",
  125. size: 4620 * MB,
  126. },
  127. {
  128. name: "gemma:7b",
  129. size: 5000 * MB,
  130. },
  131. // TODO - uncomment this once #3565 is merged and this is rebased on it
  132. // {
  133. // name: "codellama:7b",
  134. // size: 5118 * MB,
  135. // },
  136. }
  137. // These seem to be too slow to be useful...
  138. // largeModels := []model{
  139. // {
  140. // name: "llama2:13b",
  141. // size: 7400 * MB,
  142. // },
  143. // {
  144. // name: "codellama:13b",
  145. // size: 7400 * MB,
  146. // },
  147. // {
  148. // name: "orca-mini:13b",
  149. // size: 7400 * MB,
  150. // },
  151. // {
  152. // name: "gemma:7b",
  153. // size: 5000 * MB,
  154. // },
  155. // {
  156. // name: "starcoder2:15b",
  157. // size: 9100 * MB,
  158. // },
  159. // }
  160. var chosenModels []model
  161. switch {
  162. case max < 10000*MB:
  163. slog.Info("selecting small models")
  164. chosenModels = smallModels
  165. // case max < 30000*MB:
  166. default:
  167. slog.Info("selecting medium models")
  168. chosenModels = mediumModels
  169. // default:
  170. // slog.Info("selecting large models")
  171. // chosenModels = largModels
  172. }
  173. req, resp := GenerateRequests()
  174. for i := range req {
  175. if i > len(chosenModels) {
  176. break
  177. }
  178. req[i].Model = chosenModels[i].name
  179. }
  180. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute) // TODO baseline -- 10m too short
  181. defer cancel()
  182. client, _, cleanup := InitServerConnection(ctx, t)
  183. defer cleanup()
  184. // Make sure all the models are pulled before we get started
  185. for _, r := range req {
  186. require.NoError(t, PullIfMissing(ctx, client, r.Model))
  187. }
  188. var wg sync.WaitGroup
  189. consumed := uint64(256 * MB) // Assume some baseline usage
  190. for i := 0; i < len(req); i++ {
  191. // Always get at least 2 models, but dont' overshoot VRAM too much or we'll take too long
  192. if i > 1 && consumed > max {
  193. slog.Info("achieved target vram exhaustion", "count", i, "vramMB", max/1024/1024, "modelsMB", consumed/1024/1024)
  194. break
  195. }
  196. consumed += chosenModels[i].size
  197. slog.Info("target vram", "count", i, "vramMB", max/1024/1024, "modelsMB", consumed/1024/1024)
  198. wg.Add(1)
  199. go func(i int) {
  200. defer wg.Done()
  201. for j := 0; j < 3; j++ {
  202. slog.Info("Starting", "req", i, "iter", j, "model", req[i].Model)
  203. DoGenerate(ctx, t, client, req[i], resp[i], 120*time.Second, 5*time.Second)
  204. }
  205. }(i)
  206. }
  207. wg.Wait()
  208. }