@@ -200,23 +200,29 @@ public class Model: AnyModel {
200200 /**
201201 * Move the backing tensor into the parameters. Making the original one invalid afterwards.
202202 */
203- public func attach( consuming keyValues: [ ( key: String , value: AnyTensor ) ] ) {
203+ public func attach( consuming keyValues: inout [ ( key: String , value: AnyTensor ) ] ) {
204204 let keys = keyValues. map { $0. key. utf8CString }
205205 let values = keyValues. map ( \. value)
206- withExtendedLifetime ( keys) { keys in
206+ // Once we attached, we remove all the keyValues that are consumed.
207+ keyValues = withExtendedLifetime ( keys) { keys in
207208 withExtendedLifetime ( values) { values in
208209 var cTensors : [ UnsafeMutablePointer < ccv_nnc_tensor_t > ? ] = values. map { $0. cTensor }
209210 var cKeys : [ UnsafeMutablePointer < CChar > ? ] = keys. map {
210211 $0. withUnsafeBufferPointer { $0. baseAddress. map { UnsafeMutablePointer ( mutating: $0) } }
211212 }
212- cKeys. withUnsafeMutableBufferPointer { cKeys in
213+ return cKeys. withUnsafeMutableBufferPointer { cKeys in
213214 cTensors. withUnsafeMutableBufferPointer { cTensors in
214215 ccv_cnnp_model_set_parameters_from_key_values (
215216 model!. cModel, cKeys. baseAddress, cTensors. baseAddress, Int32 ( keyValues. count) , 1 )
217+ var newKeyValues = [ ( key: String, value: AnyTensor) ] ( )
216218 for (i, value) in values. enumerated ( ) {
217- guard cTensors [ i] == nil else { continue }
219+ guard cTensors [ i] == nil else {
220+ newKeyValues. append ( keyValues [ i] )
221+ continue
222+ }
218223 value. consume ( )
219224 }
225+ return newKeyValues
220226 }
221227 }
222228 }
0 commit comments