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.

np.unwrap runtime explodes

See original GitHub issue

Dear jax team,

I implemented np.unwrap by looking at the numpy source and changing the inplace-modification bits to np.where and np.concatenate.

While the values are correct, the runtime explodes for x.size > 1e7 and is generally a lot worse than the numpy version. See repro below.

Now, I don’t want to waste your time with some micro-optimizations for my specific code. But are there some general performance tips here? I guess the where and concatenate are problematic, but I wouldn’t know how to improve this in the JAX framework. Sorry for the code being so cryptic…

Repro:

import jax
import jax.numpy as np
import numpy as onp


@jax.partial(jax.jit, static_argnums=1)
def unwrap(p, axis):
    nd = np.ndim(p)
    dd = np.diff(p, axis=axis)

    ddmod = np.mod(dd + np.pi, 2 * np.pi) - np.pi
    ddmod = np.where(
        np.isclose(ddmod, -np.pi) & (dd > 0),
        np.pi,
        ddmod)

    ph_correct = np.where(
        np.abs(dd) < np.pi,
        0,
        ddmod - dd)
    
    up = np.concatenate((
        jax.lax.slice_in_dim(p, 0, 1, axis=axis),
        jax.lax.slice_in_dim(p, 1, None, axis=axis) + np.cumsum(ph_correct, axis=axis)
    ), axis=axis)

    return up


# OKAY
x = onp.random.randn(1000) * 10
assert onp.allclose(
    onp.unwrap(x),
    unwrap(x, 0),
    atol=1e-3, rtol=1e-3
)

# NOT OKAY
x = onp.random.randn(int(1e7)) * 10
unwrap(x, 0).block_until_ready()

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
clemischcommented, Apr 3, 2020

It works and is super fast, thank you!

1reaction
hawkinspcommented, Apr 3, 2020

I believe this is now fixed at head. Let me know how it goes!

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.unwrap — NumPy v1.24 Manual
If the discontinuity in p is smaller than period/2 , but larger than discont, no unwrapping is done because taking the complement would...
Read more >
Unwrap angle to have continuous phase - python
How to get an array of more "continuous" phases from it? Of course, I already tried with np.unwrap : plt.plot(np.unwrap(phase)). or
Read more >
Untitled
... episode 216 english subbed, Runtime of interstellar, Aeg electrolux oven manual? ... Cine argentino ricardo darin, Sequoia national park rim fire, ...
Read more >
Untitled
... add wallet coupon, Baldwin denim kc hat, Qt3 runtime library install? ... Diwali festival amstelveen 2014, Unwrap hair, Leize todo por el...
Read more >
Changelog — Hypothesis 6.60.0 documentation
It also does not make sense as a runtime type on its own. 6.35.1 - 2022-01-17¶. This patch fixes hypothesis write output ...
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