# 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."""Elementwise operators"""from__future__importabsolute_importas_absimporttvmfromtvmimporttefrom..importtagfrom..utilsimportget_const_int
[文档]@tvm.te.tag_scope(tag=tag.ELEMWISE)defrelu(x):"""Take relu of input x. Parameters ---------- x : tvm.te.Tensor Input argument. Returns ------- y : tvm.te.Tensor The result. """returnte.compute(x.shape,lambda*i:tvm.te.max(x(*i),tvm.tir.const(0,x.dtype)))
[文档]@tvm.te.tag_scope(tag=tag.ELEMWISE)defleaky_relu(x,alpha):"""Take leaky relu of input x. Parameters ---------- x : tvm.te.Tensor Input argument. alpha : float The slope for the small gradient when x < 0 Returns ------- y : tvm.te.Tensor The result. """def_compute(*indices):value=x(*indices)calpha=tvm.tir.const(alpha,value.dtype)returntvm.tir.Select(value>0,value,value*calpha)returnte.compute(x.shape,_compute)
[文档]@tvm.te.tag_scope(tag=tag.ELEMWISE)defsoftplus(x,beta=1.0,threshold=20.0):"""Compute Softplus activation for input x with numerical stability. Parameters ---------- x : tvm.te.Tensor Input tensor. beta : float, optional The scaling factor β in the Softplus formula (default is 1.0). threshold : float, optional The threshold value for numerical stability (default is 20.0). Returns ------- y : tvm.te.Tensor The result. """def_compute(*indices):value=x(*indices)b=tvm.tir.const(beta,value.dtype)t=tvm.tir.const(threshold,value.dtype)returntvm.tir.Select(b*value>t,value,(1/b)*tvm.tir.log(1+tvm.tir.exp(b*value)))returnte.compute(x.shape,_compute)
[文档]@tvm.te.tag_scope(tag=tag.BROADCAST)defprelu(x,slope,axis=1):"""PReLU. It accepts two arguments: an input ``x`` and a weight array ``W`` and computes the output as :math:`PReLU(x) y = x > 0 ? x : W * x`, where :math:`*` is an elementwise multiplication for each sample in the batch. Parameters ---------- x : tvm.te.Tensor Input argument. slope : tvm.te.Tensor Channelised slope tensor for prelu axis : int The axis where the channel data needs to be applied Returns ------- y : tvm.te.Tensor The result. Links ----- [http://arxiv.org/pdf/1502.01852v1.pdf] """assertlen(slope.shape)==1assertaxis<len(x.shape)assertget_const_int(slope.shape[0])==get_const_int(x.shape[axis])def_compute_channelwise(*indices):xval=x(*indices)returntvm.tir.Select(xval>0,xval,xval*slope(indices[axis]))returnte.compute(x.shape,_compute_channelwise)