Skip to content

Commit 43c5851

Browse files
[Docs] Outputs.mdx (huggingface#422)
* up * remove bogus file
1 parent 46013e8 commit 43c5851

File tree

2 files changed

+42
-45
lines changed

2 files changed

+42
-45
lines changed

docs/source/api/outputs.mdx

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,46 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o
1010
specific language governing permissions and limitations under the License.
1111
-->
1212

13-
# Models
13+
# BaseOutputs
1414

15-
Diffusers contains pretrained models for popular algorithms and modules for creating the next set of diffusion models.
16-
The primary function of these models is to denoise an input sample, by modeling the distribution $p_\theta(\mathbf{x}_{t-1}|\mathbf{x}_t)$.
17-
The models are built on the base class ['ModelMixin'] that is a `torch.nn.module` with basic functionality for saving and loading models both locally and from the HuggingFace hub.
15+
All models have outputs that are instances of subclasses of [`~utils.BaseOutput`]. Those are
16+
data structures containing all the information returned by the model, but that can also be used as tuples or
17+
dictionaries.
1818

19-
## API
19+
Let's see how this looks in an example:
2020

21-
Models should provide the `def forward` function and initialization of the model.
22-
All saving, loading, and utilities should be in the base ['ModelMixin'] class.
21+
```python
22+
from diffusers import DDIMPipeline
2323

24-
## Examples
24+
pipeline = DDIMPipeline.from_pretrained("google/ddpm-cifar10-32")
25+
outputs = pipeline()
26+
```
2527

26-
- The ['UNetModel'] was proposed in [TODO](https://arxiv.org/) and has been used in paper1, paper2, paper3.
27-
- Extensions of the ['UNetModel'] include the ['UNetGlideModel'] that uses attention and timestep embeddings for the [GLIDE](https://arxiv.org/abs/2112.10741) paper, the ['UNetGradTTS'] model from this [paper](https://arxiv.org/abs/2105.06337) for text-to-speech, ['UNetLDMModel'] for latent-diffusion models in this [paper](https://arxiv.org/abs/2112.10752), and the ['TemporalUNet'] used for time-series prediciton in this reinforcement learning [paper](https://arxiv.org/abs/2205.09991).
28-
- TODO: mention VAE / SDE score estimation
28+
The `outputs` object is a [`~pipeline_utils.ImagePipelineOutput`], as we can see in the
29+
documentation of that class below, it means it has an image attribute.
30+
31+
You can access each attribute as you would usually do, and if that attribute has not been returned by the model, you will get `None`:
32+
33+
```python
34+
outputs.images
35+
```
36+
37+
or via keyword lookup
38+
39+
```python
40+
outputs["images"]
41+
```
42+
43+
When considering our `outputs` object as tuple, it only considers the attributes that don't have `None` values.
44+
Here for instance, we could retrieve images via indexing:
45+
46+
```python
47+
outputs[:1]
48+
```
49+
50+
which will return the tuple `(outputs.images)` for instance.
51+
52+
## BaseOutput
53+
54+
[[autodoc]] utils.BaseOutput
55+
- to_tuple

src/diffusers/utils/outputs.py

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,40 +59,10 @@ def __post_init__(self):
5959
if not len(class_fields):
6060
raise ValueError(f"{self.__class__.__name__} has no fields.")
6161

62-
first_field = getattr(self, class_fields[0].name)
63-
other_fields_are_none = all(getattr(self, field.name) is None for field in class_fields[1:])
64-
65-
if other_fields_are_none and not is_tensor(first_field):
66-
if isinstance(first_field, dict):
67-
iterator = first_field.items()
68-
first_field_iterator = True
69-
else:
70-
try:
71-
iterator = iter(first_field)
72-
first_field_iterator = True
73-
except TypeError:
74-
first_field_iterator = False
75-
76-
# if we provided an iterator as first field and the iterator is a (key, value) iterator
77-
# set the associated fields
78-
if first_field_iterator:
79-
for element in iterator:
80-
if (
81-
not isinstance(element, (list, tuple))
82-
or not len(element) == 2
83-
or not isinstance(element[0], str)
84-
):
85-
break
86-
setattr(self, element[0], element[1])
87-
if element[1] is not None:
88-
self[element[0]] = element[1]
89-
elif first_field is not None:
90-
self[class_fields[0].name] = first_field
91-
else:
92-
for field in class_fields:
93-
v = getattr(self, field.name)
94-
if v is not None:
95-
self[field.name] = v
62+
for field in class_fields:
63+
v = getattr(self, field.name)
64+
if v is not None:
65+
self[field.name] = v
9666

9767
def __delitem__(self, *args, **kwargs):
9868
raise Exception(f"You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.")

0 commit comments

Comments
 (0)