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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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
# coding: utf-8
__author__ = "Roman Solovyev (ZFTurbo): https://github.com/ZFTurbo/"

import argparse
import os
import time
import warnings
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, Union

import librosa
import numpy as np
import soundfile as sf
import torch
import torch.distributed as dist
from ml_collections import ConfigDict
from tqdm.auto import tqdm
from utils.audio_utils import (
    denormalize_audio,
    draw_2_mel_spectrogram,
    normalize_audio,
    read_audio_transposed,
)
from utils.metrics import get_metrics
from utils.model_utils import (
    apply_tta,
    demix,
    load_start_checkpoint,
    prefer_target_instrument,
)
from utils.settings import (
    get_model_from_config,
    logging,
    parse_args_valid,
    write_results_in_file,
)

warnings.filterwarnings("ignore")


def get_mixture_paths(

    args: argparse.Namespace, verbose: bool, config: ConfigDict, extension: str

) -> List[str]:
    """

    Collect validation mixture file paths from one or more root directories.



    Scans each directory in `args.valid_path` for files matching the pattern

    `<root>/*/mixture.<extension>` and returns a sorted list of absolute paths.

    In Distributed Data Parallel (DDP) runs, status messages are printed only

    on rank 0; otherwise they are printed unconditionally when `verbose=True`.



    Args:

        args (argparse.Namespace): Arguments with `valid_path` (str or List[str])

            specifying root directories to search.

        verbose (bool): If True, print collection details and summary.

        config (ConfigDict): Configuration used for informational logging

            (e.g., `inference.num_overlap`, `inference.batch_size`).

        extension (str): Audio file extension to match (with or without a leading dot).



    Returns:

        List[str]: Sorted list of discovered mixture file paths.

    """

    ddp_mode = dist.is_initialized()
    should_print = (not ddp_mode) or (dist.get_rank() == 0)

    # --- read & normalize args.valid_path ---
    try:
        valid_path = args.valid_path
    except Exception as e:
        if should_print:
            print("No valid path in args")
        raise e

    if isinstance(valid_path, str):
        valid_paths: List[str] = [valid_path]
    else:
        valid_paths = list(valid_path)

    # --- collect mixture files ---
    all_mixtures_path: List[str] = []

    def find_mixture_files(root_dir):
        from pathlib import Path

        root_path = Path(root_dir)
        wav_files = list(root_path.rglob("mixture.wav"))
        flac_files = list(root_path.rglob("mixture.flac"))
        if extension not in ["wav", "flac"]:
            ext_file = list(root_path.rglob(f"mixture.{extension}"))
        else:
            ext_file = []
        return wav_files + flac_files + ext_file

    for root in valid_paths:
        part = find_mixture_files(root)
        if not part and verbose and should_print:
            print(f"No validation data found in: {root}")
        all_mixtures_path.extend(part)

    # --- verbose summary ---
    if verbose and should_print:
        # be robust to dict-like or attribute-like config
        inference = getattr(config, "inference", None)
        if inference is None and isinstance(config, dict):
            inference = config.get("inference", None)

        def _get(obj, name, default=None):
            if obj is None:
                return default
            if isinstance(obj, dict):
                return obj.get(name, default)
            return getattr(obj, name, default)

        num_overlap = _get(inference, "num_overlap", "?")
        batch_size = _get(inference, "batch_size", "?")

        print(f"Total mixtures: {len(all_mixtures_path)}")
        print(f"Overlap: {num_overlap} Batch size: {batch_size}")

    return all_mixtures_path


