tensorflow / swift-apis

Swift for TensorFlow Deep Learning Library
Apache License 2.0
794 stars 133 forks source link

uncaught exception of type Xbyak #908

Closed ematejska closed 3 years ago

ematejska commented 4 years ago

Importing from JIRA: https://bugs.swift.org/browse/TF-1264

Type: Bug Priority: Minor
Reporter: Cyril Garcia Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: RunTimeCrash

Training a neural network with the softMaxCrossEntropy() function within the valueWithGradient() call back results in the following error

terminating with uncaught exception of type Xbyak::Error: can't protect

Similar error was found with TF-1230 and the provided solution did not work.

The error does not point to the valueWithGradient() function, but I assume the error came from that function by running through pint statements on every function and the printing stopped at the end of the callback.

let (loss, grad) = valueWithGradient(at: model)
{ (model: ColorModel) -> Tensor<Float> in

return softmaxCrossEntropy(logits: model(batch), labels: self.outputs[i]) 
}
optimizer.update(&model, along: grad)

Any idea on how evaluate this error?

By CyrilCyril Garcia added a comment - 26 Apr 2020 9:15 PM Further examination suggest this may be a bug when referencing the model parameter within the callback. The function works fine when referencing the model outside the callback.

Dan Zheng added a comment - 27 Apr 2020 12:43 AM Could you please share a full program reproducing the error? Also, how are you running S4TF? If you're using a S4TF toolchain, could you please also share the output of swift --version?

Cyril Garcia added a comment - 27 Apr 2020 2:20 PM Hi Dan, thanks for the response. I'm running S4TF on Xcode 11.4.1 in a macOS app template.

Sure, here is the model

struct ColorModel: Layer {

 var layer1 = Dense<Float>(inputSize: 3, 
outputSize: hiddenSize, 
activation: softmax)

 var layer2 = Dense<Float>(inputSize: hiddenSize,
outputSize: 3, 
activation: softmax)

 @differentiable
 func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> {
 return input.sequenced(through: layer1, layer2)
 }
}

training data, hard coded for testing purposes

var trainingInputs: Tensor<Float> = [[0.6862745,0.93333334,0.58431375],
 [0.34901962,0.8117647,0.043137256],
 [0.5372549,0.14117648,0.25490198]]

var trainingOutputs: Tensor<Int32> = [1, 1, 0]

The training process

let optimizer = SGD(for: model, learningRate: learningRate)

 let (loss, grad) = valueWithGradient(at: model) { (model) -> Tensor<Float> in

let val = softmaxCrossEntropy(logits: model(self.trainingInputs), labels: self.trainingOutputs)

 return val

 }

 optimizer.update(&model, along: grad)
BradLarson commented 3 years ago

I'm unable to reproduce this on macOS using the current 0.12 release toolchain. I tried all variants I could think of for the above, and none of them produced the error any longer. I'm hoping this was fixed in the interim, so I'l closing this issue.