问题描述:
我用的版本:MindSpore 1.1.1-Ascend
我开发了几个自定义算子(npu的),想要单独测一下每个算子的性能,要怎么做?
我知道可以在模型训练中用Profiler记录训练过程中的性能情况,包括算子的执行耗时;但我想自己设计一些测试用例,特定的shape的输入来测试单算子的性能。
我能想到用python的time.time()来记录算子执行前后的时间戳然后相减,但这样得出的时间应该很不准确?因为会包括编译之类的时间。
解答:
单算子性能也可以通过Profiler获得
import numpy as np
from mindspore import context, Tensor, Parameter
from mindspore.nn import Cellimport mindspore.ops as ops
from mindspore.profiler.profiling import Profiler
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
class Net(Cell):
def __init__(self, out_channel, kernel_size, shape1, shape2, shape3):
super(Net, self).__init__()
self.shape3 = shape3
self.conv_input = ops.Conv2DBackpropInput(out_channel,
kernel_size,
pad_mode="valid",
pad=0,
mode=1,
stride=1,
dilation=1,
group=1)
self.w = Parameter(Tensor(np.random.normal(0, 1, shape2).astype(np.float32)), name='w')
self.out = Parameter(Tensor(np.random.normal(0, 1, shape1).astype(np.float32)), name='y')
def construct(self):
return self.conv_input(self.out, self.w, self.shape3)
if __name__ == "__main__":
profiler = Profiler()
net = Net(256, (3, 256), (64, 256, 102, 1), (256, 1, 3, 256), (64, 1, 104, 256))
output = net()
profiler.analyse()
可以参考这样写