Skip to content

Commit 7eed6c7

Browse files
Merge pull request #2757 from madeline-underwood/fexpa
Fexpa_reviewed
2 parents 865e5aa + e1d2910 commit 7eed6c7

5 files changed

Lines changed: 56 additions & 34 deletions

File tree

content/learning-paths/servers-and-cloud-computing/fexpa/_index.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
---
2-
title: Accelerate the exponential function
2+
title: Optimize exponential functions with FEXPA
33

4-
draft: true
5-
cascade:
6-
draft: true
74

85
minutes_to_complete: 15
96

10-
who_is_this_for: This is an introductory topic for developers interested in implementing the exponential function and optimizing it. The Scalable Vector Extension (SVE), introduced with the Armv8-A architecture, includes a dedicated instruction, FEXPA. Although initially not supported in SME, the FEXPA instruction has been made available in Scalable Matrix Extension (SME) version 2.2.
7+
who_is_this_for: This is an introductory topic for developers interested in accelerating exponential function computations using Arm's Scalable Vector Extension (SVE). The FEXPA instruction provides hardware acceleration for exponential calculations on Arm Neoverse processors.
118

129
learning_objectives:
1310
- Implement the exponential function using SVE intrinsics
1411
- Optimize the function with FEXPA
1512

1613
prerequisites:
17-
- Access to an [AWS Graviton4, Google Axion, or Azure Cobalt 100 virtual machine from a cloud service provider](/learning-paths/servers-and-cloud-computing/csp/).
18-
- Some familiarity with SIMD programming and SVE intrinsics.
14+
- Access to an [AWS Graviton4, Google Axion, or Azure Cobalt 100 virtual machine from a cloud service provider](/learning-paths/servers-and-cloud-computing/csp/)
15+
- Some familiarity with SIMD programming and SVE intrinsics
1916

2017
author:
2118
- Arnaud Grasset
@@ -54,3 +51,4 @@ weight: 1 # _index.md always has weight of 1 to order corr
5451
layout: "learningpathall" # All files under learning paths have this same wrapper
5552
learning_path_main_page: "yes" # This should be surfaced when looking for related content. Only set for _index.md of learning path content.
5653
---
54+
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
---
2-
title: Conclusion
2+
title: Review benefits and next steps
33
weight: 5
44

55
### FIXED, DO NOT MODIFY
66
layout: learningpathall
77
---
88

9-
## Conclusion
10-
The SVE FEXPA instruction can speed-up the computation of the exponential functions by implementing table lookup and bit manipulation. The exponential function is the core of the Softmax function that, with the shift toward Generative AI, has become a critical component of modern neural network architectures.
9+
## Summary
1110

12-
An implementation of the exponential function based on FEXPA can achieve a specified target precision using a polynomial of lower degree than that required by alternative implementations. Moreover, SME support for FEXPA lets you embed the exponential approximation directly into the matrix computation path and that translates into:
11+
The SVE FEXPA instruction speeds up the computation of exponential functions by implementing table lookup and bit manipulation. The exponential function is the core of the Softmax function that, with the shift toward Generative AI, has become a critical component of modern neural network architectures.
12+
13+
An implementation of the exponential function based on FEXPA can achieve a specified target precision using a polynomial of lower degree than alternative implementations. SME support for FEXPA lets you embed the exponential approximation directly into the matrix computation path, which translates into:
1314
- Fewer instructions (no back-and-forth to scalar/SVE code)
1415
- Potentially higher aggregate throughput (more exponentials per cycle)
1516
- Lower power & bandwidth (data being kept in the SME engine)
1617
- Cleaner fusion with GEMM/GEMV workloads
1718

18-
All of which makes all exponential heavy workloads significantly faster on ARM CPUs.
19+
These improvements make exponential-heavy workloads significantly faster on Arm CPUs.

content/learning-paths/servers-and-cloud-computing/fexpa/fexpa.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: FEXPA
2+
title: Optimize with FEXPA instruction
33
weight: 4
44

