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 6.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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(device_num=1, rank=0, do_shuffle="true", data_dir=None, schema_dir=None):
  25. """create train dataset"""
  26. # apply repeat operations
  27. files = os.listdir(data_dir)
  28. data_files = []
  29. for file_name in files:
  30. if "tfrecord" in file_name:
  31. data_files.append(os.path.join(data_dir, file_name))
  32. ds = de.TFRecordDataset(data_files, schema_dir if schema_dir != "" else None,
  33. columns_list=["input_ids", "input_mask", "segment_ids", "next_sentence_labels",
  34. "masked_lm_positions", "masked_lm_ids", "masked_lm_weights"],
  35. shuffle=de.Shuffle.FILES if do_shuffle == "true" else False,
  36. num_shards=device_num, shard_id=rank, shard_equal_rows=True)
  37. ori_dataset_size = ds.get_dataset_size()
  38. print('origin dataset size: ', ori_dataset_size)
  39. type_cast_op = C.TypeCast(mstype.int32)
  40. ds = ds.map(input_columns="masked_lm_ids", operations=type_cast_op)
  41. ds = ds.map(input_columns="masked_lm_positions", operations=type_cast_op)
  42. ds = ds.map(input_columns="next_sentence_labels", operations=type_cast_op)
  43. ds = ds.map(input_columns="segment_ids", operations=type_cast_op)
  44. ds = ds.map(input_columns="input_mask", operations=type_cast_op)
  45. ds = ds.map(input_columns="input_ids", operations=type_cast_op)
  46. # apply batch operations
  47. ds = ds.batch(bert_net_cfg.batch_size, drop_remainder=True)
  48. logger.info("data size: {}".format(ds.get_dataset_size()))
  49. logger.info("repeat count: {}".format(ds.get_repeat_count()))
  50. return ds
  51. def create_ner_dataset(batch_size=1, repeat_count=1, assessment_method="accuracy",
  52. data_file_path=None, schema_file_path=None, do_shuffle=True):
  53. """create finetune or evaluation dataset"""
  54. type_cast_op = C.TypeCast(mstype.int32)
  55. ds = de.TFRecordDataset([data_file_path], schema_file_path if schema_file_path != "" else None,
  56. columns_list=["input_ids", "input_mask", "segment_ids", "label_ids"], shuffle=do_shuffle)
  57. if assessment_method == "Spearman_correlation":
  58. type_cast_op_float = C.TypeCast(mstype.float32)
  59. ds = ds.map(input_columns="label_ids", operations=type_cast_op_float)
  60. else:
  61. ds = ds.map(input_columns="label_ids", operations=type_cast_op)
  62. ds = ds.map(input_columns="segment_ids", operations=type_cast_op)
  63. ds = ds.map(input_columns="input_mask", operations=type_cast_op)
  64. ds = ds.map(input_columns="input_ids", operations=type_cast_op)
  65. ds = ds.repeat(repeat_count)
  66. # apply shuffle operation
  67. buffer_size = 960
  68. ds = ds.shuffle(buffer_size=buffer_size)
  69. # apply batch operations
  70. ds = ds.batch(batch_size, drop_remainder=True)
  71. return ds
  72. def create_classification_dataset(batch_size=1, repeat_count=1, assessment_method="accuracy",
  73. data_file_path=None, schema_file_path=None, do_shuffle=True):
  74. """create finetune or evaluation dataset"""
  75. type_cast_op = C.TypeCast(mstype.int32)
  76. ds = de.TFRecordDataset([data_file_path], schema_file_path if schema_file_path != "" else None,
  77. columns_list=["input_ids", "input_mask", "segment_ids", "label_ids"], shuffle=do_shuffle)
  78. if assessment_method == "Spearman_correlation":
  79. type_cast_op_float = C.TypeCast(mstype.float32)
  80. ds = ds.map(input_columns="label_ids", operations=type_cast_op_float)
  81. else:
  82. ds = ds.map(input_columns="label_ids", operations=type_cast_op)
  83. ds = ds.map(input_columns="segment_ids", operations=type_cast_op)
  84. ds = ds.map(input_columns="input_mask", operations=type_cast_op)
  85. ds = ds.map(input_columns="input_ids", operations=type_cast_op)
  86. ds = ds.repeat(repeat_count)
  87. # apply shuffle operation
  88. buffer_size = 960
  89. ds = ds.shuffle(buffer_size=buffer_size)
  90. # apply batch operations
  91. ds = ds.batch(batch_size, drop_remainder=True)
  92. return ds
  93. def create_squad_dataset(batch_size=1, repeat_count=1, data_file_path=None, schema_file_path=None,
  94. is_training=True, do_shuffle=True):
  95. """create finetune or evaluation dataset"""
  96. type_cast_op = C.TypeCast(mstype.int32)
  97. if is_training:
  98. ds = de.TFRecordDataset([data_file_path], schema_file_path if schema_file_path != "" else None,
  99. columns_list=["input_ids", "input_mask", "segment_ids", "start_positions",
  100. "end_positions", "unique_ids", "is_impossible"],
  101. shuffle=do_shuffle)
  102. ds = ds.map(input_columns="start_positions", operations=type_cast_op)
  103. ds = ds.map(input_columns="end_positions", operations=type_cast_op)
  104. else:
  105. ds = de.TFRecordDataset([data_file_path], schema_file_path if schema_file_path != "" else None,
  106. columns_list=["input_ids", "input_mask", "segment_ids", "unique_ids"])
  107. ds = ds.map(input_columns="input_ids", operations=type_cast_op)
  108. ds = ds.map(input_columns="input_mask", operations=type_cast_op)
  109. ds = ds.map(input_columns="segment_ids", operations=type_cast_op)
  110. ds = ds.map(input_columns="segment_ids", operations=type_cast_op)
  111. ds = ds.map(input_columns="input_mask", operations=type_cast_op)
  112. ds = ds.map(input_columns="input_ids", operations=type_cast_op)
  113. ds = ds.repeat(repeat_count)
  114. # apply shuffle operation
  115. buffer_size = 960
  116. ds = ds.shuffle(buffer_size=buffer_size)
  117. # apply batch operations
  118. ds = ds.batch(batch_size, drop_remainder=True)
  119. return ds