text
stringlengths 1
2.05k
|
---|
import RunAll
from ..helpers |
import make_test, Tensor, Dtype, FixedImpl, to_fp |
class Reduce_log_sum_exp(RunAll):
@staticmethod
def reduce_log_sum_exp_fp32x32():
def reduce_log_sum_exp_export_do_not_keepdims():
shape = [3, 2, 2]
axes = np.array([2], dtype=np.int64)
keepdims = False
x = np.reshape(np.arange(1, np.prod(shape) + 1), shape)
y = np.log(np.sum(np.exp(x), axis=tuple(axes), keepdims=False)).astype(np.float64)
x = Tensor(Dtype.FP32x32, x.shape, to_fp(
x.flatten(), FixedImpl.FP32x32))
y = Tensor(Dtype.FP32x32, y.shape, to_fp(
y.flatten(), FixedImpl.FP32x32))
name = "reduce_log_sum_exp_fp32x32_export_do_not_keepdims"
make_test(
[x], y, "input_0.reduce_log_sum_exp(2, false)", name)
def reduce_log_sum_exp_export_keepdims():
shape = [3, 2, 2]
axes = np.array([2], dtype=np.int64)
keepdims = True
x = np.reshape(np.arange(1, np.prod(shape) + 1), shape)
y = np.log(np.sum(np.exp(x), axis=tuple(axes), keepdims=True)).astype(np.float64)
x = Tensor(Dtype.FP32x32, x.shape, to_fp(
x.flatten(), FixedImpl.FP32x32))
y = Tensor(Dtype.FP32x32, y.shape, to_fp(
y.flatten(), FixedImpl.FP32x32))
name = "reduce_log_sum_exp_fp32x32_export_keepdims"
make_test(
[x], y, "input_0.reduce_log_sum_exp(2, true)", name)
def reduce_log_sum_exp_axis_0():
shape = [3, 2, 2]
axes = np.array([0], dtype=np.int64)
keepdims = True
x = np.reshape(np.arange(1, np.prod(shape) + 1), shape)
y = np.log(np.sum(np.exp(x), axis=tuple(axes), keepdims=True)).astype(np.float64)
x = Tensor(Dtype.FP32x32, x.shape, to_fp(
x.flatten(), FixedImpl.FP32x32))
y = Tensor(Dtype.FP32x32, y.shape, to_fp(
y.flatten(), FixedImpl.FP32x32))
name = "reduce_log_sum_exp_fp32x32_export_n |
egative_axes_keepdims"
make_test(
[x], y, "input_0.reduce_log_sum_exp(0, true)", name)
reduce_log_sum_exp_export_do_not_keepdims()
reduce_log_sum_exp_export_keepdims()
reduce_log_sum_exp_axis_0() |
import numpy as np
from nodegen.node |
import RunAll
from ..helpers |
import make_test, to_fp, Tensor, Dtype, FixedImpl |
class Reduce_mean(RunAll):
@staticmethod
def reduce_mean_u32():
def reduce_mean_1D():
x = np.array([0, 1, 2,]).astype(np.uint32)
y = np.mean(x, keepdims=True).astype(np.uint32)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_mean_u32_1D"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::None(()), Option::None(()))", name)
def reduce_mean_2D():
def default():
x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2)
y = np.mean(x, keepdims=True).astype(np.uint32)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_mean_u32_2D_default"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::None(()), Option::None(()))", name)
def keepdims():
x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2)
y = np.mean(x, keepdims=False).astype(np.uint32)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_mean_u32_2D_keepdims"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::Some(false), Option::None(()))", name)
def axis_1():
x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2)
y = np.mean(x, axis=(1), keepdims=True).astype(np.uint32)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_mean_u32_2D_axis_1"
make_test(
[x], y, "input_0.reduce_mean(Option::Some(array![1].span()), Option::None(()), Option::None(()))", name)
default()
keepdims() |
axis_1()
reduce_mean_1D()
reduce_mean_2D()
@staticmethod
def reduce_mean_i32():
def reduce_mean_1D():
x = np.array([0, 1, 2,]).astype(np.int32)
y = np.mean(x, keepdims=True).astype(np.int32)
x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reduce_mean_i32_1D"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::None(()), Option::None(()))", name)
def reduce_mean_2D():
def default():
x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2)
y = np.mean(x, keepdims=True).astype(np.int32)
x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reduce_mean_i32_2D_default"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::None(()), Option::None(()))", name)
def keepdims():
x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2)
y = np.mean(x, keepdims=False).astype(np.int32)
x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reduce_mean_i32_2D_keepdims"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::Some(false), Option::None(()))", name)
def axis_1():
x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2)
y = np.mean(x, axis=(1), keepdims=True).astype(np.int32)
x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reduce_mean_i32_2D_axis_1"
make_test(
[x], y, "input_0.reduce_mean(Option::Some(array![1].span()), Option::None(()), Option::None(()))", name)
default( |
)
keepdims()
axis_1()
reduce_mean_1D()
reduce_mean_2D()
@staticmethod
def reduce_mean_i8():
def reduce_mean_1D():
x = np.array([0, 1, 2,]).astype(np.int8)
y = np.mean(x, keepdims=True).astype(np.int8)
x = Tensor(Dtype.FP8x23, x.shape, x.flatten())
y = Tensor(Dtype.FP8x23, y.shape, y.flatten())
name = "reduce_mean_i8_1D"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::None(()), Option::None(()))", name)
def reduce_mean_2D():
def default():
x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2)
y = np.mean(x, keepdims=True).astype(np.int8)
x = Tensor(Dtype.FP8x23, x.shape, x.flatten())
y = Tensor(Dtype.FP8x23, y.shape, y.flatten())
name = "reduce_mean_i8_2D_default"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::None(()), Option::None(()))", name)
def keepdims():
x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2)
y = np.mean(x, keepdims=False).astype(np.int8)
x = Tensor(Dtype.FP8x23, x.shape, x.flatten())
y = Tensor(Dtype.FP8x23, y.shape, y.flatten())
name = "reduce_mean_i8_2D_keepdims"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::Some(false), Option::None(()))", name)
def axis_1():
x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2)
y = np.mean(x, axis=(1), keepdims=True).astype(np.int8)
x = Tensor(Dtype.FP8x23, x.shape, x.flatten())
y = Tensor(Dtype.FP8x23, y.shape, y.flatten())
name = "reduce_mean_i8_2D_axis_1"
make_test(
[x], y, "input_0.reduce_mean(Option::Some(array![1].span()), Option::None(()), Optio |
n::None(()))", name)
default()
keepdims()
axis_1()
reduce_mean_1D()
reduce_mean_2D()
@staticmethod
def reduce_mean_fp8x23():
def reduce_mean_1D():
x = np.array([0, 1, 2,]).astype(np.int64)
y = np.mean(x, keepdims=True)
x = Tensor(Dtype.FP8x23, x.shape, to_fp(
x.flatten(), FixedImpl.FP8x23))
y = Tensor(Dtype.FP8x23, y.shape, to_fp(
y.flatten(), FixedImpl.FP8x23))
name = "reduce_mean_fp8x23_1D"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::None(()), Option::None(()))", name)
def reduce_mean_2D():
def default():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2)
y = np.mean(x, keepdims=True)
x = Tensor(Dtype.FP8x23, x.shape, to_fp(
x.flatten(), FixedImpl.FP8x23))
y = Tensor(Dtype.FP8x23, y.shape, to_fp(
y.flatten(), FixedImpl.FP8x23))
name = "reduce_mean_fp8x23_2D_default"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::None(()), Option::None(()))", name)
def keepdims():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2)
y = np.mean(x, keepdims=False)
x = Tensor(Dtype.FP8x23, x.shape, to_fp(
x.flatten(), FixedImpl.FP8x23))
y = Tensor(Dtype.FP8x23, y.shape, to_fp(
y.flatten(), FixedImpl.FP8x23))
name = "reduce_mean_fp8x23_2D_keepdims"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::Some(false), Option::None(()))", name)
def axis_1():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2)
y = np.mean(x, axis=(1), keepdims=True)
x = Tensor(Dtype.FP8x23, x |
.shape, to_fp(
x.flatten(), FixedImpl.FP8x23))
y = Tensor(Dtype.FP8x23, y.shape, to_fp(
y.flatten(), FixedImpl.FP8x23))
name = "reduce_mean_fp8x23_2D_axis_1"
make_test(
[x], y, "input_0.reduce_mean(Option::Some(array![1].span()), Option::None(()), Option::None(()))", name)
default()
keepdims()
axis_1()
reduce_mean_1D()
reduce_mean_2D()
@staticmethod
def reduce_mean_fp16x16():
def reduce_mean_1D():
x = np.array([0, 1, 2,]).astype(np.int64)
y = np.mean(x, keepdims=True)
x = Tensor(Dtype.FP16x16, x.shape, to_fp(
x.flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(
y.flatten(), FixedImpl.FP16x16))
name = "reduce_mean_fp16x16_1D"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::None(()), Option::None(()))", name)
def reduce_mean_2D():
def default():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2)
y = np.mean(x, keepdims=True)
x = Tensor(Dtype.FP16x16, x.shape, to_fp(
x.flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(
y.flatten(), FixedImpl.FP16x16))
name = "reduce_mean_fp16x16_2D_default"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::None(()), Option::None(()))", name)
def keepdims():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2)
y = np.mean(x, keepdims=False)
x = Tensor(Dtype.FP16x16, x.shape, to_fp(
x.flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(
y.flatten(), FixedImpl.FP16x16)) |
name = "reduce_mean_fp16x16_2D_keepdims"
make_test(
[x], y, "input_0.reduce_mean(Option::None(()), Option::Some(false), Option::None(()))", name)
def axis_1():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2)
y = np.mean(x, axis=(1), keepdims=True)
x = Tensor(Dtype.FP16x16, x.shape, to_fp(
x.flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(
y.flatten(), FixedImpl.FP16x16))
name = "reduce_mean_fp16x16_2D_axis_1"
make_test(
[x], y, "input_0.reduce_mean(Option::Some(array![1].span()), Option::None(()), Option::None(()))", name)
default()
keepdims()
axis_1()
reduce_mean_1D()
reduce_mean_2D() |
import numpy as np
from nodegen.node |
import RunAll
from ..helpers |
import make_test, to_fp, Tensor, Dtype, FixedImpl |
class Reduce_min(RunAll):
@staticmethod
def reduce_min_u32():
def reduce_min_1D():
x = np.array([0, 1, 2,]).astype(np.uint32)
y = np.minimum.reduce(x, axis=None, keepdims=True).astype(np.uint32)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_min_u32_1D"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::None(()), Option::None(()))", name)
def reduce_min_2D():
def default():
x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2)
y = np.minimum.reduce(x, axis=None, keepdims=True).astype(np.uint32)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_min_u32_2D_default"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::None(()), Option::None(()))", name)
def keepdims():
x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2)
y = np.minimum.reduce(x, axis=None, keepdims=False).astype(np.uint32)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_min_u32_2D_keepdims"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::Some(false), Option::None(()))", name)
def axis_1():
x = np.array([0, 1, 2, 3]).astype(np.uint32).reshape(2, 2)
y = np.minimum.reduce(x, axis=(1), keepdims=True).astype(np.uint32)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_min_u32_2D_axis_1"
make_test(
[x], y, "input_0.reduce_min(Option::Some(array![1].span()), Option::None(()), Option::None(())) |
", name)
default()
keepdims()
axis_1()
reduce_min_1D()
reduce_min_2D()
@staticmethod
def reduce_min_i32():
def reduce_min_1D():
x = np.array([0, 1, 2,]).astype(np.int32)
y = np.minimum.reduce(x, axis=None, keepdims=True).astype(np.int32)
x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reduce_min_i32_1D"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::None(()), Option::None(()))", name)
def reduce_min_2D():
def default():
x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2)
y = np.minimum.reduce(x, axis=None, keepdims=True).astype(np.int32)
x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reduce_min_i32_2D_default"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::None(()), Option::None(()))", name)
def keepdims():
x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2)
y = np.minimum.reduce(x, axis=None, keepdims=False).astype(np.int32)
x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reduce_min_i32_2D_keepdims"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::Some(false), Option::None(()))", name)
def axis_1():
x = np.array([0, 1, 2, 3]).astype(np.int32).reshape(2, 2)
y = np.minimum.reduce(x, axis=(1), keepdims=True).astype(np.int32)
x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reduce_min_i32_2D_axis_1"
make_test(
[x], |
y, "input_0.reduce_min(Option::Some(array![1].span()), Option::None(()), Option::None(()))", name)
default()
keepdims()
axis_1()
reduce_min_1D()
reduce_min_2D()
@staticmethod
def reduce_min_i8():
def reduce_min_1D():
x = np.array([0, 1, 2,]).astype(np.int8)
y = np.minimum.reduce(x, axis=None, keepdims=True).astype(np.int8)
x = Tensor(Dtype.FP8x23, x.shape, x.flatten())
y = Tensor(Dtype.FP8x23, y.shape, y.flatten())
name = "reduce_min_i8_1D"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::None(()), Option::None(()))", name)
def reduce_min_2D():
def default():
x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2)
y = np.minimum.reduce(x, axis=None, keepdims=True).astype(np.int8)
x = Tensor(Dtype.FP8x23, x.shape, x.flatten())
y = Tensor(Dtype.FP8x23, y.shape, y.flatten())
name = "reduce_min_i8_2D_default"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::None(()), Option::None(()))", name)
def keepdims():
x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2)
y = np.minimum.reduce(x, axis=None, keepdims=False).astype(np.int8)
x = Tensor(Dtype.FP8x23, x.shape, x.flatten())
y = Tensor(Dtype.FP8x23, y.shape, y.flatten())
name = "reduce_min_i8_2D_keepdims"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::Some(false), Option::None(()))", name)
def axis_1():
x = np.array([0, 1, 2, 3]).astype(np.int8).reshape(2, 2)
y = np.minimum.reduce(x, axis=(1), keepdims=True).astype(np.int8)
x = Tensor(Dtype.FP8x23, x.shape, x.flatten())
y = Tensor(Dtype.FP8x23, y.shape, y.flatten()) |
name = "reduce_min_i8_2D_axis_1"
make_test(
[x], y, "input_0.reduce_min(Option::Some(array![1].span()), Option::None(()), Option::None(()))", name)
default()
keepdims()
axis_1()
reduce_min_1D()
reduce_min_2D()
@staticmethod
def reduce_min_fp8x23():
def reduce_min_1D():
x = np.array([0, 1, 2,]).astype(np.int64)
y = np.minimum.reduce(x, axis=None, keepdims=True)
x = Tensor(Dtype.FP8x23, x.shape, to_fp(
x.flatten(), FixedImpl.FP8x23))
y = Tensor(Dtype.FP8x23, y.shape, to_fp(
y.flatten(), FixedImpl.FP8x23))
name = "reduce_min_fp8x23_1D"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::None(()), Option::None(()))", name)
def reduce_min_2D():
def default():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2)
y = np.minimum.reduce(x, axis=None, keepdims=True)
x = Tensor(Dtype.FP8x23, x.shape, to_fp(
x.flatten(), FixedImpl.FP8x23))
y = Tensor(Dtype.FP8x23, y.shape, to_fp(
y.flatten(), FixedImpl.FP8x23))
name = "reduce_min_fp8x23_2D_default"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::None(()), Option::None(()))", name)
def keepdims():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2)
y = np.minimum.reduce(x, axis=None, keepdims=False)
x = Tensor(Dtype.FP8x23, x.shape, to_fp(
x.flatten(), FixedImpl.FP8x23))
y = Tensor(Dtype.FP8x23, y.shape, to_fp(
y.flatten(), FixedImpl.FP8x23))
name = "reduce_min_fp8x23_2D_keepdims"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::Some(f |
alse), Option::None(()))", name)
def axis_1():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2)
y = np.minimum.reduce(x, axis=(1), keepdims=True)
x = Tensor(Dtype.FP8x23, x.shape, to_fp(
x.flatten(), FixedImpl.FP8x23))
y = Tensor(Dtype.FP8x23, y.shape, to_fp(
y.flatten(), FixedImpl.FP8x23))
name = "reduce_min_fp8x23_2D_axis_1"
make_test(
[x], y, "input_0.reduce_min(Option::Some(array![1].span()), Option::None(()), Option::None(()))", name)
default()
keepdims()
axis_1()
reduce_min_1D()
reduce_min_2D()
@staticmethod
def reduce_min_fp16x16():
def reduce_min_1D():
x = np.array([0, 1, 2,]).astype(np.int64)
y = np.minimum.reduce(x, axis=None, keepdims=True)
x = Tensor(Dtype.FP16x16, x.shape, to_fp(
x.flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(
y.flatten(), FixedImpl.FP16x16))
name = "reduce_min_fp16x16_1D"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::None(()), Option::None(()))", name)
def reduce_min_2D():
def default():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2)
y = np.minimum.reduce(x, axis=None, keepdims=True)
x = Tensor(Dtype.FP16x16, x.shape, to_fp(
x.flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(
y.flatten(), FixedImpl.FP16x16))
name = "reduce_min_fp16x16_2D_default"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::None(()), Option::None(()))", name)
def keepdims():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2) |
y = np.minimum.reduce(x, axis=None, keepdims=False)
x = Tensor(Dtype.FP16x16, x.shape, to_fp(
x.flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(
y.flatten(), FixedImpl.FP16x16))
name = "reduce_min_fp16x16_2D_keepdims"
make_test(
[x], y, "input_0.reduce_min(Option::None(()), Option::Some(false), Option::None(()))", name)
def axis_1():
x = np.array([0, 1, 2, 3]).astype(np.int64).reshape(2, 2)
y = np.minimum.reduce(x, axis=(1), keepdims=True)
x = Tensor(Dtype.FP16x16, x.shape, to_fp(
x.flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(
y.flatten(), FixedImpl.FP16x16))
name = "reduce_min_fp16x16_2D_axis_1"
make_test(
[x], y, "input_0.reduce_min(Option::Some(array![1].span()), Option::None(()), Option::None(()))", name)
default()
keepdims()
axis_1()
reduce_min_1D()
reduce_min_2D() |
import numpy as np
from nodegen.node |
import RunAll
from ..helpers |
import make_test, to_fp, Tensor, Dtype, FixedImpl |
class Reduce_sum(RunAll):
@staticmethod
def reduce_sum_no_keep_dims():
axes = np.array([1], dtype=np.uint32)
keepdims = 0
x = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [
[9, 10], [11, 12]]]).astype(np.uint32)
y = np.sum(x, axis=tuple(axes.tolist()), keepdims=keepdims == 1)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_sum_no_keep_dims"
make_test(
[x], y, "input_0.reduce_sum(Option::Some(array![1].span()), Option::Some(false), Option::None)", name)
@staticmethod
def reduce_sum_keep_dims():
axes = np.array([1], dtype=np.uint32)
keepdims = 1
x = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [
[9, 10], [11, 12]]]).astype(np.uint32)
y = np.sum(x, axis=tuple(axes.tolist()), keepdims=keepdims == 1)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_sum_keep_dims"
make_test(
[x], y, "input_0.reduce_sum(Option::Some(array![1].span()), Option::Some(true), Option::None)", name)
@staticmethod
def reduce_sum_default_axes_keepdims():
keepdims = 1
x = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [
[9, 10], [11, 12]]]).astype(np.uint32)
y = np.sum(x, axis=None, keepdims=keepdims == 1)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_sum_default_axes_keepdims"
make_test(
[x], y, "input_0.reduce_sum(Option::Some(array![].span()), Option::Some(true), Option::None)", name)
@staticmethod
def reduce_sum_negative_axes_keepdims():
axes = np.array([-2], dtype=np.int64)
keepdims = 1
x = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [
[9, 10], [11, 12]]]).astype(np.uint32)
y = np.sum(x, axis=tuple(axes.tolist()), keepd |
ims=keepdims == 1)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_sum_negative_axes_keepdims"
make_test(
[x], y, "input_0.reduce_sum(Option::Some(array![-2].span()), Option::Some(true), Option::None)", name)
@staticmethod
def reduce_sum_empty_axes_input_noop():
x = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [
[9, 10], [11, 12]]]).astype(np.uint32)
y = np.array(x)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_sum_empty_axes_input_noop"
make_test(
[x], y, "input_0.reduce_sum(Option::None, Option::Some(true), Option::Some(true))", name) |
import numpy as np
from nodegen.node |
import RunAll
from ..helpers |
import make_test, to_fp, Tensor, Dtype, FixedImpl |
import numpy as np |
class Reduce_sum_square(RunAll):
@staticmethod
def reduce_sum_square_fp8x23():
def reduce_sum_square_export_do_not_keepdims():
shape = [3, 2, 2]
axes = np.array([2], dtype=np.int64)
keepdims = False
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int64)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=False).astype(np.int64)
x = Tensor(Dtype.FP8x23, x.shape, x.flatten())
y = Tensor(Dtype.FP8x23, y.shape, y.flatten())
name = "reduce_sum_square_fp8x23_export_do_not_keepdims"
make_test(
[x], y, "input_0.reduce_sum_square(2, false)", name)
def reduce_sum_square_export_keepdims():
shape = [3, 2, 2]
axes = np.array([2], dtype=np.int64)
keepdims = True
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int64)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int64)
x = Tensor(Dtype.FP8x23, x.shape, x.flatten())
y = Tensor(Dtype.FP8x23, y.shape, y.flatten())
name = "reduce_sum_square_fp8x23_export_keepdims"
make_test(
[x], y, "input_0.reduce_sum_square(2, true)", name)
def reduce_sum_square_axis_0():
shape = [3, 3, 3]
axes = np.array([0], dtype=np.int64)
keepdims = True
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int64)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int64)
x = Tensor(Dtype.FP8x23, x.shape, x.flatten())
y = Tensor(Dtype.FP8x23, y.shape, y.flatten())
name = "reduce_sum_square_fp8x23_export_negative_axes_keepdims"
make_test(
[x], y, "input_0.reduce_sum_square(0, true)", name) |
reduce_sum_square_export_do_not_keepdims()
reduce_sum_square_export_keepdims()
reduce_sum_square_axis_0()
@staticmethod
def reduce_sum_square_fp16x16():
def reduce_sum_square_export_do_not_keepdims():
shape = [3, 2, 2]
axes = np.array([2], dtype=np.int64)
keepdims = False
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int64)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=False).astype(np.int64)
x = Tensor(Dtype.FP16x16, x.shape, x.flatten())
y = Tensor(Dtype.FP16x16, y.shape, y.flatten())
name = "reduce_sum_square_fp16x16_export_do_not_keepdims"
make_test(
[x], y, "input_0.reduce_sum_square(2, false)", name)
def reduce_sum_square_export_keepdims():
shape = [3, 2, 2]
axes = np.array([2], dtype=np.int64)
keepdims = True
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int64)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int64)
x = Tensor(Dtype.FP16x16, x.shape, x.flatten())
y = Tensor(Dtype.FP16x16, y.shape, y.flatten())
name = "reduce_sum_square_fp16x16_export_keepdims"
make_test(
[x], y, "input_0.reduce_sum_square(2, true)", name)
def reduce_sum_square_axis_0():
shape = [2, 2, 2]
axes = np.array([0], dtype=np.int64)
keepdims = True
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int64)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int64)
x = Tensor(Dtype.FP16x16, x.shape, x.flatten())
y = Tensor(Dtype.FP16x16, y.shape, y.flatten())
name = "reduce_sum_square_fp16x16_export_negative_axes_keepdims" |
make_test(
[x], y, "input_0.reduce_sum_square(0, true)", name)
reduce_sum_square_export_do_not_keepdims()
reduce_sum_square_export_keepdims()
reduce_sum_square_axis_0()
@staticmethod
def reduce_sum_square_i8():
def reduce_sum_square_export_do_not_keepdims():
shape = [2, 2, 2]
axes = np.array([2], dtype=np.int8)
keepdims = False
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int8)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=False).astype(np.int8)
x = Tensor(Dtype.I8, x.shape, x.flatten())
y = Tensor(Dtype.I8, y.shape, y.flatten())
name = "reduce_sum_square_i8_export_do_not_keepdims"
make_test(
[x], y, "input_0.reduce_sum_square(2, false)", name)
def reduce_sum_square_export_keepdims():
shape = [2, 2, 2]
axes = np.array([2], dtype=np.int8)
keepdims = True
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int8)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int8)
x = Tensor(Dtype.I8, x.shape, x.flatten())
y = Tensor(Dtype.I8, y.shape, y.flatten())
name = "reduce_sum_square_i8_export_keepdims"
make_test(
[x], y, "input_0.reduce_sum_square(2, true)", name)
def reduce_sum_square_axis_0():
shape = [2, 2, 2]
axes = np.array([0], dtype=np.int8)
keepdims = True
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int8)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int8)
x = Tensor(Dtype.I8, x.shape, x.flatten())
y = Tensor(Dtype.I8, y.shape, y.flatten())
name = "reduce_sum_s |
quare_i8_export_negative_axes_keepdims"
make_test(
[x], y, "input_0.reduce_sum_square(0, true)", name)
reduce_sum_square_export_do_not_keepdims()
reduce_sum_square_export_keepdims()
reduce_sum_square_axis_0()
@staticmethod
def reduce_sum_square_i32():
def reduce_sum_square_export_do_not_keepdims():
shape = [3, 2, 2]
axes = np.array([2], dtype=np.int32)
keepdims = False
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int32)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=False).astype(np.int32)
x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reduce_sum_square_i32_export_do_not_keepdims"
make_test(
[x], y, "input_0.reduce_sum_square(2, false)", name)
def reduce_sum_square_export_keepdims():
shape = [3, 2, 2]
axes = np.array([2], dtype=np.int32)
keepdims = True
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int32)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int32)
x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reduce_sum_square_i32_export_keepdims"
make_test(
[x], y, "input_0.reduce_sum_square(2, true)", name)
def reduce_sum_square_axis_0():
shape = [3, 3, 3]
axes = np.array([0], dtype=np.int32)
keepdims = True
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.int32)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.int32)
x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor( |
Dtype.I32, y.shape, y.flatten())
name = "reduce_sum_square_i32_export_negative_axes_keepdims"
make_test(
[x], y, "input_0.reduce_sum_square(0, true)", name)
reduce_sum_square_export_do_not_keepdims()
reduce_sum_square_export_keepdims()
reduce_sum_square_axis_0()
@staticmethod
def reduce_sum_square_u32():
def reduce_sum_square_export_do_not_keepdims():
shape = [3, 2, 2]
axes = np.array([2], dtype=np.uint32)
keepdims = False
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.uint32)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=False).astype(np.uint32)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_sum_square_u32_export_do_not_keepdims"
make_test(
[x], y, "input_0.reduce_sum_square(2, false)", name)
def reduce_sum_square_export_keepdims():
shape = [3, 2, 2]
axes = np.array([2], dtype=np.uint32)
keepdims = True
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.uint32)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.uint32)
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_sum_square_u32_export_keepdims"
make_test(
[x], y, "input_0.reduce_sum_square(2, true)", name)
def reduce_sum_square_axis_0():
shape = [3, 3, 3]
axes = np.array([0], dtype=np.uint32)
keepdims = True
x = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape).astype(np.uint32)
y = np.sum(a=np.square(x), axis=tuple(axes), keepdims=True).astype(np.uint32) |
x = Tensor(Dtype.U32, x.shape, x.flatten())
y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reduce_sum_square_u32_export_negative_axes_keepdims"
make_test(
[x], y, "input_0.reduce_sum_square(0, true)", name)
reduce_sum_square_export_do_not_keepdims()
reduce_sum_square_export_keepdims()
reduce_sum_square_axis_0() |
import numpy as np
from nodegen.node import RunAll
from ..helpers import make_test, to_fp, Tensor, Dtype, Trait, FixedImpl
import tensorflow as tf
class Relu(RunAll):
@staticmethod
def relu_i32():
x = np.random.randint(-5, 9, (2, 2)).astype(np.int32)
layer = tf.keras.layers.ReLU()
y = layer(x).numpy()
x = Tensor(Dtype.I32, x.shape, x.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "relu_i32"
make_test([x], y, "NNTrait::relu(@input_0)",
name, Trait.NN)
@staticmethod
def relu_i8():
x = np.random.randint(-5, 9, (2, 2)).astype(np.int8)
layer = tf.keras.layers.ReLU()
y = layer(x).numpy()
x = Tensor(Dtype.I8, x.shape, x.flatten())
y = Tensor(Dtype.I8, y.shape, y.flatten())
name = "relu_i8"
make_test([x], y, "NNTrait::relu(@input_0)",
name, Trait.NN)
@staticmethod
def relu_fp8x23():
x = np.random.uniform(-5, 7, (2, 2)).astype(np.float64)
layer = tf.keras.layers.ReLU()
y = layer(x).numpy()
x = Tensor(Dtype.FP8x23, x.shape, to_fp(
x.flatten(), FixedImpl.FP8x23))
y = Tensor(Dtype.FP8x23, y.shape, to_fp(
y.flatten(), FixedImpl.FP8x23))
name = "relu_fp8x23"
make_test([x], y, "NNTrait::relu(@input_0)",
name, Trait.NN)
@staticmethod
def relu_fp16x16():
x = np.random.uniform(-5, 7, (2, 2)).astype(np.float64)
layer = tf.keras.layers.ReLU()
y = layer(x).numpy()
x = Tensor(Dtype.FP16x16, x.shape, to_fp(
x.flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(
y.flatten(), FixedImpl.FP16x16))
name = "relu_fp16x16"
make_test([x], y, "NNTrait::relu(@input_0)",
name, Trait.NN)
|
import numpy as np
from nodegen.node |
import RunAll
from ..helpers |
import make_test, Tensor, Dtype
original_shape = [2, 3, 4]
data = np.random.random_sample(original_shape).astype(np.int32)
def reshape_reference_implementation(
data: np.ndarray, shape: np.ndarray, allowzero: int = 0
) -> np.ndarray:
new_shape = np.copy(shape)
if allowzero == 0:
zeros_index = np.where(shape == 0)
new_shape[zeros_index] = np.array(data.shape)[zeros_index]
reshaped = np.reshape(data, new_shape)
return reshaped |
class Reshape(RunAll):
@staticmethod
def reshape_reordered_all_dims():
y = reshape_reference_implementation(
data, np.array([4, 2, 3], dtype=np.int64))
x = Tensor(Dtype.I32, data.shape, data.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reshape_reordered_all_dims"
make_test([x], y, "input_0.reshape(array![4,2,3].span(), false)", name)
@staticmethod
def reshape_reordered_last_dims():
y = reshape_reference_implementation(
data, np.array([2, 4, 3], dtype=np.int64))
x = Tensor(Dtype.I32, data.shape, data.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reshape_reordered_last_dims"
make_test([x], y, "input_0.reshape(array![2,4,3].span(), false)", name)
@staticmethod
def reshape_reduced_dims():
y = reshape_reference_implementation(
data, np.array([2, 12], dtype=np.int64))
x = Tensor(Dtype.I32, data.shape, data.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reshape_reduced_dims"
make_test([x], y, "input_0.reshape(array![2,12].span(), false)", name)
@staticmethod
def reshape_extended_dims():
y = reshape_reference_implementation(
data, np.array([2, 3, 2, 2], dtype=np.int64))
x = Tensor(Dtype.I32, data.shape, data.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reshape_extended_dims"
make_test([x], y, "input_0.reshape(array![2, 3, 2, 2].span(), false)", name)
@staticmethod
def reshape_one_dim():
y = reshape_reference_implementation(
data, np.array([24], dtype=np.int64))
x = Tensor(Dtype.I32, data.shape, data.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reshape_one_dim"
make_test([x], y, "input_0.reshape(array![24].span(), false)", name)
@staticmethod
def reshape_negative_dim():
y = reshape_reference_implementation( |
data, np.array([2, -1, 2], dtype=np.int64))
x = Tensor(Dtype.I32, data.shape, data.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reshape_negative_dim"
make_test([x], y, "input_0.reshape(array![2, -1, 2].span(), false)", name)
@staticmethod
def reshape_negative_extended_dims():
y = reshape_reference_implementation(
data, np.array([-1, 2, 3, 4], dtype=np.int64))
x = Tensor(Dtype.I32, data.shape, data.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reshape_negative_extended_dims"
make_test([x], y, "input_0.reshape(array![-1, 2, 3, 4].span(), false)", name)
@staticmethod
def reshape_zero_dim():
y = reshape_reference_implementation(
data, np.array([2, 0, 4, 1], dtype=np.int64))
x = Tensor(Dtype.I32, data.shape, data.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reshape_zero_dim"
make_test([x], y, "input_0.reshape(array![2, 0, 4, 1].span(), false)", name)
@staticmethod
def reshape_zero_and_negative_dim():
y = reshape_reference_implementation(
data, np.array([2, 0, 1, -1], dtype=np.int64))
x = Tensor(Dtype.I32, data.shape, data.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reshape_zero_and_negative_dim"
make_test([x], y, "input_0.reshape(array![2, 0, 1, -1].span(), false)", name)
@staticmethod
def reshape_zero_and_negative_dim():
original_shape = [0, 3, 4]
data = np.random.random_sample(original_shape).astype(np.int32)
y = reshape_reference_implementation(
data, np.array([3, 4, 0], dtype=np.int64), allowzero=1)
x = Tensor(Dtype.I32, data.shape, data.flatten())
y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reshape_zero_and_negative_dim"
make_test([x], y, "input_0.reshape(array![3, 4, 0].span(), true)", name) |
import numpy as np
from typing |
import Any, Callable
from nodegen.node |
import RunAll
from ..helpers |
import make_test, to_fp, Tensor, Dtype, FixedImpl
def _cartesian(arrays: list[np.ndarray], out: np.ndarray | None = None) -> np.ndarray:
arrays = [np.asarray(x) for x in arrays]
dtype = arrays[0].dtype
n = np.prod([x.size for x in arrays])
if out is None:
out = np.zeros([n, len(arrays)], dtype=dtype)
m = n
out[:, 0] = np.repeat(arrays[0], m)
if arrays[1:]:
_cartesian(arrays[1:], out=out[0:m, 1:])
for j in range(1, arrays[0].size):
out[j * m : (j + 1) * m, 1:] = out[0:m, 1:]
return out
def _get_neighbor_idxes(x: float, n: int, limit: int) -> np.ndarray:
idxes = sorted(range(limit), key=lambda idx: (abs(x - idx), idx))[:n]
idxes = sorted(idxes)
return np.array(idxes)
def _get_neighbor(x: float, n: int, data: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
pad_width = np.ceil(n / 2).astype(int)
padded = np.pad(data, pad_width, mode="edge")
x += pad_width
idxes = _get_neighbor_idxes(x, n, len(padded))
ret = padded[idxes]
return idxes - pad_width, ret
def linear_coeffs(ratio: float, scale: float | None = None) -> np.ndarray:
del scale
return np.array([1 - ratio, ratio])
def linear_coeffs_antialias(ratio: float, scale: float) -> np.ndarray:
scale = min(scale, 1.0)
start = int(np.floor(-1 / scale) + 1)
footprint = 2 - 2 * start
args = (np.arange(start, start + footprint) - ratio) * scale
coeffs = np.clip(1 - np.abs(args), 0, 1)
return np.array(coeffs) / sum(coeffs)
def cubic_coeffs_antialias(ratio: float, scale: float, A: float = -0.75) -> np.ndarray:
scale = min(scale, 1.0)
def compute_coeff(x: float) -> float:
x = abs(x)
x_2 = x * x
x_3 = x * x_2
if x <= 1:
return (A + 2) * x_3 - (A + 3) * x_2 + 1
if x < 2:
return A * x_3 - 5 * A * x_2 + 8 * A * x - 4 * A
return 0.0
i_start = int(np.floor(-2 / scale) + 1)
i_end = 2 - i_start
args = [scale * (i - ratio) for i in range( |
i_start, i_end)]
coeffs = [compute_coeff(x) for x in args]
return np.array(coeffs) / sum(coeffs)
def nearest_coeffs(
ratio: float | int | np.ndarray, mode: str = "round_prefer_floor"
) -> np.ndarray:
if isinstance(ratio, int) or ratio.is_integer():
return np.array([0, 1])
if mode == "round_prefer_floor":
return np.array([ratio <= 0.5, ratio > 0.5])
if mode == "round_prefer_ceil":
return np.array([ratio < 0.5, ratio >= 0.5])
if mode == "floor":
return np.array([1, 0])
if mode == "ceil":
return np.array([0, 1])
raise ValueError(f"Unexpected value {mode!r}.")
def _interpolate_1d_with_x(
data: np.ndarray,
scale_factor: float,
output_width_int: int,
x: float,
get_coeffs: Callable[[float, float], np.ndarray],
roi: np.ndarray | None = None,
extrapolation_value: float = 0.0,
coordinate_transformation_mode: str = "half_pixel",
exclude_outside: bool = False,
) -> np.ndarray:
input_width = len(data)
output_width = scale_factor * input_width
if coordinate_transformation_mode == "align_corners":
if output_width == 1:
x_ori = 0.0
else:
x_ori = x * (input_width - 1) / (output_width - 1)
elif coordinate_transformation_mode == "asymmetric":
x_ori = x / scale_factor
elif coordinate_transformation_mode == "tf_crop_and_resize":
if roi is None:
raise ValueError("roi cannot be None.")
if output_width == 1:
x_ori = (roi[1] - roi[0]) * (input_width - 1) / 2
else:
x_ori = x * (roi[1] - roi[0]) * (input_width - 1) / (output_width - 1)
x_ori += roi[0] * (input_width - 1)
if x_ori < 0 or x_ori > input_width - 1:
return np.array(extrapolation_value)
elif coordinate_transformation_mode == "pytorch_half_pixel":
if output_width == 1:
x_ori = -0.5
else:
x_ori = (x + 0.5) / scale_factor - 0.5
elif coordinate_transformation_mo |
de == "half_pixel":
x_ori = (x + 0.5) / scale_factor - 0.5
elif coordinate_transformation_mode == "half_pixel_symmetric":
adjustment = output_width_int / output_width
center = input_width / 2
offset = center * (1 - adjustment)
x_ori = offset + (x + 0.5) / scale_factor - 0.5
else:
raise ValueError(
f"Invalid coordinate_transformation_mode: {coordinate_transformation_mode!r}."
)
x_ori_int = np.floor(x_ori).astype(int).item()
if x_ori.is_integer():
ratio = 1
else:
ratio = x_ori - x_ori_int
coeffs = get_coeffs(ratio, scale_factor)
n = len(coeffs)
idxes, points = _get_neighbor(x_ori, n, data)
if exclude_outside:
for i, idx in enumerate(idxes):
if idx < 0 or idx >= input_width:
coeffs[i] = 0
coeffs /= sum(coeffs)
return np.dot(coeffs, points).item()
def _interpolate_nd_with_x(
data: np.ndarray,
n: int,
scale_factors: list[float],
output_size: list[int],
x: list[float],
get_coeffs: Callable[[float, float], np.ndarray],
roi: np.ndarray | None = None,
exclude_outside: bool = False,
**kwargs: Any,
) -> np.ndarray:
if n == 1:
return _interpolate_1d_with_x(
data,
scale_factors[0],
output_size[0],
x[0],
get_coeffs,
roi=roi,
exclude_outside=exclude_outside,
**kwargs,
)
res1d = []
for i in range(data.shape[0]):
r = _interpolate_nd_with_x(
data[i],
n - 1,
scale_factors[1:],
output_size[1:],
x[1:],
get_coeffs,
roi=None if roi is None else np.concatenate([roi[1:n], roi[n + 1 :]]),
exclude_outside=exclude_outside,
**kwargs,
)
res1d.append(r)
return _interpolate_1d_with_x(
res1d,
scale_factors[0],
output_size[0],
x[0], |
get_coeffs,
roi=None if roi is None else [roi[0], roi[n]],
exclude_outside=exclude_outside,
**kwargs,
)
def _get_all_coords(data: np.ndarray) -> np.ndarray:
return _cartesian(
[list(range(data.shape[i])) for i in range(len(data.shape))]
)
def interpolate_nd(
data: np.ndarray,
get_coeffs: Callable[[float, float], np.ndarray],
output_size: list[int] | None = None,
scale_factors: list[float] | None = None,
axes: list[int] | None = None,
roi: np.ndarray | None = None,
keep_aspect_ratio_policy: str | None = "stretch",
exclude_outside: bool = False,
**kwargs: Any,
) -> np.ndarray:
if output_size is None and scale_factors is None:
raise ValueError("output_size is None and scale_factors is None.")
r = len(data.shape)
if axes is not None:
if scale_factors is not None:
new_scale_factors = [1.0] * r
for i, d in enumerate(axes):
new_scale_factors[d] = scale_factors[i]
scale_factors = new_scale_factors
if output_size is not None:
new_output_size = [data.shape[i] for i in range(r)]
for i, d in enumerate(axes):
new_output_size[d] = output_size[i]
output_size = new_output_size
if roi is not None:
new_roi = ([0.0] * r) + ([1.0] * r)
naxes = len(axes)
for i, d in enumerate(axes):
new_roi[d] = roi[i]
new_roi[r + d] = roi[naxes + i]
roi = new_roi
else:
axes = list(range(r))
if output_size is not None:
scale_factors = [output_size[i] / data.shape[i] for i in range(r)]
if keep_aspect_ratio_policy != "stretch":
if keep_aspect_ratio_policy == "not_larger":
scale = np.array(scale_factors)[axes].min()
elif keep_aspect_ratio_policy == "not_smaller":
scale = np.array(scale_factors)[axes].max()
else:
raise Va |
lueError(
f"Invalid keep_aspect_ratio_policy={keep_aspect_ratio_policy!r}"
)
scale_factors = [scale if i in axes else 1.0 for i in range(r)]
def round_half_up(x: float) -> int:
return int(x + 0.5)
output_size = [
round_half_up(scale * data.shape[i]) if i in axes else data.shape[i]
for i in range(r)
]
else:
output_size = (scale_factors * np.array(data.shape)).astype(int)
if scale_factors is None:
raise ValueError("scale_factors is None.")
if output_size is None:
raise ValueError("output_size is None.")
ret = np.zeros(output_size)
for x in _get_all_coords(ret):
ret[tuple(x)] = _interpolate_nd_with_x(
data,
len(data.shape),
scale_factors,
output_size,
x,
get_coeffs,
roi=roi,
exclude_outside=exclude_outside,
**kwargs,
)
return ret
def cubic_coeffs(
ratio: float, scale: float | None = None, A: float = -0.75
) -> np.ndarray:
del scale
coeffs = [
((A * (ratio + 1) - 5 * A) * (ratio + 1) + 8 * A) * (ratio + 1) - 4 * A,
((A + 2) * ratio - (A + 3)) * ratio * ratio + 1,
((A + 2) * (1 - ratio) - (A + 3)) * (1 - ratio) * (1 - ratio) + 1,
((A * ((1 - ratio) + 1) - 5 * A) * ((1 - ratio) + 1) + 8 * A)
* ((1 - ratio) + 1)
- 4 * A,
]
return np.array(coeffs) |
class Resize(RunAll):
@staticmethod
def resize_upsample_scales_nearest() -> None:
data = np.array(
[
[
[
[1, 2],
[3, 4],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)
output = interpolate_nd(
data, lambda x, _: nearest_coeffs(x), scale_factors=scales
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_scales_nearest"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(TRANSFORMATION_MODE::HALF_PIXEL_SYMMETRIC),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_downsample_scales_nearest() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)
output = interpolate_nd(
data, lambda x, _: nearest_coeffs(x), scale_factors=scales
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dt |
ype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_downsample_scales_nearest"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_upsample_sizes_nearest() -> None:
data = np.array(
[
[
[
[1, 2],
[3, 4],
]
]
],
dtype=np.float32,
)
sizes = np.array([1, 1, 7, 8], dtype=np.int64)
output = interpolate_nd(
data, lambda x, _: nearest_coeffs(x), output_size=sizes
).astype(np.float32)
x = [data, sizes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_sizes_nearest"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "O |
ption::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_downsample_sizes_nearest() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
]
]
],
dtype=np.float32,
)
sizes = np.array([1, 1, 1, 3], dtype=np.int64)
output = interpolate_nd(
data, lambda x, _: nearest_coeffs(x), output_size=sizes
).astype(np.float32)
x = [data, sizes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_downsample_sizes_nearest"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_upsample_scales_linear() -> None:
data = np.array(
[
[
[
[1, 2],
[3, 4],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)
output = interpolate_nd(
data, lambda x, _: linear_coeffs(x, None), scale_factors=scales
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)): |
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_scales_linear"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::LINEAR),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_upsample_scales_linear_align_corners() -> None:
data = np.array(
[
[
[
[1, 2],
[3, 4],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)
output = interpolate_nd(
data,
lambda x, _: linear_coeffs(x, None),
scale_factors=scales,
coordinate_transformation_mode="align_corners",
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_scales_linear_align_corners"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(TRANSFORMATION_MODE::ALIGN_CORNERS),"
func_sig += "Option::None,"
func_sig += "Option |
::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::LINEAR),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_downsample_scales_linear() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)
output = interpolate_nd(
data, lambda x, _: linear_coeffs(x, None), scale_factors=scales
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_scales_linear"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::LINEAR),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_downsample_scales_linear_align_corners() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)
output = interpolate_nd(
data, |
lambda x, _: linear_coeffs(x, None),
scale_factors=scales,
coordinate_transformation_mode="align_corners",
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_downsample_scales_linear_align_corners"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(TRANSFORMATION_MODE::ALIGN_CORNERS),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::LINEAR),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_upsample_scales_cubic() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)
output = interpolate_nd(
data, lambda x, _: cubic_coeffs(x, None), scale_factors=scales
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_scales_cubic"
func_sig = "data.resize("
func_sig += "Op |
tion::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::CUBIC),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_upsample_scales_cubic_align_corners() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)
output = interpolate_nd(
data,
lambda x, _: cubic_coeffs(x),
scale_factors=scales,
coordinate_transformation_mode="align_corners",
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_scales_cubic_align_corners"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(TRANSFORMATION_MODE::ALIGN_CORNERS),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::CUBIC),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_ |
sig, name)
@staticmethod
def resize_downsample_scales_cubic() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)
output = interpolate_nd(
data, lambda x, _: cubic_coeffs(x), scale_factors=scales
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_downsample_scales_cubic"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::CUBIC),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_downsample_scales_cubic_align_corners() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)
output = interpolate_nd(
data,
lambda x, _: cubic_coeffs(x),
scal |
e_factors=scales,
coordinate_transformation_mode="align_corners",
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_downsample_scales_cubic_align_corners"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(TRANSFORMATION_MODE::ALIGN_CORNERS),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::CUBIC),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_upsample_sizes_cubic() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
sizes = np.array([1, 1, 9, 10], dtype=np.int64)
output = interpolate_nd(
data, lambda x, _: cubic_coeffs(x), output_size=sizes
).astype(np.float32)
x = [data, sizes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_sizes_cubic"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
fun |
c_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::CUBIC),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_downsample_sizes_cubic() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
sizes = np.array([1, 1, 3, 3], dtype=np.int64)
output = interpolate_nd(
data, lambda x, _: cubic_coeffs(x), output_size=sizes
).astype(np.float32)
x = [data, sizes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_downsample_sizes_cubic"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::CUBIC),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_upsample_scales_cubic_A_n0p5_exclude_outside() -> None:
data = np.array(
[
[
[ |
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)
output = interpolate_nd(
data,
lambda x, _: cubic_coeffs(x, A=-0.5),
scale_factors=scales,
exclude_outside=True,
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_scales_cubic_A_n0p5_exclude_outside"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(FixedTrait::<FP16x16>::new(32768, true)),"
func_sig += "Option::Some(true),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::CUBIC),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_downsample_scales_cubic_A_n0p5_exclude_outside() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)
output = interpolate_nd(
data,
lambda x, _: cubic_coeffs(x, A=-0.5),
scale_factors=sca |
les,
exclude_outside=True,
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_downsample_scales_cubic_A_n0p5_exclude_outside"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(FixedTrait::<FP16x16>::new(32768, true)),"
func_sig += "Option::Some(true),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::CUBIC),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_upsample_scales_cubic_asymmetric() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)
output = interpolate_nd(
data,
lambda x, _: cubic_coeffs(x, A=-0.75),
scale_factors=scales,
coordinate_transformation_mode="asymmetric",
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_scales_cubic_asymmetric"
func_sig = "data.resize(" |
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(TRANSFORMATION_MODE::ASYMMETRIC),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::CUBIC),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_tf_crop_and_resize() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
roi = np.array([0, 0, 0.4, 0.6, 1, 1, 0.6, 0.8], dtype=np.float32)
sizes = np.array([1, 1, 3, 3], dtype=np.int64)
output = interpolate_nd(
data,
lambda x, _: linear_coeffs(x),
output_size=sizes,
roi=roi,
coordinate_transformation_mode="tf_crop_and_resize",
).astype(np.float32)
x = [data, sizes, roi]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
x[2] = Tensor(Dtype.FP16x16, x[2].shape, to_fp(x[2].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_tf_crop_and_resize"
func_sig = "data.resize("
func_sig += "roi,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(TRANSFORMATION_MODE::TF_CROP_AND_RESIZE),"
func_sig += "Option::None,"
fun |
c_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::LINEAR),"
func_sig += "Option::None,)"
make_test([x[0], x[1], x[2]], y, func_sig, name)
@staticmethod
def resize_tf_crop_and_resize_extrapolation_value() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
roi = np.array([0, 0, 0.4, 0.6, 1, 1, 1.2, 1.7], dtype=np.float32)
sizes = np.array([1, 1, 3, 3], dtype=np.int64)
output = interpolate_nd(
data,
lambda x, _: linear_coeffs(x),
output_size=sizes,
roi=roi,
coordinate_transformation_mode="tf_crop_and_resize",
extrapolation_value=10.0,
).astype(np.float32)
x = [data, sizes, roi]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
x[2] = Tensor(Dtype.FP16x16, x[2].shape, to_fp(x[2].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_tf_crop_and_resize_extrapolation_value"
func_sig = "data.resize("
func_sig += "roi,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(TRANSFORMATION_MODE::TF_CROP_AND_RESIZE),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(FixedTrait::<FP16x16>::new(655360, false)),"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::LINEAR)," |
func_sig += "Option::None,)"
make_test([x[0], x[1], x[2]], y, func_sig, name)
@staticmethod
def resize_downsample_sizes_linear_pytorch_half_pixel() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
sizes = np.array([1, 1, 3, 1], dtype=np.int64)
output = interpolate_nd(
data,
lambda x, _: linear_coeffs(x),
output_size=sizes,
coordinate_transformation_mode="pytorch_half_pixel",
).astype(np.float32)
x = [data, sizes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_downsample_sizes_linear_pytorch_half_pixel"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(TRANSFORMATION_MODE::PYTORCH_HALF_PIXEL),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::LINEAR),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_upsample_sizes_nearest_floor_align_corners() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16], |
]
]
],
dtype=np.float32,
)
sizes = np.array([1, 1, 8, 8], dtype=np.int64)
output = interpolate_nd(
data,
lambda x, _: nearest_coeffs(x, mode="floor"),
output_size=sizes,
coordinate_transformation_mode="align_corners",
).astype(np.float32)
x = [data, sizes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_sizes_nearest_floor_align_corners"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(TRANSFORMATION_MODE::ALIGN_CORNERS),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "Option::Some(NEAREST_MODE::FLOOR),)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
sizes = np.array([1, 1, 8, 8], dtype=np.int64)
output = interpolate_nd(
data,
lambda x, _: nearest_coeffs(x, mode="round_prefer_ceil"),
output_size=sizes,
coordinate_transformation_mode="asymmetric",
).astype(np.float32) |
x = [data, sizes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(TRANSFORMATION_MODE::ASYMMETRIC),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "Option::Some(NEAREST_MODE::ROUND_PREFER_CEIL),)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_upsample_sizes_nearest_ceil_half_pixel() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
sizes = np.array([1, 1, 8, 8], dtype=np.int64)
output = interpolate_nd(
data, lambda x, _: nearest_coeffs(x, mode="ceil"), output_size=sizes
).astype(np.float32)
x = [data, sizes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_sizes_nearest_ceil_half_pixel"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
fun |
c_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(TRANSFORMATION_MODE::HALF_PIXEL),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "Option::Some(NEAREST_MODE::CEIL),)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_downsample_scales_linear_antialias() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)
output = interpolate_nd(
data, linear_coeffs_antialias, scale_factors=scales
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_downsample_scales_linear_antialias"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::Some(1),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::LINEAR),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_downsample_sizes_linear_antialias() -> None:
data = np.array(
[ |
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
sizes = np.array([1, 1, 3, 3], dtype=np.int64)
output = interpolate_nd(
data, linear_coeffs_antialias, output_size=sizes
).astype(np.float32)
x = [data, sizes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_downsample_sizes_linear_pytorch_half_pixel"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::Some(1),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::LINEAR),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_downsample_scales_cubic_antialias() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
scales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)
output = interpolate_nd(
data, cubic_coeffs_antialias, scale_factors=scales
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)): |
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_downsample_scales_cubic_antialias"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::Some(1),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::CUBIC),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_downsample_sizes_cubic_antialias() -> None:
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
sizes = np.array([1, 1, 3, 3], dtype=np.int64)
output = interpolate_nd(data, cubic_coeffs_antialias, output_size=sizes).astype(
np.float32
)
x = [data, sizes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_downsample_sizes_cubic_antialias"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::Some(1),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_s |
ig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::CUBIC),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_upsample_scales_nearest_axes_2_3() -> None:
axes = np.array([2, 3], dtype=np.int64)
data = np.array(
[
[
[
[1, 2],
[3, 4],
]
]
],
dtype=np.float32,
)
scales = np.array([2.0, 3.0], dtype=np.float32)
output = interpolate_nd(
data, lambda x, _: nearest_coeffs(x), scale_factors=scales, axes=axes
).astype(np.float32)
x = [data, scales, axes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.FP16x16, x[1].shape, to_fp(x[1].flatten(), FixedImpl.FP16x16))
x[2] = Tensor(Dtype.U32, x[2].shape, x[2].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_scales_nearest_axes_2_3"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "axes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "Option::None,)"
make_test([x[0], x[1], x[2]], y, func_sig, name)
@staticmethod
def resize_upsample_scales_nearest_axes_3_2() -> None:
axes = np.array([3, 2], dtype=np.int64)
data = np.array([[[[1, 2],[3, 4],]]],dtype=np.float32,)
scales = np.array([3.0, 2.0], dtype=np.float32)
output = interpolate_nd(
data, lambda x, |
_: nearest_coeffs(x), scale_factors=scales, axes=axes
).astype(np.float32)
x = [data, scales, axes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.FP16x16, x[1].shape, to_fp(x[1].flatten(), FixedImpl.FP16x16))
x[2] = Tensor(Dtype.U32, x[2].shape, x[2].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_scales_nearest_axes_3_2"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "axes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "Option::None,)"
make_test([x[0], x[1], x[2]], y, func_sig, name)
@staticmethod
def resize_upsample_sizes_nearest_axes_2_3() -> None:
data = np.array(
[
[
[
[1, 2],
[3, 4],
]
]
],
dtype=np.float32,
)
sizes = np.array([7, 8], dtype=np.int64)
axes = np.array([2, 3], dtype=np.int64)
output = interpolate_nd(
data, lambda x, _: nearest_coeffs(x), output_size=sizes, axes=axes
).astype(np.float32)
x = [data, sizes, axes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
x[2] = Tensor(Dtype.U32, x[2].shape, x[2].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_sizes_nearest_axes_2_3"
func_sig = "data.resize( |
"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "axes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "Option::None,)"
make_test([x[0], x[1], x[2]], y, func_sig, name)
@staticmethod
def resize_upsample_sizes_nearest_axes_3_2() -> None:
data = np.array(
[
[
[
[1, 2],
[3, 4],
]
]
],
dtype=np.float32,
)
sizes = np.array([8, 7], dtype=np.int64)
axes = np.array([3, 2], dtype=np.int64)
output = interpolate_nd(
data, lambda x, _: nearest_coeffs(x), output_size=sizes, axes=axes
).astype(np.float32)
x = [data, sizes, axes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
x[2] = Tensor(Dtype.U32, x[2].shape, x[2].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_sizes_nearest_axes_3_2"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "axes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "Option::None,)"
make_test([x[0], x[1], x[2]], y, func_sig, name)
@staticmethod
def resize_tf_crop_and_resize_a |
xes_2_3() -> None:
axes = np.array([2, 3], dtype=np.int64)
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
roi = np.array([0.4, 0.6, 0.6, 0.8], dtype=np.float32)
sizes = np.array([3, 3], dtype=np.int64)
output = interpolate_nd(
data,
lambda x, _: linear_coeffs(x),
output_size=sizes,
roi=roi,
axes=axes,
coordinate_transformation_mode="tf_crop_and_resize",
).astype(np.float32)
x = [data, sizes, roi, axes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
x[2] = Tensor(Dtype.FP16x16, x[2].shape, to_fp(x[2].flatten(), FixedImpl.FP16x16))
x[3] = Tensor(Dtype.U32, x[3].shape, x[3].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_tf_crop_and_resize_axes_2_3"
func_sig = "data.resize("
func_sig += "roi,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "axes,"
func_sig += "Option::Some(TRANSFORMATION_MODE::TF_CROP_AND_RESIZE),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::LINEAR),"
func_sig += "Option::None,)"
make_test([x[0], x[1], x[2], x[3]], y, func_sig, name)
@staticmethod
def resize_tf_crop_and_resize_axes_3_2() -> None:
axes = np.array([3, 2], dtype=np.int64)
data = np.array(
[
[
[ |
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
]
],
dtype=np.float32,
)
roi = np.array([0.6, 0.4, 0.8, 0.6], dtype=np.float32)
sizes = np.array([3, 3], dtype=np.int64)
output = interpolate_nd(
data,
lambda x, _: linear_coeffs(x),
output_size=sizes,
roi=roi,
axes=axes,
coordinate_transformation_mode="tf_crop_and_resize",
).astype(np.float32)
x = [data, sizes, roi, axes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
x[2] = Tensor(Dtype.FP16x16, x[2].shape, to_fp(x[2].flatten(), FixedImpl.FP16x16))
x[3] = Tensor(Dtype.U32, x[3].shape, x[3].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_tf_crop_and_resize_axes_3_2"
func_sig = "data.resize("
func_sig += "roi,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "axes,"
func_sig += "Option::Some(TRANSFORMATION_MODE::TF_CROP_AND_RESIZE),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::LINEAR),"
func_sig += "Option::None,)"
make_test([x[0], x[1], x[2], x[3]], y, func_sig, name)
@staticmethod
def resize_upsample_sizes_nearest_not_larger() -> None:
keep_aspect_ratio_policy = "not_larger"
axes = np.array([2, 3], dtype=np.int64)
data = np.array(
[
[
[
[1, 2],
[3, 4],
] |
]
],
dtype=np.float32,
)
sizes = np.array([7, 8], dtype=np.int64)
output = interpolate_nd(
data,
lambda x, _: nearest_coeffs(x),
output_size=sizes,
axes=axes,
keep_aspect_ratio_policy=keep_aspect_ratio_policy,
).astype(np.float32)
x = [data, sizes, axes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
x[2] = Tensor(Dtype.U32, x[2].shape, x[2].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_sizes_nearest_not_larger"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "axes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(KEEP_ASPECT_RATIO_POLICY::NOT_LARGER),"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "Option::None,)"
make_test([x[0], x[1], x[2]], y, func_sig, name)
@staticmethod
def resize_upsample_sizes_nearest_not_smaller() -> None:
keep_aspect_ratio_policy = "not_smaller"
axes = np.array([2, 3], dtype=np.int64)
data = np.array(
[
[
[
[1, 2],
[3, 4],
]
]
],
dtype=np.float32,
)
sizes = np.array([7, 8], dtype=np.int64)
output = interpolate_nd(
data,
lambda x, _: nearest_coeffs(x),
output_size=sizes,
axes=axes,
keep_aspect_ratio_policy=keep_aspect_ratio_policy,
).astype(np.float32)
x |
= [data, sizes, axes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
x[2] = Tensor(Dtype.U32, x[2].shape, x[2].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_sizes_nearest_not_smaller"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "axes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(KEEP_ASPECT_RATIO_POLICY::NOT_SMALLER),"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "Option::None,)"
make_test([x[0], x[1], x[2]], y, func_sig, name)
@staticmethod
def resize_downsample_sizes_nearest_not_larger() -> None:
keep_aspect_ratio_policy = "not_larger"
axes = np.array([2, 3], dtype=np.int64)
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
]
]
],
dtype=np.float32,
)
sizes = np.array([1, 3], dtype=np.int64)
output = interpolate_nd(
data,
lambda x, _: nearest_coeffs(x),
output_size=sizes,
axes=axes,
keep_aspect_ratio_policy=keep_aspect_ratio_policy,
).astype(np.float32)
x = [data, sizes, axes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
x[2] = Tensor(Dtype.U32, x[2].shape, x[2].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16)) |
name = "resize_downsample_sizes_nearest_not_larger"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "axes,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(KEEP_ASPECT_RATIO_POLICY::NOT_LARGER),"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "Option::None,)"
make_test([x[0], x[1], x[2]], y, func_sig, name)
@staticmethod
def resize_downsample_sizes_nearest_not_smaller() -> None:
keep_aspect_ratio_policy = "not_smaller"
axes = np.array([2, 3], dtype=np.int64)
data = np.array(
[
[
[
[1, 2, 3, 4],
[5, 6, 7, 8],
]
]
],
dtype=np.float32,
)
sizes = np.array([1, 3], dtype=np.int64)
output = interpolate_nd(
data,
lambda x, _: nearest_coeffs(x),
output_size=sizes,
axes=axes,
keep_aspect_ratio_policy=keep_aspect_ratio_policy,
).astype(np.float32)
x = [data, sizes, axes]
y = output
x[0] = Tensor(Dtype.FP16x16, x[0].shape, to_fp(x[0].flatten(), FixedImpl.FP16x16))
x[1] = Tensor(Dtype.U32, x[1].shape, x[1].flatten())
x[2] = Tensor(Dtype.U32, x[2].shape, x[2].flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_downsample_sizes_nearest_not_smaller"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "sizes,"
func_sig += "Option::None,"
func_sig += "axes,"
func_sig += "Option::None,"
func_sig += "Option::None," |
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(KEEP_ASPECT_RATIO_POLICY::NOT_SMALLER),"
func_sig += "Option::Some(MODE::NEAREST),"
func_sig += "Option::None,)"
make_test([x[0], x[1], x[2]], y, func_sig, name)
@staticmethod
def resize_downsample_scales_linear_half_pixel_symmetric() -> None:
data = np.array([[[[1, 2, 3, 4]]]], dtype=np.float32)
scales = np.array([1.0, 1.0, 1.0, 0.6], dtype=np.float32)
output = interpolate_nd(
data,
lambda x, _: linear_coeffs(x),
scale_factors=scales,
coordinate_transformation_mode="half_pixel_symmetric",
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_downsample_scales_linear_half_pixel_symmetric"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(TRANSFORMATION_MODE::HALF_PIXEL_SYMMETRIC),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::LINEAR),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name)
@staticmethod
def resize_upsample_scales_linear_half_pixel_symmetric() -> None:
data = np.array([[[[1, 2], [3, 4]]]], dtype=np.float32)
scales = np.array([1.0, 1.0, 2.3, 2.94], dtype=np.float32)
output = interpolate_nd(
data,
lambda x, _: linear_coeffs(x),
scale_factors=scales,
coordinate_transformation_mode="half_pixel_s |
ymmetric",
).astype(np.float32)
x = [data, scales]
y = output
for i in range(len(x)):
x[i] = Tensor(Dtype.FP16x16, x[i].shape, to_fp(x[i].flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "resize_upsample_scales_linear_half_pixel_symmetric"
func_sig = "data.resize("
func_sig += "Option::None,"
func_sig += "scales,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(TRANSFORMATION_MODE::HALF_PIXEL_SYMMETRIC),"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::None,"
func_sig += "Option::Some(MODE::LINEAR),"
func_sig += "Option::None,)"
make_test([x[0], x[1]], y, func_sig, name) |
import numpy as np
from nodegen.node |
import RunAll
from ..helpers |
import make_test, to_fp, Tensor, Dtype, FixedImpl |
class Reverse_sequence(RunAll):
@staticmethod
def Reverse_sequence_u32():
def reverse_sequence_u32_4x4_batch():
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], dtype=np.uint32).reshape((4, 4))
y = np.array([0, 1, 2, 3, 5, 4, 6, 7, 10, 9, 8, 11, 15, 14, 13, 12], dtype=np.uint32).reshape((4, 4))
_x = Tensor(Dtype.U32, x.shape, x.flatten())
_y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reverse_sequence_u32_4x4_batch"
make_test(
[_x],
_y,
"input_0.reverse_sequence(TensorTrait::<usize>::new(array![4].span(), array![1,2,3,4].span()), Option::Some(0), Option::Some(1))",
name
)
def reverse_sequence_u32_4x4_time():
x = np.array([0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15], dtype=np.uint32).reshape((4, 4))
y = np.array([3, 6, 9, 12, 2, 5, 8, 13, 1, 4, 10, 14, 0, 7, 11, 15], dtype=np.uint32).reshape((4, 4))
_x = Tensor(Dtype.U32, x.shape, x.flatten())
_y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reverse_sequence_u32_4x4_time"
make_test(
[_x],
_y,
"input_0.reverse_sequence(TensorTrait::<usize>::new(array![4].span(), array![4,3,2,1].span()), Option::Some(1), Option::Some(0))",
name
)
def reverse_sequence_u32_3x3_batch():
x = np.array([0,1,2,3,4,5,6,7,8], dtype=np.uint32).reshape(3,3)
y = np.array([2,1,0,3,4,5,7,6,8], dtype=np.uint32).reshape(3,3)
_x = Tensor(Dtype.U32, x.shape, x.flatten())
_y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reverse_sequence_u32_3x3_batch"
make_test(
[_x],
_y,
"input_0.reverse_sequence(TensorTrait::<usize>::new(array![3].span(), array![3,1,2].span()), Option::Some(0), Option::Some(1))", |
name
)
def reverse_sequence_u32_3x3_time():
x = np.array([0,1,2,3,4,5,6,7,8], dtype=np.uint32).reshape(3,3)
y = np.array([0,7,8,3,4,5,6,1,2], dtype=np.uint32).reshape(3,3)
_x = Tensor(Dtype.U32, x.shape, x.flatten())
_y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reverse_sequence_u32_3x3_time"
make_test(
[_x],
_y,
"input_0.reverse_sequence(TensorTrait::<usize>::new(array![3].span(), array![1,3,3].span()), Option::Some(1), Option::Some(0))",
name
)
reverse_sequence_u32_4x4_batch()
reverse_sequence_u32_4x4_time()
reverse_sequence_u32_3x3_batch()
reverse_sequence_u32_3x3_time()
@staticmethod
def Reverse_sequence_i32():
def reverse_sequence_i32_batch():
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], dtype=np.int32).reshape((4, 4))
y = np.array([0, 1, 2, 3, 5, 4, 6, 7, 10, 9, 8, 11, 15, 14, 13, 12], dtype=np.int32).reshape((4, 4))
_x = Tensor(Dtype.I32, x.shape, x.flatten())
_y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reverse_sequence_i32_batch_equal_parts"
make_test(
[_x],
_y,
"input_0.reverse_sequence(TensorTrait::<usize>::new(array![4].span(), array![1,2,3,4].span()), Option::Some(0), Option::Some(1))",
name
)
def reverse_sequence_i32_time():
x = np.array([0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15], dtype=np.int32).reshape((4, 4))
y = np.array([3, 6, 9, 12, 2, 5, 8, 13, 1, 4, 10, 14, 0, 7, 11, 15], dtype=np.int32).reshape((4, 4))
_x = Tensor(Dtype.I32, x.shape, x.flatten())
_y = Tensor(Dtype.I32, y.shape, y.flatten())
name = "reverse_sequence_i32_time_equal_parts"
make_test |
(
[_x],
_y,
"input_0.reverse_sequence(TensorTrait::<usize>::new(array![4].span(), array![4,3,2,1].span()), Option::Some(1), Option::Some(0))",
name
)
reverse_sequence_i32_batch()
reverse_sequence_i32_time()
@staticmethod
def Reverse_sequence_i8():
def reverse_sequence_batch():
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], dtype=np.int8).reshape((4, 4))
y = np.array([0, 1, 2, 3, 5, 4, 6, 7, 10, 9, 8, 11, 15, 14, 13, 12], dtype=np.int8).reshape((4, 4))
_x = Tensor(Dtype.I8, x.shape, x.flatten())
_y = Tensor(Dtype.I8, y.shape, y.flatten())
name = "reverse_sequence_i8_batch_equal_parts"
make_test(
[_x],
_y,
"input_0.reverse_sequence(TensorTrait::<usize>::new(array![4].span(), array![1,2,3,4].span()), Option::Some(0), Option::Some(1))",
name
)
def reverse_sequence_time():
x = np.array([0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15], dtype=np.uint32).reshape((4, 4))
y = np.array([3, 6, 9, 12, 2, 5, 8, 13, 1, 4, 10, 14, 0, 7, 11, 15], dtype=np.uint32).reshape((4, 4))
_x = Tensor(Dtype.U32, x.shape, x.flatten())
_y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reverse_sequence_i8_time_equal_parts"
make_test(
[_x],
_y,
"input_0.reverse_sequence(TensorTrait::<usize>::new(array![4].span(), array![4,3,2,1].span()), Option::Some(1), Option::Some(0))",
name
)
reverse_sequence_batch()
reverse_sequence_time()
def Reverse_sequence_fp16x16():
def reverse_sequence_batch():
x = to_fp(np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], dtype=np.int64).reshape(4, 4), FixedImpl.FP16x16)
y = to_fp( |
np.array([0, 1, 2, 3, 5, 4, 6, 7, 10, 9, 8, 11, 15, 14, 13, 12], dtype=np.int64).reshape(4, 4), FixedImpl.FP16x16)
_x = Tensor(Dtype.FP16x16, x.shape, x.flatten())
_y = Tensor(Dtype.FP16x16, y.shape, y.flatten())
name = "reverse_sequence_fp16x16_batch_equal_parts"
make_test(
[_x],
_y, "input_0.reverse_sequence(TensorTrait::<usize>::new(array![4].span(), array![1,2,3,4].span()), Option::Some(0), Option::Some(1))",
name
)
def reverse_sequence_time():
x = to_fp(np.array([0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15], dtype=np.int64).reshape(4, 4), FixedImpl.FP16x16)
y = to_fp(np.array([3, 6, 9, 12, 2, 5, 8, 13, 1, 4, 10, 14, 0, 7, 11, 15], dtype=np.int64).reshape(4, 4), FixedImpl.FP16x16)
_x = Tensor(Dtype.FP16x16, x.shape, x.flatten())
_y = Tensor(Dtype.FP16x16, y.shape, y.flatten())
name = "reverse_sequence_fp16x16_time_equal_parts"
make_test(
[_x],
_y,
"input_0.reverse_sequence(TensorTrait::<usize>::new(array![4].span(), array![4,3,2,1].span()), Option::Some(1), Option::Some(0))",
name
)
reverse_sequence_batch()
reverse_sequence_time()
def reverse_sequence_different_dimensions():
def reverse_sequence_different_dimensions_4_5():
x = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20], dtype=np.uint32).reshape(4,5)
y = np.array([5,4,3,2,1,9,8,7,6,10,13,12,11,14,15,17,16,18,19,20], dtype=np.uint32).reshape(4,5)
_x = Tensor(Dtype.U32, x.shape, x.flatten())
_y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reverse_sequence_different_dimensions_4_5"
make_test(
[_x],
_y,
"input_0.reverse_sequence(TensorTrait::<usize>::new(array![4].span(), array![5,4,3,2].span()), Option::Some(0), Option::So |
me(1))",
name
)
def reverse_sequence_different_dimensions_2_4():
x = np.array([1,2,3,4,5,6,7,8], dtype=np.uint32).reshape(2,4)
y = np.array([5,6,7,8,1,2,3,4], dtype=np.uint32).reshape(2,4)
_x = Tensor(Dtype.U32, x.shape, x.flatten())
_y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reverse_sequence_different_dimensions_2_4"
make_test(
[_x],
_y,
"input_0.reverse_sequence(TensorTrait::<usize>::new(array![4].span(), array![2,2,2,2].span()), Option::Some(1), Option::Some(0))",
name
)
def reverse_sequence_different_dimensions_1_6():
x = np.array([0,1,2,3,4,5], dtype=np.uint32).reshape(1,6)
y = np.array([4,3,2,1,0,5], dtype=np.uint32).reshape(1,6)
_x = Tensor(Dtype.U32, x.shape, x.flatten())
_y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reverse_sequence_different_dimensions_1_6"
make_test(
[_x],
_y,
"input_0.reverse_sequence(TensorTrait::<usize>::new(array![1].span(), array![5].span()), Option::Some(0), Option::Some(1))",
name
)
def reverse_sequence_different_dimensions_3x9_batch():
x = np.array([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], dtype=np.uint32).reshape(3,9)
y = np.array([6,5,4,3,2,1,0,7,8,16,15,14,13,12,11,10,9,17,26,25,24,23,22,21,20,19,18], dtype=np.uint32).reshape(3,9)
_x = Tensor(Dtype.U32, x.shape, x.flatten())
_y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reverse_sequence_different_dimensions_3x9_batch"
make_test(
[_x],
_y,
"input_0.reverse_sequence(TensorTrait::<usize>::new(array![3].span(), array![7,8,9].span()), Option::Some(0), Option::Some(1))", |
name
)
def reverse_sequence_different_dimensions_3x9_time():
x = np.array([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], dtype=np.uint32).reshape(3,9)
y = np.array([18,10,20,12,22,14,24,16,8,9,1,11,3,13,5,15,7,17,0,19,2,21,4,23,6,25,26], dtype=np.uint32).reshape(3,9)
_x = Tensor(Dtype.U32, x.shape, x.flatten())
_y = Tensor(Dtype.U32, y.shape, y.flatten())
name = "reverse_sequence_different_dimensions_3x9_time"
make_test(
[_x],
_y,
"input_0.reverse_sequence(TensorTrait::<usize>::new(array![9].span(), array![3,2,3,2,3,2,3,2,1].span()), Option::Some(1), Option::Some(0))",
name
)
reverse_sequence_different_dimensions_4_5()
reverse_sequence_different_dimensions_2_4()
reverse_sequence_different_dimensions_1_6()
reverse_sequence_different_dimensions_3x9_batch()
reverse_sequence_different_dimensions_3x9_time() |
import numpy as np
from nodegen.node import RunAll
from ..helpers import make_test, to_fp, Tensor, Dtype, FixedImpl
class Round(RunAll):
@staticmethod
def round_fp8x23():
x = np.array([0.1, 0.5, 0.9, 1.2, 1.5, 1.8, 2.3, 2.5, 2.7, -1.1, -1.5, -1.9, -2.2, -2.5, -2.8]).astype(np.float64)
y = np.array([0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, -1.0, -2.0, -2.0, -2.0, -3.0, -3.0]).astype(np.float64)
x = Tensor(Dtype.FP8x23, x.shape, to_fp(x.flatten(), FixedImpl.FP8x23))
y = Tensor(Dtype.FP8x23, y.shape, to_fp(y.flatten(), FixedImpl.FP8x23))
name = "round_fp8x23"
make_test([x], y, "input_0.round()", name)
@staticmethod
def round_fp16x16():
x = np.array([0.1, 0.5, 0.9, 1.2, 1.5, 1.8, 2.3, 2.5, 2.7, -1.1, -1.5, -1.9, -2.2, -2.5, -2.8]).astype(np.float64)
y = np.array([0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, -1.0, -2.0, -2.0, -2.0, -3.0, -3.0]).astype(np.float64)
x = Tensor(Dtype.FP16x16, x.shape, to_fp(x.flatten(), FixedImpl.FP16x16))
y = Tensor(Dtype.FP16x16, y.shape, to_fp(y.flatten(), FixedImpl.FP16x16))
name = "round_fp16x16"
make_test([x], y, "input_0.round()", name)
|
import os
import glob
# Directory path where Python files/modules are located
directory_path = 'nodegen/node/'
# Get all files in the directory
all_files = os.listdir(directory_path)
# Filter Python files using glob and '*.py' pattern
python_files = [file[:-3] for file in all_files if file.endswith('.py')]
fixed = [
'abs',
'argmax',
'argmin',
'concat',
'cumsum',
'div',
'equal',
'less_equal',
'greater',
'linear',
'matmul',
'mul',
'or',
'reduce_sum',
'sub',
'transpose',
'xor',
'less',
'greater_equal',
'slice',
'gather',
'nonzero',
'squeeze',
'unsqueeze',
'sign',
'clip',
'__init__',
'running'
]
for node in python_files:
if node not in fixed:
current_dir = os.getcwd()
command = f'python nodegen/node/__init__.py {node}'
os.system(command) |
import numpy as np
from nodegen.node |
import RunAll
from ..helpers |
import make_test, to_fp, Tensor, Dtype, FixedImpl, Trait
def scatter_elements(data, indices, updates, axis=0, reduction="none"):
if axis < 0:
axis = data.ndim + axis
idx_xsection_shape = indices.shape[:axis] + indices.shape[axis + 1 :]
def make_slice(arr, axis, i):
slc = [slice(None)] * arr.ndim
slc[axis] = i
return slc
def unpack(packed):
unpacked = packed[0]
for i in range(1, len(packed)):
unpacked = unpacked, packed[i]
return unpacked
def make_indices_for_duplicate(idx):
final_idx = []
for i in range(len(idx[0])):
final_idx.append(tuple(idx_element[i] for idx_element in idx))
return list(final_idx)
idx = [
[
unpack(np.indices(idx_xsection_shape).reshape(indices.ndim - 1, -1)),
indices[tuple(make_slice(indices, axis, i))].reshape(1, -1)[0],
]
for i in range(indices.shape[axis])
]
idx = list(np.concatenate(idx, axis=1))
idx.insert(axis, idx.pop())
updates_idx = list(idx)
updates_idx.pop(axis)
updates_idx.insert(
axis, np.repeat(np.arange(indices.shape[axis]), np.prod(idx_xsection_shape))
)
scattered = np.copy(data)
if reduction == "none":
scattered[tuple(idx)] = updates[tuple(updates_idx)]
else:
idx, updates_idx = make_indices_for_duplicate(idx), make_indices_for_duplicate(
updates_idx
)
for iter, idx_set in enumerate(idx):
if reduction == "add":
scattered[idx_set] += updates[updates_idx[iter]]
elif reduction == "mul":
scattered[idx_set] *= updates[updates_idx[iter]]
elif reduction == "max":
scattered[idx_set] = np.maximum(
scattered[idx_set], updates[updates_idx[iter]]
)
elif reduction == "min":
scattered[idx_set] = np.minimum(
scattered[idx_set], upda |
tes[updates_idx[iter]]
)
return scattered |
class Scatter(RunAll):
@staticmethod
def scatter_fp16x16():
def scatter():
def default():
x1 = np.zeros((3, 3)).astype(np.int64)
x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int64)
x3 = np.array(
[[0,1,2],
[2,0,1],
[1,0,1]],
)
y = scatter_elements(x1, x3, x2, 0, 'none')
x1 = Tensor(Dtype.FP16x16, x1.shape, to_fp(x1.flatten(), FixedImpl.FP16x16))
x2 = Tensor(Dtype.FP16x16, x2.shape, to_fp(x2.flatten(), FixedImpl.FP16x16))
x3 = Tensor(Dtype.U32, x3.shape, x3.flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(
y.flatten(), FixedImpl.FP16x16))
name = "scatter_fp16x16_3d_default"
make_test(
inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('none'))",
name= name)
def axis_1():
x1 = np.zeros((3, 3)).astype(np.int64)
x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int64)
x3 = np.array(
[[0,1,2],
[2,0,1],
[1,0,1]],
)
y = scatter_elements(x1, x3, x2, 1, 'none')
x1 = Tensor(Dtype.FP16x16, x1.shape, to_fp(x1.flatten(), FixedImpl.FP16x16))
x2 = Tensor(Dtype.FP16x16, x2.shape, to_fp(x2.flatten(), FixedImpl.FP16x16))
x3 = Tensor(Dtype.U32, x3.shape, x3.flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(
y.flatten(), FixedImpl.FP16x16))
name = "scatter_fp16x16_3d_axis1"
make_test(
inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, ind |
ices:input_2, axis:Option::Some(1), reduction:Option::Some('none'))",
name= name)
def axis_1_add():
x1 = np.zeros((3, 3)).astype(np.int64)
x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int64)
x3 = np.array(
[[0,1,2],
[2,0,1],
[1,0,1]],
)
y = scatter_elements(x1, x3, x2, 1, 'add')
x1 = Tensor(Dtype.FP16x16, x1.shape, to_fp(x1.flatten(), FixedImpl.FP16x16))
x2 = Tensor(Dtype.FP16x16, x2.shape, to_fp(x2.flatten(), FixedImpl.FP16x16))
x3 = Tensor(Dtype.U32, x3.shape, x3.flatten())
y = Tensor(Dtype.FP16x16, y.shape, to_fp(
y.flatten(), FixedImpl.FP16x16))
name = "scatter_fp16x16_3d_axis1_add"
make_test(
inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('add'))",
name= name)
default()
axis_1()
axis_1_add()
scatter()
@staticmethod
def scatter_fp8x23():
def scatter():
def default():
x1 = np.zeros((3, 3)).astype(np.int64)
x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int64)
x3 = np.array(
[[0,1,2],
[2,0,1],
[1,0,1]],
)
y = scatter_elements(x1, x3, x2, 0, 'none')
x1 = Tensor(Dtype.FP8x23, x1.shape, to_fp(x1.flatten(), FixedImpl.FP8x23))
x2 = Tensor(Dtype.FP8x23, x2.shape, to_fp(x2.flatten(), FixedImpl.FP8x23))
x3 = Tensor(Dtype.U32, x3.shape, x3.flatten())
y = Tensor(Dtype.FP8x23, y.shape, to_fp(y.flatten(), FixedImpl.FP8x23))
n |
ame = "scatter_fp8x23_default"
make_test(
inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('none'))",
name= name)
def axis1():
x1 = np.zeros((3, 3)).astype(np.int64)
x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int64)
x3 = np.array(
[[0,1,2],
[2,0,1],
[1,0,1]],
)
y = scatter_elements(x1, x3, x2, 1, 'none')
x1 = Tensor(Dtype.FP8x23, x1.shape, to_fp(x1.flatten(), FixedImpl.FP8x23))
x2 = Tensor(Dtype.FP8x23, x2.shape, to_fp(x2.flatten(), FixedImpl.FP8x23))
x3 = Tensor(Dtype.U32, x3.shape, x3.flatten())
y = Tensor(Dtype.FP8x23, y.shape, to_fp(y.flatten(), FixedImpl.FP8x23))
name = "scatter_fp8x23_axis1"
make_test(
inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(1), reduction:Option::Some('none'))",
name= name)
def axis1_mul():
x1 = np.zeros((3, 3)).astype(np.int64)
x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int64)
x3 = np.array(
[[0,1,2],
[2,0,1],
[1,0,1]],
)
y = scatter_elements(x1, x3, x2, 0, 'mul')
x1 = Tensor(Dtype.FP8x23, x1.shape, to_fp(x1.flatten(), FixedImpl.FP8x23))
x2 = Tensor(Dtype.FP8x23, x2.shape, to_fp(x2.flatten(), FixedImpl.FP8x23))
x3 = Tensor(Dtype.U32, x3.shape, x3.flatten())
y = Tensor(Dtype.FP8x23, y.shape, to_fp(y.flatten(), FixedImpl.FP8x23))
name = "scatter_fp8x23_mul" |
make_test(
inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('mul'))",
name= name)
default()
axis1()
axis1_mul()
scatter()
@staticmethod
def scatter_i8():
def scatter_3D():
def default():
x1 = np.zeros((3, 3)).astype(np.int8)
x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int8)
x3 = np.array(
[[0,1,2],
[2,0,1],
[1,0,1]],
)
y = scatter_elements(x1, x3, x2, 0, 'none')
x1 = Tensor(Dtype.I8, x1.shape, x1.flatten())
x2 = Tensor(Dtype.I8, x2.shape, x2.flatten())
x3 = Tensor(Dtype.U32, x3.shape, x3.flatten())
y = Tensor(Dtype.I8, y.shape, y.flatten())
name = "scatter_i8_default"
make_test(
inputs = [x1, x2, x3], output = y, func_sig = "input_0.scatter(updates:input_1, indices:input_2, axis:Option::Some(0), reduction:Option::Some('none'))",
name= name)
def axis1():
x1 = np.zeros((3, 3)).astype(np.int8)
x2 = np.arange(1, 10).reshape((3, 3)).astype(np.int8)
x3 = np.array(
[[0,1,2],
[2,0,1],
[1,0,1]],
)
y = scatter_elements(x1, x3, x2, 1, 'none')
x1 = Tensor(Dtype.I8, x1.shape, x1.flatten())
x2 = Tensor(Dtype.I8, x2.shape, x2.flatten())
x3 = Tensor(Dtype.U32, x3.shape, x3.flatten())
y = Tensor(Dtype.I8, y.shape, y.flatten())
name = "scatter_i8_axis1"
make_test(
inputs = [x1, x2, x3], |