@@ -2208,3 +2208,85 @@ def quantized_softmax(
22082208 out_scale ,
22092209 out_zero_point ,
22102210 )
2211+
2212+
2213+ @impl_tracked (m , "sdpa_bitwise_mask_gen" )
2214+ def sdpa_bitwise_mask_gen (mask : torch .Tensor , threshold : float ) -> torch .Tensor :
2215+ """
2216+ Generate a bit-packed mask tensor for SDPA.
2217+
2218+ Notes:
2219+ - The semantic of "mask" here is inverted relative to PyTorch's typical boolean masks.
2220+ In PyTorch, a boolean mask generally uses True to indicate positions to keep/attend,
2221+ and False to indicate positions to mask out. In this function, the convention is
2222+ reversed: a value of 1 in the packed byte indicates a masked (disallowed) position,
2223+ while 0 indicates an unmasked (allowed) position.
2224+ Concretely:
2225+ * For a bool mask input:
2226+ True -> unmasked/allowed -> stored as bit 0 (we invert with ~)
2227+ False -> masked/disallowed -> stored as bit 1
2228+ * For a float mask input with threshold:
2229+ value < threshold -> masked/disallowed (bit 1)
2230+ value >= threshold -> unmasked/allowed (bit 0)
2231+
2232+ Behavior:
2233+ - Input mask can be torch.bool or torch.float.
2234+ - The last dimension must be a multiple of 8 so that each group of 8 elements
2235+ packs into one byte (little-endian within the byte: the first element maps
2236+ to the least significant bit).
2237+ - For bool masks, bits are computed as the inverse of the boolean value to align
2238+ with the "1 means masked" convention.
2239+ - For float masks, a comparison against `threshold` is used to determine masked
2240+ positions (value < threshold -> masked -> bit = 1).
2241+ """
2242+
2243+ assert len (mask .shape ) >= 1 , "Mask must be at least 1D"
2244+ assert mask .dtype in [torch .bool , torch .float ], "Mask must be bool or float"
2245+ assert mask .shape [- 1 ] % 8 == 0 , "Mask last dim must be a multiple of 8"
2246+ if mask .dtype == torch .bool :
2247+ # Pack every 8 boolean elements into a single byte in the output tensor.
2248+ # Flatten to 1D for straightforward packing
2249+ original_shape = mask .shape
2250+ flat = mask .contiguous ().view (- 1 )
2251+ # Convert boolean to uint8 and group into chunks of 8
2252+ bits = flat .to (torch .uint8 ).view (- 1 , 8 )
2253+
2254+ # Pack 8 bits into one byte (little-endian within a byte: first element -> LSB)
2255+ packed = (
2256+ (~ bits [:, 0 ] & 1 ) << 0
2257+ | (~ bits [:, 1 ] & 1 ) << 1
2258+ | (~ bits [:, 2 ] & 1 ) << 2
2259+ | (~ bits [:, 3 ] & 1 ) << 3
2260+ | (~ bits [:, 4 ] & 1 ) << 4
2261+ | (~ bits [:, 5 ] & 1 ) << 5
2262+ | (~ bits [:, 6 ] & 1 ) << 6
2263+ | (~ bits [:, 7 ] & 1 ) << 7
2264+ ).to (torch .uint8 )
2265+
2266+ # Compute packed last dim size
2267+ last_dim = original_shape [- 1 ]
2268+ packed_last = last_dim // 8
2269+ # Reshape packed to match mask shape, with last dim packed to bytes
2270+ return packed .view (* original_shape [:- 1 ], packed_last )
2271+ else :
2272+ assert mask .dtype == torch .float , "Mask must be bool or float"
2273+ # Pack every 8 boolean elements into a single byte in the output tensor.
2274+ # Flatten to 1D for straightforward packing
2275+ original_shape = mask .shape
2276+ flat = mask .contiguous ().view (- 1 , 8 )
2277+ packed = (
2278+ (flat [:, 0 ] < threshold ) << 0
2279+ | (flat [:, 1 ] < threshold ) << 1
2280+ | (flat [:, 2 ] < threshold ) << 2
2281+ | (flat [:, 3 ] < threshold ) << 3
2282+ | (flat [:, 4 ] < threshold ) << 4
2283+ | (flat [:, 5 ] < threshold ) << 5
2284+ | (flat [:, 6 ] < threshold ) << 6
2285+ | (flat [:, 7 ] < threshold ) << 7
2286+ ).to (torch .uint8 )
2287+
2288+ # Compute packed last dim size
2289+ last_dim = original_shape [- 1 ]
2290+ packed_last = last_dim // 8
2291+ # Reshape packed to match mask shape, with last dim packed to bytes
2292+ return packed .view (* original_shape [:- 1 ], packed_last )
0 commit comments