forked from rustformers/llm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.rs
216 lines (193 loc) · 6.52 KB
/
tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::{
collections::BTreeMap,
error::Error,
io::{BufRead, Write},
};
use crate::*;
use rand::{distributions::Uniform, prelude::*};
#[derive(Debug)]
struct DummyError;
impl std::fmt::Display for DummyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&self, f)
}
}
impl Error for DummyError {}
#[test]
fn can_roundtrip_loader_and_saver_ggml() {
let tokenizer = vec![
("blazingly".as_bytes().to_vec(), 0.0),
("fast".as_bytes().to_vec(), 0.0),
("memory".as_bytes().to_vec(), 0.0),
("efficient".as_bytes().to_vec(), 0.0),
];
roundtrip_test(format::SaveContainerType::Ggml, tokenizer).unwrap();
}
#[test]
fn will_fail_on_scored_ggml_save() {
let tokenizer = vec![
("blazingly".as_bytes().to_vec(), 0.1),
("fast".as_bytes().to_vec(), 0.2),
("memory".as_bytes().to_vec(), 0.3),
("efficient".as_bytes().to_vec(), 0.4),
];
assert_eq!(
roundtrip_test(format::SaveContainerType::Ggml, tokenizer)
.unwrap_err()
.to_string(),
format::SaveError::<std::io::Error>::VocabularyScoringNotSupported.to_string()
);
}
#[test]
fn can_roundtrip_loader_and_saver_ggjt_v3() {
let tokenizer = vec![
("blazingly".as_bytes().to_vec(), 0.1),
("fast".as_bytes().to_vec(), 0.2),
("memory".as_bytes().to_vec(), 0.3),
("efficient".as_bytes().to_vec(), 0.4),
];
roundtrip_test(format::SaveContainerType::GgjtV3, tokenizer).unwrap();
}
fn roundtrip_test(
save_container_type: format::SaveContainerType,
tokenizer: Vec<(Vec<u8>, f32)>,
) -> anyhow::Result<()> {
let mut rng = rand::thread_rng();
let element_type = crate::Type::F16;
let model = Model {
hyperparameters: Hyperparameters {
some_hyperparameter: random(),
some_other_hyperparameter: random(),
tokenizer_size: tokenizer.len().try_into()?,
},
tokenizer,
tensors: (0..10)
.map(|i| {
let n_dims = Uniform::from(1..3).sample(&mut rng);
let dims = (0..n_dims)
.map(|_| Uniform::from(1..10).sample(&mut rng))
.chain(std::iter::repeat(1).take(2 - n_dims))
.collect::<Vec<_>>();
let n_elements = dims.iter().product::<usize>();
let data = (0..format::data_size(element_type, n_elements))
.map(|_| random())
.collect::<Vec<_>>();
(
format!("tensor_{}", i),
format::TensorSaveInfo {
n_dims,
dims: dims.try_into().unwrap(),
element_type,
data,
},
)
})
.collect(),
};
// Save the model.
let mut buffer = Vec::new();
let mut cursor = std::io::Cursor::new(&mut buffer);
let mut save_handler = MockSaveHandler { model: &model };
format::save(
&mut cursor,
&mut save_handler,
save_container_type,
&model.tokenizer,
&model.tensors.keys().cloned().collect::<Vec<String>>(),
)?;
// Load the model and confirm that it is the same as the original.
let mut cursor = std::io::Cursor::new(&buffer);
let mut load_handler = MockLoadHandler {
data: &buffer,
loaded_model: Model::default(),
expected_container_type: save_container_type.into(),
};
format::load(&mut cursor, &mut load_handler)?;
assert_eq!(load_handler.loaded_model, model);
Ok(())
}
#[derive(Default, PartialEq, Debug)]
struct Hyperparameters {
some_hyperparameter: u32,
some_other_hyperparameter: u32,
tokenizer_size: u32,
}
impl Hyperparameters {
fn read(reader: &mut dyn BufRead) -> Result<Self, std::io::Error> {
Ok(Self {
some_hyperparameter: util::read_u32(reader)?,
some_other_hyperparameter: util::read_u32(reader)?,
tokenizer_size: util::read_u32(reader)?,
})
}
fn write(&self, writer: &mut dyn Write) -> Result<(), std::io::Error> {
util::write_u32(writer, self.some_hyperparameter)?;
util::write_u32(writer, self.some_other_hyperparameter)?;
util::write_u32(writer, self.tokenizer_size)?;
Ok(())
}
}
#[derive(Default, PartialEq, Debug)]
struct Model {
hyperparameters: Hyperparameters,
tokenizer: Vec<(Vec<u8>, f32)>,
tensors: BTreeMap<String, format::TensorSaveInfo>,
}
struct MockSaveHandler<'a> {
model: &'a Model,
}
impl format::SaveHandler<DummyError> for MockSaveHandler<'_> {
fn write_hyperparameters(&mut self, writer: &mut dyn Write) -> Result<(), DummyError> {
self.model.hyperparameters.write(writer).unwrap();
Ok(())
}
fn tensor_data(&mut self, tensor_name: &str) -> Result<format::TensorSaveInfo, DummyError> {
self.model
.tensors
.get(tensor_name)
.cloned()
.ok_or(DummyError)
}
}
struct MockLoadHandler<'a> {
data: &'a [u8],
loaded_model: Model,
expected_container_type: ContainerType,
}
impl format::LoadHandler<DummyError> for MockLoadHandler<'_> {
fn container_type(&mut self, container_type: ContainerType) -> Result<(), DummyError> {
assert_eq!(container_type, self.expected_container_type);
Ok(())
}
fn vocabulary_token(&mut self, i: usize, token: Vec<u8>, score: f32) -> Result<(), DummyError> {
assert_eq!(i, self.loaded_model.tokenizer.len());
self.loaded_model.tokenizer.push((token, score));
Ok(())
}
fn read_hyperparameters(
&mut self,
reader: &mut dyn BufRead,
) -> Result<format::PartialHyperparameters, DummyError> {
self.loaded_model.hyperparameters = Hyperparameters::read(reader).unwrap();
Ok(format::PartialHyperparameters {
n_vocab: self
.loaded_model
.hyperparameters
.tokenizer_size
.try_into()
.unwrap(),
})
}
fn tensor_buffer(&mut self, info: format::TensorLoadInfo) -> Result<(), DummyError> {
let data = format::TensorSaveInfo {
n_dims: info.n_dims,
dims: info.dims,
element_type: info.element_type,
data: info
.read_data(&mut std::io::Cursor::new(self.data))
.unwrap(),
};
self.loaded_model.tensors.insert(info.name, data);
Ok(())
}
}