test_opt_pass.py 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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.
# ==============================================================================
import numpy as np

import mindspore.dataset as ds

J
Jamie Nisbet 已提交
19 20 21 22
# tests the construction of multiple ops from a single dataset.
# map dataset with columns order arguments should produce a ProjectOp over MapOp
# This test does not utilize the compiling passes at this time.
def test_map_reorder0():
23 24 25 26 27 28 29
    def generator_mc(maxid=1):
        for _ in range(maxid):
            yield (np.array([0]), np.array([1]))

    # Generator -> Map
    data0 = ds.GeneratorDataset(generator_mc, ["col0", "col1"])

N
nhussain 已提交
30
    data0 = data0.map(input_columns="col0", output_columns="out", column_order=["col1", "out"],
31 32
                      operations=(lambda x: x))

33
    for item in data0.create_tuple_iterator(num_epochs=1):  # each data is a dictionary
34 35
        assert item == [np.array(1), np.array(0)]

J
Jamie Nisbet 已提交
36 37 38 39
# tests the construction of multiple ops from a single dataset.
# map dataset with columns order arguments should produce a ProjectOp over MapOp
# This test does not utilize the compiling passes at this time.
def test_map_reorder1():
40 41 42 43 44 45
    def generator_mc(maxid=1):
        for _ in range(maxid):
            yield (np.array([0]), np.array([1]), np.array([2]))

    # Three map and zip
    data0 = ds.GeneratorDataset(generator_mc, ["a0", "a1", "a2"])
N
nhussain 已提交
46
    data0 = data0.map(input_columns="a0", column_order=["a2", "a1", "a0"], operations=(lambda x: x))
47
    data1 = ds.GeneratorDataset(generator_mc, ["b0", "b1", "b2"])
N
nhussain 已提交
48
    data1 = data1.map(input_columns="b0", column_order=["b1", "b2", "b0"], operations=(lambda x: x))
49
    data2 = ds.zip((data0, data1))
N
nhussain 已提交
50
    data2 = data2.map(input_columns="a0", column_order=["b2", "a2", "b1", "a1", "b0", "a0"], operations=(lambda x: x))
51

52
    for item in data2.create_tuple_iterator(num_epochs=1):
53 54
        assert item == [np.array(2), np.array(2), np.array(1), np.array(1), np.array(0), np.array(0)]

J
Jamie Nisbet 已提交
55 56 57 58
# tests the construction of multiple ops from a single dataset.
# TFRecordDataset with global shuffle should produce a ShuffleOp over TfReaderOp.
# This test does not utilize the compiling passes at this time.
def test_shuffle():
59 60 61 62 63 64 65 66 67 68 69

    FILES = ["../data/dataset/testTFTestAllTypes/test.data"]
    SCHEMA_FILE = "../data/dataset/testTFTestAllTypes/datasetSchema.json"

    ds.config.set_seed(1)
    data1 = ds.TFRecordDataset(FILES, schema=SCHEMA_FILE, shuffle=ds.Shuffle.GLOBAL)
    data2 = ds.TFRecordDataset(FILES, schema=SCHEMA_FILE, shuffle=ds.Shuffle.FILES)
    data2 = data2.shuffle(10000)

    for d1, d2 in zip(data1, data2):
        for t1, t2 in zip(d1, d2):
70
            np.testing.assert_array_equal(t1, t2)
71 72 73 74 75 76 77 78 79

    ds.config.set_seed(1)
    DATA_ALL_FILE = "../data/dataset/testTextFileDataset/*"
    data1 = ds.TextFileDataset(DATA_ALL_FILE, shuffle=ds.Shuffle.GLOBAL)
    data2 = ds.TextFileDataset(DATA_ALL_FILE, shuffle=ds.Shuffle.FILES)
    data2 = data2.shuffle(10000)

    for d1, d2 in zip(data1, data2):
        for t1, t2 in zip(d1, d2):
80
            np.testing.assert_array_equal(t1, t2)
81 82 83 84 85 86 87 88 89

    ds.config.set_seed(1)
    TRAIN_FILE = '../data/dataset/testCLUE/afqmc/train.json'
    data1 = ds.CLUEDataset(TRAIN_FILE, task='AFQMC', usage='train', shuffle=ds.Shuffle.GLOBAL)
    data2 = ds.CLUEDataset(TRAIN_FILE, task='AFQMC', usage='train', shuffle=ds.Shuffle.FILES)
    data2 = data2.shuffle(10000)

    for d1, d2 in zip(data1, data2):
        for t1, t2 in zip(d1, d2):
90
            np.testing.assert_array_equal(t1, t2)
91 92 93


if __name__ == "__main__":
J
Jamie Nisbet 已提交
94 95 96
    test_map_reorder0()
    test_map_reorder1()
    test_global_shuffle()