55
### FIXED, DO NOT MODIFY
@@ -8,9 +8,9 @@ layout: learningpathall
88

99
## The FEXPA instruction
1010

11-
Arm introduced in SVE an instruction called FEXPA: the Floating Point Exponential Accelerator.
11+
Arm introduced an instruction in SVE called FEXPA: the Floating Point Exponential Accelerator.
1212

13-
Let’s segment the IEEE 754 floating-point representation fraction part into several sub-fields (Index, Exp and Remaining bits) with respective length of _Idxb_, _Expb_ and _Remb_ bits.
13+
The IEEE 754 floating-point representation fraction part can be segmented into several sub-fields (Index, Exp and Remaining bits) with respective length of _Idxb_, _Expb_ and _Remb_ bits.
1414

1515
| IEEE 754 precision | Idxb | Expb | Remb |
1616
|-------------------------|------|------|------|
@@ -46,7 +46,7 @@ With a table of size 2^L, the evaluation interval for the approximation polynomi
4646

4747
## Exponential implementation with FEXPA
4848

49-
FEXPA can be used to rapidly perform the table lookup. With this instruction a degree-2 polynomial is sufficient to obtain the same accuracy as the degree-4 polynomial implementation from the previous section.
49+
Use FEXPA to rapidly perform the table lookup. With this instruction, a degree-2 polynomial is sufficient to obtain the same accuracy as the degree-4 polynomial implementation from the previous section.
5050

5151
### Add the FEXPA implementation
5252

@@ -93,7 +93,7 @@ void exp_sve_fexpa(float *x, float *y, size_t n) {
9393
```
9494
9595
{{% notice Arm Optimized Routines %}}
96-
This implementation can be found in [ARM Optimized Routines](https://github.com/ARM-software/optimized-routines/blob/ba35b32/math/aarch64/sve/sv_expf_inline.h).
96+
This implementation can be found in [Arm Optimized Routines](https://github.com/ARM-software/optimized-routines/blob/ba35b32/math/aarch64/sve/sv_expf_inline.h).
9797
{{% /notice %}}
9898
9999
@@ -146,11 +146,20 @@ SVE+FEXPA (degree-2) 0.000414 5.95x
146146

147147
The benchmark shows the performance progression:
148148

149-
1. **SVE with degree-4 polynomial**: Provides up to 4x speedup through vectorization
150-
2. **SVE with FEXPA and degree-2 polynomial**: Achieves an additional 1-2x improvement
149+
- SVE with degree-4 polynomial provides up to 4x speedup through vectorization
150+
- SVE with FEXPA and degree-2 polynomial achieves an additional 1-2x improvement
151151

152152
The FEXPA instruction delivers this improvement by:
153153
- Replacing manual bit manipulation with a single hardware instruction (`svexpa()`)
154154
- Enabling a simpler polynomial (degree-2 instead of degree-4) while maintaining accuracy
155155

156156
Both SVE implementations maintain comparable accuracy (errors in the 10^-9 to 10^-10 range), demonstrating that specialized hardware instructions can significantly improve performance without sacrificing precision.
157+
158+
## What you've accomplished and what's next
159+
160+
In this section, you:
161+
- Implemented exponential function optimization using the FEXPA instruction
162+
- Reduced polynomial degree from four to two while maintaining accuracy
163+
- Achieved up to 6x speedup over the baseline implementation
164+
165+
Next, you'll review the key benefits and applications of FEXPA optimization.

content/learning-paths/servers-and-cloud-computing/fexpa/implementation.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: First implementation
2+
title: Implement exponential with SVE intrinsics
33
weight: 3
44

55
### FIXED, DO NOT MODIFY
@@ -8,11 +8,11 @@ layout: learningpathall
88

99
## Implement the exponential function
1010

11-
Based on the theory covered in the previous section, you can implement the exponential function using SVE intrinsics with polynomial approximation. This Learning Path was tested using a AWS Graviton4 instance type `r8g.medium`.
11+
Based on the theory covered in the previous section, implement the exponential function using SVE intrinsics with polynomial approximation. This Learning Path was tested using an AWS Graviton4 instance type `r8g.medium`.
1212

1313
## Set up your environment
1414

15-
To run the example, you will need `gcc`.
15+
To run the example, you need `gcc`.
1616

1717
```bash
1818
sudo apt update
@@ -230,4 +230,11 @@ The benchmark demonstrates the performance benefit of using SVE intrinsics for v
230230

231231
The accuracy check confirms that the polynomial approximation maintains high precision, with errors typically in the range of 10^-9 to 10^-10 for single-precision floating-point values.
232232

233-
Continue to the next section to dive into the FEXPA intrinsic implementation, providing further performance uplifts.
233+
## What you've accomplished and what's next
234+
235+
In this section, you:
236+
- Implemented a vectorized exponential function using SVE intrinsics
237+
- Applied range reduction and polynomial approximation techniques
238+
- Achieved up to 4x speedup over the scalar baseline
239+
240+
Next, you'll optimize further using the FEXPA instruction for additional performance gains.
Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
---
2-
title: Theory
2+
title: Learn exponential function optimization techniques
33
weight: 2
44

55
### FIXED, DO NOT MODIFY
66
layout: learningpathall
77
---
88

99
## The exponential function
10-
The exponential function is a fundamental mathematical function used across a wide range of algorithms for signal processing, High-Performance Computing and Machine Learning. Optimizing its computation has been the subject of extensive research for decades. The precision of the computation depends both on the selected approximation method and on the inherent rounding errors associated with finite-precision arithmetic, and it is directly traded off against performance when implementing the exponential function.
10+
The exponential function is a fundamental mathematical function used across a wide range of algorithms for signal processing, High-Performance Computing and Machine Learning. Researchers have extensively studied optimizing its computation for decades. The precision of the computation depends both on the selected approximation method and on the inherent rounding errors associated with finite-precision arithmetic, and it is directly traded off against performance when implementing the exponential function.
1111

1212
## Range reduction
13-
Polynomial approximations are among the most widely used methods for software implementations of the exponential function. The accuracy of a Taylor series approximation for exponential function can be improved with the polynomials degree but will always deteriorate as the evaluation point moves further from the expansion point. By applying range reduction techniques, the approximation of the exponential function can however be restricted to a very narrow interval where the function is well-conditioned. This approach consists in reformulating the exponential function in the following way:
13+
Polynomial approximations are among the most widely used methods for software implementations of the exponential function. The accuracy of a Taylor series approximation for exponential function can be improved with the polynomial's degree but deteriorates as the evaluation point moves further from the expansion point. By applying range reduction techniques, you can restrict the approximation of the exponential function to a very narrow interval where the function is well-conditioned. This approach reformulates the exponential function in the following way:
1414

1515
$$e^x=e^{k×ln2+r}=2^k \times e^r$$
1616

@@ -22,16 +22,16 @@ Since k is an integer, the evaluation of 2^k can be efficiently performed using
2222

2323
$$e^x \approx 2^k \times p(r)$$
2424

25-
It is important to note that the polynomial p(r) is evaluated exclusively over the interval [-ln2/2, +ln2/2]. So, the computational complexity can be optimized by selecting the polynomial degree based on the required precision of p(r) within this narrow range. Rather than relying on a Taylor polynomial, a minimax polynomial approximation can be used to minimize the maximum approximation error over the considered interval.
25+
The polynomial p(r) is evaluated exclusively over the interval [-ln2/2, +ln2/2]. So, the computational complexity can be optimized by selecting the polynomial degree based on the required precision of p(r) within this narrow range. Rather than relying on a Taylor polynomial, a minimax polynomial approximation can be used to minimize the maximum approximation error over the considered interval.
2626

2727
## Decomposition of the input
28-
The decomposition of an input value as x = k × ln2 + r can be done in 2 steps:
28+
Decompose an input value as x = k × ln2 + r in two steps:
2929
- Compute k as: k = round(x⁄ln2), where round(.) is the round-to-nearest function
3030
- Compute r as: r = x - k × ln2
3131

32-
Rounding of k is performed by adding an adequately chosen large number to a floating-point value and subtracting it just afterward (the original value is rounded due to the finite precision of floating-point representation). Although explicit rounding instructions are available in both SVE and SME, this method remains advantageous as the addition of the constant can be fused with the multiplication by the reciprocal of ln2. This approach assumes however that the floating-point rounding mode is set to round-to-nearest, which is the default mode in Armv9-A. By integrating the bias into the constant, 2^k can also be directly computed by shifting the intermediate value.
32+
Rounding of k is performed by adding an adequately chosen large number to a floating-point value and subtracting it just afterward (the original value is rounded because of the finite precision of floating-point representation). Although explicit rounding instructions are available in both SVE and SME, this method remains advantageous because the addition of the constant can be fused with the multiplication by the reciprocal of ln2. This approach assumes that the floating-point rounding mode is set to round-to-nearest, which is the default mode in Armv9-A. By integrating the bias into the constant, 2^k can also be directly computed by shifting the intermediate value.
3333

34-
Rounding error during the second step will introduce a global error as we will have:
34+
A rounding error during the second step introduces a global error as you have:
3535

3636
$$ x \approx k \times ln2 + r $$
3737

@@ -44,20 +44,27 @@ $$ (-1)^s \times 2^{(exponent - bias)} \times (1.fraction)_2 $$
4444

4545
where s is the sign bit and 1.fraction represents the significand.
4646

47-
The value 2^k can be encoded by setting both the sign and fraction bits to zero and assigning the exponent field the value k + bias. If k is an 8-bits integer, 2^k can be efficiently computed by adding the bias value and positioning the result into the exponent bits of a 32-bit floating-point number using a logical shift.
47+
The value 2^k can be encoded by setting both the sign and fraction bits to zero and assigning the exponent field the value k + bias. If k is an 8-bit integer, 2^k can be efficiently computed by adding the bias value and positioning the result into the exponent bits of a 32-bit floating-point number using a logical shift.
4848

49-
Taking this approach a step further, a fast approximation of exponential function can be achieved using bits manipulation techniques alone. Specifically, adding a bias to an integer k and shifting the result into the exponent field can be accomplished by computing an integer i as follows:
49+
Taking this approach a step further, you can achieve a fast approximation of exponential function using bit manipulation techniques alone. Specifically, adding a bias to an integer k and shifting the result into the exponent field can be accomplished by computing an integer i as follows:
5050

5151
$$i=2^{23} \times (k+bias) = 2^{23} \times k+2^{23} \times bias$$
5252

5353
This formulation assumes a 23-bit significand, but the method can be generalized to other floating-point precisions.
5454

55-
Now, consider the case where k is a real number. The fractional part of k will propagate into the significand bits of the resulting 2^k approximation. However, this side effect is not detrimental, it effectively acts as a form of linear interpolation, thereby improving the overall accuracy of the approximation. To approximate the exponential function, the following identity can be used:
55+
Now, consider the case where k is a real number. The fractional part of k propagates into the significand bits of the resulting 2^k approximation. However, this side effect isn't detrimental; it effectively acts as a form of linear interpolation, thereby improving the overall accuracy of the approximation. To approximate the exponential function, use the following identity:
5656

5757
$$e^x = 2^{x⁄ln2}$$
5858

5959
As previously discussed, this value can be approximated by computing a 32-bit integer:
6060

6161
$$i = 2^{23} \times x⁄ln2 + 2^{23} \times bias = a \times x + b $$
6262

63-
Continue to the next section to make a C-based implementation of the exponential function.
63+
## What you've accomplished and what's next
64+
65+
In this section, you learned the mathematical foundations for optimizing exponential functions:
66+
- Range reduction techniques that narrow the evaluation interval
67+
- How to decompose inputs using k × ln2 + r reformulation
68+
- Bit manipulation techniques for computing scaling factors
69+
70+
Next, you'll implement these concepts in C using SVE intrinsics.

0 commit comments

Comments
 (0)