models icon indicating copy to clipboard operation
models copied to clipboard

Profiling results for sync data reader of DeepASR

Open pkuyym opened this issue 8 years ago • 9 comments

测试采用实际音频数据,共run了10个batch,batch size=32,去掉异步和buffer,结论如下:

  1. 最主要热点是trans add delta,这个占用了超过80%的时间
  2. feeding操作耗时很少,可以暂不考虑优化

主函数耗时分析:

  1. 热点是data processing
  2. feeding操作耗时很少,可以排除掉这个风险
Total time: 109.838 s
File: profile_sync_reader.py
Function: profile_reader at line 33

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    33                                           @profile
    34                                           def profile_reader(epoch_num, batch_size):
    35         2         10.0      5.0      0.0      for epoch_id in xrange(epoch_num):
    36         1          4.0      4.0      0.0          for batch_id, one_batch in enumerate(
    37        11   95392189.0 8672017.2     86.8                  data_reader.batch_iterator(batch_size, batch_size)):
    38        11      46440.0   4221.8      0.0              (bat_feature, bat_label, lod) = one_batch
    39        11      83802.0   7618.4      0.1              res_feature.set(bat_feature, place)
    40        11        452.0     41.1      0.0              res_feature.set_lod([lod])
    41        11       1048.0     95.3      0.0              res_label.set(bat_label, place)
    42        11         90.0      8.2      0.0              res_label.set_lod([lod])
    43        11   14309543.0 1300867.5     13.0              time.sleep(1.3)
    44        11         61.0      5.5      0.0              if batch_id > 9:
    45         1       4296.0   4296.0      0.0                  break

pkuyym avatar Feb 28 '18 09:02 pkuyym

这是batch iterator耗时分析:

  1. 热点是数据I/O + augmentation
Total time: 95.39 s
File: data_utils/sync_reader.py
Function: batch_iterator at line 308

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   308                                               @profile
   309                                               def batch_iterator(self, batch_size, minimum_batch_size):
   310         1          1.0      1.0      0.0          def batch_to_ndarray(batch_samples, lod):
   311                                                       assert len(batch_samples)
   312                                                       frame_dim = batch_samples[0][0].shape[1]
   313                                                       batch_feature = np.zeros((lod[-1], frame_dim), dtype="float32")
   314                                                       batch_label = np.zeros((lod[-1], 1), dtype="int64")
   315                                                       start = 0
   316                                                       for sample in batch_samples:
   317                                                           frame_num = sample[0].shape[0]
   318                                                           batch_feature[start:start + frame_num, :] = sample[0]
   319                                                           batch_label[start:start + frame_num, :] = sample[1]
   320                                                           start += frame_num
   321                                                       return (batch_feature, batch_label)
   322
   323         1          1.0      1.0      0.0          batch_samples = []
   324         1          1.0      1.0      0.0          lod = [0]
   325       352   95037376.0 269992.5     99.6          for sample in self._sample_generator():
   326       352        948.0      2.7      0.0              batch_samples.append(sample)
   327       352        836.0      2.4      0.0              lod.append(lod[-1] + sample[0].shape[0])
   328       352        635.0      1.8      0.0              if len(batch_samples) == batch_size:
   329        11          9.0      0.8      0.0                  (batch_feature, batch_label) = batch_to_ndarray(
   330        11     349225.0  31747.7      0.4                      batch_samples, lod)
   331        11         31.0      2.8      0.0                  yield (batch_feature, batch_label, lod)
   332        10        912.0     91.2      0.0                  batch_samples = []
   333        10         24.0      2.4      0.0                  lod = [0]
   334
   335                                                   if len(batch_samples) >= minimum_batch_size:
   336                                                       (batch_feature, batch_label) = batch_to_ndarray(batch_samples,
   337                                                                                                       lod)
   338                                                       yield (batch_feature, batch_label, lod)

pkuyym avatar Feb 28 '18 09:02 pkuyym

这是单条样本预处理耗时分析:

  1. 热点为augmentation
  2. I/O耗时很少
