Skip to content

Add permute_1D_sparse_data Operation for Intel XPU - #72

Open
aagalleg wants to merge 21 commits into
intel:mainfrom
aagalleg:feat/permute_1d_sparse_data
Open

Add permute_1D_sparse_data Operation for Intel XPU#72
aagalleg wants to merge 21 commits into
intel:mainfrom
aagalleg:feat/permute_1d_sparse_data

Conversation

@aagalleg

@aagalleg aagalleg commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Depends on #71

Add permute_1D_sparse_data Operation for Intel XPU

This PR introduces the permute_1D_sparse_data operator to fbgemm-xpu, enabling permutation of sparse data in jagged/1D format on Intel XPU devices.

Changes

Core Implementation

  • SYCL Kernels (permute_1d_sparse_data.cpp/h): SYCL implementation with three specialized functors for permuting lengths, indices, and weighted data, plus optimized cumsum helpers for offset computation
  • Operator Registration (ops_registry.cpp): Registers the operator with PyTorch's dispatch system using conditional schema registration to avoid conflicts
  • Python API (ops.py): Clean Python wrapper function with type hints supporting optional weights and length sum parameters

Infrastructure

  • Build System (CMakeLists.txt): Added SYCL kernel to build configuration

Testing

  • Test Suite (test_permute_1d_sparse_data.py):
    • Correctness validation with and without weights
    • Various segment configurations (uniform, mixed lengths, empty segments)
    • Edge cases (empty tensors, single segments, identity permutations)
    • Random permutations with validation
    • CPU-XPU consistency verification

cc: @manuelhsantana, @flezaalv

@dvrogozh
dvrogozh marked this pull request as draft June 22, 2026 18:36
flezaalv added a commit to aagalleg/torchlib-xpu that referenced this pull request Jul 13, 2026
@flezaalv

Copy link
Copy Markdown
Contributor

Depends on #71

Add permute_1D_sparse_data Operation for Intel XPU

This PR introduces the permute_1D_sparse_data operator to fbgemm-xpu, enabling permutation of sparse data in jagged/1D format on Intel XPU devices.

Changes

Core Implementation

  • SYCL Kernels (permute_1d_sparse_data.cpp/h): SYCL implementation with three specialized functors for permuting lengths, indices, and weighted data, plus optimized cumsum helpers for offset computation
  • Operator Registration (ops_registry.cpp): Registers the operator with PyTorch's dispatch system using conditional schema registration to avoid conflicts
  • Python API (ops.py): Clean Python wrapper function with type hints supporting optional weights and length sum parameters

Infrastructure

  • Build System (CMakeLists.txt): Added SYCL kernel to build configuration

Testing

  • Test Suite (test_permute_1d_sparse_data.py):

    • Correctness validation with and without weights
    • Various segment configurations (uniform, mixed lengths, empty segments)
    • Edge cases (empty tensors, single segments, identity permutations)
    • Random permutations with validation
    • CPU-XPU consistency verification

cc: @manuelhsantana, @flezaalv

The test suite has been replaced with the patch 0001-Add-XPU-support-to-fbgemm-tests.patch for running upstream FBGEMM tests for permute_2d_sparse_data.
Removed ops.py file (no longer necessary).

flezaalv added a commit to aagalleg/torchlib-xpu that referenced this pull request Jul 28, 2026
@flezaalv
flezaalv force-pushed the feat/permute_1d_sparse_data branch from 33197a5 to ea4b40f Compare July 28, 2026 19:01
aagalleg added 10 commits July 29, 2026 16:24
- Add invert_permute kernel to CMake build
- Implement invert_permute Python wrapper in ops.py
- Register invert_permute operator with schema existence check
- Add torch_library.h utility for schema validation
Add SYCL/XPU kernel implementation for invert_permute operation.
Add complete test coverage for invert_permute operator on XPU
devices, covering correctness, validation, parity, and performance.

Test coverage includes:
- Correctness tests for int32/int64 with edge cases (empty, single
  element, identity, reverse, random permutations)
- Input validation tests for invalid dimensions and dtypes
- Meta function tests for torch.compile compatibility
- PyTorch opcheck validation for operator conventions
- Parametric tests with varying sizes (1 to 1M elements)
- CPU-XPU parity tests to ensure consistent results
- Performance benchmarks measuring execution time and bandwidth
Replace the custom standalone test_invert_permute.py with a git-am
patch applied to upstream FBGEMM v1.7.0 misc_ops_test.py, following the
torchcodec-xpu convention. The patch makes test_invert_permute run on
XPU (permute.xpu(), gated on torch.xpu.is_available()) and skips the
remaining operator tests that are not implemented on XPU.
Collapse the two type-specific kernel functors (InvertPermuteKernelInt32,
InvertPermuteKernelInt64) into a single templated functor
InvertPermuteKernel<index_t>, mirroring the reference CUDA kernel
invert_permute_kernel<index_t>.
…cture

- Fix test patches to match FBGEMM v1.8.0 tests.
- Move test patches from test/patches/ subdirectory to patches/ at the
package root level for better organization. Remove now-unnecessary
.gitkeep file and update patch with correct base commit reference.
Replace TODO placeholders with actual commands to apply the FBGEMM XPU
test patch and execute pytest for misc_ops_test.py in the CI pipeline.
flezaalv added a commit to aagalleg/torchlib-xpu that referenced this pull request Jul 29, 2026
@flezaalv
flezaalv force-pushed the feat/permute_1d_sparse_data branch from e15792b to a31b216 Compare July 29, 2026 17:36
aagalleg and others added 7 commits July 29, 2026 17:42
- CMakeLists: add permute_1d_sparse_data.cpp to build sources
- ops.py: add Python wrapper with type hints
- ops_registry.cpp: register operator schema in fbgemm namespace
Implement SYCL/XPU kernel implementation of permute_1D_sparse_data
operator for sparse jagged/1D format data permutation.
Replaced test for upstream patched FBGEMM tests that enables testing XPU. This file is no longer needed.
@flezaalv
flezaalv force-pushed the feat/permute_1d_sparse_data branch from a31b216 to 936e378 Compare July 29, 2026 17:43
Signed-off-by: Felipe Leza Alvarez <felipe.leza.alvarez@intel.com>
@aagalleg
aagalleg marked this pull request as ready for review July 29, 2026 21:55
low=1,
high=L,
size=(T,), # 1D tensor with T elements
- device=torch.accelerator.current_accelerator(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should not be needed as torch.accelerator.current_accelerator() should return xpu as in trivial:

$ python -c "import torch; print(torch.xpu.is_available()); print(torch.accelerator.current_accelerator())"
True
xpu

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That line will be deleted by the patch and the device="xpu" will be added, the result code will be:

 lengths = torch.randint(
            low=1,
            high=L,
            size=(T,),  # 1D tensor with T elements
            device="xpu",
        ).type(index_dtype)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I understand what this patch is currently doing and I am arguing that this should not be needed as upstream code should already do what you are trying to achieve - torch.accelerator.current_accelerator() should return xpu.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

got it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reverted, using the original torch.accelerator.current_accelerator() instead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you verify that it works?

@flezaalv flezaalv Jul 31, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, I did. The pytest command is already integrated into the Test fbgemm-xpu step: https://github.com/intel/torchlib-xpu/actions/runs/30579440544/job/90997602510#step:13:3

…st patch

Signed-off-by: Felipe Leza Alvarez <felipe.leza.alvarez@intel.com>
- @unittest.skipIf(*running_in_oss)
- @unittest.skipIf(*gpu_unavailable)
+ @unittest.skipIf(
+ gpu_unavailable[0] and not torch.xpu.is_available(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok for now, but I suspect that going forward we will need to mimic gpu_unavailable.

/**
* @brief Permute1DLengthsKernel operator implementation
*/
template <typename index_t>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done at commit ce10815

* output[n] = sum(all input)
*/
template <typename T>
at::Tensor complete_cumsum_xpu(const at::Tensor& input) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same as above. This seems to be defined elsewhere in the cuda reference. Why do we define it here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@manuelhsantana why does exclusive_cumsum_xpu and complete_cumsum_xpu have the same code?
Also, the original code of permute_1D_sparse_data uses asynchronous_complete_cumsum that is an operator that we did implement to enable TorchRec, why wasn't that implementation used?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

complete_cumsum_xpu can be changed in PR #73 for asynchronouse_complete_cumsum if they provide the same results. But there is still the mystery of why exclusive_cumsum_xpu and complete_cumsum_xpu have the same code.

Introduce a permute_t template parameter on Permute1DLengthsKernel
(defaulting to int32_t).
@dvrogozh

dvrogozh commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

@aagalleg , this PR requires manual rebase for whatever reason (I am not sure what's wrong). Please, try to rebase. Once done I will attempt to merge.

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