hedos-kernel 1.4.0

The hedos kernel: model records, registry, discovery, install planning, and resolution.
Documentation
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! The diffusers pipeline-family registry: maps a `model_index.json`
//! `_class_name` to a modality/capability/parameter profile, refined by the
//! model's scheduler and repo name. This is how a discovered diffusers bundle
//! gets its image/video/audio shape and its sampler parameter schema.

use std::collections::HashSet;
use std::path::Path;
use std::sync::LazyLock;

use crate::records::{Capability, JsonValue, Modality, ParamSpec, ParamType};

/// The `_class_name` declared in a diffusers `model_index.json` (or scheduler
/// config), if the file reads and parses as an object.
pub(crate) fn diffusers_pipeline_class(path: &Path) -> Option<String> {
    let bytes = std::fs::read(path).ok()?;
    let JsonValue::Object(index) = serde_json::from_slice::<JsonValue>(&bytes).ok()? else {
        return None;
    };
    index
        .get("_class_name")
        .and_then(JsonValue::as_str)
        .map(str::to_owned)
}

/// The resolved shape of a diffusers pipeline: what it produces and the sampler
/// parameters it exposes.
#[derive(Debug, Clone, PartialEq)]
pub struct DiffusersPipelineProfile {
    /// What the pipeline generates.
    pub modality: Modality,
    /// The capabilities it serves.
    pub capabilities: Vec<Capability>,
    /// Its tunable parameter schema.
    pub params: Vec<ParamSpec>,
}

/// The facts read from a pipeline's `scheduler/scheduler_config.json` that a
/// refinement matches against.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct SchedulerFacts {
    /// The scheduler's `_class_name`.
    pub class_name: Option<String>,
    /// Its `timestep_spacing`.
    pub timestep_spacing: Option<String>,
}

impl SchedulerFacts {
    /// Scheduler facts from a class name and timestep spacing.
    pub fn new(class_name: Option<String>, timestep_spacing: Option<String>) -> Self {
        Self {
            class_name,
            timestep_spacing,
        }
    }
}

/// A conditional parameter override applied to a family when the model's
/// scheduler (and optionally its repo name) matches — e.g. the SDXL "turbo"
/// low-step preset.
#[derive(Debug, Clone, PartialEq)]
pub struct PipelineRefinement {
    /// Scheduler `_class_name`s this refinement applies to.
    pub scheduler_classes: HashSet<String>,
    /// A required `timestep_spacing`, if the refinement is spacing-specific.
    pub timestep_spacing: Option<String>,
    /// Repo-name tokens that must be present (any one), if name-gated.
    pub name_signals: HashSet<String>,
    /// The parameter specs to overlay (replacing by key, or appending).
    pub param_overrides: Vec<ParamSpec>,
}

impl PipelineRefinement {
    /// Whether this refinement applies given the scheduler `facts` and an optional
    /// `repo_hint`. The scheduler class must match; a set `timestep_spacing` must
    /// match; and if any `name_signals` are declared, a token of `repo_hint` must
    /// be among them.
    pub fn matches(&self, facts: &SchedulerFacts, repo_hint: Option<&str>) -> bool {
        let Some(class_name) = &facts.class_name else {
            return false;
        };
        if !self.scheduler_classes.contains(class_name) {
            return false;
        }
        if let Some(spacing) = &self.timestep_spacing
            && facts.timestep_spacing.as_deref() != Some(spacing.as_str())
        {
            return false;
        }
        if self.name_signals.is_empty() {
            return true;
        }
        let Some(repo_hint) = repo_hint else {
            return false;
        };
        let tokens = name_tokens(repo_hint);
        self.name_signals
            .iter()
            .any(|signal| tokens.contains(signal))
    }
}

/// The lowercase alphanumeric tokens of a repo name (`"SDXL-Turbo/v1"` →
/// `{"sdxl", "turbo", "v1"}`).
fn name_tokens(repo_hint: &str) -> HashSet<String> {
    repo_hint
        .to_lowercase()
        .split(|ch: char| !ch.is_alphanumeric())
        .filter(|token| !token.is_empty())
        .map(str::to_owned)
        .collect()
}

/// A family of diffusers pipeline classes that share a modality/capability/
/// parameter profile (e.g. all SDXL pipelines), plus any scheduler refinements.
#[derive(Debug, Clone, PartialEq)]
pub struct PipelineFamily {
    /// A stable family id (`"stable-diffusion-xl"`).
    pub id: String,
    /// The `_class_name`s that belong to this family.
    pub class_names: HashSet<String>,
    /// What the family generates.
    pub modality: Modality,
    /// The capabilities it serves (empty for edit/upscale/video/audio families).
    pub capabilities: Vec<Capability>,
    /// The base parameter schema.
    pub params: Vec<ParamSpec>,
    /// Scheduler-conditional parameter refinements.
    pub refinements: Vec<PipelineRefinement>,
}

