Skip to content

Completely Refactor #179

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

Open
wants to merge 70 commits into
base: main
Choose a base branch
from
Open

Completely Refactor #179

wants to merge 70 commits into from

Conversation

willtebbutt
Copy link
Member

@willtebbutt willtebbutt commented Feb 27, 2025

Per #177 this is an attempt to significantly refactor the internals for robustness.

Todo:

  • understand what the desired semantics are regarding copying, per the discussion in Questions About Semantics #178
  • determine whether the task field of a TapedTask ought actually to be part of the public interface -- issue to discussion open at Accessing task field of Libtask TapedTask AdvancedPS.jl#113
  • implement an opt-in mechanism for nested calls containing produce statements
  • tidy up main body of code
  • document how PhiNodes are handled more thoroughly (include a worked example)
  • document the intent of the code + the high-level structure. In particular, re-work the README to explain what the public interface is, and what it does.
  • get all CI passing
  • check that we can make AdvancedPS and Turing work using this PR
  • kwarg support
  • improve nesting mechanism to ensure that we always catch everything
  • optimise nested calls (currently throwing away all return type information to get prototype running) see below
  • benchmark to ensure no regressions
  • add BBCode section to Mooncake docs so that this implementation is intelligible to everyone
  • add more high-level documentation

Linked Issues:

Closes #165
Closes #167
Closes #171
Closes #176

edit: performance optimisation. Everything (as far as I know) is now type-stable where it ought to be, except in the context of nested calls. These are somewhat harder to achieve optimal performance with (you need the IR of the thing that you're going to recurse into in order to figure out the return type of a call-which-might-produce -- since nasty things like recursion exist, it's not completely trivial to do this correctly).

@willtebbutt
Copy link
Member Author

@FredericWantiez I think your problems should now be fixed. Could you let me know if it's working okay for you on the AdvancedPS side of things?

@FredericWantiez
Copy link
Member

Yes, that fixed the issue with the return value ! Thanks

@willtebbutt willtebbutt marked this pull request as ready for review April 15, 2025 12:48
@willtebbutt willtebbutt requested review from sunxd3 and mhauru April 15, 2025 16:38
@willtebbutt
Copy link
Member Author

@mhauru @sunxd3 apologies in advance for the rather large PR.

When reviewing, I suggest that you ignore everything in src/bbcode.jl and src/utils.jl -- they are copies of utility functionality from Mooncake. Additionally, if anything is unclear, please let me know rather than trying to spend too long to understand it if you feel that it is under-documented -- I'm keen to add additional docs etc, so it's really rather helpful to know where things aren't straightforward to understand.

Copy link
Member

@mhauru mhauru left a comment

Choose a reason for hiding this comment

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

I didn't read the last 700 lines of src/copyable_task.jl, nor did read through the deleted tests to see that the new ones cover the same cases. I may come to those later, but figured leaving some comments now would be helpful.

I only have some localised comments and questions, and a couple of design questions. Mostly looks great, thanks so much for doing this!

@@ -11,7 +11,6 @@ jobs:
strategy:
matrix:
version:
- '1.7'
- '1.10'
- '1'
- 'nightly'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- 'nightly'
- 'pre'

We've switched to pre over nightly in other TuringLang repos.

LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
MistyClosures = "dbe65cb8-6be2-42dd-bbc5-4196aaced4f4"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Copy link
Member

Choose a reason for hiding this comment

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

Is Test meant to be a strong dep of the package and not just of tests?

Libtask.TapedTask
```

The functions discussed the above docstring (in addition to [`TapedTask`](@ref) itself) form the
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
The functions discussed the above docstring (in addition to [`TapedTask`](@ref) itself) form the
The functions discussed in the above docstring (in addition to [`TapedTask`](@ref) itself) form the

its `taped_globals` field.

The type `T` is required for optimal performance. If you know that the result of this
operation must return a specific type, specific `T`. If you do not know what type it will
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
operation must return a specific type, specific `T`. If you do not know what type it will
operation must return a specific type, specify `T`. If you do not know what type it will

Comment on lines +13 to +15
# Extended Help


Copy link
Member

Choose a reason for hiding this comment

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

Expand or remove?

end
return nothing
end

Copy link
Member

Choose a reason for hiding this comment

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

The test cases made me wonder, what happens if I do

f = Libtask.produce
f(0)

or something like

using Libtask: produce as prod

prod(0)

?

Comment on lines +144 to +145
# Test case 1: stack allocated objects are deep copied.
@testset "stack allocated objects shallow copy" begin
Copy link
Member

Choose a reason for hiding this comment

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

The comment and the name seem conflicting.

t[1] = 0
while true
produce(t[1])
t[1]
Copy link
Member

Choose a reason for hiding this comment

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

What does this line do?

@test consume(a) == 4
end
end
@testset "Issue: PR-86 (DynamicPPL.jl/pull/261)" begin
Copy link
Member

Choose a reason for hiding this comment

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

Am I missing something, or is this just the same test as above but with more reps?

end

# Test case 2: Array objects are deeply copied.
@testset "Array objects deep copy" begin
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to have a test like this but where the array returned by consume(ttask/a) is mutated by the caller?

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.

Use MistyClosures for Libtask 2.0?
4 participants