Skip to main content

maxray

the problem with doing weird metaprogramming shit is having to deal with other people's weird metaprogramming shit

Trace and modify the result of (almost) every single expression executed in a Python program in realtime, requiring zero changes to your source code. Very WIP.

from maxray import transform, xray, maxray

from torch import tensor, Tensor, device


def move_tensor(x, ctx):
    if isinstance(x, Tensor) and x.device != device("cuda"):
        return x.to("cuda")
    return x


@transform(move_tensor)
def show_multiply(a, b):
    print(a @ b)

# Source code is rewritten to be equivalent to:

def _show_multiply(a, b):
    move_tensor(move_tensor(print, ...)(move_tensor(a, ...) @ move_tensor(b, ...)), ...)

# ---

show_multiply(
    tensor([[0.0, 1.0], [1.0, 1.0]], device="cpu"),
    tensor([[1.0], [1.0]], device="cuda"),
)  # Without the decorator, you'd expect `RuntimeError: Expected all tensors to be on the same device`

# tensor([[1.],
#         [2.]], device='cuda:0')

The ctx argument contains context information about the location of the original source code, which may be useful to build editor/LSP integrations.

The *xray decorators will recursively trace and patch every single callable they encounter until reaching either builtins, native, or generated code.

Usage

This package comes with 2 CLI commands (see --help for limited docs):

  • xpy: TUI for debugging and observability. Replace python your_script.py with xpy your_script.py to apply transformations (via -W args) globally
    • -W takes file:symbol args... (be careful with shell quoting) generated by maxray template <new_script.py> and hot-reloads it on every filesystem change. Can be used multiple times to compose transformations (applied sequentially)
    • --restrict to only transform "your" (same source file or module) source code (i.e. does not descend into external packages). Significantly faster, more robust, and probably more useful - should probably be the default...
    • -m is equivalent to python -m some_module
  • maxray
    • template: see --help for options

Examples

  • callgraph: draws the runtime function call graph
    • compared to tools relying on static analysis, can exactly resolve calls like [fn_a, fn_b][rand() < 0.5]()
pip install maxray[all]
xpy -W 'callgraph:Draw --labels' <your_script.py>
  • capture: dump all runtime captured information to an Arrow IPC file for analysis
  • rerun: replace all uses of print with logging to a Rerun viewer, attaching source file and line information for filtering
  • tensorcheck: global nan and inf checking for tensors
  • strings: grepping of runtime values to find and rewrite URLs and filesystem paths
  • progress: tqdm, but like, everywhere. Automatically shows a progress bar and the last yielded value for every for _ in iterable expected to take longer than 1s to complete
  • ...: Write your own! Argument to -W can be any source file or installed package/module

Issues

If running a script or module with xpy deterministically results in differing output or exceptions compared to when run with python (CPython >= 3.11), that's a bug. A stopgap solution is to restrict the offending modules to prevent them from being transformed.

Metadata

Release files for maxray 0.7.0

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

Source distribution (sdist)

Source distribution for maxray 0.7.0
File Size Uploaded
maxray-0.7.0.tar.gz 41.6 kB Details

Built distribution (wheel)

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

Total release size: 90.5 kB

Release files / maxray-0.7.0.tar.gz

Download URL maxray-0.7.0.tar.gz
Size 41.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e070ebf3fc1e1cd1231620f46d37420a5792ee4bf3eb1f5d659f837308af29ca
BLAKE2b-256 checksum
How to use checksums
5155468421a939f951b11652217acebe0645a24c224a2beee5c69edee1935369
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.10 Linux/6.8.0-1014-azure

Release files / maxray-0.7.0-py3-none-any.whl

Download URL maxray-0.7.0-py3-none-any.whl
Size 49.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d0b91fe7d1363fa4fc470d786d557db01f3f2091e4bcfff8ef6aba75f14fbedd
BLAKE2b-256 checksum
How to use checksums
a6e0a4a5283b3d03212f90802fc8a386419927734df56cc8280424de8d991062
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.10 Linux/6.8.0-1014-azure

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

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