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/ScaledDotProductAttentionTest.lvt

namespace Livt.ML.Tests.Attention

using Livt.ML.Attention

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

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

	/**
	 * Checks that Compute s empty cache as zero.
	 */
	@Test
	fn ComputesEmptyCacheAsZero()
	{
		var query: int[8]

		attn.Compute(query)

		assert attn.GetOutput(0) == 0
		assert attn.GetOutput(7) == 0
	}

	/**
	 * Checks that Compute s single dominant write.
	 */
	@Test
	fn ComputesSingleDominantWrite()
	{
		attn.ClearCache()

		var key: int[8] = [4, 4, 4, 4, 4, 4, 4, 4]
		var val: int[8] = [10, 20, 30, 40, 50, 60, 70, 80]
		var query: int[8] = [4, 4, 4, 4, 4, 4, 4, 4]

		attn.Write(key, val)
		attn.Compute(query)

		assert attn.GetOutput(0) == 10
		assert attn.GetOutput(1) == 20
		assert attn.GetOutput(2) == 30
	}

	/**
	 * Checks that Compute s two uniform scores.
	 */
	@Test
	fn ComputesTwoUniformScores()
	{
		attn.ClearCache()

		var key0: int[8] = [1, 0, 0, 0, 0, 0, 0, 0]
		var val0: int[8] = [8, 0, 0, 0, 0, 0, 0, 0]
		var key1: int[8] = [0, 1, 0, 0, 0, 0, 0, 0]
		var val1: int[8] = [0, 16, 0, 0, 0, 0, 0, 0]
		var query: int[8] = [1, 0, 0, 0, 0, 0, 0, 0]

		attn.Write(key0, val0)
		attn.Write(key1, val1)
		attn.Compute(query)

		assert attn.GetOutput(0) == 4
		assert attn.GetOutput(1) == 8
		assert attn.GetOutput(2) == 0
	}

	/**
	 * Checks the reports cache size and output bounds case.
	 */
	@Test
	fn ReportsCacheSizeAndOutputBounds()
	{
		attn.ClearCache()

		assert attn.GetCacheSize() == 0

		var key: int[8]
		var val: int[8]
		var query: int[8]

		attn.Write(key, val)
		attn.Compute(query)

		assert attn.GetCacheSize() == 1
		assert attn.GetOutput(7) == 0
		assert attn.GetOutput(8) == 0
	}
}