gaia.core.universal_coalgebras module

Universal Coalgebras for GAIA Framework

Implements Section 3 from GAIA paper: “Backpropagation as an Endofunctor: Generative AI using Universal Coalgebras”

THEORETICAL FOUNDATIONS: - Definition 7: F-coalgebra (A, α) with structure map α: A → F(A) - Definition 9: Coalgebra homomorphisms with commutative diagrams - Definition 10: Bisimulations between coalgebras - Lambek’s Theorem: Final coalgebras as fixed points - Applications to generative AI, LLMs, diffusion models

This module provides the categorical foundation for all generative AI in GAIA.

class gaia.core.universal_coalgebras.Endofunctor[source]

Bases: ABC, Generic[A]

Abstract base class for endofunctors F: C → C

From (MAHADEVAN,2024) Definition 7: Endofunctor required for F-coalgebras

abstractmethod apply(obj)[source]

Apply functor to object: F(A) → F(A)

abstractmethod fmap(morphism)[source]

Apply functor to morphism: F(f: A → B) → F(A) → F(B)

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

class gaia.core.universal_coalgebras.PowersetFunctor[source]

Bases: Endofunctor[set]

Powerset functor F: S ⇒ P(S) From (MAHADEVAN,2024) Section 3.1

Models context-free grammars, finite state machines, and basic generative models.

apply(obj)[source]

Return powerset P(S) = {A | A ⊆ S}

fmap(morphism)[source]

Apply morphism to each element of powerset

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

class gaia.core.universal_coalgebras.StreamFunctor[source]

Bases: Endofunctor[List]

Stream functor Str: Set → Set, Str(X) = ℕ × X from GAIA paper

Models infinite data streams for generative AI (LLMs, sequence models).

apply(obj)[source]

Return (index, stream) pair

fmap(morphism)[source]

Apply morphism to stream component

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

class gaia.core.universal_coalgebras.NeuralFunctor(activation_dim, bias_dim)[source]

Bases: Endofunctor[<MockTorch name=’mock.Tensor’ id=’4349657872’>]

Neural network endofunctor for backpropagation coalgebras

Models F_B(X) = A × B × X From (MAHADEVAN,2024) Definition 11 where A = activations, B = biases, X = parameters

__init__(activation_dim, bias_dim)[source]
apply(params)[source]

Return (activations, biases, parameters) triple

fmap(morphism)[source]

Apply morphism to parameter component

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

class gaia.core.universal_coalgebras.FCoalgebra(carrier, structure_map, endofunctor, name=None)[source]

Bases: Generic[A]

F-coalgebra (A, α) From (MAHADEVAN,2024) Definition 7

Consists of: - carrier: Object A in category C - structure_map: Arrow α: A → F(A) defining dynamics

carrier: A
structure_map: Callable[[A], A]
endofunctor: Endofunctor[A]
name: str | None = None
evolve(state)[source]

Evolve state using structure map α: A → F(A)

This is the core dynamics of the coalgebra - how states transition.

iterate(initial_state, steps)[source]

Iterate coalgebra dynamics for multiple steps

Generates trajectory: state → α(state) → α²(state) → …

is_fixed_point(state, tolerance=1e-6)[source]

Check if state is a fixed point: α(state) = state

Related to Lambek’s theorem for final coalgebras.

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

__init__(carrier, structure_map, endofunctor, name=None)
class gaia.core.universal_coalgebras.CoalgebraHomomorphism(source, target, morphism)[source]

Bases: Generic[A, B]

Homomorphism between F-coalgebras From (MAHADEVAN,2024) Definition 9

Arrow f: A → B such that the diagram commutes: A –α–> F(A) | | f F(f) | | v v B –β–> F(B)

__init__(source, target, morphism)[source]
apply(state)[source]

Apply homomorphism to state

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

class gaia.core.universal_coalgebras.Bisimulation(coalgebra1, coalgebra2, relation)[source]

Bases: Generic[A, B]

Bisimulation between coalgebras From (MAHADEVAN,2024) Definition 10

Relation R ⊆ S × T with structure map α_R: R → F(R) such that projections π₁, π₂ are homomorphisms.

Critical for comparing generative AI models (e.g., two LLMs).

__init__(coalgebra1, coalgebra2, relation)[source]
are_bisimilar(state1, state2)[source]

Check if two states are bisimilar

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

class gaia.core.universal_coalgebras.GenerativeCoalgebra(model, optimizer, loss_fn)[source]

Bases: FCoalgebra[<MockTorch name=’mock.Tensor’ id=’4349657872’>]

Specialized coalgebra for generative AI models

Implements backpropagation as F-coalgebra From (MAHADEVAN,2024) Section 3.2

__init__(model, optimizer, loss_fn)[source]
classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

evolve(state)

Evolve state using structure map α: A → F(A)

This is the core dynamics of the coalgebra - how states transition.

is_fixed_point(state, tolerance=1e-6)

Check if state is a fixed point: α(state) = state

Related to Lambek’s theorem for final coalgebras.

iterate(initial_state, steps)

Iterate coalgebra dynamics for multiple steps

Generates trajectory: state → α(state) → α²(state) → …

name: str | None = None
carrier: A
structure_map: Callable[[A], A]
endofunctor: Endofunctor[A]
class gaia.core.universal_coalgebras.CoalgebraCategory[source]

Bases: object

Category of F-coalgebras with homomorphisms as morphisms

Provides categorical structure for organizing generative AI models.

__init__()[source]
add_coalgebra(name, coalgebra)[source]

Add coalgebra as object in category

add_homomorphism(source_name, target_name, morphism)[source]

Add homomorphism as morphism in category

compose_morphisms(first, second)[source]

Compose two homomorphisms if composable

Implements categorical composition in coalgebra category.

find_bisimulations()[source]

Find bisimulations between coalgebras in category

Critical for comparing different generative AI models.

gaia.core.universal_coalgebras.create_llm_coalgebra(model, optimizer, loss_fn)[source]

Create coalgebra for Large Language Model

gaia.core.universal_coalgebras.create_diffusion_coalgebra(model, noise_schedule)[source]

Create coalgebra for diffusion model

Models probabilistic coalgebra over ODEs from GAIA paper

gaia.core.universal_coalgebras.create_transformer_coalgebra(attention_heads, hidden_dim)[source]

Create coalgebra for Transformer model

Will be extended with categorical transformer structure in later tasks.