def update_metrics_and_pbar(

    track_metrics: Dict[str, float],

    all_metrics: Dict[str, Dict[str, Union[Dict[str, float], List[float]]]],

    instr: str,

    pbar_dict: Dict[str, float],

    mixture_paths: Optional[Union[List[str], tqdm]],

    verbose: bool = False,

    path: Optional[str] = None,

) -> None:
    """

    Update accumulated metrics and (optionally) a tqdm progress bar.



    In non-DDP runs, appends each metric value to `all_metrics[metric_name][instr]`

    (a list). In DDP runs (when `torch.distributed` is initialized), stores values

    as `all_metrics[metric_name][instr][path]` (a dict keyed by file `path`);

    therefore `path` must be provided under DDP. When `verbose=True`, metric

    values are printed only on rank 0. Also updates `pbar_dict` and, if a tqdm

    instance is provided, calls `set_postfix` for live display.



    Args:

        track_metrics (Dict[str, float]): Mapping from metric name to its value for

            the current track/instrument.

        all_metrics (Dict[str, Dict[str, Union[Dict[str, float], List[float]]]]):

            Aggregator for all collected metrics, organized as

            `{metric_name: {instrument: list_or_dict}}`, where the inner container

            is a list (non-DDP) or dict keyed by `path` (DDP).

        instr (str): Instrument name associated with the current metrics.

        pbar_dict (Dict[str, float]): Dictionary holding the latest values to show

            in the tqdm postfix (updated in place).

        mixture_paths (Optional[Union[List[str], tqdm]]): If a tqdm progress bar is

            supplied, its `set_postfix` is called with `pbar_dict`.

        verbose (bool, optional): If True, print metric updates (rank 0 only in DDP).

            Defaults to False.

        path (Optional[str], optional): File path key required in DDP mode to index

            per-track metrics for `instr`. Ignored in non-DDP. Defaults to None.



    Returns:

        None

    """

    ddp_mode = dist.is_initialized()
    should_print = (not ddp_mode) or (dist.get_rank() == 0)

    if ddp_mode and path is None:
        raise ValueError(
            "`path` must be provided when torch.distributed is initialized."
        )

    for metric_name, metric_value in track_metrics.items():
        if verbose and should_print:
            print(f"Metric {metric_name:11s} value: {metric_value:.4f}")

        if metric_name not in all_metrics:
            all_metrics[metric_name] = {}
        if instr not in all_metrics[metric_name]:
            all_metrics[metric_name][instr] = {} if ddp_mode else []

        if ddp_mode:
            all_metrics[metric_name][instr][path] = metric_value  # type: ignore[index]
        else:
            all_metrics[metric_name][instr].append(metric_value)  # type: ignore[union-attr]

        pbar_dict[f"{metric_name}_{instr}"] = metric_value

    if mixture_paths is not None and hasattr(mixture_paths, "set_postfix"):
        try:
            mixture_paths.set_postfix(pbar_dict)
        except Exception:
            pass


