cache_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package ollamarunner
  2. import (
  3. "errors"
  4. "fmt"
  5. "image"
  6. "testing"
  7. "time"
  8. "github.com/ollama/ollama/ml"
  9. "github.com/ollama/ollama/model/input"
  10. )
  11. func TestCountCommon(t *testing.T) {
  12. imgA := image.NewRGBA(image.Rect(0, 0, 100, 100))
  13. imgB := image.NewRGBA(image.Rect(0, 0, 50, 50))
  14. imgC := image.NewRGBA(image.Rect(50, 50, 100, 100))
  15. tests := []struct {
  16. name string
  17. t1 []input.Input
  18. t2 []input.Input
  19. expected int32
  20. }{
  21. {
  22. name: "Equal",
  23. t1: []input.Input{{Token: 1}, {Token: 2}, {Token: 3}},
  24. t2: []input.Input{{Token: 1}, {Token: 2}, {Token: 3}},
  25. expected: 3,
  26. },
  27. {
  28. name: "Prefix",
  29. t1: []input.Input{{Token: 1}},
  30. t2: []input.Input{{Token: 1}, {Token: 2}, {Token: 3}},
  31. expected: 1,
  32. },
  33. {
  34. name: "Image Prefix",
  35. t1: []input.Input{{Multimodal: imgA, MultimodalHash: 1}},
  36. t2: []input.Input{{Multimodal: imgA, MultimodalHash: 1}, {Multimodal: imgB, MultimodalHash: 2}, {Multimodal: imgC, MultimodalHash: 3}},
  37. expected: 1,
  38. },
  39. {
  40. name: "Mixed",
  41. t1: []input.Input{{Token: 1}, {Multimodal: imgA, MultimodalHash: 1}},
  42. t2: []input.Input{{Token: 1}, {Multimodal: imgA, MultimodalHash: 1}, {Token: 5}},
  43. expected: 2,
  44. },
  45. {
  46. name: "Mixed, Same Length",
  47. t1: []input.Input{{Token: 1}, {Multimodal: imgA, MultimodalHash: 1}},
  48. t2: []input.Input{{Token: 1}, {Multimodal: imgB, MultimodalHash: 2}},
  49. expected: 1,
  50. },
  51. {
  52. name: "Empty",
  53. t1: []input.Input{},
  54. t2: []input.Input{{Token: 1}, {Token: 2}, {Token: 3}},
  55. expected: 0,
  56. },
  57. {
  58. name: "Both Empty",
  59. t1: []input.Input{},
  60. t2: []input.Input{},
  61. expected: 0,
  62. },
  63. }
  64. for _, tt := range tests {
  65. t.Run(tt.name, func(t *testing.T) {
  66. result := countCommonPrefix(tt.t1, tt.t2)
  67. if result != tt.expected {
  68. t.Errorf("countCommonPrefix(%v, %v): have %v; want %v", tt.t1, tt.t2, result, tt.expected)
  69. }
  70. })
  71. }
  72. }
  73. func TestFindCacheSlot(t *testing.T) {
  74. type expected struct {
  75. result int
  76. len int32
  77. }
  78. tests := []struct {
  79. name string
  80. cache InputCache
  81. prompt []input.Input
  82. longest expected
  83. best expected
  84. }{
  85. {
  86. name: "Empty",
  87. cache: InputCache{slots: []InputCacheSlot{
  88. {
  89. Id: 0,
  90. Inputs: []input.Input{},
  91. InUse: false,
  92. lastUsed: time.Time{},
  93. },
  94. {
  95. Id: 1,
  96. Inputs: []input.Input{},
  97. InUse: false,
  98. lastUsed: time.Time{},
  99. },
  100. }},
  101. prompt: []input.Input{{Token: 1}},
  102. longest: expected{result: 0, len: 0},
  103. best: expected{result: 0, len: 0},
  104. },
  105. {
  106. name: "Extend",
  107. cache: InputCache{slots: []InputCacheSlot{
  108. {
  109. Id: 0,
  110. Inputs: []input.Input{{Token: 1}},
  111. InUse: false,
  112. lastUsed: time.Now().Add(-time.Second),
  113. },
  114. {
  115. Id: 1,
  116. Inputs: []input.Input{{Token: 1}, {Token: 2}},
  117. InUse: false,
  118. lastUsed: time.Now().Add(-2 * time.Second),
  119. },
  120. }},
  121. prompt: []input.Input{{Token: 1}, {Token: 2}},
  122. longest: expected{result: 1, len: 2},
  123. best: expected{result: 1, len: 2},
  124. },
  125. {
  126. name: "New",
  127. cache: InputCache{slots: []InputCacheSlot{
  128. {
  129. Id: 0,
  130. Inputs: []input.Input{{Token: 1}, {Token: 2}},
  131. InUse: false,
  132. lastUsed: time.Now().Add(-time.Second),
  133. },
  134. {
  135. Id: 1,
  136. Inputs: []input.Input{},
  137. InUse: false,
  138. lastUsed: time.Time{},
  139. },
  140. }},
  141. prompt: []input.Input{{Token: 2}},
  142. longest: expected{result: 0, len: 0},
  143. best: expected{result: 1, len: 0},
  144. },
  145. {
  146. name: "Fork",
  147. cache: InputCache{
  148. slots: []InputCacheSlot{
  149. {
  150. Id: 0,
  151. Inputs: []input.Input{{Token: 1}, {Token: 2}},
  152. InUse: false,
  153. lastUsed: time.Now().Add(-time.Second),
  154. },
  155. {
  156. Id: 1,
  157. Inputs: []input.Input{},
  158. InUse: false,
  159. lastUsed: time.Time{},
  160. },
  161. },
  162. },
  163. prompt: []input.Input{{Token: 1}},
  164. longest: expected{result: 0, len: 1},
  165. best: expected{result: 1, len: 1},
  166. },
  167. {
  168. name: "Evict",
  169. cache: InputCache{slots: []InputCacheSlot{
  170. {
  171. Id: 0,
  172. Inputs: []input.Input{{Token: 1}},
  173. InUse: false,
  174. lastUsed: time.Now().Add(-time.Second),
  175. },
  176. {
  177. Id: 1,
  178. Inputs: []input.Input{{Token: 1}, {Token: 2}},
  179. InUse: false,
  180. lastUsed: time.Now().Add(-2 * time.Second),
  181. },
  182. }},
  183. prompt: []input.Input{{Token: 2}, {Token: 3}},
  184. longest: expected{result: 0, len: 0},
  185. best: expected{result: 1, len: 0},
  186. },
  187. {
  188. name: "In use",
  189. cache: InputCache{slots: []InputCacheSlot{
  190. {
  191. Id: 0,
  192. Inputs: []input.Input{{Token: 1}, {Token: 2}},
  193. InUse: true,
  194. lastUsed: time.Now().Add(-time.Second),
  195. },
  196. {
  197. Id: 1,
  198. Inputs: []input.Input{{Token: 1}},
  199. InUse: false,
  200. lastUsed: time.Now().Add(-2 * time.Second),
  201. },
  202. }},
  203. prompt: []input.Input{{Token: 1}, {Token: 2}},
  204. longest: expected{result: 1, len: 1},
  205. best: expected{result: 1, len: 2},
  206. },
  207. }
  208. for _, tt := range tests {
  209. t.Run("Longest-"+tt.name, func(t *testing.T) {
  210. result, resultLen, err := tt.cache.findLongestCacheSlot(tt.prompt)
  211. if err != nil {
  212. t.Errorf("findLongestCacheSlot: err %v", err)
  213. } else if result.Id != tt.longest.result || resultLen != tt.longest.len {
  214. t.Errorf("findLongestCacheSlot: slot have %v, want %v len have %v, want %v",
  215. result.Id, tt.longest.result, resultLen, tt.longest.len)
  216. }
  217. })
  218. }
  219. for _, tt := range tests {
  220. t.Run("Best-"+tt.name, func(t *testing.T) {
  221. result, resultLen, err := tt.cache.findBestCacheSlot(tt.prompt)
  222. if err != nil {
  223. t.Errorf("findBestCacheSlot: err %v", err)
  224. } else if result.Id != tt.best.result || resultLen != tt.best.len {
  225. t.Errorf("findBestCacheSlot: slot have %v, want %v len have %v, want %v",
  226. result.Id, tt.best.result, resultLen, tt.best.len)
  227. }
  228. })
  229. }
  230. }
  231. func TestShiftDiscard(t *testing.T) {
  232. tests := []struct {
  233. name string
  234. numCtx int32
  235. numKeep int32
  236. inputLen int32
  237. expected int32
  238. }{
  239. {
  240. name: "Shift",
  241. numCtx: 2048,
  242. numKeep: 5,
  243. inputLen: 2048,
  244. expected: 1021,
  245. },
  246. {
  247. name: "Max Keep",
  248. numCtx: 2048,
  249. numKeep: 2047,
  250. inputLen: 2048,
  251. expected: 1,
  252. },
  253. {
  254. name: "No Keep",
  255. numCtx: 2048,
  256. numKeep: 0,
  257. inputLen: 2048,
  258. expected: 1024,
  259. },
  260. {
  261. name: "Truncate",
  262. numCtx: 2048,
  263. numKeep: 5,
  264. inputLen: 5000,
  265. expected: 3973,
  266. },
  267. {
  268. name: "Truncate Keep",
  269. numCtx: 2048,
  270. numKeep: 2047,
  271. inputLen: 5000,
  272. expected: 2953,
  273. },
  274. {
  275. name: "No Op",
  276. numCtx: 2048,
  277. numKeep: 5,
  278. inputLen: 512,
  279. expected: 0,
  280. },
  281. }
  282. for _, tt := range tests {
  283. t.Run(tt.name, func(t *testing.T) {
  284. c := InputCache{numCtx: tt.numCtx}
  285. result := c.ShiftDiscard(tt.inputLen, tt.numKeep)
  286. if result != tt.expected {
  287. t.Errorf("shiftDiscard(ctx: %v, keep: %v input: %v): have %v; want %v", tt.numCtx, tt.numKeep, tt.inputLen, result, tt.expected)
  288. }
  289. })
  290. }
  291. }
  292. // Mock implementation of the Cache interface
  293. type mockCache struct {
  294. shouldFail bool
  295. }
  296. // Implement only the methods needed for the test
  297. func (m *mockCache) Remove(seq int, beginIndex, endIndex int32) error {
  298. if m.shouldFail {
  299. return fmt.Errorf("mock cache removal error")
  300. }
  301. return nil
  302. }
  303. // Stub implementations for other interface methods
  304. func (m *mockCache) SetLayer(layer int) {}
  305. func (m *mockCache) Get(ctx ml.Context) (ml.Tensor, ml.Tensor, ml.Tensor) { return nil, nil, nil }
  306. func (m *mockCache) Put(ctx ml.Context, key, value ml.Tensor) {}
  307. func (m *mockCache) Init(backend ml.Backend, dtype ml.DType, capacity int32) {}
  308. func (m *mockCache) Close() {}
  309. func (m *mockCache) StartForward(ctx ml.Context, positions []int32, seqs []int) error { return nil }
  310. func (m *mockCache) CopyPrefix(srcSeq, dstSeq int, len int32) {}
  311. func (m *mockCache) SetConfig(ml.CacheConfig) {}
  312. func TestShiftCacheSlot(t *testing.T) {
  313. tests := []struct {
  314. name string
  315. numCtx int32
  316. inputs []input
  317. numKeep int32
  318. cacheErr bool
  319. wantErr any
  320. wantInputsLen int
  321. }{
  322. {
  323. name: "Normal shift",
  324. numCtx: 10,
  325. inputs: []input{{token: 1}, {token: 2}, {token: 3}, {token: 4}, {token: 5}, {token: 6}, {token: 7}, {token: 8}, {token: 9}, {token: 10}},
  326. numKeep: 2,
  327. cacheErr: false, // No error
  328. wantErr: nil,
  329. wantInputsLen: 6, // After discarding 4 tokens
  330. },
  331. {
  332. name: "Cache removal fails",
  333. numCtx: 10,
  334. inputs: []input{{token: 1}, {token: 2}, {token: 3}, {token: 4}, {token: 5}, {token: 6}, {token: 7}, {token: 8}, {token: 9}, {token: 10}},
  335. numKeep: 2,
  336. cacheErr: true,
  337. wantErr: &ErrReprocessInputs{},
  338. wantInputsLen: 0, // Original inputs should be cleared
  339. },
  340. }
  341. for _, tt := range tests {
  342. t.Run(tt.name, func(t *testing.T) {
  343. mock := &mockCache{shouldFail: tt.cacheErr}
  344. c := InputCache{
  345. numCtx: tt.numCtx,
  346. cache: mock,
  347. }
  348. slot := &InputCacheSlot{
  349. Id: 123,
  350. Inputs: make([]input, len(tt.inputs)),
  351. }
  352. copy(slot.Inputs, tt.inputs)
  353. err := c.ShiftCacheSlot(slot, tt.numKeep)
  354. if tt.wantErr != nil {
  355. if err == nil {
  356. t.Errorf("Expected error but got nil")
  357. return
  358. }
  359. if !errors.As(err, &tt.wantErr) {
  360. t.Errorf("Expected error of type %T but got %T: %v", tt.wantErr, err, err)
  361. }
  362. if errReproc, ok := err.(*ErrReprocessInputs); ok {
  363. if errReproc.SlotId != slot.Id {
  364. t.Errorf("ErrReprocessInputs has wrong SlotId: got %v, want %v", errReproc.SlotId, slot.Id)
  365. }
  366. }
  367. } else if err != nil {
  368. t.Errorf("Unexpected error: %v", err)
  369. }
  370. if len(slot.Inputs) != tt.wantInputsLen {
  371. t.Errorf("Slot inputs length after operation: got %v, want %v", len(slot.Inputs), tt.wantInputsLen)
  372. }
  373. })
  374. }
  375. }