From 7130a89d1f59d238ac649a536a137918098263c1 Mon Sep 17 00:00:00 2001 From: saharNooby Date: Sat, 1 Apr 2023 14:41:30 +0400 Subject: [PATCH] [FILE FORMAT CHANGED] Reverse dimensions in ggml file (makes it more similar to llama.cpp format) --- rwkv.cpp | 7 +------ rwkv/convert_pytorch_rwkv_to_ggml.py | 9 ++++++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/rwkv.cpp b/rwkv.cpp index f3127a8..e3f9a3d 100644 --- a/rwkv.cpp +++ b/rwkv.cpp @@ -192,7 +192,6 @@ struct rwkv_context * rwkv_init_from_file(const char * file_path, int n_threads) int32_t x = -1; int32_t y = -1; - int32_t z = -1; int32_t element_count; if (dim_count == 1) { @@ -203,11 +202,7 @@ struct rwkv_context * rwkv_init_from_file(const char * file_path, int n_threads) read_int32(file, &x); read_int32(file, &y); element_count = x * y; - // Dimension order is reversed here: - // * PyTorch shape is (x rows, y columns) - // * ggml shape is (y elements in a row, x elements in a column) - // Both shapes represent the same tensor. - tensor = ggml_new_tensor_2d(ctx, ggml_data_type, y, x); + tensor = ggml_new_tensor_2d(ctx, ggml_data_type, x, y); } else { abort(); } diff --git a/rwkv/convert_pytorch_rwkv_to_ggml.py b/rwkv/convert_pytorch_rwkv_to_ggml.py index 2ff8ea1..67532de 100644 --- a/rwkv/convert_pytorch_rwkv_to_ggml.py +++ b/rwkv/convert_pytorch_rwkv_to_ggml.py @@ -103,8 +103,11 @@ def write_state_dict(state_dict: Dict[str, torch.Tensor], dest_path: str, data_t 1 if tensor.dtype == torch.float16 else 0 )) - # Note that shape is not reversed here like in llama.cpp! - for dim in tensor.shape: + # Dimension order is reversed here: + # * PyTorch shape is (x rows, y columns) + # * ggml shape is (y elements in a row, x elements in a column) + # Both shapes represent the same tensor. + for dim in reversed(tensor.shape): out_file.write(struct.pack('=i', dim)) out_file.write(k_encoded) @@ -150,7 +153,7 @@ def test() -> None: 2, 10, 0, - 3, 2, + 2, 3, 'emb.weight'.encode('utf-8'), 1.0, 2.0, 3.0, 4.0, 5.0, 6.0,