Fmod

Function Usage

Computes the remainder of two floating-point numbers a and b element-wise using the following formula.

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 tmpTensor argument.
    • All or part of the source operand tensors are involved in computation.
      1
      2
      template <typename T, bool isReuseSource = false>
      __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>
      __aicore__ inline void Fmod(const LocalTensor<T>& dstTensor, const LocalTensor<T>& src0Tensor,const LocalTensor<T>& src1Tensor, const LocalTensor<uint8_t>& sharedTmpBuffer)
      
  • Allocate the temporary space through the API framework.
    • All or part of the source operand tensors are involved in computation.
      1
      2
      template <typename T, bool isReuseSource = false>
      __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>
      __aicore__ inline void Fmod(const LocalTensor<T>& dstTensor, const LocalTensor<T>& src0Tensor,const LocalTensor<T>& src1Tensor)
      

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 manually passed through the sharedTmpBuffer input parameter.

  • When the API framework is used for temporary space allocation, you do not need to allocate the space, but must reserve the required size for the 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 you to manage the sharedTmpBuffer space and reuse the buffer after calling the API, so that the buffer is not repeatedly allocated and deallocated, improving the flexibility and buffer utilization.

If the API framework is used, reserve the temporary space. If sharedTmpBuffer is used, allocate space for the tensor. To obtain the size of the temporary space (BufferSize) to be reserved, use the GetFmodMaxMinTmpSize API provided in GetFmodMaxMinTmpSize.

Parameters

Table 1 Parameters in the template

Parameter

Description

T

Data type of the operand.

isReuseSource

Whether the source operand can be modified. This parameter is reserved. Pass the default value false.

Table 2 API parameters

Parameter

Input/Output

Description

dstTensor

Output

Destination operand.

The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT.

src0Tensor and src1Tensor

Input

Source operand.

The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT.

The source operand must have the same data type as the destination operand.

sharedTmpBuffer

Input

Temporary buffer.

The type is LocalTensor, and the supported TPosition is 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 actually computed data elements. The value range is [0, src0Tensor.GetSize()].

Returns

None

Availability

Constraints

  • The data length of the source operand src0Tensor must be the same as that of src1Tensor.
  • The source operand address must not overlap the destination operand address.
  • The address of sharedTmpBuffer must not overlap the addresses of the source operand and destination operand.
  • For details about the alignment requirements of the operand address offset, see General Restrictions.

Example

1
2
3
4
5
6
AscendC::TPipe pipe;
AscendC::TQue<AscendC::TPosition::VECCALC, 1> tmpQue;
pipe.InitBuffer(tmpQue, 1, bufferSize);  // bufferSize is obtained through the tiling parameter on the host.
AscendC::LocalTensor<uint8_t> sharedTmpBuffer = tmpQue.AllocTensor<uint8_t>();
// The input shape is 1024, the data type of the operator input is half, and the number of actually computed data elements is 512.
AscendC::Fmod(dstLocal, src0Local, src1Local, sharedTmpBuffer, 512);
Result example:
1
2
3
Input (src0Local): [ 0.5317103  -6.37912032  5.53408647 ... 11.11059642 -11.67860335 ]
Input (src1Local): [ 2.12526834  3.09347812  -0.327234  ...  5.64334232 5.97345923]
Output (dstLocal): [ 0.5317 -0.1922  0.2983 ...  5.4673  -5.7051]