Skip to content

[WIP] Implement rewrite validator #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 45 commits into
base: main
Choose a base branch
from

Conversation

serenity4
Copy link
Member

Closes #47.

This PR is based on #54, for a clean view of the changes see the comparison against that PR.

This is still largely a work in progress, I am just putting it here for discussion.

@serenity4
Copy link
Member Author

@Keno does the code in test/validation.jl look aligned with your expectations?

sciml_prob = DiffEqBase.get_concrete_problem(our_prob, true);
f_compressed = sciml_prob.f.f
f_compressed(residuals_compressed, du_compressed, u_compressed, p, t)
# XXX: `ddt(x₁)` gets substituted by `x₁ *ᵢ x₂` for the last equation after scheduling.
Copy link
Member Author

Choose a reason for hiding this comment

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

I briefly mentioned it in our last call, but I'm not sure I understand how we should work around that assuming that the overall approach is correct. Codegen looks at the past du for the first equation but then looks at its expected value (i.e. its RHS) for the last equation, even though we are using ddt(x1) in both expressions.

Copy link
Member

Choose a reason for hiding this comment

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

For linear equations, you can't compare the residuals directly, since ssrm rewrites them. However, you can reconstitute them uniquely from the state vector, since the total_incidence has the coefficients. Sorry, I think I forgot to mention that detail in the issue.

Copy link
Member Author

@serenity4 serenity4 Jun 24, 2025

Choose a reason for hiding this comment

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

I see, so that tells us how to reconstruct the residual for the linear equation x₃ - x₁.
However, the equations I wrote about in #55 (comment) were ddt(x₁) - x₁ * x₂ and x₄ * x₄ - ddt(x₁), which are each using a different value for ddt(x₁) after scheduling. Would you have any pointers on how to deal with that?

Copy link
Member

Choose a reason for hiding this comment

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

The values should be numerically equal up to tolerance (in an on-manifold state vector), so it shouldn't matter.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do we want to require the u/du we provide for testing to be on-manifold? That sounds like a reasonable requirement, but perhaps it might be good to also check that outside of this condition the optimized version behaves similarly to the unoptimized version, unless that doesn't affect the correctness of our solutions.

@Keno
Copy link
Member

Keno commented Jun 23, 2025

I need to read it in more detail, some initial comments are that I don't think we should be using RHSSpec for this, since the ABI is different. The other is that in this mode, we should probably also not do the IPO/argument transform and then in particular, the ABI needs to retain all the original arguments (in addition to the vectors that you have). Because of those two points then, there should also not be a TornCacheKey for this, since there's always only one specialization for this (for each method instance). In general, I think you're on the right track though.

@serenity4
Copy link
Member Author

That makes sense, thanks. IIUC, RHSSpec should then be replaced by a singleton struct for the CodeInstance's owner, right?

we should probably also not do the IPO/argument transform and then in particular, the ABI needs to retain all the original arguments

To make sure I understand: by IPO/argument transform, do you mean the flattening transform, or/and another one? Then my understanding is that because we don't flatten, we must pass around all arguments because the callees will need them whole (which is a consequence of not performing SICM). Is that correct?

@Keno
Copy link
Member

Keno commented Jun 23, 2025

That makes sense, thanks. IIUC, RHSSpec should then be replaced by a singleton struct for the CodeInstance's owner, right?

Yes

To make sure I understand: by IPO/argument transform, do you mean the flattening transform, or/and another one?

Yes, the flattening and the nonlinear replacement rewrite. Although it's a little bit tricky, because the latter introduces extra equations, so you may want to run it anyway and just copy the IR before it finishes (and then the introduced extra equations just don't show up in the IR and end up as 0).

Then my understanding is that because we don't flatten, we must pass around all arguments because the callees will need them whole (which is a consequence of not performing SICM). Is that correct?

Yes, the code data flow (and code itself, frankly) is exactly the same as the original code, except that all the functions have a few extra arguments to magically synthesize the correct return value for variable.

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.

Add rewrite validator
2 participants