183
© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. System Frameworks #WWDC16 Session 715 Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

System Frameworks #WWDC16

Session 715

Neural Networks and Accelerate

Eric Bainville Core OS, Vector and Numerics GroupSteve Canon Core OS, Vector and Numerics Group

Page 2: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

That thing we do in the Vector and Numerics groupPerformance Libraries for CPU

Page 3: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Optimized Low-Level Libraries

Page 4: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Optimized Low-Level Libraries

Accelerate - Image processing: vImage

Page 5: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Optimized Low-Level Libraries

Accelerate - Image processing: vImageAccelerate - Signal processing: vDSP

Page 6: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Optimized Low-Level Libraries

Accelerate - Image processing: vImageAccelerate - Signal processing: vDSPAccelerate - Linear algebra: BLAS, SparseBLAS, LAPACK, LinearAlgebra

Page 7: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Optimized Low-Level Libraries

Accelerate - Image processing: vImageAccelerate - Signal processing: vDSPAccelerate - Linear algebra: BLAS, SparseBLAS, LAPACK, LinearAlgebraVector extensions: simd

Page 8: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Optimized Low-Level Libraries

Accelerate - Image processing: vImageAccelerate - Signal processing: vDSPAccelerate - Linear algebra: BLAS, SparseBLAS, LAPACK, LinearAlgebraVector extensions: simdLossless compression: Compression

Page 9: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Optimized Low-Level Libraries

Accelerate - Image processing: vImageAccelerate - Signal processing: vDSPAccelerate - Linear algebra: BLAS, SparseBLAS, LAPACK, LinearAlgebraVector extensions: simdLossless compression: Compression

Optimized for all supported CPUs

Page 10: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Agenda

Page 11: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Agenda

Lossless compression: Compression

Page 12: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Agenda

Lossless compression: CompressionAccelerate - Machine learning: BNNS

Page 13: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Agenda

Lossless compression: CompressionAccelerate - Machine learning: BNNSAccelerate - Numerical integration: Quadrature

Page 14: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Agenda

Lossless compression: CompressionAccelerate - Machine learning: BNNSAccelerate - Numerical integration: QuadratureVector extensions: simd

Page 15: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Using Accelerate

Page 16: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

// Swift import Accelerate

// C / C++ #include <Accelerate/Accelerate.h>

// Objective-C @import Accelerate

Page 17: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Using the Accelerate Framework

Page 18: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Using the Accelerate Framework

Page 19: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Using the Accelerate Framework

Page 20: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Using the Accelerate Framework

Page 21: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Using the Accelerate Framework

Page 22: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Using the Accelerate Framework

Page 23: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Using the Accelerate Framework

Page 24: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Using the Accelerate Framework

Page 25: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Remember last year?Compression

Page 26: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

LZFSECompression

Page 27: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

LZFSECompression

Page 28: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

LZFSECompression

LZFSE is now Open Source

Page 29: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

LZFSECompression

LZFSE is now Open Source

Hosted on github.com/lzfse

Page 30: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

LZFSECompression

LZFSE is now Open Source

Hosted on github.com/lzfse

BSD license

Page 31: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

LZFSECompression

LZFSE is now Open Source

Hosted on github.com/lzfse

BSD license

1x

2x

3x

decode speed

2.6x

zlib lzfse

1x

2x

3x

encode speed

1.4x

clang -Os -march=haswell

Page 32: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Basic Neural Network SubroutinesBNNS

Page 33: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

New in AccelerateBNNS

BNNS = Basic Neural Network Subroutines BLAS = Basic Linear Algebra Subroutines

NEW

Page 34: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Deep Neural Network

Giraffe

Snake

Cat

Dog

Deep Neural Network

Weights/Bias

Training

InputImage

Page 35: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Deep Neural Network

Deep Neural Network

Giraffe

Snake

Cat

Dog

Weights/Bias

Training

Page 36: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Dog

Deep Neural Network

Giraffe

Snake

Cat

Training

Deep Neural Network

Weights/Bias

Page 37: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Dog

Deep Neural Network

Giraffe

Snake

Cat

Dog

Training

Deep Neural Network

Weights/Bias