Total time: 95.0125 s
File: data_utils/sync_reader.py
Function: _sample_generator at line 249

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   249                                               @profile
   250                                               def _sample_generator(self):
   251         1          4.0      4.0      0.0          def read_bytes(fpath, start, size):
   252                                                       try:
   253                                                           f = open(fpath, 'r')
   254                                                           f.seek(start, 0)
   255                                                           binary_bytes = f.read(size)
   256                                                           f.close()
   257                                                           return binary_bytes
   258                                                       except Exception as e:
   259                                                           raise CriticalException(e)
   260
   261         1          3.0      3.0      0.0          for sample_info_bucket in self._bucket_list:
   262         1      78510.0  78510.0      0.1              sample_info_list = sample_info_bucket.generate_sample_info_list()
   263         1       4222.0   4222.0      0.0              self._rng.shuffle(sample_info_list)  # do shuffle here
   264       352       2146.0      6.1      0.0              for sample_info in sample_info_list:
   265       352       1955.0      5.6      0.0                  feature_bytes = read_bytes(sample_info.feature_bin_path,
   266       352       1191.0      3.4      0.0                                             sample_info.feature_start,
   267       352      31686.0     90.0      0.0                                             sample_info.feature_size)
   268
   269       352        815.0      2.3      0.0                  assert sample_info.feature_frame_num * sample_info.feature_dim * 4 \
   270       352        642.0      1.8      0.0                          == len(feature_bytes), \
   271                                                                   (sample_info.feature_bin_path,
   272                                                                    sample_info.feature_frame_num,
   273                                                                    sample_info.feature_dim,
   274                                                                    len(feature_bytes))
   275
   276       352        467.0      1.3      0.0                  label_bytes = read_bytes(sample_info.label_bin_path,
   277       352        440.0      1.2      0.0                                           sample_info.label_start,
   278       352       9199.0     26.1      0.0                                           sample_info.label_size)
   279
   280       352        689.0      2.0      0.0                  assert sample_info.label_frame_num * 4 == len(label_bytes), (
   281                                                               sample_info.label_bin_path, sample_info.label_array,
   282                                                               len(label_bytes))
   283
   284       352       1499.0      4.3      0.0                  label_array = struct.unpack('I' * sample_info.label_frame_num,
   285       352      13455.0     38.2      0.0                                              label_bytes)
   286       352        601.0      1.7      0.0                  label_data = np.array(
   287       352      10761.0     30.6      0.0                      label_array, dtype='int64').reshape(
   288       352       1092.0      3.1      0.0                          (sample_info.label_frame_num, 1))
   289
   290       352        473.0      1.3      0.0                  feature_frame_num = sample_info.feature_frame_num
   291       352        434.0      1.2      0.0                  feature_dim = sample_info.feature_dim
   292       352        565.0      1.6      0.0                  assert feature_frame_num * feature_dim * 4 == len(feature_bytes)
   293       352        690.0      2.0      0.0                  feature_array = struct.unpack('f' * feature_frame_num *
   294       352     402072.0   1142.2      0.4                                                feature_dim, feature_bytes)
   295       352        616.0      1.8      0.0                  feature_data = np.array(
   296       352     122821.0    348.9      0.1                      feature_array, dtype='float32').reshape((
   297       352       1326.0      3.8      0.0                          sample_info.feature_frame_num, sample_info.feature_dim))
   298
   299       352        439.0      1.2      0.0                  sample_data = (feature_data, label_data)
   300      1408       2663.0      1.9      0.0                  for transformer in self._transformers:
   301                                                               # @TODO(pkuyym) to make transfomer only accept feature_data
   302      1056   94319620.0  89317.8     99.3                      sample_data = transformer.perform_trans(sample_data)
   303
   304                                                           # drop long sentence
   305       352       1037.0      2.9      0.0                  if self._drop_frame_len == -1 or self._drop_frame_len >= sample_data[0].shape[0]:
   306       352        398.0      1.1      0.0                      yield sample_data

pkuyym avatar Feb 28 '18 09:02 pkuyym

trans splice耗时很少

