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/transformer/FeedForwardBlockTest.lvt

namespace Livt.ML.Tests.Transformer

using Livt.ML.Transformer

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

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

	/**
	 * Checks that zero weights produce zero output.
	 */
	@Test
	fn ZeroWeightsGiveZeroOutput()
	{
		ffn.ClearWeights()

		var input: int[8] = [1, 2, 3, 4, 5, 6, 7, 8]

		ffn.Compute(input)

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

	/**
	 * Checks that identity weights preserve the expected ReLU activation.
	 */
	@Test
	fn IdentityWeightsApplyRelu()
	{
		ffn.ClearWeights()
		ffn.SetIdentityWeights()

		var input: int[8] = [-5, 2, 0, 4, -1, 6, 7, 8]

		ffn.Compute(input)

		assert ffn.GetOutput(0) == 0
		assert ffn.GetOutput(1) == 2
		assert ffn.GetOutput(3) == 4
		assert ffn.GetOutput(4) == 0
		assert ffn.GetOutput(7) == 8
	}

	/**
	 * Checks that the component can project hidden features.
	 */
	@Test
	fn CanProjectHiddenFeatures()
	{
		ffn.ClearWeights()

		ffn.SetHiddenRowValues(0, 1, 1, 0, 0, 0, 0, 0, 0)
		ffn.SetHiddenRowValues(99, 9, 9, 9, 9, 9, 9, 9, 9)
		ffn.SetOutputRowValues(0, 3, 0, 0, 0, 0, 0, 0, 0)
		ffn.SetOutputRowValues(99, 9, 9, 9, 9, 9, 9, 9, 9)

		var input: int[8] = [4, 5, 0, 0, 0, 0, 0, 0]

		ffn.Compute(input)

		assert ffn.GetOutput(0) == 27
		assert ffn.GetOutput(1) == 0
		assert ffn.GetOutput(8) == 0
	}
}