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

jax.random.randint在spu环境下结果似乎不能正确运行 #80

Open
Maxime-Tong opened this issue Jan 20, 2023 · 3 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@Maxime-Tong
Copy link

Maxime-Tong commented Jan 20, 2023

Issue Type

Bug

Source

binary

Secretflow Version

0.7.18b0

OS Platform and Distribution

Linux centos 3.10.0

Python version

3.8.13

Bazel version

No response

GCC/Compiler version

No response

What happend and What you expected to happen.

根据jax文档jax.random.randint函数可以根据key生成一个可以选定范围大小的数字,但是在spu环境下,这个范围大小似乎不成立。
如下面给出的代码,尝试循环100个epoch,生成100个在[0,5)范围的随机数,统计超出范围的随机数个数。
在jit环境下,返回的统计个数为0;而在spu环境下,返回的统计个数却是100。这是为什么?

Reproduction code to reproduce the issue.

def run_on_cpu():
    def train():
        def loop_fun(_, carry):
            key, cnt = carry
            key, subkey = random.split(key)
            j = random.randint(subkey, shape=(), minval=0, maxval=5, dtype=jnp.int32)
            delta = jnp.where(jnp.any(jnp.array([j>=5, j<0])), 1, 0)
            cnt += delta
            return (key, cnt)
        key = random.PRNGKey(42)
        return lax.fori_loop(0, 100, loop_fun, (key, 0))
    key, cnt = jax.jit(train)()
    print(key, cnt)
# > [3622954601 1653733208] 0

# spu环境下
def run_on_spu():
    @ppd.device("SPU")
    def train():        
        def loop_fun(_, carry):
            key, cnt = carry
            key, subkey = random.split(key)
            j = random.randint(subkey, shape=(), minval=0, maxval=5, dtype=jnp.int32)
            delta = jnp.where(jnp.any(jnp.asarray([j>=5, j<0])), 1, 0)
            cnt += delta
            return (key, cnt)
        key = random.PRNGKey(42)
        key, cnt = lax.fori_loop(0, 100, loop_fun, (key, 0))
        return key, cnt
     key, cnt = train()
     key, cnt = ppd.get(key), ppd.get(cnt)
     print(key, cnt)
#> [3463333171 2461478805] 100
@anakinxc anakinxc self-assigned this Jan 20, 2023
@anakinxc anakinxc added the bug Something isn't working label Jan 20, 2023
@anakinxc
Copy link
Contributor

anakinxc commented Jan 20, 2023

Hi @Maxime-Tong

jax.random 的支持现在的确是有点问题。我来大概解释一下,jax 在生成随机数的过程里用了大量连续的位操作和运算,SPU 因为是用的一个定长的 ring 来模拟所有的整型和浮点数,在这个过程中一旦实际行为和 jax 的预期出现偏差就会导致随机数完全不在范围内

目前的建议是随机数可以在明文里生成然后传给 spu。您提供的这个 repro step 我们会内部分析一下,然后争取在后面的跌代里修复这个问题

祝新年快乐 :P

@Maxime-Tong
Copy link
Author

好的,谢谢,另外似乎jax提供的三种控制流的方法中,lax.condlax.while_loop函数在spu环境下还不能正常运行,这是正常的吗?

另同祝新年快乐 : )

@anakinxc
Copy link
Contributor

好的,谢谢,另外似乎jax提供的三种控制流的方法中,lax.condlax.while_loop函数在spu环境下还不能正常运行,这是正常的吗?

另同祝新年快乐 : )

cond 不应该,while_loop 有个限制是 condition 结果必须是明文的

最好可以提供一个有问题的 example,我来看看

