raskell-0.0.0.2: RASP-L in Haskell
Safe HaskellSafe-Inferred
LanguageHaskell2010

RaskellCore

Description

This module provides the core of the RASP-L language.

It is based on Listing 2 of "What Algorithms Can Transformers Learn", https://arxiv.org/abs/2310.16028, by Zhou et al.

Synopsis

Types

Most of these types are merely aliases.

Tokens and Sequences

type Token = Int8 Source #

A Token in a Sequence is a small integer. RASP-L uses Int8 to ensure all maps of type Token -> Token are learnable.

type Sequence = [Token] Source #

A Sequence is a list of Tokens.

type Keys = Sequence Source #

A collection of keys is a list of Tokens.

type Queries = Sequence Source #

A collection of queries is a list of Tokens.

type Values = Sequence Source #

A collection of values is a list of Tokens.

Predicates and Selectors

type Predicate = Token -> Token -> Bool Source #

We can compare Keys and Queries to determine if they match.

type Selector = [BoolSequence] Source #

The equivalents of "attention maps" are collections of Boolean sequences.

type BoolSequence = [Bool] Source #

Internally, we sometimes need to operate on collections of Bools.

Aggregation

type Aggregator = Selector -> Values -> Sequence Source #

Type alias for "fully-specified" aggregators that are ready to aggregate a sequence of values with a selector.

data AggregationType Source #

Enum for the three methods for aggregating selected values

Constructors

Min 
Mean 
Max 

Functions

Key-Query-Value lookup

kqv Source #

Arguments

:: Token

Filler token used in aggregation

-> AggregationType

Type of aggregation (Min, Mean, Max)

-> Keys

Sequence of keys

-> Queries

Sequence of queries

-> Predicate

A boolean predicate that determines whether a key and query match

-> Values

Sequence of values

-> Sequence

The output sequence

Performs a key-query-value lookup operation and aggregates over values.

Given a filler token, an aggregation type, two sequences (keys and queries), and a predicate, it returns a processed sequence. It first selects elements based on the predicate and then aggregates them.

Roughly matches the attention layer of a Transformer.

maxKQV :: Keys -> Queries -> Predicate -> Values -> Sequence Source #

Performs Key-Query-Value lookup with maximum aggregation of values.

minKQV :: Keys -> Queries -> Predicate -> Values -> Sequence Source #

Performs Key-Query-Value lookup with minimum aggregation of values.

selectCausal :: Keys -> Queries -> Predicate -> Selector Source #

Compares pairs of elements from sequences with a predicate subject to a causal constraint.

filledWith :: Sequence -> Token -> Sequence Source #

Creates a matched-length constant sequence with the provided token.

indicesOf :: Sequence -> Sequence Source #

Extracts the indices of the elements in a sequence.

Aggregation

aggregate :: AggregationType -> Token -> Aggregator Source #

Aggregates values with some aggregation, filling in with a default token.

aggrMax :: Token -> Aggregator Source #

Aggregates values by selecting the largest value.

aggrMean :: Token -> Aggregator Source #

Aggregates values by taking the mean.

aggrMin :: Token -> Aggregator Source #

Aggregates values by selecting the smallest value.

Selection

selWidth :: Selector -> Sequence Source #

Computes the "width", or number of nonzero entries, of the rows of a Selector.

Compatibility

These functions are provided for closer compatibility with the original Python implementation of RASP-L.

tokMap :: (Token -> Token) -> Sequence -> Sequence Source #

Applies an elementwise operation to a sequence of tokens.

Roughly matches the MLP layer in a Transformer. Alias for map.

seqMap :: (Token -> Token -> Token) -> Sequence -> Sequence -> Sequence Source #

Applies an elementwise operation for pairs of tokens on a pair of sequences. Alias for zipWith.

full :: Sequence -> Token -> Sequence Source #

Creates a sequence of the same length as the provided sequence filled with the provided token. Alias for filledWith.

indices :: Sequence -> Sequence Source #

Extracts the indices of the elements in a sequence. Alias for indicesOf.

aggr :: AggregationType -> Token -> Aggregator Source #

Creates an aggregator with a given aggregation type. Alias for aggregate.

select Source #

Arguments

:: Bool

Whether to use causal selection

-> Keys

A collection of Keys to check against Queries

-> Queries

A collection of Queries to check against Keys

-> Predicate

A boolean predicate that determines whether a key and query match

-> Selector

A collection of boolean sequences indicating which pairs of Keys and Queries match

Produces a selector indicating which pairs of Keys and Queries match.