def process_audio_files(

    mixture_paths: List[str],

    model: torch.nn.Module,

    args: Any,

    config: ConfigDict,

    device: torch.device,

    verbose: bool = False,

    is_tqdm: bool = True,

) -> Dict[str, Dict[str, Union[Dict[str, float], List[float]]]]:
    """

    Run source separation on a list of mixtures and collect evaluation metrics.



    Performs optional resampling and normalization, demixes each track (with

    optional Test-Time Augmentation), saves separated stems (FLAC PCM_16 when

    peak ≤ 1.0 else WAV FLOAT), optionally renders spectrograms, computes the

    requested metrics, and aggregates them in a nested dictionary.



    In non-DDP runs, metrics are stored as lists:

        {metric_name: {instrument: [values...]}}

    In DDP runs (when `torch.distributed` is initialized), metrics are stored as

    dicts keyed by the track path:

        {metric_name: {instrument: {path: value, ...}}}



    Args:

        mixture_paths (List[str]): Absolute or relative paths to `mixture.<ext>` files.

        model (torch.nn.Module): Trained separator model in eval mode.

        args (Any): Runtime arguments (e.g., `metrics`, `model_type`, `use_tta`,

            `store_dir`, `draw_spectro`, `extension`).

        config (ConfigDict): Configuration with audio/inference/training settings

            (e.g., `audio.sample_rate`, `inference.batch_size`, `inference.num_overlap`,

            `inference.normalize`, `training.instruments`).

        device (torch.device): Device for inference (CPU/CUDA).

        verbose (bool, optional): Print per-track details and timings. Defaults to False.

        is_tqdm (bool, optional): Show a tqdm progress bar (rank 0 only under DDP). Defaults to True.



    Returns:

        Dict[str, Dict[str, Union[Dict[str, float], List[float]]]]: Aggregated metrics

        per metric and instrument; inner container is a list (non-DDP) or a dict keyed

        by track path (DDP).

    """

    ddp_mode = dist.is_initialized()
    should_print = (not ddp_mode) or (dist.get_rank() == 0)

    instruments = prefer_target_instrument(config)
    use_tta = getattr(args, "use_tta", False)
    store_dir = getattr(args, "store_dir", "")

    # extension is used only for reading GT stems; outputs use FLAC/WAV rule unconditionally
    if "inference" in config and "extension" in config["inference"]:
        extension = config["inference"]["extension"]
    else:
        extension = getattr(args, "extension", "wav")

    # --- init metrics container ---
    if ddp_mode:
        # behave like first: dict of dicts
        all_metrics: Dict[str, Dict[str, Dict]] = {
            metric: {instr: {} for instr in config.training.instruments}
            for metric in args.metrics
        }
    else:
        # behave like second: dict of lists
        all_metrics: Dict[str, Dict[str, List[float]]] = {
            metric: {instr: [] for instr in config.training.instruments}
            for metric in args.metrics
        }

    # --- tqdm wrapping as requested ---
    if is_tqdm and should_print:
        mixture_paths = tqdm(mixture_paths)

    def get_instruments(path: str) -> dict[str, str]:
        """Detect available instrument files and their extensions."""
        real_instruments: dict[str, str] = {}

        for instr in instruments:
            # Check supported extensions for each instrument
            for ext in [extension, "flac", "wav"]:
                file_path = Path(path) / f"{instr}.{ext}"
                if file_path.exists():
                    real_instruments[instr] = ext
                    break

        return real_instruments

    for path in mixture_paths:
        start_time = time.time()
        mix, sr = read_audio_transposed(path)
        mix_orig = mix.copy()
        folder = os.path.dirname(path)
        real_instruments = get_instruments(folder)
        # resample input to config SR if needed
        if "audio" in config and "sample_rate" in config.audio:
            target_sr = config.audio["sample_rate"]
            if sr != target_sr:
                orig_length = mix.shape[-1]
                if verbose and should_print:
                    print(
                        f"Warning: sample rate is different. In config: {target_sr} in file {path}: {sr}"
                    )
                mix = librosa.resample(
                    mix, orig_sr=sr, target_sr=target_sr, res_type="kaiser_best"
                )

        if verbose and should_print:
            print(f"Song: {os.path.abspath(folder)} Shape: {mix.shape}")

        # optional normalize
        if "inference" in config and config.inference.get("normalize", False):
            mix, norm_params = normalize_audio(mix)
        else:
            norm_params = None

        waveforms_orig = demix(
            config, model, mix.copy(), device, model_type=args.model_type
        )

        if use_tta:
            waveforms_orig = apply_tta(
                config, model, mix, waveforms_orig, device, args.model_type
            )

        pbar_dict = {}

        for instr, extension in real_instruments.items():
            if verbose and should_print:
                print(f"Instr: {instr}")

            # read GT track
            if instr != "other" or not getattr(config.training, "other_fix", False):
                track, sr1 = read_audio_transposed(
                    f"{folder}/{instr}.{extension}", instr, skip_err=True
                )
                if track is None:
                    continue
            else:
                # other = mix - vocals
                track, sr1 = read_audio_transposed(f"{folder}/vocals.{extension}")
                track = mix_orig - track

            estimates = waveforms_orig[instr]

            # back-resample estimates to original SR if input was resampled
            if "audio" in config and "sample_rate" in config.audio:
                target_sr = config.audio["sample_rate"]
                if sr != target_sr:
                    estimates = librosa.resample(
                        estimates,
                        orig_sr=target_sr,
                        target_sr=sr,
                        res_type="kaiser_best",
                    )
                    estimates = librosa.util.fix_length(estimates, size=orig_length)

            # denormalize if needed
            if (
                norm_params is not None
                and "inference" in config
                and config.inference.get("normalize", False)
            ):
                estimates = denormalize_audio(estimates, norm_params)

            # --- saving (uniform rule) ---
            if store_dir:
                os.makedirs(store_dir, exist_ok=True)
                base = f"{store_dir}/{os.path.basename(folder)}_{instr}"
                peak = float(np.abs(estimates).max())
                if peak <= 1.0:
                    out_path = f"{base}.flac"
                    sf.write(out_path, estimates.T, sr, subtype="PCM_16")
                else:
                    out_path = f"{base}.wav"
                    sf.write(out_path, estimates.T, sr, subtype="FLOAT")

                draw_spec = getattr(args, "draw_spectro", 0)
                if draw_spec and draw_spec > 0:
                    draw_2_mel_spectrogram(estimates.T, track.T, sr, draw_spec, base)

            # --- metrics ---
            k = config.training.get("k_sdr", 10)
            track_metrics = get_metrics(
                args.metrics, track, estimates, mix_orig, device=device, k=k
            )

            # --- update metrics + progress ---
            if ddp_mode:
                # behave like first: include path in call
                update_metrics_and_pbar(
                    track_metrics,
                    all_metrics,
                    instr,
                    pbar_dict,
                    mixture_paths=mixture_paths,
                    verbose=verbose and should_print,
                    path=path,
                )
            else:
                # behave like second: no path argument
                update_metrics_and_pbar(
                    track_metrics,
                    all_metrics,
                    instr,
                    pbar_dict,
                    mixture_paths=mixture_paths,
                    verbose=verbose and should_print,
                )

        if verbose and should_print:
            print(f"Time for song: {time.time() - start_time:.2f} sec")

    return all_metrics


