daniilrobnikov / vits2

VITS2: Improving Quality and Efficiency of Single-Stage Text-to-Speech with Adversarial Learning and Architecture Design
https://vits-2.github.io/demo/
MIT License
503 stars 53 forks source link

DistributedBucketSampler division by zero problem #4

Open y1guo opened 1 year ago

y1guo commented 1 year ago

In the DistributedBucketSampler class, it's possible that the first bucket has no elements and thus results in a Division by zero error.

class DistributedBucketSampler(torch.utils.data.distributed.DistributedSampler):

    # ...

    def _create_buckets(self):
        buckets = [[] for _ in range(len(self.boundaries) - 1)]
        for i in range(len(self.lengths)):
            length = self.lengths[i]
            idx_bucket = self._bisect(length)
            if idx_bucket != -1:
                buckets[idx_bucket].append(i)

        # for i in range(len(buckets) - 1, 0, -1): 
        for i in range(len(buckets) - 1, -1, -1):  # here: changed the stopping index to -1 to allow pop(0)
            if len(buckets[i]) == 0:
                buckets.pop(i)
                self.boundaries.pop(i + 1)

        num_samples_per_bucket = []
        for i in range(len(buckets)):
            len_bucket = len(buckets[i])
            total_batch_size = self.num_replicas * self.batch_size
            rem = (total_batch_size - (len_bucket % total_batch_size)) % total_batch_size
            num_samples_per_bucket.append(len_bucket + rem)
        return buckets, num_samples_per_bucket

    # ...

Glad to discuss :)

daniilrobnikov commented 1 year ago

Thanks for pointing that out, I will research into that.

Actually, I had a idea to change the sampler technique for the future release