File size: 5,849 Bytes
19712a8
 
 
 
 
 
 
 
 
 
 
 
 
 
f1d2416
19712a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1d2416
19712a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61b0e28
19712a8
 
 
61b0e28
19712a8
8ad6ef6
19712a8
 
 
 
 
 
 
 
 
 
 
 
 
 
f1d2416
19712a8
8ad6ef6
19712a8
f1d2416
19712a8
 
 
 
 
 
 
8ad6ef6
 
 
 
 
 
 
 
f1d2416
 
8ad6ef6
 
 
f1d2416
8ad6ef6
 
 
 
 
 
19712a8
f1d2416
 
 
 
 
 
 
 
 
 
 
266a16a
 
19712a8
 
 
 
 
 
8ad6ef6
f1d2416
19712a8
 
f1d2416
19712a8
8ad6ef6
 
 
19712a8
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import gradio as gr
import torch
from torch import Tensor, nn
import spaces
import numpy as np
import io
import base64
from flax import nnx
import jax.numpy as jnp
from jax import Array as Tensor

from transformers import (FlaxCLIPTextModel, CLIPTokenizer, FlaxT5EncoderModel,
                          T5Tokenizer)

models = {}
class HFEmbedder(nnx.Module):
    def __init__(self, version: str, max_length: int, **hf_kwargs):
        self.is_clip = version.startswith("openai")
        self.max_length = max_length
        self.output_key = "pooler_output" if self.is_clip else "last_hidden_state"
        dtype = hf_kwargs.get("dtype", jnp.float32)
        if self.is_clip:
            self.tokenizer: CLIPTokenizer = CLIPTokenizer.from_pretrained(version, max_length=max_length)
            # self.hf_module: CLIPTextModel = CLIPTextModel.from_pretrained(version, **hf_kwargs)
            self.hf_module, params = FlaxCLIPTextModel.from_pretrained(version, _do_init=False, **hf_kwargs)
        else:
            self.tokenizer: T5Tokenizer = T5Tokenizer.from_pretrained(version, max_length=max_length)
            # self.hf_module: T5EncoderModel = T5EncoderModel.from_pretrained(version, **hf_kwargs)
            self.hf_module, params = FlaxT5EncoderModel.from_pretrained(version, _do_init=False,**hf_kwargs)
        self.hf_module._is_initialized = True
        import jax
        self.hf_module.params = jax.tree.map(lambda x: jax.device_put(x, jax.devices("cuda")[0]), params)
        # if dtype==jnp.bfloat16:

    def tokenize(self, text: list[str]) -> Tensor:
        batch_encoding = self.tokenizer(
            text,
            truncation=True,
            max_length=self.max_length,
            return_length=False,
            return_overflowing_tokens=False,
            padding="max_length",
            return_tensors="jax",
        )
        return batch_encoding["input_ids"]
    
    def __call__(self, input_ids: Tensor) -> Tensor:
        # outputs = self.hf_module(
        #     input_ids=batch_encoding["input_ids"].to(self.hf_module.device),
        #     attention_mask=None,
        #     output_hidden_states=False,
        # )
        outputs = self.hf_module(
            input_ids=input_ids,
            attention_mask=None,
            output_hidden_states=False,
            train=False,
        )
        return outputs[self.output_key]
    # def __call__(self, text: list[str]) -> Tensor:
    #     batch_encoding = self.tokenizer(
    #         text,
    #         truncation=True,
    #         max_length=self.max_length,
    #         return_length=False,
    #         return_overflowing_tokens=False,
    #         padding="max_length",
    #         return_tensors="jax",
    #     )

    #     # outputs = self.hf_module(
    #     #     input_ids=batch_encoding["input_ids"].to(self.hf_module.device),
    #     #     attention_mask=None,
    #     #     output_hidden_states=False,
    #     # )
    #     outputs = self.hf_module(
    #         input_ids=batch_encoding["input_ids"],
    #         attention_mask=None,
    #         output_hidden_states=False,
    #         train=False,
    #     )
    #     return outputs[self.output_key]




def load_t5(device: str | torch.device = "cuda", max_length: int = 512) -> HFEmbedder:
    # max length 64, 128, 256 and 512 should work (if your sequence is short enough)
    return HFEmbedder("lnyan/t5-v1_1-xxl-encoder", max_length=max_length, dtype=jnp.bfloat16)


def load_clip(device: str | torch.device = "cuda") -> HFEmbedder:
    return HFEmbedder("openai/clip-vit-large-patch14", max_length=77, dtype=jnp.bfloat16)

@spaces.GPU(duration=60)
def load_encoders():
    is_schnell = True
    t5 = load_t5("cuda", max_length=256 if is_schnell else 512)
    clip = load_clip("cuda")
    return t5, clip

import numpy as np
def b64(txt,vec):
    buffer = io.BytesIO()
    jnp.savez(buffer, txt=txt, vec=vec)
    buffer.seek(0)
    encoded = base64.b64encode(buffer.getvalue()).decode('utf-8')
    return encoded

# t5,clip=load_encoders()

@spaces.GPU(duration=20)
def convert(prompt):
    t5,clip=models["t5"],models["clip"]
    if isinstance(prompt, str):
        prompt = [prompt]
    txt = t5.tokenize(prompt)
    txt = t5(txt)
    vec = clip.tokenize(prompt)
    vec = clip(vec)
    return b64(txt,vec)
import jax
def _to_embed(t5, clip, txt, vec):
    t5=nnx.merge(*t5)
    clip=nnx.merge(*clip)
    return t5(txt), clip(vec)

to_embed=jax.jit(_to_embed)

# t5_tuple=nnx.split(t5)
# clip_tuple=nnx.split(clip)

@spaces.GPU(duration=120)
def compile(prompt):
    t5,clip,t5_tuple,clip_tuple=models["t5"],models["clip"],models["t5_tuple"],models["clip_tuple"]
    if isinstance(prompt, str):
        prompt = [prompt]
    txt = t5.tokenize(prompt)
    vec = clip.tokenize(prompt)
    text,vec=to_embed(t5_tuple,clip_tuple,txt,vec)
    return b64(txt,vec)

@spaces.GPU(duration=120)
def load(prompt):
    is_schnell = True
    t5 = load_t5("cuda", max_length=256 if is_schnell else 512)
    clip = load_clip("cuda")
    models["t5"]=t5
    models["clip"]=clip
    models["t5_tuple"]=nnx.split(t5)
    models["clip_tuple"]=nnx.split(clip)
    return "Loaded"

print(load(""))

with gr.Blocks() as demo:
    gr.Markdown("""A workaround for flux-flax to fit into 40G VRAM""")
    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(label="prompt")
            convert_btn = gr.Button(value="Convert")
            compile_btn = gr.Button(value="Compile")
            load_btn = gr.Button(value="Load")
        with gr.Column():
            output = gr.Textbox(label="output")
    load_btn.click(load, inputs=prompt, outputs=output, api_name="load")
    convert_btn.click(convert, inputs=prompt, outputs=output, api_name="convert")
    compile_btn.click(compile, inputs=prompt, outputs=output, api_name="compile")




demo.launch()