Skip to content

Learning Algorithms

Defined in: griffith_logic.training.spec.LearningSpec (class)

class LearningSpec(
rule: str = 'bp',
lr: float | None = None,
update_mode: int = 1,
feedback_init: str = 'gaussian',
block_upstream: bool = True,
eggroll: EGGROLLConfig | None = _default_eggroll(),
)

Per-layer / per-module learning rule specification.

Attributes

  • rule — ‘bp’ | ‘ap’ | ‘dfa’ | ‘eggroll’
  • lr — rule-specific learning rate (None uses the trainer/optimizer default)
  • update_mode — specific to Akiho Propagation (1 = update all weights, 2 = only the largest basis input)
  • feedback_init — specific to DFA. Initialization of the fixed random feedback matrix
  • block_upstream — specific to DFA. The current implementation only supports True (no gradient flows upstream)
  • eggroll — hyperparameters specific to EGGROLL (None uses EGGROLLConfig())

Examples using this API

Defined in: griffith_logic.training.spec.with_learning (function)

def with_learning(module, spec: LearningSpec)

Annotates a module with a learning rule and returns it (without wrapping it).

Examples using this API

Defined in: griffith_logic.training.spec.get_learning_spec (function)

def get_learning_spec(module) -> LearningSpec | None

Returns a module’s learning rule annotation (None if it is not annotated).

Defined in: griffith_logic.training.trainer.HybridTrainer (class)

class HybridTrainer(
model: nn.Module,
optimizer: torch.optim.Optimizer | None = None,
loss_fn=F.cross_entropy,
clamp_raw: bool = True,
)

Trainer for models that mix learning rules.

Arguments

  • model — a model annotated with with_learning (or unannotated, meaning all-bp)
  • optimizer — optimizer for the bp/dfa parameters. If None, an AdamW(lr=1e-3) is created automatically when bp/dfa parameters exist
  • loss_fn — (logits, target) -> scalar loss
  • clamp_raw — clamp(0,1) after each step for raw-parameterized LutLayers (BinaryBrain semantics)

Examples using this API

Defined in: griffith_logic.training.gpu_accel.make_train_step (function)

def make_train_step(
model: torch.nn.Module,
optimizer: torch.optim.Optimizer,
compute_loss: Callable[[torch.Tensor, torch.Tensor], torch.Tensor],
mode: str = 'auto',
warmup_steps: int = 3,
reasons: list[str] | None = None,
)

Builds a training-step closure that applies CUDA Graphs automatically when possible.

Arguments

  • model — the model to train (CUDA is detected from the device of its parameters).
  • optimizer — Adam/AdamW or similar. capturable must already have been set by the caller when constructing it (see wants_capturable(mode)). If you use an LR scheduler, pass lr as a CUDA tensor. A float lr is fixed in the graph at its value at capture time, and later changes by the scheduler are not reflected (a warning is issued if any param_group has one).
  • compute_loss — compute_loss(x, y) -> loss. Forward and loss computation only (it must not contain .item(), optimizer.zero_grad() or optimizer.step()).
  • mode — ‘auto’ (enabled on CUDA when nothing prevents it) / ‘off’ (always disabled) / ‘force’ (enabled unless there are blocking reasons in reasons). The environment variable MDL_NO_CUDA_GRAPHS=1 takes precedence over mode and always disables it.
  • warmup_steps — number of eager steps before capture (default 3).
  • reasons — a list of strings, identified by the caller (the training script), explaining why CUDA Graphs are numerically unsafe for this model configuration (for example a tau anneal or an emb_reg ramp: schedulers whose host-side values change every step and affect the forward pass). If non-empty, CUDA Graphs are disabled even with mode=‘force’ (correctness first; the reasons are printed).

Returns

(step_fn, info): step_fn(x, y) -> loss (a tensor detached from the autograd graph; calling .item() is the caller’s responsibility). During replay it returns a new tensor cloned from the graph’s output, so values are not overwritten across calls. info: dict with ‘mode’/‘device’/‘attempted’/‘enabled’/‘reasons’/‘warmup_steps’/ ‘captured_shape’ (a mutable dict: ‘enabled’/‘captured_shape’ are updated with the result of the first capture once the training loop starts).

Defined in: griffith_logic.training.gpu_accel.wants_capturable (function)

def wants_capturable(mode: str) -> bool

Decides whether capturable=True should be attempted when constructing the optimizer.

Whether the device is ‘cuda’, and any script-specific reasons for disabling (annealing schedulers and so on), are not considered here. The caller is expected to make the final decision as wants_capturable(mode) and device == 'cuda' and not <script-specific reasons> and to use the result consistently for both the optimizer’s capturable= and make_train_step(..., reasons=...).

Arguments

  • mode — ‘auto’ | ‘off’ | ‘force’

Returns

False if mode==‘off’ or the environment variable MDL_NO_CUDA_GRAPHS=1 is set, True otherwise (‘auto’/‘force’).