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

Move model to device after pytorch interpolation #2525

Merged
merged 2 commits into from
Jan 27, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
ModelLoader,
)

from api.node_context import NodeContext
from nodes.impl.pytorch.utils import np2tensor, tensor2np
from nodes.properties.inputs import ModelInput, SliderInput
from nodes.properties.outputs import ModelOutput, NumberOutput
from packages.chaiNNer_pytorch.settings import get_settings

from .. import utility_group

Expand Down Expand Up @@ -98,15 +100,26 @@ def check_can_interp(model_a: dict, model_b: dict):
NumberOutput("Amount A", output_type="100 - Input2"),
NumberOutput("Amount B", output_type="Input2"),
],
node_context=True,
)
def interpolate_models_node(
model_a: ModelDescriptor, model_b: ModelDescriptor, amount: int
context: NodeContext,
model_a: ModelDescriptor,
model_b: ModelDescriptor,
amount: int,
) -> tuple[ModelDescriptor, int, int]:
exec_options = get_settings(context)
pytorch_device = exec_options.device

if amount == 0:
return model_a, 100, 0
elif amount == 100:
return model_b, 0, 100

if model_a.device != model_b.device:
model_a.to(pytorch_device)
model_b.to(pytorch_device)

state_a = model_a.model.state_dict()
state_b = model_b.model.state_dict()

Expand All @@ -118,6 +131,6 @@ def interpolate_models_node(

state_dict = perform_interp(state_a, state_b, amount)

model = ModelLoader().load_from_state_dict(state_dict)
model = ModelLoader(pytorch_device).load_from_state_dict(state_dict)

return model, 100 - amount, amount
Loading