Livt packages

Livt.ML

Version 1.6.1

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

Published files (132)

src/linear/Int8LinearAccumulator.lvt

namespace Livt.ML.Linear

using Livt.Math.Arithmetic

/**
 * Storage-free signed-int8 linear-output accumulator with int32 accumulation.
 * Callers stream one activation/weight pair at a time, allowing weights to
 * reside in constants, Livt.IO RAM, or an external memory controller.
 */
component Int8LinearAccumulator
{
	// Owned arithmetic worker used by serialized operations.
	mac: MacInt32

	// Configured coefficients and numeric scaling parameters.
	inputZeroPoint: int
	weightZeroPoint: int

	/**
	 * Creates the owned int32 MAC. Begin configures the bias and zero points for each output
	 * reduction.
	 */
	new()
	{
		this.mac = new MacInt32()
	}

	/**
	 * Starts one output with its bias and quantization zero points.
	 */
	public fn Begin(bias: int, inputZeroPointValue: int, weightZeroPointValue: int)
	{
		this.mac.Reset()
		this.mac.Add(bias)
		inputZeroPoint = inputZeroPointValue
		weightZeroPoint = weightZeroPointValue
	}

	/**
	 * Adds one centered activation-by-weight product.
	 */
	public fn Accumulate(activation: int, weight: int)
	{
		this.mac.Accumulate(activation - inputZeroPoint, weight - weightZeroPoint)
	}

	/**
	 * Returns the signed int32 accumulator.
	 */
	public fn GetAccumulator() int
	{
		return this.mac.GetResult()
	}
}