Skip to content

regex

Functions:

Name Description
patterns_to_regex

Convert a list of patterns into a regex pattern for matching nn.Module submodule or parameter names.

patterns_to_regex

patterns_to_regex(
    patterns: Iterable[str],
) -> re.Pattern[str]

Convert a list of patterns into a regex pattern for matching nn.Module submodule or parameter names.

As part of the syntax of the patterns
  • . is treated as a literal period.
  • * is treated as a wildcard that matches any number of characters.
  • Each pattern is wrapped in ^ and $, i.e. they must match strings beginning to end.

Parameters:

Name Type Description Default

patterns

Iterable[str]

A list of patterns to match.

required

Raises:

Type Description
ValueError

If patterns does not contain at least one pattern.

Returns:

Type Description
re.Pattern[str]

A regex pattern that matches any str that matches a pattern in patterns.

Examples:

>>> patterns_to_regex(["*"]).match("transformer.wte")
<re.Match object; span=(0, 15), match='transformer.wte'>
>>> patterns_to_regex(["*.bias"]).match("linear.bias")
<re.Match object; span=(0, 11), match='linear.bias'>
>>> patterns_to_regex(["*.*"]).match("abc")
>>> patterns_to_regex(["ace"]).match("racecar")