Skip to content

Commit cf0e9aa

Browse files
authored
Update exir_deserialize logic
Differential Revision: D87112636 Pull Request resolved: #15840
1 parent 5e5a801 commit cf0e9aa

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

exir/serde/export_serialize.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,17 +2143,23 @@ def deserialize_meta_func(serialized_target: str):
21432143
def import_nn_module_stack(key, path, ty):
21442144
return key, (path, ty)
21452145

2146-
# Helper function that splits strings by commas except for those
2147-
# encapsulated by parens, which are valid traces.
2148-
# TODO: Currently this is needed due to indexing Sequential
2149-
# layers introducing names in the form "layer.slice(1, None, None)".
2150-
# If that naming is improved, this fancier splitting can probably be
2151-
# reverted to a simple split by comma.
2146+
# Helper function to split string by commas, accounting for nested parentheses/brackets
21522147
def metadata_split(metadata):
2153-
# Remove the parentheses and commas inside them
2154-
metadata = re.sub(r"\(.*?\)", "", metadata)
2155-
# Split the string by comma, except for those inside parentheses
2156-
return re.split(r"(?<!\()\s*,\s*(?!\()", metadata)
2148+
out = []
2149+
start, depth = 0, 0
2150+
for position, char in enumerate(metadata):
2151+
if char in "[(":
2152+
depth += 1
2153+
elif char in ")]":
2154+
depth -= 1
2155+
if depth < 0:
2156+
raise ValueError(f"Mismatched brackets in metadata: {metadata}")
2157+
elif char == "," and depth == 0:
2158+
out.append(metadata[start:position].strip())
2159+
start = position + 1
2160+
out.append(metadata[start:].strip())
2161+
assert len(out) == 3
2162+
return out
21572163

21582164
nn_module_stack = dict(
21592165
import_nn_module_stack(*metadata_split(item))

0 commit comments

Comments
 (0)