def compute_metric_avg(

    store_dir: str,

    args,

    instruments: List[str],

    config: ConfigDict,

    all_metrics: Dict[str, Dict[str, Union[List[float], Dict[str, float]]]],

    start_time: float,

) -> Dict[str, float]:
    """

    Compute average metrics across instruments (DDP-aware) and optionally log to file.



    For each metric, computes the mean value per instrument from its collected values

    (list in non-DDP, or dict-of-{path: value} in DDP), sums these instrument means,

    and divides by `len(instruments)` to obtain the final average (legacy behavior).

    Prints/logs only on rank 0 when `torch.distributed` is initialized; if `store_dir`

    is non-empty, writes a `results.txt` with logs.



    Args:

        store_dir (str): Directory to write `results.txt` when logging is enabled.

        args: Run arguments included in the log header when `store_dir` is provided.

        instruments (List[str]): Instruments to include in the averaging.

        config (ConfigDict): Config used for informational logging (e.g., overlap).

        all_metrics (Dict[str, Dict[str, Union[List[float], Dict[str, float]]]]):

            Nested metrics container:

              - non-DDP: {metric: {instrument: [values...]}}

              - DDP:     {metric: {instrument: {path: value, ...}}}

        start_time (float): Timestamp for reporting elapsed time.



    Returns:

        Dict[str, float]: Mapping from metric name to its average over instruments.

    """

    ddp_mode = dist.is_initialized()
    should_print = (not ddp_mode) or (dist.get_rank() == 0)

    logs: List[str] = []
    verbose_logging = bool(store_dir) and should_print
    if verbose_logging:
        logs.append(str(args))

    logs = logging(
        logs,
        text=f"Num overlap: {config.inference.num_overlap}",
        verbose_logging=verbose_logging,
    )

    metric_sum: Dict[str, float] = {}

    for instr in instruments:
        for metric_name in all_metrics:
            per_instr_container = all_metrics[
                metric_name
            ]  # dict: instr -> (list | dict[path->val])

            values_obj = (
                per_instr_container.get(instr, [])
                if isinstance(per_instr_container, dict)
                else []
            )
            if isinstance(values_obj, dict):
                vals = list(values_obj.values())
            else:
                vals = list(values_obj)

            arr = np.asarray(vals, dtype=float)
            if arr.size == 0:
                mean_val = float("nan")
                std_val = float("nan")
            else:
                mean_val = float(arr.mean())
                std_val = float(arr.std())

            logs = logging(
                logs,
                text=f"Instr {instr} {metric_name}: {mean_val:.4f} (Std: {std_val:.4f})",
                verbose_logging=verbose_logging,
            )

            metric_sum[metric_name] = metric_sum.get(metric_name, 0.0) + mean_val

    metric_avg: Dict[str, float] = {}
    denom = max(len(instruments), 1)
    for metric_name in all_metrics:
        metric_avg[metric_name] = metric_sum.get(metric_name, float("nan")) / denom

    if len(instruments) > 1:
        for metric_name, avg in metric_avg.items():
            logs = logging(
                logs,
                text=f"Metric avg {metric_name:11s}: {avg:.4f}",
                verbose_logging=verbose_logging,
            )

    logs = logging(
        logs,
        text=f"Elapsed time: {time.time() - start_time:.2f} sec",
        verbose_logging=verbose_logging,
    )

    if store_dir:
        write_results_in_file(store_dir, logs)

    return metric_avg


