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

feature: rng primitive refactoring #3040

Open
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

Alexandr-Solovev
Copy link
Contributor

@Alexandr-Solovev Alexandr-Solovev commented Jan 10, 2025

Description

Add a comprehensive description of proposed changes

List associated issue number(s) if exist(s): #6 (for example)

Documentation PR (if needed): #1340 (for example)

Benchmarks PR (if needed): IntelPython/scikit-learn_bench#155 (for example)


PR should start as a draft, then move to ready for review state after CI is passed and all applicable checkboxes are closed.
This approach ensures that reviewers don't spend extra time asking for regular requirements.

You can remove a checkbox as not applicable only if it doesn't relate to this PR in any way.
For example, PR with docs update doesn't require checkboxes for performance while PR with any change in actual code should have checkboxes and justify how this code change is expected to affect performance (or justification should be self-evident).

Checklist to comply with before moving PR from draft:

PR completeness and readability

  • I have reviewed my changes thoroughly before submitting this pull request.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation to reflect the changes or created a separate PR with update and provided its number in the description, if necessary.
  • Git commit message contains an appropriate signed-off-by string (see CONTRIBUTING.md for details).
  • I have added a respective label(s) to PR if I have a permission for that.
  • I have resolved any merge conflicts that might occur with the base branch.

Testing

  • I have run it locally and tested the changes extensively.
  • All CI jobs are green or I have provided justification why they aren't.
  • I have extended testing suite if new functionality was introduced in this PR.

Performance

  • I have measured performance for affected algorithms using scikit-learn_bench and provided at least summary table with measured data, if performance change is expected.
  • I have provided justification why performance has changed or why changes are not expected.
  • I have provided justification why quality metrics have changed or why changes are not expected.
  • I have extended benchmarking suite and provided corresponding scikit-learn_bench PR if new measurable functionality was introduced in this PR.

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev Alexandr-Solovev changed the title inc 1 feature: rng primitive refactoring Jan 22, 2025
@Alexandr-Solovev Alexandr-Solovev added the dpc++ Issue/PR related to DPC++ functionality label Jan 22, 2025
@Alexandr-Solovev Alexandr-Solovev marked this pull request as ready for review January 22, 2025 20:53
@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