Page 38: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Deep Neural Network

Giraffe

Snake

Cat

Dog

Deep Neural Network

Weights/Bias

Training

Dog

Page 39: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Deep Neural Network

Giraffe

Snake

Cat

Dog

Deep Neural Network

Weights/Bias

Training

Weights/Bias

Cat

Page 40: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Deep Neural Network

Giraffe

Snake

Cat

Dog

Deep Neural Network

Weights/Bias

Inference

InputImage

Page 41: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Giraffe

InferenceDeep Neural Network

Snake

Cat

Dog

Deep Neural Network

Weights/Bias

Page 42: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Giraffe

InferenceDeep Neural Network

Giraffe

Snake

Cat

Dog

Deep Neural Network

Weights/Bias

Page 43: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

13 x 13 x 5

29 x 29 x 1

5 x52convolution

13 x 13 13 x 13 13 x 13 13 x 13 13 x 13

5 x 51convolution

29 x 29

Digit Recognition NetworkExample

1

Page 44: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

100…1

13 x 13 x 5

Fully connected

13 x 13 13 x 13 13 x 13 13 x 13 13 x 13

5 x 51convolution

1Digit Recognition NetworkExample

Page 45: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

100…1

13 x 13 x 5

5 x 5 x 50

5 x 52convolution

5 x 5 5 x 5 5 x 5 5 x 5 5 x 5 5 x 5 5 x 5…

Fully connected

13 x 13 13 x 13 13 x 13 13 x 13 13 x 13

5 x 51convolution

1Digit Recognition NetworkExample

Page 46: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

5 x 5 x 50

5 x 52convolution

5 x 5 5 x 5 5 x 5 5 x 5 5 x 5 5 x 5 5 x 5…

3

Digit Recognition NetworkExample

10

Fully2connected

Page 47: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

5 x 5 x 50

5 x 52convolution

5 x 5 5 x 5 5 x 5 5 x 5 5 x 5 5 x 5 5 x 5…

100…1

Fully connected3

Digit Recognition NetworkExample

10

Fully2connected

Page 48: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

100…1

5 x 5 x 505 x 5 5 x 5 5 x 5 5 x 5 5 x 5 5 x 5 5 x 5…

Fully connected3

Digit Recognition NetworkExample

Page 49: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

100…1

5 x 5 x 50

10

5 x 5 5 x 5 5 x 5 5 x 5 5 x 5 5 x 5 5 x 5…

Fully connected3

0 1 2 3 4 5 6 7 8 9

Digit Recognition NetworkExample

Fully2connected

Page 50: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Performance, four core Haswell MacBook ProBNNS

High

er Is

Bet

ter

224 x 224 x 3 x 32 x K3

112 x 112 x 32 x 32 x K3

112 x 112 x 32 x 64 x K3

56 x 56 x 64 x 64 x K3

64 x 64 x 12 x 32 x K1

224 x 224 x 3 x 64 x K7

56 x 56 x 64 x 192 x K3

28 x 28 x 192 x 176 x K3

28 x 28 x 96 x 128 x K3

28 x 28 x 16 x 32 x K5

30 x 30 x 96 x 48 x K1

30 x 30 x 48 x 48 x K3

61 x 61 x 96 x 32 x K1

61 x 61 x 32 x 32 x K3

Page 51: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Performance, four core Haswell MacBook ProBNNS

High

er Is

Bet

ter

224 x 224 x 3 x 32 x K3

112 x 112 x 32 x 32 x K3

112 x 112 x 32 x 64 x K3

56 x 56 x 64 x 64 x K3

64 x 64 x 12 x 32 x K1

224 x 224 x 3 x 64 x K7

56 x 56 x 64 x 192 x K3

28 x 28 x 192 x 176 x K3

28 x 28 x 96 x 128 x K3

28 x 28 x 16 x 32 x K5

30 x 30 x 96 x 48 x K1

30 x 30 x 48 x 48 x K3

61 x 61 x 96 x 32 x K1

61 x 61 x 32 x 32 x K3

Caffe + Accelerate

Page 52: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Performance, four core Haswell MacBook ProBNNS

2.1x faster

High

er Is

Bet

ter

