FilesLast commitLast update
16 days ago
3 days ago
18 days ago
16 days ago
16 days ago
2 days ago
3 days ago
9 days ago
9 days ago
README

Pdist

English | 简体中文

Product Support Status

Product Supported
Ascend 950PR&950DT products √
Atlas A3 products √
Atlas A2 products √
Atlas 200I/500 A2 inference products ×
Atlas inference products ×
Atlas training products √

Function Description

  • Operator function: the Pdist operator computes the p-norm distances (pairwise p-norm distances) between each pair of row vectors. The output is a one-dimensional vector in upper-triangular row-major order. It is equivalent to torch.nn.functional.pdist.

  • Kernel implementation form: PyPTO DSL (op_kernel/pdist.py). Code generation is performed at the configure stage through enable_pypto_kernel, and the result is delivered as a pypto opc (MIX launch, the AIC stays idle, and all computation is performed on the Vector cores).

  • Computation formula:

yidx=∥xi−xj∥p=(∑k=0M−1∣xi,k−xj,k∣p)1/p,idx=i⋅(2N−i−1)2+(j−i−1)y_{idx}=\left\|x_{i}-x_{j}\right\|_{p}=\left(\sum_{k=0}^{M-1}\left|x_{i,k}-x_{j,k}\right|^{p}\right)^{1/p},\quad idx=\frac{i\cdot(2N-i-1)}{2}+(j-i-1)

0≤i<j≤N−10\le i<j\le N-1

p branch semantics:

p Value Semantics
p = 0 Counts the number of unequal dimensions (Hamming distance). NaN dimensions are counted as unequal
p = +inf Maximum absolute difference (Chebyshev distance). NaN dimensions are ignored
0 < p < +inf The 1/p-th power of the sum of powers (Minkowski distance). p=2 is the Euclidean distance and p=1 is the Manhattan distance

NaN semantics (aligned with GPU/CUDA): when p=0, NaN dimensions are counted as unequal; when p=+inf, NaN dimensions are ignored (the maximum absolute difference of non-NaN dimensions is output, and all-NaN row pairs output 0); for a general p (not 0/0.5/1/2), NaN dimensions are detected pair by pair and NaN is output; for p∈{0.5, 1, 2}, NaN propagates naturally through IEEE arithmetic.

fp16 input: on-chip intermediate accumulation always uses FP32, and the result is narrowed back to FP16 at the output.

Parameter Description

Parameter Name Input/Output/Attribute Description Data Type Data Format
x Input Input tensor, with the shape (N, M), where N≥2 and M≥1. Row i and row j (j>i) form a row pair for computation. Row-major contiguous layout is required. FLOAT16, FLOAT32 ND
p Attribute Norm order, which is p≥0 or +inf. The default value is 2.0 (Euclidean distance). p=0 counts the number of unequal dimensions, p=+inf takes the maximum absolute difference, and 0 FLOAT -
y Output Pairwise distance vector, with the shape (N*(N-1)/2,), arranged in upper-triangular row-major order. The dtype is the same as that of x. FLOAT16, FLOAT32 ND

Constraint Description

  • x must be a two-dimensional (N, M) row-major contiguous tensor, and the dtype of y must be the same as that of x.
  • The shape of y must be (N*(N-1)/2,).
  • Intermediate accumulation of fp16 input uses FP32, and the result is narrowed back to FP16 at the output.
  • Platform differences:
    • When p=+inf and the input contains NaN: on Ascend 950PR/950DT, NaN dimensions are ignored (the maximum absolute difference of non-NaN dimensions is output, and all-NaN row pairs output 0), which is aligned with the GPU/CUDA semantics. On other platforms, NaN is propagated to the output.
Specification Item Specification Specification Description
N 2~65535 Number of rows, at least 2 (at least one row pair).
M 1~65535 Number of dimensions per row. When it exceeds the on-chip memory, automatic chunked reduction is performed.
p [0, +inf) p≥0 or +inf; p=0 is the Hamming distance, p=+inf is the Chebyshev distance, and p<0 or NaN is rejected as a parameter error.
dtype FLOAT16/FLOAT32 The input and output have the same dtype; fp16 intermediate computation uses FP32 accumulation.

Invocation Description

Invocation Method Invocation Sample Description
aclnn invocation test_aclnn_pdist Invokes the pdist operator through the aclnnPdist API, covering all p branches of fp32, multi-M chunking, and fp16.
aclnn invocation (Forward) test_aclnn_pdist_forward Invokes the operator through the aclnnPdistForward API (the composite scalar p entry).
GEIR invocation test_geir_pdist Invokes the pdist operator in GE IR graph mode (RunGraph).