1515//! conversion.
1616
1717use std:: {
18+ cell:: Cell ,
1819 collections:: { BTreeMap , BTreeSet } ,
1920 fs:: { self , File , OpenOptions } ,
20- io:: Write as _ ,
21+ io:: { self , Write } ,
2122 path:: { Path , PathBuf } ,
2223} ;
2324
@@ -30,7 +31,7 @@ use super::{
3031 reader:: { self , Metadata } ,
3132 schema:: { self , Catalog , wire} ,
3233} ;
33- use crate :: { Result , runtime_package:: wire as runtime} ;
34+ use crate :: { Result , runtime_package:: wire as runtime, startup :: StartupProgress } ;
3435use ring:: digest:: { Context as DigestContext , SHA256 } ;
3536
3637/// Container form of a payload-format package.
@@ -145,12 +146,24 @@ fn packed_tensor_for(
145146/// for a ready cache entry holding a subset. Unselected payloads may be absent;
146147/// signed bodies and locations for them are preserved when present.
147148pub fn pack_selected ( source : & Path , output : & Path , selected : Option < & [ usize ] > ) -> Result < ( ) > {
149+ pack_with ( source, output, selected, & StartupProgress :: silent ( ) )
150+ }
151+
152+ /// Packs the selected deployments, reporting verification, writing, and publication phases.
153+ pub fn pack_with (
154+ source : & Path ,
155+ output : & Path ,
156+ selected : Option < & [ usize ] > ,
157+ progress : & StartupProgress ,
158+ ) -> Result < ( ) > {
159+ let phase = progress. phase ( "Verify package" , None ) ;
148160 let verified = Verified :: open ( source, selected) ?;
149161 if !directory_form ( source, & verified. metadata ) {
150162 return Err ( invalid ( "package is already in packed form" ) ) ;
151163 }
152164 let payloads = verified. payloads ( ) ?;
153- payloads. verify ( ) ?;
165+ phase. finish ( "signatures" ) ;
166+ verify_stored_data ( & payloads, progress) ?;
154167
155168 let mut locations = Vec :: with_capacity ( verified. locations . locations . len ( ) ) ;
156169 let mut packed = Vec :: new ( ) ;
@@ -202,15 +215,33 @@ pub fn pack_selected(source: &Path, output: &Path, selected: Option<&[usize]>) -
202215 . open ( & staging)
203216 . map_err ( |e| invalid ( format ! ( "create packed package: {e}" ) ) ) ?;
204217 let guard = TempFile :: new ( & staging) ;
205- let mut writer = Writer :: new ( file, verified. metadata . primary ( ) . format ( ) ) ?;
206- for name in verified. metadata . primary ( ) . tensor_names ( ) {
207- if name == reader:: LOCATIONS || name. starts_with ( container:: ALIGNMENT_PREFIX ) {
208- continue ;
209- }
218+ let locations = rewritten. encode_to_vec ( ) ;
219+ let primary: Vec < & str > = verified
220+ . metadata
221+ . primary ( )
222+ . tensor_names ( )
223+ . filter ( |name| * name != reader:: LOCATIONS && !name. starts_with ( container:: ALIGNMENT_PREFIX ) )
224+ . collect ( ) ;
225+ let mut total = locations. len ( ) as u64 ;
226+ for name in & primary {
227+ total = add_bytes ( total, verified. metadata . primary ( ) . tensor ( name) ?. len ( ) ) ?;
228+ }
229+ for ( _, id) in & packed {
230+ total = add_bytes ( total, payloads. get ( id) ?. len ( ) ) ?;
231+ }
232+ let phase = progress. phase ( "Write packed package" , Some ( total) ) ;
233+ let written = Cell :: new ( 0u64 ) ;
234+ let mut writer = Writer :: new (
235+ Reporting :: new ( file, |bytes| {
236+ written. set ( written. get ( ) + bytes) ;
237+ phase. advance_bytes ( bytes) ;
238+ } ) ,
239+ verified. metadata . primary ( ) . format ( ) ,
240+ ) ?;
241+ for name in primary {
210242 let slice = verified. metadata . primary ( ) . tensor ( name) ?;
211243 writer. add ( name, slice. len ( ) , Source :: Slice ( slice) ) ?;
212244 }
213- let locations = rewritten. encode_to_vec ( ) ;
214245 writer. add (
215246 reader:: LOCATIONS ,
216247 locations. len ( ) as u64 ,
@@ -221,24 +252,80 @@ pub fn pack_selected(source: &Path, output: &Path, selected: Option<&[usize]>) -
221252 writer. add ( & name, slice. len ( ) , Source :: Slice ( slice) ) ?;
222253 }
223254 writer. finish ( ) ?;
255+ phase. finish ( format ! ( "{} bytes written" , written. get( ) ) ) ;
256+ let phase = progress. phase ( "Publish packed package" , None ) ;
224257 publish_staged_file ( & staging, output) ?;
225258 guard. keep ( ) ;
259+ phase. finish ( output. display ( ) . to_string ( ) ) ;
226260 Ok ( ( ) )
227261}
228262
263+ fn add_bytes ( total : u64 , bytes : u64 ) -> Result < u64 > {
264+ total
265+ . checked_add ( bytes)
266+ . ok_or_else ( || invalid ( "package byte count overflow" ) )
267+ }
268+
269+ /// Hashes every stored payload as one phase whose detail counts the bytes read.
270+ fn verify_stored_data ( payloads : & reader:: PayloadFiles , progress : & StartupProgress ) -> Result < ( ) > {
271+ let total = payloads
272+ . iter ( )
273+ . try_fold ( 0u64 , |total, ( _, slice) | add_bytes ( total, slice. len ( ) ) ) ?;
274+ let phase = progress. phase ( "Verify stored data" , Some ( total) ) ;
275+ payloads. verify_with_progress ( & |bytes| phase. advance_bytes ( bytes) ) ?;
276+ phase. finish ( format ! ( "{total} source bytes verified" ) ) ;
277+ Ok ( ( ) )
278+ }
279+
280+ /// Forwards writes to `inner` and reports the count of each write.
281+ struct Reporting < W : Write , F : FnMut ( u64 ) > {
282+ inner : W ,
283+ report : F ,
284+ }
285+
286+ impl < W : Write , F : FnMut ( u64 ) > Reporting < W , F > {
287+ fn new ( inner : W , report : F ) -> Self {
288+ Self { inner, report }
289+ }
290+ }
291+
292+ impl < W : Write , F : FnMut ( u64 ) > Write for Reporting < W , F > {
293+ fn write ( & mut self , buffer : & [ u8 ] ) -> io:: Result < usize > {
294+ let count = self . inner . write ( buffer) ?;
295+ ( self . report ) ( count as u64 ) ;
296+ Ok ( count)
297+ }
298+
299+ fn flush ( & mut self ) -> io:: Result < ( ) > {
300+ self . inner . flush ( )
301+ }
302+ }
303+
229304/// Rewrites a packed package as a directory with deterministic companions.
230305pub fn unpack ( source : & Path , output : & Path ) -> Result < ( ) > {
231306 unpack_selected ( source, output, None )
232307}
233308
234309/// Unpacks only the closure of the selected deployments.
235310pub fn unpack_selected ( source : & Path , output : & Path , selected : Option < & [ usize ] > ) -> Result < ( ) > {
311+ unpack_with ( source, output, selected, & StartupProgress :: silent ( ) )
312+ }
313+
314+ /// Unpacks the selected deployments, reporting verification, writing, and publication phases.
315+ pub fn unpack_with (
316+ source : & Path ,
317+ output : & Path ,
318+ selected : Option < & [ usize ] > ,
319+ progress : & StartupProgress ,
320+ ) -> Result < ( ) > {
321+ let phase = progress. phase ( "Verify package" , None ) ;
236322 let verified = Verified :: open ( source, selected) ?;
237323 if directory_form ( source, & verified. metadata ) {
238324 return Err ( invalid ( "package is already in directory form" ) ) ;
239325 }
240326 let payloads = verified. payloads ( ) ?;
241- payloads. verify ( ) ?;
327+ phase. finish ( "signatures" ) ;
328+ verify_stored_data ( & payloads, progress) ?;
242329
243330 let mut locations = Vec :: with_capacity ( verified. locations . locations . len ( ) ) ;
244331 let mut companions = Vec :: new ( ) ;
@@ -285,30 +372,52 @@ pub fn unpack_selected(source: &Path, output: &Path, selected: Option<&[usize]>)
285372 let staging = unique_staging ( output, "unpack" ) ?;
286373 fs:: create_dir ( & staging) . map_err ( |e| invalid ( format ! ( "create package staging: {e}" ) ) ) ?;
287374 let result = ( || {
375+ let locations = rewritten. encode_to_vec ( ) ;
376+ let primary_names: Vec < & str > = verified
377+ . metadata
378+ . primary ( )
379+ . tensor_names ( )
380+ . filter ( |name| * name != reader:: LOCATIONS && primary_payload ( name) )
381+ . collect ( ) ;
382+ let mut total = locations. len ( ) as u64 ;
383+ for ( _, _, slice) in & companions {
384+ total = add_bytes ( total, slice. len ( ) ) ?;
385+ }
386+ for name in & primary_names {
387+ total = add_bytes ( total, verified. metadata . primary ( ) . tensor ( name) ?. len ( ) ) ?;
388+ }
389+ let phase = progress. phase ( "Write package companions" , Some ( total) ) ;
390+ let written = Cell :: new ( 0u64 ) ;
288391 for ( _tensor, relative, slice) in & companions {
289392 container:: write_payload_companion ( & staging, relative, slice) ?;
393+ written. set ( written. get ( ) + slice. len ( ) ) ;
394+ phase. advance_bytes ( slice. len ( ) ) ;
290395 }
291396 let primary = staging. join ( "model.safetensors" ) ;
292397 let file = OpenOptions :: new ( )
293398 . write ( true )
294399 . create_new ( true )
295400 . open ( & primary)
296401 . map_err ( |e| invalid ( format ! ( "create package primary: {e}" ) ) ) ?;
297- let mut writer = Writer :: new ( file, verified. metadata . primary ( ) . format ( ) ) ?;
298- for name in verified. metadata . primary ( ) . tensor_names ( ) {
299- if name == reader:: LOCATIONS || !primary_payload ( name) {
300- continue ;
301- }
402+ let mut writer = Writer :: new (
403+ Reporting :: new ( file, |bytes| {
404+ written. set ( written. get ( ) + bytes) ;
405+ phase. advance_bytes ( bytes) ;
406+ } ) ,
407+ verified. metadata . primary ( ) . format ( ) ,
408+ ) ?;
409+ for name in primary_names {
302410 let slice = verified. metadata . primary ( ) . tensor ( name) ?;
303411 writer. add ( name, slice. len ( ) , Source :: Slice ( slice) ) ?;
304412 }
305- let locations = rewritten. encode_to_vec ( ) ;
306413 writer. add (
307414 reader:: LOCATIONS ,
308415 locations. len ( ) as u64 ,
309416 Source :: Bytes ( locations) ,
310417 ) ?;
311418 writer. finish ( ) ?;
419+ phase. finish ( format ! ( "{} bytes written" , written. get( ) ) ) ;
420+ let phase = progress. phase ( "Publish package directory" , None ) ;
312421 container:: sync_file ( & primary)
313422 . map_err ( |e| invalid ( format ! ( "sync package primary: {e}" ) ) ) ?;
314423 // Companion entries must be durable before the directory becomes visible,
@@ -321,7 +430,9 @@ pub fn unpack_selected(source: &Path, output: &Path, selected: Option<&[usize]>)
321430 invalid ( format ! ( "publish package directory: {e}" ) )
322431 }
323432 } ) ?;
324- sync_parent ( output)
433+ sync_parent ( output) ?;
434+ phase. finish ( output. display ( ) . to_string ( ) ) ;
435+ Ok ( ( ) )
325436 } ) ( ) ;
326437 if result. is_err ( ) {
327438 let _ = fs:: remove_dir_all ( & staging) ;
@@ -4343,6 +4454,37 @@ mod tests {
43434454 assert ! ( rank. compressed_prepared_weights. is_none( ) ) ;
43444455 }
43454456
4457+ #[ test]
4458+ fn reporting_writer_forwards_bytes_and_reports_each_write ( ) {
4459+ let mut written = 0u64 ;
4460+ let mut sink = Vec :: new ( ) ;
4461+ {
4462+ let mut writer = Reporting :: new ( & mut sink, |count| written += count) ;
4463+ writer. write_all ( b"abc" ) . unwrap ( ) ;
4464+ writer. write_all ( b"defgh" ) . unwrap ( ) ;
4465+ writer. flush ( ) . unwrap ( ) ;
4466+ }
4467+ assert_eq ! ( sink, b"abcdefgh" ) ;
4468+ assert_eq ! ( written, 8 ) ;
4469+ }
4470+
4471+ #[ test]
4472+ fn reported_pack_and_unpack_write_the_same_bytes_as_the_silent_forms ( ) {
4473+ let scratch = Scratch :: new ( "reported-roundtrip" ) ;
4474+ let fixture = fixture ( & scratch) ;
4475+ let silent = scratch. join ( "silent.safetensors" ) ;
4476+ pack ( & fixture. directory , & silent) . unwrap ( ) ;
4477+ let reported = scratch. join ( "reported.safetensors" ) ;
4478+ pack_with ( & fixture. directory , & reported, None , & StartupProgress :: silent ( ) ) . unwrap ( ) ;
4479+ assert_eq ! ( fs:: read( & silent) . unwrap( ) , fs:: read( & reported) . unwrap( ) ) ;
4480+
4481+ let unpacked = scratch. join ( "unpacked" ) ;
4482+ unpack_with ( & reported, & unpacked, None , & StartupProgress :: silent ( ) ) . unwrap ( ) ;
4483+ let again = scratch. join ( "repacked.safetensors" ) ;
4484+ pack ( & unpacked, & again) . unwrap ( ) ;
4485+ assert_eq ! ( fs:: read( & silent) . unwrap( ) , fs:: read( & again) . unwrap( ) ) ;
4486+ }
4487+
43464488 #[ test]
43474489 fn out_of_bounds_recipe_is_rejected_before_any_publication ( ) {
43484490 let scratch = Scratch :: new ( "recipe-bounds" ) ;
0 commit comments