code: def construct_peft_lokrconfig(
      info: Dict[str, LoKrInfo], decompose_factor: int = -1, **kwargs
  ) -> LoKrConfig:
      """Constructs LoKrConfig from data extracted from adapter checkpoint
  
      Args:
          info (Dict[str, LoKrInfo]): Information extracted from adapter checkpoint
  
      Returns:
          LoKrConfig: config for constructing LoKr
      """
  
      # Unpack all ranks and alphas
      ranks = {x[0]: x[1].rank for x in info.items()}
      alphas = {x[0]: x[1].alpha or x[1].rank for x in info.items()}
  
      # Determine which modules needs to be transformed
      target_modules = sorted(info.keys())
  
      # Determine most common rank and alpha
      r = int(Counter(ranks.values()).most_common(1)[0][0])
      alpha = Counter(alphas.values()).most_common(1)[0][0]
  
      # Determine which modules have different rank and alpha
      rank_pattern = dict(
          sorted(
              filter(lambda x: x[1] != r, ranks.items()),
              key=lambda x: x[0],
          )
      )
      alpha_pattern = dict(
          sorted(
              filter(lambda x: x[1] != alpha, alphas.items()),
              key=lambda x: x[0],
          )
      )
  
      # Determine whether any of modules have effective conv2d decomposition
      use_effective_conv2d = any(
          ((val.lokr_t2 is not None) for val in info.values())
      )
  
      # decompose_both should be enabled if any w1 matrix in any layer is decomposed into 2
      decompose_both = any(
          (val.lokr_w1_a is not None and val.lokr_w1_b is not None)
          for val in info.values()
      )
  
      # Determining decompose factor is a bit tricky (but it is most often -1)
      # Check that decompose_factor is equal to provided
      for val in info.values():
          # Determine shape of first matrix
          if val.lokr_w1 is not None:
              w1_shape = tuple(val.lokr_w1.shape)
          else:
              w1_shape = (
                  val.lokr_w1_a.shape[0],
                  val.lokr_w1_b.shape[1],
              )
  
          # Determine shape of second matrix
          if val.lokr_w2 is not None:
              w2_shape = tuple(val.lokr_w2.shape[:2])
          elif val.lokr_t2 is not None:
              w2_shape = (
                  val.lokr_w2_a.shape[1],
                  val.lokr_w2_b.shape[1],
              )
          else:
              # We may iterate over Conv2d layer, for which second item in shape is multiplied by ksize^2
              w2_shape = (
                  val.lokr_w2_a.shape[0],
                  val.lokr_w2_b.shape[1],
              )
  
          # We need to check, whether decompose_factor is really -1 or not
          shape = (w1_shape[0], w2_shape[0])
          if factorization(shape[0] * shape[1], factor=-1) != shape:
              raise ValueError(
                  "Cannot infer decompose_factor, probably it is not equal to -1"
              )
  
      config = LoKrConfig(
          r=r,
          alpha=alpha,
          target_modules=target_modules,
          rank_dropout=0.0,
          module_dropout=0.0,
          init_weights=False,
          rank_pattern=rank_pattern,
          alpha_pattern=alpha_pattern,
          use_effective_conv2d=use_effective_conv2d,
          decompose_both=decompose_both,
          decompose_factor=decompose_factor,
      )
  
      return config
  
imports: import argparse
  import json
  import logging
  import os
  from collections import Counter
  from dataclasses import dataclass
  from operator import attrgetter
  from typing import Dict, List, Optional, Union
  import safetensors
  import torch
  import torch.nn
  from diffusers import UNet2DConditionModel
  from transformers import CLIPTextModel
  from peft import LoHaConfig, LoKrConfig, LoraConfig, PeftType, get_peft_model, set_peft_model_state_dict
  from peft.tuners.lokr.layer import factorization
label_desc: Use image node_id 3 for calling a global function with example usage: construct_peft_lokrconfig(info, decompose_factor) and returns: config