224 x 224 x 3 x 32 x K3

112 x 112 x 32 x 32 x K3

112 x 112 x 32 x 64 x K3

56 x 56 x 64 x 64 x K3

64 x 64 x 12 x 32 x K1

224 x 224 x 3 x 64 x K7

56 x 56 x 64 x 192 x K3

28 x 28 x 192 x 176 x K3

28 x 28 x 96 x 128 x K3

28 x 28 x 16 x 32 x K5

30 x 30 x 96 x 48 x K1

30 x 30 x 48 x 48 x K3

61 x 61 x 96 x 32 x K1

61 x 61 x 32 x 32 x K3

Caffe + Accelerate BNNS

Page 53: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

FeaturesBNNS

Page 54: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

FeaturesBNNS

Low-level compute functions for CPU

Page 55: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

FeaturesBNNS

Low-level compute functions for CPUInference only

Page 56: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

FeaturesBNNS

Low-level compute functions for CPUInference onlyConvolution layers

Page 57: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

FeaturesBNNS

Low-level compute functions for CPUInference onlyConvolution layersPooling layers

Page 58: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

FeaturesBNNS

Low-level compute functions for CPUInference onlyConvolution layersPooling layersFully connected layers

Page 59: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Deep Neural Network

Convolution layerDeep Neural Network

Weights/Bias

Page 60: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Deep Neural Network

Convolution layerDeep Neural Network

Weights/Bias

ConvolutionLayer

Page 61: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Convolution Layer

Input image Output imageWeights

Page 62: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Convolution Layer

Input image Output imageWeights

Page 63: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Convolution Layer

Input image Output imageWeights

Page 64: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Convolution Layer

Input image Output imageWeights

Page 65: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Convolution Layer

Input image stack Output imageWeights

Page 66: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Convolution Layer

Input image stack Output imageWeights

Page 67: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Convolution Layer

Input image stack Output image stackWeights

Page 68: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Convolution Layer

Input image stack Output image stackWeights

Page 69: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

ExampleConvolution Layer

Input image stack: 224 x 224 x 64Output image stack: 222 x 222 x 96Weights: 3 x 3 x 64 x 96Floating point operations: 5.45 billionAll layers: 1-2 trillion

Page 70: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

#include <Accelerate/Accelerate.h>

// Describe input stack

BNNSImageStackDescriptor in_stack = {

.width = 224, // width

.height = 224, // height

.channels = 64, // channels

.row_stride = 224, // increment to next row (pix)

.image_stride = 224*224, // increment to next channel (pix)

.data_type = BNNSDataTypeFloat32 // storage type

};

Page 71: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

#include <Accelerate/Accelerate.h>

// Describe convolution layer

BNNSConvolutionLayerParameters conv = {

.k_width = 3, // kernel height

.k_height = 3, // kernel width

.x_padding = 0, // X padding

.y_padding = 0, // Y padding

.x_stride = 1, // X stride

.y_stride = 1, // Y stride

.in_channels = 64, // input channels

.out_channels = 96, // output channels

.weights = {

.data_type = BNNSDataTypeFloat16, // weights storage type

.data = weights // pointer to weights data

}

};

Page 72: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

#include <Accelerate/Accelerate.h>

// Create convolution layer filter

BNNSFilter filter = BNNSFilterCreateConvolutionLayer(

&in_stack, // BNNSImageStackDescriptor for input stack

&out_stack, // BNNSImageStackDescriptor for output stack

&conv, // BNNSConvolutionLayerParameters

NULL); // BNNSFilterParameters (NULL = defaults)

// Use the filter ...

// Destroy filter

BNNSFilterDestroy(filter);

Page 73: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Deep Neural Network

Pooling layerDeep Neural Network

Weights/Bias

Page 74: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Deep Neural Network

Pooling layerDeep Neural Network

Weights/Bias

PoolingLayer

Page 75: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Pooling Layer

Input image Output image

Page 76: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

#include <Accelerate/Accelerate.h>

// Describe pooling layer

