Skip to content

Commit

Permalink
Refine pytest cases
Browse files Browse the repository at this point in the history
1.Change the import path of "keres" since the path "tensorflow/python/keras" have legacy issues
2.Simplify the usage of "pytest.apporx"
3.Remove unused import

Type: Code Improvement
Signed-off-by: Feiyue Chen <[email protected]>
  • Loading branch information
chenfeiyue-cfy authored and sunshinemyson committed Aug 3, 2023
1 parent d0ce320 commit 4bf1e87
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 56 deletions.
19 changes: 9 additions & 10 deletions test/python/test_UnidirectionalSequenceLSTM.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import pytest
import tensorflow as tf
from tensorflow.python import keras
from tensorflow import keras
import utils

@pytest.mark.parametrize("batch,timesteps,feature", [(32,10,8),(5,28,28)])
@pytest.mark.parametrize("unit", [4])
@pytest.mark.parametrize("unroll_type", [False])

def test_UnidirectionalSequenceLSTM(delegate_lib, batch, timesteps, feature, unit, unroll_type):
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Input(shape = (timesteps,feature), batch_size=batch))
model.add(tf.keras.layers.LSTM(units = unit,unroll = unroll_type))

model = keras.models.Sequential()
model.add(keras.layers.Input(shape = (timesteps,feature), batch_size=batch))
model.add(keras.layers.LSTM(units = unit,unroll = unroll_type))
model.build()

converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

tflite_model = converter.convert()
model_path = "./test_model.tflite"
open(model_path, "wb").write(tflite_model)

