From 0f94004b5c8736794f9be39e5c786ea6ca75e1d1 Mon Sep 17 00:00:00 2001 From: Liu Liu Date: Fri, 22 Dec 2023 16:34:38 -0500 Subject: [PATCH] Add isEager to push the compilation further up until just about executing. --- WORKSPACE | 4 ++-- deps.bzl | 4 ++-- nnc/Model.swift | 14 +++++++++++--- nnc/ModelBuilder.swift | 22 +++++++++++----------- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 328d669a60d..72b8324ff3a 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -5,9 +5,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") git_repository( name = "ccv", - commit = "54f82af445f407a2690defeb9f704b34f9693a39", + commit = "7f8e9e8fa91d0a65e6a3b47a925eb2379ca0f9f8", remote = "https://github.com/liuliu/ccv.git", - shallow_since = "1703273698 -0500", + shallow_since = "1703280796 -0500", ) load("@ccv//config:ccv.bzl", "ccv_deps", "ccv_setting") diff --git a/deps.bzl b/deps.bzl index 3f2ff680d1e..79aefdfd7f9 100644 --- a/deps.bzl +++ b/deps.bzl @@ -17,8 +17,8 @@ def s4nnc_deps(): git_repository, name = "ccv", remote = "https://github.com/liuliu/ccv.git", - commit = "54f82af445f407a2690defeb9f704b34f9693a39", - shallow_since = "1703273698 -0500", + commit = "7f8e9e8fa91d0a65e6a3b47a925eb2379ca0f9f8", + shallow_since = "1703280796 -0500", ) _maybe( diff --git a/nnc/Model.swift b/nnc/Model.swift index 44b91b49c96..02b79708a3e 100644 --- a/nnc/Model.swift +++ b/nnc/Model.swift @@ -206,8 +206,9 @@ extension Model { /** * Compile a model with the given inputs without executing it. After this, you can load * parameters from the store. + * - isEager: Whether we want to push compilation as far as possible up until the actual execution. */ - public func compile(inputs: [DynamicGraph_Any]) { + public func compile(inputs: [DynamicGraph_Any], isEager: Bool = false) { assert(inputs.count > 0) let params = CmdParamsFactory.factory.newParams() let noop = ccv_nnc_cmd(CCV_NNC_NOOP, nil, params, 0) @@ -223,13 +224,20 @@ extension Model { return ccv_nnc_tensor_variable_params(tensor.graph.cGraph, tensor._tensor) } ccv_cnnp_model_compile(cModel, inputParams, Int32(inputParams.count), noop, noop) + if isEager { + let graph = inputs[0].graph + let _inputs: [ccv_nnc_tensor_variable_t?] = inputs.map { $0.untyped[0]._tensor } + let _streamContext = graph.streamContext?._stream + ccv_nnc_dynamic_graph_dry_run( + graph.cGraph, cModel, testing ? 1 : 0, _inputs, Int32(_inputs.count), _streamContext) + } } /** * Compile a model with the given inputs without executing it. After this, you can load * parameters from the store. */ - public func compile(inputs: DynamicGraph_Any...) { - compile(inputs: inputs) + public func compile(inputs: DynamicGraph_Any..., isEager: Bool = false) { + compile(inputs: inputs, isEager: isEager) } } diff --git a/nnc/ModelBuilder.swift b/nnc/ModelBuilder.swift index aa78ddea49d..2206e1cf423 100644 --- a/nnc/ModelBuilder.swift +++ b/nnc/ModelBuilder.swift @@ -30,7 +30,7 @@ public class AnyModelBuilder { return outputSize } // Compile explicitly. - compileModel() + compileModel(isEager: false) // After the model compiled, we can access the outputSize now. let outputSize = Int(ccv_cnnp_model_output_size(model!.cModel)) _outputSize = outputSize @@ -140,9 +140,9 @@ public class AnyModelBuilder { _key = key } - fileprivate func compileModel() { + fileprivate func compileModel(isEager: Bool) { let inputs = self.inputs! - model!.compile(inputs: inputs) + model!.compile(inputs: inputs, isEager: isEager) // If we have store / key, try to load parameters now after it is compiled. if let store = _store, let key = _key { if let reader = _reader { @@ -214,10 +214,10 @@ public final class ModelBuilder: AnyModelBuilder { * Compile a model with the given inputs without executing it. After this, you can load * parameters from the store. */ - public func compile(_ t: T, inputs: [DynamicGraph_Any]) { + public func compile(_ t: T, inputs: [DynamicGraph_Any], isEager: Bool = false) { self.t = t self.inputs = inputs - compileModel() + compileModel(isEager: isEager) _outputSize = Int(ccv_cnnp_model_output_size(model!.cModel)) self.inputs = nil self.t = nil @@ -227,8 +227,8 @@ public final class ModelBuilder: AnyModelBuilder { * Compile a model with the given inputs without executing it. After this, you can load * parameters from the store. */ - public func compile(_ t: T, inputs: DynamicGraph_Any...) { - compile(t, inputs: inputs) + public func compile(_ t: T, inputs: DynamicGraph_Any..., isEager: Bool = false) { + compile(t, inputs: inputs, isEager: isEager) } } @@ -245,10 +245,10 @@ extension ModelBuilder where T == Void { * Compile a model with the given inputs without executing it. After this, you can load * parameters from the store. */ - public func compile(inputs: [DynamicGraph_Any]) { + public func compile(inputs: [DynamicGraph_Any], isEager: Bool = false) { self.t = Void() self.inputs = inputs - compileModel() + compileModel(isEager: isEager) _outputSize = Int(ccv_cnnp_model_output_size(model!.cModel)) self.inputs = nil self.t = nil @@ -258,7 +258,7 @@ extension ModelBuilder where T == Void { * Compile a model with the given inputs without executing it. After this, you can load * parameters from the store. */ - public func compile(inputs: DynamicGraph_Any...) { - compile(inputs: inputs) + public func compile(inputs: DynamicGraph_Any..., isEager: Bool = false) { + compile(inputs: inputs, isEager: isEager) } }