|
|
@@ -1,7 +1,7 @@ |
|
|
|
import numpy as np |
|
|
|
import pytest |
|
|
|
|
|
|
|
from fastNLP.core.collators.padders.paddle_padder import paddleTensorPadder, paddleSequencePadder, paddleNumberPadder |
|
|
|
from fastNLP.core.collators.padders.paddle_padder import PaddleTensorPadder, PaddleSequencePadder, PaddleNumberPadder |
|
|
|
from fastNLP.core.collators.padders.exceptions import DtypeError |
|
|
|
from fastNLP.envs.imports import _NEED_IMPORT_PADDLE |
|
|
|
|
|
|
@@ -10,9 +10,9 @@ if _NEED_IMPORT_PADDLE: |
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.paddle |
|
|
|
class TestpaddleNumberPadder: |
|
|
|
class TestPaddleNumberPadder: |
|
|
|
def test_run(self): |
|
|
|
padder = paddleNumberPadder(ele_dtype=int, dtype=int, pad_val=-1) |
|
|
|
padder = PaddleNumberPadder(ele_dtype=int, dtype=int, pad_val=-1) |
|
|
|
a = [1, 2, 3] |
|
|
|
t_a = padder(a) |
|
|
|
assert isinstance(t_a, paddle.Tensor) |
|
|
@@ -20,9 +20,9 @@ class TestpaddleNumberPadder: |
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.paddle |
|
|
|
class TestpaddleSequencePadder: |
|
|
|
class TestPaddleSequencePadder: |
|
|
|
def test_run(self): |
|
|
|
padder = paddleSequencePadder(ele_dtype=int, dtype=int, pad_val=-1) |
|
|
|
padder = PaddleSequencePadder(ele_dtype=int, dtype=int, pad_val=-1) |
|
|
|
a = [[1, 2, 3], [3]] |
|
|
|
a = padder(a) |
|
|
|
shape = a.shape |
|
|
@@ -32,20 +32,20 @@ class TestpaddleSequencePadder: |
|
|
|
assert (a == b).sum().item() == shape[0]*shape[1] |
|
|
|
|
|
|
|
def test_dtype_check(self): |
|
|
|
padder = paddleSequencePadder(ele_dtype=np.zeros(3, dtype=np.int32).dtype, dtype=int, pad_val=-1) |
|
|
|
padder = PaddleSequencePadder(ele_dtype=np.zeros(3, dtype=np.int32).dtype, dtype=int, pad_val=-1) |
|
|
|
with pytest.raises(DtypeError): |
|
|
|
padder = paddleSequencePadder(ele_dtype=str, dtype=int, pad_val=-1) |
|
|
|
padder = paddleSequencePadder(ele_dtype='int64', dtype=int, pad_val=-1) |
|
|
|
padder = paddleSequencePadder(ele_dtype=np.int32, dtype=None, pad_val=-1) |
|
|
|
padder = PaddleSequencePadder(ele_dtype=str, dtype=int, pad_val=-1) |
|
|
|
padder = PaddleSequencePadder(ele_dtype='int64', dtype=int, pad_val=-1) |
|
|
|
padder = PaddleSequencePadder(ele_dtype=np.int32, dtype=None, pad_val=-1) |
|
|
|
a = padder([[1], [2, 322]]) |
|
|
|
# assert (a>67).sum()==0 # 因为int8的范围为-67 - 66 |
|
|
|
padder = paddleSequencePadder(ele_dtype=np.zeros(2).dtype, dtype=None, pad_val=-1) |
|
|
|
padder = PaddleSequencePadder(ele_dtype=np.zeros(2).dtype, dtype=None, pad_val=-1) |
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.paddle |
|
|
|
class TestpaddleTensorPadder: |
|
|
|
class TestPaddleTensorPadder: |
|
|
|
def test_run(self): |
|
|
|
padder = paddleTensorPadder(ele_dtype=paddle.zeros((3,)).dtype, dtype=paddle.zeros((3,)).dtype, pad_val=-1) |
|
|
|
padder = PaddleTensorPadder(ele_dtype=paddle.zeros((3,)).dtype, dtype=paddle.zeros((3,)).dtype, pad_val=-1) |
|
|
|
a = [paddle.zeros((3,)), paddle.zeros((2,))] |
|
|
|
a = padder(a) |
|
|
|
shape = a.shape |
|
|
@@ -74,7 +74,7 @@ class TestpaddleTensorPadder: |
|
|
|
[[0, -1], [-1, -1], [-1, -1]]]) |
|
|
|
assert (a == b).sum().item() == shape[0]*shape[1]*shape[2] |
|
|
|
|
|
|
|
padder = paddleTensorPadder(ele_dtype=paddle.zeros((3, )).dtype, dtype=paddle.zeros((3, )).dtype, pad_val=-1) |
|
|
|
padder = PaddleTensorPadder(ele_dtype=paddle.zeros((3, )).dtype, dtype=paddle.zeros((3, )).dtype, pad_val=-1) |
|
|
|
a = [paddle.zeros((3, 2)), paddle.zeros((2, 2))] |
|
|
|
a = padder(a) |
|
|
|
shape = a.shape |
|
|
@@ -85,7 +85,7 @@ class TestpaddleTensorPadder: |
|
|
|
]) |
|
|
|
assert (a == b).sum().item() == shape[0]*shape[1]*shape[2] |
|
|
|
|
|
|
|
padder = paddleTensorPadder(ele_dtype=paddle.zeros((3, 2)).dtype, dtype=None, pad_val=-1) |
|
|
|
padder = PaddleTensorPadder(ele_dtype=paddle.zeros((3, 2)).dtype, dtype=None, pad_val=-1) |
|
|
|
a = [np.zeros((3, 2), dtype=np.float32), np.zeros((2, 2), dtype=np.float32)] |
|
|
|
a = padder(a) |
|
|
|
shape = a.shape |
|
|
@@ -96,11 +96,11 @@ class TestpaddleTensorPadder: |
|
|
|
assert (a == b).sum().item() == shape[0]*shape[1]*shape[2] |
|
|
|
|
|
|
|
def test_dtype_check(self): |
|
|
|
padder = paddleTensorPadder(ele_dtype=np.zeros(3, dtype=np.int8).dtype, dtype=int, pad_val=-1) |
|
|
|
padder = PaddleTensorPadder(ele_dtype=np.zeros(3, dtype=np.int8).dtype, dtype=int, pad_val=-1) |
|
|
|
with pytest.raises(DtypeError): |
|
|
|
padder = paddleTensorPadder(ele_dtype=str, dtype=int, pad_val=-1) |
|
|
|
padder = paddleTensorPadder(ele_dtype='int64', dtype=int, pad_val=-1) |
|
|
|
padder = paddleTensorPadder(ele_dtype=int, dtype='int64', pad_val=-1) |
|
|
|
padder = PaddleTensorPadder(ele_dtype=str, dtype=int, pad_val=-1) |
|
|
|
padder = PaddleTensorPadder(ele_dtype='int64', dtype=int, pad_val=-1) |
|
|
|
padder = PaddleTensorPadder(ele_dtype=int, dtype='int64', pad_val=-1) |
|
|
|
|
|
|
|
def test_v1(self): |
|
|
|
print(paddle.zeros((3, )).dtype) |