You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Investigate the root causes in DeviceScan that lead to invalid values being passed to the custom reduction operator #5019
The handling of out-of-bounds items is clearly a root cause because the underlying BlockScan algorithm does not have any way of specifying a reduced number of elements. Currently only few block-algorithms (BlockLoad and BlockStore + some exceptions like single element-per-thread BlockReduce and *PartialTile versions in BlockAdjacentDifference) have overloads taking a runtime value for the number of elements. This is used to fill the padding with the first item of the tile which becomes an invalid value being passed to the scan operator because it is copied and commuted to the end.
TailSegmentedReduce as used to compute the prefix from the results of previous blacks does not seem to be a root cause as it does not apply its reduction operator when it would combine results from different segments. Combining values among each segment should be fine, even though we are only interested in the first segment (containing the last values due to how we map threads to memory).
The following code waits for the tile states of the next window to have a status of anything other than SCAN_TILE_INVALID, so either SCAN_TILE_OOB or SCAN_TILE_PARTIAL or SCAN_TILE_INCLUSIVE and then fill our value correspondingly:
The value is then passed to the TailSegmentedReduce described in the previous comment.
The implementation of the ScanTileState and therefore WaitForValid() depends on the element type being fundamental (detail::is_primitive<T>::value) or not.
Fundamental
In this case a kind of AoS implementation is used where the status and the value are packed into one TxnWord aliased to TileDescriptor:
So once the status is not SCAN_TILE_INVALID anymore for all lanes of the warp (only first warp of the block executes this), we just take the value from the already loaded TileDescriptor:
if ((blockIdx.x == 0) && (threadIdx.x < TILE_STATUS_PADDING))
{
// Padding
descriptor->status = StatusWord(SCAN_TILE_OOB);
d_tile_descriptors[threadIdx.x] = val;
}
}
As we can see, the value part is never initialized but should be zeroed from initializing TxnWord. This zeroed value is an invalid element.
Non-fundamental
Here the ScanTileState is implemented in a SoA-fashion so the value is only loaded from gmem once the status of all lanes is not SCAN_TILE_INVALID anymore:
value = ThreadLoad<LOAD_CG>(d_tile_inclusive + TILE_STATUS_PADDING + tile_idx);
}
}
In case of SCAN_TILE_OOB, value is not modified at all and remains in the default initialized state from the first code snippet in this comment which is an invalid value.
Solution
Luckily there seems to be an easy fix for avoiding these invalid values being passed to the scan operator that might also be relatively harmless in terms of performance:
Just change the calculation of tail flags above to
int tail_flag = (predecessor_status == StatusWord(SCAN_TILE_INCLUSIVE) || predecessor_status == StatusWord(SCAN_TILE_OOB));
This means that every OOB value gets its own segment which, according to my previous comment about the workings of TailSegmentedReduce(), is not combined with the values of other segments (specifically the other OOB values).
After finding higher than expected error counts in our test, we found another root cause: The BlockScan can be problematic even when not handling OOB items. In particular BLOCK_SCAN_WARP_SCANS dispatches to detail::WarpScanShfl which runs
if (static_cast<int>(lane_id) < first_lane + offset)
{
output = input;
}
return output;
}
I.e. the scan operator is applied unconditionally which avoids warp divergence but causes invalid elements being passed to it. So changing the code here would most probably cause significant performance regressions. Another question is if just using other block/warp scan implementation isn't preferrable in terms of performance to "fixing" detail::WarpScanShfl.
Closing the issue since all presumed root causes have been analyzed - and even one more has been identified. Thank you for the investigation and the excellent documentation, @pauleonix.
The following code waits for the tile states of the next window to have a status of anything other than SCAN_TILE_INVALID, so either SCAN_TILE_OOB or SCAN_TILE_PARTIAL or SCAN_TILE_INCLUSIVE and then fill our value correspondingly:
Solution
Luckily there seems to be an easy fix for avoiding these invalid values being passed to the scan operator that might also be relatively harmless in terms of performance:
Just change the calculation of tail flags above to
int tail_flag = (predecessor_status == StatusWord(SCAN_TILE_INCLUSIVE) || predecessor_status == StatusWord(SCAN_TILE_OOB));
This means that every OOB value gets its own segment which, according to my previous comment about the workings of TailSegmentedReduce(), is not combined with the values of other segments (specifically the other OOB values).
This is not quite true. I overlooked that while only the first lane gets a valid result out of the segmented reduction, all lanes apply the scan_op to their respective results containing invalid elements. Therefore another conditional is needed around the call to scan_op in
Activity
cub::DeviceScaninvokes the reduction operator only with valid data #5017pauleonix commentedon Jul 1, 2025
The handling of out-of-bounds items is clearly a root cause because the underlying
BlockScanalgorithm does not have any way of specifying a reduced number of elements. Currently only few block-algorithms (BlockLoadandBlockStore+ some exceptions like single element-per-threadBlockReduceand*PartialTileversions inBlockAdjacentDifference) have overloads taking a runtime value for the number of elements. This is used to fill the padding with the first item of the tile which becomes an invalid value being passed to the scan operator because it is copied and commuted to the end.cccl/cub/cub/agent/agent_scan.cuh
Lines 342 to 351 in f6548bd
Fixing this means adding a lot of overloads but given that only the last tile needs to call this new overload performance should be fine.
pauleonix commentedon Jul 1, 2025
TailSegmentedReduceas used to compute the prefix from the results of previous blacks does not seem to be a root cause as it does not apply its reduction operator when it would combine results from different segments. Combining values among each segment should be fine, even though we are only interested in the first segment (containing the last values due to how we map threads to memory).cccl/cub/cub/warp/specializations/warp_reduce_shfl.cuh
Lines 696 to 722 in f6548bd
cccl/cub/cub/warp/specializations/warp_reduce_shfl.cuh
Lines 454 to 458 in f6548bd
cccl/cub/cub/warp/specializations/warp_reduce_smem.cuh
Lines 215 to 256 in f6548bd
pauleonix commentedon Jul 2, 2025
The OOB tile states look like another root-cause:
The following code waits for the tile states of the next window to have a status of anything other than
SCAN_TILE_INVALID, so eitherSCAN_TILE_OOBorSCAN_TILE_PARTIALorSCAN_TILE_INCLUSIVEand then fill ourvaluecorrespondingly:cccl/cub/cub/agent/single_pass_scan_operators.cuh
Lines 1260 to 1265 in eaba18d
The
valueis then passed to theTailSegmentedReducedescribed in the previous comment.The implementation of the
ScanTileStateand thereforeWaitForValid()depends on the element type being fundamental (detail::is_primitive<T>::value) or not.Fundamental
In this case a kind of AoS implementation is used where the status and the value are packed into one
TxnWordaliased toTileDescriptor:cccl/cub/cub/agent/single_pass_scan_operators.cuh
Lines 616 to 621 in eaba18d
So once the status is not
SCAN_TILE_INVALIDanymore for all lanes of the warp (only first warp of the block executes this), we just take the value from the already loadedTileDescriptor:cccl/cub/cub/agent/single_pass_scan_operators.cuh
Lines 775 to 798 in eaba18d
Therefore we look at how these descriptors were initialized for
SCAN_TILE_OOB:cccl/cub/cub/agent/single_pass_scan_operators.cuh
Lines 678 to 701 in eaba18d
As we can see, the
valuepart is never initialized but should be zeroed from initializingTxnWord. This zeroedvalueis an invalid element.Non-fundamental
Here the
ScanTileStateis implemented in a SoA-fashion so thevalueis only loaded from gmem once the status of all lanes is notSCAN_TILE_INVALIDanymore:cccl/cub/cub/agent/single_pass_scan_operators.cuh
Lines 938 to 959 in eaba18d
In case of
SCAN_TILE_OOB,valueis not modified at all and remains in the default initialized state from the first code snippet in this comment which is an invalid value.Solution
Luckily there seems to be an easy fix for avoiding these invalid values being passed to the scan operator that might also be relatively harmless in terms of performance:
cccl/cub/cub/agent/single_pass_scan_operators.cuh
Lines 1270 to 1273 in eaba18d
Just change the calculation of tail flags above to
This means that every OOB value gets its own segment which, according to my previous comment about the workings of
TailSegmentedReduce(), is not combined with the values of other segments (specifically the other OOB values).EDIT: Not quite enough, see #5019 (comment) below.
pauleonix commentedon Jul 7, 2025
After finding higher than expected error counts in our test, we found another root cause: The
BlockScancan be problematic even when not handling OOB items. In particularBLOCK_SCAN_WARP_SCANSdispatches todetail::WarpScanShflwhich runscccl/cub/cub/warp/specializations/warp_scan_shfl.cuh
Lines 404 to 417 in 5d7fd42
I.e. the scan operator is applied unconditionally which avoids warp divergence but causes invalid elements being passed to it. So changing the code here would most probably cause significant performance regressions. Another question is if just using other block/warp scan implementation isn't preferrable in terms of performance to "fixing"
detail::WarpScanShfl.elstehle commentedon Jul 9, 2025
Closing the issue since all presumed root causes have been analyzed - and even one more has been identified. Thank you for the investigation and the excellent documentation, @pauleonix.
pauleonix commentedon Aug 21, 2025
Another problematic application of the scan op for
BLOCK_SCAN_WARP_SCANS:cccl/cub/cub/block/specializations/block_scan_warp_scans.cuh
Lines 372 to 379 in 5d7fd42
The first warp gets a default-initialized
warp_prefixfromComputeWarpPrefixwhich is in general an invalid input to a custom scan op.Fix: Put the scan op in an else branch of the given conditional.
pauleonix commentedon Aug 22, 2025
This is not quite true. I overlooked that while only the first lane gets a valid result out of the segmented reduction, all lanes apply the
scan_opto their respective results containing invalid elements. Therefore another conditional is needed around the call toscan_opincccl/cub/cub/agent/single_pass_scan_operators.cuh
Lines 1298 to 1305 in eaba18d
This change could be another heavy performance hit.