Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also infer dynamic axes for every model input and output #529

Merged
merged 8 commits into from
Jul 23, 2024
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions returnn/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import logging
import shutil
import subprocess as sp
from typing import Any, Dict, Optional, Sequence, Union
import sys
from typing import Any, Dict, List, Optional, Sequence, Union

import i6_core.util as util

Expand Down Expand Up @@ -242,17 +243,6 @@ def __init__(
self.returnn_config = returnn_config
self.checkpoint = checkpoint

# Get the list here, because ReturnnConfig serialization might potentially reorder via `sort_config=True`.
input_names = (
list(returnn_config.config["extern_data"].keys())
if ("extern_data" in returnn_config.config and input_names is None)
else input_names
)
output_names = (
list(returnn_config.config["model_outputs"].keys())
if ("model_outputs" in returnn_config.config and output_names is None)
else output_names
)
self.input_names = input_names
self.output_names = output_names

Expand Down Expand Up @@ -281,10 +271,37 @@ def run(self):
"--verbosity",
"5",
]
if self.input_names:
cmd += ["--input_names", ",".join(self.input_names)]
if self.output_names:
cmd += ["--output_names", ",".join(self.output_names)]

# Pass the tensor names here because ReturnnConfig serialization might
# potentially reorder via `sort_config=True`, and the order matters for
# output tensor <-> tensor name association.
input_names = self.input_names or self.collect_tensor_names(
str(self.returnn_root), self.returnn_config.config.get("extern_data", {})
)
output_names = self.output_names or self.collect_tensor_names(
str(self.returnn_root), self.returnn_config.config.get("model_outputs", {})
)
if input_names:
cmd += ["--input_names", ",".join(input_names)]
if output_names:
cmd += ["--output_names", ",".join(output_names)]
albertz marked this conversation as resolved.
Show resolved Hide resolved

util.create_executable("compile.sh", cmd) # convenience file for manual execution
sp.run(cmd, check=True)

@staticmethod
def collect_tensor_names(returnn_root: str, data_dict: Dict[str, Any]) -> List[str]:
if returnn_root not in sys.path:
sys.path.append(returnn_root)

from returnn.tensor import Tensor

names = []
for name, opts in data_dict.items():
names.append(name)
tensor = Tensor(name=name, **opts)
for i, dim in enumerate(tensor.dims):
# We need seq lengths if there is a dyn size which is not a scalar.
if dim.dyn_size_ext and dim.dyn_size_ext.dims:
names.append(f"{name}:size{i}")
return names
Loading