sd2_clip.py 1.24 KB
Newer Older
1
from comfy import sd1_clip
comfyanonymous's avatar
comfyanonymous committed
2
import torch
3
import os
comfyanonymous's avatar
comfyanonymous committed
4

5
class SD2ClipModel(sd1_clip.SD1ClipModel):
6
    def __init__(self, arch="ViT-H-14", device="cpu", max_length=77, freeze=True, layer="penultimate", layer_idx=None, textmodel_path=None):
7
8
9
10
        if layer == "penultimate":
            layer="hidden"
            layer_idx=23

11
        textmodel_json_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), "sd2_clip_config.json")
12
        super().__init__(device=device, freeze=freeze, layer=layer, layer_idx=layer_idx, textmodel_json_config=textmodel_json_config, textmodel_path=textmodel_path)
comfyanonymous's avatar
comfyanonymous committed
13
        self.empty_tokens = [[49406] + [49407] + [0] * 75]
14
15
16
17
18
19
20
21
22
23

    def clip_layer(self, layer_idx):
        if layer_idx < 0:
            layer_idx -= 1 #The real last layer of SD2.x clip is the penultimate one. The last one might contain garbage.
        if abs(layer_idx) >= 24:
            self.layer = "hidden"
            self.layer_idx = -2
        else:
            self.layer = "hidden"
            self.layer_idx = layer_idx
comfyanonymous's avatar
comfyanonymous committed
24
25

class SD2Tokenizer(sd1_clip.SD1Tokenizer):
26
    def __init__(self, tokenizer_path=None, embedding_directory=None):
27
        super().__init__(tokenizer_path, pad_with_end=False, embedding_directory=embedding_directory, embedding_size=1024)