-
Notifications
You must be signed in to change notification settings - Fork 62
QR operator utilizing XPU. #2399
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
base: main
Are you sure you want to change the base?
Changes from all commits
78e95cd
60b868b
2267fca
ba893b0
e0f5f6b
16028d1
c40b846
025291a
6e025e1
5e593d8
2038538
e564e1a
8f5a74c
3c26a9e
1531fa1
980732b
e8b49e7
419eb57
a56a4ff
7906ef2
c995189
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |||
| #include <ATen/native/BatchLinearAlgebra.h> | ||||
| #include <ATen/native/DispatchStub.h> | ||||
| #include <ATen/native/LinearAlgebraUtils.h> | ||||
| #include <ATen/ops/linalg_qr_native.h> | ||||
| #include <ATen/ops/linalg_qr_cpu_dispatch.h> | ||||
| #if defined(USE_ONEMKL_XPU) | ||||
| #include <ATen/native/xpu/mkl/BatchLinearAlgebra.h> | ||||
| #endif // USE_ONEMKL_XPU | ||||
|
|
@@ -64,4 +66,21 @@ void lu_factor_kernel_xpu( | |||
|
|
||||
| REGISTER_XPU_DISPATCH(lu_factor_stub, &lu_factor_kernel_xpu); | ||||
|
|
||||
| TORCH_IMPL_FUNC(linalg_qr_xpu_out)(const Tensor& A, | ||||
| std::string_view mode, | ||||
| const Tensor & Q, | ||||
| const Tensor & R) { | ||||
| #if defined(USE_ONEMKL_XPU) | ||||
| xpu::linalg_qr_kernel(A, mode, Q, R); | ||||
| #else | ||||
| auto A_cpu = A.to(at::kCPU); | ||||
| auto Q_cpu = at::empty_like(Q, at::kCPU); | ||||
| auto R_cpu = at::empty_like(R, at::kCPU); | ||||
| at::cpu::linalg_qr_out(Q_cpu, R_cpu, A_cpu, mode); | ||||
| Q.copy_(Q_cpu); | ||||
| R.copy_(R_cpu); | ||||
| #endif // USE_ONEMKL_XPU | ||||
| } | ||||
|
Comment on lines
+69
to
+83
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My suggestion is to register |
||||
|
|
||||
|
|
||||
|
||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -561,4 +561,133 @@ void lu_factor_mkl( | |||||||||||||||||||||||||||||||||
| pivots.copy_(pivots_); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| template <typename scalar_t> | ||||||||||||||||||||||||||||||||||
| void linalg_qr_kernel_impl( | ||||||||||||||||||||||||||||||||||
| const at::Tensor& A, | ||||||||||||||||||||||||||||||||||
| std::string_view mode, | ||||||||||||||||||||||||||||||||||
| const at::Tensor& Q, | ||||||||||||||||||||||||||||||||||
| const at::Tensor& R) { | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| at::Tensor a_contig = A.contiguous(); | ||||||||||||||||||||||||||||||||||
| at::Tensor result_r = at::clone(a_contig); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| auto options = at::TensorOptions().dtype(A.dtype()).device(kXPU); | ||||||||||||||||||||||||||||||||||
| auto dimensions = A.sizes(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| result_r = result_r.transpose(-2, -1).contiguous(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| int numel = a_contig.numel(); | ||||||||||||||||||||||||||||||||||
| int range = a_contig.dim(); | ||||||||||||||||||||||||||||||||||
| int64_t n = a_contig.sizes().at(range - 2); | ||||||||||||||||||||||||||||||||||
| int64_t m = a_contig.sizes().at(range - 1); | ||||||||||||||||||||||||||||||||||
| int64_t mn = int64_t(m * n); | ||||||||||||||||||||||||||||||||||
| int64_t b = numel ==0 ? 0 : numel / mn; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| int64_t b = numel ==0 ? 0 : numel / mn; | |
| int64_t b = numel == 0 ? 0 : numel / mn; |
Copilot
AI
Dec 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing spaces around comparison operators. Should be b == 0, mode == \"complete\", and n > 0 for consistency with coding standards.
| if (b==0 && mode=="complete" && n>0) { | |
| if (b == 0 && mode == "complete" && n > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at::native::batchCount can help you get flattened batch size. https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/native/LinearAlgebraUtils.h#L112
Copilot
AI
Dec 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating a vector with size 0 dimension {0} may cause issues when used to construct q_dimensions. This should likely be an empty vector std::vector<long>() or retain the original dimensions.
| v = std::vector<long>({0}); | |
| v = std::vector<long>(); |
Copilot
AI
Dec 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Variable name 'v' is ambiguous. Consider renaming to something more descriptive like 'q_dimensions_vec' to clarify its purpose.
| std::vector v(dimensions.begin(), dimensions.end()); | |
| if (mode != "r") { | |
| v[range - 1] = v[range - 2]; | |
| v[range - 2] = out_q_columns; | |
| } else { | |
| v = std::vector<long>({0}); | |
| } | |
| auto q_dimensions = at::IntArrayRef(v); | |
| std::vector<long> q_dimensions_vec(dimensions.begin(), dimensions.end()); | |
| if (mode != "r") { | |
| q_dimensions_vec[range - 1] = q_dimensions_vec[range - 2]; | |
| q_dimensions_vec[range - 2] = out_q_columns; | |
| } else { | |
| q_dimensions_vec = std::vector<long>({0}); | |
| } | |
| auto q_dimensions = at::IntArrayRef(q_dimensions_vec); |
Copilot
AI
Dec 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The scratchpad size calculations use n+1 and m+1, but the actual LAPACK calls on lines 636 and 644-654 use n and m without the +1 offset. This dimension mismatch could lead to buffer overflows or incorrect scratchpad allocation.
| oneapi::mkl::lapack::geqrf_scratchpad_size<scalar_t>(queue, n+1, m+1, n+1); | |
| int64_t bufsize2 = | |
| oneapi::mkl::lapack::orgqr_scratchpad_size<scalar_t>(queue, n+1, m+1, m+1, n+1); | |
| oneapi::mkl::lapack::geqrf_scratchpad_size<scalar_t>(queue, n, m, n); | |
| int64_t bufsize2 = | |
| oneapi::mkl::lapack::orgqr_scratchpad_size<scalar_t>(queue, n, out_q_columns, tau_len, n); |
Copilot
AI
Dec 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing spaces around '!=' operator. Should be mn != 0 for consistency.
| if (mn!=0) // make QR if there is something to orthogonalize | |
| if (mn != 0) // make QR if there is something to orthogonalize |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9443,6 +9443,17 @@ | |
| - func: linalg_solve(Tensor A, Tensor B, *, bool left=True) -> Tensor | ||
| python_module: linalg | ||
|
|
||
| - func: linalg_qr(Tensor A, str mode='reduced') -> (Tensor Q, Tensor R) | ||
| python_module: linalg | ||
| variants: function | ||
| structured_delegate: linalg_qr.out | ||
|
|
||
| - func: linalg_qr.out(Tensor A, str mode='reduced', *, Tensor(a!) Q, Tensor(b!) R) -> (Tensor(a!) Q, Tensor(b!) R) | ||
| python_module: linalg | ||
| structured: True | ||
| dispatch: | ||
| XPU: linalg_qr_xpu_out | ||
|
|
||
|
Comment on lines
+9446
to
+9456
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please refer to https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/native/native_functions.yaml#L14623 and consider stub mentioned above.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using existing PT stub for implementing QR has both pros and cons. At the side of pros, we take the most of the existing infrastructure with simplified flow on sycl/xpu side. The implementation proposed here has, however also several strengths. Namely:
Thus, although provitidn these two kernels separately is a natural development path, I would prioritize it for later, after QR and two pending requests will be ready.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mwiktor-intel For layout differences, could you indicate in MKL document the reason why LAPACK is row-major? https://www.intel.com/content/www/us/en/docs/onemkl/developer-reference-dpcpp/2025-2/geqrf-usm-version.html Please pay attention to leading dimension
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LDA cannot be replacement of stride. LDA can be used to skip some elements in matrix, but with LDA we cannot force difference data orientation. Specifically: say we have 3x2 matrix. Lapack expects this 6 element array in memory to represent the data as follows: Some BLAS function have row_major of col_major order, but it is not the case within LAPACK.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the elaboration. Let me rephase your example 1: given 3x2 matrix stored as [0, 2, 4, 1, 3, 5] in memory, the logic order is [[0, 1], [2, 3], [4, 5]] if this matrix is col-major, right? You mentioned "LDA here is 2", but why LDA here can be smaller than the number of rows? |
||
| - func: linalg_inv_ex(Tensor A, *, bool check_errors=False) -> (Tensor inverse, Tensor info) | ||
| python_module: linalg | ||
| structured_delegate: linalg_inv_ex.inverse | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The include
linalg_qr_cpu_dispatch.happears unused in this file since the CPU fallback usesat::cpu::linalg_qr_outwhich should be available through the native header.