mindspore.Profiler

class mindspore.Profiler(**kwargs)[source]

This class to enable the profiling of MindSpore neural networks. MindSpore users can import the mindspore.Profiler, initialize the Profiler object to start profiling, and use Profiler.analyse() to stop profiling and analyse the results. Users can visualize the results using the MindInsight tool. Now, Profiler supports AICORE operator, AICPU operator, HostCPU operator, memory, correspondence, cluster, etc data analysis.

Parameters
  • output_path (str, optional) – Output data path. Default: “./data”.

  • profile_communication (bool, optional) – (Ascend only) Whether to collect communication performance data in a multi devices training,collect when True. Setting this parameter has no effect during single device training. Default: False.

  • profile_memory (bool, optional) – (Ascend only) Whether to collect tensor memory data, collect when True. Default: False.

  • start_profile (bool, optional) – The start_profile parameter controls whether to enable or disable performance data collection based on conditions. Default: True.

Raises

RuntimeError – When the version of CANN does not match the version of MindSpore, MindSpore cannot parse the generated ascend_job_id directory structure.

Supported Platforms:

Ascend GPU

Examples

>>> import numpy as np
>>> import mindspore as ms
>>> from mindspore import nn
>>> import mindspore.dataset as ds
>>> from mindspore import Profiler
>>>
>>>
>>> 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):
...     optimizer = nn.Momentum(net.trainable_params(), 1, 0.9)
...     loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
...     data = ds.GeneratorDataset(generator, ["data", "label"])
...     model = ms.Model(net, loss, optimizer)
...     model.train(1, data)
>>>
>>> if __name__ == '__main__':
...     # If the device_target is GPU, set the device_target to "GPU"
...     ms.set_context(mode=ms.GRAPH_MODE, device_target="Ascend")
...
...     # Init Profiler
...     # Note that the Profiler should be initialized before model.train
...     profiler = Profiler()
...
...     # Train Model
...     net = Net()
...     train(net)
...
...     # Profiler end
...     profiler.analyse()
analyse()[source]

Collect and analyze training performance data, support calls during and after training. The example shows above.

op_analyse(op_name, device_id=None)[source]

Profiler users can use this interface to obtain operator performance data.

Parameters
  • op_name (str or list) – The primitive operator name to query.

  • device_id (int, optional) – ID of the target device. This parameter is optional during network training or inference, and users can use device_id parameter to specify which card operator performance data to parse. If this interface is used for offline data parsing, Default: 0.

Raises
  • TypeError – If the op_name parameter type is incorrect.

  • TypeError – If the device_id parameter type is incorrect.

  • RunTimeError – If MindSpore runs on Ascend, this interface cannot be used.

Supported Platforms:

GPU CPU

Examples

>>> from mindspore import Profiler
>>>
...     # Profiler init.
...     profiler = Profiler()
...
...     # Train Model or eval Model.
...     net = Net()
...     train(net)
...
...     # Profiler end
...     profiler.analyse()
...
...     profiler.op_analyse(op_name=["BiasAdd", "Conv2D"])
...
>>> from mindspore import Profiler
>>>
...     # Profiler init.
...     profiler = Profiler(output_path="my_profiler_path")
...     profiler.op_analyse(op_name="Conv2D")
start()[source]

Used for Ascend, GPU, start profiling. Profiling can be turned on based on step and epoch.

Raises
  • RuntimeError – If the profiler has already started.

  • RuntimeError – If MD profiling has stopped, repeated start action is not supported.

  • RuntimeError – If the start_profile parameter is not set or is set to True.

Examples

>>> class StopAtStep(Callback):
>>>     def __init__(self, start_step, stop_step):
...         super(StopAtStep, self).__init__()
...         self.start_step = start_step
...         self.stop_step = stop_step
...         self.profiler = Profiler(start_profile=False)
...
>>>     def step_begin(self, run_context):
...         cb_params = run_context.original_args()
...         step_num = cb_params.cur_step_num
...         if step_num == self.start_step:
...             self.profiler.start()
...
>>>     def step_end(self, run_context):
...         cb_params = run_context.original_args()
...         step_num = cb_params.cur_step_num
...         if step_num == self.stop_step:
...             self.profiler.stop()
...
>>>     def end(self, run_context):
...         self.profiler.analyse()
stop()[source]

Used for Ascend, GPU, stop profiling. Profiling can be turned off based on step and epoch.

Raises

RuntimeError – If the profiler has not started, this function is disabled.

Examples

>>> class StopAtEpoch(Callback):
>>>     def __init__(self, start_epoch, stop_epoch):
...         super(StopAtEpoch, self).__init__()
...         self.start_epoch = start_epoch
...         self.stop_epoch = stop_epoch
...         self.profiler = Profiler(start_profile=False)
...
>>>     def epoch_begin(self, run_context):
...         cb_params = run_context.original_args()
...         epoch_num = cb_params.cur_epoch_num
...         if epoch_num == self.start_epoch:
...             self.profiler.start()
...
>>>     def epoch_end(self, run_context):
...         cb_params = run_context.original_args()
...         epoch_num = cb_params.cur_epoch_num
...         if epoch_num == self.stop_epoch:
...             self.profiler.stop()
...
>>>     def end(self, run_context):
...         self.profiler.analyse()