Skip to content

operators

PotentialOps

Mixin providing operations for potential functions:

  1. Product (*): Take the product of two potentials.

  2. Coercion (coerce): Coerce the potential to operate on another potential's vocabulary.

  3. Auto-batching (to_autobatched): Create a version that automatically batches concurrent requests to the instance methods.

  4. Parallelization (to_multiprocess): Create a version that parallelizes operations over multiple processes.

Source code in genlm/control/potential/operators.py
class PotentialOps:
    """Mixin providing operations for potential functions:

    1. Product (`*`): Take the product of two potentials.\n
    2. Coercion (`coerce`): Coerce the potential to operate on another potential's vocabulary.\n
    3. Auto-batching (`to_autobatched`): Create a version that automatically batches concurrent requests to the instance methods.\n
    4. Parallelization (`to_multiprocess`): Create a version that parallelizes operations over multiple processes.\n
    """

    def __mul__(self, other):
        """Take the product of two potentials.

        See [`Product`][genlm.control.potential.product.Product] for more details.

        Args:
            other (Potential): Another potential instance to take the product with.

        Returns:
            (Product): A Product instance representing the unnormalized product of the two potentials.

        Note:
            Potentials must operate on the same token type and the intersection of their vocabularies must be non-empty.
        """
        from genlm.control.potential.product import Product

        return Product(self, other)

    def __pow__(self, beta):
        """Scale this potential's log-weights by `beta`.

        See [`Tempered`][genlm.control.potential.tempered.Tempered] for more details.

        Args:
            beta (float): The exponent, `1/temperature`.

        Returns:
            (Tempered): The unnormalized `self ** beta`.
        """
        from genlm.control.potential.tempered import Tempered

        return Tempered(self, beta)

    def normalize(self):
        """Renormalize this potential's next-token rows.

        See [`Normalized`][genlm.control.potential.normalized.Normalized] for more details.

        Returns:
            (Normalized): A potential whose `logw_next` sums to one at every context.
        """
        from genlm.control.potential.normalized import Normalized

        return Normalized(self)

    def coerce(self, other, f, prune=True, homomorphic=None, trie=None, tables=None):
        """Coerce the current potential to operate on the vocabulary of another potential.

        See [`Coerced`][genlm.control.potential.coerce.Coerced] for more details.

        Args:
            other (Potential): The potential instance whose vocabulary will be used.
            f (callable): A function mapping sequences of tokens from self's vocab to sequences of tokens from other's vocab.
            prune (bool): Whether to prune the coerced potential's vocabulary to only include tokens that can be mapped to the original potential's vocabulary.
                If `False`, the coerced potential's vocabulary will include all tokens from the target vocabulary.
            homomorphic (bool | None): Whether `f` distributes over concatenation,
                which enables the trie fast path. `None` probes it. See `Coerced`.
            trie (dict | None): The symbol trie over `(other.vocab, f)`, as built by
                `Coerced.build_trie`.
            tables (VocabTables | None): Prebuilt tables for `other.vocab`, as built
                by `Potential.build_tables`. `trie` and `tables` require `prune=False`.

        Returns:
            (Coerced): A Potential that operates on the vocabulary of `other`.
        """
        from genlm.control.potential.coerce import Coerced

        return Coerced(
            self,
            other.vocab,
            f=f,
            prune=prune,
            homomorphic=homomorphic,
            trie=trie,
            tables=tables,
        )

    def to_autobatched(self):
        """Return a potential instance that automatically batches concurrent requests to the instance methods.

        The wrapper is memoized: repeated calls return the same instance.

        See [`AutoBatchedPotential`][genlm.control.potential.autobatch.AutoBatchedPotential] for more details.

        Returns:
            (AutoBatchedPotential): A potential instance that wraps the current potential and automatically batches concurrent requests to the instance methods.
        """
        from genlm.control.potential.autobatch import autobatched

        return autobatched(self)

    def to_multiprocess(self, num_workers=2, spawn_args=None):
        """Create a new potential instance that parallelizes operations using multiprocessing.

        See [`MultiProcPotential`][genlm.control.potential.multi_proc.MultiProcPotential] for more details.

        Args:
            num_workers (int): The number of workers to use in the multiprocessing pool.
            spawn_args (tuple): The positional arguments to pass to the potential's `spawn` method.

        Returns:
            (MultiProcPotential): A new potential instance that wraps the current potential and uses multiprocessing to parallelize operations.

        Note:
            For this method to be used, the potential must implement a picklable `spawn` method.
        """
        from genlm.control.potential.multi_proc import MultiProcPotential

        factory_args = spawn_args or ()
        return MultiProcPotential(
            potential_factory=self.spawn,
            factory_args=factory_args,
            num_workers=num_workers,
        )

__mul__(other)

Take the product of two potentials.

See Product for more details.

Parameters:

Name Type Description Default
other Potential

Another potential instance to take the product with.

required

Returns:

Type Description
Product

A Product instance representing the unnormalized product of the two potentials.

Note

Potentials must operate on the same token type and the intersection of their vocabularies must be non-empty.

Source code in genlm/control/potential/operators.py
def __mul__(self, other):
    """Take the product of two potentials.

    See [`Product`][genlm.control.potential.product.Product] for more details.

    Args:
        other (Potential): Another potential instance to take the product with.

    Returns:
        (Product): A Product instance representing the unnormalized product of the two potentials.

    Note:
        Potentials must operate on the same token type and the intersection of their vocabularies must be non-empty.
    """
    from genlm.control.potential.product import Product

    return Product(self, other)

__pow__(beta)

Scale this potential's log-weights by beta.

See Tempered for more details.

Parameters:

Name Type Description Default
beta float

