ComfyUI models that agree on their nodes share an environment; a licence says what a machine can match
CI / test (push) Failing after 6m21s
CI / test (push) Failing after 6m21s
A ComfyUI model may name an environment: the models of one environment agree on every custom node they both name and on every package range, so one replica can carry all of them and ComfyUI moves between their weights itself rather than the card being handed over with a cold start each time. The catalog refuses an environment that cannot share a Python, naming both models; Catalog.Environments gives each one's models, the union of their nodes and their weight sources, for placement to build on. A licence gains an id (SPDX where there is one), whether its output may be used commercially, the regions it excludes, a revenue cap and the attribution it asks for, so a person's or a site's choice of licences can be matched without reading prose. BACKLOG gains section H: ComfyUI as a platform. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
0639edfd3b
commit
b43e66c95e
7 files changed
+549
No files matched your search
@@ -518,12 +518,53 @@ func (m Model) MakesMeshes() bool {
|
||||
// License is the terms what a model makes comes under: said beside every
|
||||
// result, and written into a mesh, which travels without the API.
|
||||
type License struct {
|
||||
// ID names the licence for a machine: an SPDX identifier where there is
|
||||
// one (MIT, Apache-2.0, CC-BY-NC-4.0), else a lowercase name of the
|
||||
// licence's own (stability-community, tencent-hunyuan-community). What a
|
||||
// person's or a site's choice of licences matches on. Name is for people.
|
||||
ID string `json:"id,omitempty" koanf:"id"`
|
||||
// Name is the licence's own: MIT, Apache-2.0.
|
||||
Name string `json:"name" koanf:"name"`
|
||||
URL string `json:"url,omitempty" koanf:"url"`
|
||||
// Notice is what must accompany what was made: attribution, terms on
|
||||
// the output, what the weights were trained on.
|
||||
Notice string `json:"notice,omitempty" koanf:"notice"`
|
||||
// Commercial says whether what is made may be used commercially: nil
|
||||
// is not said, which is not the same as yes.
|
||||
Commercial *bool `json:"commercial,omitempty" koanf:"commercial"`
|
||||
// ExcludedRegions are where the licence grants nothing: ISO 3166 codes,
|
||||
// and EU for the union (Hunyuan's: EU, UK, KR).
|
||||
ExcludedRegions []string `json:"excluded_regions,omitempty" koanf:"excluded_regions"`
|
||||
// RevenueCapUSD is the yearly revenue above which the licence asks for
|
||||
// another agreement (Stability's community licence: 1000000). Zero is
|
||||
// none.
|
||||
RevenueCapUSD int64 `json:"revenue_cap_usd,omitempty" koanf:"revenue_cap_usd"`
|
||||
// Attribution is what a product using the output must show: "Powered by
|
||||
// Stability AI".
|
||||
Attribution string `json:"attribution,omitempty" koanf:"attribution"`
|
||||
}
|
||||
|
||||
var (
|
||||
licenseID = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9.+-]{0,63}$`)
|
||||
regionID = regexp.MustCompile(`^[A-Z]{2}$`)
|
||||
)
|
||||
|
||||
func (l License) validate(model string) error {
|
||||
if strings.TrimSpace(l.Name) == "" {
|
||||
return fmt.Errorf("model %q: license.name is required: a licence nobody can name is none", model)
|
||||
}
|
||||
if l.ID != "" && !licenseID.MatchString(l.ID) {
|
||||
return fmt.Errorf("model %q: license.id %q is not an SPDX-style identifier (letters, digits, '.', '+', '-')", model, l.ID)
|
||||
}
|
||||
for _, r := range l.ExcludedRegions {
|
||||
if !regionID.MatchString(r) {
|
||||
return fmt.Errorf("model %q: license.excluded_regions %q is not an ISO 3166 code in capitals (EU for the union)", model, r)
|
||||
}
|
||||
}
|
||||
if l.RevenueCapUSD < 0 {
|
||||
return fmt.Errorf("model %q: license.revenue_cap_usd is negative", model)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EngineComfyUI is the engine that runs ComfyUI on a node.
|
||||
@@ -555,6 +596,11 @@ type ComfyUI struct {
|
||||
// Nodes are custom nodes the workflow needs, put in each replica's own
|
||||
// custom_nodes at the commit named.
|
||||
Nodes []CustomNode `json:"nodes,omitempty" koanf:"nodes"`
|
||||
// Environment names the models that may share one ComfyUI replica: every
|
||||
// model of an environment agrees on the custom nodes they both name, so
|
||||
// one replica can carry all of them and ComfyUI moves between their
|
||||
// weights itself. Empty is an environment of the model alone.
|
||||
Environment string `json:"environment,omitempty" koanf:"environment"`
|
||||
}
|
||||
|
||||
// CustomNode is a ComfyUI node from a git repository.
|
||||
@@ -763,6 +809,9 @@ func New(models []Model) (*Catalog, error) {
|
||||
}
|
||||
canaryOf[target.Name] = name
|
||||
}
|
||||
if _, err := c.environments(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
@@ -897,6 +946,11 @@ func (m Model) validate() error {
|
||||
if m.Source == "" {
|
||||
return fmt.Errorf("model %q: source is required", m.Name)
|
||||
}
|
||||
if m.License != nil {
|
||||
if err := m.License.validate(m.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// A page, not weights: a node fetches the HTML as one file and the
|
||||
// engine fails at start, on a path that looks right. For vLLM it was
|
||||
// read as the repository id when the model was made; only a source
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Environments: the ComfyUI models that may share one replica.
|
||||
//
|
||||
// Every ComfyUI model used to run in a replica of its own, so two models on
|
||||
// one card took turns with a cold start each time: flux, then a mesh model,
|
||||
// then flux again, each loading ComfyUI and its weights from nothing. ComfyUI
|
||||
// itself moves between models' weights in one process; what keeps two models
|
||||
// out of one process is their custom nodes, which share one Python. An
|
||||
// environment is a set of models whose nodes can: the catalog checks it, and
|
||||
// placement may then run one replica for all of them.
|
||||
|
||||
// Environment is one environment's models and what a replica of it carries.
|
||||
type Environment struct {
|
||||
Name string `json:"name"`
|
||||
// Models are the catalog models in it, in catalog order.
|
||||
Models []string `json:"models"`
|
||||
// Nodes are every custom node any of them names, once each.
|
||||
Nodes []CustomNode `json:"nodes,omitempty"`
|
||||
// Sources are every weights source its models load, once each: what a
|
||||
// replica of it must have on its node.
|
||||
Sources []string `json:"sources,omitempty"`
|
||||
}
|
||||
|
||||
// ComfyEnvironment is the environment a ComfyUI model is in, or "" for one in
|
||||
// none, which is an environment of its own.
|
||||
func (m Model) ComfyEnvironment() string {
|
||||
if m.ComfyUI == nil {
|
||||
return ""
|
||||
}
|
||||
return m.ComfyUI.Environment
|
||||
}
|
||||
|
||||
var envName = regexp.MustCompile(`^[a-z0-9][a-z0-9._-]{0,62}$`)
|
||||
|
||||
// Environments are the catalog's named environments, by name.
|
||||
func (c *Catalog) Environments() []Environment {
|
||||
envs, _ := c.environments()
|
||||
return envs
|
||||
}
|
||||
|
||||
// EnvironmentOf is the environment a model is in, when it is in a named one.
|
||||
func (c *Catalog) EnvironmentOf(model string) (Environment, bool) {
|
||||
m, ok := c.Resolve(model)
|
||||
if !ok || m.ComfyEnvironment() == "" {
|
||||
return Environment{}, false
|
||||
}
|
||||
for _, e := range c.Environments() {
|
||||
if e.Name == m.ComfyEnvironment() {
|
||||
return e, true
|
||||
}
|
||||
}
|
||||
return Environment{}, false
|
||||
}
|
||||
|
||||
// environments builds the named environments and refuses one whose models
|
||||
// cannot share a replica: two commits of one node, or two ranges of one
|
||||
// package, of which one Python can hold only one.
|
||||
func (c *Catalog) environments() ([]Environment, error) {
|
||||
byName := map[string]*Environment{}
|
||||
var order []string
|
||||
// Who brought each node and each package into each environment, to name
|
||||
// both models when two disagree.
|
||||
nodeFrom := map[string]map[string]string{} // env -> repo -> model
|
||||
nodeAt := map[string]map[string]string{} // env -> repo -> commit
|
||||
pkgFrom := map[string]map[string]string{} // env -> package -> model
|
||||
pkgSpec := map[string]map[string]string{} // env -> package -> spec
|
||||
for _, name := range c.order {
|
||||
m := c.models[name]
|
||||
env := m.ComfyEnvironment()
|
||||
if env == "" {
|
||||
continue
|
||||
}
|
||||
if !envName.MatchString(env) {
|
||||
return nil, fmt.Errorf("model %q: comfyui.environment %q is not a name of lowercase letters, digits, '.', '_' and '-'",
|
||||
name, env)
|
||||
}
|
||||
if !m.ServesComfyUI() || m.External != nil {
|
||||
return nil, fmt.Errorf("model %q: comfyui.environment is for a model the cluster's ComfyUI serves", name)
|
||||
}
|
||||
e := byName[env]
|
||||
if e == nil {
|
||||
e = &Environment{Name: env}
|
||||
byName[env] = e
|
||||
order = append(order, env)
|
||||
nodeFrom[env], nodeAt[env] = map[string]string{}, map[string]string{}
|
||||
pkgFrom[env], pkgSpec[env] = map[string]string{}, map[string]string{}
|
||||
}
|
||||
e.Models = append(e.Models, name)
|
||||
for _, src := range m.WeightSources() {
|
||||
if !slices.Contains(e.Sources, src) {
|
||||
e.Sources = append(e.Sources, src)
|
||||
}
|
||||
}
|
||||
for _, n := range m.ComfyUI.Nodes {
|
||||
repo := strings.TrimSuffix(strings.TrimRight(n.Repo, "/"), ".git")
|
||||
if at, seen := nodeAt[env][repo]; seen {
|
||||
if at != n.Commit {
|
||||
return nil, fmt.Errorf("environment %q: models %q and %q name custom node %s at different commits (%s, %s): "+
|
||||
"one replica holds one of them", env, nodeFrom[env][repo], name, n.Repo, short(at), short(n.Commit))
|
||||
}
|
||||
} else {
|
||||
nodeAt[env][repo], nodeFrom[env][repo] = n.Commit, name
|
||||
e.Nodes = append(e.Nodes, CustomNode{Repo: n.Repo, Commit: n.Commit})
|
||||
}
|
||||
for _, r := range n.Requirements {
|
||||
pkg, spec := requirementName(r), normalizeSpec(r)
|
||||
if prev, seen := pkgSpec[env][pkg]; seen {
|
||||
if prev != spec {
|
||||
return nil, fmt.Errorf("environment %q: models %q and %q ask for %s as %q and %q: "+
|
||||
"one Python holds one version; name the same range in both",
|
||||
env, pkgFrom[env][pkg], name, pkg, prev, spec)
|
||||
}
|
||||
continue
|
||||
}
|
||||
pkgSpec[env][pkg], pkgFrom[env][pkg] = spec, name
|
||||
// The requirement goes with the node that first named it:
|
||||
// what the replica installs is the union, and each once.
|
||||
i := slices.IndexFunc(e.Nodes, func(x CustomNode) bool {
|
||||
return strings.TrimSuffix(strings.TrimRight(x.Repo, "/"), ".git") == repo
|
||||
})
|
||||
e.Nodes[i].Requirements = append(e.Nodes[i].Requirements, strings.TrimSpace(r))
|
||||
}
|
||||
}
|
||||
}
|
||||
out := make([]Environment, 0, len(order))
|
||||
for _, n := range order {
|
||||
out = append(out, *byName[n])
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// requirementName is a requirement's package, as PyPI compares names.
|
||||
func requirementName(spec string) string {
|
||||
s := strings.TrimSpace(spec)
|
||||
if i := strings.IndexAny(s, "<>=!~;[ "); i >= 0 {
|
||||
s = s[:i]
|
||||
}
|
||||
return strings.NewReplacer("_", "-", ".", "-").Replace(strings.ToLower(s))
|
||||
}
|
||||
|
||||
// normalizeSpec is a requirement without its spaces and with its package
|
||||
// named as PyPI compares it, so "transformers >=4.50, <5" and
|
||||
// "Transformers>=4.50,<5" are one range.
|
||||
func normalizeSpec(spec string) string {
|
||||
s := strings.Join(strings.Fields(spec), "")
|
||||
name := requirementName(s)
|
||||
i := strings.IndexAny(s, "<>=!~;[")
|
||||
if i < 0 {
|
||||
return name
|
||||
}
|
||||
return name + s[i:]
|
||||
}
|
||||
|
||||
func short(commit string) string {
|
||||
if len(commit) > 12 {
|
||||
return commit[:12]
|
||||
}
|
||||
return commit
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
commitA = "0123456789abcdef0123456789abcdef01234567"
|
||||
commitB = "fedcba9876543210fedcba9876543210fedcba98"
|
||||
)
|
||||
|
||||
func inEnv(name, env, source string, nodes ...CustomNode) Model {
|
||||
m := comfyModel(source, &ComfyUI{Workflow: "w.json", Output: OutputMeshes, Environment: env, Nodes: nodes})
|
||||
m.Name = name
|
||||
return m
|
||||
}
|
||||
|
||||
func TestModelsInOneEnvironmentShareTheirNodesAndWeights(t *testing.T) {
|
||||
sf3d := CustomNode{Repo: "https://github.com/Stability-AI/stable-fast-3d", Commit: commitA,
|
||||
Requirements: []string{"trimesh", "transformers>=4.42,<5"}}
|
||||
tripo := CustomNode{Repo: "https://github.com/flowtyone/ComfyUI-Flowty-TripoSR", Commit: commitB,
|
||||
Requirements: []string{"Transformers >= 4.42, <5", "rembg"}}
|
||||
flux := comfyModel("/w/flux.safetensors", &ComfyUI{Environment: "shared"})
|
||||
cat, err := New([]Model{
|
||||
flux,
|
||||
inEnv("sf3d", "shared", "/w/sf3d/model.safetensors", sf3d),
|
||||
inEnv("triposr", "shared", "/w/tripo/model.ckpt", tripo),
|
||||
inEnv("alone", "", "/w/alone.ckpt"),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
envs := cat.Environments()
|
||||
if len(envs) != 1 {
|
||||
t.Fatalf("environments %+v", envs)
|
||||
}
|
||||
e := envs[0]
|
||||
if strings.Join(e.Models, ",") != "flux,sf3d,triposr" {
|
||||
t.Errorf("models %v", e.Models)
|
||||
}
|
||||
if len(e.Sources) != 3 || len(e.Nodes) != 2 {
|
||||
t.Errorf("sources %v nodes %+v", e.Sources, e.Nodes)
|
||||
}
|
||||
// transformers once, with the node that named it first; rembg with its own.
|
||||
if got := strings.Join(e.Nodes[0].Requirements, " "); got != "trimesh transformers>=4.42,<5" {
|
||||
t.Errorf("first node's requirements %q", got)
|
||||
}
|
||||
if got := strings.Join(e.Nodes[1].Requirements, " "); got != "rembg" {
|
||||
t.Errorf("second node's requirements %q", got)
|
||||
}
|
||||
if got, ok := cat.EnvironmentOf("sf3d"); !ok || got.Name != "shared" {
|
||||
t.Errorf("sf3d's environment %+v %v", got, ok)
|
||||
}
|
||||
if _, ok := cat.EnvironmentOf("alone"); ok {
|
||||
t.Error("a model with no environment is in one")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnEnvironmentWhoseModelsDisagreeIsRefused(t *testing.T) {
|
||||
node := func(commit string, reqs ...string) CustomNode {
|
||||
return CustomNode{Repo: "https://github.com/a/node", Commit: commit, Requirements: reqs}
|
||||
}
|
||||
for name, c := range map[string]struct {
|
||||
models []Model
|
||||
want string
|
||||
}{
|
||||
"two commits of one node": {[]Model{
|
||||
inEnv("a", "e", "/w/a", node(commitA)), inEnv("b", "e", "/w/b", node(commitB))}, "different commits"},
|
||||
"two ranges of one package": {[]Model{
|
||||
inEnv("a", "e", "/w/a", node(commitA, "numpy<2")),
|
||||
inEnv("b", "e", "/w/b", CustomNode{Repo: "https://github.com/b/other", Commit: commitB,
|
||||
Requirements: []string{"numpy>=2"}})}, `"a" and "b" ask for numpy`},
|
||||
"a bad name": {[]Model{inEnv("a", "Shared Env", "/w/a")}, "comfyui.environment"},
|
||||
} {
|
||||
if _, err := New(c.models); err == nil || !strings.Contains(err.Error(), c.want) {
|
||||
t.Errorf("%s: %v", name, err)
|
||||
}
|
||||
}
|
||||
// The same node at the same commit, the same range written two ways: fine.
|
||||
if _, err := New([]Model{
|
||||
inEnv("a", "e", "/w/a", node(commitA, "numpy >= 2")),
|
||||
inEnv("b", "e", "/w/b", node(commitA, "NumPy>=2")),
|
||||
}); err != nil {
|
||||
t.Errorf("agreeing models refused: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestALicenceIsSaidSoAMachineCanMatchIt(t *testing.T) {
|
||||
yes := true
|
||||
ok := License{ID: "tencent-hunyuan-community", Name: "Tencent Hunyuan Community License", Commercial: &yes,
|
||||
ExcludedRegions: []string{"EU", "GB", "KR"}}
|
||||
m := comfyModel("/w/f.safetensors", nil)
|
||||
m.License = &ok
|
||||
if _, err := New([]Model{m}); err != nil {
|
||||
t.Fatalf("a full licence refused: %v", err)
|
||||
}
|
||||
for name, c := range map[string]struct {
|
||||
l License
|
||||
want string
|
||||
}{
|
||||
"no name": {License{ID: "MIT"}, "license.name"},
|
||||
"a sentence id": {License{ID: "the MIT one", Name: "MIT"}, "license.id"},
|
||||
"a lowercase eu": {License{Name: "x", ExcludedRegions: []string{"eu"}}, "excluded_regions"},
|
||||
"a negative cap": {License{Name: "x", RevenueCapUSD: -1}, "revenue_cap_usd"},
|
||||
} {
|
||||
m := comfyModel("/w/f.safetensors", nil)
|
||||
l := c.l
|
||||
m.License = &l
|
||||
if _, err := New([]Model{m}); err == nil || !strings.Contains(err.Error(), c.want) {
|
||||
t.Errorf("%s: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user