Skip to content

Support for negative prompts in Swift CLI #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mfellner opened this issue Dec 11, 2022 · 1 comment
Open

Support for negative prompts in Swift CLI #60

mfellner opened this issue Dec 11, 2022 · 1 comment

Comments

@mfellner
Copy link

As far as I could see, the Swift CLI implementation currently doesn't support negative prompts.

In the Python implementation, the negative prompt argument is tokenised, encoded into uncond_embeddings, and prepended to the text embeddings of the regular prompt:

else:
uncond_tokens = negative_prompt
max_length = text_input_ids.shape[-1]
uncond_input = self.tokenizer(
uncond_tokens,
padding="max_length",
max_length=max_length,
truncation=True,
return_tensors="np",
)
uncond_embeddings = self.text_encoder(
input_ids=uncond_input.input_ids.astype(
np.float32))["last_hidden_state"]
# For classifier free guidance, we need to do two forward passes.
# Here we concatenate the unconditional and text embeddings into a single batch
# to avoid doing two forward passes
text_embeddings = np.concatenate(
[uncond_embeddings, text_embeddings])

The Swift implementation on the other hand does not handle a negative prompt argument:

// Encode the input prompt as well as a blank unconditioned input
let promptEmbedding = try textEncoder.encode(prompt)
let blankEmbedding = try textEncoder.encode("")
// Convert to Unet hidden state representation
let concatEmbedding = MLShapedArray<Float32>(
concatenating: [blankEmbedding, promptEmbedding],
alongAxis: 0
)

I don't really know how Stable Diffusion (v2) deals with negative prompts but how would one add support for them to the Swift implementation? The Core ML model seems to support it after all.

@JustinMeans
Copy link
Contributor

I just opened a PR with the Swift implementation. See: #61

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants