2022-07-25 10:37:44 +02:00
using Flowframes.Data ;
using Flowframes.IO ;
using Flowframes.Main ;
using Flowframes.Os ;
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.Drawing ;
using System.IO ;
using System.Linq ;
using System.Threading.Tasks ;
2024-08-12 10:47:19 +02:00
using Padding = Flowframes . Data . Padding ;
2022-07-25 10:37:44 +02:00
namespace Flowframes.Utilities
{
class NcnnUtils
{
2023-12-21 21:05:23 +01:00
public static int GetRifeNcnnGpuThreads ( Size res , int gpuId , AI ai )
2022-07-25 10:37:44 +02:00
{
2023-02-17 22:10:33 +01:00
int threads = Config . GetInt ( Config . Key . ncnnThreads ) ;
2023-12-21 21:05:23 +01:00
int maxThreads = VulkanUtils . GetMaxNcnnThreads ( gpuId ) ;
2022-07-25 10:37:44 +02:00
2023-12-21 21:05:23 +01:00
threads = threads . Clamp ( 1 , maxThreads ) ; // To avoid exceeding the max queue count
Logger . Log ( $"Using {threads}/{maxThreads} GPU compute threads." , true , false , ai . LogFilename ) ;
2022-07-25 10:37:44 +02:00
return threads ;
}
public static string GetNcnnPattern ( )
{
return $"%0{Padding.interpFrames}d{Interpolate.currentSettings.interpExt}" ;
}
public static string GetNcnnTilesize ( int tilesize )
{
int gpusAmount = Config . Get ( Config . Key . ncnnGpus ) . Split ( ',' ) . Length ;
string tilesizeStr = $"{tilesize}" ;
for ( int i = 1 ; i < gpusAmount ; i + + )
tilesizeStr + = $",{tilesize}" ;
return tilesizeStr ;
}
2023-12-21 21:05:23 +01:00
public static string GetNcnnThreads ( AI ai )
2022-07-25 10:37:44 +02:00
{
2023-12-21 21:05:23 +01:00
List < int > enabledGpuIds = Config . Get ( Config . Key . ncnnGpus ) . Split ( ',' ) . Select ( s = > s . GetInt ( ) ) . ToList ( ) ; // Get GPU IDs
List < int > gpuThreadCounts = enabledGpuIds . Select ( g = > GetRifeNcnnGpuThreads ( new Size ( ) , g , ai ) ) . ToList ( ) ; // Get max thread count for each GPU
string progThreadsStr = string . Join ( "," , gpuThreadCounts ) ;
2023-02-17 22:11:18 +01:00
return $"{(Interpolate.currentlyUsingAutoEnc ? 2 : 4)}:{progThreadsStr}:4" ; // Read threads: 1 for singlethreaded, 2 for autoenc, 4 if order is irrelevant
2022-07-25 10:37:44 +02:00
}
public static async Task DeleteNcnnDupes ( string dir , float factor )
{
int dupeCount = InterpolateUtils . GetRoundedInterpFramesPerInputFrame ( factor ) ;
var files = IoUtils . GetFileInfosSorted ( dir , false ) . Reverse ( ) . Take ( dupeCount ) . ToList ( ) ;
Logger . Log ( $"DeleteNcnnDupes: Calculated dupe count from factor; deleting last {dupeCount} interp frames of {IoUtils.GetAmountOfFiles(dir, false)} ({string.Join(" , ", files.Select(x => x.Name))})" , true ) ;
int attempts = 4 ;
while ( attempts > 0 )
{
try
{
files . ForEach ( x = > x . Delete ( ) ) ;
break ;
}
catch ( Exception ex )
{
attempts - - ;
if ( attempts < 1 )
{
Logger . Log ( $"DeleteNcnnDupes Error: {ex.Message}" , true ) ;
break ;
}
else
{
await Task . Delay ( 500 ) ;
}
}
}
}
}
}