Skip to content

Commit

Permalink
Add isEager to push the compilation further up until just about execu…
Browse files Browse the repository at this point in the history
…ting.
  • Loading branch information
liuliu committed Dec 22, 2023
1 parent 1d74c0c commit 0f94004
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 11 additions & 3 deletions nnc/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}

Expand Down
22 changes: 11 additions & 11 deletions nnc/ModelBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -214,10 +214,10 @@ public final class ModelBuilder<T>: 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
Expand All @@ -227,8 +227,8 @@ public final class ModelBuilder<T>: 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)
}
}

Expand All @@ -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
Expand All @@ -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)
}
}

0 comments on commit 0f94004

Please sign in to comment.