Skip to main content

Semiring programming in PyTorch

Project description

semiring_torch

Run your pytorch code on any semiring with a single line of code! Semiring_torch is built on top of autoray.

Warning: this is a proof of concept. Expect bugs and missing features.

Example

By using the logarithmic semiring, you can easily write numerically stable code. In the following example, we compute a matrix product in log-space.

Regular torch semiring_torch
import torch

x1 = torch.tensor([[0.1, 0.6], [0.1, 0.4]])
x2 = torch.tensor([[0.5, 0.3], [0.2, 0.1]])
x1 = x1.log()
x2 = x2.log()
result = x1[:, :, None] + x2[None, :, :]
result = torch.logsumexp(result, dim=1)
result = result.exp()
from autoray import numpy as torch
from semiring_torch import logarithmic_semiring

with logarithmic_semiring:
    x1 = torch.tensor([[0.1, 0.6], [0.1, 0.4]])
    x2 = torch.tensor([[0.5, 0.3], [0.2, 0.1]])
    result = x1 @ x2

Supported semirings

Currently only the logarithmic semiring is supported, but more semirings can be added easily.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

semiring_torch-0.0.1.tar.gz (21.4 kB view hashes)

Uploaded Source

Built Distribution

semiring_torch-0.0.1-py3-none-any.whl (28.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page