impl PipelineFamily {
    fn new(
        id: &str,
        class_names: &[&str],
        modality: Modality,
        capabilities: Vec<Capability>,
        params: Vec<ParamSpec>,
        refinements: Vec<PipelineRefinement>,
    ) -> Self {
        Self {
            id: id.to_owned(),
            class_names: class_names.iter().map(|name| (*name).to_owned()).collect(),
            modality,
            capabilities,
            params,
            refinements,
        }
    }
}

/// The registry of diffusers pipeline families. Look up a `_class_name` to get
/// its [`DiffusersPipelineProfile`].
#[derive(Debug, Clone)]
pub struct PipelineFamilyRegistry {
    /// The known families.
    pub families: Vec<PipelineFamily>,
}

impl PipelineFamilyRegistry {
    /// A registry over `families`.
    pub fn new(families: Vec<PipelineFamily>) -> Self {
        Self { families }
    }

    /// The process-wide built-in registry, built once on first use. Prefer this
    /// over [`builtin`](Self::builtin) at call sites — identification runs it per
    /// model, and the table is immutable.
    pub fn shared() -> &'static Self {
        static BUILTIN: LazyLock<PipelineFamilyRegistry> =
            LazyLock::new(PipelineFamilyRegistry::builtin);
        &BUILTIN
    }

    /// The family owning `class_name`, if any.
    pub fn family(&self, class_name: &str) -> Option<&PipelineFamily> {
        self.families
            .iter()
            .find(|family| family.class_names.contains(class_name))
    }

    /// The resolved profile for `class_name`, applying the first scheduler
    /// refinement that matches `scheduler`/`repo_hint`. `None` if the class name
    /// is unknown.
    pub fn profile(
        &self,
        class_name: &str,
        scheduler: Option<&SchedulerFacts>,
        repo_hint: Option<&str>,
    ) -> Option<DiffusersPipelineProfile> {
        let family = self.family(class_name)?;
        let mut params = family.params.clone();
        if let Some(scheduler) = scheduler
            && let Some(refinement) = family
                .refinements
                .iter()
                .find(|refinement| refinement.matches(scheduler, repo_hint))
        {
            for overlay in &refinement.param_overrides {
                if let Some(index) = params.iter().position(|spec| spec.key == overlay.key) {
                    params[index] = overlay.clone();
                } else {
                    params.push(overlay.clone());
                }
            }
        }
        Some(DiffusersPipelineProfile {
            modality: family.modality.clone(),
            capabilities: family.capabilities.clone(),
            params,
        })
    }

    /// The built-in family table covering the common diffusers pipelines.
    pub fn builtin() -> Self {
        Self::new(vec![
            PipelineFamily::new(
                "flux",
                &["FluxPipeline"],
                Modality::image(),
                vec![Capability::image()],
                flux_params(),
                Vec::new(),
            ),
            PipelineFamily::new(
                "stable-diffusion",
                &["StableDiffusionPipeline"],
                Modality::image(),
                vec![Capability::image()],
                sd1_params(),
                vec![turbo_refinement()],
            ),
            PipelineFamily::new(
                "stable-diffusion-xl",
                &["StableDiffusionXLPipeline"],
                Modality::image(),
                vec![Capability::image()],
                sdxl_params(),
                vec![turbo_refinement()],
            ),
            PipelineFamily::new(
                "stable-diffusion-3",
                &["StableDiffusion3Pipeline"],
                Modality::image(),
                vec![Capability::image()],
                sd3_params(),
                Vec::new(),
            ),
            PipelineFamily::new(
                "pixart",
                &["PixArtAlphaPipeline", "PixArtSigmaPipeline"],
                Modality::image(),
                vec![Capability::image()],
                pixart_params(),
                Vec::new(),
            ),
            PipelineFamily::new(
                "kandinsky",
                &["KandinskyV22Pipeline", "KandinskyV22CombinedPipeline"],
                Modality::image(),
                vec![Capability::image()],
                kandinsky_params(),
                Vec::new(),
            ),
            PipelineFamily::new(
                "latent-consistency",
                &["LatentConsistencyModelPipeline"],
                Modality::image(),
                vec![Capability::image()],
                lcm_params(),
                Vec::new(),
            ),
            PipelineFamily::new(
                "image-edit",
                &[
                    "StableDiffusionImg2ImgPipeline",
                    "StableDiffusionInpaintPipeline",
                    "StableDiffusionXLImg2ImgPipeline",
                    "StableDiffusionXLInpaintPipeline",
                    "StableDiffusion3Img2ImgPipeline",
                    "StableDiffusion3InpaintPipeline",
                    "FluxImg2ImgPipeline",
                    "FluxInpaintPipeline",
                    "KandinskyV22Img2ImgPipeline",
                    "LatentConsistencyModelImg2ImgPipeline",
                ],
                Modality::image(),
                Vec::new(),
                Vec::new(),
                Vec::new(),
            ),
            PipelineFamily::new(
                "image-upscale",
                &[
                    "StableDiffusionUpscalePipeline",
                    "StableDiffusionLatentUpscalePipeline",
                ],
                Modality::image(),
                Vec::new(),
                Vec::new(),
                Vec::new(),
            ),
            PipelineFamily::new(
                "video",
                &[
                    "TextToVideoSDPipeline",
                    "AnimateDiffPipeline",
                    "CogVideoXPipeline",
                    "StableVideoDiffusionPipeline",
                    "HunyuanVideoPipeline",
                    "LTXPipeline",
                    "WanPipeline",
                ],
                Modality::video(),
                Vec::new(),
                Vec::new(),
                Vec::new(),
            ),
            PipelineFamily::new(
                "audio",
                &[
                    "AudioLDMPipeline",
                    "AudioLDM2Pipeline",
                    "MusicLDMPipeline",
                    "StableAudioPipeline",
                ],
                Modality::audio(),
                Vec::new(),
                Vec::new(),
                Vec::new(),
            ),
        ])
    }
}

