Skip to main content

FPy

An embedded Python DSL for specifying and simulating numerical algorithms.

Important links:

Example

FPy is Python with explicit control of both the mathematics and rounding: every operation is correctly rounded by the rounding context it appears in, and contexts are ordinary values you can pass around — including fp.REAL, which never rounds. Here is a blocked dot product: the products are exact, each block of K elements is summed in a narrow format, and the running total is float32.

import fpy2 as fp

@fp.fpy(ctx=fp.REAL)
def dot(xs: list[fp.Real], ys: list[fp.Real], K: int, block: fp.Context) -> fp.Real:
    """A blocked dot product: exact products, blocks of `K` summed in `block`.
    The length of `xs` must be a multiple of `K`."""
    assert len(xs) == len(ys)
    acc = 0
    for start in range(0, len(xs), K):
        with block:
            inner_acc = 0
            for x, y in zip(xs[start:start + K], ys[start:start + K]):
                with fp.REAL:
                    p = x * y       # every product is exact ...
                inner_acc += p      # ... but block sums round to `block`
        with fp.FP32:
            acc += inner_acc        # one fp32 addition per block
    return acc

@fp.fpy(ctx=fp.REAL)
def dot_ref(xs: list[fp.Real], ys: list[fp.Real]) -> fp.Real:
    """The same dot product, with no rounding anywhere."""
    return sum([x * y for x, y in zip(xs, ys)])

xs = ys = [0.1] * 4096
exact = dot_ref(xs, ys).as_rational()   # the true value, as a Fraction

print(f'{"block":>6} {"format":>9} {"result":>11} {"rel. error":>11}')
for K, name, ctx in [(4096, 'float16', fp.FP16), (32, 'float16', fp.FP16),
                     (4096, 'bfloat16', fp.BF16), (32, 'bfloat16', fp.BF16),
                     (32, 'float32', fp.FP32)]:
    r = dot(xs, ys, K, ctx)
    err = abs(r.as_rational() - exact) / exact
    print(f'{K:>6} {name:>9} {float(r):>11.6f} {float(err):>11.3%}')

which prints

 block    format      result  rel. error
  4096   float16   32.000000     21.875%
    32   float16   40.968750      0.021%
  4096  bfloat16    4.000000     90.234%
    32  bfloat16   40.250000      1.733%
    32   float32   40.959972      0.000%

A flat float16 accumulator stalls out once the running total dwarfs the product being added to it; summing 32 elements at a time keeps the error three orders of magnitude smaller.

See the usage guide for the language and the examples page for more.

Installation

FPy can be installed from PyPI with either uv or pip, or built from source for development. The following instructions assume a bash-like shell.

Installing from PyPI

Requirements:

  • Python 3.11 or later

To install the latest stable release of FPy, run:

uv pip install fpy2

or, with pip:

pip install fpy2

The Triton backend also needs PyTorch, Triton and a GPU: install fpy2[triton].

Installing from source

Requirements:

  • Python 3.11 or later
  • make

With uv (preferred)

uv is the recommended development workflow — it handles the virtual environment and dependency installation in a single step:

uv sync

This creates .venv/ and installs FPy in editable mode along with the dev dependency group, pinned by uv.lock. Rerun it to reset .venv/ to the lockfile; it removes anything installed by hand. Activate the environment with

source .venv/bin/activate

or prefix individual commands with uv run (e.g. uv run pytest tests/unit).

With pip (legacy)

This path is preserved for compatibility with existing tooling; new contributors should prefer uv above.

If you do not have a Python virtual environment, create one using

python3 -m venv .venv/

and activate it using

source .venv/bin/activate

To install an instance of FPy for development, run:

pip install -e .[dev]

To uninstall FPy, run:

pip uninstall fpy2

Testing

There are a number of tests that can be run through the Makefile including

make lint

to ensure formatting and type safety;

make unittest

to run the unit tests;

make infratest

to run the infrastructure tests.

Metadata

Release files for fpy2 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fpy2 0.2.4
File Size Uploaded Provenance Actions
fpy2-0.2.4.tar.gz 664.1 kB Without provenance Details for fpy2-0.2.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fpy2 0.2.4
File Interpreter ABI Platform Actions
fpy2-0.2.4-py3-none-any.whl Python 3 none any Details for fpy2-0.2.4-py3-none-any.whl

Total release size: 1.4 MB

Release files / fpy2-0.2.4.tar.gz

Download URL fpy2-0.2.4.tar.gz
Size 664.1 kB
Tags Source
SHA-256 checksum
How to use checksums
84fe2bc84b99304591d5a9ad8b568ebc0d8d70e7bfb49ca31d6b63fee3f6b28a
BLAKE2b-256 checksum
How to use checksums
0f68706a7cdb01542b1041c23ee17c81589d0b161d633c3646fdb518bc6d8b27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / fpy2-0.2.4-py3-none-any.whl

Download URL fpy2-0.2.4-py3-none-any.whl
Size 776.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a3ac5253d10032569bd01cf71c1483fd3139b95cee2fe64b8c33617c2e1f2d75
BLAKE2b-256 checksum
How to use checksums
f023e1692183a4a96b15cdd58f69925ad0ab118948eef54065e3744b75ffdeae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for fpy2 0.2.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.4 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page