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

Dirichlet distribution with dynamic size n #14

Open
MortenLohne opened this issue Feb 25, 2025 · 5 comments
Open

Dirichlet distribution with dynamic size n #14

MortenLohne opened this issue Feb 25, 2025 · 5 comments

Comments

@MortenLohne
Copy link

Background

#8 changed the Dirichlet distribution to be const-generic over size, meaning it can no longer be used where the vector length is only known at runtime. This broke my use case, see motivation below.

What is your motivation?

Reinforcement learning for "chess-like" games. In AlphaZero and its many derivatives, Dirichlet noise is applied to each position's move probabilities vector, where the number of moves depends on the position and cannot be known at compile-time. Stack Exchange post with some context.

Feature request

Either revert Dirichlet back to having a dynamic size, or add it back as DirichletDynamic or similar. My choice would be to simply revert it, but it's honestly not clear to me why the change was done in the first place, since the const-generic version still requires the alloc feature. Was generating Dirichlet distributions a performance bottleneck for anyone?

@benjamin-lieser
Copy link
Member

I agree. There is a similar issue in rust-random/rand#1478 which would also introduce on static and one dynamic sized distribution.

We should definitely decide how to handle multidimensional distributions and it should provide the user with both options.
My current preferred way is just having one static and one dynamic one.

@MortenLohne
Copy link
Author

Do you have any concrete use cases for the static (i.e. const-generic) implementations of multi-dimensional distributions? I understand the general desire to reduce the number of allocations, but duplicating all the relevant distributions adds a lot of bloat, so it should need to justify itself imo.

@benjamin-lieser
Copy link
Member

I am pretty sure the only advantage is reducing the number of allocations, but this also allows it to be used in non std mode.

@dhardy
Copy link
Member

dhardy commented Feb 26, 2025

Thanks for the issue and PR @MortenLohne. As you noted, I did point out the limitation of rust-random/rand#1292 at the time, but I did not appreciate the impact.

Do you have any concrete use cases for the static (i.e. const-generic) implementations of multi-dimensional distributions?

This is a good question. I tried searching for usages of Dirichlet, but didn't find many (and most of what I did find looks like toy/student projects).

  • Uses with fixed-length, single-value (symmetric) input: omok-ai
  • Uses with fixed-length multi-value input: [none]
  • Uses with variable-length, single-value (symmetric) input: banditbench, onitama-alphazero, trees_rs
  • Uses with variable-length multi-value input: mixturs

Going by the above (very limited) data, there doesn't appear to be much use for compile-time-fixed-length input but there may be justification for a SymmetricDirichlet distribution if we want a specialised implementation.

I am pretty sure the only advantage is reducing the number of allocations, but this also allows it to be used in non std mode.

I doubt there's much need for Dirichlet in (embedded) non-alloc code.

but duplicating all the relevant distributions adds a lot of bloat

I did wonder if we could parameterise over the storage type (e.g. Dirichlet<Vec<f64>>), but we have field samplers: [Gamma<F>; N] which would require higher-kinded type generics or something (samplers: V<Gamma<V::Item>> where V: Iterator). So not possible, maybe not even in the far future of Rust.

@dhardy
Copy link
Member

dhardy commented Feb 26, 2025

I wonder if we should simply #[deprecate] rand_distr::Dirichlet and add a new crate, rand_multi or some such, for multi-dimensional inputs?

Edit: I moved this topic to #16.

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

No branches or pull requests

3 participants