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.

test_opt_pass.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. import numpy as np
  16. import mindspore.dataset as ds
  17. def test_map_reorder_pass_0():
  18. def generator_mc(maxid=1):
  19. for _ in range(maxid):
  20. yield (np.array([0]), np.array([1]))
  21. # Generator -> Map
  22. data0 = ds.GeneratorDataset(generator_mc, ["col0", "col1"])
  23. data0 = data0.map(input_columns="col0", output_columns="out", columns_order=["col1", "out"],
  24. operations=(lambda x: x))
  25. for item in data0.create_tuple_iterator(): # each data is a dictionary
  26. assert item == [np.array(1), np.array(0)]
  27. def test_map_reorder_pass_1():
  28. def generator_mc(maxid=1):
  29. for _ in range(maxid):
  30. yield (np.array([0]), np.array([1]), np.array([2]))
  31. # Three map and zip
  32. data0 = ds.GeneratorDataset(generator_mc, ["a0", "a1", "a2"])
  33. data0 = data0.map(input_columns="a0", columns_order=["a2", "a1", "a0"], operations=(lambda x: x))
  34. data1 = ds.GeneratorDataset(generator_mc, ["b0", "b1", "b2"])
  35. data1 = data1.map(input_columns="b0", columns_order=["b1", "b2", "b0"], operations=(lambda x: x))
  36. data2 = ds.zip((data0, data1))
  37. data2 = data2.map(input_columns="a0", columns_order=["b2", "a2", "b1", "a1", "b0", "a0"], operations=(lambda x: x))
  38. for item in data2.create_tuple_iterator():
  39. assert item == [np.array(2), np.array(2), np.array(1), np.array(1), np.array(0), np.array(0)]
  40. def test_global_shuffle_pass():
  41. FILES = ["../data/dataset/testTFTestAllTypes/test.data"]
  42. SCHEMA_FILE = "../data/dataset/testTFTestAllTypes/datasetSchema.json"
  43. ds.config.set_seed(1)
  44. data1 = ds.TFRecordDataset(FILES, schema=SCHEMA_FILE, shuffle=ds.Shuffle.GLOBAL)
  45. data2 = ds.TFRecordDataset(FILES, schema=SCHEMA_FILE, shuffle=ds.Shuffle.FILES)
  46. data2 = data2.shuffle(10000)
  47. for d1, d2 in zip(data1, data2):
  48. for t1, t2 in zip(d1, d2):
  49. assert np.array_equal(t1, t2)
  50. ds.config.set_seed(1)
  51. DATA_ALL_FILE = "../data/dataset/testTextFileDataset/*"
  52. data1 = ds.TextFileDataset(DATA_ALL_FILE, shuffle=ds.Shuffle.GLOBAL)
  53. data2 = ds.TextFileDataset(DATA_ALL_FILE, shuffle=ds.Shuffle.FILES)
  54. data2 = data2.shuffle(10000)
  55. for d1, d2 in zip(data1, data2):
  56. for t1, t2 in zip(d1, d2):
  57. assert np.array_equal(t1, t2)
  58. ds.config.set_seed(1)
  59. TRAIN_FILE = '../data/dataset/testCLUE/afqmc/train.json'
  60. data1 = ds.CLUEDataset(TRAIN_FILE, task='AFQMC', usage='train', shuffle=ds.Shuffle.GLOBAL)
  61. data2 = ds.CLUEDataset(TRAIN_FILE, task='AFQMC', usage='train', shuffle=ds.Shuffle.FILES)
  62. data2 = data2.shuffle(10000)
  63. for d1, d2 in zip(data1, data2):
  64. for t1, t2 in zip(d1, d2):
  65. assert np.array_equal(t1, t2)
  66. if __name__ == "__main__":
  67. test_map_reorder_pass_0()
  68. test_map_reorder_pass_1()
  69. test_global_shuffle_pass()