incin-core 0.1.0

Core tensor operations and static shape checking for incin
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
use crate::backend_authoring::{Execute, op};
use crate::err::ErrorMessage;
use crate::err::{Error, Result};
use crate::io::ResourceLimits;
use crate::nn::{VisitState, VisitStateMut};
use crate::shapes::ShapeBuf;
use crate::tensor::dtype::{DTypeDescriptor, DTypeId, DTypeKey, DTypeKind, StorageEncoding};
use alloc::collections::{BTreeMap, BTreeSet};
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use std::path::Path;

/// Metadata for an individual parameter stored in a global checkpoint.
#[cfg(feature = "std")]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct CheckpointDType {
    /// Stable logical identity of the dtype.
    pub key: DTypeKey,
    /// Semantic category, retained independently from the logical name.
    pub kind: DTypeKind,
    /// Physical storage/block encoding used by the checkpoint bytes.
    pub encoding: StorageEncoding,
}

#[cfg(feature = "std")]
impl CheckpointDType {
    /// Maps one dtype descriptor onto checkpoint encoding.
    pub fn from_descriptor(dtype: DTypeDescriptor) -> Self {
        Self {
            key: dtype.key(),
            kind: dtype.kind(),
            encoding: dtype.encoding(),
        }
    }

    /// Resolve a manifest dtype into the runtime descriptor after validating
    /// all semantic and physical fields against the built-in registry.
    pub fn descriptor(&self) -> Result<DTypeDescriptor> {
        let descriptor = match self.key.name() {
            "u8" => DTypeId::U8.descriptor(),
            "u32" => DTypeId::U32.descriptor(),
            "i64" => DTypeId::I64.descriptor(),
            "bf16" => DTypeId::BF16.descriptor(),
            "f16" => DTypeId::F16.descriptor(),
            "f32" => DTypeId::F32.descriptor(),
            "f64" => DTypeId::F64.descriptor(),
            "q8_0" => DTypeId::Q8_0.descriptor(),
            "bool" => DTypeId::Bool.descriptor(),
            _ => {
                return Err(Error::Msg(format!(
                    "Unsupported checkpoint dtype key {}:{}:{}",
                    self.key.namespace(),
                    self.key.name(),
                    self.key.version()
                )));
            }
        };
        if descriptor.key() != self.key
            || descriptor.kind() != self.kind
            || descriptor.encoding() != self.encoding
        {
            return Err(Error::Msg(format!(
                "Checkpoint dtype metadata does not match registered dtype {}",
                self.key.name()
            )));
        }
        Ok(descriptor)
    }
}

#[cfg(feature = "std")]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
/// Placement-aware metadata for one saved tensor.
pub struct TensorCheckpointMeta {
    /// State-path name of the tensor.
    pub name: String,
    /// Global logical shape.
    pub global_shape: Vec<usize>,
    /// Explicit semantic and physical dtype record; schema version is independent.
    pub dtype: CheckpointDType,
    /// Placement kind as text.
    pub placement_kind: String,
}

/// The schema version of the sharded-checkpoint manifest.
///
/// Independent of [`crate::serialize::STATE_FORMAT_VERSION`]: the manifest
/// describes topology and global shapes, the state format describes the
/// payload, and either can change without the other.
#[cfg(feature = "std")]
pub const CHECKPOINT_MANIFEST_VERSION: u32 = 1;

/// Global checkpoint manifest recording overall topology and global parameter shapes.
#[cfg(feature = "std")]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct GlobalCheckpointManifest {
    /// Checkpoint envelope format version.
    pub version: u32,
    /// World size that wrote the checkpoint.
    pub world_size: usize,
    /// Tensors by state-path name.
    pub tensors: BTreeMap<String, TensorCheckpointMeta>,
}

#[cfg(feature = "std")]
impl GlobalCheckpointManifest {
    /// Create a new global checkpoint manifest for a specified world size.
    pub fn new(world_size: usize) -> Self {
        Self {
            version: CHECKPOINT_MANIFEST_VERSION,
            world_size,
            tensors: BTreeMap::new(),
        }
    }

