MCPcopy Create free account
hub / github.com/VisionXLab/OF-Diff / _hacked_clip_forward

Function _hacked_clip_forward

cldm/hack.py:32–68  ·  view source on GitHub ↗
(self, text)

Source from the content-addressed store, hash-verified

30
31# Written by Lvmin
32def _hacked_clip_forward(self, text):
33 PAD = self.tokenizer.pad_token_id
34 EOS = self.tokenizer.eos_token_id
35 BOS = self.tokenizer.bos_token_id
36
37 def tokenize(t):
38 return self.tokenizer(t, truncation=False, add_special_tokens=False)["input_ids"]
39
40 def transformer_encode(t):
41 if self.clip_skip > 1:
42 rt = self.transformer(input_ids=t, output_hidden_states=True)
43 return self.transformer.text_model.final_layer_norm(rt.hidden_states[-self.clip_skip])
44 else:
45 return self.transformer(input_ids=t, output_hidden_states=False).last_hidden_state
46
47 def split(x):
48 return x[75 * 0: 75 * 1], x[75 * 1: 75 * 2], x[75 * 2: 75 * 3]
49
50 def pad(x, p, i):
51 return x[:i] if len(x) >= i else x + [p] * (i - len(x))
52
53 raw_tokens_list = tokenize(text)
54 tokens_list = []
55
56 for raw_tokens in raw_tokens_list:
57 raw_tokens_123 = split(raw_tokens)
58 raw_tokens_123 = [[BOS] + raw_tokens_i + [EOS] for raw_tokens_i in raw_tokens_123]
59 raw_tokens_123 = [pad(raw_tokens_i, PAD, 77) for raw_tokens_i in raw_tokens_123]
60 tokens_list.append(raw_tokens_123)
61
62 tokens_list = torch.IntTensor(tokens_list).to(self.device)
63
64 feed = einops.rearrange(tokens_list, 'b f i -> (b f) i')
65 y = transformer_encode(feed)
66 z = einops.rearrange(y, '(b f) i c -> b (f i) c', f=3)
67
68 return z
69
70
71# Stolen from https://github.com/basujindal/stable-diffusion/blob/main/optimizedSD/splitAttention.py

Callers

nothing calls this directly

Calls 4

tokenizeFunction · 0.85
splitFunction · 0.85
padFunction · 0.85
transformer_encodeFunction · 0.85

Tested by

no test coverage detected