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

[Support] Linear Congruential Random Engine #8642

Merged
merged 7 commits into from
Aug 6, 2021
Merged

Conversation

zxybazh
Copy link
Member

@zxybazh zxybazh commented Aug 4, 2021

This PR introduce a new implementation of linear congruential engine and its test. This linear congruential engine is a drop-in replacement for and strictly corresponds to std::minstd_rand but designed to be serializable and strictly reproducible. Specifically implemented for meta schedule but also reusable for other purposes.

The main differences are as follows:

  1. The random engine is stateless and its state is managed by the outside environment via a pointer to an int64_t variable. Therefore, the state of the random engine can be serialized as a single int64_t.
  2. The random engine's result is strictly reproducible, i.e, given a seed we can always reproduce the same tuning result.
  3. The randomness is still preserved because it's implemented as a linear congruential engine, with the same parameter setup as std::minstd_rand.
  4. It's platform-independent.

Copy link
Member

@junrushao junrushao left a comment

Choose a reason for hiding this comment

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

Thanks Xiyou! It overall looks good to me, especially the random numbers in cpptests :-) Please fix the minor comments

@junrushao junrushao changed the title Add Serializable and Reproducible Linear Congruential Engine for Random Number Generation [Support] Linear Congruential Random Engine Aug 4, 2021
@junrushao
Copy link
Member

The CI got stuck so I retriggered just now

include/tvm/support/random_engine.h Outdated Show resolved Hide resolved
include/tvm/support/random_engine.h Outdated Show resolved Hide resolved
include/tvm/support/random_engine.h Outdated Show resolved Hide resolved
include/tvm/support/random_engine.h Outdated Show resolved Hide resolved
@junrushao
Copy link
Member

junrushao commented Aug 4, 2021

The random engine can be serialized as a single int64_t

To be crystal clear, the engine cannot be "serialized" because it is stateless. Its state is managed by the outside environment via a pointer to int64_t

Copy link
Contributor

@comaniac comaniac left a comment

Choose a reason for hiding this comment

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

LGTM. Just a nit.

include/tvm/support/random_engine.h Outdated Show resolved Hide resolved
Copy link
Member

@junrushao junrushao left a comment

Choose a reason for hiding this comment

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

Thanks Xiyou!

@junrushao
Copy link
Member

CC @areusch @electriclilies if you guys are interested in review

@junrushao
Copy link
Member

Going to merge it in by the end of the day if there are no more comments :-)

@junrushao
Copy link
Member

Thanks @zxybazh for the awesome work, and @comaniac for review :-)

@zxybazh zxybazh deleted the rng branch August 6, 2021 00:09
mehrdadh pushed a commit to mehrdadh/tvm that referenced this pull request Aug 11, 2021
* Add linear congruential engine.

* Fix typo.

* Minor fix.

* Fix comments and intros.

* Change to unsigned.

* Minor comment fix.

* Fix unsigned rand state to signed.
ylc pushed a commit to ylc/tvm that referenced this pull request Sep 29, 2021
* Add linear congruential engine.

* Fix typo.

* Minor fix.

* Fix comments and intros.

* Change to unsigned.

* Minor comment fix.

* Fix unsigned rand state to signed.
ylc pushed a commit to ylc/tvm that referenced this pull request Jan 13, 2022
* Add linear congruential engine.

* Fix typo.

* Minor fix.

* Fix comments and intros.

* Change to unsigned.

* Minor comment fix.

* Fix unsigned rand state to signed.
MasterJH5574 pushed a commit that referenced this pull request Mar 15, 2024
…16722)

This PR implements `LinearCongruentialGenerator` in TVMjs,
following the C++ counterpart in #8642.
The motivation is that we want to seed autoregressive generation
to make results reproducible, supporting the OpenAI field `seed`.
The main function is `nextInt()`, which generates a number
`(0, 2^32 - 1)` non-inclusive.

Subsequently, we change all `Math.random()` in `runtime.ts` to
`this.rng.randomFloat()`, exposing API `Instance.setSeed()`.

Unit tests are added for `LinearCongruentialGenerator` for testing
seed and coverage.
thaisacs pushed a commit to thaisacs/tvm that referenced this pull request Apr 3, 2024
…pache#16722)

This PR implements `LinearCongruentialGenerator` in TVMjs,
following the C++ counterpart in apache#8642.
The motivation is that we want to seed autoregressive generation
to make results reproducible, supporting the OpenAI field `seed`.
The main function is `nextInt()`, which generates a number
`(0, 2^32 - 1)` non-inclusive.

Subsequently, we change all `Math.random()` in `runtime.ts` to
`this.rng.randomFloat()`, exposing API `Instance.setSeed()`.

Unit tests are added for `LinearCongruentialGenerator` for testing
seed and coverage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants