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_cache_nomap.py 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. Testing cache operator with non-mappable datasets
  17. """
  18. import mindspore.common.dtype as mstype
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.transforms.vision.c_transforms as c_vision
  21. from mindspore import log as logger
  22. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  23. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  24. GENERATE_GOLDEN = False
  25. def test_cache_nomap_basic1():
  26. """
  27. A random dataset (a non mappable dataset) with a cache over it just after the leaf
  28. """
  29. logger.info("Test cache nomap basic 1")
  30. schema = ds.Schema()
  31. schema.add_column('image', de_type=mstype.uint8,
  32. shape=[640, 480, 3]) # 921600 bytes (a bit less than 1 MB per image)
  33. schema.add_column('label', de_type=mstype.uint8, shape=[1])
  34. # create a cache. arbitrary session_id for now
  35. some_cache = ds.DatasetCache(session_id=1, size=0, spilling=True)
  36. # User-created sampler here
  37. ds1 = ds.RandomDataset(schema=schema, total_rows=10, num_parallel_workers=4, cache=some_cache)
  38. ds1 = ds1.repeat(4)
  39. num_iter = 0
  40. for data in ds1.create_dict_iterator():
  41. logger.info("printing the label: {}".format(data["label"]))
  42. num_iter += 1
  43. logger.info("Number of data in ds1: {} ".format(num_iter))
  44. assert num_iter == 40
  45. logger.info("test_cache_nomap_basic1 Ended.\n")
  46. def test_cache_nomap_basic2():
  47. """
  48. A random dataset (a non mappable dataset) with a cache over it just after the leaf
  49. """
  50. logger.info("Test cache nomap basic 2")
  51. schema = ds.Schema()
  52. schema.add_column('image', de_type=mstype.uint8,
  53. shape=[640, 480, 3]) # 921600 bytes (a bit less than 1 MB per image)
  54. schema.add_column('label', de_type=mstype.uint8, shape=[1])
  55. # create a cache. arbitrary session_id for now
  56. some_cache = ds.DatasetCache(session_id=1, size=0, spilling=True)
  57. # sampler arg not given directly, however any of these args will auto-generate an appropriate sampler:
  58. # num_samples, shuffle, num_shards, shard_id
  59. # In this case, the presence of num_samples chooses a sampler.
  60. ds1 = ds.RandomDataset(schema=schema, total_rows=20, num_samples=20, num_parallel_workers=4, cache=some_cache)
  61. ds1 = ds1.repeat(2)
  62. num_iter = 0
  63. for data in ds1.create_dict_iterator():
  64. logger.info("printing the label: {}".format(data["label"]))
  65. num_iter += 1
  66. logger.info("Number of data in ds1: {} ".format(num_iter))
  67. assert num_iter == 40
  68. logger.info("test_cache_nomap_basic2 Ended.\n")
  69. def test_cache_nomap_basic3():
  70. """
  71. A TF reader dataset (a non mappable dataset) with a cache over it just after the leaf
  72. Repeat
  73. |
  74. Map(decode)
  75. |
  76. Cache
  77. |
  78. TFReader
  79. """
  80. logger.info("Test cache nomap basic 3")
  81. some_cache = ds.DatasetCache(session_id=1, size=0, spilling=True)
  82. ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False, cache=some_cache)
  83. decode_op = c_vision.Decode()
  84. ds1 = ds1.map(input_columns=["image"], operations=decode_op)
  85. ds1 = ds1.repeat(4)
  86. num_iter = 0
  87. for _ in ds1.create_dict_iterator():
  88. num_iter += 1
  89. logger.info("Number of data in ds1: {} ".format(num_iter))
  90. assert num_iter == 12
  91. logger.info("test_cache_nomap_basic3 Ended.\n")
  92. def test_cache_nomap_basic4():
  93. """
  94. A TF reader dataset (a non mappable dataset) with a map decode and cache after it
  95. Since a global shuffle is used for the tf reader, it will inject a shuffle op over the tf.
  96. But, if there's a cache later, that shuffle becomes invalid and should be removed.
  97. Repeat
  98. |
  99. Cache
  100. |
  101. Map(decode)
  102. |
  103. TFReader
  104. """
  105. logger.info("Test cache nomap basic 4")
  106. # This dataset has 3 records in it only
  107. some_cache = ds.DatasetCache(session_id=1, size=0, spilling=True)
  108. # With shuffle not being set, TF defaults to a "global" shuffle when there is no cache
  109. # in the picture. This causes a shuffle-injection over the TF. For clarify, this test will
  110. # explicitly give the global option, even though it's the default in python.
  111. # But, when caching is added in the ascendent tree above TF, we do global shuffling
  112. # through the sampler over the cache, not by the shuffle op. In that case, tree prepare
  113. # will remove the shuffle op that got injected by the initial tree creation.
  114. ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=ds.Shuffle.GLOBAL)
  115. decode_op = c_vision.Decode()
  116. ds1 = ds1.map(input_columns=["image"], operations=decode_op, cache=some_cache)
  117. ds1 = ds1.repeat(4)
  118. num_iter = 0
  119. for _ in ds1.create_dict_iterator():
  120. num_iter += 1
  121. logger.info("Number of data in ds1: {} ".format(num_iter))
  122. assert num_iter == 12
  123. logger.info("test_cache_nomap_basic4 Ended.\n")
  124. def test_cache_nomap_basic5():
  125. """
  126. A TF reader dataset (a non mappable dataset) with a cache over it just after the leaf
  127. Same as test 3, but this one does not have shuffle arg, causing tf to default to global
  128. shuffle which attempts to inject a shuffle operator. However, since there is a cache
  129. we do not need global shuffle, so the shuffle will not be built. It ends up being
  130. identical to test basic 3, however we arrive at the same tree in different codepaths
  131. (if there was no cache, then the shuffle IS built)
  132. Repeat
  133. |
  134. Map(decode)
  135. |
  136. Cache
  137. |
  138. TFReader
  139. """
  140. logger.info("Test cache nomap basic 5")
  141. # This dataset has 3 records in it only
  142. some_cache = ds.DatasetCache(session_id=1, size=0, spilling=True)
  143. ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], cache=some_cache)
  144. decode_op = c_vision.Decode()
  145. ds1 = ds1.map(input_columns=["image"], operations=decode_op)
  146. ds1 = ds1.repeat(4)
  147. num_iter = 0
  148. for _ in ds1.create_dict_iterator():
  149. num_iter += 1
  150. logger.info("Number of data in ds1: {} ".format(num_iter))
  151. assert num_iter == 12
  152. logger.info("test_cache_nomap_basic5 Ended.\n")
  153. def test_cache_nomap_basic6():
  154. """
  155. A TF reader dataset (a non mappable dataset) with a cache over it just after the leaf
  156. In this one, the tf dataset will be given sharding configuration, however since a cache is
  157. used, the tree prepare should undo the sharding configuration and instead, a distributed
  158. sampler will be chosen with the same shard config.
  159. Repeat
  160. |
  161. Map(decode)
  162. |
  163. Cache
  164. |
  165. TFReader
  166. """
  167. logger.info("Test cache nomap basic 6")
  168. # This dataset has 3 records in it only
  169. some_cache = ds.DatasetCache(session_id=1, size=0, spilling=True)
  170. # With only 3 records shard into 3, we expect only 1 record returned for this shard
  171. # However, the sharding will be done by the sampler, not by the tf record leaf node
  172. # In this case, it is a row-based sharding, not the file-based sharding that would happen if
  173. # there was not any cache.
  174. ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], num_shards=3, shard_id=1, cache=some_cache)
  175. decode_op = c_vision.Decode()
  176. ds1 = ds1.map(input_columns=["image"], operations=decode_op)
  177. ds1 = ds1.repeat(4)
  178. num_iter = 0
  179. for _ in ds1.create_dict_iterator():
  180. num_iter += 1
  181. logger.info("Number of data in ds1: {} ".format(num_iter))
  182. assert num_iter == 4
  183. logger.info("test_cache_nomap_basic6 Ended.\n")
  184. def test_cache_nomap_basic7():
  185. """
  186. A TF reader dataset (a non mappable dataset) that uses global shuffle, and is cached followed by
  187. map.
  188. In this one, the tf dataset with global shuffle might want to inject a shuffle op over top of the
  189. tf reader, but since a cache is given, it will choose not to.
  190. Repeat
  191. |
  192. Map(decode)
  193. |
  194. cache
  195. |
  196. TFReader
  197. """
  198. logger.info("Test cache nomap basic 7")
  199. # This dataset has 3 records in it only
  200. some_cache = ds.DatasetCache(session_id=1, size=0, spilling=True)
  201. ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=ds.Shuffle.GLOBAL, cache=some_cache)
  202. decode_op = c_vision.Decode()
  203. ds1 = ds1.map(input_columns=["image"], operations=decode_op)
  204. ds1 = ds1.repeat(4)
  205. num_iter = 0
  206. for _ in ds1.create_dict_iterator():
  207. num_iter += 1
  208. logger.info("Number of data in ds1: {} ".format(num_iter))
  209. assert num_iter == 12
  210. logger.info("test_cache_nomap_basic7 Ended.\n")
  211. def test_cache_nomap_allowed_share1():
  212. """
  213. It is allowed to share the cache between the following two trees:
  214. Repeat Shuffle
  215. | |
  216. Cache Cache
  217. | |
  218. TFReader TFReader
  219. """
  220. logger.info("Test cache nomap allowed share 1")
  221. ds.config.set_seed(1)
  222. # This dataset has 3 records in it only
  223. some_cache = ds.DatasetCache(session_id=1, size=0, spilling=True)
  224. ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False, cache=some_cache)
  225. ds1 = ds1.repeat(4)
  226. ds2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False, cache=some_cache)
  227. ds2 = ds2.shuffle(buffer_size=2)
  228. num_iter = 0
  229. for _ in ds1.create_dict_iterator():
  230. num_iter += 1
  231. assert num_iter == 12
  232. logger.info("Number of data in ds1: {} ".format(num_iter))
  233. num_iter = 0
  234. for _ in ds2.create_dict_iterator():
  235. num_iter += 1
  236. assert num_iter == 3
  237. logger.info("test_cache_nomap_allowed_share1 Ended.\n")
  238. def test_cache_nomap_allowed_share2():
  239. """
  240. It is allowed to share the cache between the following two trees (with map decode):
  241. Repeat Shuffle
  242. | |
  243. Cache Cache
  244. | |
  245. Map(decode) Map(decode)
  246. | |
  247. TFReader TFReader
  248. """
  249. logger.info("Test cache nomap allowed share 2")
  250. ds.config.set_seed(1)
  251. # This dataset has 3 records in it only
  252. some_cache = ds.DatasetCache(session_id=2, size=0, spilling=True)
  253. decode_op = c_vision.Decode()
  254. ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  255. ds1 = ds1.map(input_columns=["image"], operations=decode_op, cache=some_cache)
  256. ds1 = ds1.repeat(4)
  257. ds2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  258. ds2 = ds2.map(input_columns=["image"], operations=decode_op, cache=some_cache)
  259. ds2 = ds2.shuffle(buffer_size=2)
  260. num_iter = 0
  261. for _ in ds1.create_dict_iterator():
  262. num_iter += 1
  263. logger.info("Number of data in ds1: {} ".format(num_iter))
  264. assert num_iter == 12
  265. num_iter = 0
  266. for _ in ds2.create_dict_iterator():
  267. num_iter += 1
  268. assert num_iter == 3
  269. logger.info("test_cache_nomap_allowed_share2 Ended.\n")
  270. def test_cache_nomap_allowed_share3():
  271. """
  272. It is allowed to share the cache between the following two trees (different shard ids):
  273. Repeat Repeat
  274. | |
  275. Cache Cache
  276. | |
  277. TFReader(shard_id = 0) TFReader(shard_id = 1)
  278. """
  279. logger.info("Test cache nomap allowed share 3")
  280. some_cache = ds.DatasetCache(session_id=1, size=0, spilling=True)
  281. tf_files = ["../data/dataset/tf_file_dataset/test1.data", "../data/dataset/tf_file_dataset/test2.data"]
  282. ds1 = ds.TFRecordDataset(tf_files, num_shards=2, shard_id=0, num_samples=3, shuffle=False, cache=some_cache)
  283. ds1 = ds1.repeat(4)
  284. ds2 = ds.TFRecordDataset(tf_files, num_shards=2, shard_id=1, num_samples=3, shuffle=False, cache=some_cache)
  285. ds2 = ds2.repeat(4)
  286. num_iter = 0
  287. for _ in ds1.create_dict_iterator():
  288. num_iter += 1
  289. logger.info("Number of data in ds1: {} ".format(num_iter))
  290. assert num_iter == 12
  291. num_iter = 0
  292. for _ in ds2.create_dict_iterator():
  293. num_iter += 1
  294. assert num_iter == 12
  295. logger.info("test_cache_nomap_allowed_share3 Ended.\n")
  296. def test_cache_nomap_disallowed_share1():
  297. """
  298. It is not allowed to share the cache between the following two trees:
  299. Cache Cache
  300. | |
  301. Map(decode) Map(rescale)
  302. | |
  303. TFReader TFReader
  304. """
  305. logger.info("Test cache nomap disallowed share1")
  306. # This dataset has 3 records in it only
  307. some_cache = ds.DatasetCache(session_id=1, size=0, spilling=True)
  308. decode_op = c_vision.Decode()
  309. rescale_op = c_vision.Rescale(1.0 / 255.0, -1.0)
  310. ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  311. ds1 = ds1.map(input_columns=["image"], operations=decode_op, cache=some_cache)
  312. ds2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  313. ds2 = ds2.map(input_columns=["image"], operations=rescale_op, cache=some_cache)
  314. num_iter = 0
  315. for _ in ds1.create_dict_iterator():
  316. num_iter += 1
  317. logger.info("Number of data in ds1: {} ".format(num_iter))
  318. assert num_iter == 3
  319. try:
  320. sum([1 for _ in ds2])
  321. except RuntimeError as e:
  322. logger.info("Got an exception in DE: {}".format(str(e)))
  323. assert "Attempt to re-use a cache for a different tree!" in str(e)
  324. logger.info("test_cache_nomap_disallowed_share1 Ended.\n")
  325. if __name__ == '__main__':
  326. test_cache_nomap_basic1()
  327. test_cache_nomap_basic2()
  328. test_cache_nomap_basic3()
  329. test_cache_nomap_basic4()
  330. test_cache_nomap_basic5()
  331. test_cache_nomap_basic6()
  332. test_cache_nomap_basic7()
  333. test_cache_nomap_allowed_share1()
  334. test_cache_nomap_allowed_share2()
  335. test_cache_nomap_allowed_share3()
  336. test_cache_nomap_disallowed_share1()