## Slide 1

### Slide 1 text

Swi\$Ͱॻ͔ΕͨϓϩάϥϜΛ 1000ഒ଎ͨ͘͠࿩ Yuta Koshizawa @koher

No content

## Slide 3

### Slide 3 text

͕Μ͹ͬͨΒߦྻͷੵͷܭࢉ͕ 1000 ഒ଎͘ͳͬͨ

## Slide 4

### Slide 4 text

ͦͷͱ͖ʹ Swi% ͍͢͝ͱײͨ͡࿩

## Slide 5

### Slide 5 text

Tensor let a = Tensor(shape: [2, 3], elements: [1, 2, 3, 4, 5, 6]) // [[1, 2, 3], [4, 5, 6]] let b = Tensor(shape: [2, 2, 2], elements: [1, 2, 3, 4, 5, 6, 7, 8]) // [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]

## Slide 6

### Slide 6 text

ཁૉ΁ͷΞΫηε let a = Tensor(shape: [2, 3], elements: [1, 2, 3, 4, 5, 6]) // [[1, 2, 3], [4, 5, 6]] let b = Tensor(shape: [2, 2, 2], elements: [1, 2, 3, 4, 5, 6, 7, 8]) // [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] print(a[1, 2]) // 6 print(b[1, 1, 1]) // 8

## Slide 7

### Slide 7 text

Tensor ͷ࣮૷ public struct Tensor { public let shape: [Int] public private(set) var elements: [Float] }

## Slide 8

### Slide 8 text

subscript ͷ࣮૷ extension Tensor { internal func index(indices: [Int]) -> Int { return zip(shape, indices).reduce(0) { \$0 * \$1.0 + \$1.1 } } public subscript(indices: Int...) -> Float { get { return elements[index(indices)] } set { elements[index(indices)] = newValue } } }

## Slide 9

### Slide 9 text

matmul extension Tensor { // Matrix public func matmul(tensor: Tensor) -> Tensor { precondition(shape.count == 2) precondition(tensor.shape.count == 2) let n = shape[1] precondition(n == tensor.shape[0]) let numRows = shape[0] let numCols = tensor.shape[1] var elements: [Float] = [] elements.reserveCapacity(numCols * numRows) for r in 0..

## Slide 10

### Slide 10 text

ߦྻʹݶఆ extension Tensor { // Matrix public func matmul(tensor: Tensor) -> Tensor { precondition(shape.count == 2) precondition(tensor.shape.count == 2) let n = shape[1] precondition(n == tensor.shape[0]) let numRows = shape[0] let numCols = tensor.shape[1] var elements: [Float] = [] elements.reserveCapacity(numCols * numRows) for r in 0..

1.84 ഒߴ଎Խ

## Slide 12

### Slide 12 text

ϧʔϓॱΛೖΕସ͑ͯΩϟογϡͷώοτ཰Λ޲্ extension Tensor { // Matrix public func matmul(tensor: Tensor) -> Tensor { precondition(shape.count == 2) precondition(tensor.shape.count == 2) let n = shape[1] precondition(n == tensor.shape[0]) let numRows = shape[0] let numCols = tensor.shape[1] var elements = [Float](count: numCols * numRows, repeatedValue: 0.0) for r in 0..

͞Βʹ 4.69 ഒߴ଎Խ

## Slide 14

### Slide 14 text

ϙΠϯλʹॻ͖ม͑ extension Tensor { // Matrix public func matmul(tensor: Tensor) -> Tensor { precondition(shape.count == 2) precondition(tensor.shape.count == 2) let n = shape[1] precondition(n == tensor.shape[0]) let numRows = shape[0] let numCols = tensor.shape[1] // `Array` ΛϙΠϯλʹม׵ let leftHead = UnsafeMutablePointer(self.elements) let rightHead = UnsafeMutablePointer(tensor.elements) let elements = [Float](count: numCols * numRows, repeatedValue: 0.0) for r in 0..(elements) + r * numCols let left = leftHead[r * n + i] var rightPointer = rightHead + i * numCols for _ in 0..

͞Βʹ 2.99 ഒߴ଎Խ

## Slide 16

### Slide 16 text

BLAS ͷར༻ import Accelerate extension Tensor { // Matrix public func matmul(tensor: Tensor) -> Tensor { precondition(shape.count == 2) precondition(tensor.shape.count == 2) precondition(shape[1] == tensor.shape[0]) let result = Tensor(shape: [shape[0], tensor.shape[1]], elements: [Float](count: shape[0] * tensor.shape[1],repeatedValue: 0.0)) let n = Int32(tensor.shape[1]) let k = Int32(shape[1]) cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, Int32(shape[0]), n, k, 1.0, elements, k, tensor.elements, n, 1.0, UnsafeMutablePointer(result.elements), n) return result } }

͞Βʹ 39.1 ഒߴ଎Խ

͋Θͤͯ 1012 ഒߴ଎Խ

## Slide 19

### Slide 19 text

Swi\$ ͸ Swi\$ ͳʢ଎͍ʣ ϓϩάϥϜ͕ॻ͚ΔΑ͏ʹ ࡞ΒΕ͍ͯΔʂʂ

## Slide 20

### Slide 20 text

References 6 Accelerate Framework: h1ps:/ /developer.apple.com/library/tvos/documenta=on/Accelerate/Reference/AccelerateFWRef/index.html 5 BLAS: h)p:/ /www.netlib.org/blas/ 4 "Deep MNIST for Experts": h5ps:/ /www.tensorﬂow.org/versions/r0.8/tutorials/mnist/pros/index.html 3 "TensorFlowͰֶशͨ͠ϞσϧΛ࢖ͬͯiOS/Swi1Ͱ࣮ߦ͢Δ" h3p:/ /qiita.com/koher/items/2c0bfca4d6e31cde674b 2 TensorSwi,: h/ps:/ /github.com/qoncept/TensorSwi, 1 Qoncept: h,p:/ /qoncept.co.jp/