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/linear/Linear8x8Test.lvt

namespace Livt.ML.Tests.Linear

using Livt.ML.Linear

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

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

	/**
	 * Configures the eight weight rows as an identity matrix without changing the test input.
	 */
	fn LoadIdentity()
	{
		layer.SetRowValues(0, 1, 0, 0, 0, 0, 0, 0, 0)
		layer.SetRowValues(1, 0, 1, 0, 0, 0, 0, 0, 0)
		layer.SetRowValues(2, 0, 0, 1, 0, 0, 0, 0, 0)
		layer.SetRowValues(3, 0, 0, 0, 1, 0, 0, 0, 0)
		layer.SetRowValues(4, 0, 0, 0, 0, 1, 0, 0, 0)
		layer.SetRowValues(5, 0, 0, 0, 0, 0, 1, 0, 0)
		layer.SetRowValues(6, 0, 0, 0, 0, 0, 0, 1, 0)
		layer.SetRowValues(7, 0, 0, 0, 0, 0, 0, 0, 1)
	}

	/**
	 * Checks that zero weights produce zero output.
	 */
	@Test
	fn ZeroWeightsGiveZeroOutput()
	{
		var input: int[8] = [1, 2, 3, 4, 5, 6, 7, 8]

		layer.Compute(input)

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

	/**
	 * Checks that identity rows pass input values through unchanged.
	 */
	@Test
	fn IdentityRowsPassInputsThrough()
	{
		LoadIdentity()

		var input: int[8] = [11, 22, 33, 44, 55, 66, 77, 88]

		layer.Compute(input)

		assert layer.GetOutput(0) == 11
		assert layer.GetOutput(3) == 44
		assert layer.GetOutput(7) == 88
	}

	/**
	 * Checks that the component supports array row setter.
	 */
	@Test
	fn SupportsArrayRowSetter()
	{
		var weights: int[8] = [2, 0, 0, 0, 0, 0, 0, 0]
		var input: int[8] = [9, 0, 0, 0, 0, 0, 0, 0]

		layer.SetRow(0, weights)
		layer.SetRow(99, weights)
		layer.Compute(input)

		assert layer.GetOutput(0) == 18
		assert layer.GetOutput(8) == 0
	}

	/**
	 * Checks that the component saturates outputs.
	 */
	@Test
	fn SaturatesOutputs()
	{
		layer.SetRowValues(0, 100, 100, 100, 100, 100, 100, 100, 100)

		var input: int[8] = [100, 100, 100, 100, 100, 100, 100, 100]

		layer.Compute(input)

		assert layer.GetOutput(0) == DotProduct.ACC_MAX
	}
}