Skip to content
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

Make to_tensor an operation #247

Merged
merged 38 commits into from
Mar 18, 2025
Merged

Make to_tensor an operation #247

merged 38 commits into from
Mar 18, 2025

Conversation

jfeser
Copy link
Contributor

@jfeser jfeser commented Mar 4, 2025

Allows partial evaluation to be part of a term.

@jfeser jfeser added the blocked label Mar 4, 2025
@jfeser
Copy link
Contributor Author

jfeser commented Mar 4, 2025

Blocked by #246

@jfeser jfeser removed the blocked label Mar 5, 2025
@jfeser jfeser changed the title Introduce a _partial_eval effect and handler Make to_tensor an operation Mar 12, 2025
t: Annotated[torch.Tensor, Scoped[A | B]],
*args: Annotated[Operation[[], torch.Tensor], Scoped[A]],
) -> Annotated[torch.Tensor, Scoped[B]]:
def _evaluate(expr):
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is there a duplicate of evaluate here? This seems to suggest we should be changing the interface of _partial_eval to be an apply rule. Also, should _partial_eval just be absorbed into the body of to_tensor_? When would it ever be correct to use _partial_eval on its own?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had a bad time trying to overload apply without breaking something else, so I gave up.

_partial_eval has other callers, so I left it as its own function.

Copy link
Contributor

Choose a reason for hiding this comment

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

I only see one other caller (_register_torch_op). Should that be made to use to_tensor_ instead, or should _partial_eval be absorbed into its body? Right now it seems like the logic is dispersed across a few different locations in a way that makes it hard to unwind.

Copy link
Contributor

Choose a reason for hiding this comment

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

I looked at this a bit over the weekend, will put up a separate PR.

dim_ops = [a.op if isinstance(a, Term) else None for a in dims]
perm = [dim_ops.index(o) for o in args] + reindex_dims
tensor = tensor.permute(perm)
return tensor[(slice(None),) * len(args) + tuple(dims[i] for i in reindex_dims)]
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not seeing the equivalent code in the previous version for this block at the end of to_tensor_. What is this logic doing that is not accomplished by _partial_eval's reindex_flat_tensor? Why is the permute call above necessary? Is this meant to fix a bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The distinction between the two functions is that _partial_eval is only concerned with the partial evaluation rule and to_tensor applies partial evaluation, then reorders dimensions. In the previous implementation, _partial_eval did both things and the permute was implicit in ordered_sized_fvs.

Copy link
Contributor

@eb8680 eb8680 left a comment

Choose a reason for hiding this comment

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

Looks good - seems like handlers.torch is converging on the idealized design mentioned in #203.

@eb8680
Copy link
Contributor

eb8680 commented Mar 18, 2025

Will merge as soon as conflicts from #254 are addressed.

@jfeser jfeser merged commit 70b0aee into master Mar 18, 2025
3 checks passed
@jfeser jfeser deleted the jf-to-tensor-op branch March 18, 2025 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants