use crate::{Float, Result};
pub struct ModelPartition {
pub id: usize,
pub layer_start: usize,
pub layer_end: usize,
}
impl ModelPartition {
pub fn new(id: usize, layer_start: usize, layer_end: usize) -> Self {
Self {
id,
layer_start,
layer_end,
}
}
pub fn num_layers(&self) -> usize {
self.layer_end - self.layer_start
}
}
pub fn partition_model(num_layers: usize, num_partitions: usize) -> Result<Vec<ModelPartition>> {
let layers_per_partition = (num_layers + num_partitions - 1) / num_partitions;
let mut partitions = Vec::with_capacity(num_partitions);
for id in 0..num_partitions {
let start = id * layers_per_partition;
let end = ((id + 1) * layers_per_partition).min(num_layers);
partitions.push(ModelPartition::new(id, start, end));
}
Ok(partitions)
}