Unverified Commit bf5062d5 authored by PanZezhong1725's avatar PanZezhong1725 Committed by GitHub
Browse files

Merge pull request #249 from InfiniTensor/issue/228

issue/228: swiglu测例0步长添加
parents de0fa8bf 22a3115c
......@@ -110,7 +110,7 @@ std::vector<std::string> Test::tensor_names() {
}
std::vector<std::string> Test::output_names() {
return {"result"};
return {};
}
std::string Test::toString() const {
......
from .infiniop_test import InfiniopTestCase, InfiniopTestWriter, np_dtype_to_ggml, gguf_strides, contiguous_gguf_strides
from .infiniop_test import InfiniopTestCase, InfiniopTestWriter, np_dtype_to_ggml, gguf_strides, contiguous_gguf_strides, process_zero_stride_tensor
......@@ -37,6 +37,13 @@ def contiguous_gguf_strides(shape: tuple[int, ...]) -> list[int]:
acc *= size
return strides[::-1]
def process_zero_stride_tensor(tensor, stride=None):
if stride:
slices = tuple(slice(0, 1) if s == 0 else slice(None) for s in stride)
return tensor[slices]
else:
return tensor
class InfiniopTestCase:
op_name: str
......
......@@ -4,7 +4,7 @@ import gguf
from typing import List
from numpy.lib.stride_tricks import as_strided
from .. import InfiniopTestWriter, InfiniopTestCase, np_dtype_to_ggml, gguf_strides, contiguous_gguf_strides
from .. import InfiniopTestWriter, InfiniopTestCase, np_dtype_to_ggml, gguf_strides, contiguous_gguf_strides, process_zero_stride_tensor
def add(
......@@ -13,17 +13,6 @@ def add(
):
return a + b
def process_tensor(a, b, stride_a=None, stride_b=None):
def normalize_stride(tensor, stride):
if stride:
slices = tuple(slice(0, 1) if s == 0 else slice(None) for s in stride)
return tensor[slices]
else:
return tensor
a_unique = normalize_stride(a, stride_a)
b_unique = normalize_stride(b, stride_b)
return a_unique, b_unique
class AddTestCase(InfiniopTestCase):
def __init__(
......@@ -111,9 +100,8 @@ if __name__ == "__main__":
a = np.random.rand(*shape).astype(dtype)
b = np.random.rand(*shape).astype(dtype)
c = np.empty(tuple(0 for _ in shape), dtype=dtype)
a, b = process_tensor(a, b, stride_a, stride_b)
if stride_c is None:
stride_c = contiguous_gguf_strides(shape)
a = process_zero_stride_tensor(a, stride_a)
b = process_zero_stride_tensor(b, stride_b)
test_case = AddTestCase(
a=a,
shape_a=shape,
......
......@@ -2,7 +2,7 @@ import numpy as np
import gguf
from typing import List, Optional, Tuple
from .. import InfiniopTestWriter, InfiniopTestCase, np_dtype_to_ggml, gguf_strides
from .. import InfiniopTestWriter, InfiniopTestCase, np_dtype_to_ggml, gguf_strides, contiguous_gguf_strides
def clip(
......@@ -52,6 +52,7 @@ class ClipTestCase(InfiniopTestCase):
max_val: np.ndarray,
max_stride: Optional[List[int]],
y: np.ndarray,
y_shape: Optional[List[int]],
y_stride: Optional[List[int]],
):
super().__init__("clip")
......@@ -62,6 +63,7 @@ class ClipTestCase(InfiniopTestCase):
self.max_val = max_val
self.max_stride = max_stride
self.y = y
self.y_shape=y_shape
self.y_stride = y_stride
def write_test(self, test_writer: "InfiniopTestWriter"):
......@@ -69,13 +71,17 @@ class ClipTestCase(InfiniopTestCase):
# Add strides as arrays if they exist
if self.x_stride is not None:
test_writer.add_array(test_writer.gguf_key("x.strides"), self.x_stride)
test_writer.add_array(test_writer.gguf_key("x.strides"), gguf_strides(*self.x_stride))
if self.min_stride is not None:
test_writer.add_array(test_writer.gguf_key("min_val.strides"), self.min_stride)
test_writer.add_array(test_writer.gguf_key("min_val.strides"), gguf_strides(*self.min_stride))
if self.max_stride is not None:
test_writer.add_array(test_writer.gguf_key("max_val.strides"), self.max_stride)
if self.y_stride is not None:
test_writer.add_array(test_writer.gguf_key("y.strides"), self.y_stride)
test_writer.add_array(test_writer.gguf_key("max_val.strides"), gguf_strides(*self.max_stride))
if self.y_shape is not None:
test_writer.add_array(test_writer.gguf_key("y.shape"), self.y_shape)
test_writer.add_array(
test_writer.gguf_key("y.strides"),
gguf_strides(*self.y_stride if self.y_stride is not None else contiguous_gguf_strides(self.y_shape))
)
# Add tensors to the test
test_writer.add_tensor(
......@@ -153,7 +159,7 @@ if __name__ == "__main__":
x = random_tensor(shape, dtype)
min_tensor = np.full(shape, min_val, dtype=dtype)
max_tensor = np.full(shape, max_val, dtype=dtype)
y = np.zeros(shape, dtype=dtype)
y = np.empty(tuple(0 for _ in shape), dtype=dtype)
test_cases.append(
ClipTestCase(
......@@ -164,6 +170,7 @@ if __name__ == "__main__":
max_val=max_tensor,
max_stride=None,
y=y,
y_shape=shape,
y_stride=None
)
)
......@@ -172,15 +179,15 @@ if __name__ == "__main__":
for shape in [s for s in shapes if len(s) == 2]:
for dtype in dtypes:
# Row-major stride
row_stride = gguf_strides(shape[1], 1)
row_stride = (shape[1], 1)
# Column-major stride
col_stride = gguf_strides(1, shape[0])
col_stride = (1, shape[0])
# Test case with row-major input and output
x = random_tensor(shape, dtype)
min_tensor = np.full(shape, -1.0, dtype=dtype)
max_tensor = np.full(shape, 1.0, dtype=dtype)
y = np.zeros(shape, dtype=dtype)
y = np.empty(tuple(0 for _ in shape), dtype=dtype)
test_cases.append(
ClipTestCase(
......@@ -191,6 +198,7 @@ if __name__ == "__main__":
max_val=max_tensor,
max_stride=row_stride,
y=y,
y_shape=shape,
y_stride=row_stride
)
)
......@@ -199,7 +207,7 @@ if __name__ == "__main__":
x = random_tensor(shape, dtype)
min_tensor = np.full(shape, -1.0, dtype=dtype)
max_tensor = np.full(shape, 1.0, dtype=dtype)
y = np.zeros(shape, dtype=dtype)
y = np.empty(tuple(0 for _ in shape), dtype=dtype)
test_cases.append(
ClipTestCase(
......@@ -210,6 +218,7 @@ if __name__ == "__main__":
max_val=max_tensor,
max_stride=col_stride,
y=y,
y_shape=shape,
y_stride=col_stride
)
)
......@@ -218,7 +227,7 @@ if __name__ == "__main__":
x = random_tensor(shape, dtype)
min_tensor = np.full(shape, -1.0, dtype=dtype)
max_tensor = np.full(shape, 1.0, dtype=dtype)
y = np.zeros(shape, dtype=dtype)
y = np.empty(tuple(0 for _ in shape), dtype=dtype)
test_cases.append(
ClipTestCase(
......@@ -229,6 +238,7 @@ if __name__ == "__main__":
max_val=max_tensor,
max_stride=row_stride,
y=y,
y_shape=shape,
y_stride=col_stride
)
)
......
......@@ -2,7 +2,7 @@ import numpy as np
import gguf
from typing import List
from .. import InfiniopTestWriter, InfiniopTestCase, np_dtype_to_ggml, gguf_strides, contiguous_gguf_strides
from .. import InfiniopTestWriter, InfiniopTestCase, np_dtype_to_ggml, gguf_strides, contiguous_gguf_strides, process_zero_stride_tensor
def swiglu(
......@@ -92,6 +92,8 @@ if __name__ == "__main__":
((2, 3, 400), (1200, 400, 1), (1200, 400, 1), (1, 2, 6)),
((4, 4, 5632), None, None, None),
((4, 4, 5632), (45056, 5632, 1), (45056, 5632, 1), (45056, 5632, 1)),
((13, 4), (0, 1), None, None),
((13, 4, 4), (4, 0, 1), (0, 4, 1), None),
]
_TENSOR_DTYPES_ = [np.float32, np.float16]
......@@ -100,6 +102,8 @@ if __name__ == "__main__":
a = np.random.rand(*shape).astype(dtype)
b = np.random.rand(*shape).astype(dtype)
c = np.empty(tuple(0 for _ in shape), dtype=dtype)
a = process_zero_stride_tensor(a, stride_a)
b = process_zero_stride_tensor(b, stride_b)
test_case = SwiGLUTestCase(
a=a,
shape_a=list(shape),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment