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/activation/SoftmaxApproxTest.lvt

namespace Livt.ML.Tests.Activation

using Livt.ML.Activation

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

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

	/**
	 * Checks that Compute s uniform distribution.
	 */
	@Test
	fn ComputesUniformDistribution()
	{
		var input: int[8]

		softmax.Compute(input)

		assert softmax.GetOutput(0) == 32
		assert softmax.GetOutput(7) == 32
	}

	/**
	 * Checks that Compute s only configured active values.
	 */
	@Test
	fn ComputesOnlyConfiguredActiveValues()
	{
		var input: int[8]
		softmax.ComputeCount(input, 2)

		assert softmax.GetOutput(0) == 128
		assert softmax.GetOutput(1) == 128
		assert softmax.GetOutput(2) == 0
		softmax.ComputeCount(input, 0)
		assert softmax.GetOutput(0) == 0
	}

	/**
	 * Checks that Compute s dominant element.
	 */
	@Test
	fn ComputesDominantElement()
	{
		var input: int[8] = [6, 3, 0, 0, 0, 0, 0, 0]

		softmax.Compute(input)

		assert softmax.GetOutput(0) == 240
		assert softmax.GetOutput(1) == 11
		assert softmax.GetOutput(2) == 0
		assert softmax.GetOutput(8) == 0
	}

	/**
	 * Checks that Clamp s high input.
	 */
	@Test
	fn ClampsHighInput()
	{
		var input: int[8] = [20, 0, 0, 0, 0, 0, 0, 0]

		softmax.Compute(input)

		assert softmax.GetOutput(0) == 255
		assert softmax.GetOutput(1) == 0
	}
}