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() { ...@@ -110,7 +110,7 @@ std::vector<std::string> Test::tensor_names() {
} }
std::vector<std::string> Test::output_names() { std::vector<std::string> Test::output_names() {
return {"result"}; return {};
} }
std::string Test::toString() const { 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]: ...@@ -37,6 +37,13 @@ def contiguous_gguf_strides(shape: tuple[int, ...]) -> list[int]:
acc *= size acc *= size
return strides[::-1] 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: class InfiniopTestCase:
op_name: str op_name: str
......
...@@ -4,7 +4,7 @@ import gguf ...@@ -4,7 +4,7 @@ import gguf
from typing import List from typing import List
from numpy.lib.stride_tricks import as_strided 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( def add(
...@@ -13,17 +13,6 @@ def add( ...@@ -13,17 +13,6 @@ def add(
): ):
return a + b 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): class AddTestCase(InfiniopTestCase):
def __init__( def __init__(
...@@ -111,9 +100,8 @@ if __name__ == "__main__": ...@@ -111,9 +100,8 @@ if __name__ == "__main__":
a = np.random.rand(*shape).astype(dtype) a = np.random.rand(*shape).astype(dtype)
b = np.random.rand(*shape).astype(dtype) b = np.random.rand(*shape).astype(dtype)
c = np.empty(tuple(0 for _ in shape), dtype=dtype) c = np.empty(tuple(0 for _ in shape), dtype=dtype)
a, b = process_tensor(a, b, stride_a, stride_b) a = process_zero_stride_tensor(a, stride_a)
if stride_c is None: b = process_zero_stride_tensor(b, stride_b)
stride_c = contiguous_gguf_strides(shape)
test_case = AddTestCase( test_case = AddTestCase(
a=a, a=a,
shape_a=shape, shape_a=shape,
......
...@@ -2,7 +2,7 @@ import numpy as np ...@@ -2,7 +2,7 @@ import numpy as np
import gguf import gguf
from typing import List, Optional, Tuple 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( def clip(
...@@ -52,6 +52,7 @@ class ClipTestCase(InfiniopTestCase): ...@@ -52,6 +52,7 @@ class ClipTestCase(InfiniopTestCase):
max_val: np.ndarray, max_val: np.ndarray,
max_stride: Optional[List[int]], max_stride: Optional[List[int]],
y: np.ndarray, y: np.ndarray,
y_shape: Optional[List[int]],
y_stride: Optional[List[int]], y_stride: Optional[List[int]],
): ):
super().__init__("clip") super().__init__("clip")
...@@ -62,6 +63,7 @@ class ClipTestCase(InfiniopTestCase): ...@@ -62,6 +63,7 @@ class ClipTestCase(InfiniopTestCase):
self.max_val = max_val self.max_val = max_val
self.max_stride = max_stride self.max_stride = max_stride
self.y = y self.y = y
self.y_shape=y_shape
self.y_stride = y_stride self.y_stride = y_stride
def write_test(self, test_writer: "InfiniopTestWriter"): def write_test(self, test_writer: "InfiniopTestWriter"):
...@@ -69,13 +71,17 @@ class ClipTestCase(InfiniopTestCase): ...@@ -69,13 +71,17 @@ class ClipTestCase(InfiniopTestCase):
# Add strides as arrays if they exist # Add strides as arrays if they exist
if self.x_stride is not None: 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: 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: if self.max_stride is not None:
test_writer.add_array(test_writer.gguf_key("max_val.strides"), self.max_stride) test_writer.add_array(test_writer.gguf_key("max_val.strides"), gguf_strides(*self.max_stride))
if self.y_stride is not None: if self.y_shape is not None:
test_writer.add_array(test_writer.gguf_key("y.strides"), self.y_stride) 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 # Add tensors to the test
test_writer.add_tensor( test_writer.add_tensor(
...@@ -153,7 +159,7 @@ if __name__ == "__main__": ...@@ -153,7 +159,7 @@ if __name__ == "__main__":
x = random_tensor(shape, dtype) x = random_tensor(shape, dtype)
min_tensor = np.full(shape, min_val, dtype=dtype) min_tensor = np.full(shape, min_val, dtype=dtype)
max_tensor = np.full(shape, max_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( test_cases.append(
ClipTestCase( ClipTestCase(
...@@ -164,6 +170,7 @@ if __name__ == "__main__": ...@@ -164,6 +170,7 @@ if __name__ == "__main__":
max_val=max_tensor, max_val=max_tensor,
max_stride=None, max_stride=None,
y=y, y=y,
y_shape=shape,
y_stride=None y_stride=None
) )
) )
...@@ -172,15 +179,15 @@ if __name__ == "__main__": ...@@ -172,15 +179,15 @@ if __name__ == "__main__":
for shape in [s for s in shapes if len(s) == 2]: for shape in [s for s in shapes if len(s) == 2]:
for dtype in dtypes: for dtype in dtypes:
# Row-major stride # Row-major stride
row_stride = gguf_strides(shape[1], 1) row_stride = (shape[1], 1)
# Column-major stride # Column-major stride
col_stride = gguf_strides(1, shape[0]) col_stride = (1, shape[0])
# Test case with row-major input and output # Test case with row-major input and output
x = random_tensor(shape, dtype) x = random_tensor(shape, dtype)
min_tensor = np.full(shape, -1.0, dtype=dtype) min_tensor = np.full(shape, -1.0, dtype=dtype)
max_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( test_cases.append(
ClipTestCase( ClipTestCase(
...@@ -191,6 +198,7 @@ if __name__ == "__main__": ...@@ -191,6 +198,7 @@ if __name__ == "__main__":
max_val=max_tensor, max_val=max_tensor,
max_stride=row_stride, max_stride=row_stride,
y=y, y=y,
y_shape=shape,
y_stride=row_stride y_stride=row_stride
) )
) )
...@@ -199,7 +207,7 @@ if __name__ == "__main__": ...@@ -199,7 +207,7 @@ if __name__ == "__main__":
x = random_tensor(shape, dtype) x = random_tensor(shape, dtype)
min_tensor = np.full(shape, -1.0, dtype=dtype) min_tensor = np.full(shape, -1.0, dtype=dtype)
max_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( test_cases.append(
ClipTestCase( ClipTestCase(
...@@ -210,6 +218,7 @@ if __name__ == "__main__": ...@@ -210,6 +218,7 @@ if __name__ == "__main__":
max_val=max_tensor, max_val=max_tensor,
max_stride=col_stride, max_stride=col_stride,
y=y, y=y,
y_shape=shape,
y_stride=col_stride y_stride=col_stride
) )
) )
...@@ -218,7 +227,7 @@ if __name__ == "__main__": ...@@ -218,7 +227,7 @@ if __name__ == "__main__":
x = random_tensor(shape, dtype) x = random_tensor(shape, dtype)
min_tensor = np.full(shape, -1.0, dtype=dtype) min_tensor = np.full(shape, -1.0, dtype=dtype)
max_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( test_cases.append(
ClipTestCase( ClipTestCase(
...@@ -229,6 +238,7 @@ if __name__ == "__main__": ...@@ -229,6 +238,7 @@ if __name__ == "__main__":
max_val=max_tensor, max_val=max_tensor,
max_stride=row_stride, max_stride=row_stride,
y=y, y=y,
y_shape=shape,
y_stride=col_stride y_stride=col_stride
) )
) )
......
...@@ -2,7 +2,7 @@ import numpy as np ...@@ -2,7 +2,7 @@ import numpy as np
import gguf import gguf
from typing import List 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( def swiglu(
...@@ -92,6 +92,8 @@ if __name__ == "__main__": ...@@ -92,6 +92,8 @@ if __name__ == "__main__":
((2, 3, 400), (1200, 400, 1), (1200, 400, 1), (1, 2, 6)), ((2, 3, 400), (1200, 400, 1), (1200, 400, 1), (1, 2, 6)),
((4, 4, 5632), None, None, None), ((4, 4, 5632), None, None, None),
((4, 4, 5632), (45056, 5632, 1), (45056, 5632, 1), (45056, 5632, 1)), ((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] _TENSOR_DTYPES_ = [np.float32, np.float16]
...@@ -100,6 +102,8 @@ if __name__ == "__main__": ...@@ -100,6 +102,8 @@ if __name__ == "__main__":
a = np.random.rand(*shape).astype(dtype) a = np.random.rand(*shape).astype(dtype)
b = np.random.rand(*shape).astype(dtype) b = np.random.rand(*shape).astype(dtype)
c = np.empty(tuple(0 for _ in shape), dtype=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( test_case = SwiGLUTestCase(
a=a, a=a,
shape_a=list(shape), 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