pypto_pro.language.simt.hypot#

产品支持情况#

  • Ascend 950PR&950DT系列产品:支持

  • Atlas A3系列产品:不支持

  • Atlas A2系列产品:不支持

功能说明#

以缩放方式计算两个分量的欧几里得范数,避免直接计算平方和造成不必要的上溢或下溢。

\[result = \sqrt{lhs^2+rhs^2}\]

函数原型#

pypto_pro.language.simt.hypot(
    lhs: Scalar,
    rhs: Scalar,
) -> Scalar

参数说明#

参数

输入/输出

说明

lhs

输入

源操作数,Scalar类型,仅支持DT_FP32。Tensor或Tile元素需通过下标访问后传入。

rhs

输入

源操作数,Scalar类型,仅支持DT_FP32。Tensor或Tile元素需通过下标访问后传入。

约束说明#

只能在由@pypto_pro.language.vector_function(mode=”simt”)定义的SIMT入口函数或辅助函数中调用。

返回值说明#

返回两个分量的欧几里得范数,数据类型为DT_FP32。两个输入均为零时返回正零;任一输入为无穷时返回正无穷,即使另有输入为NaN;无无穷输入且任一输入为NaN时返回NaN。

调用示例#

import pypto_pro.language as pl


@pl.vector_function(mode="simt", max_threads=64)
def hypot_example(
    lhs: pl.Tensor[[1, 64], pl.DT_FP32],
    rhs: pl.Tensor[[1, 64], pl.DT_FP32],
    output: pl.Tensor[[1, 64], pl.DT_FP32],
):
    tid = pl.simt.linear_thread_idx()
    output[0, tid] = pl.simt.hypot(lhs[0, tid], rhs[0, tid])


@pl.jit()
def hypot_kernel(
    lhs: pl.Tensor[[1, 64], pl.DT_FP32],
    rhs: pl.Tensor[[1, 64], pl.DT_FP32],
    output: pl.Tensor[[1, 64], pl.DT_FP32],
):
    with pl.section_vector():
        hypot_example[64](lhs, rhs, output)