Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Example/BatchNormTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ struct BatchNormDataSet {

static func test1(useScale: Bool, depth: Int = 8) -> Test {
let texture = Texture(
data: [[Float].init(repeating: 1, count: depth), [Float].init(repeating: 2, count: depth),
[Float].init(repeating: 3, count: depth), [Float].init(repeating: 4, count: depth),
[Float].init(repeating: 5, count: depth), [Float].init(repeating: 6, count: depth)],
data: [[Float](repeating: 1, count: depth), [Float](repeating: 2, count: depth),
[Float](repeating: 3, count: depth), [Float](repeating: 4, count: depth),
[Float](repeating: 5, count: depth), [Float](repeating: 6, count: depth)],
size: LayerSize(h: 3, w: 2, f: depth)
)

Expand Down
92 changes: 46 additions & 46 deletions Example/Example/Tests/ConcatTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,41 @@ struct ConcatDataSet {
let texture1Depth = depth
let texture1 = Texture(
data: [
[Float].init(repeating: 1, count: texture1Depth), [Float].init(repeating: 2, count: texture1Depth),
[Float].init(repeating: 3, count: texture1Depth), [Float].init(repeating: 4, count: texture1Depth),
[Float].init(repeating: 5, count: texture1Depth), [Float].init(repeating: 6, count: texture1Depth),
[Float](repeating: 1, count: texture1Depth), [Float](repeating: 2, count: texture1Depth),
[Float](repeating: 3, count: texture1Depth), [Float](repeating: 4, count: texture1Depth),
[Float](repeating: 5, count: texture1Depth), [Float](repeating: 6, count: texture1Depth),
],
size: LayerSize(h: 3, w: 2, f: texture1Depth)
)
let texture2Depth = depth
let texture2 = Texture(
data: [
// row
[Float].init(repeating: 1, count: texture2Depth), [Float].init(repeating: 2, count: texture2Depth),
[Float].init(repeating: 3, count: texture2Depth),
[Float](repeating: 1, count: texture2Depth), [Float](repeating: 2, count: texture2Depth),
[Float](repeating: 3, count: texture2Depth),
// row
[Float].init(repeating: 4, count: texture2Depth), [Float].init(repeating: 5, count: texture2Depth),
[Float].init(repeating: 6, count: texture2Depth),
[Float](repeating: 4, count: texture2Depth), [Float](repeating: 5, count: texture2Depth),
[Float](repeating: 6, count: texture2Depth),
// row
[Float].init(repeating: 7, count: texture2Depth), [Float].init(repeating: 8, count: texture2Depth),
[Float].init(repeating: 8, count: texture2Depth),
[Float](repeating: 7, count: texture2Depth), [Float](repeating: 8, count: texture2Depth),
[Float](repeating: 8, count: texture2Depth),
],
size: LayerSize(h: 3, w: 3, f: texture2Depth)
)
let expectedDepth = depth
let expectedData: [[Float]] = [
// row
[Float].init(repeating: 1, count: expectedDepth), [Float].init(repeating: 2, count: expectedDepth),
[Float].init(repeating: 1, count: expectedDepth), [Float].init(repeating: 2, count: expectedDepth),
[Float].init(repeating: 3, count: expectedDepth),
[Float](repeating: 1, count: expectedDepth), [Float](repeating: 2, count: expectedDepth),
[Float](repeating: 1, count: expectedDepth), [Float](repeating: 2, count: expectedDepth),
[Float](repeating: 3, count: expectedDepth),
// row
[Float].init(repeating: 3, count: texture1Depth), [Float].init(repeating: 4, count: texture1Depth),
[Float].init(repeating: 4, count: texture2Depth), [Float].init(repeating: 5, count: texture2Depth),
[Float].init(repeating: 6, count: texture2Depth),
[Float](repeating: 3, count: texture1Depth), [Float](repeating: 4, count: texture1Depth),
[Float](repeating: 4, count: texture2Depth), [Float](repeating: 5, count: texture2Depth),
[Float](repeating: 6, count: texture2Depth),
// row
[Float].init(repeating: 5, count: texture1Depth), [Float].init(repeating: 6, count: texture1Depth),
[Float].init(repeating: 7, count: texture2Depth), [Float].init(repeating: 8, count: texture2Depth),
[Float].init(repeating: 8, count: texture2Depth),
[Float](repeating: 5, count: texture1Depth), [Float](repeating: 6, count: texture1Depth),
[Float](repeating: 7, count: texture2Depth), [Float](repeating: 8, count: texture2Depth),
[Float](repeating: 8, count: texture2Depth),
]

let expected: Texture = Texture(
Expand All @@ -78,33 +78,33 @@ struct ConcatDataSet {
let texture1Depth = depth
let texture1 = Texture(
data: [
[Float].init(repeating: 1, count: texture1Depth), [Float].init(repeating: 2, count: texture1Depth),
[Float].init(repeating: 3, count: texture1Depth), [Float].init(repeating: 4, count: texture1Depth),
[Float].init(repeating: 5, count: texture1Depth), [Float].init(repeating: 6, count: texture1Depth),
[Float](repeating: 1, count: texture1Depth), [Float](repeating: 2, count: texture1Depth),
[Float](repeating: 3, count: texture1Depth), [Float](repeating: 4, count: texture1Depth),
[Float](repeating: 5, count: texture1Depth), [Float](repeating: 6, count: texture1Depth),
],
size: LayerSize(h: 3, w: 2, f: texture1Depth)
)
let texture2Depth = depth
let texture2 = Texture(
data: [
[Float].init(repeating: 1, count: texture1Depth), [Float].init(repeating: 2, count: texture1Depth),
[Float].init(repeating: 3, count: texture1Depth), [Float].init(repeating: 4, count: texture1Depth),
[Float].init(repeating: 5, count: texture1Depth), [Float].init(repeating: 6, count: texture1Depth),
[Float].init(repeating: 9, count: texture1Depth), [Float].init(repeating: 7, count: texture1Depth),
[Float].init(repeating: 3, count: texture1Depth), [Float].init(repeating: 3, count: texture1Depth),
[Float](repeating: 1, count: texture1Depth), [Float](repeating: 2, count: texture1Depth),
[Float](repeating: 3, count: texture1Depth), [Float](repeating: 4, count: texture1Depth),
[Float](repeating: 5, count: texture1Depth), [Float](repeating: 6, count: texture1Depth),
[Float](repeating: 9, count: texture1Depth), [Float](repeating: 7, count: texture1Depth),
[Float](repeating: 3, count: texture1Depth), [Float](repeating: 3, count: texture1Depth),
],
size: LayerSize(h: 5, w: 2, f: texture2Depth)
)
let expectedDepth = depth
let expectedData: [[Float]] = [
[Float].init(repeating: 1, count: texture1Depth), [Float].init(repeating: 2, count: texture1Depth),
[Float].init(repeating: 3, count: texture1Depth), [Float].init(repeating: 4, count: texture1Depth),
[Float].init(repeating: 5, count: texture1Depth), [Float].init(repeating: 6, count: texture1Depth),
[Float].init(repeating: 1, count: texture1Depth), [Float].init(repeating: 2, count: texture1Depth),
[Float].init(repeating: 3, count: texture1Depth), [Float].init(repeating: 4, count: texture1Depth),
[Float].init(repeating: 5, count: texture1Depth), [Float].init(repeating: 6, count: texture1Depth),
[Float].init(repeating: 9, count: texture1Depth), [Float].init(repeating: 7, count: texture1Depth),
[Float].init(repeating: 3, count: texture1Depth), [Float].init(repeating: 3, count: texture1Depth),
[Float](repeating: 1, count: texture1Depth), [Float](repeating: 2, count: texture1Depth),
[Float](repeating: 3, count: texture1Depth), [Float](repeating: 4, count: texture1Depth),
[Float](repeating: 5, count: texture1Depth), [Float](repeating: 6, count: texture1Depth),
[Float](repeating: 1, count: texture1Depth), [Float](repeating: 2, count: texture1Depth),
[Float](repeating: 3, count: texture1Depth), [Float](repeating: 4, count: texture1Depth),
[Float](repeating: 5, count: texture1Depth), [Float](repeating: 6, count: texture1Depth),
[Float](repeating: 9, count: texture1Depth), [Float](repeating: 7, count: texture1Depth),
[Float](repeating: 3, count: texture1Depth), [Float](repeating: 3, count: texture1Depth),
]

let expected: Texture = Texture(
Expand All @@ -118,32 +118,32 @@ struct ConcatDataSet {
let texture1Depth = depth1
let texture1 = Texture(
data: [
[Float].init(repeating: 1, count: texture1Depth), [Float].init(repeating: 2, count: texture1Depth),
[Float].init(repeating: 3, count: texture1Depth), [Float].init(repeating: 4, count: texture1Depth),
[Float].init(repeating: 5, count: texture1Depth), [Float].init(repeating: 6, count: texture1Depth),
[Float](repeating: 1, count: texture1Depth), [Float](repeating: 2, count: texture1Depth),
[Float](repeating: 3, count: texture1Depth), [Float](repeating: 4, count: texture1Depth),
[Float](repeating: 5, count: texture1Depth), [Float](repeating: 6, count: texture1Depth),
],
size: LayerSize(h: 3, w: 2, f: texture1Depth)
)
let texture2Depth = depth2
let texture2 = Texture(
data: [
[Float].init(repeating: 7, count: texture2Depth), [Float].init(repeating: 8, count: texture2Depth),
[Float].init(repeating: 9, count: texture2Depth), [Float].init(repeating: 10, count: texture2Depth),
[Float].init(repeating: 11, count: texture2Depth), [Float].init(repeating: 12, count: texture2Depth),
[Float](repeating: 7, count: texture2Depth), [Float](repeating: 8, count: texture2Depth),
[Float](repeating: 9, count: texture2Depth), [Float](repeating: 10, count: texture2Depth),
[Float](repeating: 11, count: texture2Depth), [Float](repeating: 12, count: texture2Depth),
],
size: LayerSize(h: 3, w: 2, f: texture2Depth)
)
let expectedDepth = depth1 + depth2
let expectedData: [[Float]] = [
// row
[Float].init(repeating: 1, count: texture1Depth) + [Float].init(repeating: 7, count: texture2Depth),
[Float].init(repeating: 2, count: texture1Depth) + [Float].init(repeating: 8, count: texture2Depth),
[Float](repeating: 1, count: texture1Depth) + [Float](repeating: 7, count: texture2Depth),
[Float](repeating: 2, count: texture1Depth) + [Float](repeating: 8, count: texture2Depth),
// row
[Float].init(repeating: 3, count: texture1Depth) + [Float].init(repeating: 9, count: texture2Depth),
[Float].init(repeating: 4, count: texture1Depth) + [Float].init(repeating: 10, count: texture2Depth),
[Float](repeating: 3, count: texture1Depth) + [Float](repeating: 9, count: texture2Depth),
[Float](repeating: 4, count: texture1Depth) + [Float](repeating: 10, count: texture2Depth),
// row
[Float].init(repeating: 5, count: texture1Depth) + [Float].init(repeating: 11, count: texture2Depth),
[Float].init(repeating: 6, count: texture1Depth) + [Float].init(repeating: 12, count: texture2Depth)
[Float](repeating: 5, count: texture1Depth) + [Float](repeating: 11, count: texture2Depth),
[Float](repeating: 6, count: texture1Depth) + [Float](repeating: 12, count: texture2Depth)
]

let expected: Texture = Texture(
Expand Down
8 changes: 4 additions & 4 deletions Example/Example/Tests/Helpers/Texture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public class Texture {
data[y * width + x][z] = newValue
}
}

var width: Int {
return size.w
}

var height: Int {
return size.h
}

var depth: Int {
return size.f
}
Expand Down Expand Up @@ -164,5 +164,5 @@ extension Texture {
}
return texture
}

}
10 changes: 5 additions & 5 deletions Example/Example/Tests/InstanceNormTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class InstanceNormTest: BenderTest {

func test(texture: Texture, completion: @escaping () -> ()) {
let styleNet = Network(inputSize: texture.size)
let weights = [Float].init(repeating: Float.random(), count: texture.depth)
let bias = [Float].init(repeating: Float.random(), count: texture.depth)
let scale = Data.init(bytes: weights, count: texture.totalCount * MemoryLayout<Float>.stride)
let shift = Data.init(bytes: bias, count: texture.totalCount * MemoryLayout<Float>.stride)
let weights = [Float](repeating: Float.random(), count: texture.depth)
let bias = [Float](repeating: Float.random(), count: texture.depth)
let scale = Data(bytes: weights, count: texture.totalCount * MemoryLayout<Float>.stride)
let shift = Data(bytes: bias, count: texture.totalCount * MemoryLayout<Float>.stride)
styleNet.start ->> InstanceNorm(scale: scale, shift: shift)
styleNet.initialize()
let metalTexture = texture.metalTexture(with: Device.shared)
Expand Down Expand Up @@ -100,5 +100,5 @@ class InstanceNormTest: BenderTest {
// }
return output
}

}
2 changes: 1 addition & 1 deletion Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit
class TestViewController: UIViewController {

let testRunner = BenderTestRunner()

override func viewDidLoad() {
super.viewDidLoad()
testRunner.run()
Expand Down
6 changes: 3 additions & 3 deletions Example/MNISTTestController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MNISTTestController: UIViewController, ExampleViewController {

// If you want to create it from scratch comment the line above and uncomment:
// createMNISTNetwork()

var me = self
me.setPixelBufferPool()
setupMetalView()
Expand Down Expand Up @@ -197,7 +197,7 @@ class MNISTTestController: UIViewController, ExampleViewController {
}

}

}

// MARK: - AVCaptureVideoDataOutputSampleBufferDelegate
Expand Down Expand Up @@ -231,7 +231,7 @@ extension MNISTTestController: AVCaptureVideoDataOutputSampleBufferDelegate {
} else {
debugPrint("samplebuffer is nil \(sampleBuffer)")
}

return nil
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Adapters/TFOptimizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public protocol TFOptimizer {

/// Optimize a grsph imported from TensorFlow. Nodes that are to be removed should be left without adjacencies
func optimize(graph: TFGraph)

}

public extension TFOptimizer {
Expand All @@ -20,7 +20,7 @@ public extension TFOptimizer {
/// This information can later be used by the 'activationNeuron' function
func addNeuronIfThere(node: TFNode) {
let outgoing = node.outgoingNodes()
if outgoing.count == 1, let next = (outgoing.first as? TFNode),
if let next = (outgoing.first as? TFNode),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't want to remove this check. There could be more than one outgoingNodes

next.nodeDef.isTFReLuOp || next.nodeDef.isTFTanhOp || next.nodeDef.isTFSigmoidOp {
var neuron = Tensorflow_AttrValue()
neuron.value = Tensorflow_AttrValue.OneOf_Value.s(next.nodeDef.op.data(using: .utf8)!)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adapters/Tensorflow/ProtoExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension Tensorflow_NodeDef {
let formatString = String(data: dataFormat, encoding: .utf8) else {
return (Int(strides[1]), Int(strides[2]))
}

let strideX = formatString == "NHWC" ? strides[2] : strides[3]
let strideY = formatString == "NHWC" ? strides[1] : strides[2]
return (Int(strideX), Int(strideY))
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adapters/Tensorflow/TFConvOptimizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ class TFConvOptimizer: TFOptimizer {
}
}
}

}
2 changes: 1 addition & 1 deletion Sources/Adapters/Tensorflow/TFConverter+Mappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,5 @@ public extension TFConverter {
}
mappers[Constants.Ops.BatchNormGlobal] = batchnormMapper
}

}
4 changes: 2 additions & 2 deletions Sources/Adapters/Tensorflow/TFConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public enum ProtoFileType {

case binary
case text

}

/// Converts a TFNode to a NetworkLayer of Bender
Expand Down Expand Up @@ -105,7 +105,7 @@ open class TFConverter: Converter {
}

/// Runs the mappers through all the nodes in the `graph`.
/// Ops that cannot be mapped are discarded. If these ops are in the main path of the graph then the resulting graph will be disconnected.
/// Ops that cannot be mapped are discarded. If these ops are in the main path of the graph then the resulting graph will be disconnected.
///
/// - Parameter graph: The TFGraph to be mapped
/// - Returns: An array of mapped oprations as NetworkLayer's
Expand Down
6 changes: 3 additions & 3 deletions Sources/Adapters/Tensorflow/TFDeleteOptimizers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class TFIgnoredOpsDeleter: TFOptimizer {

let ops = ["NoOp", "ExpandDims", "Cast", "Squeeze", "StopGradient", "CheckNumerics", "Assert", "Equal", "All",
"Dequantize", "RequantizationRange", "Requantize", "PlaceholderWithDefault", "Identity"]

public func optimize(graph: TFGraph) {
for node in graph.nodes {
if ops.contains(node.nodeDef.op) {
Expand All @@ -41,7 +41,7 @@ public class TFIgnoredOpsDeleter: TFOptimizer {
public class TFDeleteSave: TFDeleteSubgraphOptimizer {

public var regex: Regex = try! Regex("save(_\\d+)?/")

}

/// Deletes 'Initializer' subgraphs
Expand Down Expand Up @@ -79,5 +79,5 @@ fileprivate extension String {
let regex = try! Regex("dropout(_\\d+)?/mul")
return regex.test(self)
}

}
4 changes: 2 additions & 2 deletions Sources/Adapters/Tensorflow/TFDeleteSubgraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/// Deletes a specific subgraph from a TFGraph
public protocol TFDeleteSubgraphOptimizer: TFOptimizer {

/// This Regex tells if a node is in a subgraph to be deleted or not.
/// This Regex tells if a node is in a subgraph to be deleted or not.
/// If the node's name has a match for this regex then it will be considered as belonging to a subgraph
var regex: Regex { get set }

Expand Down Expand Up @@ -93,5 +93,5 @@ public extension TFDeleteSubgraphOptimizer {
// wire together
rewire(mappings: mappings)
}

}
2 changes: 1 addition & 1 deletion Sources/Adapters/Tensorflow/TFDenseSubstitution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class TFDenseSubstitution: TFOptimizer {
MatMul --> BiasAdd [--> Neuron]
^ ^
Variable Variable

Returns:
Variable -> BiasAdd(+add-ons) <- Variable

Expand Down
4 changes: 2 additions & 2 deletions Sources/Adapters/Tensorflow/TFGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class TFGraph: GraphProtocol {
for node in nodes {
// Filter TF control inputs
let filtered = node.nodeDef.input.filter { $0.first != "^" }

for input in filtered {
if let inputNode = nodesByName[input] {
node.addIncomingEdge(from: inputNode)
Expand All @@ -46,5 +46,5 @@ public class TFGraph: GraphProtocol {
var me = self
me.sortNodes()
}

}
4 changes: 2 additions & 2 deletions Sources/Adapters/Tensorflow/TFInstanceNormOptimizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TFInstanceNormOptimizer: TFDeleteSubgraphOptimizer {
Input --> InstanceNormAdd --> Output
^ ^
Variable -> InstanceNormMul | Variable

Set_of_nodes is ([Add -> Pow, Sub] -> RealDiv)

*/
Expand Down Expand Up @@ -79,5 +79,5 @@ public class TFInstanceNormOptimizer: TFDeleteSubgraphOptimizer {
}
}
}

}
Loading