You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dataset.py 2.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """
  16. Data operations, will be used in run_pretrain.py
  17. """
  18. import os
  19. import mindspore.common.dtype as mstype
  20. import mindspore.dataset.engine.datasets as de
  21. import mindspore.dataset.transforms.c_transforms as C
  22. from mindspore import log as logger
  23. from config import bert_net_cfg
  24. def create_bert_dataset(epoch_size=1, device_num=1, rank=0, do_shuffle="true", enable_data_sink="true",
  25. data_sink_steps=1, data_dir=None, schema_dir=None):
  26. """create train dataset"""
  27. # apply repeat operations
  28. repeat_count = epoch_size
  29. files = os.listdir(data_dir)
  30. data_files = []
  31. for file_name in files:
  32. data_files.append(os.path.join(data_dir, file_name))
  33. ds = de.TFRecordDataset(data_files, schema_dir,
  34. columns_list=["input_ids", "input_mask", "segment_ids", "next_sentence_labels",
  35. "masked_lm_positions", "masked_lm_ids", "masked_lm_weights"],
  36. shuffle=(do_shuffle == "true"), num_shards=device_num, shard_id=rank,
  37. shard_equal_rows=True)
  38. ori_dataset_size = ds.get_dataset_size()
  39. new_size = ori_dataset_size
  40. if enable_data_sink == "true":
  41. new_size = data_sink_steps * bert_net_cfg.batch_size
  42. ds.set_dataset_size(new_size)
  43. repeat_count = int(repeat_count * ori_dataset_size // ds.get_dataset_size())
  44. type_cast_op = C.TypeCast(mstype.int32)
  45. ds = ds.map(input_columns="masked_lm_ids", operations=type_cast_op)
  46. ds = ds.map(input_columns="masked_lm_positions", operations=type_cast_op)
  47. ds = ds.map(input_columns="next_sentence_labels", operations=type_cast_op)
  48. ds = ds.map(input_columns="segment_ids", operations=type_cast_op)
  49. ds = ds.map(input_columns="input_mask", operations=type_cast_op)
  50. ds = ds.map(input_columns="input_ids", operations=type_cast_op)
  51. # apply batch operations
  52. ds = ds.batch(bert_net_cfg.batch_size, drop_remainder=True)
  53. ds = ds.repeat(repeat_count)
  54. logger.info("data size: {}".format(ds.get_dataset_size()))
  55. logger.info("repeatcount: {}".format(ds.get_repeat_count()))
  56. return ds