Total time: 0.506966 s
File: data_utils/augmentor/trans_splice.py
Function: perform_trans at line 28

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    28                                               @profile
    29                                               def perform_trans(self, sample):
    30                                                   """ copy feature context
    31                                                   Args:
    32                                                       sample(object): input sample(feature, label)
    33                                                   Return:
    34                                                       (feature, label)
    35                                                   """
    36       352        303.0      0.9      0.1          (feature, label) = sample
    37       352        375.0      1.1      0.1          nframe_num = feature.shape[0]
    38       352        271.0      0.8      0.1          nframe_dim = feature.shape[1]
    39       352        202.0      0.6      0.0          nnew_frame_dim = nframe_dim * (
    40       352        390.0      1.1      0.1              self._nleft_context + self._nright_context + 1)
    41       352        491.0      1.4      0.1          mat = np.zeros(
    42       352        309.0      0.9      0.1              (nframe_num + self._nleft_context + self._nright_context,
    43       352        232.0      0.7      0.0               nframe_dim),
    44       352      13645.0     38.8      2.7              dtype="float32")
    45       352      90143.0    256.1     17.8          ret = np.zeros((nframe_num, nnew_frame_dim), dtype="float32")
    46
    47                                                   #copy left
    48      2112       1900.0      0.9      0.4          for i in xrange(self._nleft_context):
    49      1760       3329.0      1.9      0.7              mat[i, :] = feature[0, :]
    50
    51                                                   #copy middle
    52                                                   mat[self._nleft_context:self._nleft_context +
    53       352       6919.0     19.7      1.4              nframe_num, :] = feature[:, :]
    54
    55                                                   #copy right
    56      2112       1703.0      0.8      0.3          for i in xrange(self._nright_context):
    57      1760       3122.0      1.8      0.6              mat[i + self._nleft_context + nframe_num, :] = feature[-1, :]
    58
    59       352       1372.0      3.9      0.3          mat = mat.reshape(mat.shape[0] * mat.shape[1])
    60       352        820.0      2.3      0.2          ret = ret.reshape(ret.shape[0] * ret.shape[1])
    61     94084      57871.0      0.6     11.4          for i in xrange(nframe_num):
    62     93732     103951.0      1.1     20.5              np.copyto(ret[i * nnew_frame_dim:(i + 1) * nnew_frame_dim],
    63     93732     218609.0      2.3     43.1                        mat[i * nframe_dim:i * nframe_dim + nnew_frame_dim])
    64       352        779.0      2.2      0.2          ret = ret.reshape((nframe_num, nnew_frame_dim))
    65       352        230.0      0.7      0.0          return (ret, label)

pkuyym avatar Feb 28 '18 09:02 pkuyym

trans mean variance norm耗时很少

Total time: 0.50597 s
File: data_utils/augmentor/trans_mean_variance_norm.py
Function: perform_trans at line 51

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    51                                               @profile
    52                                               def perform_trans(self, sample):
    53                                                   """ feature = (feature - mean) * var
    54                                                       Args:
    55                                                           sample(object):input sample, contain feature numpy and label numpy
    56                                                       Returns:
    57                                                           (feature, label)
    58                                                   """
    59       352        346.0      1.0      0.1          (feature, label) = sample
    60       352        272.0      0.8      0.1          shape = feature.shape
    61       352        834.0      2.4      0.2          assert len(shape) == 2
    62       352        336.0      1.0      0.1          nfeature_len = shape[0] * shape[1]
    63       352        503.0      1.4      0.1          assert nfeature_len % self._nLen == 0
    64       352        194.0      0.6      0.0          ncur_idx = 0
    65       352       1972.0      5.6      0.4          feature = feature.reshape((nfeature_len))
    66     94084      51197.0      0.5     10.1          while ncur_idx < nfeature_len:
    67     93732      82675.0      0.9     16.3              block = feature[ncur_idx:ncur_idx + self._nLen]
    68     93732     195748.0      2.1     38.7              block = (block - self._mean) * self._var
    69     93732     110739.0      1.2     21.9              feature[ncur_idx:ncur_idx + self._nLen] = block
    70     93732      60208.0      0.6     11.9              ncur_idx += self._nLen
    71       352        718.0      2.0      0.1          feature = feature.reshape(shape)
    72       352        228.0      0.6      0.0          return (feature, label)

pkuyym avatar Feb 28 '18 09:02 pkuyym