    /// Record parameter metadata in the manifest.
    pub fn add_tensor(
        &mut self,
        name: impl Into<String>,
        global_shape: Vec<usize>,
        dtype: impl Into<DTypeDescriptor>,
        placement_kind: impl Into<String>,
    ) {
        let key = name.into();
        self.tensors.insert(
            key.clone(),
            TensorCheckpointMeta {
                name: key,
                global_shape,
                dtype: CheckpointDType::from_descriptor(dtype.into()),
                placement_kind: placement_kind.into(),
            },
        );
    }
}

/// Saves a global checkpoint manifest to a JSON file.
#[cfg(feature = "std")]
pub fn save_checkpoint_manifest<P: AsRef<Path>>(
    manifest: &GlobalCheckpointManifest,
    path: P,
) -> Result<()> {
    let json = serde_json::to_string_pretty(manifest)
        .map_err(|e| Error::Msg(format!("Failed to serialize checkpoint manifest: {}", e)))?;
    std::fs::write(path.as_ref(), json)
        .map_err(|e| Error::Msg(format!("Failed to write checkpoint manifest: {}", e)))?;
    Ok(())
}

/// Loads and validates a global checkpoint manifest from a JSON file.
#[cfg(feature = "std")]
pub fn load_checkpoint_manifest<P: AsRef<Path>>(path: P) -> Result<GlobalCheckpointManifest> {
    let path_ref = path.as_ref();
    let metadata = std::fs::metadata(path_ref)?;
    let limits = ResourceLimits::model_load_defaults();
    if metadata.len() > limits.max_header_bytes {
        return Err(Error::Msg(format!(
            "Checkpoint manifest size {} exceeds header limit {} bytes",
            metadata.len(),
            limits.max_header_bytes
        )));
    }
    let data = std::fs::read_to_string(path_ref)
        .map_err(|e| Error::Msg(format!("Failed to read checkpoint manifest: {}", e)))?;
    let manifest: GlobalCheckpointManifest = serde_json::from_str(&data)
        .map_err(|e| Error::Msg(format!("Failed to parse checkpoint manifest: {}", e)))?;
    // The manifest has carried a version since it was introduced, but nothing
    // read it back, so a newer manifest would have been interpreted under this
    // build's rules instead of refused.
    if manifest.version > CHECKPOINT_MANIFEST_VERSION {
        return Err(Error::Msg(format!(
            "Checkpoint manifest declares version {}, but this build reads at most version {}; \
             upgrade incin to read it",
            manifest.version, CHECKPOINT_MANIFEST_VERSION
        )));
    }
    Ok(manifest)
}

