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

fix: avoid additional array allocation in host to device transfer #2966

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

Conversation

Alexandr-Solovev
Copy link
Contributor

@Alexandr-Solovev Alexandr-Solovev commented Nov 4, 2024

Description

PR introduces fix for memcpy function in oneDAL. It saves memory and improve the performance of transferring data from host to device and back and gives an opportunity to use memcpy directly.


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 Alexandr-Solovev changed the title avoid additional array fix: avoid additional array allocation in host to device transfer Nov 5, 2024
@Alexandr-Solovev Alexandr-Solovev marked this pull request as ready for review November 5, 2024 07:39
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.

Looks like #2375 was just missing the wait_and_throw() :(

@Alexandr-Solovev Alexandr-Solovev changed the title fix: avoid additional array allocation in host to device transfer DO NOT MERGE fix: avoid additional array allocation in host to device transfer Nov 5, 2024
@Alexandr-Solovev
Copy link
Contributor Author

Please dont merge, until the additional check on local machines

@Alexandr-Solovev
Copy link
Contributor Author

Looks like #2375 was just missing the wait_and_throw() :(

not sure, looks like in all places where the function is called there is additional wait_and_throw()

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev Alexandr-Solovev marked this pull request as draft November 20, 2024 12:42
@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev Alexandr-Solovev marked this pull request as ready for review November 21, 2024 08:10
@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

@Alexandr-Solovev Alexandr-Solovev changed the title DO NOT MERGE fix: avoid additional array allocation in host to device transfer fix: avoid additional array allocation in host to device transfer Nov 21, 2024
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.

Made comments help to me understand. Would it be worth just adding some? Otherwise good to go by me (if my comments in the review are correct)

@@ -130,33 +130,20 @@ static void pull_row_major_impl(const Policy& policy,

auto src_data = origin_data.get_data() + origin_offset * origin_dtype_size;
auto dst_data = block_data.get_mutable_data();

if (block_info.get_column_count() > 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just because I am not familiar, it looks like just removing the if and relying on the for loop (a simplification here).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes!

dal::detail::check_mul_overflow(src_element_size_in_bytes, element_count);
const std::int64_t src_stride_in_bytes =
dal::detail::check_mul_overflow(src_element_size_in_bytes, src_stride);
if (src_type == dst_type && src_size_in_bytes == dst_size_in_bytes &&
Copy link
Contributor

Choose a reason for hiding this comment

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

If allocation exists, skip the convert_convert vector and uses memcpy_host2usm

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue, that when we copy from host to device in different data types(float on host and allocated on device in double), we still just copy bytes, and in the scenario of usage different data types it makes sense to use strides

@@ -227,33 +227,50 @@ sycl::event convert_vector_device2host(sycl::queue& q,
// To perform conversion, we gather data from device to host in temporary
// contigious array and then run host conversion function

const std::int64_t element_size_in_bytes = dal::detail::get_data_type_size(src_type);
const std::int64_t dst_element_size_in_bytes = dal::detail::get_data_type_size(dst_type);
Copy link
Contributor

Choose a reason for hiding this comment

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

collect more information about the destination to see if a shortcut can be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly!

Copy link
Contributor

@samir-nasibli samir-nasibli left a comment

Choose a reason for hiding this comment

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

Indeed good suggestion! Thank you @Alexandr-Solovev
Could you please share perf improvements?

@Alexandr-Solovev
Copy link
Contributor Author

/intelci: run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants