Skip to content

Commit

Permalink
Add "trng" support to mux() as well
Browse files Browse the repository at this point in the history
  • Loading branch information
pzelasko committed Dec 28, 2023
1 parent d1a8137 commit dbc6f48
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lhotse/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
extension_contains,
open_best,
)
from lhotse.utils import Pathlike, fastcopy, is_module_available, streaming_shuffle
from lhotse.utils import (
Pathlike,
build_rng,
fastcopy,
is_module_available,
streaming_shuffle,
)

T = TypeVar("T")

Expand Down Expand Up @@ -360,7 +366,7 @@ def __init__(
assert len(self.iterators) == len(self.weights)

def __iter__(self):
rng = random.Random(self.seed)
rng = build_rng(self.seed)
iters = [iter(it) for it in self.iterators]
exhausted = [False for _ in range(len(iters))]

Expand Down Expand Up @@ -450,10 +456,7 @@ def __iter__(self):
- each stream may be interpreted as a shard belonging to some larger group of streams
(e.g. multiple shards of a given dataset).
"""
if self.seed == "trng":
rng = secrets.SystemRandom()
else:
rng = random.Random(self.seed)
rng = build_rng(self.seed)

def shuffled_streams():
# Create an infinite iterable of our streams.
Expand Down
8 changes: 8 additions & 0 deletions lhotse/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import math
import os
import random
import secrets
import sys
import urllib
import uuid
Expand Down Expand Up @@ -1092,3 +1093,10 @@ def type_cast_value(self, ctx, value):

def is_torchaudio_available() -> bool:
return is_module_available("torchaudio")


def build_rng(seed: Union[int, Literal["trng"]]) -> random.Random:
if seed == "trng":
return secrets.SystemRandom()

Check warning on line 1100 in lhotse/utils.py

View check run for this annotation

Codecov / codecov/patch

lhotse/utils.py#L1100

Added line #L1100 was not covered by tests
else:
return random.Random(seed)

0 comments on commit dbc6f48

Please sign in to comment.