mindspore.profiler.mstx
- class mindspore.profiler.mstx
该类可在NPU上进行性能分析的标记和跟踪。该类提供了mark、range_start和range_end三个静态方法,用于在性能分析中添加标记点和区间。
- static mark(message: str, stream: mindspore.runtime.Stream = None)
在性能分析中添加标记点。
- 参数:
样例:
>>> import numpy as np >>> import mindspore as ms >>> from mindspore import nn >>> import mindspore.dataset as ds >>> from mindspore import Profiler >>> from mindspore.profiler import ProfilerLevel, ProfilerActivity, schedule, tensor_board_trace_handler >>> from mindspore.profiler import mstx >>> >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.fc = nn.Dense(2,2) ... def construct(self, x): ... return self.fc(x) >>> >>> def generator(): ... for i in range(2): ... yield (np.ones([2, 2]).astype(np.float32), np.ones([2]).astype(np.int32)) >>> >>> def train(net): ... stream = ms.runtime.current_stream() ... optimizer = nn.Momentum(net.trainable_params(), 1, 0.9) ... loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True) ... data = ds.GeneratorDataset(generator, ["data", "label"]) ... model = ms.train.Model(net, loss, optimizer) ... # Add marker before training ... mstx.mark("train start", stream) ... model.train(1, data) ... # Add marker after training ... mstx.mark("train end", stream) >>> >>> if __name__ == '__main__': ... # Note: mstx only supports Ascend device and cannot be used in mindspore.nn.Cell.construct ... # when in mindspore.GRAPH_MODE ... ms.set_context(mode=ms.PYNATIVE_MODE) ... ms.set_device(device_target="Ascend", device_id=0) ... # Init Profiler ... with Profiler(profiler_level=ProfilerLevel.LevelNone, ... on_trace_ready=tensor_board_trace_handler, ... activities=[ProfilerActivity.CPU, ProfilerActivity.NPU], ... schedule=schedule(wait=0, warmup=0, active=3, repeat=1, skip_first=0), ... mstx=True) as profiler: ... net = Net() ... for i in range(5): ... train(net) ... profiler.step()
- static range_end(range_id: int)
结束一个性能分析区间。
- 参数:
range_id (int) - 从range_start返回的区间ID。
样例:
>>> # Please refer to the example in range_start >>> # range_id = mstx.range_start("training process", stream) >>> # model.train(1, data) >>> # mstx.range_end(range_id)
- static range_start(message: str, stream: mindspore.runtime.Stream = None)
开始一个性能分析区间。
- 参数:
- 返回:
int,区间ID,用于range_end方法。
样例:
>>> import numpy as np >>> import mindspore as ms >>> from mindspore import nn >>> import mindspore.dataset as ds >>> from mindspore import Profiler >>> from mindspore.profiler import ProfilerLevel, ProfilerActivity, schedule, tensor_board_trace_handler >>> from mindspore.profiler import mstx >>> >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.fc = nn.Dense(2,2) ... def construct(self, x): ... return self.fc(x) >>> >>> def generator(): ... for i in range(2): ... yield (np.ones([2, 2]).astype(np.float32), np.ones([2]).astype(np.int32)) >>> >>> def train(net): ... stream = ms.runtime.current_stream() ... optimizer = nn.Momentum(net.trainable_params(), 1, 0.9) ... loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True) ... data = ds.GeneratorDataset(generator, ["data", "label"]) ... model = ms.train.Model(net, loss, optimizer) ... # Start profiling range ... range_id = mstx.range_start("training process", stream) ... model.train(1, data) ... # End profiling range ... mstx.range_end(range_id) >>> >>> if __name__ == '__main__': ... # Note: mstx only supports Ascend device and cannot be used in mindspore.nn.Cell.construct ... # when in mindspore.GRAPH_MODE ... ms.set_context(mode=ms.PYNATIVE_MODE) ... ms.set_device(device_target="Ascend", device_id=0) ... with Profiler(profiler_level=ProfilerLevel.LevelNone, ... on_trace_ready=tensor_board_trace_handler, ... activities=[ProfilerActivity.CPU, ProfilerActivity.NPU], ... schedule=schedule(wait=0, warmup=0, active=3, repeat=1, skip_first=0), ... mstx=True) as profiler: ... net = Net() ... for i in range(5): ... train(net) ... profiler.step()