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

Add dump_dummy script #25

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
57 changes: 57 additions & 0 deletions scripts/dump_dummy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# ruff: noqa: E402
"""
This is a tool for dumping the state dict of a dummy model.

Purpose:
When adding/testing model detection or model parameter detection code,
it is useful to see the effects a single parameter has on the state dict of a
model. Since there aren't pretrained models for every possible parameter
configuration, this script can be used to generate a dummy model with the given
parameters.

Usage:
To use this script, you need to edit the `create_dummy` function below. Edit
the function to make it return a model with your desired parameters. As always,
VSCode is the recommended IDE for this task.

After you edited the function, run this script, and it will dump the state dict
of the dummy model to `dump.yml`.

python scripts/dump_dummy.py

For more detail on the dump itself, see the docs of `dump_state_dict.py`.
"""


import inspect
import os
import sys
from textwrap import dedent

import torch

# This hack is necessary to make our module import
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../src")))

from dump_state_dict import dump

from spandrel.architectures import SCUNet


def create_dummy() -> torch.nn.Module:
"""Edit this function"""
return SCUNet.SCUNet()


if __name__ == "__main__":
net = create_dummy()
state = net.state_dict()

# get source code expression of network
source = inspect.getsource(create_dummy)
source = "\n".join(source.split("\n")[1:]) # remove "def create_dummy():
source = dedent(source)
if source.startswith("return "):
source = source[7:]

dump(state, source)
24 changes: 15 additions & 9 deletions scripts/dump_state_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
import os
import sys
from dataclasses import dataclass
from typing import Dict, Generic, Iterable, TypeVar
from typing import Any, Dict, Generic, Iterable, TypeVar

from torch import Tensor

# I fucking hate python. This hack is necessary to make our module import
# This hack is necessary to make our module import
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../src")))

from spandrel import ModelLoader # noqa: E402
Expand Down Expand Up @@ -181,12 +181,18 @@ def dump(s: Fork[State] | State, level: int = 0):
return lines


file = sys.argv[1]
print(f"Input file: {file}")
state = load_state(file)
def dump(state: dict[str, Any], comment: str, file: str = "dump.yml"):
with open(file, "w") as f:
comment = "\n".join("# " + s for s in comment.splitlines())
f.write(f"{comment}\n")
f.write("\n".join(dump_lines(state)))

with open("dump.yml", "w") as f:
f.write(f"# {file}\n")
f.write("\n".join(dump_lines(state)))
print(f"Dumped {len(state)} keys to {file}")

print(f"Dumped {len(state)} keys to dump.yml")

if __name__ == "__main__":
file = sys.argv[1]
print(f"Input file: {file}")
state = load_state(file)

dump(state, file)