trans add delta 耗时非常严重

Total time: 92.8168 s
File: data_utils/augmentor/trans_add_delta.py
Function: perform_trans at line 28

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    28                                               @profile
    29                                               def perform_trans(self, sample):
    30                                                   """ add delta for feature
    31                                                       trans feature shape from (a,b) to (a, b * 3)
    32
    33                                                       Args:
    34                                                           sample(object,tuple): contain feature numpy and label numpy
    35                                                       Returns:
    36                                                           (feature, label)
    37                                                   """
    38       352        360.0      1.0      0.0          (feature, label) = sample
    39       352        483.0      1.4      0.0          frame_dim = feature.shape[1]
    40       352        277.0      0.8      0.0          d_frame_dim = frame_dim * 3
    41       352        235.0      0.7      0.0          head_filled = 5
    42       352        228.0      0.6      0.0          tail_filled = 5
    43       352        308.0      0.9      0.0          mat = np.zeros(
    44       352        433.0      1.2      0.0              (feature.shape[0] + head_filled + tail_filled, d_frame_dim),
    45       352       9226.0     26.2      0.0              dtype="float32")
    46                                                   #copy first frame
    47      2112       1969.0      0.9      0.0          for i in xrange(head_filled):
    48      1760       4312.0      2.5      0.0              np.copyto(mat[i, 0:frame_dim], feature[0, :])
    49
    50       352        707.0      2.0      0.0          np.copyto(mat[head_filled:head_filled + feature.shape[0], 0:frame_dim],
    51       352       4024.0     11.4      0.0                    feature[:, :])
    52
    53                                                   # copy last frame
    54      2112       1987.0      0.9      0.0          for i in xrange(head_filled + feature.shape[0], mat.shape[0], 1):
    55      1760       3730.0      2.1      0.0              np.copyto(mat[i, 0:frame_dim], feature[feature.shape[0] - 1, :])
    56
    57       352        312.0      0.9      0.0          nframe = feature.shape[0]
    58       352        233.0      0.7      0.0          start = head_filled
    59       352        257.0      0.7      0.0          tmp_shape = mat.shape
    60       352       1232.0      3.5      0.0          mat = mat.reshape((tmp_shape[0] * tmp_shape[1]))
    61       352        413.0      1.2      0.0          self._regress(mat, start * d_frame_dim, mat,
    62       352        296.0      0.8      0.0                        start * d_frame_dim + frame_dim, frame_dim, nframe,
    63       352   46471549.0 132021.4     50.1                        d_frame_dim)
    64       352       1196.0      3.4      0.0          self._regress(mat, start * d_frame_dim + frame_dim, mat,
    65       352        344.0      1.0      0.0                        start * d_frame_dim + 2 * frame_dim, frame_dim, nframe,
    66       352   46307610.0 131555.7     49.9                        d_frame_dim)
    67       352       3106.0      8.8      0.0          mat.shape = tmp_shape
    68       352       1932.0      5.5      0.0          return (mat[head_filled:mat.shape[0] - tail_filled, :], label)

pkuyym avatar Feb 28 '18 09:02 pkuyym

有个疑问,此处跑10个batch的总时间109s似乎比跑tools/profile.py长很多,请验证下总时间是否可靠

kuke avatar Feb 28 '18 09:02 kuke

这是sync版,去掉了异步+buffer

pkuyym avatar Feb 28 '18 09:02 pkuyym

OK, 可以适当备注下测试方法

kuke avatar Feb 28 '18 09:02 kuke

您好,此issue在近一个月内暂无更新,我们将于今天内关闭。若在关闭后您仍需跟进提问,可重新开启此问题,我们将在24小时内回复您。因关闭带来的不便我们深表歉意,请您谅解~感谢您对PaddlePaddle的支持! Hello, this issue has not been updated in the past month. We will close it today for the sake of other user‘s experience. If you still need to follow up on this question after closing, please feel free to reopen it. In that case, we will get back to you within 24 hours. We apologize for the inconvenience caused by the closure and thank you so much for your support of PaddlePaddle Group!

shanyi15 avatar Aug 15 '18 09:08 shanyi15