# Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.# pylint: disable=invalid-name"""ScatterND operator"""fromtvmimportte,tir# hide redefinition of min and maxfromtvm.tirimportexprfromtvm.arith.analyzerimportAnalyzerdef_verify_scatter_nd_inputs(data,indices,updates):analyzer=Analyzer()mdim=int(indices.shape[0])assertmdim<=len(data.shape),(f"The first dimension of the indices ({mdim}) must be less than or equal to "f"the length of the shape of the output ({len(data.shape)}).")foriinrange(len(indices.shape)-1):ifisinstance(indices.shape[i+1],expr.Var)orisinstance(updates.shape[i],expr.Var):continueassertanalyzer.can_prove_equal(indices.shape[i+1],updates.shape[i]),(f"Dimension of indices[{i+1}] ({indices.shape[i+1]}) must equal dimension of "f"updates[{i}] ({updates.shape[i]}).")foriinrange(mdim,len(data.shape)):data_ind=i-mdim+len(indices.shape)-1ifisinstance(updates.shape[data_ind],expr.Var)orisinstance(data.shape[i],expr.Var):continueassertupdates.shape[data_ind]==data.shape[i],(f"Dimension of updates[{data_ind}] ({updates.shape[data_ind]}) must equal dimension "f"of out_shape[{i}] ({data.shape[i]}).")assert("int"inindices.dtype),f"Indices must be a tensor of integers, but its elements are {indices.dtype}."
[文档]defscatter_nd(data,indices,updates,mode):"""Scatter elements from a n-dimension array. Given updates with shape (Y_0, ..., Y_{K-1}, X_M, ..., X_{N-1}), indices with shape (M, Y_0, ..., Y_{K-1}), and output copied from data with shape (X_0, X_1, ..., X_{N-1}), scatter_nd computes .. code-block:: output[indices[0, y_0, ..., y_{K-1}], ..., indices[M-1, y_0, ..., y_{K-1}], x_M, ..., x_{N-1} ] = f(output[...], updates[y_0, ..., y_{K-1}, x_M, ..., x_{N-1}]) where the update function f is determinted by the mode. Parameters ---------- data : tvm.te.Tensor The source array. indices : tvm.te.Tensor The indices of the values to extract. updates : tvm.te.Tensor The updates to apply at the Indices mode : string The update mode for the algorithm, either "update" or "add" If update, the update values will replace the input data If add, the update values will be added to the input data Returns ------- ret : tvm.te.Tensor """_verify_scatter_nd_inputs(data,indices,updates)defgen_ir(data_ptr,indices_ptr,updates_ptr,out_ptr):# pylint: disable=invalid-nameib=tir.ir_builder.create()data=ib.buffer_ptr(data_ptr)indices=ib.buffer_ptr(indices_ptr)updates=ib.buffer_ptr(updates_ptr)out=ib.buffer_ptr(out_ptr)# We combine all the indices dimensions but the first one into a single# dimension so we can iterate it in single loop instead of an arbitrary# number of loops. We do the same thing for all the update dimensions.fused_indices_dimension=1foriinindices_ptr.shape[1:]:fused_indices_dimension*=ifused_updates_dimension=1foriinupdates_ptr.shape[len(indices_ptr.shape)-1:]:fused_updates_dimension*=ifused_shape=1foriindata_ptr.shape:fused_shape*=iwithib.for_range(0,fused_shape)asi:out[i]=data[i]withib.for_range(0,fused_indices_dimension)asi:withib.for_range(0,fused_updates_dimension,kind="parallel")asj:offset=fused_updates_dimensionindex=j# This is x_M, .. x_{N-1} part of the index into out.# Build up the indices[0, y_0, .. y_{K-1}], .. indices[M-1, y_0, .. y_{K-1}] part# of the index into out.forlinreversed(range(indices_ptr.shape[0].value)):# indices[i * l * fused_indices_dimension] = indices[l, y_0, ... y_{k-1}]index+=offset*indices[i+l*fused_indices_dimension]offset*=data_ptr.shape[l]ifmode=="update":out[index]=updates[i*fused_updates_dimension+j]elifmode=="add":out[index]+=updates[i*fused_updates_dimension+j]elifmode=="mul":out[index]*=updates[i*fused_updates_dimension+j]elifmode=="min":out[index]=tir.min(out[index],updates[i*fused_updates_dimension+j])elifmode=="max":out[index]=tir.max(out[index],updates[i*fused_updates_dimension+j])else:raiseNotImplementedError("scatter_nd mode not in [update, add, mul, min, max]:",mode)returnib.get()out_buf=tir.decl_buffer(data.shape,data.dtype,"out_buf")returnte.extern([data.shape],[data,indices,updates],lambdains,outs:gen_ir(ins[0],ins[1],ins[2],outs[0]),dtype=data.dtype,out_buffers=[out_buf],name="scatter_nd.generic",tag="scatter_nd.generic",)