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.

Questions about control flow and indexed assignment

See original GitHub issue

Hi, I want to implement a finite element solver with jax. I made a quick demo, which runs well without JIT but extremely slow. There are two problems:

  1. How do I deal with control flow? For instance, I have if np.isclose(np.abs(np.linalg.det(jacobian), 0.0): raise ValueError(). Since this will run for every element, I cannot just leave it in python. lax.cond seems not suitable for this purpose. Other examples could be calling different material model function, based on element info stored in an array.

  2. How do I do indexed assignment? This is a relatively smaller issue. At least I managed doing it with np.where and jax.ops.index_add. But these two methods are creating instead doing in-place update and it requires some helping arrays. lax.dynamic_update_slice can only update a contiguous sub-array. Also it requires creating a sub-array first. Is there something that could do indexed update/add? I’m not very familiar with tensorflow, but it seems you can do something like my_var[4:8].assign(tf.zeros(4)) . This is not exactly what I want, my_var[4:8].add(tf.zeros(4)) would be better, but it looks better at least. Or else, I should not worry about the memory and performance cost of creating a new array?

Thank you!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
shoyercommented, Nov 1, 2019

Hi, I want to implement a finite element solver with jax

I just wanted to say that I’m very excited about this sort of thing!

In case it helps, we wrote a version simple/naive finite element solver in autograd (which is conceptually quite similar to JAX) in https://github.com/google-research/neural-structural-optimization

1reaction
duhd1993commented, Nov 3, 2019

@joaogui1 Thanks. I haven’t. It will look very ugly. For the first example, it will need nested lax.cond cuz I have many candidate values. Some sorts of lax.switch or dict object would be what I need here.

For the second example, something like tf.debugging.Assert would be more proper than doing it in python. Currently I just commented them out as there’s one element type and no exceptions in my example and shoyer’s paper.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sample Exam Questions and Answers with Tips & Tricks
Sample Exam Questions and Answers with Tips & Tricks - Control Flow Statements course ... If you notice, the multiply and assignment operator...
Read more >
Answers to Questions and Exercises: Control Flow Statements
Exercise: Write a test program containing the previous code snippet; make aNumber 3. What is the output of the program? Is it what...
Read more >
5 Control flow | Advanced R - Hadley Wickham
There are two primary tools of control flow: choices and loops. Choices, like if statements and ... Go for it, if you can...
Read more >
Control Flow Statements | Java Language Basics - InformIT
Questions and Exercises: Control Flow Statements. Questions. The most basic control flow statement supported by the Java programming language is ...
Read more >
Assignment Statements in Programming Languages
The assignment statement produces a result, ... Flow of control (execution sequence) in a program: – Within expressions ... Overall Design Question:.
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