|
|
|
|
|
|
|
|
|
|
|
from onnxscript import script |
|
|
from onnxscript.onnx_opset import opset15 as op |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@script() |
|
|
def ReduceSumSquare(data, axes, keepdims: int): |
|
|
return op.ReduceSum(data * data, axes, keepdims=keepdims) |
|
|
|
|
|
|
|
|
@script() |
|
|
def ReduceL1(data, axes, keepdims: int): |
|
|
return op.ReduceSum(op.Abs(data), axes, keepdims=keepdims) |
|
|
|
|
|
|
|
|
@script() |
|
|
def ReduceL2(data, axes, keepdims: int): |
|
|
sum_square = op.ReduceSum(data * data, axes, keepdims=keepdims) |
|
|
|
|
|
|
|
|
|
|
|
sum_square_dbl = op.Cast(sum_square, to=1) |
|
|
sqrt = op.Sqrt(sum_square_dbl) |
|
|
return op.CastLike(sqrt, data) |
|
|
|
|
|
|
|
|
@script() |
|
|
def ReduceLogSum(data, axes, keepdims: int): |
|
|
return op.Log(op.ReduceSum(data, axes, keepdims=keepdims)) |
|
|
|
|
|
|
|
|
@script() |
|
|
def ReduceLogSumExp(data, axes, keepdims: int): |
|
|
return op.Log(op.ReduceSum(op.Exp(data), axes, keepdims=keepdims)) |
|
|
|
|
|
|
|
|
@script() |
|
|
def Hardmax(X, axis: int): |
|
|
"""Hardmax is similar to ArgMax, with the result being encoded OneHot style.""" |
|
|
argmax = op.ArgMax(X, axis=axis, keepdims=False) |
|
|
|
|
|
|
|
|
|
|
|
xshape = op.Shape(X, start=axis) |
|
|
zero = op.Constant(value_ints=[0]) |
|
|
depth = op.GatherElements(xshape, zero) |
|
|
empty_shape = op.Constant(value_ints=[0]) |
|
|
depth = op.Reshape(depth, empty_shape) |
|
|
values = op.Constant(value_ints=[0, 1]) |
|
|
cast_values = op.CastLike(values, X) |
|
|
return op.OneHot(argmax, depth, cast_values, axis=axis) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@script() |
|
|
def DepthToSpace(input, blocksize: int, mode: str): |
|
|
|
|
|
b, c, h, w = op.Split(op.Shape(input), [1, 1, 1, 1]) |
|
|
|
|
|
size_0 = op.Constant(value_int=blocksize) |
|
|
size = op.Reshape(size_0, [1]) |
|
|
if mode == "DCR": |
|
|
tmpshape = op.Concat(b, size, size, c / (size * size), h, w, axis=0) |
|
|
reshaped = op.Reshape(input, tmpshape) |
|
|
transposed = op.Transpose(reshaped, perm=[0, 3, 4, 1, 5, 2]) |
|
|
else: |
|
|
|
|
|
tmpshape = op.Concat(b, c / (size * size), size, size, h, w, axis=0) |
|
|
reshaped = op.Reshape(input, tmpshape) |
|
|
transposed = op.Transpose(reshaped, perm=[0, 1, 4, 2, 5, 3]) |
|
|
finalshape = op.Concat(b, c / (size * size), h * size, w * size, axis=0) |
|
|
y = op.Reshape(transposed, finalshape) |
|
|
return y |
|
|
|
|
|
|
|
|
@script() |
|
|
def SpaceToDepth(input, blocksize: int): |
|
|
|
|
|
b, C, H, W = op.Split(op.Shape(input), [1, 1, 1, 1], axis=0) |
|
|
size_0 = op.Constant(value_int=blocksize) |
|
|
|
|
|
size = op.Reshape(size_0, [1]) |
|
|
|
|
|
tmpshape = op.Concat(b, C, H / size, size, W / size, size, axis=0) |
|
|
reshaped = op.Reshape(input, tmpshape) |
|
|
transposed = op.Transpose(reshaped, perm=[0, 3, 5, 1, 2, 4]) |
|
|
finalshape = op.Concat(b, C * size * size, H / size, W / size, axis=0) |
|
|
y = op.Reshape(transposed, finalshape) |
|
|
return y |
|
|
|