Skip to content

Commit

Permalink
Tidy up some logging
Browse files Browse the repository at this point in the history
  • Loading branch information
saddam213 committed Nov 16, 2023
1 parent eb09a0a commit 1ae0ec8
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
8 changes: 5 additions & 3 deletions OnnxStack.Core/LogExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public static void Log(this ILogger logger, LogLevel logLevel, string message, [
}


public static long LogBegin(this ILogger logger, string message, [CallerMemberName] string caller = default)
public static long LogBegin(this ILogger logger, string message = default, [CallerMemberName] string caller = default)
{
return LogBeginInternal(logger, LogLevel.Information, message, caller);
}


public static long LogBegin(this ILogger logger, LogLevel logLevel, string message, [CallerMemberName] string caller = default)
public static long LogBegin(this ILogger logger, LogLevel logLevel, string message = default, [CallerMemberName] string caller = default)
{

return LogBeginInternal(logger, logLevel, message, caller);
Expand All @@ -46,7 +46,9 @@ public static void LogEnd(this ILogger logger, LogLevel logLevel, string message

private static long LogBeginInternal(ILogger logger, LogLevel logLevel, string message, string caller)
{
LogInternal(logger, logLevel, message, caller);
if (!string.IsNullOrEmpty(message))
LogInternal(logger, logLevel, message, caller);

return Stopwatch.GetTimestamp();
}

Expand Down
15 changes: 9 additions & 6 deletions OnnxStack.StableDiffusion/Diffusers/DiffuserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public virtual async Task<DenseTensor<float>> DiffuseAsync(IModelOptions modelOp
// Create random seed if none was set
schedulerOptions.Seed = schedulerOptions.Seed > 0 ? schedulerOptions.Seed : Random.Shared.Next();

var diffuseTime = _logger?.LogBegin("Begin...");
var diffuseTime = _logger?.LogBegin("Diffuse starting...");
_logger?.Log($"Model: {modelOptions.Name}, Pipeline: {modelOptions.PipelineType}, Diffuser: {promptOptions.DiffuserType}, Scheduler: {schedulerOptions.SchedulerType}");

// Check guidance
Expand All @@ -115,7 +115,7 @@ public virtual async Task<DenseTensor<float>> DiffuseAsync(IModelOptions modelOp
// Run Scheduler steps
var schedulerResult = await SchedulerStepAsync(modelOptions, promptOptions, schedulerOptions, promptEmbeddings, performGuidance, progressCallback, cancellationToken);

_logger?.LogEnd($"End", diffuseTime);
_logger?.LogEnd($"Diffuse complete", diffuseTime);

return schedulerResult;
}
Expand All @@ -137,8 +137,9 @@ public virtual async IAsyncEnumerable<BatchResult> DiffuseBatchAsync(IModelOptio
// Create random seed if none was set
schedulerOptions.Seed = schedulerOptions.Seed > 0 ? schedulerOptions.Seed : Random.Shared.Next();

var diffuseBatchTime = _logger?.LogBegin("Begin...");
var diffuseBatchTime = _logger?.LogBegin("Batch Diffuser starting...");
_logger?.Log($"Model: {modelOptions.Name}, Pipeline: {modelOptions.PipelineType}, Diffuser: {promptOptions.DiffuserType}, Scheduler: {schedulerOptions.SchedulerType}");
_logger?.Log($"BatchType: {batchOptions.BatchType}, ValueFrom: {batchOptions.ValueFrom}, ValueTo: {batchOptions.ValueTo}, Increment: {batchOptions.Increment}");

// Check guidance
var performGuidance = ShouldPerformGuidance(schedulerOptions);
Expand All @@ -153,11 +154,13 @@ public virtual async IAsyncEnumerable<BatchResult> DiffuseBatchAsync(IModelOptio
var schedulerCallback = (int step, int steps) => progressCallback?.Invoke(batchIndex, batchSchedulerOptions.Count, step, steps);
foreach (var batchSchedulerOption in batchSchedulerOptions)
{
var diffuseTime = _logger?.LogBegin("Diffuse starting...");
yield return new BatchResult(batchSchedulerOption, await SchedulerStepAsync(modelOptions, promptOptions, batchSchedulerOption, promptEmbeddings, performGuidance, schedulerCallback, cancellationToken));
_logger?.LogEnd($"Diffuse complete", diffuseTime);
batchIndex++;
}

_logger?.LogEnd($"End", diffuseBatchTime);
_logger?.LogEnd($"Diffuse batch complete", diffuseBatchTime);
}


Expand Down Expand Up @@ -205,7 +208,7 @@ protected virtual DenseTensor<float> PerformGuidance(DenseTensor<float> noisePre
/// <returns></returns>
protected virtual async Task<DenseTensor<float>> DecodeLatentsAsync(IModelOptions model, PromptOptions prompt, SchedulerOptions options, DenseTensor<float> latents)
{
var timestamp = _logger?.LogBegin("Begin...");
var timestamp = _logger.LogBegin();

// Scale and decode the image latents with vae.
latents = latents.MultiplyBy(1.0f / model.ScaleFactor);
Expand All @@ -224,7 +227,7 @@ protected virtual async Task<DenseTensor<float>> DecodeLatentsAsync(IModelOption
var results = await _onnxModelService.RunInferenceAsync(model, OnnxModelType.VaeDecoder, inputs, outputs);
using (var imageResult = results.First())
{
_logger?.LogEnd("End", timestamp);
_logger?.LogEnd("Latents decoded", timestamp);
return imageResult.ToDenseTensor();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected override async Task<DenseTensor<float>> SchedulerStepAsync(IModelOptio
}

progressCallback?.Invoke(step, timesteps.Count);
_logger?.LogEnd(LogLevel.Debug, $"Step {step}/{timesteps.Count}", stepTime);
_logger?.LogEnd($"Step {step}/{timesteps.Count}", stepTime);
}

// Decode Latents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected override async Task<DenseTensor<float>> SchedulerStepAsync(IModelOptio
}

progressCallback?.Invoke(step, timesteps.Count);
_logger?.LogEnd(LogLevel.Debug, $"Step {step}/{timesteps.Count}", stepTime);
_logger?.LogEnd($"Step {step}/{timesteps.Count}", stepTime);
}

// Decode Latents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected override async Task<DenseTensor<float>> SchedulerStepAsync(IModelOptio
}

progressCallback?.Invoke(step, timesteps.Count);
_logger?.LogEnd(LogLevel.Debug, $"Step {step}/{timesteps.Count}", stepTime);
_logger?.LogEnd($"Step {step}/{timesteps.Count}", stepTime);
}

// Decode Latents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected override async Task<DenseTensor<float>> SchedulerStepAsync(IModelOptio
}

progressCallback?.Invoke(step, timesteps.Count);
_logger?.LogEnd(LogLevel.Debug, $"Step {step}/{timesteps.Count}", stepTime);
_logger?.LogEnd($"Step {step}/{timesteps.Count}", stepTime);
}

// Decode Latents
Expand Down

0 comments on commit 1ae0ec8

Please sign in to comment.