mindspore.profiler.schedule

查看源文件
class mindspore.profiler.schedule(wait, active, warm_up=0, repeat=0, skip_first=0)

该类用于获取每一步的操作。

调度如下:

(NONE)        (NONE)          (NONE)       (WARM_UP)       (RECORD)      (RECORD)     (RECORD_AND_SAVE)    None
START------->skip_first------->wait-------->warm_up-------->active........active.........active----------->stop
                              |                                                             |
                              |                           repeat_1                          |
                              ---------------------------------------------------------------

Profiler将跳过前 skip_first 步,然后等待 wait 步,接着在接下来的 warm_up 步中进行预热,然后在接下来的 active 步中进行活动记录,然后从 wait 步开始重复循环。可选的循环次数由 repeat 参数指定, repeat 值为0表示循环将继续直到分析完成。

参数:
  • wait (int) - 预热阶段等待的步数。

  • active (int) - 活动阶段执行的步数。

  • warm_up (int, 可选) - 预热阶段执行的步数。默认值: 0

  • repeat (int, 可选) - 重复次数。默认值: 0

  • skip_first (int, 可选) - 跳过第一个步数。默认值: 0

异常:
  • ValueError - 参数step必须大于等于0。

支持平台:

Ascend

样例:

>>> import numpy as np
>>> import mindspore as ms
>>> import mindspore.dataset as ds
>>> from mindspore import context, nn, Profiler
>>> from mindspore.profiler import schedule, tensor_board_trace_handler
>>>
>>> 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_net():
...     for _ in range(2):
...         yield np.ones([2, 2]).astype(np.float32), np.ones([2]).astype(np.int32)
>>>
>>> def train(test_net):
...     optimizer = nn.Momentum(test_net.trainable_params(), 1, 0.9)
...     loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
...     data = ds.GeneratorDataset(generator_net(), ["data", "label"])
...     model = ms.train.Model(test_net, loss, optimizer)
...     model.train(1, data)
>>>
>>> if __name__ == '__main__':
...     context.set_context(mode=ms.PYNATIVE_MODE, device_target="Ascend")
...
...     net = Net()
...     STEP_NUM = 15
...
...     with Profiler(schedule=schedule(wait=1, warm_up=1, active=2, repeat=1, skip_first=2),
...                   on_trace_ready=tensor_board_trace_handler) as prof:
...         for i in range(STEP_NUM):
...             train(net)
...             prof.step()
to_dict()

将schedule类转换为一个字典。

返回:

字典,schedule类的参数与对应的值。