Skip to content

Conversation

@swap357
Copy link
Contributor

@swap357 swap357 commented Jun 5, 2025

for better rulesets readability,
use egraph.as_egglog_string to get all S-expressions,
then convert each rule(...) and rewrite(...) form into its LaTeX representation.

rendered rules and rewrite:
image

@swap357 swap357 requested a review from sklam June 5, 2025 23:47
@swap357
Copy link
Contributor Author

swap357 commented Jun 13, 2025

updated. latex rendering util can be used by importing it from -

from egglog_to_latex import visualize_ruleset_latex

and placing it on scripts with notebook check -

if IN_NOTEBOOK:
    # Visualize the constant propagation ruleset
    visualize_ruleset_latex(ruleset_const_propagate)

@swap357 swap357 requested a review from Copilot June 18, 2025 18:12
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds functionality to convert Egglog rules and rewrites into LaTeX representations and integrates notebook visualization for those rulesets.

  • Introduces egglog_to_latex.py with S-expression parsing, LaTeX conversion, and notebook display.
  • Hooks into chapter 3 tutorial script to visualize constant-propagation and if-else folding rulesets when in a notebook.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
sealir-tutorials/egglog_to_latex.py New module for tokenizing S-expressions, converting them to LaTeX, and visualizing in notebooks
sealir-tutorials/ch03_egraph_program_rewrites.py Added imports and if IN_NOTEBOOK blocks to display LaTeX-rendered rulesets
Comments suppressed due to low confidence (2)

sealir-tutorials/egglog_to_latex.py:89

  • Consider adding a docstring to _sexp_tex explaining its purpose and how it handles both str and list inputs.
def _sexp_tex(x) -> str:

sealir-tutorials/egglog_to_latex.py:5

  • Add unit tests for tokenize (and parse_sexps) to ensure S-expression edge cases are correctly handled.
def tokenize(egglog_str: str) -> List[str]:

tokens = tokenize(egglog_str)
sexps = parse_sexps(tokens)

from IPython.display import display, Math
Copy link

Copilot AI Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Move the IPython.display import to the top of the module to avoid runtime overhead when calling visualize_ruleset_latex multiple times.

Copilot uses AI. Check for mistakes.
from sealir import rvsdg
from sealir.eqsat import rvsdg_eqsat
from sealir.eqsat.rvsdg_eqsat import GraphRoot, Term, TermList
from utils import IN_NOTEBOOK
Copy link

Copilot AI Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this script is intended to be run as a module, consider using a relative import (from .utils import IN_NOTEBOOK) to avoid ImportError when the package is installed.

Suggested change
from utils import IN_NOTEBOOK
from .utils import IN_NOTEBOOK

Copilot uses AI. Check for mistakes.
Comment on lines +121 to 124
if IN_NOTEBOOK:
# Visualize the constant propagation ruleset
visualize_ruleset_latex(ruleset_const_propagate)

Copy link

Copilot AI Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] These if IN_NOTEBOOK blocks are repeated—consider extracting to a helper function to DRY up the visualization calls.

Suggested change
if IN_NOTEBOOK:
# Visualize the constant propagation ruleset
visualize_ruleset_latex(ruleset_const_propagate)
def visualize_if_in_notebook(ruleset):
if IN_NOTEBOOK:
# Visualize the given ruleset
visualize_ruleset_latex(ruleset)
visualize_if_in_notebook(ruleset_const_propagate)

Copilot uses AI. Check for mistakes.
@sklam
Copy link
Member

sklam commented Jul 2, 2025

Long rewrite rules can be too long for the screen; e.g. the pade44 tanh expansion in demo1

@ruleset
def pade44_tanh_expansion(x: Term):
   ...
    yield rewrite(Npy_tanh_float32(x)).to(
        div(
            add(mul(flt(10), pow(x, liti64(3))), mul(flt(105), x)),
            add(
                add(pow(x, liti64(4)), mul(flt(45), pow(x, liti64(2)))),
                flt(105),
            ),
        )
    )

The denominator gets very long.

Screenshot 2025-07-02 at 8 36 43 AM

Needs a way to split the long equations.

@sklam
Copy link
Member

sklam commented Jul 2, 2025

When converted to HTML pages, somehow the \ is visible.
Screenshot 2025-07-02 at 12 34 14 PM

In live notebook, they are currently rendered (hidden).

@sklam
Copy link
Member

sklam commented Jul 25, 2025

I used a modified version of this with ability to replace long names for shorter ones. Compare with https://github.com/numba/numba-prototypes/blob/archive/scipy_2025_slides/sealir-tutorials/utils/egglog_to_latex.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants