Skip to content

Add asynchronous_complete_cumsum operator for XPU - #73

Draft
aagalleg wants to merge 25 commits into
intel:mainfrom
aagalleg:feat/asynchronous_complete_cumsum
Draft

Add asynchronous_complete_cumsum operator for XPU#73
aagalleg wants to merge 25 commits into
intel:mainfrom
aagalleg:feat/asynchronous_complete_cumsum

Conversation

@aagalleg

@aagalleg aagalleg commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Depends on: #72

This PR introduces the asynchronous_complete_cumsum operator to fbgemm-xpu, enabling complete cumulative sum computation with a leading zero on Intel XPU devices.

Changes

Core Implementation

  • XPU Implementation (asynchronous_complete_cumsum.cpp/h): SYCL port of FBGEMM's asynchronous complete cumsum operator that computes cumulative sum with prepended zero (e.g., [a, b, c][0, a, a+b, a+b+c])
  • 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 and documentation for easy integration

Infrastructure

  • Build System (CMakeLists.txt): Added implementation to build configuration
  • Tensor Support: Handles both 1D and 2D tensors (cumsum along last dimension for 2D)
  • Data Types: Supports int32 and int64 tensors

Testing

  • Comprehensive Test Suite (test_asynchronous_complete_cumsum.py):
    • Correctness validation for int32/int64 data types
    • Edge cases (empty tensors)
    • Random input validation with numpy reference
    • XPU device testing with proper error handling

cc: @flezaalv, @manuelhsantana

@dvrogozh dvrogozh left a comment

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.

In addition to inline comments:

  1. Repository has lint CI - please, address identified issues.
  2. What we implement and merge must be tested under CI. And IMHO doing that by engineers who implemented the code is the best variant rather than offloading that to dedicated CI engineers. Thus, please, modify .github/workflows/ci.yml and add a test section for FBGEMM following the pattern outlined for torchcodec (see link below). The test job for FBGEMM will be much simpler. Target only bmg for now is fine. Note that fbgemm wheel is already getting build by CI.

test-torchcodec:

.has_value();
}

} // namespace fbgemm_xpu::utils::torch No newline at end of file

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.

add new line in end of file

}
}

void InvertPermuteKernelInt64::operator()(const sycl::nd_item<1>& item) const {

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 seems templetable kernel over the operand type. Why don't we do that especially considering that the reference CUDA kernel is doing exactly that [1]?

[1] https://github.com/pytorch/FBGEMM/blob/8744183e4bd7390a674a0d8736bf4d7ef2d0516e/fbgemm_gpu/src/sparse_ops/sparse_invert_permute.cu#L15

* @brief Permute1DDataWithWeightsKernel operator implementation
*/
template <typename offsets_t, typename indices_t, typename weights_t>
void Permute1DDataWithWeightsKernel<offsets_t, indices_t, weights_t>::operator()(

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.

Why not to follow CUDA reference [1] and additionally parameterize with has_weight?

[1] https://github.com/pytorch/FBGEMM/blob/8744183e4bd7390a674a0d8736bf4d7ef2d0516e/fbgemm_gpu/src/sparse_ops/sparse_permute_1d.cu#L30


__all__ = [
"dense_embedding_codegen_lookup_function",
"invert_permute",

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.

Here I am confused. Why do we expose these XPU specific APIs from fbgemm xpu? who is supposed to use them? My understanding was that we are just register low level entrypoints for XPU key so that standard fbgemm APIs start to work on XPU device. Shouldn't that be that user calls fbgemm::invert_permute for example?


def asynchronous_complete_cumsum(t_in: Tensor) -> Tensor:
"""Computes complete cumulative sum: output[0] = 0, output[i] = sum(t_in[0:i])."""
return torch.ops.fbgemm.asynchronous_complete_cumsum.default(t_in) No newline at end of file

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.

add empty line in end of file

@@ -0,0 +1,71 @@
"""

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.

Should also have a copyright.

For 1D input [a, b, c], returns [0, a, a+b, a+b+c].
For 2D input, cumsum along dimension 1 (columns).
"""

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.

Tests are eventually good, but our side tests won't guarantee that what we've implemented correctly integrates with the upstream FBGEMM. We must explore if upstream tests cover what we are implementing and use them if they are. If not, then we need to raise PRs with upstream to add test coverage. That's critical concern - we need to resolve that before proceeding.

torch.manual_seed(SEED)

def test_basic_int32(self):
if 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.

This check is inconsistent with checks in other test file: @unittest.skipIf(not torch.xpu.is_available(), "XPU not available").

@dvrogozh
dvrogozh marked this pull request as draft June 22, 2026 18:36
@aagalleg
aagalleg force-pushed the feat/asynchronous_complete_cumsum branch from 389845d to dcdd4df Compare July 15, 2026 17:03
aagalleg and others added 22 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.
- 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.
Signed-off-by: Felipe Leza Alvarez <felipe.leza.alvarez@intel.com>
Add SYCL port of FBGEMM's asynchronous_complete_cumsum operator for
Intel XPU devices. The operator computes a complete cumulative sum
with a leading zero (e.g., [a, b, c] → [0, a, a+b, a+b+c]).
Integrate asynchronous_complete_cumsum operator into fbgemm-xpu:
- Add Python wrapper with complete cumsum documentation
- Register operator schema in torch library
- Include implementation in CMake build
Delete the accidentally tracked submodule reference to FBGEMM-v1.7.0.
@flezaalv
flezaalv force-pushed the feat/asynchronous_complete_cumsum branch from c458218 to bc7c7a9 Compare July 29, 2026 22:39
flezaalv added 2 commits July 29, 2026 23:37
Signed-off-by: Felipe Leza Alvarez <felipe.leza.alvarez@intel.com>
Signed-off-by: Felipe Leza Alvarez <felipe.leza.alvarez@intel.com>
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