Skip to content

How to dispatch to different instances of the same Module? #1146

Answered by marcvanzee
marcvanzee asked this question in Q&A
Discussion options

You must be logged in to vote

Answer by @levskaya:

Dynamically dispatching can have complicated performance behavior in the XLA setting (if you are rnning on TPU or GPU).
Why not just computing all results and indexing?

class Foo(nn.Module):
  @nn.compact
  def __call__(self, x, idx):
    denses = [nn.Dense(2) for i in range(3)]
    ys = [denses[i](x) for i in range(3)]
    return ys[idx]

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by marcvanzee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant