ferrum-models 0.11.0

Model architectures (LLaMA, Qwen, BERT) for Ferrum inference
Documentation
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Real HTTP framing and on-disk recovery, without weights or an external Hub.
use super::{DownloadRetryPolicy, HfDownloader};
use std::collections::VecDeque;
use std::num::NonZeroU32;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tempfile::TempDir;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::TcpListener;
use tokio::task::JoinHandle;

const MODEL: &str = "fixture/recoverable";
const REVISION: &str = "1234567890abcdef1234567890abcdef12345678";
const FILE: &str = "model.safetensors";
const ETAG: &str = "immutable-payload";
const BYTES: &[u8] = b"immutable model bytes: abcdefghijklmnopqrstuvwxyz";

enum BodyFraming {
    ContentLength,
    Chunked { chunk_size: usize },
}

#[derive(Clone, Copy)]
enum BodyEnding {
    Complete,
    Interrupted,
    Pending,
}

struct Reply {
    status: &'static str,
    range: Option<String>,
    length: usize,
    body: Vec<u8>,
    delay: Duration,
    framing: BodyFraming,
    ending: BodyEnding,
}

impl Reply {
    fn full() -> Self {
        Self {
            status: "200 OK",
            range: None,
            length: BYTES.len(),
            body: BYTES.to_vec(),
            delay: Duration::ZERO,
            framing: BodyFraming::ContentLength,
            ending: BodyEnding::Complete,
        }
    }

    fn partial(offset: usize) -> Self {
        Self {
            status: "206 Partial Content",
            range: Some(format!(
                "bytes {offset}-{}/{}",
                BYTES.len() - 1,
                BYTES.len()
            )),
            length: BYTES.len() - offset,
            body: BYTES[offset..].to_vec(),
            ..Self::full()
        }
    }

    fn error(status: &'static str) -> Self {
        Self {
            status,
            length: 0,
            body: vec![],
            ..Self::full()
        }
    }

    fn chunked(mut self, chunk_size: usize, ending: BodyEnding) -> Self {
        assert!(chunk_size > 0);
        self.framing = BodyFraming::Chunked { chunk_size };
        self.ending = ending;
        self
    }
}

#[derive(Debug, Clone)]
struct Request {
    method: String,
    path: String,
    range: Option<String>,
}

struct TransferFixture {
    cache: TempDir,
    endpoint: String,
    requests: Arc<Mutex<Vec<Request>>>,
    server: JoinHandle<()>,
}

impl TransferFixture {
    async fn start(replies: Vec<Reply>) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let endpoint = format!("http://{}", listener.local_addr().unwrap());
        let requests = Arc::new(Mutex::new(Vec::new()));
        let recorded = requests.clone();
        let mut replies: VecDeque<_> = replies.into();
        let server = tokio::spawn(async move {
            loop {
                let (socket, _) = listener.accept().await.unwrap();
                let mut socket = BufReader::new(socket);
                let mut line = String::new();
                socket.read_line(&mut line).await.unwrap();
                let mut fields = line.split_whitespace();
                let method = fields.next().unwrap().to_owned();
                let path = fields.next().unwrap().to_owned();
                let mut range = None;
                let mut header_bytes = line.len();
                loop {
                    line.clear();
                    let count = socket.read_line(&mut line).await.unwrap();
                    header_bytes += count;
                    assert!(header_bytes < 16_384);
                    if count == 0 || line == "\r\n" {
                        break;
                    }
                    if let Some((name, value)) = line.split_once(':') {
                        if name.eq_ignore_ascii_case("range") {
                            range = Some(value.trim().to_owned());
                        }
                    }
                }
                recorded.lock().unwrap().push(Request {
                    method: method.clone(),
                    path,
                    range,
                });
                let reply = if method == "HEAD" {
                    Reply::full()
                } else {
                    replies
                        .pop_front()
                        .unwrap_or_else(|| Reply::error("503 Service Unavailable"))
                };
                tokio::time::sleep(reply.delay).await;
                let mut headers = format!(
                    "HTTP/1.1 {}\r\nETag: \"{ETAG}\"\r\nConnection: close\r\n",
                    reply.status
                );
                match &reply.framing {
                    BodyFraming::ContentLength => {
                        headers.push_str(&format!("Content-Length: {}\r\n", reply.length));
                    }
                    BodyFraming::Chunked { .. } => {
                        headers.push_str("Transfer-Encoding: chunked\r\n");
                    }
                }
                if let Some(range) = &reply.range {
                    headers.push_str(&format!("Content-Range: {range}\r\n"));
                }
                headers.push_str("\r\n");
                // A timeout may close the client before this intentionally
                // delayed reply. That is not a server-task panic.
                if socket.get_mut().write_all(headers.as_bytes()).await.is_ok() && method != "HEAD"
                {
                    match reply.framing {
                        BodyFraming::ContentLength => {
                            let _ = socket.get_mut().write_all(&reply.body).await;
                        }
                        BodyFraming::Chunked { chunk_size } => {
                            for chunk in reply.body.chunks(chunk_size) {
                                let framing = format!("{:x}\r\n", chunk.len());
                                let _ = socket.get_mut().write_all(framing.as_bytes()).await;
                                let _ = socket.get_mut().write_all(chunk).await;
                                let _ = socket.get_mut().write_all(b"\r\n").await;
                                let _ = socket.get_mut().flush().await;
                                tokio::task::yield_now().await;
                            }
                            if matches!(reply.ending, BodyEnding::Complete) {
                                let _ = socket.get_mut().write_all(b"0\r\n\r\n").await;
                            }
                        }
                    }
                    let _ = socket.get_mut().flush().await;
                    if matches!(reply.ending, BodyEnding::Pending) {
                        // Keep the HTTP body open until the fixture is dropped;
                        // only the downloader's whole-file budget can end it.
                        std::future::pending::<()>().await;
                    }
                    if reply.body.len() < reply.length {
                        tokio::time::sleep(Duration::from_millis(10)).await;
                    }
                }
                let _ = socket.get_mut().shutdown().await;
            }
        });
        let cache = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(cache.path().join("blobs")).unwrap();
        std::fs::create_dir_all(cache.path().join("snapshot")).unwrap();
        Self {
            cache,
            endpoint,
            requests,
            server,
        }
    }

    fn incomplete(&self) -> PathBuf {
        self.cache
            .path()
            .join("blobs")
            .join(format!("{ETAG}.incomplete"))
    }

    fn gets(&self) -> Vec<Request> {
        self.requests
            .lock()
            .unwrap()
            .iter()
            .filter(|request| request.method == "GET")
            .cloned()
            .collect()
    }

    async fn download(&self, policy: DownloadRetryPolicy) -> ferrum_types::Result<()> {
        let downloader = HfDownloader {
            client: reqwest::Client::builder().no_proxy().build().unwrap(),
            cache_dir: self.cache.path().to_path_buf(),
            token: None,
            endpoint: self.endpoint.clone(),
            retry_policy: DownloadRetryPolicy::default(),
        }
        .with_retry_policy(policy)?;
        downloader
            .download_file_concurrent(
                MODEL,
                REVISION,
                FILE,
                BYTES.len() as u64,
                &self.cache.path().join("blobs"),
                &self.cache.path().join("snapshot"),
                None,
            )
            .await
    }

    fn assert_complete(&self) {
        assert_eq!(
            std::fs::read(self.cache.path().join("snapshot").join(FILE)).unwrap(),
            BYTES
        );
        assert_eq!(
            std::fs::read(self.cache.path().join("blobs").join(ETAG)).unwrap(),
            BYTES
        );
        assert!(!self.incomplete().exists());
        let expected_path = format!("/{MODEL}/resolve/{REVISION}/{FILE}");
        assert!(self
            .requests
            .lock()
            .unwrap()
            .iter()
            .all(|request| request.path == expected_path));
    }

    fn assert_unpublished(&self) {
        assert!(!self.cache.path().join("snapshot").join(FILE).exists());
        assert!(!self.cache.path().join("blobs").join(ETAG).exists());
    }
}

impl Drop for TransferFixture {
    fn drop(&mut self) {
        self.server.abort();
    }
}

fn policy(attempts: u32) -> DownloadRetryPolicy {
    DownloadRetryPolicy {
        max_attempts: NonZeroU32::new(attempts).unwrap(),
        initial_backoff: Duration::ZERO,
        max_elapsed: Duration::from_secs(3),
    }
}

#[tokio::test]
async fn interrupted_body_resumes_the_saved_prefix_within_one_download() {
    let prefix = 13;
    let mut first = Reply::full();
    first.body.truncate(prefix);
    let fixture = TransferFixture::start(vec![first, Reply::partial(prefix)]).await;
    fixture.download(policy(2)).await.unwrap();
    fixture.assert_complete();
    let gets = fixture.gets();
    assert_eq!(gets[0].range, None);
    assert_eq!(gets[1].range.as_deref(), Some("bytes=13-"));
}

#[tokio::test]
async fn ignored_range_replaces_partial_bytes_instead_of_appending_a_full_body() {
    let fixture = TransferFixture::start(vec![Reply::full()]).await;
    std::fs::write(fixture.incomplete(), &BYTES[..9]).unwrap();
    fixture.download(policy(1)).await.unwrap();
    fixture.assert_complete();
    assert_eq!(fixture.gets()[0].range.as_deref(), Some("bytes=9-"));
}

#[tokio::test]
async fn invalid_partial_response_never_recombines_or_publishes_bytes() {
    for range in [
        None,
        Some(format!("bytes 8-{}/{}", BYTES.len() - 1, BYTES.len())),
        Some(format!("bytes 9-8/{}", BYTES.len())),
        Some(format!("bytes 9-{}/{}", BYTES.len() - 1, BYTES.len() + 1)),
        Some("unknown-unit 9-47/48".into()),
    ] {
        let mut reply = Reply::partial(9);
        reply.range = range;
        let fixture = TransferFixture::start(vec![reply]).await;
        std::fs::write(fixture.incomplete(), &BYTES[..9]).unwrap();
        assert!(fixture.download(policy(3)).await.is_err());
        fixture.assert_unpublished();
        assert_eq!(std::fs::read(fixture.incomplete()).unwrap(), &BYTES[..9]);
        assert_eq!(fixture.gets().len(), 1, "invalid framing is not transient");
    }
}

#[tokio::test]
async fn permanent_http_error_preserves_partial_without_retry_or_publication() {
    for status in ["401 Unauthorized", "404 Not Found"] {
        let fixture = TransferFixture::start(vec![Reply::error(status)]).await;
        std::fs::write(fixture.incomplete(), &BYTES[..9]).unwrap();
        assert!(fixture.download(policy(3)).await.is_err());
        fixture.assert_unpublished();
        assert_eq!(std::fs::read(fixture.incomplete()).unwrap(), &BYTES[..9]);
        assert_eq!(fixture.gets().len(), 1);
    }
}

#[tokio::test]
async fn transient_http_statuses_retry_the_same_saved_range_and_complete() {
    for status in [
        "408 Request Timeout",
        "429 Too Many Requests",
        "500 Internal Server Error",
        "503 Service Unavailable",
    ] {
        let fixture = TransferFixture::start(vec![Reply::error(status), Reply::partial(9)]).await;
        std::fs::write(fixture.incomplete(), &BYTES[..9]).unwrap();
        fixture.download(policy(2)).await.unwrap();
        fixture.assert_complete();
        let gets = fixture.gets();
        assert_eq!(
            gets.len(),
            2,
            "{status} must recover within the attempt budget"
        );
        assert!(gets
            .iter()
            .all(|request| request.range.as_deref() == Some("bytes=9-")));
    }
}

#[tokio::test]
async fn oversized_chunked_partial_body_rolls_back_to_the_original_prefix() {
    let mut reply = Reply::partial(9);
    reply.body.push(b'!');
    // Valid bytes and the excess byte use separate HTTP chunks. Even if valid
    // chunks have already reached disk, the invalid attempt must be rolled back.
    let reply = reply.chunked(BYTES.len() - 9, BodyEnding::Complete);
    let fixture = TransferFixture::start(vec![reply]).await;
    std::fs::write(fixture.incomplete(), &BYTES[..9]).unwrap();
    assert!(fixture.download(policy(3)).await.is_err());
    fixture.assert_unpublished();
    assert_eq!(std::fs::read(fixture.incomplete()).unwrap(), &BYTES[..9]);
    assert_eq!(
        fixture.gets().len(),
        1,
        "an oversized body is not transient"
    );
}

