"...git@developer.sourcefind.cn:tsoc/superbenchmark.git" did not exist on "f57d86f4d1fdadffb08e7d882a94718df123bb46"
Commit faf97b39 authored by Catheriany's avatar Catheriany
Browse files

issue/228: gguf冗余移除,rms_norm测例

parent c2e87202
...@@ -105,6 +105,10 @@ std::vector<std::string> Test::tensor_names() { ...@@ -105,6 +105,10 @@ std::vector<std::string> Test::tensor_names() {
return {"x", "w", "ans", "y"}; return {"x", "w", "ans", "y"};
} }
std::vector<std::string> Test::output_names() {
return {"y"};
}
std::string Test::toString() const { std::string Test::toString() const {
std::ostringstream oss; std::ostringstream oss;
oss << op_name() << std::endl; oss << op_name() << std::endl;
......
from ast import List
import numpy as np import numpy as np
import gguf from typing import List
from typing import Optional
from numpy.lib.stride_tricks import as_strided
from .. import InfiniopTestWriter, InfiniopTestCase, np_dtype_to_ggml, gguf_strides from .. import InfiniopTestWriter, InfiniopTestCase, np_dtype_to_ggml, gguf_strides, contiguous_gguf_strides
def random_tensor(shape: tuple, dtype: np.dtype) -> np.ndarray: def random_tensor(shape: tuple, dtype: np.dtype) -> np.ndarray:
return np.random.uniform(-1.0, 1.0, shape).astype(dtype) * 0.001 return np.random.uniform(-1.0, 1.0, shape).astype(dtype) * 0.001
...@@ -29,46 +26,35 @@ def rms_norm(x: np.ndarray, w: np.ndarray, epsilon: float) -> np.ndarray: ...@@ -29,46 +26,35 @@ def rms_norm(x: np.ndarray, w: np.ndarray, epsilon: float) -> np.ndarray:
class RMSNormTestCase(InfiniopTestCase): class RMSNormTestCase(InfiniopTestCase):
def __init__( def __init__(
self, self,
shape: tuple, x: np.ndarray,
atype: np.dtype, w: np.ndarray,
wtype: np.dtype, y: np.ndarray,
shape: List[int] | None,
x_strides: List[int] | None,
y_strides: List[int] | None,
epsilon: float = 1e-5, epsilon: float = 1e-5,
x_strides: Optional[tuple] = None,
y_strides: Optional[tuple] = None,
): ):
super().__init__("rms_norm") super().__init__("rms_norm")
self.x = x
self.w = w
self.y = y
self.shape = shape self.shape = shape
self.atype = atype
self.w_shape = (shape[1],)
if x_strides is not None:
itemsize = np.dtype(atype).itemsize
byte_strides = tuple(s * itemsize for s in x_strides)
max_offset = sum((dim - 1) * abs(stride) for dim, stride in zip(shape, x_strides))
buffer_size = max_offset + 1
buffer = np.random.uniform(-1.0, 1.0, buffer_size).astype(atype) * 0.001
self.x = as_strided(buffer, shape=shape, strides=byte_strides)
else:
self.x = random_tensor(shape, atype)
self.w = random_tensor(self.w_shape, wtype)
self.epsilon = epsilon self.epsilon = epsilon
self.x_strides=x_strides
self.y_strides=y_strides
if y_strides is not None:
itemsize_out = np.dtype(atype).itemsize
byte_strides_out = tuple(s * itemsize_out for s in y_strides)
max_offset_out = sum((dim - 1) * abs(stride) for dim, stride in zip(shape, y_strides))
buffer_size_out = max_offset_out + 1
buffer_out = np.zeros(buffer_size_out, dtype=atype)
self.y = as_strided(buffer_out, shape=shape, strides=byte_strides_out)
else:
self.y = np.zeros_like(self.x)
self.ans = rms_norm(self.x.astype(np.float64), self.w.astype(np.float64), self.epsilon)
def write_test(self, test_writer: "InfiniopTestWriter"): def write_test(self, test_writer: "InfiniopTestWriter"):
super().write_test(test_writer) super().write_test(test_writer)
test_writer.add_float32(test_writer.gguf_key("epsilon"), self.epsilon) test_writer.add_float32(test_writer.gguf_key("epsilon"), self.epsilon)
if self.shape is not None:
test_writer.add_array(test_writer.gguf_key("x.shape"), self.shape)
test_writer.add_array(test_writer.gguf_key("y.shape"), self.shape)
if self.x_strides is not None:
test_writer.add_array(test_writer.gguf_key("x.strides"), gguf_strides(*self.x_strides))
test_writer.add_array(
test_writer.gguf_key("y.strides"),
gguf_strides(*self.y_strides if self.y_strides is not None else contiguous_gguf_strides(self.shape))
)
test_writer.add_tensor( test_writer.add_tensor(
test_writer.gguf_key("x"), test_writer.gguf_key("x"),
self.x, self.x,
...@@ -79,150 +65,56 @@ class RMSNormTestCase(InfiniopTestCase): ...@@ -79,150 +65,56 @@ class RMSNormTestCase(InfiniopTestCase):
self.w, self.w,
raw_dtype=np_dtype_to_ggml(self.w.dtype), raw_dtype=np_dtype_to_ggml(self.w.dtype),
) )
test_writer.add_tensor(
test_writer.gguf_key("ans"),
self.ans,
raw_dtype=np_dtype_to_ggml(np.float64),
)
test_writer.add_tensor( test_writer.add_tensor(
test_writer.gguf_key("y"), test_writer.gguf_key("y"),
self.y, self.y,
raw_dtype=np_dtype_to_ggml(self.y.dtype), raw_dtype=np_dtype_to_ggml(self.y.dtype),
) )
ans = rms_norm(self.x.astype(np.float64), self.w.astype(np.float64), self.epsilon)
test_writer.add_tensor(
test_writer.gguf_key("ans"),
ans,
raw_dtype=np_dtype_to_ggml(np.float64),
)
if __name__ == "__main__": if __name__ == "__main__":
test_writer = InfiniopTestWriter("rms_norm.gguf") test_writer = InfiniopTestWriter("rms_norm.gguf")
test_cases = []
test_cases = [
RMSNormTestCase( _TEST_CASES_ = [
shape=(2, 256), # shape, x_strides, y_strides
atype=np.float32, ((2, 256), None, None),
wtype=np.float32, ((4, 512), None, None),
), ((8, 1024), None, None),
RMSNormTestCase( ((1, 768), None, None),
shape=(4, 512), ((8, 256), None, None),
atype=np.float32, ((500, 4096), None, None),
wtype=np.float32, ((4, 512), (1024, 1), None),
), ((4, 512), (512, 1), None),
RMSNormTestCase( ((500, 4096), (9192, 1), None),
shape=(8, 1024), ((500, 4096), (4096, 1), None),
atype=np.float32, ((4, 512), None, (1024, 1)),
wtype=np.float32, ((500, 4096), None, (8192, 1)),
), ((4, 512), (1024, 1), (512, 1)),
RMSNormTestCase( ((4, 512), None, (2048, 1)),
shape=(1, 768),
atype=np.float32,
wtype=np.float32,
),
RMSNormTestCase(
shape=(8, 256),
atype=np.float32,
wtype=np.float32,
),
RMSNormTestCase(
shape=(500, 4096),
atype=np.float32,
wtype=np.float32,
),
RMSNormTestCase(
shape=(2, 256),
atype=np.float16,
wtype=np.float16,
),
RMSNormTestCase(
shape=(4, 512),
atype=np.float16,
wtype=np.float16,
),
RMSNormTestCase(
shape=(500, 4096),
atype=np.float16,
wtype=np.float16,
),
RMSNormTestCase(
shape=(4, 512),
atype=np.float16,
wtype=np.float32,
),
RMSNormTestCase(
shape=(500, 4096),
atype=np.float16,
wtype=np.float32,
),
RMSNormTestCase(
shape=(4, 512),
atype=np.float32,
wtype=np.float32,
x_strides=(1024, 1),
),
RMSNormTestCase(
shape=(4, 512),
atype=np.float32,
wtype=np.float32,
x_strides=(512, 2),
),
RMSNormTestCase(
shape=(500, 4096),
atype=np.float32,
wtype=np.float32,
x_strides=(9192, 1),
),
RMSNormTestCase(
shape=(500, 4096),
atype=np.float32,
wtype=np.float32,
x_strides=(4096, 2),
),
RMSNormTestCase(
shape=(4, 512),
atype=np.float16,
wtype=np.float16,
x_strides=(1024, 1),
),
RMSNormTestCase(
shape=(500, 4096),
atype=np.float16,
wtype=np.float16,
x_strides=(9192, 1),
),
RMSNormTestCase(
shape=(4, 512),
atype=np.float16,
wtype=np.float32,
x_strides=(1024, 1),
),
RMSNormTestCase(
shape=(500, 4096),
atype=np.float16,
wtype=np.float32,
x_strides=(9192, 1),
),
RMSNormTestCase(
shape=(4, 512),
atype=np.float32,
wtype=np.float32,
y_strides=(1024, 1),
),
RMSNormTestCase(
shape=(500, 4096),
atype=np.float32,
wtype=np.float32,
y_strides=(8192, 2),
),
RMSNormTestCase(
shape=(4, 512),
atype=np.float32,
wtype=np.float32,
x_strides=(1024, 1),
y_strides=(512, 2),
),
RMSNormTestCase(
shape=(4, 512),
atype=np.float16,
wtype=np.float16,
y_strides=(2048, 1),
),
] ]
_TENSOR_DTYPES_ = [np.float32, np.float16]
for dtype in _TENSOR_DTYPES_:
for shape, x_strides, y_strides in _TEST_CASES_:
w = np.random.rand(shape[-1]).astype(dtype)
x = np.random.rand(*shape).astype(dtype)
y = np.empty(tuple(0 for _ in shape), dtype=dtype)
epsilon = 1e-5
test_case = RMSNormTestCase(
x=x,
w=w,
y=y,
shape=shape,
x_strides=x_strides,
y_strides=y_strides,
epsilon=epsilon
)
test_cases.append(test_case)
test_writer.add_tests(test_cases) test_writer.add_tests(test_cases)
test_writer.save() test_writer.save()
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