|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860 |
- # Copyright 2020 Huawei Technologies Co., Ltd
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- # ==============================================================================
- """
- Testing cache operator with non-mappable datasets
- """
- import os
- import itertools
- import pytest
- import mindspore.common.dtype as mstype
- import mindspore.dataset as ds
- import mindspore.dataset.text as text
- import mindspore.dataset.vision.c_transforms as c_vision
- from mindspore import log as logger
-
- DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
- SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
-
- TEXT_TF_DATA_DIR = ["../data/dataset/testTextTFRecord/text.tfrecord"]
- SCHEMA_DIR2 = "../data/dataset/testTextTFRecord/datasetSchema.json"
-
- TRAIN_DATA_DIR = ["../data/dataset/test_tf_file_3_images2/train-0000-of-0001.data",
- "../data/dataset/test_tf_file_3_images2/train-0000-of-0002.data",
- "../data/dataset/test_tf_file_3_images2/train-0000-of-0003.data",
- "../data/dataset/test_tf_file_3_images2/train-0000-of-0004.data"]
- TRAIN_SCHEMA_DIR = "../data/dataset/test_tf_file_3_images2/datasetSchema.json"
-
- IMAGE_FOLDER_DATA_DIR = "../data/dataset/testImageNetData/train/"
- CLUE_DATA_DIR = '../data/dataset/testCLUE/afqmc/train.json'
- CSV_DATA_DIR = '../data/dataset/testCSV/1.csv'
- TEXT_FILE_DATA_DIR = "../data/dataset/testTextFileDataset/1.txt"
-
- GENERATE_GOLDEN = False
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_basic1():
- """
- A random dataset (a non mappable dataset) with a cache over it just after the leaf
- """
-
- logger.info("Test cache nomap basic 1")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- schema = ds.Schema()
- schema.add_column('image', de_type=mstype.uint8,
- shape=[640, 480, 3]) # 921600 bytes (a bit less than 1 MB per image)
- schema.add_column('label', de_type=mstype.uint8, shape=[1])
-
- # create a cache. arbitrary session_id for now
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # User-created sampler here
- ds1 = ds.RandomDataset(schema=schema, total_rows=10, num_parallel_workers=4, cache=some_cache)
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for data in ds1.create_dict_iterator(num_epochs=1):
- logger.info("printing the label: {}".format(data["label"]))
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 40
- logger.info("test_cache_nomap_basic1 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_basic2():
- """
- A random dataset (a non mappable dataset) with a cache over it just after the leaf
- """
-
- logger.info("Test cache nomap basic 2")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- schema = ds.Schema()
- schema.add_column('image', de_type=mstype.uint8,
- shape=[640, 480, 3]) # 921600 bytes (a bit less than 1 MB per image)
- schema.add_column('label', de_type=mstype.uint8, shape=[1])
-
- # create a cache. arbitrary session_id for now
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # sampler arg not given directly, however any of these args will auto-generate an appropriate sampler:
- # num_samples, shuffle, num_shards, shard_id
- # In this case, the presence of num_samples chooses a sampler.
- ds1 = ds.RandomDataset(schema=schema, total_rows=20, num_samples=20, num_parallel_workers=4, cache=some_cache)
- ds1 = ds1.repeat(2)
-
- num_iter = 0
- for data in ds1.create_dict_iterator(num_epochs=1):
- logger.info("printing the label: {}".format(data["label"]))
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 40
- logger.info("test_cache_nomap_basic2 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_basic3():
- """
- A TF reader dataset (a non mappable dataset) with a cache over it just after the leaf
-
- Repeat
- |
- Map(decode)
- |
- Cache
- |
- TFReader
- """
-
- logger.info("Test cache nomap basic 3")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False, cache=some_cache)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(operations=decode_op, input_columns=["image"])
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
-
- # Contact the server to get the statistics
- stat = some_cache.GetStat()
- cache_sz = stat.avg_cache_sz
- num_mem_cached = stat.num_mem_cached
- num_disk_cached = stat.num_disk_cached
-
- logger.info("Number of rows cached in memory: {}".format(num_mem_cached))
- logger.info("Number of rows spilled to disk: {}".format(num_disk_cached))
- logger.info("Average row cache size: {}".format(cache_sz))
-
- logger.info("test_cache_nomap_basic3 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_basic4():
- """
- A TF reader dataset (a non mappable dataset) with a map decode and cache after it
- Since a global shuffle is used for the tf reader, it will inject a shuffle op over the tf.
- But, if there's a cache later, that shuffle becomes invalid and should be removed.
-
- Repeat
- |
- Cache
- |
- Map(decode)
- |
- TFReader
- """
-
- logger.info("Test cache nomap basic 4")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- # This dataset has 3 records in it only
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
- # With shuffle not being set, TF defaults to a "global" shuffle when there is no cache
- # in the picture. This causes a shuffle-injection over the TF. For clarify, this test will
- # explicitly give the global option, even though it's the default in python.
- # But, when caching is added in the ascendent tree above TF, we do global shuffling
- # through the sampler over the cache, not by the shuffle op. In that case, tree prepare
- # will remove the shuffle op that got injected by the initial tree creation.
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=ds.Shuffle.GLOBAL)
- decode_op = c_vision.Decode()
-
- ds1 = ds1.map(operations=decode_op, input_columns=["image"], cache=some_cache)
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
- logger.info("test_cache_nomap_basic4 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_basic5():
- """
- A TF reader dataset (a non mappable dataset) with a cache over it just after the leaf
- Same as test 3, but this one does not have shuffle arg, causing tf to default to global
- shuffle which attempts to inject a shuffle operator. However, since there is a cache
- we do not need global shuffle, so the shuffle will not be built. It ends up being
- identical to test basic 3, however we arrive at the same tree in different codepaths
- (if there was no cache, then the shuffle IS built)
-
- Repeat
- |
- Map(decode)
- |
- Cache
- |
- TFReader
- """
-
- logger.info("Test cache nomap basic 5")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- # This dataset has 3 records in it only
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], cache=some_cache)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(operations=decode_op, input_columns=["image"])
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
- logger.info("test_cache_nomap_basic5 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_basic6():
- """
- A TF reader dataset (a non mappable dataset) with a cache over it just after the leaf
- In this one, the tf dataset will be given sharding configuration, however since a cache is
- used, the tree prepare should undo the sharding configuration and instead, a distributed
- sampler will be chosen with the same shard config.
-
- Repeat
- |
- Map(decode)
- |
- Cache
- |
- TFReader
- """
-
- logger.info("Test cache nomap basic 6")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- # This dataset has 3 records in it only
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # With only 3 records shard into 3, we expect only 1 record returned for this shard
- # However, the sharding will be done by the sampler, not by the tf record leaf node
- # In this case, it is a row-based sharding, not the file-based sharding that would happen if
- # there was not any cache.
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], num_shards=3, shard_id=1, cache=some_cache)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(operations=decode_op, input_columns=["image"])
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 4
- logger.info("test_cache_nomap_basic6 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_basic7():
- """
- A TF reader dataset (a non mappable dataset) that uses global shuffle, and is cached followed by
- map.
- In this one, the tf dataset with global shuffle might want to inject a shuffle op over top of the
- tf reader, but since a cache is given, it will choose not to.
-
- Repeat
- |
- Map(decode)
- |
- cache
- |
- TFReader
- """
-
- logger.info("Test cache nomap basic 7")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=ds.Shuffle.GLOBAL, cache=some_cache)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(operations=decode_op, input_columns=["image"])
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
- logger.info("test_cache_nomap_basic7 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_basic8():
- """
- Test cache as root node
-
- cache
- |
- TFReader
- """
- logger.info("Test cache basic 8")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, cache=some_cache)
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- logger.info("get data from dataset")
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 3
- logger.info('test_cache_basic8 Ended.\n')
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_basic9():
- """
- Testing the GetStat interface for getting some info from server, but this should fail if the cache is not created
- in a pipeline.
- """
-
- logger.info("Test cache nomap basic 9")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # Contact the server to get the statistics, this should fail because we have not used this cache in any pipeline
- # so there will not be any cache to get stats on.
- with pytest.raises(RuntimeError) as e:
- stat = some_cache.GetStat()
- cache_sz = stat.avg_cache_sz
- logger.info("Average row cache size: {}".format(cache_sz))
- assert "Unexpected error" in str(e.value)
-
- logger.info("test_cache_nomap_basic9 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_allowed_share1():
- """
- It is allowed to share the cache between the following two trees:
-
- Repeat Shuffle
- | |
- Cache Cache
- | |
- TFReader TFReader
- """
-
- logger.info("Test cache nomap allowed share 1")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- ds.config.set_seed(1)
- # This dataset has 3 records in it only
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True, prefetch_size=32)
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False, cache=some_cache)
- ds1 = ds1.repeat(4)
-
- ds2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False, cache=some_cache)
- ds2 = ds2.shuffle(buffer_size=2)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- num_iter += 1
- assert num_iter == 12
- logger.info("Number of data in ds1: {} ".format(num_iter))
-
- num_iter = 0
- for _ in ds2.create_dict_iterator(num_epochs=1):
- num_iter += 1
- assert num_iter == 3
- logger.info("test_cache_nomap_allowed_share1 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_allowed_share2():
- """
- It is allowed to share the cache between the following two trees (with map decode):
-
- Repeat Shuffle
- | |
- Cache Cache
- | |
- Map(decode) Map(decode)
- | |
- TFReader TFReader
- """
-
- logger.info("Test cache nomap allowed share 2")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- ds.config.set_seed(1)
- # This dataset has 3 records in it only
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
- decode_op = c_vision.Decode()
-
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
- ds1 = ds1.map(operations=decode_op, input_columns=["image"], cache=some_cache)
- ds1 = ds1.repeat(4)
-
- ds2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
- ds2 = ds2.map(operations=decode_op, input_columns=["image"], cache=some_cache)
- ds2 = ds2.shuffle(buffer_size=2)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- num_iter += 1
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
-
- num_iter = 0
- for _ in ds2.create_dict_iterator(num_epochs=1):
- num_iter += 1
- assert num_iter == 3
- logger.info("test_cache_nomap_allowed_share2 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_allowed_share3():
- """
- It is allowed to share the cache between the following two trees (different shard ids):
-
- Repeat Repeat
- | |
- Cache Cache
- | |
- TFReader(shard_id = 0) TFReader(shard_id = 1)
- """
-
- logger.info("Test cache nomap allowed share 3")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- tf_files = ["../data/dataset/tf_file_dataset/test1.data", "../data/dataset/tf_file_dataset/test2.data"]
- ds1 = ds.TFRecordDataset(tf_files, num_shards=2, shard_id=0, num_samples=3, shuffle=False, cache=some_cache)
- ds1 = ds1.repeat(4)
-
- ds2 = ds.TFRecordDataset(tf_files, num_shards=2, shard_id=1, num_samples=3, shuffle=False, cache=some_cache)
- ds2 = ds2.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- num_iter += 1
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
-
- num_iter = 0
- for _ in ds2.create_dict_iterator(num_epochs=1):
- num_iter += 1
- assert num_iter == 12
- logger.info("test_cache_nomap_allowed_share3 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_allowed_share4():
- """
- It is allowed to share the cache between the following two trees:
-
- Cache Cache
- | |
- Map(decode, num_parallel_workers=1) Map(decode, num_parallel_workers=2)
- | |
- TFReader TFReader
- """
-
- logger.info("Test cache nomap allowed share 4")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- # This dataset has 3 records in it only
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
- decode_op = c_vision.Decode()
-
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
- ds1 = ds1.map(operations=decode_op, input_columns=["image"], cache=some_cache, num_parallel_workers=1)
-
- ds2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
- ds2 = ds2.map(operations=decode_op, input_columns=["image"], cache=some_cache, num_parallel_workers=2)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- num_iter += 1
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 3
-
- num_iter = 0
- for _ in ds2.create_dict_iterator(num_epochs=1):
- num_iter += 1
- logger.info("Number of data in ds2: {} ".format(num_iter))
- assert num_iter == 3
-
- logger.info("test_cache_nomap_allowed_share4 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_disallowed_share1():
- """
- It is not allowed to share the cache between the following two trees:
-
- Cache Cache
- | |
- Map(decode) Map(rescale)
- | |
- TFReader TFReader
- """
-
- logger.info("Test cache nomap disallowed share1")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- # This dataset has 3 records in it only
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
- decode_op = c_vision.Decode()
- rescale_op = c_vision.Rescale(1.0 / 255.0, -1.0)
-
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
- ds1 = ds1.map(operations=decode_op, input_columns=["image"], cache=some_cache)
-
- ds2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
- ds2 = ds2.map(operations=rescale_op, input_columns=["image"], cache=some_cache)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- num_iter += 1
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 3
-
- with pytest.raises(RuntimeError) as e:
- sum([1 for _ in ds2])
- assert "Attempt to re-use a cache for a different tree!" in str(e.value)
-
- logger.info("test_cache_nomap_disallowed_share1 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_running_twice1():
- """
- Executing the same pipeline for twice (from python), with cache injected after map
-
- Repeat
- |
- Cache
- |
- Map(decode)
- |
- TFRecord
- """
-
- logger.info("Test cache nomap running twice 1")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op, cache=some_cache)
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator():
- num_iter += 1
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
-
- num_iter = 0
- for _ in ds1.create_dict_iterator():
- num_iter += 1
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
-
- logger.info("test_cache_nomap_running_twice1 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_running_twice2():
- """
- Executing the same pipeline for twice (from shell), with cache injected after leaf
-
- Repeat
- |
- Map(decode)
- |
- Cache
- |
- TFRecord
- """
-
- logger.info("Test cache nomap running twice 2")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, cache=some_cache)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op)
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator():
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
- logger.info("test_cache_nomap_running_twice2 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_extra_small_size1():
- """
- Test running pipeline with cache of extra small size and spilling true
-
- Repeat
- |
- Map(decode)
- |
- Cache
- |
- TFRecord
- """
-
- logger.info("Test cache nomap extra small size 1")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
- some_cache = ds.DatasetCache(session_id=session_id, size=1, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, cache=some_cache)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op)
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator():
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
- logger.info("test_cache_nomap_extra_small_size1 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_extra_small_size2():
- """
- Test running pipeline with cache of extra small size and spilling false (failure)
-
- Repeat
- |
- Cache
- |
- Map(decode)
- |
- TFRecord
- """
-
- logger.info("Test cache nomap extra small size 2")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
- some_cache = ds.DatasetCache(session_id=session_id, size=1, spilling=False)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op, cache=some_cache)
- ds1 = ds1.repeat(4)
-
- with pytest.raises(RuntimeError) as e:
- sum([1 for _ in ds1])
- assert "Out of memory" in str(e.value)
- logger.info("test_cache_nomap_extra_small_size2 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_parallel_pipeline1(shard):
- """
- Test running two parallel pipelines (sharing cache) with cache injected after leaf op
-
- Repeat
- |
- Map(decode)
- |
- cache
- |
- TFReader
- """
-
- logger.info("Test cache nomap parallel pipeline 1")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, num_shards=3, shard_id=int(shard), cache=some_cache)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op)
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 4
- logger.info("test_cache_nomap_parallel_pipeline1 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_parallel_pipeline2(shard):
- """
- Test running two parallel pipelines (sharing cache) with cache injected after map op
-
- Repeat
- |
- cache
- |
- Map(decode)
- |
- TFReader
- """
-
- logger.info("Test cache nomap parallel pipeline 2")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, num_shards=3, shard_id=int(shard))
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op, cache=some_cache)
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 4
- logger.info("test_cache_nomap_parallel_pipeline2 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_parallel_workers():
- """
- Test cache with num_parallel_workers > 1 set for map op and leaf op
-
- Repeat
- |
- Map(decode)
- |
- cache
- |
- TFReader
- """
-
- logger.info("Test cache nomap parallel workers")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, num_parallel_workers=4)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op, num_parallel_workers=4, cache=some_cache)
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
- logger.info("test_cache_nomap_parallel_workers Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_server_workers_1():
- """
- start cache server with --workers 1 and then test cache function
-
- Repeat
- |
- cache
- |
- Map(decode)
- |
- TFRecord
- """
-
- logger.info("Test cache nomap server workers 1")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op, cache=some_cache)
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator():
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
- logger.info("test_cache_nomap_server_workers_1 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_server_workers_100():
- """
- start cache server with --workers 100 and then test cache function
-
- Repeat
- |
- Map(decode)
- |
- cache
- |
- TFRecord
- """
-
- logger.info("Test cache nomap server workers 100")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, cache=some_cache)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op)
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator():
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
- logger.info("test_cache_nomap_server_workers_100 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_num_connections_1():
- """
- Test setting num_connections=1 in DatasetCache
-
- Repeat
- |
- cache
- |
- Map(decode)
- |
- TFRecord
- """
-
- logger.info("Test cache nomap num_connections 1")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True, num_connections=1)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op, cache=some_cache)
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator():
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
- logger.info("test_cache_nomap_num_connections_1 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_num_connections_100():
- """
- Test setting num_connections=100 in DatasetCache
-
- Repeat
- |
- Map(decode)
- |
- cache
- |
- TFRecord
- """
-
- logger.info("Test cache nomap num_connections 100")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True, num_connections=100)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, cache=some_cache)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op)
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator():
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
- logger.info("test_cache_nomap_num_connections_100 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_prefetch_size_1():
- """
- Test setting prefetch_size=1 in DatasetCache
-
- Repeat
- |
- cache
- |
- Map(decode)
- |
- TFRecord
- """
-
- logger.info("Test cache nomap prefetch_size 1")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True, prefetch_size=1)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op, cache=some_cache)
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator():
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
- logger.info("test_cache_nomap_prefetch_size_1 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_prefetch_size_100():
- """
- Test setting prefetch_size=100 in DatasetCache
-
- Repeat
- |
- Map(decode)
- |
- cache
- |
- TFRecord
- """
-
- logger.info("Test cache nomap prefetch_size 100")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True, prefetch_size=100)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, cache=some_cache)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op)
- ds1 = ds1.repeat(4)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator():
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 12
- logger.info("test_cache_nomap_prefetch_size_100 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_to_device():
- """
- Test cache with to_device
-
- DeviceQueue
- |
- EpochCtrl
- |
- Repeat
- |
- Map(decode)
- |
- cache
- |
- TFReader
- """
-
- logger.info("Test cache nomap to_device")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op, cache=some_cache)
- ds1 = ds1.repeat(4)
- ds1 = ds1.to_device()
- ds1.send()
-
- logger.info("test_cache_nomap_to_device Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_session_destroy():
- """
- Test executing cache_admin -d while the pipeline is running
-
- Repeat
- |
- Cache
- |
- RandomDataset
- """
-
- logger.info("Test cache nomap session destroy")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- schema = ds.Schema()
- schema.add_column('image', de_type=mstype.uint8,
- shape=[640, 480, 3]) # 921600 bytes (a bit less than 1 MB per image)
- schema.add_column('label', de_type=mstype.uint8, shape=[1])
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # User-created sampler here
- ds1 = ds.RandomDataset(schema=schema, num_parallel_workers=4, cache=some_cache)
- ds1 = ds1.repeat()
-
- with pytest.raises(RuntimeError) as e:
- num_iter = 0
- for _ in ds1.create_dict_iterator():
- num_iter += 1
- assert "Unexpected error" in str(e.value)
-
- logger.info("test_cache_nomap_session_destroy Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_server_stop():
- """
- Test executing cache_admin --stop while the pipeline is running
-
- Repeat
- |
- Cache
- |
- RandomDataset
- """
-
- logger.info("Test cache nomap server stop")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- schema = ds.Schema()
- schema.add_column('image', de_type=mstype.uint8,
- shape=[640, 480, 3]) # 921600 bytes (a bit less than 1 MB per image)
- schema.add_column('label', de_type=mstype.uint8, shape=[1])
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # User-created sampler here
- ds1 = ds.RandomDataset(schema=schema, num_parallel_workers=4, cache=some_cache)
- ds1 = ds1.repeat()
-
- with pytest.raises(RuntimeError) as e:
- num_iter = 0
- for _ in ds1.create_dict_iterator():
- num_iter += 1
- assert "Network error. Cache server is unreachable. Make sure the server is running." in str(e.value)
-
- logger.info("test_cache_nomap_server_stop Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_epoch_ctrl1():
- """
- Test using two-loops method to run several epochs
-
- Map(decode)
- |
- cache
- |
- TFRecord
- """
-
- logger.info("Test cache nomap epoch ctrl1")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, cache=some_cache)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op)
-
- num_epoch = 5
- iter1 = ds1.create_dict_iterator(num_epochs=num_epoch)
-
- epoch_count = 0
- for _ in range(num_epoch):
- row_count = 0
- for _ in iter1:
- row_count += 1
- logger.info("Number of data in ds1: {} ".format(row_count))
- assert row_count == 3
- epoch_count += 1
- assert epoch_count == num_epoch
- logger.info("test_cache_nomap_epoch_ctrl1 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_epoch_ctrl2():
- """
- Test using two-loops method with infinite epochs
-
- cache
- |
- Map(decode)
- |
- TFRecord
- """
-
- logger.info("Test cache nomap epoch ctrl2")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op, cache=some_cache)
-
- num_epoch = 5
- # iter1 will always assume there is a next epoch and never shutdown
- iter1 = ds1.create_dict_iterator()
-
- epoch_count = 0
- for _ in range(num_epoch):
- row_count = 0
- for _ in iter1:
- row_count += 1
- logger.info("Number of data in ds1: {} ".format(row_count))
- assert row_count == 3
- epoch_count += 1
- assert epoch_count == num_epoch
-
- # manually stop the iterator
- iter1.stop()
- logger.info("test_cache_nomap_epoch_ctrl2 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_epoch_ctrl3():
- """
- Test using two-loops method with infinite epochs over repeat
-
- repeat
- |
- Map(decode)
- |
- cache
- |
- TFRecord
- """
-
- logger.info("Test cache nomap epoch ctrl3")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, cache=some_cache)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op)
- ds1 = ds1.repeat(2)
-
- num_epoch = 5
- # iter1 will always assume there is a next epoch and never shutdown
- iter1 = ds1.create_dict_iterator()
-
- epoch_count = 0
- for _ in range(num_epoch):
- row_count = 0
- for _ in iter1:
- row_count += 1
- logger.info("Number of data in ds1: {} ".format(row_count))
- assert row_count == 6
- epoch_count += 1
- assert epoch_count == num_epoch
-
- # reply on garbage collector to destroy iter1
-
- logger.info("test_cache_nomap_epoch_ctrl3 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_epoch_ctrl4():
- """
- Test using two-loops method with repeat under cache
-
- cache
- |
- Map(decode)
- |
- repeat
- |
- TFRecord
- """
-
- logger.info("Test cache nomap epoch ctrl4")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR)
- ds1 = ds1.repeat(2)
- decode_op = c_vision.Decode()
- ds1 = ds1.map(input_columns=["image"], operations=decode_op, cache=some_cache)
-
- num_epoch = 5
- iter1 = ds1.create_dict_iterator(num_epochs=num_epoch)
-
- epoch_count = 0
- for _ in range(num_epoch):
- row_count = 0
- for _ in iter1:
- row_count += 1
- logger.info("Number of data in ds1: {} ".format(row_count))
- assert row_count == 6
- epoch_count += 1
- assert epoch_count == num_epoch
-
- logger.info("test_cache_nomap_epoch_ctrl4 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_multiple_cache1():
- """
- Test multiple cache in the same python script
-
- cache cache
- | |
- Map(decode) Map(decode)
- | |
- TFRecord(train) TFRecord(eval)
- """
-
- logger.info("Test cache nomap multiple cache 1")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- train_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
- eval_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 12 records in it
- train_dataset = ds.TFRecordDataset(TRAIN_DATA_DIR, TRAIN_SCHEMA_DIR)
- decode_op = c_vision.Decode()
- train_dataset = train_dataset.map(input_columns=["image"], operations=decode_op, cache=train_cache)
-
- # This dataset has 3 records in it only
- eval_dataset = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR)
- eval_dataset = eval_dataset.map(input_columns=["image"], operations=decode_op, cache=eval_cache)
-
- num_epoch = 5
- train_iter = train_dataset.create_dict_iterator(num_epochs=num_epoch)
- eval_iter = eval_dataset.create_dict_iterator(num_epochs=num_epoch)
-
- epoch_count = 0
- for _ in range(num_epoch):
- assert sum([1 for _ in train_iter]) == 12
- assert sum([1 for _ in eval_iter]) == 3
- epoch_count += 1
- assert epoch_count == num_epoch
-
- logger.info("test_cache_nomap_multiple_cache1 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_multiple_cache2():
- """
- Test multiple cache in the same python script
-
- cache
- |
- Map(decode) cache
- | |
- TFRecord(image) TFRecord(text)
- """
-
- logger.info("Test cache nomap multiple cache 2")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- image_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
- text_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- image_dataset = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR)
- decode_op = c_vision.Decode()
- image_dataset = image_dataset.map(input_columns=["image"], operations=decode_op, cache=image_cache)
-
- # This dataset has 3 records in it only
- text_dataset = ds.TFRecordDataset(TEXT_TF_DATA_DIR, SCHEMA_DIR2, cache=text_cache)
-
- num_epoch = 5
- image_iter = image_dataset.create_dict_iterator(num_epochs=num_epoch)
- text_iter = text_dataset.create_dict_iterator(num_epochs=num_epoch, output_numpy=True)
-
- epoch_count = 0
- for _ in range(num_epoch):
- row_count = 0
- for _, _ in itertools.zip_longest(image_iter, text_iter):
- row_count += 1
- assert row_count == 3
- epoch_count += 1
- assert epoch_count == num_epoch
-
- logger.info("test_cache_nomap_multiple_cache2 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_multiple_cache3():
- """
- Test multiple cache in the same python script
-
- cache cache
- | |
- Map(decode) Map(decode)
- | |
- TFRecord ImageFolder
- """
-
- logger.info("Test cache nomap multiple cache 3")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- tf_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
- image_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- tf_dataset = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR)
- decode_op = c_vision.Decode()
- tf_dataset = tf_dataset.map(input_columns=["image"], operations=decode_op, cache=tf_cache)
-
- # This DATA_DIR only has 2 images in it
- image_dataset = ds.ImageFolderDataset(dataset_dir=IMAGE_FOLDER_DATA_DIR)
- image_dataset = image_dataset.map(input_columns=["image"], operations=decode_op, cache=image_cache)
-
- num_epoch = 5
- tf_iter = tf_dataset.create_dict_iterator(num_epochs=num_epoch)
- image_iter = image_dataset.create_dict_iterator(num_epochs=num_epoch)
-
- epoch_count = 0
- for _ in range(num_epoch):
- assert sum([1 for _ in tf_iter]) == 3
- assert sum([1 for _ in image_iter]) == 2
- epoch_count += 1
- assert epoch_count == num_epoch
-
- logger.info("test_cache_nomap_multiple_cache3 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_multiple_cache_train():
- """
- Test multiple cache in different python scripts. This test case is going to run concurrently with
- test_cache_nomap_multiple_cache_eval.
-
- cache
- |
- Map(decode)
- |
- TFRecord(train)
- """
-
- logger.info("Test cache nomap multiple cache train")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- train_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 12 records in it
- train_dataset = ds.TFRecordDataset(TRAIN_DATA_DIR, TRAIN_SCHEMA_DIR)
- decode_op = c_vision.Decode()
- train_dataset = train_dataset.map(input_columns=["image"], operations=decode_op, cache=train_cache)
-
- num_epoch = 5
- train_iter = train_dataset.create_dict_iterator(num_epochs=num_epoch)
-
- epoch_count = 0
- for _ in range(num_epoch):
- assert sum([1 for _ in train_iter]) == 12
- epoch_count += 1
- assert epoch_count == num_epoch
-
- logger.info("test_cache_nomap_multiple_cache_train Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_multiple_cache_eval():
- """
- Test multiple cache in different python scripts. This test case is going to run concurrently with
- test_cache_nomap_multiple_cache_train.
-
- cache
- |
- Map(decode)
- |
- TFRecord(eval)
- """
-
- logger.info("Test cache nomap multiple cache eval")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- eval_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset only has 3 records in it
- eval_dataset = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR)
- decode_op = c_vision.Decode()
- eval_dataset = eval_dataset.map(input_columns=["image"], operations=decode_op, cache=eval_cache)
-
- num_epoch = 5
- eval_iter = eval_dataset.create_dict_iterator(num_epochs=num_epoch)
-
- epoch_count = 0
- for _ in range(num_epoch):
- assert sum([1 for _ in eval_iter]) == 3
- epoch_count += 1
- assert epoch_count == num_epoch
-
- logger.info("test_cache_nomap_multiple_cache_eval Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_clue1():
- """
- A clue dataset (a non mappable dataset) with a cache over it just after the leaf
- In this one, the clue dataset will be given sharding configuration, however since a cache is
- used, the tree prepare should undo the sharding configuration and instead, a distributed
- sampler will be chosen with the same shard config.
-
- Cache
- |
- CLUE
- """
-
- logger.info("Test cache nomap clue 1")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # With only 3 records shard into 3, we expect only 1 record returned for this shard
- # However, the sharding will be done by the sampler, not by the clue leaf node
- # In this case, it is a row-based sharding, not the file-based sharding that would happen if
- # there was not any cache.
- ds1 = ds.CLUEDataset(CLUE_DATA_DIR, task='AFQMC', usage='train', num_shards=3, shard_id=1, cache=some_cache)
-
- num_epoch = 4
- iter1 = ds1.create_dict_iterator(num_epochs=num_epoch, output_numpy=True)
-
- epoch_count = 0
- for _ in range(num_epoch):
- assert sum([1 for _ in iter1]) == 1
- epoch_count += 1
- assert epoch_count == num_epoch
-
- logger.info("test_cache_nomap_clue1 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_clue2():
- """
- A clue dataset (a non mappable dataset) with a cache over it after map
- In this one, a num_samples argument is given
-
- Cache
- |
- map(lambda x: x)
- |
- CLUE
- """
-
- logger.info("Test cache nomap clue 2")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- ds1 = ds.CLUEDataset(CLUE_DATA_DIR, task='AFQMC', usage='train', num_samples=2)
- ds1 = ds1.map((lambda x: x), ["label"], cache=some_cache)
-
- num_epoch = 4
- iter1 = ds1.create_dict_iterator(num_epochs=num_epoch, output_numpy=True)
-
- epoch_count = 0
- for _ in range(num_epoch):
- assert sum([1 for _ in iter1]) == 2
- epoch_count += 1
- assert epoch_count == num_epoch
-
- logger.info("test_cache_nomap_clue2 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_csv1():
- """
- A csv dataset (a non mappable dataset) with a cache over it just after the leaf
- In this one, the csv dataset will be given sharding configuration, however since a cache is
- used, the tree prepare should undo the sharding configuration and instead, a distributed
- sampler will be chosen with the same shard config.
-
- Cache
- |
- CSV
- """
-
- logger.info("Test cache nomap csv 1")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # With only 3 records shard into 3, we expect only 1 record returned for this shard
- # However, the sharding will be done by the sampler, not by the clue leaf node
- # In this case, it is a row-based sharding, not the file-based sharding that would happen if
- # there was not any cache.
- ds1 = ds.CSVDataset(CSV_DATA_DIR, column_defaults=["1", "2", "3", "4"],
- column_names=['col1', 'col2', 'col3', 'col4'], num_shards=3, shard_id=1, cache=some_cache)
-
- num_epoch = 4
- iter1 = ds1.create_dict_iterator(num_epochs=num_epoch, output_numpy=True)
-
- epoch_count = 0
- for _ in range(num_epoch):
- assert sum([1 for _ in iter1]) == 1
- epoch_count += 1
- assert epoch_count == num_epoch
-
- logger.info("test_cache_nomap_csv1 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_csv2():
- """
- A csv dataset (a non mappable dataset) with a cache over it after map
- In this one, a num_samples argument is given
-
- Cache
- |
- map(lambda x: x)
- |
- CSV
- """
-
- logger.info("Test cache nomap csv 2")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- ds1 = ds.CSVDataset(CSV_DATA_DIR, column_defaults=["1", "2", "3", "4"],
- column_names=['col1', 'col2', 'col3', 'col4'], num_samples=2)
- ds1 = ds1.map((lambda x: x), ["col1"], cache=some_cache)
-
- num_epoch = 4
- iter1 = ds1.create_dict_iterator(num_epochs=num_epoch, output_numpy=True)
-
- epoch_count = 0
- for _ in range(num_epoch):
- assert sum([1 for _ in iter1]) == 2
- epoch_count += 1
- assert epoch_count == num_epoch
-
- logger.info("test_cache_nomap_csv2 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_textfile1():
- """
- A text file dataset (a non mappable dataset) with a cache over it just after the leaf
- In this one, the text file dataset will be given sharding configuration, however since a cache is
- used, the tree prepare should undo the sharding configuration and instead, a distributed
- sampler will be chosen with the same shard config.
-
- Cache
- |
- TextFile
- """
-
- logger.info("Test cache nomap textfile 1")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # With only 3 records shard into 3, we expect only 1 record returned for this shard
- # However, the sharding will be done by the sampler, not by the clue leaf node
- # In this case, it is a row-based sharding, not the file-based sharding that would happen if
- # there was not any cache.
- ds1 = ds.CSVDataset(TEXT_FILE_DATA_DIR, num_shards=3, shard_id=1, cache=some_cache)
-
- num_epoch = 4
- iter1 = ds1.create_dict_iterator(num_epochs=num_epoch, output_numpy=True)
-
- epoch_count = 0
- for _ in range(num_epoch):
- assert sum([1 for _ in iter1]) == 1
- epoch_count += 1
- assert epoch_count == num_epoch
-
- logger.info("test_cache_nomap_textfile1 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_textfile2():
- """
- A text file dataset (a non mappable dataset) with a cache over it after map
- In this one, a num_samples argument is given
-
- Cache
- |
- Map(tokenizer)
- |
- TextFile
- """
- def my_tokenizer(line):
- words = line.split()
- if not words:
- return [""]
- return words
-
- logger.info("Test cache nomap textfile 2")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- ds1 = ds.TextFileDataset(TEXT_FILE_DATA_DIR, num_samples=2)
- tokenizer = text.PythonTokenizer(my_tokenizer)
- ds1 = ds1.map(operations=tokenizer, cache=some_cache)
-
- num_epoch = 4
- iter1 = ds1.create_dict_iterator(num_epochs=num_epoch, output_numpy=True)
-
- epoch_count = 0
- for _ in range(num_epoch):
- assert sum([1 for _ in iter1]) == 2
- epoch_count += 1
- assert epoch_count == num_epoch
-
- logger.info("test_cache_nomap_textfile2 Ended.\n")
-
-
- @pytest.mark.skipif(os.environ.get('RUN_CACHE_TEST') != 'TRUE', reason="Require to bring up cache server")
- def test_cache_nomap_nested_repeat():
- """
- Test cache on pipeline with nested repeat ops
-
- Repeat
- |
- Cache
- |
- Map(decode)
- |
- Repeat
- |
- TFRecord
- """
-
- logger.info("Test cache nomap nested repeat")
- if "SESSION_ID" in os.environ:
- session_id = int(os.environ['SESSION_ID'])
- else:
- raise RuntimeError("Testcase requires SESSION_ID environment variable")
-
- some_cache = ds.DatasetCache(session_id=session_id, size=0, spilling=True)
-
- # This dataset has 3 records in it only
- ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR)
- decode_op = c_vision.Decode()
- ds1 = ds1.repeat(4)
- ds1 = ds1.map(operations=decode_op, input_columns=["image"], cache=some_cache)
- ds1 = ds1.repeat(2)
-
- num_iter = 0
- for _ in ds1.create_dict_iterator(num_epochs=1):
- logger.info("get data from dataset")
- num_iter += 1
-
- logger.info("Number of data in ds1: {} ".format(num_iter))
- assert num_iter == 24
- logger.info('test_cache_nomap_nested_repeat Ended.\n')
-
-
- if __name__ == '__main__':
- test_cache_nomap_basic1()
- test_cache_nomap_basic2()
- test_cache_nomap_basic3()
- test_cache_nomap_basic4()
- test_cache_nomap_basic5()
- test_cache_nomap_basic6()
- test_cache_nomap_basic7()
- test_cache_nomap_allowed_share1()
- test_cache_nomap_allowed_share2()
- test_cache_nomap_allowed_share3()
- test_cache_nomap_allowed_share4()
- test_cache_nomap_disallowed_share1()
|