|
| 1 | +# Copyright 2026, The FlagOS Contributors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# Generated by KernelGen: https://github.com/flagos-ai/KernelGen |
| 16 | +import logging |
| 17 | + |
| 18 | +import torch |
| 19 | +import triton |
| 20 | +import triton.language as tl |
| 21 | + |
| 22 | +from flag_gems.utils import libentry |
| 23 | +from flag_gems.utils import triton_lang_extension as tle |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +@libentry() |
| 29 | +@triton.jit |
| 30 | +def _jagged_to_padded_dense_forward_kernel( |
| 31 | + values, |
| 32 | + offsets, |
| 33 | + output, |
| 34 | + padding_value: tl.constexpr, |
| 35 | + batch_size: tl.constexpr, |
| 36 | + max_length: tl.constexpr, |
| 37 | + BLOCK_SIZE: tl.constexpr, |
| 38 | +): |
| 39 | + """Kernel for converting jagged tensor to padded dense tensor. |
| 40 | +
|
| 41 | + Args: |
| 42 | + values: 1D tensor containing concatenated variable-length sequences |
| 43 | + offsets: 1D tensor of start positions for each sequence |
| 44 | + output: 2D output tensor of shape (batch_size, max_length) |
| 45 | + padding_value: scalar value for padding |
| 46 | + batch_size: number of sequences |
| 47 | + max_length: maximum length of each sequence |
| 48 | + """ |
| 49 | + pid = tle.program_id(axis=0) |
| 50 | + batch_idx = pid |
| 51 | + |
| 52 | + if batch_idx >= batch_size: |
| 53 | + return |
| 54 | + |
| 55 | + # Get the start and end offset for this sequence |
| 56 | + seq_start = tl.load(offsets + batch_idx) |
| 57 | + seq_end = tl.load(offsets + batch_idx + 1) |
| 58 | + |
| 59 | + # Calculate the actual sequence length |
| 60 | + seq_length = seq_end - seq_start |
| 61 | + |
| 62 | + # Compute the row offset in the output |
| 63 | + row_offset = batch_idx * max_length |
| 64 | + |
| 65 | + # Fill with padding value (vectorized per block) |
| 66 | + for j in tl.range(0, max_length, BLOCK_SIZE): |
| 67 | + out_offsets = row_offset + j + tl.arange(0, BLOCK_SIZE) |
| 68 | + out_mask = (j + tl.arange(0, BLOCK_SIZE)) < max_length |
| 69 | + tl.store(output + out_offsets, padding_value, mask=out_mask) |
| 70 | + |
| 71 | + # Copy actual values (vectorized per block) |
| 72 | + for j in tl.range(0, seq_length, BLOCK_SIZE): |
| 73 | + offsets_vec = seq_start + j + tl.arange(0, BLOCK_SIZE) |
| 74 | + mask = offsets_vec < seq_end |
| 75 | + |
| 76 | + values_vec = tl.load(values + offsets_vec, mask=mask, other=padding_value) |
| 77 | + |
| 78 | + out_offsets = row_offset + j + tl.arange(0, BLOCK_SIZE) |
| 79 | + out_mask = (j + tl.arange(0, BLOCK_SIZE)) < seq_length |
| 80 | + |
| 81 | + tl.store(output + out_offsets, values_vec, mask=out_mask) |
| 82 | + |
| 83 | + |
| 84 | +def _jagged_to_padded_dense_forward(values, offsets, max_lengths, padding_value=0.0): |
| 85 | + """Convert a jagged (variable-length) tensor to a padded dense tensor. |
| 86 | +
|
| 87 | + Args: |
| 88 | + values: 1D tensor containing concatenated variable-length sequences |
| 89 | + offsets: List of 1D tensors containing start positions for each sequence |
| 90 | + max_lengths: List of integers specifying maximum length for each batch dimension |
| 91 | + padding_value: Value to use for padding (default: 0.0) |
| 92 | +
|
| 93 | + Returns: |
| 94 | + Padded dense tensor |
| 95 | + """ |
| 96 | + logger.debug("GEMS JAGGED TO PADDED DENSE FORWARD") |
| 97 | + |
| 98 | + # Currently only supports single batch dimension |
| 99 | + if not isinstance(offsets, (list, tuple)): |
| 100 | + offsets = [offsets] |
| 101 | + if not isinstance(max_lengths, (list, tuple)): |
| 102 | + max_lengths = [max_lengths] |
| 103 | + |
| 104 | + num_batch_dims = len(offsets) |
| 105 | + assert ( |
| 106 | + num_batch_dims == 1 |
| 107 | + ), f"Only single batch dimension is supported, got {num_batch_dims}" |
| 108 | + |
| 109 | + # Single batch dimension: 1D values, 1D offsets |
| 110 | + offsets_0 = offsets[0] |
| 111 | + batch_size = offsets_0.numel() - 1 |
| 112 | + max_length = max_lengths[0] |
| 113 | + |
| 114 | + # Compute output shape |
| 115 | + # For single batch dim: (batch_size, max_length) |
| 116 | + output_shape = (batch_size, max_length) |
| 117 | + output = torch.empty(output_shape, dtype=values.dtype, device=values.device) |
| 118 | + |
| 119 | + grid = lambda meta: (batch_size,) |
| 120 | + _jagged_to_padded_dense_forward_kernel[grid]( |
| 121 | + values, |
| 122 | + offsets_0, |
| 123 | + output, |
| 124 | + padding_value, |
| 125 | + batch_size, |
| 126 | + max_length, |
| 127 | + # BLOCK_SIZE=128 balances occupancy and memory efficiency for typical sequence lengths |
| 128 | + BLOCK_SIZE=128, |
| 129 | + ) |
| 130 | + |
| 131 | + return output |
0 commit comments