/// Slices a contiguous multidimensional byte array along a target sharded axis for a given rank.
#[cfg(feature = "std")]
pub fn slice_bytes_for_rank(
    bytes: &[u8],
    global_shape: &[usize],
    dtype: DTypeDescriptor,
    shard_axis: usize,
    rank: usize,
    world_size: usize,
) -> Result<(Vec<u8>, Vec<usize>)> {
    if shard_axis >= global_shape.len() {
        return Err(Error::Msg(format!(
            "Shard axis {} out of bounds for shape {:?}",
            shard_axis, global_shape
        )));
    }
    let global_dim = global_shape[shard_axis];
    if world_size == 0 || !global_dim.is_multiple_of(world_size) {
        return Err(Error::Msg(format!(
            "Global dimension {} along axis {} not divisible by world size {}",
            global_dim, shard_axis, world_size
        )));
    }
    if rank >= world_size {
        return Err(Error::Msg(format!(
            "Rank {} out of bounds for world size {}",
            rank, world_size
        )));
    }

    let local_shard_dim = global_dim / world_size;
    let mut local_shape = global_shape.to_vec();
    local_shape[shard_axis] = local_shard_dim;

    let elem_bytes = dtype
        .encoding()
        .scalar_bytes()
        .ok_or_else(|| Error::UnsupportedDType {
            dtype,
            backend: "safetensors",
            op: "shard",
        })?;
    let outer_stride = crate::shapes::ShapeBuf::from_slice(&global_shape[..shard_axis])
        .checked_numel(crate::shapes::error::OperationKind::Storage)?;
    let inner_stride = crate::shapes::ShapeBuf::from_slice(&global_shape[shard_axis + 1..])
        .checked_numel(crate::shapes::error::OperationKind::Storage)?;

    let checked_mul = |lhs: usize, rhs: usize, expression: &'static str| {
        lhs.checked_mul(rhs)
            .ok_or(crate::shapes::error::ShapeError::ArithmeticOverflow {
                operation: crate::shapes::error::OperationKind::Storage,
                expression,
            })
    };
    let elem_per_shard_block = checked_mul(
        local_shard_dim,
        inner_stride,
        "local shard dimension * inner stride",
    )?;
    let bytes_per_shard_block = checked_mul(
        elem_per_shard_block,
        elem_bytes,
        "shard elements * element byte width",
    )?;
    let elem_per_global_block = checked_mul(
        global_dim,
        inner_stride,
        "global shard dimension * inner stride",
    )?;
    let bytes_per_global_block = checked_mul(
        elem_per_global_block,
        elem_bytes,
        "global block elements * element byte width",
    )?;

    let output_bytes = checked_mul(
        outer_stride,
        bytes_per_shard_block,
        "outer stride * shard block bytes",
    )?;
    let mut sliced_bytes = Vec::with_capacity(output_bytes);

    for o in 0..outer_stride {
        let global_offset = checked_mul(
            o,
            bytes_per_global_block,
            "outer index * global block bytes",
        )?
        .checked_add(checked_mul(
            rank,
            bytes_per_shard_block,
            "rank * shard block bytes",
        )?)
        .ok_or(crate::shapes::error::ShapeError::ArithmeticOverflow {
            operation: crate::shapes::error::OperationKind::Storage,
            expression: "global block offset + rank block offset",
        })?;
        let end_offset = global_offset.checked_add(bytes_per_shard_block).ok_or(
            crate::shapes::error::ShapeError::ArithmeticOverflow {
                operation: crate::shapes::error::OperationKind::Storage,
                expression: "shard byte offset + shard byte length",
            },
        )?;
        if end_offset > bytes.len() {
            return Err(Error::Msg(format!(
                "Byte slicing offset {}..{} out of buffer len {}",
                global_offset,
                end_offset,
                bytes.len()
            )));
        }
        sliced_bytes.extend_from_slice(&bytes[global_offset..end_offset]);
    }

    Ok((sliced_bytes, local_shape))
}

/// Parses a safetensors file into backend-neutral owned state.
pub fn load_safetensors_snapshot<P: AsRef<Path>>(path: P) -> Result<crate::nn::StateSnapshot> {
    crate::serialize::deserialize_snapshot_safetensors(path.as_ref())
        .map_err(|e| Error::Msg(format!("Safetensors deserialization failed: {}", e)))
}

/// Parses a safetensors file that was not written by a versioned incin
/// build into backend-neutral owned state.
///
/// [`load_safetensors_snapshot`] refuses a file with no
/// `incin.format.version` metadata key, because that key is incin's own
/// promise about its own envelope and an absent key means the promise was
/// never made. A file produced by another toolchain - most commonly one
/// downloaded from the Hugging Face Hub - never carries that key and is not
/// thereby broken; it simply was not written by incin. Use this entry point
/// for that case. State role metadata (`incin.state.role.<name>`) is still
/// read if present and still defaults to `Parameter` if absent, exactly as
/// [`load_safetensors_snapshot`] does; the only difference is that the
/// version key is not required.
pub fn load_foreign_safetensors_snapshot<P: AsRef<Path>>(
    path: P,
) -> Result<crate::nn::StateSnapshot> {
    crate::serialize::deserialize_snapshot_safetensors_foreign(path.as_ref())
        .map_err(|e| Error::Msg(format!("Safetensors deserialization failed: {}", e)))
}

