Skip to content

Commit

Permalink
[fx] Replace _snake_case with a regexp (pytorch#135822)
Browse files Browse the repository at this point in the history
~2x speedup on this function, though saves <0.5s overall

Pull Request resolved: pytorch#135822
Approved by: https://github.com/oulgen
ghstack dependencies: pytorch#135787, pytorch#135788, pytorch#135820, pytorch#135821
  • Loading branch information
jansel authored and pytorchmergebot committed Sep 13, 2024
1 parent a72124a commit 1f15c0c
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions torch/fx/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import math
import warnings
import inspect
import functools

__all__ = ["PythonCode", "CodeGen", "Graph"]

Expand Down Expand Up @@ -86,14 +87,11 @@ def _snake_case(s: str) -> str:
``mod.pascalCase``-> ``mod.pascal_case``
``mod.ALL_CAPS`` -> ``mod.all_caps``
"""
chars = []
prev_lower = False
for c in s:
if prev_lower and c.isupper():
chars.append('_')
chars.append(c.lower())
prev_lower = c.islower()
return ''.join(chars)
return _snake_case_sub(s).lower()


# Replace occurrences where a lowercase letter is followed by an uppercase letter
_snake_case_sub = functools.partial(re.compile(r'(?<=[a-z])([A-Z])').sub, r'_\1')


def _is_from_torch(obj: Any) -> bool:
Expand Down

0 comments on commit 1f15c0c

Please sign in to comment.