Skip to content
Projects

lucidrains/torch-einops-utils

View on GitHub

lucidrains/torch-einops-utils has 55 GitHub stars and 2 forks. GitHot measured 0 stars in 24 hours and 0 in 7 days. Last checked 2026-11-16 03:32 UTC. Last push 2026-11-02.

How these counts are measured

Measured star growth

in 24h
0
in 7 days
0
in 15 days
Collecting history
in 1 month
Collecting history

Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.

About

Some utility functions to help myself (and perhaps others) go faster with ML/AI work

AI

README.md

Original

torch-einops-utils

Some utility functions to help myself (and perhaps others) go faster with ML/AI work

maybe_return

return just the primary output by default, extras only on request

from torch_einops_utils import maybe_return

@maybe_return('memories')
def forward(self, ids, return_memories = False):
    memories = build_memories(ids) if return_memories else None

    return backbone(ids), memories

model(ids)                             # logits
model(ids, return_memories = True)     # Output(logits, memories)
logits, memories = model(ids, return_memories = True)  # unpacks

any number of extras, individually or all at once

@maybe_return('memories', 'hiddens')
def forward(self, ids, return_memories = False, return_hiddens = False):
    ...
    return logits, memories, hiddens

model(ids, return_hiddens = True)  # Output(logits, hiddens)
model(ids, return_all = True)      # Output(logits, memories, hiddens)

primary may be a tuple of names, or a callable receiving the bound call arguments, for when the shape of the primary depends on the mode

def primary_fields(kwargs):
    if not kwargs.get('return_loss', False):
        return ('logits', 'memory') if kwargs.get('return_memory', False) else 'logits'

    return ('loss', 'loss_mask') if not kwargs.get('reduce_loss', False) else 'loss'

@maybe_return('pooled_repr', primary = primary_fields)
def forward(self, ids, return_loss = False, return_memory = False, reduce_loss = False, return_pooled_repr = False):
    ...
    if return_loss:
        return loss, loss_mask, pooled_repr

    return logits, memory, pooled_repr

model(ids)                            # logits
logits, memory = model(ids, return_memory = True)
loss, loss_mask = model(ids, return_loss = True)
loss = model(ids, return_loss = True, reduce_loss = True)
loss, pooled = model(ids, return_loss = True, reduce_loss = True, return_pooled_repr = True)

log_table

clean, aligned logging for a single record or a list of records

from torch_einops_utils import log_table

log_table(ep = 200, reward = 187.04, steps = 1000, avg100 = 66.03)
# ep 200  reward 187.04  steps 1000  avg100 66.03

log_table([
    dict(ep = 200, reward = 187.04, avg100 = 66.03),
    dict(ep = 225, reward = 76.72, avg100 = 89.98),
], formatters = dict(reward = '.2f', avg100 = '.2f'))
ep   reward  avg100
200  187.04   66.03
225   76.72   89.98

formatters takes a format spec or a callable per column, and format_table returns the string without printing