/// Loads weights into a module from a safetensors file.
pub fn load_safetensors<B, M, P>(module: &mut M, path: P) -> Result<()>
where
    B: crate::tensor::backend::VariableBackend,
    M: VisitState<B> + VisitStateMut<B>,
    P: AsRef<Path>,
{
    let snapshot = load_safetensors_snapshot(path)?;
    crate::nn::load_state::<B, _>(module, &snapshot)
}

/// Saves the module's weights to a safetensors file.
pub fn save_safetensors<B, M, P>(module: &M, path: P) -> Result<()>
where
    B: crate::tensor::backend::VariableBackend,
    M: VisitState<B>,
    P: AsRef<Path>,
{
    let snapshot = crate::nn::collect_state::<B, _>(module)?;
    crate::serialize::serialize_snapshot_safetensors(&snapshot, path.as_ref())
        .map_err(|e| Error::Msg(format!("Safetensors serialization failed: {}", e)))
}

/// Saves a full model checkpoint including global manifest and weights file.
#[cfg(feature = "std")]
pub fn save_checkpoint<B, M, P>(module: &M, dir_path: P, world_size: usize) -> Result<()>
where
    B: crate::tensor::backend::VariableBackend,
    M: VisitState<B>,
    P: AsRef<Path>,
{
    let dir = dir_path.as_ref();
    std::fs::create_dir_all(dir)?;

    let snapshot = crate::nn::collect_state::<B, _>(module)?;

    let mut manifest = GlobalCheckpointManifest::new(world_size);
    for (name, value) in snapshot.iter() {
        manifest.add_tensor(
            name.to_string(),
            value.shape().dims().to_vec(),
            value.dtype(),
            "Local",
        );
    }

    let manifest_path = dir.join("manifest.json");
    save_checkpoint_manifest(&manifest, manifest_path)?;

    let weights_path = dir.join("model.safetensors");
    save_safetensors::<B, M, _>(module, weights_path)?;

    Ok(())
}

