Repository navigation
Expand file tree
/
Copy pathapi.rs
More file actions
151 lines (130 loc) · 4.45 KB
/
Copy pathapi.rs
File metadata and controls
151 lines (130 loc) · 4.45 KB
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
//! Stable Diffusion API — wraps the pipeline with OpenAI-compatible types.
//!
//! Endpoint: `/v1/images/generations`
use super::clip::ClipEncoder;
use super::scheduler::{DdimScheduler, SchedulerConfig};
use super::unet;
use super::vae;
use crate::hpc::models::api_types::*;
/// Stable Diffusion API wrapper.
pub struct StableDiffusionApi {
clip: ClipEncoder,
scheduler_config: SchedulerConfig,
}
impl StableDiffusionApi {
pub fn new(clip: ClipEncoder) -> Self {
Self {
clip,
scheduler_config: SchedulerConfig::default(),
}
}
/// `/v1/images/generations`
pub fn generate(&self, req: &ImageGenerationRequest) -> ImageResponse {
let (w, h) = req.dimensions();
let latent_h = h / 8;
let latent_w = w / 8;
let n = req.n.unwrap_or(1);
let seed = req.seed.unwrap_or(42);
let prompt_tokens = req.prompt_tokens.as_deref().unwrap_or(&[]);
let text_embeddings = self.clip.encode(prompt_tokens);
let scheduler = DdimScheduler::new(SchedulerConfig {
num_inference_steps: 20,
..self.scheduler_config.clone()
});
let mut images = Vec::with_capacity(n);
for img_idx in 0..n {
let latent_size = 4 * latent_h * latent_w;
let mut latent = generate_noise(latent_size, seed.wrapping_add(img_idx as u64));
for &t in &scheduler.timesteps {
let noise_pred = unet::predict_noise(&latent, &text_embeddings, t as f32);
latent = scheduler.step(&noise_pred, t, &latent);
}
let pixels = vae::decode(&latent, latent_h, latent_w);
let rgb = vae::to_rgb_u8(&pixels, 3, h, w);
let b64 = base64_placeholder(&rgb);
images.push(ImageData {
b64_json: Some(b64),
url: None,
revised_prompt: Some(req.prompt.clone()),
});
}
ImageResponse {
created: 0,
data: images,
}
}
/// `/v1/models/{id}`
pub fn model_info() -> Model {
Model::new("stable-diffusion-v1-5", "stabilityai", 0)
}
}
fn generate_noise(size: usize, seed: u64) -> Vec<f32> {
let mut state = seed;
let mut noise = Vec::with_capacity(size);
for _ in 0..size {
state = state.wrapping_mul(6364136223846793005).wrapping_add(1);
let bits = ((state >> 32) as u32) as f32 / u32::MAX as f32;
noise.push(bits * 2.0 - 1.0);
}
noise
}
fn base64_placeholder(rgb: &[u8]) -> String {
format!("raw_rgb_{}_bytes", rgb.len())
}
#[cfg(test)]
mod tests {
use super::super::clip::{ClipWeights, CLIP_EMBED_DIM, CLIP_MAX_SEQ, CLIP_VOCAB_SIZE};
use super::*;
fn dummy_clip() -> ClipEncoder {
ClipEncoder::new(ClipWeights {
token_embedding: vec![0.0; CLIP_VOCAB_SIZE * CLIP_EMBED_DIM],
position_embedding: vec![0.0; CLIP_MAX_SEQ * CLIP_EMBED_DIM],
layers: Vec::new(),
ln_final_weight: vec![1.0; CLIP_EMBED_DIM],
ln_final_bias: vec![0.0; CLIP_EMBED_DIM],
})
}
#[test]
fn test_default_request() {
let req = ImageGenerationRequest::default();
assert_eq!(req.dimensions(), (512, 512));
assert_eq!(req.n, Some(1));
}
#[test]
fn test_generate_returns_image() {
let api = StableDiffusionApi::new(dummy_clip());
let req = ImageGenerationRequest {
prompt: "a cat".into(),
prompt_tokens: Some(vec![0, 1, 2]),
..Default::default()
};
let resp = api.generate(&req);
assert_eq!(resp.data.len(), 1);
assert!(resp.data[0].b64_json.is_some());
assert_eq!(resp.data[0].revised_prompt.as_deref(), Some("a cat"));
}
#[test]
fn test_generate_multiple() {
let api = StableDiffusionApi::new(dummy_clip());
let req = ImageGenerationRequest {
prompt: "test".into(),
prompt_tokens: Some(vec![0]),
n: Some(3),
..Default::default()
};
let resp = api.generate(&req);
assert_eq!(resp.data.len(), 3);
}
#[test]
fn test_deterministic() {
let n1 = generate_noise(100, 42);
let n2 = generate_noise(100, 42);
assert_eq!(n1, n2);
}
#[test]
fn test_model_info() {
let m = StableDiffusionApi::model_info();
assert_eq!(m.id, "stable-diffusion-v1-5");
assert_eq!(m.object, "model");
}
}