#[tokio::test]
async fn short_chunked_partial_body_resumes_after_newly_saved_bytes() {
    for ending in [BodyEnding::Complete, BodyEnding::Interrupted] {
        let mut reply = Reply::partial(9).chunked(4, ending);
        reply.body.truncate(7);
        let fixture = TransferFixture::start(vec![reply, Reply::partial(16)]).await;
        std::fs::write(fixture.incomplete(), &BYTES[..9]).unwrap();
        fixture.download(policy(2)).await.unwrap();
        fixture.assert_complete();
        let gets = fixture.gets();
        assert_eq!(gets.len(), 2);
        assert_eq!(gets[0].range.as_deref(), Some("bytes=9-"));
        assert_eq!(gets[1].range.as_deref(), Some("bytes=16-"));
    }
}

#[tokio::test]
async fn pending_body_exhausts_total_budget_and_flushes_new_partial_bytes() {
    let mut reply = Reply::partial(9).chunked(4, BodyEnding::Pending);
    reply.body.truncate(7);
    let fixture = TransferFixture::start(vec![reply]).await;
    std::fs::write(fixture.incomplete(), &BYTES[..9]).unwrap();
    let mut bounded = policy(3);
    bounded.max_elapsed = Duration::from_millis(500);
    assert!(
        tokio::time::timeout(Duration::from_secs(2), fixture.download(bounded))
            .await
            .expect("the whole-file deadline must also bound a pending body")
            .is_err()
    );
    fixture.assert_unpublished();
    assert_eq!(std::fs::read(fixture.incomplete()).unwrap(), &BYTES[..16]);
    assert_eq!(
        fixture.gets().len(),
        1,
        "retries cannot extend the total budget"
    );
}

#[tokio::test]
async fn zero_elapsed_budget_is_rejected_before_any_request_or_cache_write() {
    let fixture = TransferFixture::start(vec![Reply::full()]).await;
    let mut invalid = policy(3);
    invalid.max_elapsed = Duration::ZERO;
    assert!(matches!(
        fixture.download(invalid).await,
        Err(ferrum_types::FerrumError::Config { .. })
    ));
    assert!(fixture.requests.lock().unwrap().is_empty());
    assert!(!fixture.incomplete().exists());
    fixture.assert_unpublished();
}

#[tokio::test]
async fn backoff_exhausting_total_budget_never_starts_another_request() {
    let fixture = TransferFixture::start(vec![
        Reply::error("503 Service Unavailable"),
        Reply::partial(9),
    ])
    .await;
    std::fs::write(fixture.incomplete(), &BYTES[..9]).unwrap();
    let mut bounded = policy(3);
    bounded.initial_backoff = Duration::from_secs(10);
    bounded.max_elapsed = Duration::from_millis(500);
    assert!(
        tokio::time::timeout(Duration::from_secs(2), fixture.download(bounded))
            .await
            .expect("backoff must not extend the whole-file budget")
            .is_err()
    );
    fixture.assert_unpublished();
    assert_eq!(std::fs::read(fixture.incomplete()).unwrap(), &BYTES[..9]);
    assert_eq!(
        fixture.gets().len(),
        1,
        "no attempt remains after the deadline"
    );
}

#[tokio::test]
async fn retry_and_elapsed_budgets_leave_incomplete_bytes_unpublished() {
    let mut first = Reply::full();
    first.body.truncate(9);
    let fixture = TransferFixture::start(vec![first]).await;
    assert!(fixture.download(policy(2)).await.is_err());
    fixture.assert_unpublished();
    assert_eq!(std::fs::read(fixture.incomplete()).unwrap(), &BYTES[..9]);
    assert_eq!(fixture.gets().len(), 2, "finite attempt budget is enforced");

    let mut delayed = Reply::partial(9);
    delayed.delay = Duration::from_secs(2);
    let fixture = TransferFixture::start(vec![delayed]).await;
    std::fs::write(fixture.incomplete(), &BYTES[..9]).unwrap();
    let mut bounded = policy(3);
    bounded.max_elapsed = Duration::from_millis(100);
    assert!(
        tokio::time::timeout(Duration::from_secs(1), fixture.download(bounded))
            .await
            .expect("whole-file deadline must bound attempts and backoff")
            .is_err()
    );
    fixture.assert_unpublished();
    assert_eq!(std::fs::read(fixture.incomplete()).unwrap(), &BYTES[..9]);
}