/// Loads a checkpoint with explicit cross-mesh resharding for the target rank and topology.
#[cfg(feature = "std")]
pub fn load_resharded_checkpoint<B, M, P>(
    module: &mut M,
    dir_path: P,
    rank: usize,
    target_world_size: usize,
) -> Result<()>
where
    B: crate::tensor::backend::VariableBackend + Execute<op::TensorFromBytes>,
    M: VisitState<B> + VisitStateMut<B>,
    P: AsRef<Path>,
{
    let dir = dir_path.as_ref();
    let manifest_path = dir.join("manifest.json");
    let manifest = load_checkpoint_manifest(&manifest_path)?;

    if rank >= target_world_size {
        return Err(Error::Msg(format!(
            "Target rank {} out of bounds for target world size {}",
            rank, target_world_size
        )));
    }

    let weights_path = dir.join("model.safetensors");
    let checkpoint = crate::serialize::deserialize_snapshot_safetensors(&weights_path)
        .map_err(|e| Error::Msg(format!("Safetensors deserialization failed: {}", e)))?;
    let current = crate::nn::collect_state::<B, _>(module)?;
    let expected_paths: BTreeSet<_> = current.iter().map(|(path, _)| path).collect();
    let checkpoint_paths: BTreeSet<_> = checkpoint.iter().map(|(path, _)| path).collect();
    if expected_paths != checkpoint_paths {
        let missing = expected_paths
            .difference(&checkpoint_paths)
            .map(ToString::to_string)
            .collect::<Vec<_>>();
        let unexpected = checkpoint_paths
            .difference(&expected_paths)
            .map(ToString::to_string)
            .collect::<Vec<_>>();
        return Err(Error::InvalidModuleState {
            operation: "load checkpoint",
            reason: ErrorMessage::new(format!(
                "checkpoint paths differ: missing {:?}, unexpected {:?}",
                missing, unexpected
            )),
        });
    }
    let manifest_paths: BTreeSet<_> = manifest.tensors.keys().map(String::as_str).collect();
    let manifest_expected: BTreeSet<_> = expected_paths.iter().map(|path| path.as_str()).collect();
    if manifest_paths != manifest_expected {
        return Err(Error::InvalidModuleState {
            operation: "load checkpoint",
            reason: ErrorMessage::new("checkpoint manifest paths differ from model state"),
        });
    }
    let mut resharded = crate::nn::StateSnapshot::new();

    for (name, current_value) in current.iter() {
        let st_value = checkpoint.get(name).ok_or_else(|| {
            Error::Msg(format!(
                "Parameter {} missing from checkpoint safetensors: {:?}",
                name, "missing"
            ))
        })?;

        let meta = manifest
            .tensors
            .get(name.as_str())
            .ok_or_else(|| Error::Msg(format!("Parameter {} missing from manifest", name)))?;

        let global_shape = st_value.shape().dims().to_vec();
        let target_shape = current_value.shape().dims();
        let bytes = st_value.bytes();
        let dtype_desc = meta.dtype.descriptor()?;
        if global_shape != meta.global_shape {
            return Err(Error::InvalidModuleState {
                operation: "load checkpoint",
                reason: ErrorMessage::new(format!(
                    "manifest shape {:?} does not match checkpoint shape {:?} for {}",
                    meta.global_shape, global_shape, name
                )),
            });
        }
        if st_value.dtype() != dtype_desc {
            return Err(Error::InvalidModuleState {
                operation: "load checkpoint",
                reason: ErrorMessage::new(format!(
                    "manifest dtype {} does not match checkpoint dtype {} for {}",
                    dtype_desc.name(),
                    st_value.dtype().name(),
                    name
                )),
            });
        }

        let (final_bytes, final_shape) = if global_shape == target_shape {
            (bytes.to_vec(), global_shape)
        } else {
            let mut diff_axes = Vec::new();
            for (idx, (&g_dim, &t_dim)) in global_shape.iter().zip(target_shape.iter()).enumerate()
            {
                if g_dim != t_dim {
                    diff_axes.push(idx);
                }
            }
            if diff_axes.len() != 1 || global_shape.len() != target_shape.len() {
                return Err(Error::Msg(format!(
                    "Incompatible global shape {:?} and target local shape {:?} for parameter {}",
                    global_shape, target_shape, name
                )));
            }
            let shard_axis = diff_axes[0];
            let g_dim = global_shape[shard_axis];
            let t_dim = target_shape[shard_axis];

            if g_dim % t_dim != 0 || g_dim / t_dim != target_world_size {
                return Err(Error::Msg(format!(
                    "Global dim {} on axis {} incompatible with target dim {} and world size {} for parameter {}",
                    g_dim, shard_axis, t_dim, target_world_size, name
                )));
            }

            slice_bytes_for_rank(
                bytes,
                &global_shape,
                dtype_desc,
                shard_axis,
                rank,
                target_world_size,
            )?
        };

        if final_shape.as_slice() != target_shape {
            return Err(Error::Msg(format!(
                "Resharded shape {:?} does not match target shape {:?} for parameter {}",
                final_shape, target_shape, name
            )));
        }

        let path = crate::nn::StatePath::new(name.as_str())
            .map_err(|e| Error::Msg(format!("Invalid checkpoint path {}: {}", name, e)))?;
        let value = crate::nn::StateValue::new(
            ShapeBuf::from_slice(&final_shape),
            dtype_desc,
            final_bytes,
            st_value.role(),
        )?;
        resharded.insert(path, value)?;
    }

    crate::nn::load_state::<B, _>(module, &resharded)?;
    Ok(())
}

#[cfg(test)]
mod tests {
    // Integration tests for checkpoint save, load, and resharding are located in `crates/incin-core/tests/checkpoint_reshard.rs`.
}