Cleanup some Python2-related code (#78864)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/78864
Approved by: https://github.com/janeyx99, https://github.com/jbschlosser
This commit is contained in:
Sergii Dymchenko 2022-06-06 17:40:02 +00:00 committed by PyTorch MergeBot
parent 7adf929a99
commit 0fdc1caf02
2 changed files with 2 additions and 12 deletions

View file

@ -1315,7 +1315,7 @@ def _compute_nparams_toprune(amount, tensor_size):
if isinstance(amount, numbers.Integral): if isinstance(amount, numbers.Integral):
return amount return amount
else: else:
return int(round(amount * tensor_size)) # int needed for Python 2 return round(amount * tensor_size)
def _validate_pruning_dim(t, dim): def _validate_pruning_dim(t, dim):

View file

@ -12,17 +12,7 @@ from typing import Match, Optional, Sequence, Mapping
class CodeTemplate: class CodeTemplate:
# Python 2.7.5 has a bug where the leading (^[^\n\S]*)? does not work, substitution_str = r"(^[^\n\S]*)?\$([^\d\W]\w*|\{,?[^\d\W]\w*\,?})"
# workaround via appending another [^\n\S]? inside
substitution_str = r"(^[^\n\S]*[^\n\S]?)?\$([^\d\W]\w*|\{,?[^\d\W]\w*\,?})"
# older versions of Python have a bug where \w* does not work,
# so we need to replace with the non-shortened version [a-zA-Z0-9_]*
# https://bugs.python.org/issue18647
substitution_str = substitution_str.replace(r"\w", r"[a-zA-Z0-9_]")
substitution = re.compile(substitution_str, re.MULTILINE) substitution = re.compile(substitution_str, re.MULTILINE)
pattern: str pattern: str