Skip to content

Commit

Permalink
fix(ui): fix dynamic prompts with single prompt
Browse files Browse the repository at this point in the history
Closes #5292

The special handling for single prompt is totally extraneous and caused a bug.
  • Loading branch information
psychedelicious committed Jan 1, 2024
1 parent dfd9c44 commit 4045103
Showing 1 changed file with 71 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,165 +23,126 @@ export const prepareLinearUIBatch = (
const { prompts, seedBehaviour } = state.dynamicPrompts;

const data: Batch['data'] = [];
const firstBatchDatumList: components['schemas']['BatchDatum'][] = [];
const secondBatchDatumList: components['schemas']['BatchDatum'][] = [];

if (prompts.length === 1) {
// add seeds first to ensure the output order groups the prompts
if (seedBehaviour === 'PER_PROMPT') {
const seeds = generateSeeds({
count: iterations,
count: prompts.length * iterations,
start: shouldRandomizeSeed ? undefined : seed,
});

const zipped: components['schemas']['BatchDatum'][] = [];

if (graph.nodes[NOISE]) {
zipped.push({
firstBatchDatumList.push({
node_path: NOISE,
field_name: 'seed',
items: seeds,
});
}

// add to metadata
if (getHasMetadata(graph)) {
// add to metadata
removeMetadata(graph, 'seed');
zipped.push({
firstBatchDatumList.push({
node_path: METADATA,
field_name: 'seed',
items: seeds,
});
}

if (graph.nodes[CANVAS_COHERENCE_NOISE]) {
zipped.push({
firstBatchDatumList.push({
node_path: CANVAS_COHERENCE_NOISE,
field_name: 'seed',
items: seeds.map((seed) => (seed + 1) % NUMPY_RAND_MAX),
});
}

data.push(zipped);
} else {
// prompts.length > 1 aka dynamic prompts
const firstBatchDatumList: components['schemas']['BatchDatum'][] = [];
const secondBatchDatumList: components['schemas']['BatchDatum'][] = [];

// add seeds first to ensure the output order groups the prompts
if (seedBehaviour === 'PER_PROMPT') {
const seeds = generateSeeds({
count: prompts.length * iterations,
start: shouldRandomizeSeed ? undefined : seed,
// seedBehaviour = SeedBehaviour.PerRun
const seeds = generateSeeds({
count: iterations,
start: shouldRandomizeSeed ? undefined : seed,
});

if (graph.nodes[NOISE]) {
secondBatchDatumList.push({
node_path: NOISE,
field_name: 'seed',
items: seeds,
});
}

if (graph.nodes[NOISE]) {
firstBatchDatumList.push({
node_path: NOISE,
field_name: 'seed',
items: seeds,
});
}

// add to metadata
if (getHasMetadata(graph)) {
removeMetadata(graph, 'seed');
firstBatchDatumList.push({
node_path: METADATA,
field_name: 'seed',
items: seeds,
});
}

if (graph.nodes[CANVAS_COHERENCE_NOISE]) {
firstBatchDatumList.push({
node_path: CANVAS_COHERENCE_NOISE,
field_name: 'seed',
items: seeds.map((seed) => (seed + 1) % NUMPY_RAND_MAX),
});
}
} else {
// seedBehaviour = SeedBehaviour.PerRun
const seeds = generateSeeds({
count: iterations,
start: shouldRandomizeSeed ? undefined : seed,
// add to metadata
if (getHasMetadata(graph)) {
removeMetadata(graph, 'seed');
secondBatchDatumList.push({
node_path: METADATA,
field_name: 'seed',
items: seeds,
});
}

if (graph.nodes[NOISE]) {
secondBatchDatumList.push({
node_path: NOISE,
field_name: 'seed',
items: seeds,
});
}

// add to metadata
if (getHasMetadata(graph)) {
removeMetadata(graph, 'seed');
secondBatchDatumList.push({
node_path: METADATA,
field_name: 'seed',
items: seeds,
});
}

if (graph.nodes[CANVAS_COHERENCE_NOISE]) {
secondBatchDatumList.push({
node_path: CANVAS_COHERENCE_NOISE,
field_name: 'seed',
items: seeds.map((seed) => (seed + 1) % NUMPY_RAND_MAX),
});
}
data.push(secondBatchDatumList);
if (graph.nodes[CANVAS_COHERENCE_NOISE]) {
secondBatchDatumList.push({
node_path: CANVAS_COHERENCE_NOISE,
field_name: 'seed',
items: seeds.map((seed) => (seed + 1) % NUMPY_RAND_MAX),
});
}
data.push(secondBatchDatumList);
}

const extendedPrompts =
seedBehaviour === 'PER_PROMPT'
? range(iterations).flatMap(() => prompts)
: prompts;

// zipped batch of prompts
if (graph.nodes[POSITIVE_CONDITIONING]) {
firstBatchDatumList.push({
node_path: POSITIVE_CONDITIONING,
field_name: 'prompt',
items: extendedPrompts,
});
}

// add to metadata
if (getHasMetadata(graph)) {
removeMetadata(graph, 'positive_prompt');
firstBatchDatumList.push({
node_path: METADATA,
field_name: 'positive_prompt',
items: extendedPrompts,
});
}

const extendedPrompts =
seedBehaviour === 'PER_PROMPT'
? range(iterations).flatMap(() => prompts)
: prompts;
if (shouldConcatSDXLStylePrompt && model?.base_model === 'sdxl') {
const stylePrompts = extendedPrompts.map((p) =>
[p, positiveStylePrompt].join(' ')
);

// zipped batch of prompts
if (graph.nodes[POSITIVE_CONDITIONING]) {
firstBatchDatumList.push({
node_path: POSITIVE_CONDITIONING,
field_name: 'prompt',
items: extendedPrompts,
field_name: 'style',
items: stylePrompts,
});
}

// add to metadata
if (getHasMetadata(graph)) {
removeMetadata(graph, 'positive_prompt');
removeMetadata(graph, 'positive_style_prompt');
firstBatchDatumList.push({
node_path: METADATA,
field_name: 'positive_prompt',
field_name: 'positive_style_prompt',
items: extendedPrompts,
});
}

if (shouldConcatSDXLStylePrompt && model?.base_model === 'sdxl') {
const stylePrompts = extendedPrompts.map((p) =>
[p, positiveStylePrompt].join(' ')
);

if (graph.nodes[POSITIVE_CONDITIONING]) {
firstBatchDatumList.push({
node_path: POSITIVE_CONDITIONING,
field_name: 'style',
items: stylePrompts,
});
}

// add to metadata
if (getHasMetadata(graph)) {
removeMetadata(graph, 'positive_style_prompt');
firstBatchDatumList.push({
node_path: METADATA,
field_name: 'positive_style_prompt',
items: extendedPrompts,
});
}
}

data.push(firstBatchDatumList);
}

data.push(firstBatchDatumList);

const enqueueBatchArg: BatchConfig = {
prepend,
batch: {
Expand Down

0 comments on commit 4045103

Please sign in to comment.