This application claims the priority benefit under 35 U.S.C. § 119 of United Kingdom Patent Application No. 1821069.0, filed Dec. 21, 2018, the entire contents of which are incorporated herein by reference.
The present disclosure relates to a processing unit for approximating the values of functions calculated using floating point numbers.
In computing, bit sequences of predefined sizes are used to represent numbers. The particular representation of the bit sequence determines how a bit sequence is interpreted.
One form of representation is the floating-point representation, which is often used to approximately represent real numbers. The floating-point representation comprises 3 separate components, i.e. a sign component, a mantissa component, and an exponent component. In the single-precision (i.e. 32-bit) floating point representation according to the IEEE 754 standard, the sign component consists of a single bit, the exponent consists of 8 bits, and the mantissa consists of 23 bits. In the half-precision (i.e. 16-bit) floating-point representation, the sign component consists of a single bit, the mantissa consists of 10 bits, and the exponent consists of 5 bits. In most cases, a number is given from these 3 components by the following formula:
(−1)signbit×I.mantissa×2exponentbits−offset
The displayed “offset” to the exponent is dependent upon the number of bits used to represent the exponent, which is dependent upon the precision level. In the single-precision representation, the offset is equal to 127. In the half-precision format, the offset is equal to 15.
Here “I” is an implicit bit, which is derived from the exponent. In the case that the exponent bit sequence consists of anything other than all zeros or all ones, the implicit bit is equal to one and the number is known as a “Norm”. In this case, the floating point number is given by:
(−1)signbit×1.mantissa×2exponentbits−offset
In the case that the exponent bit sequence consists of all zeros, the implicit bit is equal to zero and the number is known as a “denorm”. In this case, the floating point number is given by:
(−1)signbit×0.mantissa×2exponentbits−offset
The denorms are useful, since they allow smaller numbers to be represented than would otherwise be representable by the limited number of exponent bits.
The other circumstance—in which the exponent bit sequence consists of all ones—may be used to represent special cases, e.g. ±infinity or NaN (not a number). NaN is a numeric data type value representing an undefined or unrepresentable value. The presence of a NaN in the results of a calculation is often taken to signal an exception.
The floating point representation may be used to represent numbers in implementations of neural network processing. An implementation of neural networks involves the storage and manipulation of such floating point numbers. Neural networks are used in the field of machine learning and artificial intelligence. Neural networks comprise arrangements of sets of nodes which are interconnected by links and which interact with each other. The principles of neural networks in computing are based on information about how electrical stimuli convey information in the human brain. For this reason, the nodes are often referred to as neurons. They may also be referred to as vertices. The links are sometimes referred to as edges. The network can take input data and certain nodes perform operations on the data. The result of these operations is passed to other nodes. The output of each node is referred to as its activation or node value. Each link is associated with a weight. A weight defines the connectivity between nodes of the neural network. Many different techniques are known by which neural networks are capable of learning, which takes place by altering values of the weights.
Certain calculations involving the use of floating point numbers may be carried out by a processing unit executing software instructions. Such calculations include the calculation of known functions, such as hyperbolic functions or logistic functions. These function may be quite complex to calculate.
A problem with which this present application is concerned is how to determine values for such functions, in such a way as to make efficient use of processing resources, such that values can be determined quickly and accurately.
A particular problem is that complex functions behave differently in different ranges of the input variable, which means that different calculation approaches need to be adopted depending on the value of the input variable.
Specifically: when constrained by a “real” physical implementation, with limited internal precision, to implement a complex function in a processor, the different methods of approximating these complex functions have differing accuracy over different parts of the input range. In the worst cases, some approximations can produce no accurate bits in the result for parts of the input range.
According to a first aspect of the invention, there is provided an execution unit configured to determine a value of a function at an input value which is a floating point number, the execution unit comprising: a plurality of look up tables each comprising a plurality of entries, each of the entries comprising a value of the function in a particular range of input values; a correction table; a table selector configured to determine the particular range in which the input value falls and to select the look up table for that particular range; and an input buffer to receive an input floating point number comprising an input exponent and an input mantissa; wherein the execution unit is configured to use the selected look up table to determine an output floating point number for the input floating point number, to search the correction table using at least part of the input floating point number to determine if a correction value is held for that input floating unit number, and to apply the correction value to the output floating point number from the selected look up table to determine the value of the function.
At least part of the input mantissa may be used as an index to search the selected look up table when using the table to determine an output floating point number.
The table selector may use part or all of the input to determine a particular range to select the look up table from. There may be a plurality of look up tables from which the look up table is selected.
The execution unit may comprise interpolation circuitry, which searches the selected look up table using the input floating point number to select as least one entry from the first look up table. It may then perform interpolation in dependence upon the entry to determine the output floating point number of the selected look up table. At least part of the input mantissa may comprise a first set of bits of the input mantissa, and the interpolation may use a second set of bits of the input mantissa, which are different to the first set of bits, where the first set of bits are more significant than the second set of bits.
The selected look up table may store a plurality of differentials, each associated with one or more of the plurality of entries. The interpolation circuitry may use one of the differentials associated with the entry when performing the interpolation. The offset value between the input mantissa and the selected entry may be multiplied by the differential value associated with that entry. The correction value may be applied to the least significant bit(s) of the correction value to the truncated mantissa.
An output value of the function may be generated outside of the particular ranges. The spacing between entries of the plurality of entries in each table may be fixed. This fixed spacing varies in different tables in a manner which is inversely correlated with curvature of the function in the range for that table. The values of the function contained in each of the plurality of entries may be offset from true values for the function by an amount correlated with the curvature of the function.
According to a second aspect of the invention, there is provided a method for determining a value of a function at an input value which is a floating point number, the method comprising: determining the particular range in which the input value falls and selecting a look up table for that particular range; and receiving an input floating point number comprising an input exponent and an input mantissa; wherein the selected look up table is used to determine an output floating point number for the input floating point number, a correction table is searched using at least part of the input floating point number to determine if a correction value is held for that input floating unit number; and applying the correction value to the output floating point number from the selected look up table to determine the value of the function.
According to a third aspect of the invention, there is provided a computer program product comprising a computer program which comprises a computer program instruction recorded on a non-transitory media which when executed causes a hardware module to carry out a method as set out herein.
It is desirable to improve the accuracy of approximation of a complex function, even when constrained by a hardware functional unit. There are schemes (such as CORDIC) in which each iteration increases the accuracy of the result by another bit. The CORDIC algorithm may be implemented in hardware for calculating values of hyperbolic functions. However, algorithms such as CORDIC can be expensive to implement in hardware when fully pipelined (which is required for certain CPU architectures, such as multi-worker thread CPU architectures) or quite slow. Therefore, in many circumstances, implementing algorithms in hardware can be prohibitively slow or require a high degree of internal complexity.
The approach described herein trades algorithmic complexity (by supporting multiple algorithms) to maintain a reasonable internal complexity and minimise execution time.
Embodiments of the application provide a hardware solution for approximating the values of functions using floating point numbers. The technique is implemented in an execution unit of a processor. The execution unit (or functional unit) comprises a set of look up tables and correction CAMs. A problem that has existed previously is that look up tables to approximate functions can be large and consume a significant amount of memory. The inventors have realised that by treating a function differently in different ranges of input values concise look up tables can be constructed which are small and do not consume significant memory. In fact, they can be implemented as synthesised logic. The execution unit is configured to receive an input floating point number as an input value to a function and to determine the range it lies in. In a certain range, a sub-range is used to select a look up table from the set. The execution unit uses the floating point number to denote an index onto the selected look up table to determine a value for the function.
Reference is made to
The execution unit 2 forms part of a pipeline of the processing unit 10. The processing unit 10 also comprises an instruction fetch stage 6 which fetches instructions from an instruction memory 11. The fetched instructions are passed to the decode stage 16 to be decoded, and the decode stage 16 then passes an indication of the decoded instruction to the execution unit 2 along with the decoded addresses of any operand registers specified in the instruction, in order for the instruction to be executed. The execution unit 2 has access to operand registers and control registers, which it may use in executing the instruction based on the decoded register addresses, such as in the case of an arithmetic instruction (e.g. by adding, multiplying, subtracting or dividing the values in two operand registers and outputting the result to another operand register of the respective thread) or a function instruction as described herein. Or if the instruction defines a memory access (load or store), the load/store logic of the execution unit 2 loads a value from the data memory 12 into an operand register of the respective thread, or stores a value from an operand register of the respective thread into the data memory 12, in accordance with the instruction.
The processing unit 10 comprises a memory access stage 8, which is responsible for accessing a data memory 12, for loading data from the memory 12, or storing data into the memory 12. A set of registers 14 is provided for holding source and destination operands for the instructions being executed at any instance by the pipeline of the processing unit 10. It will readily be understood that the pipeline may contain many different types of execution unit 2 for executing a variety of different instructions, for example for performing mathematical operations. One type of processing unit 10 which may be useful with the present invention is a processing unit 10 using barrel-threaded time slots, in which a supervisor thread may allocate different worker threads to different time slots for their execution.
The execution unit 2 may comprise a pipeline structure with a plurality of execution pipeline stages 22 for executing operations specified by the instructions fetched from instruction memory 10. The appropriate pipeline stage(s) for executing the instruction may be selected in dependence upon the decoding of the instruction performed at the decode stage 16. The execution unit described herein is configured to perform one or more operations for approximating the value of a function (e.g. y=tanh (x)). In response to the execution by the execution unit 2 of an instruction for determining the value of a function, the execution unit 2 is configured to take a floating point number (the operand of the instruction) and the pipeline stages 22 perform said one or more operations to determine the value of the function. The execution unit 2 then provides the value as a result to the memory access stage 8 of the pipeline, which is configured to provide the approximated value of the function to the data memory 12 for storage.
The input floating point number comprises an exponent and a mantissa. In principle, techniques detailed herein may be adapted for full precision numbers, but in practice the main benefits are derived when the input floating point number is a half-precision floating point number (f16). The execution unit 2 is configured to receive the floating point number and use it to determine the range of the function that the input variable (x) lies in. A first look up table is selected from a plurality of look up tables based on the range. Each look up table has a range of entries comprising a plurality of mantissa values and, for each mantissa value, a corresponding value of the function. The processing unit 10 is configured to search the range of entries in the selected look up table using at least part (i.e. one or more bits) of the input mantissa (i.e. the mantissa of the input floating point number) as an index into the table to determine a value f(x) for the function. The number of bits depends upon the size of the lookup table. In one implementation, there is always more than 2 entries per table, so more than one bit is used but in the limit, it could be a single bit.
In the case that the input mantissa matches the mantissa of one of the table entries, the execution unit 2 is configured to select that entry in the table to determine the value of the function.
In the case that the input mantissa falls between two of the values of the plurality of mantissa values in the range of entries, the execution unit 2 is configured to apply interpolation to determine an output value. The look up table comprises differential values (Diffs) for the interpolation. Each of the differential values is associated with at least one of the entries in the table. The execution unit 2 is configured to determine, using interpolation, an output value in dependence on: the difference between the mantissa value which is output and the input mantissa; and the differential value stored in the table for at least one of those entries. The interpolation used may be linear interpolation.
The look up tables have different properties. For example the spacing between entries may be different for different look up tables. This reflects the fact that there may be different levels of curvature in different ranges of the function. A greater curvature may render the results from the interpolation at those points less accurate than the results from the interpolation at points of lesser curvature. In order to compensate for this, there may be a greater density of table entries at those parts of the input range for which the function has greater curvature than those parts of the input range for which the function has less curvature. In other words, the spacing (i.e. difference between the mantissa values of adjacent entries) between table entries may inversely correlate with the curvature of the function. This enables the interpolation to produce more accurate results. For this reason, each look up table may be associated with a particular sub-range of input values.
In some embodiments, the spacing between entries is fixed within each lookup table, although it may vary between look up tables. In other embodiments, the spacing may vary within the look up table. An extra field may be provided to denote the spacing between each adjacent pair of entries.
In some embodiments, the look up table and correction table technique described above may only be applied within certain input ranges. In other input ranges, values for the function may be determined by calculating values of other functions which are approximations for those input ranges. The functions may be approximated using other functions, including constant values, over certain input ranges. For example, the function f(x)=tanh(x) (shown in
tanh(x)=x, 0≤|x|<2−5
tanh(x)=±1.0, x>4
Look up tables and interpolation will be used for other values.
Similarly, the function f(x)=logistic(x) can be determined using the following as approximation functions within the indicated following ranges:
logistic(x)=0, x<−16
logistic(x)=ex, −16<x<−4
logistic(x)=0.5, −2−10<x<210
logistic(x)=1, x>6
Other functions (as outlined earlier) may similarly be divided into different ranges.
The execution unit 10 is configured to determine a value for the function using the look up table and correction table in response to determining that the input floating point number is within a first set of one or more input ranges. The execution unit 10 is configured to instead determine a value for the function using an approximation function if the input floating point number is within a second set of one or more input ranges.
Reference will now be made to
If the input floating point number is in a range where look up tables are required, an appropriate look up table is selected based on the subrange within which the input floating point number falls. This can be done by checking its exponent or creating an index. This is shown at step S328. At step S330 that look up table is searched using the mantissa of the input floating point number, or part of the mantissa to select an output value of the function. This value is output at step S332. In parallel, a bit sequence from the input floating point number (which could be all or part of the mantissa) is applied to the CAMs at step S334 to check whether or not a correction value exists for that input bit sequence. This is labelled step S334.
Reverting back to the main flow, it is determined at step S336 whether or not the output value is an exact match for the input mantissa. If it is, the function value is output at step S338. If it is not, an interpolation step is carried at step S340 and the interpolated value is supplied as the output value at step S338.
If a correction value has been looked up from the CAM at step S334 it is added to the output value at step S342. Then, the final output value is provided at step S344.
Reference is made to
In order to produce more accurate results from the interpolated values determined from the look up table, the output values stored for the entries may be offset from the true values for the function. This may compensate for the error introduced by the curvature into the results of the interpolation. For example, for entries at which the curvature is positive, the output values of the entries may be positively offset from the true values of the function. For entries at which the curvature is negative, the output values of the entries may be negatively offset from the true values of the function. This increases the accuracy of the results from the interpolation. This is described later with reference to
Performing the offsets to compensate for the curvature in the function may also lead to inaccuracies at other parts of the input range into the table. For instance, although the accuracy of interpolation between two points may be improved, the offsets may mean that when a matching entry is located in the table for a particular floating point mantissa, the output value that is read out from the table deviates from the true value for the function due to the offset introduced. Therefore, in order to address this, predefined corrections are stored in one or more content addressable memory (CAM). The inventors have noticed that a bit sequence derived from the mantissa can be used to search the correction table to determine a correction value, which is added to the mantissa of the look up table output. For the look up tables, part of the mantissa forms an index. For the CAMs, the entries in the CAM are inspected to see if any of the values contained in the CAM are equal to the entire input bit-field (i.e. the fp16 number is treated as a 16-bit integer). All of the CAM entries are inspected in parallel and if any of them is exactly equal to the input a hit is returned with the corresponding correction value that is stored alongside the matching value. In the case of a miss, we return a default correction (always 0 in the present embodiment is returned, but other default correction values are possible). This allows a very sparsely populated table to be stored as efficiently as possible—instead of 216 entries, where most have the same value, only a few 10s (up to ˜100) entries are needed for the results that aren't the default. The trade-off is number of comparators needed to check for hits—as long as the number of entries is small enough, this is still efficient. The correction value may comprise a single bit that is added to the least significant bit (LSB) of the mantissa of the look up table output.
Reference is made to
An index is formed from a bit selection from the mantissa of the input. The range of the floating point number is determined either using the exponent (e.g. in tanh(x)) or using a range check on the index (e.g. in log). Either an approximation function unit is chosen, or a look up table is selected based on the subrange. At least part of the bit sequence representing the mantissa is also used as in input into the correction table 770 to obtain a correction value. The correction value is added to the end of the mantissa of value obtained as the look up table output so as to produce a result that represents the value of the function. This result is an approximation of the particular function at the input value x represented by the input floating point number. At many input values, it may be the true value of the function. The function could be a hyperbolic function, such as tanh ( ). The function could be a logistic function.
Reference is made to
The entries 410 shown in the look up table 730 are associated with a particular sign bit. The entries shown may be entries corresponding to a sign bit of ‘1’ or a sign bit of ‘0’. The execution unit may select a particular part of the look up table in dependence upon the sign bit, and then select an entry from this selected part.
As explained, the input exponent (or index) may select a particular look up table. In some embodiments, the input exponent of the input floating point number (or index in the case of log functions) is used to select the look up table. This may be done by determining a match between the input exponent and the exponent (i.e. Exponent1) stored in the table. Alternatively, an index can be created from the mantissa and used to select an appropriate look up table.
In one embodiment, the whole input value is used. IEEE FP formats have a property that if the packed number is treated as an integer they are still numerically sorted with respect to each other. So it is possible to use the bit representation of a floating point number directly in a comparison to see if the input falls within a predefined range. The table selector uses a set of comparators to see whether the input (or some sub-field of the input) falls within different ranges and if it does, selects the matching table.
Subsequently, the execution unit is configured to use the input mantissa of the input floating point number to search the entries in the selected table to select at least one entry. For example, in response to selecting the table 730 in
If a match between the input mantissa and one of the mantissas stored in the selected table cannot be found, the execution unit is configured to select at least one of the entries that are adjacent to one another and comprise mantissas between which the input mantissa falls. For example, if the input mantissa has a value falling between the mantissa2 and mantissa3, at least one of the entries comprising these mantissa are selected from the look up table 730. The execution unit may be configured to select the entry having a mantissa that is closest to the input mantissa.
To select an entry from the table, the execution unit may use only some of the bits of the input mantissa. The execution unit may use a first number of the most significant bits (MSBs) of the input mantissa to select an entry and then use the remaining bits of the input mantissa to perform interpolation.
The execution unit uses a selected entry from the look up table and applies interpolation to determine a result.
The Function Values shown in the table represents a value (which may be an approximate value) of the function for the particular sign bit, exponent and mantissa associated with an entry. For example if the function is a tanh function, the Function Value selected from the table represents a value for tanh for the particular sign bit, exponent and mantissa associated with the corresponding entry.
For linear interpolation, the multiplier calculates
where:
o=the offset from the index used to lookup (input mantissa−table-mantissa)
n=number of steps between indexes (entries)
d=differential value
The diff value itself is in the table but the multiplier contains a hidden 1/n constant (usually this is a power-of-2 so is a simple shift rather than constant multiplication). This produces a linear step from the function value at the index used to the next function value at the next index.
Thus, the output value of the look up table may be determined as in equation 1 below:
where the Table Mantissa is the quantised version of the input mantissa used to index the look up table 730, and the Function Value is the function value in the entry that was selected during the search of the look up table 730.
Since the gradient of the function may vary over the function, the differential value that is to perform the interpolation varies for different input values of the function. The different values are stored in the look up table 730. Each Diff value is associated with one or more entries in the table. In the example shown in
Reference is made to
As shown in
Reference is made to
For the first example 900, the table entries comprise the true values of the function f(x). In this case, since the curvature of the function 910 is quite high, if the input floating point number falls in the middle of the two table entries, when the linear interpolation is carried, the error from the true function f(x) is larger than in the case of the second example 910.
In the second example, the table entries comprise values for the function f(x) that are offset from the true values f(x). Although, this introduces some error in the entries themselves, the error in the interpolation may be reduced. Overall, the absolute error may be reduced using this technique.
In order to compensate for the offsets to the function values in the look up table, the correction table is used to adjust one or more bits of the output floating point number from the look up table.
Reference is made again to
Block 720 is a table selector configured to select a first look up table from the plurality of look up tables 730 in dependence upon the input exponent (or an index from the mantissa). In response to the input exponent being determined to lie in a sub-range, the block 720 is configured to select from the plurality of look up tables the look up table with exponents in that subrange. The block 720 also selects the table using the sign bit of the input floating point number.
The execution unit is configured to search the first look up table using the input mantissa to select at least one entry. If the execution unit finds a mantissa matching the input mantissa in the selected look up table, the execution unit selects the entry comprising that matching mantissa and outputs the matching mantissa and corresponding exponent and corresponding sign bit stored for that entry in the look up table.
If there is no matching entry, the entry that is output is the one before the next one that would be required (i.e. it is always lower than the desired match).
As mentioned, the value which is returned from the look up table is always the one exactly at or before the one that would be required. The next value is found as value+Diff—so these two values straddle the result that would be returned if the table contained an entry for every incoming mantissa value. The multiplier/add stages interpolate along a straight line between the value that was looked up and the value which would have been next, according to the magnitude of the offset field.
Note that some look up table indexes start at non-zero values to allow consecutive tables to continuously cover the entire input range.
The execution unit may be configured to select the entry using a first set of bits of the input mantissa (these being the most significant bits of the input mantissa). The execution unit may select an entry from the table for which the first set of bits of the input mantissa match corresponding mantissa bits stored for an entry in the table. The execution unit may be configured to then output the remaining bits of the input mantissa as the diff value.
The Diff value from the look up table and the offset value from the mantissa are used to perform the interpolation. They are multiplied together at the multiplier block 740, and the result of this multiplication is added at the addition block 750 to the mantissa of the selected entry from the look up table.
At the pack block 760, the values of the exponent, mantissa and sign bit derived from the selected look up table (or the approximation function 716) are used to construct an output floating point number in the same format (e.g. half-precision format) as the input floating point number. The pack block 760 is configured to reduce the field bit-widths of the output mantissa and the output exponent to the widths used for the half-precision format. The pack block 760 is configured to truncate the exponent and bias it. The pack block 760 is configured to truncate the mantissa and round the LSB of the truncated mantissa. The pack block 760 is configured to then concatenate the output mantissa and the exponent and sign bit to produce the output floating point number.
The input floating point number is used as an input into the at least one correction table 770. The execution unit is configured to use some or all of the bits of the input floating point number as an index to search for matching entry from the at least one correction table 770. If no matching entry is found, then the correction value is taken to be zero. If a matching entry is found, the correction value stored for that matching entry is added to the least significant bit of the mantissa of the output floating point number at the addition block.
It will be appreciated that the embodiments described above are by way of non-limiting examples only, and that variations within the scope of the claims may be envisaged.
Number | Date | Country | Kind |
---|---|---|---|
1821069.0 | Dec 2018 | GB | national |