示例#1
0
 /// <summary>
 /// Create a worker that will execute `model` using the best backend that is available for a given `device` type.
 /// This is just a convenience function that internally calls `ModelLoader.Load` followed by ``WorkerFactory.CreateWorker`.
 /// `model` is the associated Model to execute.
 /// `device` is the preferred device for execution. For example `WorkerFactory.Device.GPU` specifies the fast GPU path.
 /// `verbose` will log scheduling of layers execution to the console.
 /// </summary>
 public static IWorker CreateWorker(this Model model,
                                    WorkerFactory.Device device = WorkerFactory.Device.Auto, bool verbose = false)
 {
     return(WorkerFactory.CreateWorker(model, device, verbose));
 }
示例#2
0
 /// <summary>
 /// Create a worker that will execute `model` using the best backend that is available for a given `device` type.
 /// This is just a convenience function that internally calls `ModelLoader.Load` followed by ``WorkerFactory.CreateWorker`.
 /// `model` is the associated Model to execute.
 /// `additionalOutputs` are the additional outputs to track but not directly specified by the model.
 /// `trimOutputs` are the outputs not discard even if they are specified by the model.
 /// `device` is the device type to run worker on. For example `WorkerFactory.Device.GPU` specifies the fast GPU path.
 /// `verbose` will log scheduling of layers execution to the console (default == false).
 /// </summary>
 public static IWorker CreateWorker(this Model model,
                                    string[] additionalOutputs, string[] trimOutputs, WorkerFactory.Device device = WorkerFactory.Device.Auto, bool verbose = false)
 {
     return(WorkerFactory.CreateWorker(model, additionalOutputs, trimOutputs, device, verbose));
 }
示例#3
0
        internal static IWorker CreateWorker(WorkerFactory.Type type, Model model, string[] additionalOutputs, string[] trimOutputs, WorkerFactory.WorkerConfiguration workerConfiguration, IModelExecutionsReporter modelExecutionsReporter = null)
        {
            type = ResolveAutoType(type);
            var compareAgainstType = ResolveAutoType(workerConfiguration.compareAgainstType);

            Assert.AreNotEqual(type, WorkerFactory.Type.Auto);
            Assert.AreNotEqual(compareAgainstType, WorkerFactory.Type.Auto);

            bool compare = type != compareAgainstType;

            if (WorkerFactory.IsType(type, WorkerFactory.Device.GPU) && !SystemInfo.supportsComputeShaders && !Application.isEditor)
            {
                type = WorkerFactory.Type.PixelShader;
            }

            IVars vars;

            // PixelShader worker uses Blit/Textures, cannot re-use vars unless the dispatch mechanism allows rendering to sub part of the texture
            if ((type == WorkerFactory.Type.PixelShader) || (compareAgainstType == WorkerFactory.Type.PixelShader))
            {
                vars = new GenericVarsWithReuse();
            }
            else
            {
                if (WorkerFactory.IsType(type, WorkerFactory.Device.GPU) || WorkerFactory.IsType(compareAgainstType, WorkerFactory.Device.GPU))
                {
                    vars = new ComputeVarsWithSharedModel();
                }
                else
                {
                    vars = new DefaultVars();
                }
            }

            ITensorAllocator allocator = vars.GetAllocator();

            if ((type == WorkerFactory.Type.PixelShader) || (compareAgainstType == WorkerFactory.Type.PixelShader))
            {
                allocator = new TensorCachingByShapeAllocator();
            }

            if (workerConfiguration.verbose)
            {
                D.Log($"Storage type: {vars.GetType()}. Allocator type: {allocator.GetType()}.");
            }

            IOps ops = CreateOps(type, allocator, workerConfiguration.verbose);

            if (compare)
            {
                ops = new CompareOps(ops,
                                     CreateOps(compareAgainstType, allocator, workerConfiguration.verbose), workerConfiguration.compareLogLevel, workerConfiguration.compareEpsilon);
            }

            if (workerConfiguration.verbose || modelExecutionsReporter != null)
            {
                ops = new VerboseOps(ops, workerConfiguration.verbose);
            }

            if (Application.isEditor || modelExecutionsReporter != null)
            {
                ops = new StatsOps(ops);
            }

            model = ValidateModel(
                PatchModel(model, additionalOutputs, trimOutputs));

            ops.SetModelExecutionsReporter(modelExecutionsReporter);
            return(new GenericWorker(model, ops, vars, workerConfiguration.verbose, workerConfiguration.takeoverWeights));
        }