ai-simple-engine-diffusion 0.0.2__tar.gz → 0.0.3__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.
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/PKG-INFO +2 -2
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/pyproject.toml +2 -2
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/graph/operation/generate_image_from_latents.py +7 -2
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/graph/operation/sample_latents.py +32 -2
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/model/executor/abstract/latent_diffusion_model_executor_abstract.py +34 -17
- ai_simple_engine_diffusion-0.0.3/src/ai_simple_engine_diffusion/scheduler/abstract.py +57 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/types/latent_space.py +2 -2
- ai_simple_engine_diffusion-0.0.2/src/ai_simple_engine_diffusion/scheduler/abstract.py +0 -46
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/LICENSE +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/README.md +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/__init__.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/consts.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/graph/__init__.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/graph/operation/__init__.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/graph/operation/encode_image.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/graph/operation/generate_image.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/model/__init__.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/model/executor/__init__.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/model/executor/abstract/__init__.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/model/info/__init__.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/model/info/diffusion_model_info.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/noise/__init__.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/noise/noise_generator_abstract.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/prompt_embeddings/__init__.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/prompt_embeddings/abstract.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/scheduler/__init__.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/scheduler/spec/__init__.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/scheduler/spec/base.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/types/__init__.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/types/data_type.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/types/embeddings.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/types/latents.py +0 -0
- {ai_simple_engine_diffusion-0.0.2 → ai_simple_engine_diffusion-0.0.3}/src/ai_simple_engine_diffusion/types/noise_prediction.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: ai-simple-engine-diffusion
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.3
|
|
4
4
|
Summary: AI Simple Engine Diffusion Module
|
|
5
5
|
License-File: LICENSE
|
|
6
6
|
Author: danialcala94
|
|
@@ -9,7 +9,7 @@ Requires-Python: >=3.10,<3.12
|
|
|
9
9
|
Classifier: Programming Language :: Python :: 3
|
|
10
10
|
Classifier: Programming Language :: Python :: 3.10
|
|
11
11
|
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
-
Requires-Dist: ai-simple-engine (>=0.2.
|
|
12
|
+
Requires-Dist: ai-simple-engine (>=0.2.8,<1.0.0)
|
|
13
13
|
Requires-Dist: ai-simple-engine-common (>=0.0.3,<1.0.0)
|
|
14
14
|
Description-Content-Type: text/markdown
|
|
15
15
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "ai-simple-engine-diffusion"
|
|
3
|
-
version = "0.0.
|
|
3
|
+
version = "0.0.3"
|
|
4
4
|
description = "AI Simple Engine Diffusion Module"
|
|
5
5
|
authors = [
|
|
6
6
|
{name = "danialcala94", email = "danielalcalavalera@gmail.com"}
|
|
@@ -10,7 +10,7 @@ requires-python = ">=3.10,<3.12"
|
|
|
10
10
|
|
|
11
11
|
[tool.poetry.dependencies]
|
|
12
12
|
# Mandatory
|
|
13
|
-
ai-simple-engine = { version = ">=0.2.
|
|
13
|
+
ai-simple-engine = { version = ">=0.2.8,<1.0.0", optional = false }
|
|
14
14
|
ai-simple-engine-common = { version = ">=0.0.3,<1.0.0", optional = false }
|
|
15
15
|
# Optional
|
|
16
16
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from ai_simple_engine_diffusion.types.data_type import LATENTS
|
|
2
2
|
from ai_simple_engine.graph.operation.abstract.atomic_operation import AtomicOperation
|
|
3
|
+
from ai_simple_engine.models.executor.registry.family_model_executor_registry import FamilyModelExecutorRegistry
|
|
3
4
|
from ai_simple_engine.graph.input import Input
|
|
4
5
|
from ai_simple_engine.graph.output import Output
|
|
5
6
|
from ai_simple_engine.types.data_type.base import LOADED_MODEL, STRING, INT, FLOAT, IMAGE
|
|
@@ -42,11 +43,15 @@ class GenerateImageFromLatents(
|
|
|
42
43
|
self,
|
|
43
44
|
context
|
|
44
45
|
):
|
|
45
|
-
|
|
46
|
+
model_executors_registry = context.services.get(FamilyModelExecutorRegistry)
|
|
47
|
+
executor = model_executors_registry.resolve(self.model)
|
|
48
|
+
|
|
49
|
+
# executor = context.model_executors.resolve(self.model)
|
|
46
50
|
|
|
47
51
|
image = await executor.generate(
|
|
52
|
+
context = context,
|
|
48
53
|
model = self.model,
|
|
49
|
-
latents = self.latents,
|
|
54
|
+
latents = self.latents.instance,
|
|
50
55
|
prompt = self.prompt,
|
|
51
56
|
negative_prompt = self.negative_prompt,
|
|
52
57
|
steps = self.steps,
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
from ai_simple_engine_diffusion.types.data_type import LATENTS
|
|
2
|
+
# from ai_simple_engine_runtime_diffusers.models.executor.noise.pytorch_noise_generator import TorchNoiseGenerator
|
|
3
|
+
from ai_simple_engine_diffusion.types.latents import Latents
|
|
2
4
|
from ai_simple_engine_diffusion.noise.noise_generator_abstract import NoiseGenerator
|
|
3
5
|
from ai_simple_engine.graph.operation.abstract.atomic_operation import AtomicOperation
|
|
4
6
|
from ai_simple_engine.graph.input import Input
|
|
@@ -36,6 +38,13 @@ class SampleLatents(
|
|
|
36
38
|
self,
|
|
37
39
|
context
|
|
38
40
|
):
|
|
41
|
+
# noise_generator = context.services.get(TorchNoiseGenerator)
|
|
42
|
+
"""
|
|
43
|
+
This plugin is registering it with the abstract
|
|
44
|
+
as the key, but including an instance of the
|
|
45
|
+
Torch version as the actual service registered.
|
|
46
|
+
"""
|
|
47
|
+
# TODO: Is this ok like this (?)
|
|
39
48
|
noise_generator = context.services.get(NoiseGenerator)
|
|
40
49
|
|
|
41
50
|
space = self.model.info.latent_space(
|
|
@@ -43,11 +52,32 @@ class SampleLatents(
|
|
|
43
52
|
height = self.height
|
|
44
53
|
)
|
|
45
54
|
|
|
46
|
-
|
|
47
|
-
space
|
|
55
|
+
shape = (
|
|
56
|
+
space.batch_size,
|
|
57
|
+
space.channels,
|
|
58
|
+
space.height,
|
|
59
|
+
space.width
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
runtime = self.model.instance
|
|
63
|
+
|
|
64
|
+
# Dynamically detect 'device' and 'dtype'
|
|
65
|
+
device = next(runtime.unet.parameters()).device
|
|
66
|
+
dtype = next(runtime.unet.parameters()).dtype
|
|
67
|
+
|
|
68
|
+
latents_torch = noise_generator.generate(
|
|
69
|
+
# space = space,
|
|
70
|
+
shape = shape,
|
|
71
|
+
device = device,
|
|
72
|
+
dtype = dtype,
|
|
48
73
|
seed = self.seed
|
|
49
74
|
)
|
|
50
75
|
|
|
76
|
+
latents = Latents(
|
|
77
|
+
latents_torch
|
|
78
|
+
# TODO: Maybe include the 'space' (?)
|
|
79
|
+
)
|
|
80
|
+
|
|
51
81
|
return {
|
|
52
82
|
'latents': latents
|
|
53
83
|
}
|
|
@@ -3,6 +3,8 @@ from ai_simple_engine.models.loaded_model import LoadedModel
|
|
|
3
3
|
from ai_simple_engine.execution.execution_context import ExecutionContext
|
|
4
4
|
from abc import ABC, abstractmethod
|
|
5
5
|
|
|
6
|
+
import torch
|
|
7
|
+
|
|
6
8
|
|
|
7
9
|
class LatentDiffusionModelExecutorAbstract(
|
|
8
10
|
ModelExecutor,
|
|
@@ -91,25 +93,32 @@ class LatentDiffusionModelExecutorAbstract(
|
|
|
91
93
|
Generate an image with the given `prompt`,
|
|
92
94
|
`negative_prompt` and `model`.
|
|
93
95
|
"""
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
96
|
+
|
|
97
|
+
"""
|
|
98
|
+
Disable view tracking and version counter
|
|
99
|
+
bumps to improve the performance as it is
|
|
100
|
+
inference process.
|
|
101
|
+
"""
|
|
102
|
+
with torch.inference_mode():
|
|
103
|
+
embeddings = await self.encode_prompt(
|
|
104
|
+
model = model,
|
|
105
|
+
prompt = prompt,
|
|
106
|
+
negative_prompt = negative_prompt
|
|
107
|
+
)
|
|
99
108
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
109
|
+
latents = await self._diffuse(
|
|
110
|
+
context = context,
|
|
111
|
+
model = model,
|
|
112
|
+
latents = latents,
|
|
113
|
+
embeddings = embeddings,
|
|
114
|
+
steps = steps,
|
|
115
|
+
guidance_scale = guidance_scale
|
|
116
|
+
)
|
|
108
117
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
118
|
+
image = await self.decode_latents(
|
|
119
|
+
model = model,
|
|
120
|
+
latents = latents
|
|
121
|
+
)
|
|
113
122
|
|
|
114
123
|
return image
|
|
115
124
|
|
|
@@ -130,6 +139,14 @@ class LatentDiffusionModelExecutorAbstract(
|
|
|
130
139
|
|
|
131
140
|
scheduler.set_timesteps(steps)
|
|
132
141
|
|
|
142
|
+
"""
|
|
143
|
+
The noise that the `torch.randn(...)` generates
|
|
144
|
+
has a standard deviation of ~ 1, but each
|
|
145
|
+
scheduler expects to start with a different
|
|
146
|
+
amplitude, calculated with this formula below:
|
|
147
|
+
"""
|
|
148
|
+
latents = latents * scheduler.init_noise_sigma
|
|
149
|
+
|
|
133
150
|
for timestep in scheduler.timesteps:
|
|
134
151
|
latent_input = scheduler.scale_model_input(
|
|
135
152
|
latents,
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from ai_simple_engine_diffusion.scheduler.abstract import Scheduler
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class DiffusersScheduler(
|
|
5
|
+
Scheduler
|
|
6
|
+
):
|
|
7
|
+
|
|
8
|
+
def __init__(
|
|
9
|
+
self,
|
|
10
|
+
scheduler
|
|
11
|
+
):
|
|
12
|
+
self._scheduler = scheduler
|
|
13
|
+
|
|
14
|
+
@property
|
|
15
|
+
def init_noise_sigma(
|
|
16
|
+
self
|
|
17
|
+
):
|
|
18
|
+
"""
|
|
19
|
+
The `gamma` that must be used when starting
|
|
20
|
+
to create the noise.
|
|
21
|
+
"""
|
|
22
|
+
return self._scheduler.init_noise_sigma
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
def timesteps(
|
|
26
|
+
self
|
|
27
|
+
):
|
|
28
|
+
return self._scheduler.timesteps
|
|
29
|
+
|
|
30
|
+
def set_timesteps(
|
|
31
|
+
self,
|
|
32
|
+
steps: int
|
|
33
|
+
):
|
|
34
|
+
self._scheduler.set_timesteps(steps)
|
|
35
|
+
|
|
36
|
+
def scale_model_input(
|
|
37
|
+
self,
|
|
38
|
+
latents,
|
|
39
|
+
timestep
|
|
40
|
+
):
|
|
41
|
+
return self._scheduler.scale_model_input(
|
|
42
|
+
latents,
|
|
43
|
+
timestep
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
def step(
|
|
47
|
+
self,
|
|
48
|
+
*,
|
|
49
|
+
model_output,
|
|
50
|
+
timestep,
|
|
51
|
+
sample
|
|
52
|
+
):
|
|
53
|
+
return self._scheduler.step(
|
|
54
|
+
model_output = model_output,
|
|
55
|
+
timestep = timestep,
|
|
56
|
+
sample = sample
|
|
57
|
+
)
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
TODO: Remove this class, it is no longer used.
|
|
3
|
-
"""
|
|
4
|
-
from ai_simple_engine_diffusion.types.latents import Latents
|
|
5
|
-
from ai_simple_engine_diffusion.types.noise_prediction import NoisePrediction
|
|
6
|
-
from abc import ABC, abstractmethod
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class Scheduler(
|
|
10
|
-
ABC
|
|
11
|
-
):
|
|
12
|
-
|
|
13
|
-
@abstractmethod
|
|
14
|
-
def set_timesteps(
|
|
15
|
-
self,
|
|
16
|
-
*,
|
|
17
|
-
steps: int
|
|
18
|
-
) -> None:
|
|
19
|
-
"""
|
|
20
|
-
Prepare the scheduler for a diffusion run.
|
|
21
|
-
"""
|
|
22
|
-
...
|
|
23
|
-
|
|
24
|
-
@abstractmethod
|
|
25
|
-
def timesteps(
|
|
26
|
-
self
|
|
27
|
-
) -> list[int]:
|
|
28
|
-
"""
|
|
29
|
-
Return the timesteps used during inference.
|
|
30
|
-
"""
|
|
31
|
-
...
|
|
32
|
-
|
|
33
|
-
@abstractmethod
|
|
34
|
-
def step(
|
|
35
|
-
self,
|
|
36
|
-
*,
|
|
37
|
-
latents: Latents,
|
|
38
|
-
noise_prediction: NoisePrediction,
|
|
39
|
-
timestep: int
|
|
40
|
-
) -> Latents:
|
|
41
|
-
"""
|
|
42
|
-
Update the latent tensor using the predicted
|
|
43
|
-
noise for the current timestep, which means
|
|
44
|
-
computing the next latent sample.
|
|
45
|
-
"""
|
|
46
|
-
...
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|