Skip to content
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

For double sided maxwell #21264

Merged
merged 22 commits into from
Sep 11, 2023
Merged

Conversation

stalemate1
Copy link
Contributor

@stalemate1 stalemate1 commented Aug 3, 2023

close #19483

@github-actions
Copy link
Contributor

github-actions bot commented Aug 3, 2023

Thanks for contributing to Ivy! 😊👏
Here are some of the important points from our Contributing Guidelines 📝:
1. Feel free to ignore the run_tests (1), run_tests (2), … jobs, and only look at the display_test_results job. 👀 It contains the following two sections:
- Combined Test Results: This shows the results of all the ivy tests that ran on the PR. ✔️
- New Failures Introduced: This lists the tests that are passing on master, but fail on the PR Fork. Please try to make sure that there are no such tests. 💪
2. The lint / Check formatting / check-formatting tests check for the formatting of your code. 📜 If it fails, please check the exact error message in the logs and fix the same. ⚠️🔧
3. Finally, the test-docstrings / run-docstring-tests check for the changes made in docstrings of the functions. This may be skipped, as well. 📚
Happy coding! 🎉👨‍💻

@stalemate1 stalemate1 mentioned this pull request Aug 3, 2023
@ivy-leaves ivy-leaves added the JAX Frontend Developing the JAX Frontend, checklist triggered by commenting add_frontend_checklist label Aug 3, 2023
Copy link
Contributor

@zaeemansari70 zaeemansari70 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @stalemate1

uint32 is not supported by torch and paddle, kindly add those in the unsupported_dtypes decorator.
Also, do make sure that the tests are being passing when you request a review.

Feel free to comment on the PR if you need to ask any question.

Thanks!

@stalemate1
Copy link
Contributor Author

Hello @zaeemansari70!
I had push this function last week after making sure it runs for all the frameworks except for torch and paddle. When I had committed this PR there were only 5 failures in the CI tests. When I went through the display_test_results there were new failures introduced but non of them were related to the function I was working on.

Today, it says more than 90 files are failing for the same function. Could you please help me understand where I might be going wrong.

Copy link
Contributor

@zaeemansari70 zaeemansari70 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @stalemate1
You are passing in a uint32 dtype which is neither supported by torch nor paddle.
Kindly fix this 🙂
Also do not worry about the CI tests, I'll test your PR locally before merging 👍