@anakinxc anakinxc transferred this issue from secretflow/secretflow Jan 30, 2023
anakinxc pushed a commit that referenced this issue Jan 12, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [mdutils](https://togithub.com/didix21/mdutils) | `==1.4.0` ->
`==1.6.0` |
[![age](https://developer.mend.io/api/mc/badges/age/pypi/mdutils/1.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/pypi/mdutils/1.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/pypi/mdutils/1.4.0/1.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/mdutils/1.4.0/1.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>didix21/mdutils (mdutils)</summary>

###
[`v1.6.0`](https://togithub.com/didix21/mdutils/blob/HEAD/CHANGELOG.md#v160-2023-04-29)

[Compare
Source](https://togithub.com/didix21/mdutils/compare/v1.5.1...v1.6.0)

[Full
Changelog](https://togithub.com/didix21/mdutils/compare/v1.5.1...v1.6.0)

**Implemented enhancements:**

- DEPRECATION on setup.py
[#&#8203;80](https://togithub.com/didix21/mdutils/issues/80)
- Code adherence to PEP 8
[#&#8203;46](https://togithub.com/didix21/mdutils/issues/46)
- [GH-80](https://togithub.com/didix21/mdutils/issues/80): Support
poetry build system
[#&#8203;93](https://togithub.com/didix21/mdutils/pull/93)
([didix21](https://togithub.com/didix21))
- [GH-84](https://togithub.com/didix21/mdutils/issues/84): Add types to
mdutils methods
[#&#8203;85](https://togithub.com/didix21/mdutils/pull/85)
([didix21](https://togithub.com/didix21))

**Fixed bugs:**

- DeprecationWarning with Python 3.11
[#&#8203;90](https://togithub.com/didix21/mdutils/issues/90)

**Closed issues:**

- Add types on mdutils methods
[#&#8203;84](https://togithub.com/didix21/mdutils/issues/84)

**Merged pull requests:**

- [GH-90](https://togithub.com/didix21/mdutils/issues/90): Avoid warning
on python 3.11
[#&#8203;92](https://togithub.com/didix21/mdutils/pull/92)
([didix21](https://togithub.com/didix21))
- Fix build badge
[#&#8203;91](https://togithub.com/didix21/mdutils/pull/91)
([didix21](https://togithub.com/didix21))
- [GH-46](https://togithub.com/didix21/mdutils/issues/46): Add flake8
config [#&#8203;86](https://togithub.com/didix21/mdutils/pull/86)
([didix21](https://togithub.com/didix21))
- Import coverage to codecov
[#&#8203;66](https://togithub.com/didix21/mdutils/pull/66)
([didix21](https://togithub.com/didix21))

###
[`v1.5.1`](https://togithub.com/didix21/mdutils/blob/HEAD/CHANGELOG.md#v151-2023-03-11)

[Compare
Source](https://togithub.com/didix21/mdutils/compare/v1.5.0...v1.5.1)

[Full
Changelog](https://togithub.com/didix21/mdutils/compare/v1.5.0...v1.5.1)

**Fixed bugs:**

- MarkDownFile not accepting absolute file path anymore
[#&#8203;81](https://togithub.com/didix21/mdutils/issues/81)
- Fix [#&#8203;81](https://togithub.com/didix21/mdutils/issues/81):
Absolute path are broken
[#&#8203;83](https://togithub.com/didix21/mdutils/pull/83)
([didix21](https://togithub.com/didix21))

###
[`v1.5.0`](https://togithub.com/didix21/mdutils/blob/HEAD/CHANGELOG.md#v150-2023-02-11)

[Compare
Source](https://togithub.com/didix21/mdutils/compare/v1.4.0...v1.5.0)

[Full
Changelog](https://togithub.com/didix21/mdutils/compare/v1.4.0...v1.5.0)

**Implemented enhancements:**

- Introduce `header\_id` as parameter for headers
[#&#8203;78](https://togithub.com/didix21/mdutils/pull/78)
([kieferro](https://togithub.com/kieferro))

**Fixed bugs:**

- converting html tables to markdown creates unusable tables
[#&#8203;73](https://togithub.com/didix21/mdutils/issues/73)

**Closed issues:**

- Add Id to header
[#&#8203;77](https://togithub.com/didix21/mdutils/issues/77)
- mkreports package
[#&#8203;71](https://togithub.com/didix21/mdutils/issues/71)

**Merged pull requests:**

- Update setup-python version
[#&#8203;79](https://togithub.com/didix21/mdutils/pull/79)
([didix21](https://togithub.com/didix21))
- Modify tests to fix flakiness
[#&#8203;76](https://togithub.com/didix21/mdutils/pull/76)
([blazyy](https://togithub.com/blazyy))
- Fix typos in README
[#&#8203;75](https://togithub.com/didix21/mdutils/pull/75)
([kieferro](https://togithub.com/kieferro))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/secretflow/spu).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xMjcuMCIsInVwZGF0ZWRJblZlciI6IjM3LjEyNy4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants