0.2.0-dev Download ZIP
72 lines ·2.7 KB Raw
| 1 | using System.Diagnostics; |
| 2 | using System.Numerics; |
| 3 | |
| 4 | namespace EvilEngine.GenerativeMotion; |
| 5 | |
| 6 | public sealed record BackendTiming(MotionBackend Backend, int Batch, double? MedianMilliseconds, string? Failure); |
| 7 | |
| 8 | public static class BackendBenchmark |
| 9 | { |
| 10 | public static readonly int[] Batches = [1, 8, 32]; |
| 11 | |
| 12 | public static string Graph(string package, int batch) |
| 13 | { |
| 14 | return Path.Combine(package, $"mmm_v6_soma_fp32_portable_b{batch}.onnx"); |
| 15 | } |
| 16 | |
| 17 | public static IReadOnlyList<BackendTiming> Run(string package, Action<SomaInputs> fill, int warmup = 3, |
| 18 | int runs = 20, double secondsPerGraph = 2) |
| 19 | { |
| 20 | ArgumentOutOfRangeException.ThrowIfLessThan(runs, 1); |
| 21 | var timings = new List<BackendTiming>(); |
| 22 | foreach (var backend in MotionRuntime.Candidates()) |
| 23 | foreach (var batch in Batches) |
| 24 | try |
| 25 | { |
| 26 | using var model = new SomaModel(Graph(package, batch), backend); |
| 27 | fill(model.Inputs); |
| 28 | for (var i = 0; i < warmup; i++) |
| 29 | model.Run(); |
| 30 | var samples = new List<double>(); |
| 31 | var started = Stopwatch.GetTimestamp(); |
| 32 | while (samples.Count < runs && |
| 33 | (samples.Count < 3 || Stopwatch.GetElapsedTime(started).TotalSeconds < secondsPerGraph)) |
| 34 | { |
| 35 | var start = Stopwatch.GetTimestamp(); |
| 36 | model.Run(); |
| 37 | samples.Add(Stopwatch.GetElapsedTime(start).TotalMilliseconds); |
| 38 | } |
| 39 | |
| 40 | samples.Sort(); |
| 41 | var median = samples[samples.Count / 2]; |
| 42 | timings.Add(new BackendTiming(backend, batch, median, null)); |
| 43 | if (!GenerativeCapacity.Fits(median)) |
| 44 | break; |
| 45 | } |
| 46 | catch (Exception exception) when (exception is not OutOfMemoryException) |
| 47 | { |
| 48 | timings.Add(new BackendTiming(backend, batch, null, exception.Message)); |
| 49 | break; |
| 50 | } |
| 51 | |
| 52 | return timings; |
| 53 | } |
| 54 | |
| 55 | public static void StandingStep(SomaInputs inputs) |
| 56 | { |
| 57 | var request = new SomaRequest { TargetCount = 1 }; |
| 58 | var reference = SomaSkeleton.Reference; |
| 59 | for (var pose = 0; pose < SomaContract.PosesPerToken; pose++) |
| 60 | { |
| 61 | reference.CopyTo(request.ContextPose(pose)); |
| 62 | reference.CopyTo(request.TargetPose(pose)); |
| 63 | } |
| 64 | |
| 65 | var hips = request.Target[0]; |
| 66 | request.Target[0] = hips with { Position = hips.Position + new Vector3(1f, 0f, 0f) }; |
| 67 | inputs.Clear(); |
| 68 | var codec = new SomaCodec(Rigid.Identity); |
| 69 | for (var row = 0; row < inputs.Batch; row++) |
| 70 | codec.Encode(request, inputs.Row(row)); |
| 71 | } |
| 72 | } |