📘

A64FX Integer exp2() Performance Analysis Report

に公開

a64fx で softmax 向け exp2() を整数 pipe のみで処理を検討しました.

A64FX Integer exp2() Performance Analysis Report

Executive Summary

This report presents performance analysis of integer-only exp2() approximations on the Fujitsu A64FX processor (2.0 GHz). The goal is to find optimal implementations for softmax computation in LLM inference without using floating-point or SVE instructions.

Key Findings:

  • Fastest: diexp_s3 x8 achieves 914 Mops (1.09 ns/elem) with 2-bit accuracy
  • Best balance (no MUL): lut128_shift x8 achieves 322 Mops (3.11 ns/elem) with 4-bit accuracy
  • Highest accuracy: lut17 x4 achieves 257 Mops (3.90 ns/elem) with 12-bit accuracy
  • Fixed-shift optimization: 37% faster than variable-shift LUT interpolation

1. Test Environment

Parameter Value
Processor Fujitsu A64FX
Frequency 2.0 GHz
Compiler fccpx (Fujitsu Clang 7.1.0)
Flags -Kfast,nosimd -Nclang -march=armv8.2-a -O3
Profiler Fujitsu fapp (PA events)
Test size 65,536 elements × 100 iterations

A64FX Integer Pipeline Characteristics

Pipe Operations Throughput
EXA MUL, ADD, shift, logic 1 op/cycle
EXB ADD, shift, logic (no MUL) 1 op/cycle
LSU Load/Store 2 ops/cycle

Key insight: Multiply-free algorithms can use both EXA and EXB pipes for 2× integer throughput.


2. Algorithms Tested

2.1 Shift-Only Variants (DI-EXP)

Based on the identity: ln(2) ≈ 2^(-1) + 2^(-4) + 2^(-5) + 2^(-6) + ...

Variant Shifts Formula Accuracy
diexp_s3 3 1 + f/2 + f/16 + f/32 2 bits
diexp_s4 4 1 + f/2 + f/16 + f/32 + f/64 2 bits
diexp_s5 5 Above + f/256 2 bits
minimax 2 Optimized linear: 1 + 0.695·f 4 bits
piecewise2 - 2-segment linear 4 bits
piecewise4 - 4-segment linear 7 bits

2.2 Polynomial Variants (MUL required)

Variant Order Operations Accuracy
poly_p2 2 2 MUL + 2 ADD 4 bits
poly_p3 3 3 MUL + 3 ADD 7 bits
poly_p4 4 4 MUL + 4 ADD 10 bits

2.3 LUT Variants

Variant LUT Size Interpolation MUL Accuracy
lut256 256×4B None (direct) No 1 bit
lut17 17×4B Linear (MUL) Yes 12 bits
lut64_shift 64×4B + 64×1B Variable shift No 4 bits
lut128_shift 128×4B Fixed shift No 4 bits
lut256_shift 256×4B Fixed shift No 5 bits

3. Benchmark Results

3.1 Performance Summary (x8 unroll)

Variant ns/elem Mops Accuracy MUL-free
diexp_s3 1.09 914 2 bits Yes
diexp_s4 1.38 726 2 bits Yes
diexp_s5 1.25 800 2 bits Yes
minimax 2.02 494 4 bits Yes
lut256 2.27 440 1 bit Yes
lut128_shift 3.11 322 4 bits Yes
lut256_shift 3.38 296 5 bits Yes
lut64_shift 4.93 203 4 bits Yes
poly_p3 5.52 181 7 bits No
lut17 3.96 253 12 bits No

3.2 Unroll Factor Analysis

Variant x1 x4 x8 Best
diexp_s3 4.83 ns 1.50 ns 1.09 ns x8
lut128_shift 3.53 ns 4.45 ns 3.11 ns x8
poly_p3 8.68 ns 6.78 ns 5.52 ns x8

Note: x4 is often slower than x1 due to vector↔scalar transfer penalties (see Section 5).


4. Assembly Analysis

4.1 lut128_shift Scalar (8 instructions)

