pydiffuse 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pydiffuse-0.1.0/.gitignore +15 -0
- pydiffuse-0.1.0/LICENSE +21 -0
- pydiffuse-0.1.0/PKG-INFO +137 -0
- pydiffuse-0.1.0/README.md +109 -0
- pydiffuse-0.1.0/pyproject.toml +72 -0
- pydiffuse-0.1.0/src/pydiffuse/__init__.py +5 -0
- pydiffuse-0.1.0/src/pydiffuse/cli.py +106 -0
- pydiffuse-0.1.0/src/pydiffuse/clip.py +284 -0
- pydiffuse-0.1.0/src/pydiffuse/data/clip_tokenizer/tokenizer.json +245079 -0
- pydiffuse-0.1.0/src/pydiffuse/data/clip_tokenizer/tokenizer_config.json +10 -0
- pydiffuse-0.1.0/src/pydiffuse/layers.py +86 -0
- pydiffuse-0.1.0/src/pydiffuse/py.typed +0 -0
pydiffuse-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sam Ireland
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pydiffuse-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pydiffuse
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A python library for generating media with diffusion.
|
|
5
|
+
Project-URL: Homepage, https://github.com/samirelanduk/pydiffuse
|
|
6
|
+
Project-URL: Source, https://github.com/samirelanduk/pydiffuse
|
|
7
|
+
Project-URL: Issues, https://github.com/samirelanduk/pydiffuse/issues
|
|
8
|
+
Author-email: Sam Ireland <mail@samireland.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: clip,diffusion,image-generation,torch
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Multimedia :: Graphics
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
22
|
+
Requires-Python: >=3.12
|
|
23
|
+
Requires-Dist: click>=8.4
|
|
24
|
+
Requires-Dist: safetensors>=0.8
|
|
25
|
+
Requires-Dist: torch>=2.13
|
|
26
|
+
Requires-Dist: transformers>=5.13
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# pydiffuse
|
|
30
|
+
|
|
31
|
+
[](https://github.com/samirelanduk/pydiffuse/actions/workflows/ci.yml)
|
|
32
|
+
[](https://github.com/samirelanduk/pydiffuse/blob/master/pyproject.toml)
|
|
33
|
+
[](LICENSE)
|
|
34
|
+
[](https://github.com/astral-sh/uv)
|
|
35
|
+
[](https://github.com/astral-sh/ruff)
|
|
36
|
+
[](https://github.com/microsoft/pyright)
|
|
37
|
+
|
|
38
|
+
A python library for generating media with diffusion.
|
|
39
|
+
|
|
40
|
+
## Development
|
|
41
|
+
|
|
42
|
+
The project uses [uv](https://docs.astral.sh/uv/). With it installed:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
git clone git@github.com:samirelanduk/pydiffuse.git
|
|
46
|
+
cd pydiffuse
|
|
47
|
+
uv sync
|
|
48
|
+
pre-commit install
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`uv sync` creates the virtualenv in `.venv` and installs the project with its dev dependencies from the lockfile.
|
|
52
|
+
`pre-commit install` sets up the ruff check and format hooks, which CI also enforces.
|
|
53
|
+
|
|
54
|
+
Then, to run the checks:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
uv run python -m unittest discover
|
|
58
|
+
uv run pyright
|
|
59
|
+
uv run pre-commit run --all-files
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## CLI
|
|
63
|
+
|
|
64
|
+
The library exposes most of its functionality as a Command-Line Interface.
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pydiffuse <command>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### CLIP
|
|
71
|
+
|
|
72
|
+
CLIP turns a text prompt into a set of vectors that represent its semantic meaning, which diffusion models use to condition image generation.
|
|
73
|
+
The process is split into three commands - tokenising, embedding, and encoding - each of which writes its output to a file that the next one reads.
|
|
74
|
+
See [docs/clip.md](docs/clip.md) for an explanation of what each stage does.
|
|
75
|
+
|
|
76
|
+
The embedding and encoding steps need model weights in [safetensors](https://github.com/huggingface/safetensors) format - typically a Stable Diffusion checkpoint, whose CLIP text encoder tensors are stored under `cond_stage_model`.
|
|
77
|
+
|
|
78
|
+
#### Tokenising
|
|
79
|
+
|
|
80
|
+
`tokenize` converts a prompt into the integer token IDs that CLIP uses.
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pydiffuse tokenize "a photo of a lighthouse"
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
This writes two files.
|
|
87
|
+
`tokens.json` is a list of lists of token IDs, where each inner list is a chunk of exactly 77 tokens - prompts longer than that are split over as many chunks as they need, and the last chunk is padded:
|
|
88
|
+
|
|
89
|
+
```json
|
|
90
|
+
[[49406, 320, 1125, 539, 320, 13717, 49407, 49407, ...]]
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`mappings.json` has the same shape, but pairs each token ID with the substring it came from, so you can see how the prompt was split up:
|
|
94
|
+
|
|
95
|
+
```json
|
|
96
|
+
[[["<|startoftext|>", 49406], ["a</w>", 320], ["photo</w>", 1125], ...]]
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
| Option | Default | Description |
|
|
100
|
+
| --- | --- | --- |
|
|
101
|
+
| `--tokens` | `tokens.json` | Path to save the tokens JSON to. |
|
|
102
|
+
| `--mappings` | `mappings.json` | Path to save the mappings JSON to. |
|
|
103
|
+
| `--tokenizer` | bundled tokenizer | Directory containing a custom CLIP tokenizer. |
|
|
104
|
+
|
|
105
|
+
The library bundles the standard CLIP tokenizer, so `--tokenizer` is only needed if your model was trained with a different vocabulary.
|
|
106
|
+
|
|
107
|
+
#### Embedding
|
|
108
|
+
|
|
109
|
+
`embed` looks up the vector for each token, and adds the vector for its position in the chunk.
|
|
110
|
+
The result represents each token in isolation, with no context from the rest of the prompt.
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
pydiffuse embed tokens.json model.safetensors
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
This writes `embedding.pt`, a `torch.save`-d tensor of shape `(chunks, 77, width)`, where `width` is the embedding width of the model (768 for Stable Diffusion 1.x).
|
|
117
|
+
The model must contain tensors whose keys end in `token_embedding.weight` and `position_embedding.weight`, and it must use the same vocabulary as the tokenizer that produced the tokens.
|
|
118
|
+
|
|
119
|
+
| Option | Default | Description |
|
|
120
|
+
| --- | --- | --- |
|
|
121
|
+
| `--embedding` | `embedding.pt` | Path to save the embedding to. |
|
|
122
|
+
|
|
123
|
+
#### Encoding
|
|
124
|
+
|
|
125
|
+
`encode` runs the embeddings through the CLIP transformer layers, so that each vector is adjusted by the tokens before it.
|
|
126
|
+
This is the conditioning that gets fed to a diffusion model.
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
pydiffuse encode embedding.pt model.safetensors
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
This writes `conditioning.pt`, a tensor of the same shape as the embedding.
|
|
133
|
+
The model must contain the attention, MLP and layer norm tensors for every encoder layer under `cond_stage_model`, plus the final layer norm - any number of layers is supported.
|
|
134
|
+
|
|
135
|
+
| Option | Default | Description |
|
|
136
|
+
| --- | --- | --- |
|
|
137
|
+
| `--conditioning` | `conditioning.pt` | Path to save the conditioning to. |
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# pydiffuse
|
|
2
|
+
|
|
3
|
+
[](https://github.com/samirelanduk/pydiffuse/actions/workflows/ci.yml)
|
|
4
|
+
[](https://github.com/samirelanduk/pydiffuse/blob/master/pyproject.toml)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](https://github.com/astral-sh/uv)
|
|
7
|
+
[](https://github.com/astral-sh/ruff)
|
|
8
|
+
[](https://github.com/microsoft/pyright)
|
|
9
|
+
|
|
10
|
+
A python library for generating media with diffusion.
|
|
11
|
+
|
|
12
|
+
## Development
|
|
13
|
+
|
|
14
|
+
The project uses [uv](https://docs.astral.sh/uv/). With it installed:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
git clone git@github.com:samirelanduk/pydiffuse.git
|
|
18
|
+
cd pydiffuse
|
|
19
|
+
uv sync
|
|
20
|
+
pre-commit install
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`uv sync` creates the virtualenv in `.venv` and installs the project with its dev dependencies from the lockfile.
|
|
24
|
+
`pre-commit install` sets up the ruff check and format hooks, which CI also enforces.
|
|
25
|
+
|
|
26
|
+
Then, to run the checks:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
uv run python -m unittest discover
|
|
30
|
+
uv run pyright
|
|
31
|
+
uv run pre-commit run --all-files
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## CLI
|
|
35
|
+
|
|
36
|
+
The library exposes most of its functionality as a Command-Line Interface.
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pydiffuse <command>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### CLIP
|
|
43
|
+
|
|
44
|
+
CLIP turns a text prompt into a set of vectors that represent its semantic meaning, which diffusion models use to condition image generation.
|
|
45
|
+
The process is split into three commands - tokenising, embedding, and encoding - each of which writes its output to a file that the next one reads.
|
|
46
|
+
See [docs/clip.md](docs/clip.md) for an explanation of what each stage does.
|
|
47
|
+
|
|
48
|
+
The embedding and encoding steps need model weights in [safetensors](https://github.com/huggingface/safetensors) format - typically a Stable Diffusion checkpoint, whose CLIP text encoder tensors are stored under `cond_stage_model`.
|
|
49
|
+
|
|
50
|
+
#### Tokenising
|
|
51
|
+
|
|
52
|
+
`tokenize` converts a prompt into the integer token IDs that CLIP uses.
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pydiffuse tokenize "a photo of a lighthouse"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
This writes two files.
|
|
59
|
+
`tokens.json` is a list of lists of token IDs, where each inner list is a chunk of exactly 77 tokens - prompts longer than that are split over as many chunks as they need, and the last chunk is padded:
|
|
60
|
+
|
|
61
|
+
```json
|
|
62
|
+
[[49406, 320, 1125, 539, 320, 13717, 49407, 49407, ...]]
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
`mappings.json` has the same shape, but pairs each token ID with the substring it came from, so you can see how the prompt was split up:
|
|
66
|
+
|
|
67
|
+
```json
|
|
68
|
+
[[["<|startoftext|>", 49406], ["a</w>", 320], ["photo</w>", 1125], ...]]
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
| Option | Default | Description |
|
|
72
|
+
| --- | --- | --- |
|
|
73
|
+
| `--tokens` | `tokens.json` | Path to save the tokens JSON to. |
|
|
74
|
+
| `--mappings` | `mappings.json` | Path to save the mappings JSON to. |
|
|
75
|
+
| `--tokenizer` | bundled tokenizer | Directory containing a custom CLIP tokenizer. |
|
|
76
|
+
|
|
77
|
+
The library bundles the standard CLIP tokenizer, so `--tokenizer` is only needed if your model was trained with a different vocabulary.
|
|
78
|
+
|
|
79
|
+
#### Embedding
|
|
80
|
+
|
|
81
|
+
`embed` looks up the vector for each token, and adds the vector for its position in the chunk.
|
|
82
|
+
The result represents each token in isolation, with no context from the rest of the prompt.
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
pydiffuse embed tokens.json model.safetensors
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
This writes `embedding.pt`, a `torch.save`-d tensor of shape `(chunks, 77, width)`, where `width` is the embedding width of the model (768 for Stable Diffusion 1.x).
|
|
89
|
+
The model must contain tensors whose keys end in `token_embedding.weight` and `position_embedding.weight`, and it must use the same vocabulary as the tokenizer that produced the tokens.
|
|
90
|
+
|
|
91
|
+
| Option | Default | Description |
|
|
92
|
+
| --- | --- | --- |
|
|
93
|
+
| `--embedding` | `embedding.pt` | Path to save the embedding to. |
|
|
94
|
+
|
|
95
|
+
#### Encoding
|
|
96
|
+
|
|
97
|
+
`encode` runs the embeddings through the CLIP transformer layers, so that each vector is adjusted by the tokens before it.
|
|
98
|
+
This is the conditioning that gets fed to a diffusion model.
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
pydiffuse encode embedding.pt model.safetensors
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
This writes `conditioning.pt`, a tensor of the same shape as the embedding.
|
|
105
|
+
The model must contain the attention, MLP and layer norm tensors for every encoder layer under `cond_stage_model`, plus the final layer norm - any number of layers is supported.
|
|
106
|
+
|
|
107
|
+
| Option | Default | Description |
|
|
108
|
+
| --- | --- | --- |
|
|
109
|
+
| `--conditioning` | `conditioning.pt` | Path to save the conditioning to. |
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pydiffuse"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A python library for generating media with diffusion."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
authors = [{ name = "Sam Ireland", email = "mail@samireland.com" }]
|
|
13
|
+
requires-python = ">=3.12"
|
|
14
|
+
keywords = ["diffusion", "clip", "torch", "image-generation"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Intended Audience :: Science/Research",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Programming Language :: Python :: 3.14",
|
|
24
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
25
|
+
"Topic :: Multimedia :: Graphics",
|
|
26
|
+
]
|
|
27
|
+
dependencies = [
|
|
28
|
+
"click>=8.4",
|
|
29
|
+
"safetensors>=0.8",
|
|
30
|
+
"torch>=2.13",
|
|
31
|
+
"transformers>=5.13",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.scripts]
|
|
35
|
+
pydiffuse = "pydiffuse.cli:cli"
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://github.com/samirelanduk/pydiffuse"
|
|
39
|
+
Source = "https://github.com/samirelanduk/pydiffuse"
|
|
40
|
+
Issues = "https://github.com/samirelanduk/pydiffuse/issues"
|
|
41
|
+
|
|
42
|
+
[dependency-groups]
|
|
43
|
+
dev = ["ruff==0.16.3", "pyright==1.1.411", "pre-commit==4.1.0"]
|
|
44
|
+
|
|
45
|
+
[[tool.uv.index]]
|
|
46
|
+
name = "pytorch-cpu"
|
|
47
|
+
url = "https://download.pytorch.org/whl/cpu"
|
|
48
|
+
explicit = true
|
|
49
|
+
|
|
50
|
+
[tool.uv.sources]
|
|
51
|
+
torch = [{ index = "pytorch-cpu", marker = "sys_platform == 'linux'" }]
|
|
52
|
+
|
|
53
|
+
[tool.hatch.build.targets.wheel]
|
|
54
|
+
packages = ["src/pydiffuse"]
|
|
55
|
+
|
|
56
|
+
[tool.hatch.build.targets.sdist]
|
|
57
|
+
include = ["src", "README.md", "LICENSE"]
|
|
58
|
+
|
|
59
|
+
[tool.ruff]
|
|
60
|
+
line-length = 88
|
|
61
|
+
target-version = "py312"
|
|
62
|
+
|
|
63
|
+
[tool.pyright]
|
|
64
|
+
include = ["src", "tests"]
|
|
65
|
+
pythonVersion = "3.12"
|
|
66
|
+
venvPath = "."
|
|
67
|
+
venv = ".venv"
|
|
68
|
+
typeCheckingMode = "basic"
|
|
69
|
+
reportAssignmentType = "none"
|
|
70
|
+
reportOperatorIssue = "none"
|
|
71
|
+
reportAttributeAccessIssue = "none"
|
|
72
|
+
reportReturnType = "none"
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
import click
|
|
5
|
+
import torch
|
|
6
|
+
from safetensors import safe_open
|
|
7
|
+
from transformers import CLIPTokenizer
|
|
8
|
+
|
|
9
|
+
from pydiffuse.clip import embed as clip_embed
|
|
10
|
+
from pydiffuse.clip import encode as clip_encode
|
|
11
|
+
from pydiffuse.clip import tokenize as clip_tokenize
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@click.group()
|
|
15
|
+
def cli():
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def check_parent(ctx, param, value):
|
|
20
|
+
"""Rejects an output file whose containing directory doesn't exist."""
|
|
21
|
+
|
|
22
|
+
parent = Path(value).parent
|
|
23
|
+
if not parent.exists():
|
|
24
|
+
raise click.BadParameter(f"Directory '{parent}' does not exist.")
|
|
25
|
+
return value
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@cli.command()
|
|
29
|
+
@click.argument("text")
|
|
30
|
+
@click.option(
|
|
31
|
+
"--tokens",
|
|
32
|
+
type=click.Path(dir_okay=False, writable=True),
|
|
33
|
+
callback=check_parent,
|
|
34
|
+
default="tokens.json",
|
|
35
|
+
help="Path to save the tokens JSON to.",
|
|
36
|
+
)
|
|
37
|
+
@click.option(
|
|
38
|
+
"--mappings",
|
|
39
|
+
type=click.Path(dir_okay=False, writable=True),
|
|
40
|
+
callback=check_parent,
|
|
41
|
+
default="mappings.json",
|
|
42
|
+
help="Path to save the mappings JSON to.",
|
|
43
|
+
)
|
|
44
|
+
@click.option(
|
|
45
|
+
"--tokenizer",
|
|
46
|
+
type=click.Path(exists=True, file_okay=False),
|
|
47
|
+
default=None,
|
|
48
|
+
help="Path to a custom CLIP tokenizer.",
|
|
49
|
+
)
|
|
50
|
+
def tokenize(text, tokens, mappings, tokenizer):
|
|
51
|
+
"""Tokenizes the given text with a CLIP tokenizer."""
|
|
52
|
+
|
|
53
|
+
clip_tokenizer = None
|
|
54
|
+
if tokenizer:
|
|
55
|
+
try:
|
|
56
|
+
clip_tokenizer = CLIPTokenizer.from_pretrained(tokenizer)
|
|
57
|
+
except Exception as e:
|
|
58
|
+
raise click.ClickException(f"{tokenizer} is not a CLIP tokenizer.") from e
|
|
59
|
+
token_lists, mapping_lists = clip_tokenize(text, clip_tokenizer=clip_tokenizer)
|
|
60
|
+
with open(tokens, "w") as f:
|
|
61
|
+
json.dump(token_lists, f)
|
|
62
|
+
with open(mappings, "w") as f:
|
|
63
|
+
json.dump(mapping_lists, f)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@cli.command("embed")
|
|
67
|
+
@click.argument("tokens", type=click.Path(exists=True, dir_okay=False))
|
|
68
|
+
@click.argument("model", type=click.Path(exists=True, dir_okay=False))
|
|
69
|
+
@click.option(
|
|
70
|
+
"--embedding",
|
|
71
|
+
type=click.Path(dir_okay=False, writable=True),
|
|
72
|
+
callback=check_parent,
|
|
73
|
+
default="embedding.pt",
|
|
74
|
+
help="Path to save the embedding to.",
|
|
75
|
+
)
|
|
76
|
+
def embed(tokens, model, embedding):
|
|
77
|
+
"""Embeds tokens using CLIP embedding weights from a model."""
|
|
78
|
+
|
|
79
|
+
with open(tokens) as f:
|
|
80
|
+
token_lists = json.load(f)
|
|
81
|
+
with safe_open(model, framework="pt", device="cpu") as tensors:
|
|
82
|
+
embedding_tensor = clip_embed(token_lists, tensors)
|
|
83
|
+
torch.save(embedding_tensor, embedding)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@cli.command("encode")
|
|
87
|
+
@click.argument("embedding", type=click.Path(exists=True, dir_okay=False))
|
|
88
|
+
@click.argument("model", type=click.Path(exists=True, dir_okay=False))
|
|
89
|
+
@click.option(
|
|
90
|
+
"--conditioning",
|
|
91
|
+
type=click.Path(dir_okay=False, writable=True),
|
|
92
|
+
callback=check_parent,
|
|
93
|
+
default="conditioning.pt",
|
|
94
|
+
help="Path to save the conditioning to.",
|
|
95
|
+
)
|
|
96
|
+
def encode(embedding, model, conditioning):
|
|
97
|
+
"""Encodes embeddings using CLIP encoder weights from a model."""
|
|
98
|
+
|
|
99
|
+
embedding_tensor = torch.load(embedding)
|
|
100
|
+
with safe_open(model, framework="pt", device="cpu") as tensors:
|
|
101
|
+
conditioning_tensor = clip_encode(embedding_tensor, tensors)
|
|
102
|
+
torch.save(conditioning_tensor, conditioning)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
if __name__ == "__main__":
|
|
106
|
+
cli()
|