| import os |
| |
| pwd = os.getcwd() |
| if pwd.endswith("tests"): |
| os.chdir(os.path.dirname(pwd)) |
|
|
| import time |
| import torch |
| import numpy as np |
| from marble.tasks.Chords1217._datamodule_v1 import Chords1217AudioTrain |
| from torch.utils.data import DataLoader |
|
|
| def benchmark_get_targets_single_clip( |
| jsonl_path: str, |
| sample_rate: int, |
| channels: int, |
| clip_seconds: float, |
| label_freq: int, |
| mode: str, |
| num_files: int = 20 |
| ): |
| """ |
| 对 Chords1217AudioTrain 在指定 mode 下,测量前 num_files 个文件的第一个 slice (slice_idx=0) |
| 调用 get_targets 的总耗时。仅供对比三种实现(python/numpy/numba)哪种最慢。 |
| """ |
| print(f"\n--- Benchmark get_targets mode='{mode}' (仅前 {num_files} 个文件,每个文件的第 0 号 slice) ---") |
| |
| dataset = Chords1217AudioTrain( |
| jsonl=jsonl_path, |
| sample_rate=sample_rate, |
| channels=channels, |
| clip_seconds=clip_seconds, |
| label_freq=label_freq, |
| mode=mode |
| ) |
|
|
| |
| orig_sr = sample_rate |
| orig_clip_frames = int(clip_seconds * sample_rate) |
|
|
| t0 = time.perf_counter() |
| for file_idx in range(min(num_files, len(dataset.chords_meta))): |
| |
| _ = dataset.get_targets( |
| file_idx=file_idx, |
| slice_idx=0, |
| orig_sr=orig_sr, |
| orig_clip_frames=orig_clip_frames |
| ) |
| t1 = time.perf_counter() |
| total_time = t1 - t0 |
| count = min(num_files, len(dataset.chords_meta)) |
| print(f"Mode='{mode}',总耗时:{total_time:.4f}s,平均每个文件第 0 号 slice get_targets 用时 {total_time/count:.4f}s") |
|
|
|
|
| def benchmark_get_targets_full_epoch( |
| jsonl_path: str, |
| sample_rate: int, |
| channels: int, |
| clip_seconds: float, |
| label_freq: int, |
| mode: str, |
| batch_size: int = 8, |
| num_workers: int = 4 |
| ): |
| """ |
| 通过 DataLoader 对整个 train split 迭代一遍,测量整个 epoch 调用 get_targets + collate 的时间。 |
| """ |
| print(f"\n--- Benchmark get_targets mode='{mode}' (使用 DataLoader 遍历整个 train split)---") |
| train_ds = Chords1217AudioTrain( |
| jsonl=jsonl_path, |
| sample_rate=sample_rate, |
| channels=channels, |
| clip_seconds=clip_seconds, |
| label_freq=label_freq, |
| mode=mode |
| ) |
|
|
| loader = DataLoader( |
| train_ds, |
| batch_size=batch_size, |
| shuffle=False, |
| num_workers=num_workers, |
| pin_memory=True, |
| prefetch_factor=2, |
| persistent_workers=True |
| ) |
|
|
| t0 = time.perf_counter() |
| for batch in loader: |
| |
| pass |
| t1 = time.perf_counter() |
| print(f"Mode='{mode}',整 train split 迭代一轮 get_targets 耗时:{t1-t0:.4f}s") |
|
|
|
|
| def benchmark_load_and_preprocess_single_clip( |
| jsonl_path: str, |
| sample_rate: int, |
| channels: int, |
| clip_seconds: float, |
| label_freq: int, |
| mode: str, |
| num_files: int = 20 |
| ): |
| """ |
| 针对 Chords1217AudioTrain,在指定 mode 下,测量前 num_files 个文件的第一个 slice (slice_idx=0) |
| 调用 _load_and_preprocess 的总耗时。仅供对比三种实现(python/numpy/numba)在纯加载+预处理上的差异。 |
| """ |
| print(f"\n--- Benchmark load_and_preprocess mode='{mode}' (仅前 {num_files} 个文件,第 0 号 slice) ---") |
| |
| dataset = Chords1217AudioTrain( |
| jsonl=jsonl_path, |
| sample_rate=sample_rate, |
| channels=channels, |
| clip_seconds=clip_seconds, |
| label_freq=label_freq, |
| mode=mode |
| ) |
|
|
| |
| orig_sr = sample_rate |
| orig_clip_frames = int(clip_seconds * sample_rate) |
|
|
| t0 = time.perf_counter() |
| for file_idx in range(min(num_files, len(dataset.meta))): |
| info = dataset.meta[file_idx] |
| audio_path = info["audio_path"] |
| |
| _ = dataset._load_and_preprocess( |
| path=audio_path, |
| slice_idx=0, |
| orig_sr=orig_sr, |
| orig_clip_frames=orig_clip_frames |
| ) |
| t1 = time.perf_counter() |
| total_time = t1 - t0 |
| count = min(num_files, len(dataset.meta)) |
| print(f"Mode='{mode}',总耗时:{total_time:.4f}s,平均每个文件第 0 号 slice _load_and_preprocess 用时 {total_time/count:.4f}s") |
|
|
|
|
| def benchmark_load_and_preprocess_full_epoch( |
| jsonl_path: str, |
| sample_rate: int, |
| channels: int, |
| clip_seconds: float, |
| label_freq: int, |
| mode: str, |
| batch_size: int = 8, |
| num_workers: int = 4 |
| ): |
| """ |
| 通过 DataLoader 遍历整个 train split,但在 collate 阶段只收集 waveform,不调用 get_targets, |
| 仅测 _load_and_preprocess 的耗时。 |
| """ |
| print(f"\n--- Benchmark load_and_preprocess mode='{mode}' (使用 DataLoader 遍历 entire train split,仅 _load_and_preprocess) ---") |
| train_ds = Chords1217AudioTrain( |
| jsonl=jsonl_path, |
| sample_rate=sample_rate, |
| channels=channels, |
| clip_seconds=clip_seconds, |
| label_freq=label_freq, |
| mode=mode |
| ) |
|
|
| |
| def collate_waveforms(batch): |
| |
| |
| waveforms = [item[0] for item in batch] |
| return torch.stack(waveforms, dim=0) |
|
|
| loader = DataLoader( |
| train_ds, |
| batch_size=batch_size, |
| shuffle=False, |
| num_workers=num_workers, |
| collate_fn=collate_waveforms, |
| pin_memory=True, |
| prefetch_factor=2, |
| persistent_workers=True |
| ) |
|
|
| t0 = time.perf_counter() |
| for _ in loader: |
| |
| pass |
| t1 = time.perf_counter() |
| print(f"Mode='{mode}',整 train split 迭代一轮 _load_and_preprocess 耗时:{t1-t0:.4f}s") |
|
|
|
|
| if __name__ == '__main__': |
| |
| JSONL_PATH = 'data/Chords1217/Chords1217.train.jsonl' |
| SAMPLE_RATE = 24000 |
| CHANNELS = 1 |
| CLIP_SECONDS = 15.0 |
| LABEL_FREQ = 75 |
| NUM_FILES = 500 |
| BATCH_SIZE = 64 |
| NUM_WORKERS = 4 |
|
|
| |
| |
| |
| for m in ['python', 'numpy', 'numba']: |
| benchmark_get_targets_single_clip( |
| jsonl_path=JSONL_PATH, |
| sample_rate=SAMPLE_RATE, |
| channels=CHANNELS, |
| clip_seconds=CLIP_SECONDS, |
| label_freq=LABEL_FREQ, |
| mode=m, |
| num_files=NUM_FILES |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| for m in ['python', 'numpy', 'numba']: |
| benchmark_load_and_preprocess_single_clip( |
| jsonl_path=JSONL_PATH, |
| sample_rate=SAMPLE_RATE, |
| channels=CHANNELS, |
| clip_seconds=CLIP_SECONDS, |
| label_freq=LABEL_FREQ, |
| mode=m, |
| num_files=NUM_FILES |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|