Skip to content
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

refactor: optimize request params for nai4 #269

Merged
merged 1 commit into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ interface ParamConfig {
smea?: boolean
smeaDyn?: boolean
scheduler?: string
rescale?: Computed<number>
decrisper?: boolean
upscaler?: string
restoreFaces?: boolean
Expand All @@ -236,6 +237,7 @@ interface ParamConfig {
imageSteps?: Computed<number>
maxSteps?: Computed<number>
strength?: Computed<number>
noise?: Computed<number>
resolution?: Computed<Orient | Size>
maxResolution?: Computed<number>
}
Expand Down Expand Up @@ -402,8 +404,9 @@ export const Config = Schema.intersect([
}),
Schema.object({
model: Schema.const('nai-v4-curated-preview'),
sampler: sampler.createSchema(sampler.nai4),
sampler: sampler.createSchema(sampler.nai4).default('k_euler_a'),
scheduler: Schema.union(scheduler.nai4).description('默认的调度器。').default('karras'),
rescale: Schema.computed(Schema.number(), options).min(0).max(1).description('输入服从度调整规模。').default(0),
}),
Schema.object({ sampler: sampler.createSchema(sampler.nai) }),
]),
Expand All @@ -417,6 +420,7 @@ export const Config = Schema.intersect([
imageSteps: Schema.computed(Schema.natural(), options).description('以图生图时默认的迭代步数。').default(50),
maxSteps: Schema.computed(Schema.natural(), options).description('允许的最大迭代步数。').default(64),
strength: Schema.computed(Schema.number(), options).min(0).max(1).description('默认的重绘强度。').default(0.7),
noise: Schema.computed(Schema.number(), options).min(0).max(1).description('默认的重绘添加噪声强度。').default(0.2),
resolution: Schema.computed(Schema.union([
Schema.const('portrait').description('肖像 (832x2326)'),
Schema.const('landscape').description('风景 (1216x832)'),
Expand Down
18 changes: 14 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export function apply(ctx: Context, config: Config) {
Object.assign(parameters, {
height: options.resolution.height,
width: options.resolution.width,
noise: options.noise ?? 0.2,
noise: options.noise ?? session.resolve(config.noise),
strength: options.strength ?? session.resolve(config.strength),
})
}
Expand Down Expand Up @@ -334,7 +334,9 @@ export function apply(ctx: Context, config: Config) {
}
parameters.dynamic_thresholding = options.decrisper ?? config.decrisper
if (model === 'nai-diffusion-3' || model === 'nai-diffusion-4-curated-preview') {
parameters.params_version = 3
parameters.legacy = false
parameters.legacy_v3_extend = false
parameters.noise_schedule = options.scheduler ?? config.scheduler
// Max scale for nai-v3 is 10, but not 20.
// If the given value is greater than 10,
Expand All @@ -343,7 +345,6 @@ export function apply(ctx: Context, config: Config) {
parameters.scale = parameters.scale / 2
}
if (model === 'nai-diffusion-3') {
parameters.legacy_v3_extend = false
parameters.sm_dyn = options.smeaDyn ?? config.smeaDyn
parameters.sm = (options.smea ?? config.smea) || parameters.sm_dyn
if (['k_euler_ancestral', 'k_dpmpp_2s_ancestral'].includes(parameters.sampler)
Expand All @@ -357,8 +358,17 @@ export function apply(ctx: Context, config: Config) {
}
}
if (model === 'nai-diffusion-4-curated-preview') {
parameters.use_coords = false // unknown
parameters.add_original_image = true // unknown
parameters.cfg_rescale = session.resolve(config.rescale)
parameters.characterPrompts = [] satisfies NovelAI.V4CharacterPrompt[]
parameters.controlnet_strength = 1 // unknown
parameters.deliberate_euler_ancestral_bug = false // unknown
parameters.prefer_brownian = true // unknown
parameters.reference_image_multiple = [] // unknown
parameters.reference_information_extracted_multiple = [] // unknown
parameters.reference_strength_multiple = [] // unknown
parameters.skip_cfg_above_sigma = null // unknown
parameters.use_coords = false // unknown
parameters.v4_prompt = {
caption: {
base_caption: prompt,
Expand Down Expand Up @@ -479,7 +489,7 @@ export function apply(ctx: Context, config: Config) {
prompt[nodeId].inputs.steps = parameters.steps
prompt[nodeId].inputs.cfg = parameters.scale
prompt[nodeId].inputs.sampler_name = options.sampler
prompt[nodeId].inputs.denoise = options.strength ?? config.strength
prompt[nodeId].inputs.denoise = options.strength ?? session.resolve(config.strength)
prompt[nodeId].inputs.scheduler = options.scheduler ?? config.scheduler
const positiveNodeId = prompt[nodeId].inputs.positive[0]
const negativeeNodeId = prompt[nodeId].inputs.negative[0]
Expand Down
Loading