Skip to content

Use NPY_METH_get_loop for within-dtype and SIMD cross-dtype casts. - #462

Merged
copybara-service[bot] merged 1 commit into
mainfrom
test_1034591161
Dec 2, 2026
Merged

copybara-service[bot] merged 1 commit into
mainfrom
test_1034591161

Conversation

@copybara-service

Copy link
Copy Markdown

Use NPY_METH_get_loop for within-dtype and SIMD cross-dtype casts.

Move byte-order swap checks, stride dispatch, copyswapn indirection, and CPU SIMD target selection out of the per-chunk inner loop into once-per-operation get_loop callbacks (WithinDTypeGetLoop and BitwiseCastGetLoop). Also add fragmented boolean-mask, np.where, strided-copy, and masked-cast benchmarks to benchmarks/benchmark.py.

Benchmark diff (n = 262144, AMD EPYC 7B13, 6 runs):

name cpu/op cpu/op vs base INSTRUCTIONS/op vs base
fill_nonzero_bf16/n:16384 908.7n ± 3% 901.8n ± 3% ~ (p=0.818 n=6) 6.03k -> 5.96k -1.16%
mask_read_bf16/n:262144 1.731m ± 3% 1.339m ± 1% -22.66% (p=0.002 n=6) 10.51M -> 5.94M -43.44%
mask_fill_zero_bf16/n:262144 1.724m ± 1% 1.299m ± 1% -24.64% (p=0.002 n=6) 11.69M -> 7.21M -38.34%
mask_fill_nonzero_bf16/n:262144 1.701m ± 1% 1.302m ± 14% -23.44% (p=0.002 n=6) 12.40M -> 7.21M -41.88%
mask_read_e4m3fn/n:262144 1.690m ± 2% 1.352m ± 3% -19.99% (p=0.002 n=6) 9.65M -> 5.91M -38.79%
mask_fill_zero_e4m3fn/n:262144 1.698m ± 0% 1.313m ± 0% -22.71% (p=0.002 n=6) 10.66M -> 6.52M -38.84%
mask_fill_nonzero_e4m3fn/n:262144 1.696m ± 1% 1.319m ± 1% -22.24% (p=0.002 n=6) 10.66M -> 6.52M -38.84%
mask_read_complex32/n:262144 1.713m ± 1% 1.343m ± 2% -21.58% (p=0.002 n=6) 10.58M -> 6.01M -43.16%
mask_fill_zero_complex32/n:262144 1.753m ± 1% 1.315m ± 1% -24.95% (p=0.002 n=6) 11.67M -> 7.21M -38.24%
mask_fill_nonzero_complex32/n:262144 1.709m ± 1% 1.314m ± 1% -23.10% (p=0.002 n=6) 12.40M -> 7.21M -41.88%
cast_mask_bf16_to_f32/n:262144 1.403m ± 1% 1.337m ± 1% -4.67% (p=0.002 n=6) 8.03M -> 7.18M -10.64%
cast_mask_f32_to_bf16/n:262144 1.470m ± 0% 1.437m ± 0% -2.22% (p=0.002 n=6) 8.96M -> 8.10M -9.53%
cast_mask_f32_to_e4m3fn/n:262144 1.834m ± 0% 1.792m ± 1% -2.28% (p=0.002 n=6) 14.18M -> 13.33M -6.02%
cast_mask_int32_to_int4/n:262144 1.451m ± 1% 1.345m ± 0% -7.29% (p=0.002 n=6) 8.03M -> 7.17M -10.64%

@copybara-service
copybara-service Bot force-pushed the test_1034591161 branch 4 times, most recently from 5312eee to 6b715c8 Compare December 2, 2026 20:33
Move byte-order swap checks, stride dispatch, `copyswapn` indirection, and CPU SIMD target selection out of the per-chunk inner loop into once-per-operation `get_loop` callbacks (`WithinDTypeGetLoop` and `BitwiseCastGetLoop`). Also add fragmented boolean-mask, `np.where`, strided-copy, and masked-cast benchmarks to `benchmarks/benchmark.py`.

Benchmark diff (`n = 262144`, AMD EPYC 7B13, 6 runs):

name                                   cpu/op        cpu/op        vs base                INSTRUCTIONS/op   vs base
fill_nonzero_bf16/n:16384               908.7n ± 3%   901.8n ±  3%        ~ (p=0.818 n=6)   6.03k -> 5.96k   -1.16%
mask_read_bf16/n:262144                 1.731m ± 3%   1.339m ±  1%  -22.66% (p=0.002 n=6)  10.51M -> 5.94M  -43.44%
mask_fill_zero_bf16/n:262144            1.724m ± 1%   1.299m ±  1%  -24.64% (p=0.002 n=6)  11.69M -> 7.21M  -38.34%
mask_fill_nonzero_bf16/n:262144         1.701m ± 1%   1.302m ± 14%  -23.44% (p=0.002 n=6)  12.40M -> 7.21M  -41.88%
mask_read_e4m3fn/n:262144               1.690m ± 2%   1.352m ±  3%  -19.99% (p=0.002 n=6)   9.65M -> 5.91M  -38.79%
mask_fill_zero_e4m3fn/n:262144          1.698m ± 0%   1.313m ±  0%  -22.71% (p=0.002 n=6)  10.66M -> 6.52M  -38.84%
mask_fill_nonzero_e4m3fn/n:262144       1.696m ± 1%   1.319m ±  1%  -22.24% (p=0.002 n=6)  10.66M -> 6.52M  -38.84%
mask_read_complex32/n:262144            1.713m ± 1%   1.343m ±  2%  -21.58% (p=0.002 n=6)  10.58M -> 6.01M  -43.16%
mask_fill_zero_complex32/n:262144       1.753m ± 1%   1.315m ±  1%  -24.95% (p=0.002 n=6)  11.67M -> 7.21M  -38.24%
mask_fill_nonzero_complex32/n:262144    1.709m ± 1%   1.314m ±  1%  -23.10% (p=0.002 n=6)  12.40M -> 7.21M  -41.88%
cast_mask_bf16_to_f32/n:262144          1.403m ± 1%   1.337m ±  1%   -4.67% (p=0.002 n=6)   8.03M -> 7.18M  -10.64%
cast_mask_f32_to_bf16/n:262144          1.470m ± 0%   1.437m ±  0%   -2.22% (p=0.002 n=6)   8.96M -> 8.10M   -9.53%
cast_mask_f32_to_e4m3fn/n:262144        1.834m ± 0%   1.792m ±  1%   -2.28% (p=0.002 n=6)  14.18M -> 13.33M  -6.02%
cast_mask_int32_to_int4/n:262144        1.451m ± 1%   1.345m ±  0%   -7.29% (p=0.002 n=6)   8.03M -> 7.17M  -10.64%

PiperOrigin-RevId: 1034706204
@copybara-service
copybara-service Bot merged commit 9cad2fd into main Dec 2, 2026
3 checks passed
@copybara-service
copybara-service Bot deleted the test_1034591161 branch December 2, 2026 20:44
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.

1 participant