@handle_frontend_test(
fn_tree="jax.random.double_sided_maxwell",
dtype_key=helpers.dtype_and_values(
available_dtypes=["uint32"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are specifying the dtype here, this should be valid dtype and the dtypes that should be skipped should be added in the decorator above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and pass in valid here, so that the decorator skips the dtypes which are not to be tested on. Rather than us trying to restrict those in the tests.

@stalemate1
Copy link
Contributor Author

hi @zaeemansari70 I did try different things including using helper.get_dtypes("valid") and hard coding if backend_fw == torch: key.dtype = torch.int64. But nothing is working. Could you please help me understand how I can fix this.

Copy link
Contributor

@zaeemansari70 zaeemansari70 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addresses your comments 🙂

Let me know if you have some questions, thanks! 🙂

ivy/functional/frontends/jax/random.py Outdated Show resolved Hide resolved
@handle_frontend_test(
fn_tree="jax.random.double_sided_maxwell",
dtype_key=helpers.dtype_and_values(
available_dtypes=["uint32"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and pass in valid here, so that the decorator skips the dtypes which are not to be tested on. Rather than us trying to restrict those in the tests.

@Ookamice Ookamice self-assigned this Sep 5, 2023
…or_double_sided_maxwell

# Conflicts:
#	ivy/functional/frontends/jax/random.py
@Ookamice
Copy link
Contributor

Ookamice commented Sep 6, 2023

@stalemate1
Hiya, just a quick word of advice but you should always review the additions and subtractions your commits make in the "files changed" tab. Right now, it seems like there is a lot of weird changes that are not relevant with the core issue of maxwell and you should try to resolve them before pursuing further. (This can either be done manually or alternatively you can copy the work you want to keep into a temporary text file, then force fetch from upstream and then paste back your work). This will save a lot of time later on when you need to fix all the conflicts between your PR and the main stream.

@stalemate1
Copy link
Contributor Author

sorry @Ookamice I did not notice that. I have corrected those files now.

@Ookamice
Copy link
Contributor

Ookamice commented Sep 7, 2023

@stalemate1
Hiya, thanks for making the changes. I don't think the changes you've made to rademacher is valid and should be rolled back to what it was before. (0.5 is chosen means that benoulli has an equal probability of being 0 or 1 and after scaling with 2*b-1, the range becomes -1 to 1)

PS: weirdly rademacher doesn't pass the tests on my end for some reason so I'm not sure why it is merged. Perhaps this may be the cause as to why pytorch test for double_sided_maxwell not working?

@stalemate1
Copy link
Contributor Author

@Ookamice but when I change it back to 0.5 rademacher is producing only one element, thus it is not working with double_sided_maxwell. Hence, this time I created an array of 0.5's so that it is compatible with any shape. I hope this is okay.

@Ookamice
Copy link
Contributor

Ookamice commented Sep 8, 2023

@stalemate1
I think using ivy.array([0.5]) is an oversight by the original creator of the function. I think it would be simpler to just use a scaler 0.5 although I see the motivation behind your workaround. Try using a scaler and if that doesn't work then I think your workaround is the best bet! 😸

PS: make sure that when multiplying by 0.5, you get a float rather than a int, im not sure how the type promotion may carry through as your original array of 1s is a int

@stalemate1
Copy link
Contributor Author

@Ookamice
the scalar didn't work. Also b = ivy.astype(b, dtype) this will maintain the dtype right?

@stalemate1
Copy link
Contributor Author

@Ookamice Could this be merged now?

@Ookamice
Copy link
Contributor

Ookamice commented Sep 8, 2023

@stalemate1
Hiya, just doing some rudimentary tests on your commit Firstly, I had a look at rademacher which everything worked fine (minor refactor made).

Secondly, I had a look at maxwell and plotted your output distribution compared to jax's native implementation. I plotted the output of both and it seems like there's a descrepency that should be fixed:
image

PS: The code used to test this is:

import ivy
import jax

from ivy.functional.frontends.jax.random import _get_seed

def maxwell_current(key, shape=None, dtype="float64"):
    seed = _get_seed(key)
    # generate uniform random numbers between 0 and 1
    z = ivy.random_uniform(seed=seed, shape=shape, dtype=dtype)
    # applying inverse transform sampling
    x = (z**2) * ivy.exp(-(z**2) / 2)
    return x

n = 1000000
import matplotlib.pyplot as plt
x = maxwell_current((123,123), (n,))
z = jax.random.maxwell(jax.numpy.array([123,123], dtype='uint32'), shape=(n,))


num_bins = int(ivy.sqrt(n))
n0, bins0, patches0 = plt.hist(x, bins=num_bins, histtype='step', color='blue')
n, bins, patches = plt.hist(z, bins=num_bins, histtype='step', color='red')
plt.show()

@stalemate1
Copy link
Contributor Author

stalemate1 commented Sep 8, 2023

I have trouble installing and running jax. But I had changed the code for maxwell. I was just waiting to submit this PR so that I could start a new PR for maxwell. The edited code is as follows,

def maxwell(key, shape, dtype= "float64"):
    seed = _get_seed(key)
    shape = shape + (3,)
    random_normal = ivy.random_normal(seed=seed, shape=shape, dtype=dtype)
    return ivy.vector_norm(random_normal, axis=-1)

and it is passing all the tests

@Ookamice
Copy link
Contributor

Ookamice commented Sep 8, 2023

@stalemate1
Hiya, the tests aren't that accurate for random functions due to its random nature which is why it usually just involves a sanity check by the reviewer and then a shape/dtype check. This is why it is important to plot the distribution to check if the shape matches for large samples of N. I'll have a look at if the code you've posted will provide the correct distribution on Monday :)

Edit: just linking the issue regarding fixing this #23337 here for convivence (merged and resolved)

@Ookamice
Copy link
Contributor

@dash96
with the maxwell bug fixed, this is also ready now 🚀 🚀 🚀
Keep up the good work 🥳

@Ookamice Ookamice merged commit b0d6f60 into ivy-llc:main Sep 11, 2023
130 of 133 checks passed
@stalemate1 stalemate1 deleted the for_double_sided_maxwell branch September 11, 2023 18:24
iababio pushed a commit to iababio/ivy that referenced this pull request Sep 27, 2023
druvdub pushed a commit to druvdub/ivy that referenced this pull request Oct 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JAX Frontend Developing the JAX Frontend, checklist triggered by commenting add_frontend_checklist
Projects
None yet
Development

Successfully merging this pull request may close these issues.

double_sided_maxwell
4 participants