File size: 2,916 Bytes
72a5beb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# FramePack-eichi LoRA Check Helper
#
# LoRAの適用状態確認のための機能を提供します。

import torch

# 国際化対応
from locales.i18n_extended import translate as _

def check_lora_applied(model):
    """
    モデルにLoRAが適用されているかをチェック

    Args:
        model: チェック対象のモデル

    Returns:
        (bool, str): LoRAが適用されているかどうかとその適用方法
    """
    # _lora_appliedフラグのチェック
    has_flag = hasattr(model, '_lora_applied') and model._lora_applied

    if has_flag:
        return True, "direct_application"

    # モデル内の名前付きモジュールをチェックして、LoRAフックがあるかを確認
    has_hooks = False
    for name, module in model.named_modules():
        if hasattr(module, '_lora_hooks'):
            has_hooks = True
            break

    if has_hooks:
        return True, "hooks"

    return False, "none"

def analyze_lora_application(model):
    """
    モデルのLoRA適用率と影響を詳細に分析

    Args:
        model: 分析対象のモデル

    Returns:
        dict: 分析結果の辞書
    """
    total_params = 0
    lora_affected_params = 0

    # トータルパラメータ数とLoRAの影響を受けるパラメータ数をカウント
    for name, module in model.named_modules():
        if hasattr(module, 'weight') and isinstance(module.weight, torch.Tensor):
            param_count = module.weight.numel()
            total_params += param_count

            # LoRA適用されたモジュールかチェック
            if hasattr(module, '_lora_hooks') or hasattr(module, '_lora_applied'):
                lora_affected_params += param_count

    # 適用率の計算
    application_rate = 0.0
    if total_params > 0:
        application_rate = lora_affected_params / total_params * 100.0

    return {
        "total_params": total_params,
        "lora_affected_params": lora_affected_params,
        "application_rate": application_rate,
        "has_lora": lora_affected_params > 0
    }

def print_lora_status(model):
    """
    モデルのLoRA適用状況を出力

    Args:
        model: 出力対象のモデル
    """
    has_lora, source = check_lora_applied(model)

    if has_lora:
        print(_("LoRAステータス: {0}").format(_("適用済み")))
        print(_("適用方法: {0}").format(_(source)))

        # 詳細な分析
        analysis = analyze_lora_application(model)
        application_rate = analysis["application_rate"]

        print(_("LoRA適用状況: {0}/{1} パラメータ ({2:.2f}%)").format(
            analysis["lora_affected_params"],
            analysis["total_params"],
            application_rate
        ))
    else:
        print(_("LoRAステータス: {0}").format(_("未適用")))
        print(_("モデルにLoRAは適用されていません"))