def valid(

    model: torch.nn.Module,

    args,

    config: ConfigDict,

    device: torch.device,

    verbose: bool = False,

) -> Tuple[dict, dict]:
    """

    Validate a trained model on a set of audio mixtures and compute metrics.



    This function performs validation by separating audio sources from mixtures,

    computing evaluation metrics, and optionally saving results to a file.



    Parameters:

    ----------

    model : torch.nn.Module

        The trained model for source separation.

    args : Namespace

        Command-line arguments or equivalent object containing configurations.

    config : dict

        Configuration dictionary with model and processing parameters.

    device : torch.device

        The device (CPU or CUDA) to run the model on.

    verbose : bool, optional

        If True, enables verbose output during processing. Default is False.



    Returns:

    -------

    dict

        A dictionary of average metrics across all instruments.

    """

    start_time = time.time()
    model.eval().to(device)

    # dir to save files, if empty no saving
    store_dir = getattr(args, "store_dir", "")
    # codec to save files
    if "extension" in config["inference"]:
        extension = config["inference"]["extension"]
    else:
        extension = getattr(args, "extension", "wav")

    all_mixtures_path = get_mixture_paths(args, verbose, config, extension)
    all_metrics = process_audio_files(
        all_mixtures_path, model, args, config, device, verbose, not verbose
    )
    instruments = prefer_target_instrument(config)

    return compute_metric_avg(
        store_dir, args, instruments, config, all_metrics, start_time
    ), all_metrics


def validate_in_subprocess(

    proc_id: int,

    queue: torch.multiprocessing.Queue,

    all_mixtures_path: List[str],

    model: torch.nn.Module,

    args,

    config: ConfigDict,

    device: str,

    return_dict,

) -> None:
    """

    Perform validation on a subprocess with multi-processing support. Each process handles inference on a subset of the mixture files

    and updates the shared metrics dictionary.



    Parameters:

    ----------

    proc_id : int

        The process ID (used to assign metrics to the correct key in `return_dict`).

    queue : torch.multiprocessing.Queue

        Queue to receive paths to the mixture files for processing.

    all_mixtures_path : List[str]

        List of paths to the mixture files to be processed.

    model : torch.nn.Module

        The model to be used for inference.

    args : dict

        Dictionary containing various argument configurations (e.g., metrics to calculate).

    config : ConfigDict

        Configuration object containing model settings and training parameters.

    device : str

        The device to use for inference (e.g., 'cpu', 'cuda:0').

    return_dict : torch.multiprocessing.Manager().dict

        Shared dictionary to store the results from each process.



    Returns:

    -------

    None

        The function modifies the `return_dict` in place, but does not return any value.

    """

    m1 = model.eval().to(device)
    if proc_id == 0:
        progress_bar = tqdm(total=len(all_mixtures_path))

    # Initialize metrics dictionary
    all_metrics = {
        metric: {instr: [] for instr in config.training.instruments}
        for metric in args.metrics
    }

    while True:
        current_step, path = queue.get()
        if path is None:  # check for sentinel value
            break
        single_metrics = process_audio_files(
            [path], m1, args, config, device, False, False
        )
        pbar_dict = {}
        for instr in config.training.instruments:
            for metric_name in all_metrics:
                all_metrics[metric_name][instr] += single_metrics[metric_name][instr]
                if len(single_metrics[metric_name][instr]) > 0:
                    pbar_dict[f"{metric_name}_{instr}"] = (
                        f"{single_metrics[metric_name][instr][0]:.4f}"
                    )
        if proc_id == 0:
            progress_bar.update(current_step - progress_bar.n)
            progress_bar.set_postfix(pbar_dict)
        # print(f"Inference on process {proc_id}", all_sdr)
    return_dict[proc_id] = all_metrics
    return


def run_parallel_validation(

    verbose: bool,

    all_mixtures_path: List[str],

    config: ConfigDict,

    model: torch.nn.Module,

    device_ids: List[int],

    args,

    return_dict,

) -> None:
    """

    Run parallel validation using multiple processes. Each process handles a subset of the mixture files and computes the metrics.

    The results are stored in a shared dictionary.



    Parameters:

    ----------

    verbose : bool

        Flag to print detailed information about the validation process.

    all_mixtures_path : List[str]

        List of paths to the mixture files to be processed.

    config : ConfigDict

        Configuration object containing model settings and validation parameters.

    model : torch.nn.Module

        The model to be used for inference.

    device_ids : List[int]

        List of device IDs (for multi-GPU setups) to use for validation.

    args : dict

        Dictionary containing various argument configurations (e.g., metrics to calculate).



    Returns:

    -------

        A shared dictionary containing the validation metrics from all processes.

    """

    model = model.to("cpu")
    try:
        # For multiGPU training extract single model
        model = model.module
    except:
        pass

    queue = torch.multiprocessing.Queue()
    processes = []

    for i, device in enumerate(device_ids):
        if torch.cuda.is_available():
            device = f"cuda:{device}"
        else:
            device = "cpu"
        p = torch.multiprocessing.Process(
            target=validate_in_subprocess,
            args=(
                i,
                queue,
                all_mixtures_path,
                model,
                args,
                config,
                device,
                return_dict,
            ),
        )
        p.start()
        processes.append(p)
    for i, path in enumerate(all_mixtures_path):
        queue.put((i, path))
    for _ in range(len(device_ids)):
        queue.put((None, None))  # sentinel value to signal subprocesses to exit
    for p in processes:
        p.join()  # wait for all subprocesses to finish

    return


def block_bounds(num_tracks: int, world_size: int, rank: int) -> Tuple[int, int]:
    """

    Split a dataset of `num_tracks` items into `world_size` equal contiguous blocks

    and return the half-open interval [start, end) assigned to the given `rank`.



    This function enforces exact divisibility: `num_tracks` must be divisible

    by `world_size`, otherwise a ValueError is raised.



    Args:

        num_tracks (int): Total number of items to split (must be ≥ 0).

        world_size (int): Number of workers to divide the items into (must be > 0).

        rank (int): Zero-based worker index (0 ≤ rank < world_size).



    Returns:

        Tuple[int, int]: A pair `(start, end)` defining the block of indices for this rank.



    Raises:

        ValueError: If `num_tracks` is not divisible by `world_size`.



    Example:

         [block_bounds(12, 4, r) for r in range(4)]

        [(0, 3), (3, 6), (6, 9), (9, 12)]



         block_bounds(8, 2, 1)

        (4, 8)



         block_bounds(10, 3, 0)

        Traceback (most recent call last):

        ...

        ValueError: n (10) must be divisible by world_size (3)

    """
    if num_tracks % world_size != 0:
        raise ValueError(
            f"n ({num_tracks}) must be divisible by world_size ({world_size})"
        )

    chunk = num_tracks // world_size
    start = rank * chunk
    end = start + chunk
    return start, end


