question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Primitive to sequentially execute a function inside a vmapped function

See original GitHub issue

I am writing some longer algorithms which I like to vmap. I stumbled over some problems when combining vmap with the host_callback module and the lax.cond function:

  1. vmap of cond does not work when the branches are not side-effect-free
  2. vmap of cond of can be very ineffective if one branch is much longer but only called rarely
  3. There is no trivial batching rule for the host_callback.call function

I think a simple solution would be to implement a stop_vmap or sequential_vmap decorator. This decorator would define a batching rule, such that

@vmap
@stop_vmap
def some_fun(*args):
  # Some operations ...
  return results

would be the same as writing

def some_fun(*batched_args):

  def body_fun(*args):
    # Some operation...
    return results

  return lax.map(lambda args: body_fun(*args), batched_args)

The advantage of the decorator would be that some_fun could be used inside a much bigger vmapped function.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
shoyercommented, Aug 22, 2021

The “sequential_vmap” concept seems like it would be easy to implement given a more general custom batching interface, which is something that @mattjj and I have considered before.

More general “custom batching” could be interesting for use-cases like host_callback.call, where some external library that may support its own parallelism strategies. E.g., I think this could be useful for @ianwilliamson’s MEEP wrapper: https://github.com/NanoComp/meep/pull/1569

Short of that, it might be worth considering offering the special case, by writing a single higher-order primitive whose batching rule is essentially a call to lax.map.

This could definitely be a good place to start, even if only as the first step towards the general solution.

0reactions
froystigcommented, Aug 22, 2021

This could definitely be a good place to start, even if only as the first step towards the general solution.

I tend to agree, like having written linear_call as a first step towards custom transposition. Perhaps worth trying to see what it surfaces.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - How to execute promises sequentially, passing the ...
In my actual use case the array is dynamically populated and I need to execute the myPromise() function for each member in myArray...
Read more >
2.1. Primitive Tensor Function - Machine Learning Compilation
Primitive tensor function refers to the single unit of computation in model execution. A MLC process can choose to transform implementation of primitive...
Read more >
21 Iteration | R for Data Science - Hadley Wickham
In this chapter you'll learn about two important iteration paradigms: imperative programming and functional programming. On the imperative side you have tools ...
Read more >
XPath and XQuery Functions and Operators 3.1 - W3C
For a sequence of calls within the same ·execution scope·, ... The diagrams in this section show how nodes, functions, primitive simple ...
Read more >
Built-in Types — Python 3.11.1 documentation
Operations and built-in functions that have a Boolean result always return ... the machine on which your program is running is available in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found