@@ -585,39 +585,30 @@ def _validate_positive_int_list(self, input_name, input_value):
585585 )
586586
587587 def _reset_custom_samplers (self , seed ):
588- """Give each sampler its own stream, and each shared group one between
589- them.
588+ """Seed each sampler, and each shared generator once.
590589
591- Samplers that share a generator, as the documented wind pair do, are
592- seeded once as a unit. Resetting each member in turn would leave every
593- seed but the last discarded and the group's stream decided by whichever
594- member happened to go last.
595-
596- Its own pass rather than the validation loop below, whose order sets
597- ``__dict__`` and so the order every other input is drawn in.
590+ Kept out of the validation loop in ``_set_stochastic``, whose order
591+ sets ``__dict__`` and with it the order every other input is drawn in.
598592 """
599593 groups = {}
600594 for input_name in sorted (self .__stochastic_dict ):
601595 sampler = self .__stochastic_dict [input_name ]
602596 if isinstance (sampler , CustomSampler ):
603- # Held in the value as well as keyed on, because `id` is
604- # unique only among live objects. Defensive: a `seed_group`
605- # that builds its answer did not merge in practice here.
597+ # Kept in the value too: `id` is unique only among live
598+ # objects, so the group has to outlive the dict.
606599 group = sampler .seed_group
607600 shared = groups .setdefault (id (group ), ([], sampler , group ))
608601 shared [0 ].append (input_name )
609602
610603 for names , sampler , group in groups .values ():
611- # The group itself when it can be reset, since it is the thing that
612- # holds the shared state. Going through one member instead assumes
613- # every member resets the same way and keeps nothing of its own.
604+ # The group holds the shared state, so reset it directly; a member
605+ # may reset differently, or keep state of its own.
614606 resetter = group if hasattr (group , "reset_seed" ) else sampler
615607 try :
616608 resetter .reset_seed (_sampler_seed (seed , names ))
617609 except Exception as error :
618- # Not just RuntimeError. The seed handed over is now 128 bits,
619- # which the legacy RandomState refuses with a ValueError, and a
620- # bare one of those does not say which sampler raised it.
610+ # Broad: the seed is 128 bits, which legacy RandomState refuses
611+ # with a ValueError that does not name the sampler.
621612 raise RuntimeError (
622613 f"An error occurred in the 'reset_seed' method of the "
623614 f"CustomSampler for { ', ' .join (names )} "
@@ -627,9 +618,7 @@ def _validate_custom_sampler(self, input_name, sampler):
627618 """
628619 Validate a custom sampler.
629620
630- Seeding is not done here. It happens in ``_reset_custom_samplers``,
631- which runs in a fixed order because two samplers can share one
632- generator and whichever is reset last decides the stream.
621+ Seeding happens in ``_reset_custom_samplers``, not here.
633622
634623 Parameters
635624 ----------
@@ -643,9 +632,8 @@ def _validate_custom_sampler(self, input_name, sampler):
643632 AssertionError
644633 If the input is not in a valid format.
645634 """
646- # Raised rather than asserted, the same way #1103 handles it: `python -O`
647- # strips an assert, and the documented AssertionError is kept so callers
648- # that already catch it still do.
635+ # Raised, not asserted: `python -O` strips asserts. AssertionError is
636+ # kept so callers that catch it still do. Same as #1103.
649637 if not isinstance (sampler , CustomSampler ):
650638 raise AssertionError (
651639 f"`{ input_name } ` must be a CustomSampler, not { type (sampler ).__name__ } "
0 commit comments