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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. # tests the construction of multiple ops from a single dataset.
  18. # map dataset with columns order arguments should produce a ProjectOp over MapOp
  19. # This test does not utilize the compiling passes at this time.
  20. def test_map_reorder0():
  21. def generator_mc(maxid=1):
  22. for _ in range(maxid):
  23. yield (np.array([0]), np.array([1]))
  24. # Generator -> Map
  25. data0 = ds.GeneratorDataset(generator_mc, ["col0", "col1"])
  26. data0 = data0.map(input_columns="col0", output_columns="out", columns_order=["col1", "out"],
  27. operations=(lambda x: x))
  28. for item in data0.create_tuple_iterator(): # each data is a dictionary
  29. assert item == [np.array(1), np.array(0)]
  30. # tests the construction of multiple ops from a single dataset.
  31. # map dataset with columns order arguments should produce a ProjectOp over MapOp
  32. # This test does not utilize the compiling passes at this time.
  33. def test_map_reorder1():
  34. def generator_mc(maxid=1):
  35. for _ in range(maxid):
  36. yield (np.array([0]), np.array([1]), np.array([2]))
  37. # Three map and zip
  38. data0 = ds.GeneratorDataset(generator_mc, ["a0", "a1", "a2"])
  39. data0 = data0.map(input_columns="a0", columns_order=["a2", "a1", "a0"], operations=(lambda x: x))
  40. data1 = ds.GeneratorDataset(generator_mc, ["b0", "b1", "b2"])
  41. data1 = data1.map(input_columns="b0", columns_order=["b1", "b2", "b0"], operations=(lambda x: x))
  42. data2 = ds.zip((data0, data1))
  43. data2 = data2.map(input_columns="a0", columns_order=["b2", "a2", "b1", "a1", "b0", "a0"], operations=(lambda x: x))
  44. for item in data2.create_tuple_iterator():
  45. assert item == [np.array(2), np.array(2), np.array(1), np.array(1), np.array(0), np.array(0)]
  46. # tests the construction of multiple ops from a single dataset.
  47. # TFRecordDataset with global shuffle should produce a ShuffleOp over TfReaderOp.
  48. # This test does not utilize the compiling passes at this time.
  49. def test_shuffle():
  50. FILES = ["../data/dataset/testTFTestAllTypes/test.data"]
  51. SCHEMA_FILE = "../data/dataset/testTFTestAllTypes/datasetSchema.json"
  52. ds.config.set_seed(1)
  53. data1 = ds.TFRecordDataset(FILES, schema=SCHEMA_FILE, shuffle=ds.Shuffle.GLOBAL)
  54. data2 = ds.TFRecordDataset(FILES, schema=SCHEMA_FILE, shuffle=ds.Shuffle.FILES)
  55. data2 = data2.shuffle(10000)
  56. for d1, d2 in zip(data1, data2):
  57. for t1, t2 in zip(d1, d2):
  58. assert np.array_equal(t1, t2)
  59. ds.config.set_seed(1)
  60. DATA_ALL_FILE = "../data/dataset/testTextFileDataset/*"
  61. data1 = ds.TextFileDataset(DATA_ALL_FILE, shuffle=ds.Shuffle.GLOBAL)
  62. data2 = ds.TextFileDataset(DATA_ALL_FILE, shuffle=ds.Shuffle.FILES)
  63. data2 = data2.shuffle(10000)
  64. for d1, d2 in zip(data1, data2):
  65. for t1, t2 in zip(d1, d2):
  66. assert np.array_equal(t1, t2)
  67. ds.config.set_seed(1)
  68. TRAIN_FILE = '../data/dataset/testCLUE/afqmc/train.json'
  69. data1 = ds.CLUEDataset(TRAIN_FILE, task='AFQMC', usage='train', shuffle=ds.Shuffle.GLOBAL)
  70. data2 = ds.CLUEDataset(TRAIN_FILE, task='AFQMC', usage='train', shuffle=ds.Shuffle.FILES)
  71. data2 = data2.shuffle(10000)
  72. for d1, d2 in zip(data1, data2):
  73. for t1, t2 in zip(d1, d2):
  74. assert np.array_equal(t1, t2)
  75. if __name__ == "__main__":
  76. test_map_reorder0()
  77. test_map_reorder1()
  78. test_global_shuffle()