exp2_lut128_shift:
    adrp    x10, exp2_lut128            ; 1. LUT page address
    ubfx    w8, w0, #17, #7             ; 2. idx = (x >> 17) & 0x7F
    ubfx    w9, w0, #7, #10             ; 3. t >> 6 folded into extract
    add     x10, x10, :lo12:exp2_lut128 ; 4. LUT base address
    ldr     w8, [x10, w8, uxtw #2]      ; 5. y = LUT[idx]
    add     w8, w8, w9                  ; 6. y += t (interpolation)
    neg     w9, w0, lsr #24             ; 7. -k = -(x >> 24)
    asr     w0, w8, w9                  ; 8. result = y >> (-k)
    ret

Key optimization: The fixed shift (>>6) is folded into the ubfx instruction by adjusting bit positions.

4.2 lut64_shift Scalar (12 instructions)

exp2_softmax_lut64_shift:
    adrp    x10, exp2_lut65             ; 1. LUT page
    ubfx    x8, x0, #18, #6             ; 2. idx
    adrp    x11, exp2_shift65           ; 3. Shift table page  ← EXTRA
    add     x11, x11, :lo12:exp2_shift65; 4. Shift table base  ← EXTRA
    add     x10, x10, :lo12:exp2_lut65  ; 5. LUT base
    ldr     w10, [x10, x8, lsl #2]      ; 6. y = LUT[idx]
    ubfx    w9, w0, #2, #16             ; 7. t
    ldrsb   w8, [x11, x8]               ; 8. s = shift[idx]    ← EXTRA
    lsr     w8, w9, w8                  ; 9. delta = t >> s    ← EXTRA
    neg     w9, w0, lsr #24             ; 10. -k
    add     w8, w8, w10                 ; 11. y += delta
    asr     w0, w8, w9                  ; 12. result
    ret

4.3 Comparison

Metric lut64_shift lut128_shift Savings
Instructions 12 8 -4 (33%)
Memory loads 2 1 -1
Tables 325 B 512 B +187 B
Throughput 203 Mops 322 Mops +59%

5. Profiler Analysis (fapp)

5.1 Overall Metrics

Metric Value
IPC (application) 1.32
GIPS 2.83
SVE rate 0% (disabled)
SIMD rate 11.6%

5.2 Per-Variant Analysis

Variant Time (s) Instructions IPC SIMD %
lut128s_x1 0.0232 91.8M 1.98 0%
lut128s_x4 0.0292 62.3M 1.07 42%
lut128s_x8 0.0204 50.0M 1.22 49%
diexp_s3_x8 0.0073 27.9M 1.93 68%

5.3 Why x4 is Slower than x1 and x8

The x4 variant suffers from vector↔scalar transfer penalties:

Vector load (q0) → Scalar extract (mov w8, v0.s[1]) → Scalar LUT lookup
                                    ↓
                              ~6 cycle latency
                                    ↓
Scalar result → Vector insert (ld1 {v1.s}[1], [x8]) → Vector store
                                    ↓
                              ~4 cycle latency
Unroll Cross-domain transfers Work to amortize
x1 0 (pure scalar) N/A
x4 8 transfers (~40 cycles) Only 4 elements
x8 16 transfers (~80 cycles) 8 elements (2× work)

x4 is in the "bad middle ground" - same overhead as x8 but half the work.


6. Accuracy Analysis

6.1 Error Metrics

Variant Max Rel Error Bits Accuracy
diexp_s3 15.6% 2
diexp_s4 15.2% 2
minimax 5.0% 4
lut64_shift 3.8% 4
lut128_shift 3.3% 4
lut256_shift 2.9% 5
poly_p3 0.53% 7
lut17 0.024% 12

6.2 Accuracy vs Target Format

Target Format Mantissa Bits Recommended Variant
BF16 7 lut128_shift (4 bits) or poly_p3 (7 bits)
TF32 10 lut17 (12 bits)
FP32 23 lut17 + refinement

7. Recommendations

7.1 For Maximum Throughput (2-bit accuracy OK)

Use diexp_s3 x8:

  • 914 Mops, 1.09 ns/elem
  • Pure shift/add, uses both EXA+EXB pipes
  • Suitable for rough softmax approximation

7.2 For Balanced Performance/Accuracy (no MUL)

Use lut128_shift x8:

  • 322 Mops, 3.11 ns/elem, 4-bit accuracy
  • 37% faster than lut64_shift
  • 512 B LUT fits in L1 cache
  • Best choice for BF16 softmax

7.3 For High Accuracy

Use lut17 x4:

  • 257 Mops, 3.90 ns/elem, 12-bit accuracy
  • Requires MUL instruction (EXA pipe only)
  • Suitable for TF32/FP32 softmax

7.4 Unroll Factor Guidelines

Scenario Recommended
Small batches (<32) x1 (pure scalar, IPC=1.98)
Medium batches (32-256) x8
Large batches (>256) x8 with loop unrolling
Avoid x4 (vector↔scalar penalty)

8. Future Work

  1. SVE vectorization: Enable SVE for gather loads to parallelize LUT lookups
  2. Cache optimization: Prefetch LUT for streaming access patterns
  3. Fused softmax: Combine exp2 + accumulate + normalize in single pass
  4. Mixed precision: Use lower accuracy for large |x|, higher for small |x|

Appendix A: Build Instructions

# Build with Fujitsu compiler
make CC=fccpx

# Run benchmark
pjsub -g <yourgroup> -L "freq=2000,eco_state=0,rscgrp=small,node=1,elapse=00:10:00" \
      --no-check-directory ./benchmark

# Run with fapp profiling
fapp -C -d ./stats -Hevent=statistics -Icpupa ./benchmark
fapppx -A -d ./stats -Icpupa

Appendix B: Q8.24 Fixed-Point Format

Q8.24: 8 integer bits, 24 fractional bits
Range: [-128.0, +128.0)
Resolution: 2^(-24) ≈ 5.96e-8
Conversion: q8_24 = (int32_t)(float_val * 16777216.0f)

Report generated: January 2026
Platform: Fugaku A64FX @ 2.0 GHz

Discussion