BNNSPoolingLayerParameters pool = {

.k_width = 3, // kernel height

.k_height = 3, // kernel width

.x_padding = 1, // X padding

.y_padding = 1, // Y padding

.x_stride = 2, // X stride

.y_stride = 2, // Y stride

.in_channels = 64, // input channels

.out_channels = 64, // output channels

.pooling_function = BNNSPoolingFunctionMax // pooling function

};

Page 77: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

#include <Accelerate/Accelerate.h>

// Create pooling layer filter

BNNSFilter filter = BNNSFilterCreatePoolingLayer(

&in_stack, // BNNSImageStackDescriptor for input stack

&out_stack, // BNNSImageStackDescriptor for output stack

&pool, // BNNSPoolingLayerParameters

NULL); // BNNSFilterParameters (NULL = defaults)

// Use the filter ...

// Destroy filter

BNNSFilterDestroy(filter);

Page 78: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Deep Neural Network

Fully connected layerDeep Neural Network

Weights/Bias

Page 79: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Deep Neural Network

Fully connected layerDeep Neural Network

Weights/Bias

Fully connected

Layer

Page 80: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Fully Connected LayerInput vector

Page 81: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Fully Connected Layer

Output vectorWeights

Input vector

Bias

⊕ =

Page 82: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

#include <Accelerate/Accelerate.h>

// Describe input vector

BNNSVectorDescriptor in_vec = {

.size = 3000, // size

.data_type = BNNSDataTypeFloat32 // storage type

};

Page 83: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

#include <Accelerate/Accelerate.h>

// Describe fully connected layer

BNNSFullyConnectedLayerParameters full = {

.in_size = 3000, // input vector size

.out_size = 20000, // output vector size

.weights = {

.data_type = BNNSDataTypeFloat16, // weights storage type

.data = weights // pointer to weights data

}

};

Page 84: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

#include <Accelerate/Accelerate.h>

// Create fully connected layer filter

BNNSFilter filter = BNNSFilterCreateFullyConnectedLayer(

&in_vec, // BNNSVectorDescriptor for input vector

&out_vec, // BNNSVectorDescriptor for output vector

&full, // BNNSFullyConnectedLayerParameters

NULL); // BNNSFilterParameters (NULL = defaults)

// Use the filter ...

// Destroy filter

BNNSFilterDestroy(filter);

Page 85: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

#include <Accelerate/Accelerate.h>

// Apply filter to one pair of (in,out)

int status = BNNSFilterApply(filter, // BNNSFilter

in, // pointer to input data

out); // pointer to output data

Page 86: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

#include <Accelerate/Accelerate.h>

// Apply filter to N pairs of (in,out)

int status = BNNSFilterApplyBatch(filter, // BNNSFilter

20, // batch size (N)

in, // pointer to input data

3000, // input stride (values)

out, // pointer to output data

20000); // output stride (values)

Page 87: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

BNNS

Page 88: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

BNNS

Low-level compute functions for neural networks

Page 89: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

BNNS

Low-level compute functions for neural networksFast and energy-efficient inference

Page 90: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

BNNS

Low-level compute functions for neural networksFast and energy-efficient inferenceMultiple storage types

Page 91: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Numerical integrationQuadrature

Page 92: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Numerical integrationQuadrature

a b

NEW

Page 93: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

#include <Accelerate/Accelerate.h> // Quadrature is part of Accelerate

// Describe the function to integrate

quadrature_integrate_function fun = {

.fun = f, // evaluation callback

};

// Evaluates the function at n points x[i] -> y[i]

void f(void *arg, size_t n, const double *x, double *y)

{

for (size_t i=0; i<n; i++) {

y[i] = 1.0 / (1.0 + x[i] * x[i]);

}

}

Page 94: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

#include <Accelerate/Accelerate.h> // Quadrature is part of Accelerate

// Describe the integration method and parameters

quadrature_integrate_options opt = {

.integrator = QUADRATURE_INTEGRATE_QAG, // integration algorithm

.abs_tolerance = 1.0e-8, // requested tolerance

.max_intervals = 12 // max number of intervals for QAG

};

// QNG simple non-adaptive integrator

// QAG simple globally adaptive integrator

// QAGS globally adaptive integrator with convergence acceleration

Page 95: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

#include <Accelerate/Accelerate.h>

// Compute the integral

