generate package¶
Generate package
Subpackages¶
Submodules¶
generate.admin module¶
admin.py for generate app.
generate.apps module¶
Generate app config.
generate.consumers module¶
Websocket consumers for the generate app
This file contains the consumers for the generate app. The consumers are responsible for handling the websocket connections and sending messages regarding the generation of images.
- class generate.consumers.GenerateConsumer(*args, **kwargs)¶
Bases:
AsyncWebsocketConsumer
Consumer for the generation of images
- async disconnect(close_code: int) None ¶
Called when the websocket closes for any reason.
- Parameters:
close_code (int) – The code indicating why the connection closed
- async event_message(event)¶
Called when we get a message from the room group.
generate.file_utils module¶
File and directory utilities.
- generate.file_utils.download_and_cache_models(pretrained_model_name_or_path_list, config_dicts, cache_dir, resume_download, proxies, local_files_only, revision)¶
Download and cache the models
- generate.file_utils.get_allowed_patterns(config_dict)¶
Get the allowed patterns for the given config dict.
- generate.file_utils.get_cached_folder(pretrained_model_name_or_path, cache_dir, config_dict, resume_download, proxies, local_files_only, revision)¶
Get the cached folder for the given model.
- generate.file_utils.get_checkpoint_path(cached_path, attr)¶
Get the checkpoint path for the given attribute.
- generate.file_utils.load_checkpoint(checkpoint_path)¶
Load the checkpoint from the given path.
generate.image module¶
Image utilities.
- async generate.image.generate_image_status(step: int, timestep: FloatTensor | None, latents: Tensor | None, task_id, total_steps=None, vae=None, message=None, preview_image=False, *args, **kwargs) None ¶
Status callback for image generation.
Preview images are only sent if the VAE is provided.
Preview images reduce the performance of the generation process, by half of more. :param step: The current step. :type step: int :param timestep: The current timestep, unused currently. :type timestep: torch.FloatTensor :param latents: The current latents. :type latents: torch.Tensor :param task_id: The task ID. :type task_id: str :param message: The message to send. :type message: str :param preview_image: Whether to send a preview image. :type preview_image: bool
- Returns:
None
- generate.image.get_generator(seed: int | None = None) Generator ¶
Get the generator for the given seed.
- Parameters:
seed (int) – The seed to use.
- Returns:
The generator with specified seed or a random seed.
- Return type:
torch.Generator
- generate.image.numpy_to_pil(images: ndarray) list[PIL.Image.Image] ¶
Convert a numpy image or a batch of images to a PIL image.
- Parameters:
images (np.ndarray) – The image or batch of images to convert.
- Returns:
The converted image or batch of images.
- Return type:
PIL.Image.Image
- generate.image.render_image(model_path_or_name: str, nsfw: bool, seed: int | None, params: dict[str, Any], callback: Callable, callback_steps: int, callback_args: List[Any], callback_kwargs: Dict[str, Any]) Tuple[Image, int] ¶
Render the image.
- Parameters:
model_path_or_name (str) – The path or name of the model to use.
nsfw (bool) – Whether to use the NSFW model.
seed (int) – The seed to use.
params (dict) – The parameters to use.
callback (callable) – The callback to use.
callback_steps (int) – The number of steps between callbacks.
callback_args (list) – The arguments to pass to the callback.
callback_kwargs (dict) – The keyword arguments to pass to the callback.
- Returns:
The generated image and the seed used to generate it.
- Return type:
- generate.image.save_error(generated_image: GeneratedImage) None ¶
Save the error to the database.
- Parameters:
generated_image (GeneratedImage) – The generated image to save.
- Returns:
None
- generate.image.save_generated_image(generated_image: GeneratedImage, image: Image, seed: int, duration: float, end: datetime) None ¶
Save the generated image to the database and filesystem.
- Parameters:
generated_image (GeneratedImage) – The generated image to save.
image (torch.Tensor) – The image to save.
seed (int) – The seed used to generate the image.
duration (float) – The duration of the generation.
end (datetime.datetime) – The end time of the generation.
- Returns:
None
generate.input_validation module¶
Input validation for the pipeline.
- generate.input_validation.validate_callback_steps(callback_steps)¶
Validate that the callback steps are a positive integer.
- generate.input_validation.validate_generator_and_batch_size(generator, batch_size)¶
Validate that the generator is a list and has the same length as the batch size.
- generate.input_validation.validate_initial_image_latents(init_latents, batch_size)¶
Validate that the initial image latents and batch size are valid.
- generate.input_validation.validate_negative_prompt_and_embeds(negative_prompt, negative_prompt_embeds)¶
Validate that the negative prompt and negative prompt_embeds are valid.
- generate.input_validation.validate_prompt_and_embeds(prompt, prompt_embeds)¶
Validate that the prompt and prompt_embeds are valid.
- generate.input_validation.validate_prompt_and_negative_embeds_shape(prompt_embeds, negative_prompt_embeds)¶
Validate that the prompt and negative prompt embeds have the same shape.
- generate.input_validation.validate_prompt_type(prompt: str | list)¶
Validate that the prompt type is valid.
generate.load_pipeline module¶
- generate.load_pipeline.from_pretrained(cls: DiffusionPipeline, pretrained_model_name_or_path: str | PathLike, **kwargs)¶
Instantiate a PyTorch diffusion pipeline from pre-trained pipeline weights.
- The pipeline is set in evaluation mode by default using model.eval()
(Dropout modules are deactivated).
- The warning Weights from XXX not initialized from pretrained model means
that the weights of XXX do not come pretrained with the rest of the model.
It is up to you to train those weights with a downstream fine-tuning task.
- The warning Weights from XXX not used in YYY means that the layer XXX is not used
by YYY, therefore those weights are discarded.
- Parameters:
pretrained_model_name_or_path (str or os.PathLike) –
- Can be either:
A string, the repo id of a pretrained pipeline hosted inside a model repo on https://huggingface.co/ Valid repo ids have to be located under a user or organization name, like CompVis/ldm-text2im-large-256.
A path to a directory containing pipeline weights saved using [~DiffusionPipeline.save_pretrained], e.g., ./my_pipeline_directory/.
torch_dtype (str or torch.dtype, optional) –
Override the default torch.dtype and load the model under this dtype. If “auto” is passed the dtype will be automatically derived from the
model’s weights.
custom_pipeline (str, optional) –
<Tip warning={true}>
This is an experimental feature and is likely to change in the future.
</Tip>
Can be either:
- A string, the repo id of a custom pipeline hosted inside a model repo on
- Valid repo ids have to be located under a user or organization name,
like hf-internal-testing/diffusers-dummy-pipeline.
<Tip>
- It is required that the model repo has a file, called pipeline.py
that defines the custom pipeline.
</Tip>
- A string, the file name of a community pipeline hosted on GitHub under
https://github.com/huggingface/diffusers/tree/main/examples/community.
- Valid file names have to match exactly the file name without .py
located under the above link, e.g. clip_guided_stable_diffusion.
<Tip>
- Community pipelines are always loaded from the current main
branch of GitHub.
</Tip>
- A path to a directory containing a custom pipeline, e.g.,
./my_pipeline_directory/.
<Tip>
- It is required that the directory has a file, called pipeline.py
that defines the custom pipeline.
</Tip>
- For more information on how to load and create custom pipelines,
please have a look at [Loading and Adding Custom Pipelines] (https://huggingface.co/docs/diffusers/using-diffusers/custom_pipeline_overview)
torch_dtype –
force_download (bool, optional, defaults to False) –
- Whether or not to force the (re-)download of the model weights and
configuration files, overriding the cached versions if they exist.
resume_download (bool, optional, defaults to False) –
- Whether or not to delete incompletely received files. Will attempt
to resume the download if such a file exists.
proxies (Dict[str, str], optional) –
- A dictionary of proxy servers to use by protocol or endpoint, e.g.,
{‘http’: ‘foo.bar:3128’, ‘http://hostname’: ‘foo.bar:4012’}. The proxies are used on each request.
output_loading_info (bool, optional, defaults to False) –
- Whether or not to also return a dictionary containing missing keys,
unexpected keys and error messages.
local_files_only (bool, optional, defaults to False) –
- Whether or not to only look at local files
(i.e., do not try to download the model).
use_auth_token (str or bool, optional) –
The token to use as HTTP bearer authorization for remote files. If True, will use the token generated when running huggingface-cli login
(stored in ~/.huggingface).
revision (str, optional, defaults to “main”) –
- The specific model version to use. It can be a branch name, a tag name,
or a commit id, since we use a git-based system for storing models and other artifacts on huggingface.co, so revision can be any identifier allowed by git.
(str (custom_revision) –
Hub and to local version of diffusers when loading from GitHub): The specific model version to use. It can be a branch name, a tag name, or a
commit id similar to revision when loading a custom pipeline from the Hub.
It can be a diffusers version when loading a custom pipeline from GitHub.
optional –
Hub and to local version of diffusers when loading from GitHub): The specific model version to use. It can be a branch name, a tag name, or a
commit id similar to revision when loading a custom pipeline from the Hub.
It can be a diffusers version when loading a custom pipeline from GitHub.
the (defaults to "main" when loading from) –
Hub and to local version of diffusers when loading from GitHub): The specific model version to use. It can be a branch name, a tag name, or a
commit id similar to revision when loading a custom pipeline from the Hub.
It can be a diffusers version when loading a custom pipeline from GitHub.
mirror (str, optional) –
- Mirror source to accelerate downloads in China. If you are from China and have
an accessibility problem, you can set this option to resolve it.
Note that we do not guarantee the timeliness or safety. Please refer to the mirror site for more information. Specify the folder name here.
device_map (str or Dict[str, Union[int, str, torch.device]], optional) –
A map that specifies where each submodule should go. It doesn’t need to be refined to each parameter/buffer name, once a given
module name is inside, every submodule of it will be sent to the same device.
- To have Accelerate compute the most optimized device_map automatically,
set device_map=”auto”.
For more information about each option see [designing a device map] (https://hf.co/docs/accelerate/main/en/usage_guides/big_modeling#designing-a-device-map).
(bool (low_cpu_mem_usage) –
defaults to True if torch version >= 1.9.0 else False): Speed up model loading by not initializing the weights and
only loading the pre-trained weights. This also tries to not use more than 1x model size in CPU memory (including peak memory) while loading the model.
This is only supported when torch version >= 1.9.0. If you are using an older version of torch, setting this argument to True
will raise an error.
optional –
defaults to True if torch version >= 1.9.0 else False): Speed up model loading by not initializing the weights and
only loading the pre-trained weights. This also tries to not use more than 1x model size in CPU memory (including peak memory) while loading the model.
This is only supported when torch version >= 1.9.0. If you are using an older version of torch, setting this argument to True
will raise an error.
- :paramdefaults to True if torch version >= 1.9.0 else False):
- Speed up model loading by not initializing the weights and
only loading the pre-trained weights. This also tries to not use more than 1x model size in CPU memory (including peak memory) while loading the model.
This is only supported when torch version >= 1.9.0. If you are using an older version of torch, setting this argument to True
will raise an error.
- Parameters:
return_cached_folder (bool, optional, defaults to False) –
- If set to True, path to downloaded cached folder will be returned in addition
to loaded pipeline.
kwargs (remaining dictionary of keyword arguments, optional) –
- Can be used to overwrite load - and saveable variables -
i.e. the pipeline components - of the specific pipeline class.
- The overwritten components are then directly passed to the pipelines __init__
method.
See example below for more information.
<Tip>
- It is required to be logged in (huggingface-cli login) when you want to use
private or [gated models] (https://huggingface.co/docs/hub/models-gated#gated-models), e.g. “runwayml/stable-diffusion-v1-5”
</Tip>
<Tip>
- Activate the special [“offline-mode”]
(https://huggingface.co/diffusers/installation.html#offline-mode) to use this method in a firewalled environment.
</Tip>
Examples:
```py >>> from diffusers import DiffusionPipeline
>>> # Download pipeline from huggingface.co and cache. >>> pipeline = DiffusionPipeline.from_pretrained("CompVis/ldm-text2im-large-256")
>>> # Download pipeline that requires an authorization token >>> # For more information on access tokens, please refer to this section >>> # of the documentation](https://huggingface.co/docs/hub/security-tokens) >>> pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
>>> # Use a different scheduler >>> from diffusers import LMSDiscreteScheduler
>>> scheduler = LMSDiscreteScheduler.from_config(pipeline.scheduler.config) >>> pipeline.scheduler = scheduler ```
- generate.load_pipeline.get_allow_and_ignore_patterns_with_user_agent(pipeline, pretrained_model_name_or_path, config_dict, pipeline_config)¶
generate.models module¶
Models for the generate app.
- class generate.models.GeneratedImage(*args, **kwargs)¶
Bases:
Model
Model for generated images.
- exception DoesNotExist¶
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned¶
Bases:
MultipleObjectsReturned
- batch_number¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- created_at¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- duration¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- error¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- property filename¶
Return the filename.
- generated_at¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- get_next_by_created_at(*, field=<django.db.models.fields.DateTimeField: created_at>, is_next=True, **kwargs)¶
- get_previous_by_created_at(*, field=<django.db.models.fields.DateTimeField: created_at>, is_next=False, **kwargs)¶
- guidance_scale¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- height¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- host¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- id¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- model¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- negative_prompt: TextField¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- nsfw¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- num_inference_steps¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- objects = <django.db.models.manager.Manager object>¶
- owner¶
Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Child.parent
is aForwardManyToOneDescriptor
instance.
- owner_id¶
- prompt¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- seed¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- task_id¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- width¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- class generate.models.RenderWorkerDevice(*args, **kwargs)¶
Bases:
Model
Model for render worker devices.
- exception DoesNotExist¶
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned¶
Bases:
MultipleObjectsReturned
- allocated_memory¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- cached_memory¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- created_at¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- device_id¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- device_name¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- get_next_by_created_at(*, field=<django.db.models.fields.DateTimeField: created_at>, is_next=True, **kwargs)¶
- get_next_by_last_update_at(*, field=<django.db.models.fields.DateTimeField: last_update_at>, is_next=True, **kwargs)¶
- get_previous_by_created_at(*, field=<django.db.models.fields.DateTimeField: created_at>, is_next=False, **kwargs)¶
- get_previous_by_last_update_at(*, field=<django.db.models.fields.DateTimeField: last_update_at>, is_next=False, **kwargs)¶
- host¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- id¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- last_update_at¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- objects = <django.db.models.manager.Manager object>¶
- total_memory¶
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
generate.noise module¶
Noise functions
- generate.noise.decode_latents(vae: Any, latents: Tensor) ndarray ¶
decode latents to image
- Parameters:
vae (VAE) – VAE model
latents (torch.Tensor) – latents
Returns – image (np.ndarray): image
- generate.noise.denoise(scheduler, unet, latents, step, current_timestep, prompt_embeds, do_classifier_free_guidance, guidance_scale, extra_step_kwargs, timesteps, num_warmup_steps, progress_bar, callback, callback_steps, callback_args, callback_kwargs, vae=None)¶
denoise the image
- Parameters:
scheduler (Scheduler) – scheduler
unet (UNet) – unet
latents (torch.Tensor) – latents
step (int) – step
current_timestep (torch.Tensor) – current timestep
prompt_embeds (torch.Tensor) – prompt embeds
do_classifier_free_guidance (bool) – do classifier free guidance
guidance_scale (float) – guidance scale
extra_step_kwargs (dict) – extra step kwargs
timesteps (list) – timesteps
num_warmup_steps (int) – num warmup steps
progress_bar (tqdm) – progress bar
callback (function) – callback
callback_steps (int) – callback steps
callback_args (list) – callback args
callback_kwargs (dict) – callback kwargs
vae (VAE) – VAE model
- Returns:
latents
- Return type:
latents (torch.Tensor)
generate.pipeline module¶
The pipeline module.
This module contains the main class for the HardDiffusion pipeline.
The HardDiffusion pipeline is a custom implementation of the DiffusionPipeline class from the Diffusers library.
The reason for inheriting from the DiffusionPipeline class is to maintain compatibility with the Diffusers library.
- class generate.pipeline.HardDiffusionPipeline(vae: AutoencoderKL, text_encoder: CLIPTextModel, tokenizer: CLIPTokenizer, unet: UNet2DConditionModel, scheduler: KarrasDiffusionSchedulers | SchedulerMixin | ConfigMixin, safety_checker: StableDiffusionSafetyChecker, feature_extractor: CLIPFeatureExtractor, requires_safety_checker: bool = True)¶
Bases:
DiffusionPipeline
HardDiffusionPipeline is a pipeline that can be used to generate images from a given model or models.
Currently a modified version of the custom_merged_pipeline is used.
- static add_difference(theta0, theta1, theta2, alpha)¶
Add the difference between theta1 and theta2 to theta0.
- check_inputs(prompt: str | List[str], strength: float, callback_steps: int, height: int | None = None, width: int | None = None, negative_prompt: str | List[str] | None = None, prompt_embeds: Tensor | None = None, negative_prompt_embeds: Tensor | None = None) None ¶
Validate the user supplied pipeline inputs.
- Parameters:
prompt (str or List[str]) – The prompt or prompts to guide the image generation. If not defined, one has to pass prompt_embeds instead.
strength (float) – The strength of the prompt. Must be between 0 and 1.
callback_steps (int) – The number of steps between each callback.
height (int, optional) – The height of the generated image. If not defined, one has to pass prompt_embeds instead.
width (int, optional) – The width of the generated image. If not defined, one has to pass prompt_embeds instead.
negative_prompt (str or List[str], optional) – The negative prompt or prompts to guide the image generation. If not defined, one has to pass negative_prompt_embeds instead. Alternatively, it can also be None.
prompt_embeds (torch.Tensor, optional) – The prompt embeddings to guide the image generation. If not defined, one has to pass prompt instead.
negative_prompt_embeds (torch.Tensor, optional) – The negative prompt embeddings to guide the image generation. If not defined, one has to pass negative_prompt instead.
- Raises:
ValueError – If the inputs are not valid.
- create_final_pipeline(cached_folders, torch_dtype, device_map, interp, alpha)¶
Create the final pipeline
- decode_latents(latents: FloatTensor) ndarray ¶
Decode latents to image
- Parameters:
latents (torch.FloatTensor) – latents to decode
- Returns:
decoded image
- Return type:
image (np.ndarray)
- enable_sequential_cpu_offload(gpu_id=0)¶
- Offloads all models to CPU using accelerate, significantly reducing memory
usage. When called, unet, text_encoder, vae and safety checker have their state dicts saved to CPU and then are moved to a torch.device(‘meta’) and loaded to GPU only when their specific submodule has its `forward method called.
- static from_single_model(pretrained_model_name_or_path, **kwargs)¶
Create a single pipeline
- get_timesteps(num_inference_steps, strength, device)¶
get the original timestep using init_timestep
- static inv_sigmoid(theta0, theta1, theta2, alpha)¶
Inverse smoothstep interpolation.
- merge(pretrained_model_name_or_path_list: List[str | PathLike], **kwargs)¶
Returns a new pipeline object of the class ‘DiffusionPipeline’ with the merged checkpoints(weights) of the models passed in the argument ‘pretrained_model_name_or_path_list’ as a list.
Parameters:¶
pretrained_model_name_or_path_list : A list of valid pretrained model names in the HuggingFace hub or paths to locally stored models in the HuggingFace format.
- **kwargs:
Supports all the default DiffusionPipeline.get_config_dict kwargs viz.. cache_dir, resume_download, force_download, proxies, local_files_only,
use_auth_token, revision, torch_dtype, device_map.
- alpha - The interpolation parameter. Ranges from 0 to 1.
- It affects the ratio in which the checkpoints are merged. A 0.8 alpha
would mean that the first model checkpoints would affect the final result far less than an alpha of 0.2
- interp - The interpolation method to use for the merging.
Supports “sigmoid”, “inv_sigmoid”, “add_diff” and None. Passing None uses the default interpolation which is weighted sum
interpolation.
For merging three checkpoints, only “add_diff” is supported.
- force - Whether to ignore mismatch in model_config.json
for the current models. Defaults to False.
- prepare_extra_step_kwargs(generator: Generator, eta: float) dict[str, Union[torch._C.Generator, float]] ¶
Prepare extra kwargs for the scheduler step, since not all schedulers have the same signature eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
- Parameters:
generator (torch.Generator) – torch generator
eta (float) – η in DDIM paper: https://arxiv.org/abs/2010.02502 should be between [0, 1]
- Returns:
extra kwargs for the scheduler step
- Return type:
extra_step_kwargs (dict)
- prepare_latents(image, height, width, num_channels_latents, timestep, batch_size, num_images_per_prompt, dtype, device, generator=None, latents=None)¶
prepare latents
- run_safety_checker(image, device, dtype)¶
run safety checker
- Parameters:
image (np.ndarray) – image to check
device (torch.device) – torch device
dtype (torch.dtype) – torch dtype
- Returns:
image after safety check has_nsfw_concept (bool): whether the image has nsfw concept
- Return type:
image (np.ndarray)
- static sigmoid(theta0, theta1, theta2, alpha)¶
Smoothstep interpolation.
- validate_mergable_models(pretrained_model_name_or_path_list: List[str | PathLike], cache_dir, resume_download, force_download, proxies, local_files_only, use_auth_token, revision, force, **kwargs)¶
Validate that the checkpoints can be merged
- static weighted_sum(theta0, theta1, theta2, alpha)¶
Weighted sum interpolation.
- generate.pipeline.get_pipeline(model_path_or_name, nsfw)¶
Get the pipeline for the given model path or name.
- generate.pipeline.preprocess(image)¶
Preprocess the image for the model.
- generate.pipeline.validate_safety_checker(pipeline, safety_checker, feature_extractor, requires_safety_checker)¶
Validate the safety checker.
generate.pipeline_configuration module¶
Configuration utilities module for the pipeline.
- class generate.pipeline_configuration.PipelineConfiguration(cache_dir='/home/runner/.cache/huggingface/hub', resume_download: bool = False, force_download: bool = False, proxies=None, local_files_only: bool = False, from_flax: bool = False, custom_pipeline=None, custom_revision=None, provider=None, sess_options=None, low_cpu_mem_usage: bool = True, return_cached_folder: bool = False, use_auth_token=None, revision=None, torch_dtype=None, device_map=None, alpha: float = 0.5, interp=None)¶
Bases:
object
Pipeline configuration class for the pipeline.
- keys = ['cache_dir', 'resume_download', 'force_download', 'proxies', 'local_files_only', 'from_flax', 'custom_pipeline', 'custom_revision', 'provider', 'sess_options', 'low_cpu_mem_usage', 'return_cached_folder', 'use_auth_token', 'revision', 'torch_dtype', 'device_map', 'alpha', 'interp']¶
- remove_config_keys(dict_)¶
Delete configuration keys from a dict. Optional modules are also passed in using kwargs.
This function is used to cleanup the kwargs before passing them to the pipeline when loading a model.
generate.pipeline_doc_example module¶
The example doc string for the pipeline.
generate.prompt module¶
Prompt utilities.
- generate.prompt.duplicate_embeddings(embeds, text_encoder, batch_size, num_images_per_prompt, device) FloatTensor ¶
duplicate embeddings for each generation per prompt, using mps friendly method
- generate.prompt.get_attention_mask(text_encoder, text_inputs, device)¶
Get the attention mask.
- generate.prompt.get_embed_from_prompt(tokenizer, text_encoder, prompt, device)¶
Get the embedding from the prompt.
- generate.prompt.get_embeds_from_text_encoding(text_encoder, text_inputs, attention_mask, device)¶
Get the embeddings from the text encoding.
- generate.prompt.get_unconditional_embed(tokenizer, text_encoder, negative_prompt, prompt, prompt_embeds, batch_size, device)¶
Get the embedding from the negative prompt.
generate.render_devices module¶
Render devices.
- generate.render_devices.get_render_devices_by_id(device_count)¶
Get the existing render workers.
- generate.render_devices.update_render_device_stats(device_id, render_device, now, new)¶
Update the render device stats.
generate.routing module¶
Websocket routing for the generate app.
generate.schedulers module¶
Scheduler utilities.
- generate.schedulers.validate_clip_sample(scheduler, config, new_config)¶
Validate the clip sample.
- generate.schedulers.validate_steps_offset(scheduler, config, new_config)¶
Validate the steps offset.
generate.serializers module¶
Django Rest Framework Serializers for models in the generate app.
- class generate.serializers.GeneratedImageSerializer(*args, **kwargs)¶
Bases:
DynamicModelSerializer
Serializer for GeneratedImage model.
- class Meta¶
Bases:
object
Meta class for GeneratedImageSerializer.
- exclude = []¶
- list_serializer_class¶
alias of
DynamicListSerializer
- model¶
alias of
GeneratedImage
- name = 'generated_image'¶
- create(validated_data: Dict[str, Any]) GeneratedImage ¶
Create and return a new GeneratedImages instance, given the validated data.
- Parameters:
validated_data (Dict[str, Any]) – The validated data.
- Returns:
The new GeneratedImage instance.
- Return type:
- update(instance: GeneratedImage, validated_data: Dict[str, Any]) GeneratedImage ¶
Update and return an existing GeneratedImage instance, given the validated data.
- Parameters:
instance (GeneratedImage) – The existing GeneratedImage instance.
validated_data (Dict[str, Any]) – The validated data.
- Returns:
The updated GeneratedImage instance.
- Return type:
generate.tasks module¶
Celery tasks for generating images.
generate.tests module¶
Tests for the generate app.
- class generate.tests.TestGeneratedImage(methodName='runTest')¶
Bases:
TestCase
Test the generate app.
- test_nsfw_anything()¶
Test the render_image function with nsfw=False and the anything model. This model threw an error when nsfw=False and transformers 4.22.0 was used.
Fixed by transformers==4.27.0dev0
- test_nsfw_black_image()¶
Test the render_image function with nsfw=False.
- test_nsfw_no_black_image()¶
Test the render_image function with nsfw=True.
- test_render_image()¶
Test the render_image function.
- test_render_nfsw_image()¶
Test the render_image function.
- generate.tests.image_diff(img1, img2)¶
Return the difference between two images.
generate.unet_utils module¶
Utilities for UNet models.
- generate.unet_utils.validate_unet_sample_size(unet, config, new_config)¶
Validate the UNet sample size.
generate.urls module¶
generate URL Configuration
generate.views module¶
Views for the generate app.
Contains the following views: - index: The main page for the generate app. - queue_prompt: Queue a prompt to be generated. - csrf_form: CSRF endpoint to get the middleware to set the token. - images: Return generated images data as JSON. - renderer_health: Check health of all renderer devices. - renderer_status: The rendered status page.
Also contains the following viewsets: - GeneratedImageViewSet: Viewset for the GeneratedImage model.
- class generate.views.GeneratedImageViewSet(**kwargs)¶
Bases:
DynamicModelViewSet
This viewset automatically provides list, create, retrieve, update and destroy actions.
Additionally, we will also provide an extra generate action in the future.
- basename = None¶
- description = None¶
- detail = None¶
- name = None¶
- perform_create(serializer: GeneratedImageSerializer) None ¶
Set the owner of the image to the current user.
- permission_classes = [<class 'rest_framework.permissions.IsAuthenticatedOrReadOnly'>, <class 'user.permissions.IsOwnerOrReadOnly'>]¶
- queryset¶
- serializer_class¶
alias of
GeneratedImageSerializer
- suffix = None¶
- generate.views.csrf_form(request: HttpRequest) HttpResponse ¶
CSRF endpoint to get the middleware to set the token.
- Parameters:
request – The request object.
- Returns:
The rendered template.
- generate.views.images(request: HttpRequest, last: int | None = None, page_size: int = 10) JsonResponse ¶
Return generated images data as JSON.
- Parameters:
request – The request object.
last – The last image to return.
page_size – The number of images to return.
- Returns:
A JSON response with a list of generated image data in the images key, and a total key with the total number of images.
- generate.views.index(request: HttpRequest) HttpResponse ¶
The endpoint that returns the html UI used to generate images.
- Parameters:
request – The request object.
- Returns:
The rendered template.
- generate.views.queue_prompt(request: HttpRequest) JsonResponse ¶
Queue a prompt to be generated.
Parses the request parameters and queues a celery task to generate an image.
- Parameters:
request – The request object.
- Returns:
A JSON response with an error key and a task_id key.
- generate.views.renderer_health(request: HttpRequest) JsonResponse ¶
Check health of all renderer devices.
- Parameters:
request – The request object.
- Returns:
- A JSON response with a list of renderer device stats for the currently
connected hosts by hostname.
Each device reports the following stats: - id: The device ID. - name: The device name. - usage: The memory usage. - memory: The total memory. - device_allocated: The allocated memory. - device_cached: The cached memory.
- generate.views.renderer_status(request: HttpRequest) HttpResponse ¶
Render the renderer status page.
- Parameters:
request – The request object.
- Returns:
The rendered template. Displays a list of renderer devices by hostname.
generate.warnings module¶
Deprecation warnings for the generate module.