mindspore.profiler.mstx

View Source On Gitee
class mindspore.profiler.mstx

Mstx class provides profiling tools for marking and tracing on NPU. This class provides three static methods: mark, range_start and range_end for adding marker points and ranges in profiling.

static mark(message: str, stream: mindspore.runtime.Stream = None)

Add a marker point in profiling.

Parameters
  • message (str) – Description for the marker.

  • stream (Stream, optional) – NPU stream for async execution, expected type: mindspore.runtime.Stream. Default: None, which means only marking on host side without marking on device stream.

Examples

>>> 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)

End a profiling range.

Parameters

range_id (int) – Range ID from range_start.

Examples

>>> # 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)

Start a profiling range.

Parameters
  • message (str) – Description for the range.

  • stream (Stream, optional) – NPU stream for async execution, expected type: mindspore.runtime.Stream. Default: None, which means only starting mstx range on host side without starting on device stream.

Returns

int, range ID for range_end.

Examples

>>> 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()