quadrature_status status;

double est_error;

double result = quadrature_integrate(

&fun, // quadrature_integrate_function, function to integrate

-1.0, // a, first bound of interval

2.0, // b, second bound of interval

&opt, // quadrature_integrate_options, integration method and options

&status, // quadrature_status, receives success/failure code

&est_error, // double, receives the estimated absolute error

0, NULL); // optional args

Page 96: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Vector and geometry operationssimd

Steve Canon Core OS, Vector and Numerics Group

Page 97: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

simd

Page 98: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

simd

Geometric operations on vectors and matrices for C, Objective-C, C++, and Swift

Page 99: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

simd

Geometric operations on vectors and matrices for C, Objective-C, C++, and SwiftClosely mirrors Metal shading language

Page 100: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Typessimd

Page 101: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Typessimd

Vectors of floats, doubles, signed and unsigned integers of length 2, 3, and 4

Page 102: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Typessimd

Vectors of floats, doubles, signed and unsigned integers of length 2, 3, and 4Matrices of floats and doubles, of size NxM, where N and M are 2, 3, or 4

Page 103: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Operationssimd

Page 104: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Operationssimd

Arithmetic operators on vectors and matrices

Page 105: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Operationssimd

Arithmetic operators on vectors and matricesGeometry and shader functions

Page 106: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

// myCode.m: @import simd;

vector_float3 reflect(vector_float3 x, vector_float3 n) { return x - 2*vector_dot(x,n)*n; }

// myCode.cpp: #include <simd/simd.h> using namespace simd;

float3 reflect(float3 x, float3 n) { return x - 2*dot(x,n)*n; }

// myCode.swift: import simd

func reflect(x: float3, n: float3) -> float3 { return x - 2*dot(x,n)*n }

Page 107: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

// myCode.m: @import simd;

vector_float3 reflect(vector_float3 x, vector_float3 n) { return x - 2*vector_dot(x,n)*n; }

// myCode.cpp: #include <simd/simd.h> using namespace simd;

float3 reflect(float3 x, float3 n) { return x - 2*dot(x,n)*n; }

// myCode.swift: import simd

func reflect(x: float3, n: float3) -> float3 { return x - 2*dot(x,n)*n }

Page 108: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Interoperation between languagessimd

Page 109: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Interoperation between languagessimd

Vector types are compiler extensions in C, Objective-C, and C++

Page 110: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Interoperation between languagessimd

Vector types are compiler extensions in C, Objective-C, and C++Swift vector types are structs

Page 111: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Interoperation between languagessimd

Vector types are compiler extensions in C, Objective-C, and C++Swift vector types are structsThe compiler maps between corresponding vector types for you

Page 112: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

// myHeader.h: @import simd;

vector_float3 someFunction(vector_float3 x, vector_float3 y);

// myCode.swift: import simd

let x = float3(1,2,3) let y = float3(0,0,1) // Vector types are bridged automatically. let z = someFunction(x, y)

Page 113: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

// myHeader.h: @import simd;

vector_float3 someFunction(vector_float3 x, vector_float3 y);

// myCode.swift: import simd

let x = float3(1,2,3) let y = float3(0,0,1) // Vector types are bridged automatically. let z = someFunction(x, y)

Page 114: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

// myHeader.h: @import simd;

vector_float3 someFunction(vector_float3 x, vector_float3 y);

// myCode.swift: import simd

let x = float3(1,2,3) let y = float3(0,0,1) // Vector types are bridged automatically. let z = someFunction(x, y)

Page 115: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Interoperation between languagessimd

Page 116: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Interoperation between languagessimd

Swift matrix types are layout-compatible with C matrix types

Page 117: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

import simd

// Use initializer to convert C matrix to Swift matrix. let mat = float4x4(CFunctionReturningMatrix())

// Use cmatrix property to convert Swift matrix to C matrix. let result = CFunctionConsumingMatrix(mat.cmatrix)

Page 118: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

import simd

// Use initializer to convert C matrix to Swift matrix. let mat = float4x4(CFunctionReturningMatrix())

// Use cmatrix property to convert Swift matrix to C matrix. let result = CFunctionConsumingMatrix(mat.cmatrix)