The exponent, 1/temperature.

required

Returns:

Type Description
Tempered

The unnormalized self ** beta.

Source code in genlm/control/potential/operators.py
def __pow__(self, beta):
    """Scale this potential's log-weights by `beta`.

    See [`Tempered`][genlm.control.potential.tempered.Tempered] for more details.

    Args:
        beta (float): The exponent, `1/temperature`.

    Returns:
        (Tempered): The unnormalized `self ** beta`.
    """
    from genlm.control.potential.tempered import Tempered

    return Tempered(self, beta)

normalize()

Renormalize this potential's next-token rows.

See Normalized for more details.

Returns:

Type Description
Normalized

A potential whose logw_next sums to one at every context.

Source code in genlm/control/potential/operators.py
def normalize(self):
    """Renormalize this potential's next-token rows.

    See [`Normalized`][genlm.control.potential.normalized.Normalized] for more details.

    Returns:
        (Normalized): A potential whose `logw_next` sums to one at every context.
    """
    from genlm.control.potential.normalized import Normalized

    return Normalized(self)

coerce(other, f, prune=True, homomorphic=None, trie=None, tables=None)

Coerce the current potential to operate on the vocabulary of another potential.

See Coerced for more details.

Parameters:

Name Type Description Default
other Potential

The potential instance whose vocabulary will be used.

required
f callable

A function mapping sequences of tokens from self's vocab to sequences of tokens from other's vocab.

required
prune bool

Whether to prune the coerced potential's vocabulary to only include tokens that can be mapped to the original potential's vocabulary. If False, the coerced potential's vocabulary will include all tokens from the target vocabulary.

True
homomorphic bool | None

Whether f distributes over concatenation, which enables the trie fast path. None probes it. See Coerced.

None
trie dict | None

The symbol trie over (other.vocab, f), as built by Coerced.build_trie.

None
tables VocabTables | None

Prebuilt tables for other.vocab, as built by Potential.build_tables. trie and tables require prune=False.

None

Returns:

Type Description
Coerced

A Potential that operates on the vocabulary of other.

Source code in genlm/control/potential/operators.py
def coerce(self, other, f, prune=True, homomorphic=None, trie=None, tables=None):
    """Coerce the current potential to operate on the vocabulary of another potential.

    See [`Coerced`][genlm.control.potential.coerce.Coerced] for more details.

    Args:
        other (Potential): The potential instance whose vocabulary will be used.
        f (callable): A function mapping sequences of tokens from self's vocab to sequences of tokens from other's vocab.
        prune (bool): Whether to prune the coerced potential's vocabulary to only include tokens that can be mapped to the original potential's vocabulary.
            If `False`, the coerced potential's vocabulary will include all tokens from the target vocabulary.
        homomorphic (bool | None): Whether `f` distributes over concatenation,
            which enables the trie fast path. `None` probes it. See `Coerced`.
        trie (dict | None): The symbol trie over `(other.vocab, f)`, as built by
            `Coerced.build_trie`.
        tables (VocabTables | None): Prebuilt tables for `other.vocab`, as built
            by `Potential.build_tables`. `trie` and `tables` require `prune=False`.

    Returns:
        (Coerced): A Potential that operates on the vocabulary of `other`.
    """
    from genlm.control.potential.coerce import Coerced

    return Coerced(
        self,
        other.vocab,
        f=f,
        prune=prune,
        homomorphic=homomorphic,
        trie=trie,
        tables=tables,
    )

to_autobatched()

Return a potential instance that automatically batches concurrent requests to the instance methods.

The wrapper is memoized: repeated calls return the same instance.

See AutoBatchedPotential for more details.

Returns:

Type Description
AutoBatchedPotential

A potential instance that wraps the current potential and automatically batches concurrent requests to the instance methods.

Source code in genlm/control/potential/operators.py
def to_autobatched(self):
    """Return a potential instance that automatically batches concurrent requests to the instance methods.

    The wrapper is memoized: repeated calls return the same instance.

    See [`AutoBatchedPotential`][genlm.control.potential.autobatch.AutoBatchedPotential] for more details.

    Returns:
        (AutoBatchedPotential): A potential instance that wraps the current potential and automatically batches concurrent requests to the instance methods.
    """
    from genlm.control.potential.autobatch import autobatched

    return autobatched(self)

to_multiprocess(num_workers=2, spawn_args=None)

Create a new potential instance that parallelizes operations using multiprocessing.

See MultiProcPotential for more details.

Parameters:

Name Type Description Default
num_workers int

The number of workers to use in the multiprocessing pool.

2
spawn_args tuple

The positional arguments to pass to the potential's spawn method.

None

Returns:

Type Description
MultiProcPotential

A new potential instance that wraps the current potential and uses multiprocessing to parallelize operations.

Note

For this method to be used, the potential must implement a picklable spawn method.

Source code in genlm/control/potential/operators.py
def to_multiprocess(self, num_workers=2, spawn_args=None):
    """Create a new potential instance that parallelizes operations using multiprocessing.

    See [`MultiProcPotential`][genlm.control.potential.multi_proc.MultiProcPotential] for more details.

    Args:
        num_workers (int): The number of workers to use in the multiprocessing pool.
        spawn_args (tuple): The positional arguments to pass to the potential's `spawn` method.

    Returns:
        (MultiProcPotential): A new potential instance that wraps the current potential and uses multiprocessing to parallelize operations.

    Note:
        For this method to be used, the potential must implement a picklable `spawn` method.
    """
    from genlm.control.potential.multi_proc import MultiProcPotential

    factory_args = spawn_args or ()
    return MultiProcPotential(
        potential_factory=self.spawn,
        factory_args=factory_args,
        num_workers=num_workers,
    )