Adeely93 commited on
Commit
20cc952
路
verified 路
1 Parent(s): 8de1d29

Add text encoder in transformers format (config.json + model.safetensors) and update the model card

Browse files
Files changed (3) hide show
  1. README.md +17 -27
  2. config.json +25 -0
  3. model.safetensors +3 -0
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- library_name: diffusers
3
  license: mit
4
  pipeline_tag: text-to-image
5
  base_model: CompVis/stable-diffusion-v1-4
@@ -32,47 +32,37 @@ We show that existing T2I safety alignment methods create an **illusion of high
32
 
33
  ## What this repository contains
34
 
35
- `SAGE.pt` (492 MB) is **only the text encoder** of Stable Diffusion v1.4 (CLIP ViT-L/14), fine-tuned with SAGE to suppress sexual content. The UNet, VAE, tokenizer and scheduler are the unchanged, stock SD v1.4 components. To use SAGE you load SD v1.4 as usual and replace the text encoder's weights with this file; there is no inference overhead.
36
 
37
- The file is a plain PyTorch checkpoint (same format as [DES](https://github.com/amoeba04/des)):
38
-
39
- | Key | Content |
40
  |---|---|
41
- | `model_state_dict` | the 196 weight tensors of the `CLIPTextModel` text encoder |
42
- | `epoch`, `embedding_dim`, `args`, ... | training metadata (SD v1.4, CoPro sexual safe/unsafe prompts, 2 epochs, lr 1e-5, batch 128, 位_safe 0.3, concept scale 205, K 15, 伪 1.0, 位_LSA 0.1, 位_ESP 2.0) |
 
 
43
 
44
- Because the checkpoint is a text encoder for the CLIP ViT-L/14 architecture (768-dim, 77 tokens), it works with Stable Diffusion **v1.x** pipelines (v1.4 and v1.5 share this encoder). It does not apply to SD 2.x, SDXL, SD 3 or FLUX.
45
 
46
  ---
47
 
48
  ## Use this model
49
 
50
- ### Option 1: with 馃 diffusers (a few lines)
51
 
52
  ```python
53
  import torch
54
  from diffusers import StableDiffusionPipeline
55
- from huggingface_hub import hf_hub_download
56
-
57
- device = "cuda"
58
-
59
- # 1) Load stock Stable Diffusion v1.4
60
- pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16).to(device)
61
-
62
- # 2) Download SAGE.pt and take the text-encoder weights out of it
63
- ckpt_path = hf_hub_download(repo_id="Adeely93/SAGE", filename="SAGE.pt")
64
- state = torch.load(ckpt_path, map_location="cpu", weights_only=False)["model_state_dict"]
65
 
66
- # 3) Match the key names of the installed transformers version
67
- # (the checkpoint was saved with a "text_model." prefix; transformers >= 5 names the weights without it)
68
- model_keys = set(pipe.text_encoder.state_dict().keys())
69
- state = {(k if k in model_keys else k[len("text_model."):]): v for k, v in state.items()}
70
 
71
- # 4) Swap the weights into the pipeline's text encoder
72
- missing, unexpected = pipe.text_encoder.load_state_dict(state, strict=False)
73
- assert not [k for k in missing if "position_ids" not in k], missing
 
74
 
75
- # 5) Generate as usual: the pipeline is now safety-aligned
76
  image = pipe("A horned owl with a graduation cap and diploma.").images[0]
77
  image.save("owl.png")
78
  ```
 
1
  ---
2
+ library_name: transformers
3
  license: mit
4
  pipeline_tag: text-to-image
5
  base_model: CompVis/stable-diffusion-v1-4
 
32
 
33
  ## What this repository contains
34
 
35
+ SAGE fine-tunes **only the text encoder** of Stable Diffusion v1.4 (CLIP ViT-L/14). The UNet, VAE, tokenizer and scheduler are the unchanged, stock SD v1.4 components, so using SAGE means loading SD v1.4 as usual with this text encoder plugged in. There is no inference overhead.
36
 
37
+ | File | What it is |
 
 
38
  |---|---|
39
+ | `config.json` + `model.safetensors` | the SAGE text encoder in standard 馃 transformers format, loadable with `CLIPTextModel.from_pretrained("Adeely93/SAGE")` |
40
+ | `SAGE.pt` | the same weights as a raw PyTorch checkpoint (`{"model_state_dict": ..., "args": ...}`, the DES format), used by the official code and the Colab demo |
41
+
42
+ Training setup: SD v1.4, CoPro sexual safe/unsafe prompts, 2 epochs, lr 1e-5, batch 128, 位_safe 0.3, concept scale 205, K 15, 伪 1.0, 位_LSA 0.1, 位_ESP 2.0.
43
 
44
+ The weights are a CLIP ViT-L/14 text encoder (768-dim, 77 tokens), so they work with Stable Diffusion **v1.x** pipelines (v1.4 and v1.5 share this encoder). They do not apply to SD 2.x, SDXL, SD 3 or FLUX.
45
 
46
  ---
47
 
48
  ## Use this model
49
 
50
+ ### Option 1: with 馃 diffusers (recommended)
51
 
52
  ```python
53
  import torch
54
  from diffusers import StableDiffusionPipeline
55
+ from transformers import CLIPTextModel
 
 
 
 
 
 
 
 
 
56
 
57
+ # 1) Load the SAGE text encoder
58
+ text_encoder = CLIPTextModel.from_pretrained("Adeely93/SAGE", torch_dtype=torch.float16)
 
 
59
 
60
+ # 2) Load stock Stable Diffusion v1.4 with that text encoder plugged in
61
+ pipe = StableDiffusionPipeline.from_pretrained(
62
+ "CompVis/stable-diffusion-v1-4", text_encoder=text_encoder, torch_dtype=torch.float16
63
+ ).to("cuda")
64
 
65
+ # 3) Generate as usual: the pipeline is now safety-aligned
66
  image = pipe("A horned owl with a graduation cap and diploma.").images[0]
67
  image.save("owl.png")
68
  ```
config.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "CompVis/stable-diffusion-v1-4",
3
+ "architectures": [
4
+ "CLIPTextModel"
5
+ ],
6
+ "attention_dropout": 0.0,
7
+ "bos_token_id": 0,
8
+ "dropout": 0.0,
9
+ "eos_token_id": 2,
10
+ "hidden_act": "quick_gelu",
11
+ "hidden_size": 768,
12
+ "initializer_factor": 1.0,
13
+ "initializer_range": 0.02,
14
+ "intermediate_size": 3072,
15
+ "layer_norm_eps": 1e-05,
16
+ "max_position_embeddings": 77,
17
+ "model_type": "clip_text_model",
18
+ "num_attention_heads": 12,
19
+ "num_hidden_layers": 12,
20
+ "pad_token_id": 1,
21
+ "projection_dim": 512,
22
+ "torch_dtype": "float32",
23
+ "transformers_version": "4.46.3",
24
+ "vocab_size": 49408
25
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2d3b4629e9fc006473b6e7c991c04dca2fa221d80c1283dcc625585ccbe201ec
3
+ size 492265168