fn int_param(key: &str, default: i64, min: i64, max: i64) -> ParamSpec {
    ParamSpec {
        key: key.to_owned(),
        param_type: ParamType::Int,
        default_value: Some(JsonValue::Int(default)),
        range: Some(vec![JsonValue::Int(min), JsonValue::Int(max)]),
        values: None,
    }
}

fn float_param(key: &str, default: f64, min: f64, max: f64) -> ParamSpec {
    ParamSpec {
        key: key.to_owned(),
        param_type: ParamType::Float,
        default_value: Some(JsonValue::Double(default)),
        range: Some(vec![JsonValue::Double(min), JsonValue::Double(max)]),
        values: None,
    }
}

fn size_param(default: &str, values: &[&str]) -> ParamSpec {
    ParamSpec {
        key: "size".to_owned(),
        param_type: ParamType::Enum,
        default_value: Some(JsonValue::String(default.to_owned())),
        range: None,
        values: Some(values.iter().map(|value| (*value).to_owned()).collect()),
    }
}

/// A bare parameter with only a key and type (no default/range/values) — `seed`
/// and `negative_prompt`.
fn bare_param(key: &str, param_type: ParamType) -> ParamSpec {
    ParamSpec {
        key: key.to_owned(),
        param_type,
        default_value: None,
        range: None,
        values: None,
    }
}

fn seed() -> ParamSpec {
    bare_param("seed", ParamType::Int)
}

fn negative_prompt() -> ParamSpec {
    bare_param("negative_prompt", ParamType::String)
}

fn flux_params() -> Vec<ParamSpec> {
    vec![
        int_param("steps", 4, 1, 50),
        float_param("guidance", 4.0, 0.0, 10.0),
        size_param("1024x1024", &["512x512", "768x768", "1024x1024"]),
        seed(),
    ]
}

fn sd1_params() -> Vec<ParamSpec> {
    vec![
        int_param("steps", 30, 1, 75),
        float_param("guidance", 7.5, 0.0, 15.0),
        size_param("512x512", &["512x512", "576x576", "640x640", "768x768"]),
        seed(),
        negative_prompt(),
    ]
}

fn sdxl_params() -> Vec<ParamSpec> {
    vec![
        int_param("steps", 30, 1, 75),
        float_param("guidance", 7.0, 0.0, 15.0),
        size_param(
            "1024x1024",
            &["768x768", "1024x1024", "1152x896", "896x1152"],
        ),
        seed(),
        negative_prompt(),
    ]
}

fn sd3_params() -> Vec<ParamSpec> {
    vec![
        int_param("steps", 28, 1, 75),
        float_param("guidance", 7.0, 0.0, 15.0),
        size_param(
            "1024x1024",
            &["768x768", "1024x1024", "1152x896", "896x1152"],
        ),
        seed(),
        negative_prompt(),
    ]
}

fn pixart_params() -> Vec<ParamSpec> {
    vec![
        int_param("steps", 20, 1, 75),
        float_param("guidance", 4.5, 0.0, 15.0),
        size_param("1024x1024", &["512x512", "768x768", "1024x1024"]),
        seed(),
        negative_prompt(),
    ]
}

fn kandinsky_params() -> Vec<ParamSpec> {
    vec![
        int_param("steps", 30, 1, 75),
        float_param("guidance", 4.0, 0.0, 15.0),
        size_param("768x768", &["512x512", "768x768", "1024x1024"]),
        seed(),
        negative_prompt(),
    ]
}

fn lcm_params() -> Vec<ParamSpec> {
    vec![
        int_param("steps", 4, 1, 8),
        float_param("guidance", 1.5, 0.0, 2.0),
        size_param("512x512", &["512x512", "768x768"]),
        seed(),
    ]
}

/// The "turbo/lightning/lcm" low-step refinement shared by the SD and SDXL
/// families: fewer steps and (near-)zero guidance under the trailing-spacing
/// ancestral scheduler.
fn turbo_refinement() -> PipelineRefinement {
    PipelineRefinement {
        scheduler_classes: ["EulerAncestralDiscreteScheduler"]
            .into_iter()
            .map(str::to_owned)
            .collect(),
        timestep_spacing: Some("trailing".to_owned()),
        name_signals: ["turbo", "lightning", "lcm"]
            .into_iter()
            .map(str::to_owned)
            .collect(),
        param_overrides: vec![
            int_param("steps", 2, 1, 8),
            float_param("guidance", 0.0, 0.0, 2.0),
        ],
    }
}