seed_(seed) {
engines_.reserve(count_);
for (Size i = 0; i < count_; ++i) {
engines_.push_back(dpc_engine<EngineType>(queue, seed_));
Copy link
Contributor

Choose a reason for hiding this comment

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

This initialization looks meaningless for mt2203. Because different engine_idx should also be provided to have independent sequences of random numbers:
https://www.intel.com/content/www/us/en/docs/onemkl/developer-reference-dpcpp/2023-0/oneapi-mkl-rng-mt2203.html

///
/// @tparam EngineType The RNG engine type to be used. Defaults to `engine_method::mt2203`.
///
/// @param[in] seed The initial seed for the random number generator. Defaults to `777`.
Copy link
Contributor

Choose a reason for hiding this comment

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

The came comment as for dpc_engine apply here: need to add descriptions to the methods and functions; need to fix rule of five.

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

1 similar comment
@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

Copy link
Contributor

@icfaust icfaust left a comment

Choose a reason for hiding this comment

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

This is a review just for the DAAL side of things, will do a second review for the DAL side. DAAL looks rather straightforward, and looks as though oneDAL requires the most attention.

@@ -0,0 +1,183 @@
/* file: mrg32k3a.h */
/*******************************************************************************
* Copyright contributors to the oneDAL project
Copy link
Contributor

Choose a reason for hiding this comment

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

The files in cpp/daal/include/* seems to follow exactly the layout of the other RNG engines, so I have glanced through to make sure that they are the same, but didn't look super close. @Alexandr-Solovev let me know if there is anything specific to mrg32k3a or philox4x32x10 in these folders that are different. This implies that there isn't any architectural change to the RNG engines on the DAAL side of things outside of the inclusion of the two new ones. Is that so? (on second glance, it looks that we are doing a better standardization of the error codes, but thats it?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in daal there are no critical changes

@@ -0,0 +1,60 @@
/* file: mrg32k3a.cpp */
Copy link
Contributor

Choose a reason for hiding this comment

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

Overall structure in the daal/src/* also looks to match. I will focus on reviewing the batch_impl.h files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review!

{
switch (technique)
{
case engines::internal::family: return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Not anything you need to change in your PR, but I find it a bit odd that all of the RNGs in DAAL define or try unused skipping methods entirely even if the technique isn't known to be implemented (i.e. false) rather than just setting the errorcode directly. Am I misunderstanding things?

@@ -19,8 +19,8 @@
// Declaration of template function that calculate mt2203s.
//--

#ifndef __MCG59_KERNEL_H__
Copy link
Contributor

Choose a reason for hiding this comment

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

This is crazy, especially for something that is so widely used in the codebase. Good catch.

Copy link
Contributor

Choose a reason for hiding this comment

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

oh wow

/// Constructor that initializes the mt2203 generator for use on the GPU.
/// @param[in] queue The SYCL queue to manage device operations.
/// @param[in] seed The initial seed for the generator.
gen_mt2203(sycl::queue queue, std::int64_t seed) : _gen(queue, seed, 0) {}
Copy link
Contributor

@Vika-F Vika-F Jan 31, 2025

Choose a reason for hiding this comment

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

One more parameter should be added to the constructor.
Because mt2203 is a set of 6024 Mersenne Twister RNGs.

Suggested change
gen_mt2203(sycl::queue queue, std::int64_t seed) : _gen(queue, seed, 0) {}
gen_mt2203(sycl::queue queue, std::int64_t seed, std::int64_t engine_idx = 0) : _gen(queue, seed, engine_idx) {}

Comment on lines 38 to 40
for (std::int64_t i = 0; i < count_; ++i) {
engines_.push_back(device_engine<EngineType>(queue, base_seed_ + i));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

More correctly will be to initialize the collection with the different RNGs for mt2203:

Suggested change
for (std::int64_t i = 0; i < count_; ++i) {
engines_.push_back(device_engine<EngineType>(queue, base_seed_ + i));
}
if (EngineType == engine_type::mt2203)
{
for (std::int64_t i = 0; i < count_; ++i) {
engines_.push_back(device_engine<EngineType>(queue, base_seed_, i));
}
}
else
{
for (std::int64_t i = 0; i < count_; ++i) {
engines_.push_back(device_engine<EngineType>(queue, base_seed_ + i));
}
}

}
}

explicit host_engine(const daal::algorithms::engines::EnginePtr& eng) : host_engine_(eng) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I do not think that this is a proper fix to rule of three. Because a copy constructor should accept the object of the same type host_engine.

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev
Copy link
Contributor Author

correct last commit ci job
https://intel-ci.intel.com/efe23a65-55b1-f183-ad29-a4bf010d0e2d

Copy link
Contributor

@ethanglaser ethanglaser left a comment

Choose a reason for hiding this comment

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

Mostly reviewed, looking good! Please add PR description. Docs are not added, so this checkbox should not be checked. But none of the rst files for other engines are >70 lines so this should be easily added in this PR.

@@ -19,8 +19,8 @@
// Declaration of template function that calculate mt2203s.
//--

#ifndef __MCG59_KERNEL_H__
Copy link
Contributor

Choose a reason for hiding this comment

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

oh wow

@@ -19,7 +19,7 @@
#include "oneapi/dal/table/common.hpp"
#include "oneapi/dal/backend/primitives/ndarray.hpp"
#include "oneapi/dal/backend/primitives/utils.hpp"
#include "oneapi/dal/backend/primitives/rng/rng_engine_collection.hpp"
#include "oneapi/dal/backend/primitives/rng/host_engine_collection.hpp"
Copy link
Contributor

Choose a reason for hiding this comment

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

is the include here even utilized?

/// @param[in, out] dst Pointer to the array to be shuffled.
/// @param[in] engine_ Reference to the device engine.
template <typename Type>
void partial_fisher_yates_shuffle(ndview<Type, 1>& result_array,
Copy link
Contributor

Choose a reason for hiding this comment

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

Just wondering, why was this combined into the host_engine file? Previously it was in its own

/// @param[in] method The rng engine type. Defaults to `mt19937`.
/// @param[in] deps Dependencies for the SYCL event.
template <typename Type>
sycl::event partial_fisher_yates_shuffle(sycl::queue& queue_,
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be possible to somehow wrap the host function to avoid code duplication? because it looks like everything aside from the engine and event logic are the exact same

namespace internal
{
template <CpuType cpu, typename algorithmFPType = DAAL_ALGORITHM_FP_TYPE, Method method = defaultDense>
class BatchImpl : public algorithms::engines::mrg32k3a::interface1::Batch<algorithmFPType, method>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Not needed in this PR, but in general there seems to be a lot of overlap in functionality between these impls, could create a base class to define it and reduce duplication

const DataType* val_arr_2_host_ptr = arr_2_host.get_data();

for (std::int64_t el = 0; el < arr_2_host.get_count(); el++) {
// Due to MKL inside generates floats on GPU and doubles on CPU, it makes sense to add minor eps.
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe eps should be a function arg? also what is the range of values for results here - 0.01 seems a bit large

Copy link
Contributor

Choose a reason for hiding this comment

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

First test (host vs device) makes sense - what is goal of next two? and maybe they could be combined?

But overall host vs device test looks good, not sure if more test scope is required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dpc++ Issue/PR related to DPC++ functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants