Fmod
Applicability
Product |
Supported |
|---|---|
Atlas 350 Accelerator Card |
√ |
√ |
|
√ |
|
x |
|
√ |
|
x |
|
x |
Function Usage
Computes the remainder of two floating-point numbers a and b element-wise. The formula is as follows:


Trunc means rounding towards zero. Examples:
Fmod(2.0, 1.5) = 0.5
Fmod(-3.0, 1.1) = -0.8
Prototype
- Pass the temporary space through the sharedTmpBuffer input parameter.
- All or part of the source operand tensors are involved in computation.
1 2
template <typename T, bool isReuseSource = false, const FmodConfig& config = DEFAULT_FMOD_CONFIG> __aicore__ inline void Fmod(const LocalTensor<T>& dstTensor, const LocalTensor<T>& src0Tensor, const LocalTensor<T>& src1Tensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const uint32_t calCount)
- All source operand tensors are involved in computation.
1 2
template <typename T, bool isReuseSource = false, const FmodConfig& config = DEFAULT_FMOD_CONFIG> __aicore__ inline void Fmod(const LocalTensor<T>& dstTensor, const LocalTensor<T>& src0Tensor, const LocalTensor<T>& src1Tensor, const LocalTensor<uint8_t>& sharedTmpBuffer)
- All or part of the source operand tensors are involved in computation.
- The API framework allocates temporary space.
- All or part of the source operand tensors are involved in computation.
1 2
template <typename T, bool isReuseSource = false, const FmodConfig& config = DEFAULT_FMOD_CONFIG> __aicore__ inline void Fmod(const LocalTensor<T>& dstTensor, const LocalTensor<T>& src0Tensor, const LocalTensor<T>& src1Tensor, const uint32_t calCount)
- All source operand tensors are involved in computation.
1 2
template <typename T, bool isReuseSource = false, const FmodConfig& config = DEFAULT_FMOD_CONFIG> __aicore__ inline void Fmod(const LocalTensor<T>& dstTensor, const LocalTensor<T>& src0Tensor, const LocalTensor<T>& src1Tensor)
- All or part of the source operand tensors are involved in computation.
Precision conversion is involved in the internal implementation of this API. Therefore, extra temporary space is required to store intermediate variables during computation. The temporary space can be allocated through the API framework or passed by developers through the sharedTmpBuffer input parameter.
- When the API framework allocates temporary space, developers do not need to allocate the space but must reserve the required size for the temporary space.
- When the sharedTmpBuffer input parameter is used for passing the temporary space, the tensor serves as the temporary space. In this case, the API framework is not required for temporary space allocation. This enables developers to manage the sharedTmpBuffer space and reuse the buffer after calling the API, so that the buffer is not repeatedly allocated or deallocated, improving the flexibility and buffer utilization.
If the API framework is used, developers must reserve the temporary space. If sharedTmpBuffer is used, developers must allocate space for the tensor. To obtain the size of the temporary space (BufferSize) to be reserved, use the API provided in GetFmodMaxMinTmpSize.
Parameters
Parameter |
Description |
|---|---|
T |
Operand data type. For the Atlas 350 Accelerator Card, the supported data types are half and float. For the For the For the |
isReuseSource |
Whether the source operand can be modified. This parameter is reserved. Pass the default value false. |
config |
This parameter is supported only by the Atlas 350 Accelerator Card. Fmod configuration. This is an optional parameter of the FmodConfig type. The code below describes the definition. algo: Fmod algorithm. The following options are supported:
iterationNum: number of iterations for high-precision mode with iteration compensation. This parameter is valid only when algo is set to ITERATION_COMPENSATION. The value ranges from 1 to 11. The default value is 11. More iterations increase result precision but reduce performance. When using this parameter, you can determine the number of iterations based on the difference between the exponent bits of the two floating-point numbers. The float type has eight exponent bits in total. The exponent bit difference between src0Tensor and src1Tensor should not exceed 24 × iterationNum. |
1 2 3 4 5 6 7 8 9 | constexpr uint32_t FMOD_ITERATION_NUM_MAX = 11; enum class FmodAlgo { NORMAL = 0, ITERATION_COMPENSATION = 1, }; struct FmodConfig { FmodAlgo algo = FmodAlgo::NORMAL; uint32_t iterationNum = FMOD_ITERATION_NUM_MAX; }; |
Parameter |
Input/Output |
Description |
|---|---|---|
dstTensor |
Output |
Destination operand. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. |
src0Tensor, src1Tensor |
Input |
Source operand. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. The source operand must have the same data type as the destination operand. |
sharedTmpBuffer |
Input |
Temporary space. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. This parameter is used to store intermediate variables during complex computation in Fmod and is provided by developers. For details about how to obtain the temporary space size (BufferSize), see GetFmodMaxMinTmpSize. |
calCount |
Input |
Number of elements involved in the computation. |
Returns
None
Constraints
- For the
Atlas inference product AI Core, the input data must be within the range of [–2147483647.0, 2147483647.0]. - The source operands src0Tensor and src1Tensor must have the same data length.
- The source operand address must not overlap the destination operand address.
- The address of sharedTmpBuffer cannot overlap that of the source or destination operand.
- For the Atlas 350 Accelerator Card, if algo in config is set to ITERATION_COMPENSATION, the operand data type can only be float.
- For details about the operand address alignment requirements, see General Address Alignment Restrictions.
Examples
For a complete call example, see fmod operator sample.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // dstLocal: output tensor for storing the Fmod computation result // src0Local: input tensor for storing the divisor for Fmod computation // src1Local: input tensor for storing the dividend for Fmod computation // sharedTmpBuffer: tensor for storing the temporary buffer during Fmod computation // The API framework allocates temporary space so that all elements can participate in the computation. AscendC::Fmod(dstLocal, src0Local, src1Local); // The API framework allocates temporary space so that only some elements (512) participate in the computation. AscendC::Fmod(dstLocal, src0Local, src1Local, 512); // The temporary space is passed through the sharedTmpBuffer input parameter, and all elements participate in the computation. AscendC::Fmod(dstLocal, src0Local, src1Local, sharedTmpBuffer); // The temporary space is passed through the sharedTmpBuffer input parameter, and only some elements (512) participate in the computation. AscendC::Fmod(dstLocal, src0Local, src1Local, sharedTmpBuffer, 512); |
1 2 3 4 5 | __aicore__ constexpr AscendC::FmodConfig GetConfig() { return { .algo = AscendC::FmodAlgo::ITERATION_COMPENSATION, .iterationNum = 11 }; } static constexpr AscendC::FmodConfig config = GetConfig(); AscendC::Fmod<float, false, config>(dstLocal, src0Local, src1Local, sharedTmpBuffer, 512); |
1 2 3 | Input (src0Local): [-2.56 -2.55 -2.54 ... -0.01 0. 0.01 ... 2.53 2.54 2.55] Input (src1Local): [2. 2. 2. ... 2. 2. 2. ... 2. 2. 2.] Output (dstLocal): [-0.56 -0.55 -0.54 ... -0.01 0. 0.01 ... 0.53 0.54 0.55] |