Livt packages

Livt.ML

Version 1.6.1

Fixed-size quantized inference, tensor storage, attention, and transformer building blocks for Livt.

Published files (132)

tests/attention/KVCacheTest.lvt

namespace Livt.ML.Tests.Attention

using Livt.ML.Attention

/**
 * Exercises KVCache with a shared fixture instance. Each scenario establishes the inputs
 * and state it needs.
 */
@Test
component KVCacheTest
{
	// Shared fixture instances, signals, and observation state.
	cache: KVCache

	/**
	 * Creates the shared fixture instances (KVCache). Tests initialize the state required by
	 * their scenario.
	 */
	new()
	{
		cache = new KVCache()
	}

	/**
	 * Checks the new cache is empty case.
	 */
	@Test
	fn NewCacheIsEmpty()
	{
		assert cache.IsEmpty() == true
		assert cache.IsFull() == false
		assert cache.GetSize() == 0
	}

	/**
	 * Checks that Write s single pair.
	 */
	@Test
	fn WritesSinglePair()
	{
		cache.Clear()

		var k: int[8] = [1, 2, 3, 4, 5, 6, 7, 8]
		var v: int[8] = [10, 20, 30, 40, 50, 60, 70, 80]

		cache.Write(k, v)

		assert cache.GetSize() == 1
		assert cache.IsEmpty() == false
		assert cache.GetKeyElement(0, 0) == 1
		assert cache.GetKeyElement(0, 7) == 8
		assert cache.GetValueElement(0, 0) == 10
		assert cache.GetValueElement(0, 7) == 80
		assert cache.GetKeyElement(-1, 0) == 0
		assert cache.GetKeyElement(1, 0) == 0
		assert cache.GetValueElement(0, -1) == 0
		assert cache.GetValueElement(0, 8) == 0
	}

	/**
	 * Checks the evicts oldest pair when full case.
	 */
	@Test
	fn EvictsOldestPairWhenFull()
	{
		cache.Clear()

		var k: int[8]
		var v: int[8]

		k[0] = 1
		k[7] = 11
		v[0] = 10
		v[7] = 110
		cache.Write(k, v)

		k[0] = 2
		k[7] = 22
		v[0] = 20
		v[7] = 220
		cache.Write(k, v)

		k[0] = 3
		k[7] = 33
		v[0] = 30
		v[7] = 330
		cache.Write(k, v)

		k[0] = 4
		k[7] = 44
		v[0] = 40
		v[7] = 440
		cache.Write(k, v)

		k[0] = 5
		k[7] = 55
		v[0] = 50
		v[7] = 550
		cache.Write(k, v)

		assert cache.GetSize() == 4
		assert cache.IsFull() == true
		assert cache.GetKeyElement(0, 0) == 2
		assert cache.GetKeyElement(0, 7) == 22
		assert cache.GetKeyElement(3, 0) == 5
		assert cache.GetKeyElement(3, 7) == 55
		assert cache.GetValueElement(0, 0) == 20
		assert cache.GetValueElement(0, 7) == 220
		assert cache.GetValueElement(3, 0) == 50
		assert cache.GetValueElement(3, 7) == 550
	}

	/**
	 * Checks that Clear s cache.
	 */
	@Test
	fn ClearsCache()
	{
		var k: int[8] = [1, 0, 0, 0, 0, 0, 0, 0]
		var v: int[8] = [10, 0, 0, 0, 0, 0, 0, 0]

		cache.Write(k, v)
		cache.Clear()

		assert cache.IsEmpty() == true
		assert cache.GetSize() == 0
	}
}