ljleb / sd-mecha

Executable State Dict Recipes
MIT License
51 stars 4 forks source link

Recipe serialization #7

Closed ljleb closed 8 months ago

ljleb commented 8 months ago

We want to be able to serialize merge recipes into a human readable and editable format and also deserialize them into instances of MergeRecipe. I was thinking that we could represent a recipe as a simple list in a file with the extension .mecha.

I suggest to consider reducing the contents of the file to a single list of instructions that can be performed to merge:

model "ghostmix_v20Bakedvae"
model "dreamshaper_332BakedVaeClipFix"
call "weighted_sum" &0 &1 alpha=0.5

Here, weighted_sum &0 &1 alpha=0.5 means that weighted_sum has 2 positional arguments: the result of line 0 and the result of line 1; and 1 named parameter: alpha with the float value 0.5.

Each line is an expression. The nth line is associated with the index n. Indices start at 0. The first word in a line is the "operation" to perform (either a merge method, basic actions like loading a model or a lora or even the definition of hyper parameters) followed by n space-sparated words, which are its arguments. The last expression is the final output of the recipe.

To reconstruct a recipe in python from a .mecha, we can simply read it from top to bottom, mapping the first word in a and use a dictionary to keep track of existing nodes. We can then return the last node from this function.

ljleb commented 8 months ago

Disclaimer: the following was generated with GPT4.

Mecha Language Specification Version 1.0

Overview

The Mecha language offers a streamlined syntax for defining model merge operations, enabling users to efficiently describe complex model manipulation and merging processes. It is a line-oriented language where each line encapsulates a distinct expression that resolves to a specific value. This document provides a comprehensive and unambiguous specification of the Mecha language, including its syntax, expression structure, functions, and argument handling.

Syntax Specification

General Structure

Detailed Function and Argument Specification

Functions

Argument Handling

Positional Arguments
Keyword Arguments

Example Usage

Basic Model Invocation

model "ghostmix_v20Bakedvae"
model "dreamshaper_332BakedVaeClipFix"
call "weighted_sum" &0 &1 alpha=0.5

Advanced Model Merge Recipe

model "ghostmix_v20Bakedvae"
model "pure/v1-5-pruned"
call "subtract" &0 &1
model "dreamshaper_332BakedVaeClipFix"
call "subtract" &3 &1
call "perpendicular_component" &2 &4
call "add_difference" &0 &5 alpha=1.0
call "weighted_sum" &0 &3 alpha=0.5
dict txt_class_pos_embed=0 txt_class_token_embed=0 txt_class_final_norm=0 txt_class_layer_norm1=0 txt_class_layer_norm2=0 txt_class_mlp_fc1=0 txt_class_mlp_fc2=0 txt_class_q=0 txt_class_k=0 txt_class_v=0 txt_class_out=0 txt_class_default=0 unet_class_in0=1 unet_class_op=1 unet_class_proj_in=1 unet_class_proj_out=1 unet_class_trans_attn1_q=1 unet_class_trans_attn1_k=1 unet_class_trans_attn1_v=1 unet_class_trans_attn1_out=1 unet_class_trans_attn2_q=1 unet_class_trans_attn2_k=1 unet_class_trans_attn2_v=1 unet_class_trans_attn2_out=1 unet_class_trans_norm1=1 unet_class_trans_norm2=1 unet_class_trans_norm3=1 unet_class_trans_ff_net0_proj=1 unet_class_trans_ff_net2=1 unet_class_emb_layers1=1 unet_class_in_layers0=1 unet_class_in_layers2=1 unet_class_out_layers0=1 unet_class_out_layers3=1 unet_class_norm=1 unet_class_skip_connection=1 unet_class_conv=

1 unet_class_out0=1 unet_class_out2=1 unet_class_default=1
call "weighted_sum" &6 &7 alpha=&8

Additional Remarks