def valid_multi_gpu(

    model: torch.nn.Module,

    args,

    config: ConfigDict,

    device_ids: Optional[List[int]] = None,

    verbose: bool = False,

) -> Tuple[Dict[str, float], Dict]:
    """

    Validate a separator model across multiple GPUs with a unified API.



    Runs validation either in Distributed Data Parallel (DDP) mode—detected via

    `torch.distributed.is_initialized()`—or, if DDP is not active, via

    multi-processing / single-GPU execution using the provided `device_ids`.

    Collects per-track metrics, aggregates them into per-instrument/per-metric

    arrays, and computes per-metric averages.



    Behavior:

      * DDP mode: splits the dataset across ranks and gathers metrics; only rank 0

        returns results, while other ranks return `(None, None)`.

      * Non-DDP: launches parallel workers when `len(device_ids) > 1`, otherwise

        runs on a single device/CPU.



    Args:

        model (torch.nn.Module): Trained model to evaluate.

        args: Runtime arguments (e.g., metrics list, store dir).

        config (ConfigDict): Configuration with inference/training settings.

        device_ids (Optional[List[int]]): GPU device IDs for non-DDP parallelism.

            If None or length is 1, runs on a single device.

        verbose (bool, optional): If True, print progress/logs. Defaults to False.



    Returns:

        Tuple[Dict[str, float], Dict]: A pair `(metric_avg, all_metrics)` where

            - `metric_avg` maps metric name to its average score,

            - `all_metrics` is a nested dict `{metric: {instrument: List[float]}}`.

          In DDP mode, non-zero ranks return `(None, None)`.

    """

    start_time = time.time()

    inference = getattr(config, "inference", None)
    if inference is None and isinstance(config, dict):
        inference = config.get("inference", {})
    extension = getattr(inference, "extension", None)
    if extension is None:
        if isinstance(inference, dict):
            extension = inference.get("extension", getattr(args, "extension", "wav"))
        else:
            extension = getattr(args, "extension", "wav")

    all_mixtures_path = get_mixture_paths(args, verbose, config, extension)

    ddp_mode = dist.is_initialized()

    if ddp_mode:
        rank = dist.get_rank()
        world_size = dist.get_world_size()

        device = torch.device(f"cuda:{rank}")
        model.to(device)
        model.eval()

        num_tracks = len(all_mixtures_path)
        pad_needed = (-num_tracks) % world_size
        if pad_needed and num_tracks > 0:
            all_mixtures_path += all_mixtures_path[:pad_needed]
        padded_num_tracks = len(all_mixtures_path)
        target_len = padded_num_tracks // world_size
        start, end = block_bounds(padded_num_tracks, world_size, rank)
        per_rank_data = all_mixtures_path[start:end]

        local_metrics = {
            metric: {instr: [] for instr in config.training.instruments}
            for metric in args.metrics
        }

        with torch.no_grad():
            single_metrics = process_audio_files(
                per_rank_data, model, args, config, device, verbose=verbose
            )
            for instr in config.training.instruments:
                for metric_name in args.metrics:
                    local_metrics[metric_name][instr] = single_metrics[metric_name][
                        instr
                    ]

        all_metrics: Dict[str, Dict[str, List[float]]] = {m: {} for m in args.metrics}
        for metric in args.metrics:
            for instr in config.training.instruments:
                all_metrics[metric][instr] = []
                per_instr = local_metrics[metric][instr]
                if isinstance(per_instr, dict):
                    local_data = list(per_instr.values())
                else:
                    local_data = list(per_instr)

                if len(local_data) == 0:
                    local_tensor = torch.zeros(
                        target_len, dtype=torch.float32, device=device
                    )
                else:
                    if len(local_data) < target_len:
                        local_data = local_data + [0.0] * (target_len - len(local_data))
                    local_tensor = torch.tensor(
                        local_data, dtype=torch.float32, device=device
                    )

                gathered_list = [
                    torch.zeros_like(local_tensor) for _ in range(world_size)
                ]
                dist.all_gather(gathered_list, local_tensor)

                cat_vals = torch.cat(gathered_list).tolist()[:num_tracks]
                all_metrics[metric][instr] = cat_vals

        if dist.get_rank() == 0:
            instruments = prefer_target_instrument(config)
            metric_avg = compute_metric_avg(
                getattr(args, "store_dir", ""),
                args,
                instruments,
                config,
                all_metrics,
                start_time,
            )
            return metric_avg, all_metrics

        return None, None

    # Not DDP
    store_dir = getattr(args, "store_dir", "")

    return_dict = torch.multiprocessing.Manager().dict()
    run_parallel_validation(
        verbose, all_mixtures_path, config, model, device_ids, args, return_dict
    )

    all_metrics: Dict[str, Dict[str, List[float]]] = {m: {} for m in args.metrics}
    for metric in args.metrics:
        for instr in config.training.instruments:
            merged: List[float] = []
            for i in range(len(device_ids)):
                merged += return_dict[i][metric][instr]
            all_metrics[metric][instr] = merged

    instruments = prefer_target_instrument(config)
    metric_avg = compute_metric_avg(
        store_dir, args, instruments, config, all_metrics, start_time
    )
    return metric_avg, all_metrics


def check_validation(dict_args):
    args = parse_args_valid(dict_args)
    torch.backends.cudnn.benchmark = True
    try:
        torch.multiprocessing.set_start_method("spawn")
    except Exception:
        pass
    model, config = get_model_from_config(args.model_type, args.config_path)
    if "model_type" in config.training:
        args.model_type = config.training.model_type
    if args.start_check_point:
        checkpoint = torch.load(
            args.start_check_point, weights_only=False, map_location="cpu"
        )
        load_start_checkpoint(args, model, checkpoint, type_="valid")
    if args.lora_checkpoint_peft:
        from peft import PeftModel

        model = PeftModel.from_pretrained(model, args.lora_checkpoint_peft)
        model = model.merge_and_unload()
    print(f"Instruments: {config.training.instruments}")

    device_ids = args.device_ids
    if torch.cuda.is_available():
        device = torch.device(f"cuda:{device_ids[0]}")
    else:
        device = "cpu"
        print("CUDA is not available. Run validation on CPU. It will be very slow...")

    if torch.cuda.is_available() and len(device_ids) > 1:
        valid_multi_gpu(model, args, config, device_ids, verbose=False)
    else:
        valid(model, args, config, device, verbose=True)


if __name__ == "__main__":
    check_validation(None)