style: cargo fmt --workspace (whitespace/wrapping only, no semantic change)
Whole-workspace rustfmt pass picked up while iterating on Mamba GPU backward work. Verified formatting-only via diff sampling; no logic changed. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
@@ -87,12 +87,12 @@ pub struct MultiScaleRandomCrop {
|
||||
}
|
||||
|
||||
impl MultiScaleRandomCrop {
|
||||
pub fn new(
|
||||
target_size: usize,
|
||||
scale_range: (f32, f32),
|
||||
ratio_range: (f32, f32),
|
||||
) -> Self {
|
||||
Self { target_size, scale_range, ratio_range }
|
||||
pub fn new(target_size: usize, scale_range: (f32, f32), ratio_range: (f32, f32)) -> Self {
|
||||
Self {
|
||||
target_size,
|
||||
scale_range,
|
||||
ratio_range,
|
||||
}
|
||||
}
|
||||
|
||||
/// Crop and bilinear-resize a single `ImageRecord`.
|
||||
@@ -149,8 +149,7 @@ impl MultiScaleRandomCrop {
|
||||
for ox in 0..ts {
|
||||
// Map output pixel to source pixel in the crop region
|
||||
// Using half-pixel convention for better edge behaviour
|
||||
let src_y_f =
|
||||
(oy as f32 + 0.5) / ts as f32 * crop_h as f32 - 0.5 + crop_top as f32;
|
||||
let src_y_f = (oy as f32 + 0.5) / ts as f32 * crop_h as f32 - 0.5 + crop_top as f32;
|
||||
let src_x_f =
|
||||
(ox as f32 + 0.5) / ts as f32 * crop_w as f32 - 0.5 + crop_left as f32;
|
||||
|
||||
@@ -174,8 +173,7 @@ impl MultiScaleRandomCrop {
|
||||
let p01 = image.get(y0c, x1c, ch);
|
||||
let p10 = image.get(y1c, x0c, ch);
|
||||
let p11 = image.get(y1c, x1c, ch);
|
||||
out[base + ch] =
|
||||
p00 * (1.0 - dy) * (1.0 - dx)
|
||||
out[base + ch] = p00 * (1.0 - dy) * (1.0 - dx)
|
||||
+ p01 * (1.0 - dy) * dx
|
||||
+ p10 * dy * (1.0 - dx)
|
||||
+ p11 * dy * dx;
|
||||
@@ -390,9 +388,7 @@ impl InMemoryShard {
|
||||
|
||||
for i in 0..n {
|
||||
let pixel_count = image_size * image_size * 3;
|
||||
let pixels: Vec<f32> = (0..pixel_count)
|
||||
.map(|_| lcg_f32(&mut lcg))
|
||||
.collect();
|
||||
let pixels: Vec<f32> = (0..pixel_count).map(|_| lcg_f32(&mut lcg)).collect();
|
||||
|
||||
records.push(ImageRecord {
|
||||
pixels,
|
||||
@@ -696,14 +692,20 @@ impl DatasetStats {
|
||||
let avg_context = if masks.is_empty() {
|
||||
0.0
|
||||
} else {
|
||||
masks.iter().map(|m| m.context_indices.len() as f64).sum::<f64>()
|
||||
masks
|
||||
.iter()
|
||||
.map(|m| m.context_indices.len() as f64)
|
||||
.sum::<f64>()
|
||||
/ masks.len() as f64
|
||||
};
|
||||
|
||||
let avg_target = if masks.is_empty() {
|
||||
0.0
|
||||
} else {
|
||||
masks.iter().map(|m| m.all_target_indices.len() as f64).sum::<f64>()
|
||||
masks
|
||||
.iter()
|
||||
.map(|m| m.all_target_indices.len() as f64)
|
||||
.sum::<f64>()
|
||||
/ masks.len() as f64
|
||||
};
|
||||
|
||||
@@ -759,8 +761,7 @@ impl WebDatasetShard {
|
||||
/// Read the shard's `.tar` (or gzip-compressed `.tar.gz`/`.tgz`)
|
||||
/// archive from disk and decode its records into an [`InMemoryShard`].
|
||||
pub fn load(&self) -> Result<InMemoryShard, String> {
|
||||
let (raw_records, _stats) =
|
||||
read_webdataset_shard(std::path::Path::new(&self.path))?;
|
||||
let (raw_records, _stats) = read_webdataset_shard(std::path::Path::new(&self.path))?;
|
||||
let records: Vec<ImageRecord> = raw_records
|
||||
.into_iter()
|
||||
.map(webdataset_record_to_image)
|
||||
@@ -825,7 +826,11 @@ impl ShuffleBuffer {
|
||||
/// Create a new shuffle buffer seeded from `capacity`.
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
let rng_state = (capacity as u64).wrapping_mul(6364136223846793005);
|
||||
Self { capacity, buffer: Vec::new(), rng_state }
|
||||
Self {
|
||||
capacity,
|
||||
buffer: Vec::new(),
|
||||
rng_state,
|
||||
}
|
||||
}
|
||||
|
||||
/// Insert a record. If the buffer is not full, push it; otherwise replace
|
||||
@@ -941,10 +946,7 @@ pub fn parse_tar_bytes(data: &[u8]) -> Vec<WebDatasetRecord> {
|
||||
|
||||
// Derive extension and key from the filename.
|
||||
// Filenames may include a directory prefix; take the basename.
|
||||
let basename = filename
|
||||
.rsplit('/')
|
||||
.next()
|
||||
.unwrap_or(&filename);
|
||||
let basename = filename.rsplit('/').next().unwrap_or(&filename);
|
||||
|
||||
let (stem, ext) = if let Some(dot) = basename.rfind('.') {
|
||||
(&basename[..dot], &basename[dot + 1..])
|
||||
@@ -977,7 +979,12 @@ pub fn parse_tar_bytes(data: &[u8]) -> Vec<WebDatasetRecord> {
|
||||
let s = std::str::from_utf8(bytes).ok()?.trim().to_owned();
|
||||
s.parse::<usize>().ok()
|
||||
});
|
||||
Some(WebDatasetRecord { key, image_bytes, label, extension })
|
||||
Some(WebDatasetRecord {
|
||||
key,
|
||||
image_bytes,
|
||||
label,
|
||||
extension,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -995,8 +1002,8 @@ pub fn read_webdataset_shard(
|
||||
|
||||
let t0 = Instant::now();
|
||||
|
||||
let mut file = std::fs::File::open(path)
|
||||
.map_err(|e| format!("cannot open {}: {e}", path.display()))?;
|
||||
let mut file =
|
||||
std::fs::File::open(path).map_err(|e| format!("cannot open {}: {e}", path.display()))?;
|
||||
|
||||
let mut data = Vec::new();
|
||||
file.read_to_end(&mut data)
|
||||
@@ -1045,9 +1052,7 @@ fn webdataset_record_to_image(rec: WebDatasetRecord) -> ImageRecord {
|
||||
use image::io::Reader as ImageReader;
|
||||
use std::io::Cursor;
|
||||
|
||||
if let Ok(reader) = ImageReader::new(Cursor::new(&rec.image_bytes))
|
||||
.with_guessed_format()
|
||||
{
|
||||
if let Ok(reader) = ImageReader::new(Cursor::new(&rec.image_bytes)).with_guessed_format() {
|
||||
if let Ok(img) = reader.decode() {
|
||||
// Resize to 224×224 and convert to RGB
|
||||
let rgb = img
|
||||
@@ -1103,10 +1108,7 @@ impl JepaDataPipeline {
|
||||
// Validate all paths exist before loading anything.
|
||||
for p in &shard_paths {
|
||||
if !p.exists() {
|
||||
return Err(format!(
|
||||
"shard path does not exist: {}",
|
||||
p.display()
|
||||
));
|
||||
return Err(format!("shard path does not exist: {}", p.display()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1239,10 +1241,7 @@ impl JepaDataPipeline {
|
||||
/// Downloads are sequential (one shard at a time).
|
||||
///
|
||||
/// Returns `Err` if any URL fails to download or parse.
|
||||
pub fn from_urls(
|
||||
config: JepaDataConfig,
|
||||
urls: Vec<String>,
|
||||
) -> Result<Self, String> {
|
||||
pub fn from_urls(config: JepaDataConfig, urls: Vec<String>) -> Result<Self, String> {
|
||||
// Validate all URLs before downloading
|
||||
for url in &urls {
|
||||
if !url.starts_with("http://") && !url.starts_with("https://") {
|
||||
@@ -1298,13 +1297,22 @@ mod tests {
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
fn make_record(w: usize, h: usize, label: Option<usize>) -> ImageRecord {
|
||||
let pixels: Vec<f32> = (0..h * w * 3)
|
||||
.map(|i| (i % 256) as f32 / 255.0)
|
||||
.collect();
|
||||
ImageRecord { pixels, width: w, height: h, channels: 3, label, key: "test".into() }
|
||||
let pixels: Vec<f32> = (0..h * w * 3).map(|i| (i % 256) as f32 / 255.0).collect();
|
||||
ImageRecord {
|
||||
pixels,
|
||||
width: w,
|
||||
height: h,
|
||||
channels: 3,
|
||||
label,
|
||||
key: "test".into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn make_pipeline(n_shards: usize, imgs_per_shard: usize, batch_size: usize) -> JepaDataPipeline {
|
||||
fn make_pipeline(
|
||||
n_shards: usize,
|
||||
imgs_per_shard: usize,
|
||||
batch_size: usize,
|
||||
) -> JepaDataPipeline {
|
||||
let shards: Vec<InMemoryShard> = (0..n_shards)
|
||||
.map(|id| InMemoryShard::synthetic(imgs_per_shard, 32, id))
|
||||
.collect();
|
||||
@@ -1425,7 +1433,10 @@ mod tests {
|
||||
let std = [0.229, 0.224, 0.225];
|
||||
JepaAugmentationPipeline::normalize(&mut pixels, mean, std);
|
||||
// 0.5 normalized should differ from 0.5
|
||||
assert!((pixels[0] - 0.5).abs() > 1e-4, "normalization must change values");
|
||||
assert!(
|
||||
(pixels[0] - 0.5).abs() > 1e-4,
|
||||
"normalization must change values"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1588,7 +1599,11 @@ mod tests {
|
||||
fn test_batch_context_target_non_overlapping() {
|
||||
let mut pipeline = make_pipeline(1, 8, 4);
|
||||
let batch = pipeline.next_batch().unwrap();
|
||||
for (ctx, tgt) in batch.context_patch_indices.iter().zip(batch.all_target_indices.iter()) {
|
||||
for (ctx, tgt) in batch
|
||||
.context_patch_indices
|
||||
.iter()
|
||||
.zip(batch.all_target_indices.iter())
|
||||
{
|
||||
for &ci in ctx {
|
||||
assert!(!tgt.contains(&ci), "context index {ci} in target set");
|
||||
}
|
||||
@@ -1620,7 +1635,10 @@ mod tests {
|
||||
let batch = pipeline.next_batch().unwrap();
|
||||
for ctx in &batch.context_patch_indices {
|
||||
for w in ctx.windows(2) {
|
||||
assert!(w[0] < w[1], "context indices must be sorted and deduplicated");
|
||||
assert!(
|
||||
w[0] < w[1],
|
||||
"context indices must be sorted and deduplicated"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1662,7 +1680,10 @@ mod tests {
|
||||
let pipeline = make_pipeline(1, 8, 4);
|
||||
let stats = DatasetStats::compute(&pipeline);
|
||||
assert!(stats.mask_efficiency > 0.0, "mask_efficiency should be > 0");
|
||||
assert!(stats.mask_efficiency <= 1.0, "mask_efficiency should be <= 1");
|
||||
assert!(
|
||||
stats.mask_efficiency <= 1.0,
|
||||
"mask_efficiency should be <= 1"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1805,8 +1826,7 @@ mod tests {
|
||||
let fake_jpg = b"\xFF\xD8\xFF\xE0fake jpeg content";
|
||||
let tar = make_test_tar(&[("000000", fake_jpg, Some(11))]);
|
||||
|
||||
let mut encoder =
|
||||
flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
encoder.write_all(&tar).expect("gzip write");
|
||||
let gz = encoder.finish().expect("gzip finish");
|
||||
|
||||
@@ -1868,7 +1888,10 @@ mod tests {
|
||||
let tar = make_test_tar(&[("000000", fake_jpg, None)]);
|
||||
let records = parse_tar_bytes(&tar);
|
||||
assert_eq!(records.len(), 1);
|
||||
assert_eq!(records[0].label, None, "image with no .cls should have label=None");
|
||||
assert_eq!(
|
||||
records[0].label, None,
|
||||
"image with no .cls should have label=None"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1894,9 +1917,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_parse_large_file() {
|
||||
// 50 000 bytes > 512*97 = 49664, spans many blocks
|
||||
let big_image: Vec<u8> = (0..50_000u32)
|
||||
.map(|i| (i % 251) as u8)
|
||||
.collect();
|
||||
let big_image: Vec<u8> = (0..50_000u32).map(|i| (i % 251) as u8).collect();
|
||||
let tar = make_test_tar(&[("000000", &big_image, Some(7))]);
|
||||
let records = parse_tar_bytes(&tar);
|
||||
assert_eq!(records.len(), 1);
|
||||
@@ -1955,7 +1976,11 @@ mod tests {
|
||||
}
|
||||
// Drain more than available
|
||||
let batch = buf.drain_batch(100);
|
||||
assert_eq!(batch.len(), 3, "drain should return at most what's in the buffer");
|
||||
assert_eq!(
|
||||
batch.len(),
|
||||
3,
|
||||
"drain should return at most what's in the buffer"
|
||||
);
|
||||
assert_eq!(buf.len(), 0);
|
||||
|
||||
// Drain zero
|
||||
@@ -2100,10 +2125,8 @@ mod tests {
|
||||
#[test]
|
||||
fn test_from_urls_bad_scheme_err() {
|
||||
let config = JepaDataConfig::default();
|
||||
let result = JepaDataPipeline::from_urls(
|
||||
config,
|
||||
vec!["ftp://example.com/shard.tar".to_string()],
|
||||
);
|
||||
let result =
|
||||
JepaDataPipeline::from_urls(config, vec!["ftp://example.com/shard.tar".to_string()]);
|
||||
assert!(result.is_err());
|
||||
let msg = match result {
|
||||
Err(e) => e,
|
||||
|
||||
Reference in New Issue
Block a user