Page 119: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

import simd

// Use initializer to convert C matrix to Swift matrix. let mat = float4x4(CFunctionReturningMatrix())

// Use cmatrix property to convert Swift matrix to C matrix. let result = CFunctionConsumingMatrix(mat.cmatrix)

Page 120: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

NEWNew Geometry Functions

simd_orient(x, y, …) simd_incircle(x, a, b, c) simd_insphere(x, a, b, c, d)

Page 121: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient

Page 122: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient

Is a set of vectors positively oriented?

Page 123: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient

Is a set of vectors positively oriented?• Do they obey the right hand rule?

Page 124: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient

Is a set of vectors positively oriented?• Do they obey the right hand rule?• Is their determinant positive?

Page 125: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient

Is a triangle facing toward me or away from me?

Page 126: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient

Is a triangle facing toward me or away from me?-

-

Page 127: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

-

orient

Is a triangle facing toward me or away from me?-

-

Page 128: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient

Is this point on that line? If not, which side of the line is it on?

Page 129: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient

Is this point on that line? If not, which side of the line is it on?

Page 130: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient Examplelet a = float2(0,0) let b = float2(6,3) let c = float2

let orientation = simd_orient(a, b, c)

if orientation > 0 { print("(a,b,c) is positively oriented.")

else if orientation < 0 { print("(a,b,c) is negatively oriented.")

else /* orientation is zero */ { print("(a,b,c) are collinear.")

(1,5)

}

}

}

(4,0)

Page 131: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient Example

c

a

b

let a = float2(0,0) let b = float2(6,3) let c = float2

let orientation = simd_orient(a, b, c)

if orientation > 0 { print("(a,b,c) is positively oriented.")

else if orientation < 0 { print("(a,b,c) is negatively oriented.")

else /* orientation is zero */ { print("(a,b,c) are collinear.")

(1,5)

}

}

}

(4,0)

Page 132: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient Example

c

a

b

let a = float2(0,0) let b = float2(6,3) let c = float2

let orientation = simd_orient(a, b, c)

if orientation > 0 { print("(a,b,c) is positively oriented.")

else if orientation < 0 { print("(a,b,c) is negatively oriented.")

else /* orientation is zero */ { print("(a,b,c) are collinear.")

(1,5)

}

}

}

(4,0)

Page 133: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient Example

-

c

a

b

let a = float2(0,0) let b = float2(6,3) let c = float2

let orientation = simd_orient(a, b, c)

if orientation > 0 { print("(a,b,c) is positively oriented.")

else if orientation < 0 { print("(a,b,c) is negatively oriented.")

else /* orientation is zero */ { print("(a,b,c) are collinear.")

(1,5)

}

}

}

(4,0)

Page 134: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient Example

ca

-

b

let a = float2(0,0) let b = float2(6,3) let c = float2

let orientation = simd_orient(a, b, c)

if orientation > 0 { print("(a,b,c) is positively oriented.")

else if orientation < 0 { print("(a,b,c) is negatively oriented.")

else /* orientation is zero */ { print("(a,b,c) are collinear.")

}

}

}

(1,5)(4,0)(4,2)

Page 135: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient Example

ca

-

b

let a = float2(0,0) let b = float2(6,3) let c = float2

let orientation = simd_orient(a, b, c)

if orientation > 0 { print("(a,b,c) is positively oriented.")

else if orientation < 0 { print("(a,b,c) is negatively oriented.")

else /* orientation is zero */ { print("(a,b,c) are collinear.")

}

}

}

(1,5)(4,0)(4,2)

Page 136: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient Example

c

a

b

let a = float2(0,0) let b = float2(6,3) let c = float2

let orientation = simd_orient(a, b, c)

if orientation > 0 { print("(a,b,c) is positively oriented.")

else if orientation < 0 { print("(a,b,c) is negatively oriented.")

else /* orientation is zero */ { print("(a,b,c) are collinear.")

}

}

}

(4,0)(4,2)

Page 137: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

orient Example

c

a

b

let a = float2(0,0) let b = float2(6,3) let c = float2

let orientation = simd_orient(a, b, c)

