1、输入一个Tensor,赋值给ub之后再从ub赋值出来形成输出。在这两次赋值中间,多次操作了另一个不相干的Tensor,会影响到之前Tensor的输出结果。
2、文件中MAX_ITER = 1000000的时候,输出全为0
3、文件中MAX_ITER = 1的时候,输出正确
【截图信息】
错误
正确
代码:
import numpy as np
import mindspore as ms
import te
import topi
import te.tik
hfe = lambda x,y,eps=1e-2:np.max(np.abs(x-y)/(np.abs(x)+np.abs(y)+eps))
ms.context.set_context(mode=ms.context.GRAPH_MODE, device_target="Ascend")
cus_S0_op_info = ms.ops.TBERegOp("CusS0") \
.fusion_type("OPAQUE") \
.async_flag(False) \
.binfile_name("S0.so") \
.compute_cost(10) \
.kernel_name("CusS0Impl") \
.partial_flag(True) \
.input(0, "input1", False, "required", "all") \
.output(0, "output", False, "required", "all") \
.dtype_format(ms.ops.DataType.F32_Default, ms.ops.DataType.F32_Default) \
.dtype_format(ms.ops.DataType.F16_Default, ms.ops.DataType.F16_Default) \
.get_op_info()
@ms.ops.op_info_register(cus_S0_op_info)
def CusS0Impl(input1, output, kernel_name="CusS0Impl"):
tik_instance = te.tik.Tik()
input_shape = input1.get("shape")
M = input_shape[0]
N = input_shape[1]
bLength_N = ((N-1)//16)+1
bLength = ((M*N-1)//16)+1
MAX_ITER = 1000000
input1 = tik_instance.Tensor("float16", (M,N), name="input1", scope=te.tik.scope_gm)
output = tik_instance.Tensor("float16", (M,), name="output", scope=te.tik.scope_gm)
input1_ub = tik_instance.Tensor("float16", (M,N), name="input1_ub", scope=te.tik.scope_ubuf)
tik_instance.data_move(input1_ub, input1, 0, 1, bLength, 0, 0)
tmp_vector = tik_instance.Tensor("float16", (M,), name="tmp_vector", scope=te.tik.scope_ubuf)
repeat = tik_instance.Scalar('int32')
r = ((M-1)//128)+1
repeat.set_as(r)
with tik_instance.for_range(0,MAX_ITER) as i:
tik_instance.vec_dup(128, tmp_vector, 0.0, repeat, 8)
tik_instance.data_move(output, input1_ub, 0, 1, bLength_N, 0, 0)
tik_instance.BuildCCE(kernel_name=kernel_name,inputs=[input1],outputs=[output])
class CusS0(ms.ops.PrimitiveWithInfer):
@ms.ops.prim_attr_register
def __init__(self):
self.CusS0Impl = CusS0Impl
self.init_prim_io_names(inputs=['input1'], outputs=['output'])
def infer_shape(self, input1):
shape = [input1[1]]
return shape
def infer_dtype(self, input1):
return input1
if __name__=='__main__':
np0 = np.random.randn(128,128).astype(np.float16)
op = CusS0()
ret0 = op(ms.Tensor(np0)).asnumpy()
print(np0)
print(ret0)
完全看不出问题,只是怀疑两个scalar可能有坑。
repeat = tik_instance.Scalar('int32')
r = ((M-1)//128)+1
repeat.set_as(r)
with tik_instance.for_range(0,MAX_ITER) as i:
tik_instance.vec_dup(128, tmp_vector, 0.0, repeat, 8)
变成如下代码试试看:
repeat = tik_instance.Scalar('int32')
repeat.set_as(1000000)
with tik_instance.for_range(0,repeat) as i:
tik_instance.vec_dup(128, tmp_vector, 0.0, 1, 8)