File size: 3,433 Bytes
be285e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Frozen CLIP text encoder + shared tokenisation for data prep and inference."""

from __future__ import annotations

import os
from typing import Sequence, Tuple

import numpy as np
import torch

DEFAULT_TEXT_ENCODER = "openai/clip-vit-base-patch32"


def _local_override(name: str):
    """If TROUT_CLIP_LOCAL is set, serve `name` from a local directory (offline training)."""
    local = os.environ.get("TROUT_CLIP_LOCAL")
    if local and name == DEFAULT_TEXT_ENCODER and os.path.isdir(local):
        return local
    return None


def load_tokenizer(name: str):
    from transformers import AutoTokenizer
    local = _local_override(name)
    if local:
        return AutoTokenizer.from_pretrained(local, local_files_only=True)
    return AutoTokenizer.from_pretrained(name)


def tokenize_texts(tokenizer, texts: Sequence[str], text_len: int, batch: int = 65536) -> Tuple[np.ndarray, np.ndarray]:
    """Tokenise to a fixed length. Padding uses the pad id; over-long prompts are
    truncated while keeping the final EOS token.

    Returns (ids int32 (n, text_len), true_lengths int32 (n,)).
    """
    eos = tokenizer.eos_token_id
    pad = tokenizer.pad_token_id if tokenizer.pad_token_id is not None else eos
    n = len(texts)
    out = np.full((n, text_len), pad, dtype=np.int32)
    lengths = np.zeros(n, dtype=np.int32)
    for s in range(0, n, batch):
        chunk = [t if isinstance(t, str) else "" for t in texts[s:s + batch]]
        enc = tokenizer(chunk, add_special_tokens=True, truncation=False)["input_ids"]
        for i, ids in enumerate(enc):
            lengths[s + i] = len(ids)
            if len(ids) > text_len:
                ids = list(ids[:text_len - 1]) + [eos]
            out[s + i, :len(ids)] = ids
    return out, lengths


class FrozenTextEncoder:
    """Wraps a HF CLIP text tower. Deliberately not an nn.Module so its weights never
    end up inside the trainable model, optimizer or checkpoints."""

    def __init__(self, name: str = DEFAULT_TEXT_ENCODER, text_len: int = 24,
                 device="cpu", dtype: torch.dtype = torch.float32):
        from transformers import CLIPTextModel
        from transformers.utils import logging as hf_logging
        self.name = name
        self.text_len = text_len
        self.device = torch.device(device)
        self.dtype = dtype
        self.tokenizer = load_tokenizer(name)
        local = _local_override(name)
        prev = hf_logging.get_verbosity()
        hf_logging.set_verbosity_error()   # full CLIP checkpoint has vision weights we skip on purpose
        try:
            model = CLIPTextModel.from_pretrained(local or name, local_files_only=bool(local))
        finally:
            hf_logging.set_verbosity(prev)
        self.model = model.to(device=self.device, dtype=dtype).eval().requires_grad_(False)
        self.dim = int(model.config.hidden_size)

    def tokenize(self, texts: Sequence[str]) -> torch.Tensor:
        ids, _ = tokenize_texts(self.tokenizer, list(texts), self.text_len)
        return torch.from_numpy(ids).long().to(self.device)

    @torch.no_grad()
    def __call__(self, ids: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
        out = self.model(input_ids=ids.long().to(self.device))
        return out.last_hidden_state, out.pooler_output

    @torch.no_grad()
    def encode(self, texts: Sequence[str]) -> Tuple[torch.Tensor, torch.Tensor]:
        return self(self.tokenize(texts))