-
Notifications
You must be signed in to change notification settings - Fork 4
Add a sample function that computes a batch of template results in parallel
#417
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
Changes from 3 commits
cefaf7e
27cdbed
0d8532b
a03370f
427a349
4dab7aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,8 @@ | |
|
|
||
| from effectful.handlers.llm import Template | ||
| from effectful.internals.runtime import get_interpretation, interpreter | ||
| from effectful.ops.semantics import fwd | ||
| from effectful.ops.syntax import ObjectInterpretation, implements | ||
| from effectful.ops.semantics import fwd, handler | ||
| from effectful.ops.syntax import ObjectInterpretation, defop, implements | ||
|
|
||
|
|
||
| class KAheadSampler[**P, T](ObjectInterpretation): | ||
|
|
@@ -45,3 +45,22 @@ def n_votes_ahead(): | |
| tasks.append(executor.submit(interpreter(intp)(fwd), *args, **kwargs)) | ||
| executor.shutdown() | ||
| return self.votes.most_common(1)[0][0] | ||
|
|
||
|
|
||
| def sample(template, n): | ||
| @defop | ||
| def in_nested_call() -> bool: | ||
| return False | ||
|
|
||
| def _template_call(template, *args, **kwargs): | ||
| if in_nested_call(): | ||
| return fwd() | ||
|
|
||
| with handler({in_nested_call: lambda: True}): | ||
| with ThreadPoolExecutor() as executor: | ||
| intp = get_interpretation() | ||
| tasks = [executor.submit(interpreter(intp)(fwd)) for _ in range(n)] | ||
| completed = futures.wait(tasks, return_when=futures.ALL_COMPLETED) | ||
| return [t.result() for t in completed.done] | ||
|
|
||
| return handler({Template.__call__: _template_call})(template) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the benefit of sample using the handler machinery here rather than directly retrieving the information from the template passed in?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and then recursively calling
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. |
||
Uh oh!
There was an error while loading. Please reload this page.