if orientation > 0 { print("(a,b,c) is positively oriented.")

else if orientation < 0 { print("(a,b,c) is negatively oriented.")

else /* orientation is zero */ { print("(a,b,c) are collinear.")

}

}

}

(4,0)(4,2)

Page 138: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Numerical Stability

Page 139: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Numerical Stability

Orientation is numerically unstable

Page 140: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Numerical Stability

Orientation is numerically unstableWhen points are nearly collinear, usual algorithms produce garbage results

Page 141: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Numerical Stability

import simd

let tiny = Float(1).ulp let u = float2(1, 1+tiny) let v = float2(1-tiny, 1)

let m = float2x2([u, v]) matrix_determinant(m.cmatrix)

scale greatly exaggerated

1.192093e-07 float2(1.0,1.0) float2(1.0,1.0)

float2x2([[… 0

Page 142: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Numerical Stability

import simd

let tiny = Float(1).ulp let u = float2(1, 1+tiny) let v = float2(1-tiny, 1)

let m = float2x2([u, v]) matrix_determinant(m.cmatrix)

v

u

scale greatly exaggerated

1.192093e-07 float2(1.0,1.0) float2(1.0,1.0)

float2x2([[… 0

Page 143: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Numerical Stability

import simd

let tiny = Float(1).ulp let u = float2(1, 1+tiny) let v = float2(1-tiny, 1)

let m = float2x2([u, v]) matrix_determinant(m.cmatrix)

v

u

scale greatly exaggerated

1.192093e-07 float2(1.0,1.0) float2(1.0,1.0)

float2x2([[… 0

Page 144: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Numerical Stability

v

u

scale greatly exaggerated

import simd

let tiny = Float(1).ulp let u = float2(1, 1+tiny) let v = float2(1-tiny, 1)

simd_orient(u, v)

1.192093e-07 float2(1.0,1.0) float2(1.0,1.0)

1.421085e-14

Page 145: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Numerical Stability

v

u

scale greatly exaggerated

import simd

let tiny = Float(1).ulp let u = float2(1, 1+tiny) let v = float2(1-tiny, 1)

simd_orient(u, v)

1.192093e-07 float2(1.0,1.0) float2(1.0,1.0)

1.421085e-14

Page 146: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Numerical Stability

Page 147: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Numerical Stability

These geometric predicates use adaptive precision

Page 148: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Numerical Stability

These geometric predicates use adaptive precisionComputation uses as many bits as needed to produce the correct result

Page 149: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

incircle

Three points (a, b, c) determine a circle

a

b

c

Page 150: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

incircle

simd_incircle(x, a, b, c)

a

b

cx

Page 151: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

incircle

simd_incircle(x, a, b, c)• Positive if x is inside the circle

a

b

cx

Page 152: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

incircle

simd_incircle(x, a, b, c)• Positive if x is inside the circle• Zero if x is on the circle

a

b

c

x

Page 153: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

incircle

simd_incircle(x, a, b, c)• Positive if x is inside the circle• Zero if x is on the circle• Negative if x is outside the circle

a

b

c

x

Page 154: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

c

insphere

d

a

b

xsimd_insphere(x, a, b, c, d) is the same thing in three dimensions

Page 155: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

import simd

/// Simple struct representing a triangle in 3 dimensions. struct Triangle { var vertices: (float3, float3, float3) /// True iff `self` faces towards `camera`. func isFacing(camera: float3) -> Bool { // Vector normal to front face of triangle. let normal = cross(vertices.0 - vertices.2, vertices.1 - vertices.2) // Vector from triangle to camera. let toCamera = camera - vertices.2 // If dot product is positive, the triangle faces the camera. return dot(toCamera, normal) > 0 } }

Page 156: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

import simd

/// Simple struct representing a triangle in 3 dimensions. struct Triangle { var vertices: (float3, float3, float3) /// True iff `self` faces towards `camera`. func isFacing(camera: float3) -> Bool { // Vector normal to front face of triangle. let normal = cross(vertices.0 - vertices.2, vertices.1 - vertices.2) // Vector from triangle to camera. let toCamera = camera - vertices.2 // If dot product is positive, the triangle faces the camera. return dot(toCamera, normal) > 0 } }

Page 157: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

import simd

/// Simple struct representing a triangle in 3 dimensions. struct Triangle { var vertices: (float3, float3, float3) /// True iff `self` faces towards `camera`. func isFacing(camera: float3) -> Bool { // Vector normal to front face of triangle. let normal = cross(vertices.0 - vertices.2, vertices.1 - vertices.2) // Vector from triangle to camera. let toCamera = camera - vertices.2 // If dot product is positive, the triangle faces the camera. return dot(toCamera, normal) > 0 } }

Page 158: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

import simd

/// Simple struct representing a triangle in 3 dimensions. struct Triangle { var vertices: (float3, float3, float3) /// True iff `self` faces towards `camera`. func isFacing(camera: float3) -> Bool { return simd_orient(camera, vertices.0, vertices.1, vertices.2) > 0 } }

Page 159: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

import simd

/// Simple struct representing a triangle in 3 dimensions. struct Triangle { var vertices: (float3, float3, float3) /// True iff `self` faces towards `camera`. func isFacing(camera: float3) -> Bool { return simd_orient(camera, vertices.0, vertices.1, vertices.2) > 0 } }

Page 160: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

import simd

/// Simple struct representing a triangle in 3 dimensions. struct Triangle { var vertices: (float3, float3, float3) /// True iff `self` faces towards `camera`. func isFacing(camera: float3) -> Bool { return simd_orient(camera, vertices.0, vertices.1, vertices.2) > 0 } }

Page 161: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

What’s New?

Page 162: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

What’s New?

New libraries

Page 163: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

What’s New?

New libraries• BNNS

Page 164: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

What’s New?

New libraries• BNNS• Quadrature

Page 165: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

What’s New?

New libraries• BNNS• Quadrature

New features

Page 166: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

What’s New?

New libraries• BNNS• Quadrature

New features• Orientation and Incircle

Page 167: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

What’s New?

New libraries• BNNS• Quadrature

New features• Orientation and Incircle

All added in response to feature requests!

Page 168: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

OK, but What Else Is New?

Page 169: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

OK, but What Else Is New?

vImage geometry operations for interleaved chroma planes

Page 170: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

OK, but What Else Is New?

vImage geometry operations for interleaved chroma planesExpanded supported formats for vImage conversion

Page 171: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

OK, but What Else Is New?

vImage geometry operations for interleaved chroma planesExpanded supported formats for vImage conversionImproved performance for interleaved complex formats in vDSP

Page 172: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

OK, but What Else Is New?

vImage geometry operations for interleaved chroma planesExpanded supported formats for vImage conversionImproved performance for interleaved complex formats in vDSPImproved performance of level 2 BLAS operations

Page 173: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

OK, but What Else Is New?

vImage geometry operations for interleaved chroma planesExpanded supported formats for vImage conversionImproved performance for interleaved complex formats in vDSPImproved performance of level 2 BLAS operations…

Page 174: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Summary

Page 175: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Summary

Single-stop shopping for computational operations

Page 176: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Summary

Single-stop shopping for computational operations• Correct

Page 177: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Summary

Single-stop shopping for computational operations• Correct• Fast

Page 178: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Summary

Single-stop shopping for computational operations• Correct• Fast• Energy efficient

Page 179: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Summary

Single-stop shopping for computational operations• Correct• Fast• Energy efficient

Keep the feature requests coming!

Page 180: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

More Information

https://developer.apple.com/wwdc16/715

81

Page 181: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

What’s New in Metal, Part 2 Pacific Heights Wednesday 1:40PM

Advanced Metal Shader Optimization Nob Hill Wednesday 3:00PM

Increase Usage of Your App with Proactive Suggestions Mission Friday 1:40PM

Related Sessions

Page 182: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group

Accelerate Lab Graphics, Games, and Media Lab D Thursday 12:00PM

Accelerate Lab Fort Mason Thursday 5:00PM

Labs

Page 183: Neural Networks and Accelerate - Apple Inc.€¦ · Neural Networks and Accelerate Eric Bainville Core OS, Vector and Numerics Group Steve Canon Core OS, Vector and Numerics Group