Expand All @@ -25,5 +25,4 @@ def test_UnidirectionalSequenceLSTM(delegate_lib, batch, timesteps, feature, uni
(gold_in, gold_out)= cpu_.run_with_rand_data(model_path)
npu_out = npu_.run(model_path, gold_in)

for (g, n) in zip(gold_out, npu_out):
assert pytest.approx(g, n)
pytest.approx(gold_out,npu_out)
6 changes: 2 additions & 4 deletions test/python/test_attention.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest
import tensorflow as tf
from tensorflow.python import keras
import numpy as np
from tensorflow import keras
import utils
import tempfile
import os
Expand Down Expand Up @@ -61,6 +60,5 @@ def rand_dataset():
cpu_ = utils.cpu()
(gold_in, gold_out)= cpu_.run_with_rand_data(model_path)
npu_out = npu_.run(model_path, gold_in)
for (g, n) in zip(gold_out, npu_out):
assert g == pytest.approx(n[1])
pytest.approx(gold_out,npu_out)
temp_model.close()
8 changes: 2 additions & 6 deletions test/python/test_batchmatmul.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import pytest
import tensorflow as tf
import numpy as np
from tensorflow.python import keras
from tensorflow import keras
import tempfile

import sys

import utils


Expand Down Expand Up @@ -63,5 +60,4 @@ def data_set():
(gold_in, gold_out)= cpu_.run_with_rand_data(fp.name)
npu_out = npu_.run(fp.name, gold_in)
fp.close()
for (g, n) in zip(gold_out, npu_out):
assert pytest.approx(g, n)
pytest.approx(gold_out,npu_out)
6 changes: 2 additions & 4 deletions test/python/test_conv1d.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest
import tensorflow as tf
from tensorflow.python import keras
import numpy as np
from tensorflow import keras
import utils
import tempfile

Expand Down Expand Up @@ -56,5 +55,4 @@ def rand_dataset():
# (gold_in, gold_out)= cpu_.run_with_rand_data(model_path)
# npu_out = npu_.run(model_path, gold_in)

for (g, n) in zip(gold_out, npu_out):
assert pytest.approx(g, n[1])
pytest.approx(gold_out,npu_out)
9 changes: 3 additions & 6 deletions test/python/test_conv2d.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest
import tensorflow as tf
from tensorflow.python import keras
import numpy as np
from tensorflow import keras
import utils
import tempfile

Expand All @@ -17,7 +16,7 @@ def test_conv2d(delegate_lib, batch_size, in_w, in_h, in_ch, out_ch, k_w, k_h, q
input_dtype = tf.float32

def rand_calibration():
for _ in range(100):
yield [tf.random.normal((batch_size, in_h, in_w, in_ch), 0, 127, input_dtype)]
yield [ tf.random.normal((batch_size, in_h, in_w, in_ch), 0, 127, input_dtype) ]

model = keras.models.Sequential([
Expand All @@ -42,6 +41,4 @@ def rand_calibration():
cpu_ = utils.cpu()
(gold_in, gold_out)= cpu_.run_with_rand_data(fp.name)
npu_out = npu_.run(fp.name, gold_in)
fp.close()
for (g, n) in zip(gold_out, npu_out):
assert pytest.approx(g, n[1])
pytest.approx(gold_out,npu_out)
4 changes: 1 addition & 3 deletions test/python/test_conv3d.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest
import tensorflow as tf
import numpy as np
import utils

@pytest.mark.parametrize("batch_size", [1])
Expand Down Expand Up @@ -43,5 +42,4 @@ def rand_calibration():
(gold_in, gold_out)= cpu_.run_with_rand_data(model_path)
npu_out = npu_.run(model_path, gold_in)

for (g, n) in zip(gold_out, npu_out):
assert pytest.approx(g, n)
pytest.approx(gold_out,npu_out)
6 changes: 2 additions & 4 deletions test/python/test_depthwise_conv2d.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest
import tensorflow as tf
from tensorflow.python import keras
import numpy as np
from tensorflow import keras
import utils
import tempfile

Expand Down Expand Up @@ -49,5 +48,4 @@ def rand_dataset():
(gold_in, gold_out)= cpu_.run_with_rand_data(fp.name)
npu_out = npu_.run(fp.name, gold_in)
fp.close()
for (g, n) in zip(gold_out, npu_out):
assert pytest.approx(g, n[1])
pytest.approx(gold_out,npu_out)
7 changes: 2 additions & 5 deletions test/python/test_grucell.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import pytest
import tensorflow as tf
from tensorflow.python import keras
import numpy as np
from tensorflow import keras
import utils
import tempfile

@pytest.mark.parametrize("num_units", [2])
@pytest.mark.parametrize("feature", [4])
Expand Down Expand Up @@ -47,5 +45,4 @@ def test_GRUCell(delegate_lib, num_units, feature):

npu_out = npu_.run(model_path, gold_in)

for (g, n) in zip(gold_out, npu_out):
assert pytest.approx(g, n[1])
pytest.approx(gold_out,npu_out)
5 changes: 2 additions & 3 deletions test/python/test_reverseV2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
import tensorflow as tf
from tensorflow.python import keras
from tensorflow import keras
import tempfile
import numpy as np
import utils
Expand Down Expand Up @@ -62,5 +62,4 @@ def data_set():
npu_out = npu_.run(fp.name, gold_in)
fp.close()

for (g, n) in zip(gold_out, npu_out):
assert pytest.approx(g, n)
pytest.approx(gold_out,npu_out)
5 changes: 2 additions & 3 deletions test/python/test_stack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
import tensorflow as tf
from tensorflow.python import keras
from tensorflow import keras
import tempfile

import utils
Expand Down Expand Up @@ -73,5 +73,4 @@ def data_set():
npu_out = npu_.run(fp.name, gold_in)
fp.close()

for (g, n) in zip(gold_out, npu_out):
assert pytest.approx(g, n)
pytest.approx(gold_out,npu_out)
5 changes: 2 additions & 3 deletions test/python/test_stride_slice.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
import tensorflow as tf
from tensorflow.python import keras
from tensorflow import keras
import tempfile

import utils
Expand Down Expand Up @@ -82,5 +82,4 @@ def data_set():
npu_out = npu_.run(fp.name, gold_in)
fp.close()

for (g, n) in zip(gold_out, npu_out):
assert pytest.approx(g, n)
pytest.approx(gold_out,npu_out)
7 changes: 2 additions & 5 deletions test/python/test_transpose_conv2d.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest
import tensorflow as tf
from tensorflow.python import keras
import numpy as np
from tensorflow import keras
import utils
import tempfile

Expand Down Expand Up @@ -55,6 +54,4 @@ def rand_dataset():
# open(model_path, "wb").write(tflite_model)
# (gold_in, gold_out)= cpu_.run_with_rand_data(model_path)
# npu_out = npu_.run(model_path, gold_in)

for (g, n) in zip(gold_out, npu_out):
assert pytest.approx(g, n[1])
pytest.approx(gold_out,npu_out)

0 comments on commit 4bf1e87

Please sign in to comment.