Skip to content

Commit

Permalink
Support for cocoapods and docs update (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Ernst authored and bryant1410 committed Aug 9, 2017
1 parent aa12f15 commit 73a62e6
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 30 deletions.
2 changes: 0 additions & 2 deletions Bender.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
8F0669B71EC37424009F5C7C /* ParameterLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F0669B61EC37424009F5C7C /* ParameterLoader.swift */; };
8F0669BA1EC3A31B009F5C7C /* PerLayerBinaryLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F0669B91EC3A31B009F5C7C /* PerLayerBinaryLoader.swift */; };
8F0669BC1EC3A4C1009F5C7C /* SingleBinaryLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F0669BB1EC3A4C1009F5C7C /* SingleBinaryLoader.swift */; };
8F06933A1F352B3C0004D3E9 /* MetalPerformanceShadersStub.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F0693391F352B3C0004D3E9 /* MetalPerformanceShadersStub.framework */; };
8F3DE15A1EBB962200D81949 /* Network.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F3DE1591EBB962200D81949 /* Network.swift */; };
8F3DE15D1EBB9ACC00D81949 /* MetalShaderManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F3DE15C1EBB9ACC00D81949 /* MetalShaderManager.swift */; };
8F3DE15F1EBBD0C700D81949 /* Add.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F3DE15E1EBBD0C700D81949 /* Add.swift */; };
Expand Down Expand Up @@ -177,7 +176,6 @@
files = (
8FC2B3A51EE5D9B80038E582 /* SwiftProtobuf.framework in Frameworks */,
8F7C22321F30EE5B006DA176 /* MetalPerformanceShadersProxy.framework in Frameworks */,
8F06933A1F352B3C0004D3E9 /* MetalPerformanceShadersStub.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
16 changes: 14 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
# Change Log
All notable changes to Bender will be documented in this file.

### [1.0.0](https://github.com/xmartlabs/Bender/releases/tag/1.0.0)
<!-- Released on ????-??-??. -->
### [0.2.0](https://github.com/xmartlabs/Bender/releases/tag/0.2.0)
<!-- Released on 2017-08-09. -->

* Adds Cocoapods & Carthage support.
* Adds [MetalPerformanceShadersProxy](https://github.com/xmartlabs/MetalPerformanceShadersProxy) in order to allow compilation on simulators.
* Adds support for Concat layer.
* Removes "convSize" constructor parameter from ResidualLayer.
* Fixes ordering issue while optimizing TensorFlow graph.
* Fixes memory usage issue.
* Fixes ConvTranspose layer.
* Adds optional dispatchQueue where to run.

### [0.1.0](https://github.com/xmartlabs/Bender/releases/tag/0.1.0)
<!-- Released on 2017-06-05. -->

* This is the initial version.

Expand Down
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "apple/swift-protobuf" "0.9.904"
github "xmartlabs/MetalPerformanceShadersProxy" ~> 0.1
github "xmartlabs/MetalPerformanceShadersProxy" ~> 0.1.5
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "xmartlabs/MetalPerformanceShadersProxy" "0.1.3"
github "apple/swift-protobuf" "0.9.904"
github "xmartlabs/MetalPerformanceShadersProxy" "0.1.5"
8 changes: 4 additions & 4 deletions Documentation/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This document explains the basic API in __Bender__.
To create a network model you can create it from scratch or [import](Importing.md) it from a TensorFlow graph. We will explain how to create a network from scratch:

```swift
let network = Network(device: device,
let network = Network(device: device,
inputSize: inputSize,
parameterLoader: loader)

Expand Down Expand Up @@ -47,7 +47,7 @@ previousLayer
->> Add()
```

In this case the output of `previousLayer` is passed to two different Convolution layers and then tthrough a normalization layer and after that they are added with the `Add` layer.
In this case the output of `previousLayer` is passed to two different Convolution layers and then through a normalization layer and after that they are added with the `Add` layer.

After you finish adding layers to your network, you must call `network.initialize()` to finish setting up your network.

Expand Down Expand Up @@ -138,7 +138,7 @@ The MetalShaderManager keeps all the custom kernel functions that an app has loa

It also manages the function constants passed to these Metal functions. If you have a function that relies on function constants then you can pass them to the MetalShaderManager when you get your function. The function to get a kernel function is this:

```swift
```swift
/// Get a MTLComputePipelineState with a Metal function of the given name
///
/// - Parameters:
Expand Down Expand Up @@ -196,4 +196,4 @@ A `CompositeLayer` is the same. It will be destroyed as soon as it is applied in
[ParameterLoader]: #parameterloader
[Creating custom layers]: #custom-layers
[MetalShaderManager]: #metalshadermanager
[Composing Layers]: #composing-layers
[Composing Layers]: #composing-layers
12 changes: 6 additions & 6 deletions Documentation/Importing.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# Importing

Bender aims to be a lightweight framework that executes its neural networks on the GPU and also allows to import models from other frameworks without adding big binaries.
Bender aims to be a lightweight framework that executes its neural networks on the GPU and also allows to import models from other frameworks without adding big binaries.
In the case of TensorFlow, you could use it to run code using Accelerate framework (CPU) but to include TensorFlow in your app you have to compile it as a static binary and it will add around 23 MB to your app (June 2017).
Bender offers a lightweight solution which also uses the GPU for performance gain.

Expand Down Expand Up @@ -38,7 +38,7 @@ let url = Bundle.main.url(forResource: "myGraph", withExtension: "pb")!
let converter = TFConverter.default()

// Convert graph
network.nodes = converter.convertGraph(file: url, type: .binary)
network.convert(converter: converter, url: url, type: .binary)

// Initialize network
network.initialize()
Expand All @@ -50,9 +50,9 @@ network.initialize()
### Default `TFConverter` and limitations

The default converter will make the following simplifications / optimizations:
* Remove subgraphs named with 'save', 'dropout', 'Regularizer' and 'Initializer' as this are training functions. There will certainly be more subgraphs added to this list in the future. This is implemented with `TFStripTrainingOps` and `TFDeleteDropout`.
* Remove subgraphs named with 'save', 'dropout', 'Regularizer' and 'Initializer' as these are training functions. There will certainly be more subgraphs added to this list in the future. This is implemented with `TFStripTrainingOps` and `TFDeleteDropout`.
* Process Variables (`TFVariableProcessor`): It leaves only constant variables (not randomly initialized).
* Create FullyConnected (`TFDenseSubstitution`): In TF, the fully connected layer is implemented as a MatMul with an BiasAdd (or Add). If these nodes appear in the graph then they will be substitued by a 'FullyConnected' node.
* Create FullyConnected (`TFDenseSubstitution`): In TF, the fully connected layer is implemented as a MatMul with an BiasAdd (or Add). If these nodes appear in the graph then they will be substituted by a 'FullyConnected' node.
* Remove Reshape (`TFReshapeOptimizer`): Removes reshape nodes from the graph. We should investigate if in future releases we want to do something else in this case.
* Add bias to a Convolution (`TFConvOptimizer`).

Expand All @@ -70,13 +70,13 @@ For __MPSCNNConvolution__ you need to pass [outputChannels, kernelHeight, kernel
If the default optimizers do not fill your needs you might want to add custom optimizers. Also keep in mind that Bender does not support every function implemented in TensorFlow so it might be that you use not supported ops. In this case you can create a custom layer as explained below. We are open to receive Pull Requests implementing more layers.

To create an optimizer, create a class that conforms to `TFOptimizer`.
A TFOptimizer does just have one simple function:
A TFOptimizer does just have one simple function:

```swift
func optimize(graph: TFGraph)
```

After you create the optimizer, you have to add it to your `TFConverter` like this;
After you create the optimizer, you have to add it to your `TFConverter` like this:

```swift
let converter = TFConverter.default()
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Supported_Layers.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ Also there are some layers that can be used for pre and post processing or which


Last but not least, there is a _CompositeLayer_ included:
* __Residual__: A Residual layer contains a group of sublayers which are executed and the ouput is the result of this sublayers added to the input of the residual layer.

* __Residual__: A Residual layer contains a group of sublayers which are executed and the output is the result of this sublayers added to the input of the residual layer.

## TensorFlow Mapping

The following are the TensorFlow ops that are mapped to Bender layers:
* __Add__ -> Add (works for two textures/tensors of the same size)
Expand Down
4 changes: 0 additions & 4 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
28F828DA1C4B714D00330CF4 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 28F828D81C4B714D00330CF4 /* LaunchScreen.storyboard */; };
46FAFCBD1F02CB89008754E1 /* ConcatTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46FAFCBC1F02CB89008754E1 /* ConcatTest.swift */; };
46FAFCC11F04142C008754E1 /* ConstantLayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46FAFCC01F04142C008754E1 /* ConstantLayer.swift */; };
8F0693371F3527F60004D3E9 /* MetalPerformanceShadersStub.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F0693361F3527F60004D3E9 /* MetalPerformanceShadersStub.framework */; };
8F0693381F3527F60004D3E9 /* MetalPerformanceShadersStub.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 8F0693361F3527F60004D3E9 /* MetalPerformanceShadersStub.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
8F28C68D1ED724BE0062039F /* g_and_w.pb in Resources */ = {isa = PBXBuildFile; fileRef = 8F28C68C1ED724BE0062039F /* g_and_w.pb */; };
8F4466D01ECF756B00915819 /* UIImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F4466CF1ECF756B00915819 /* UIImage.swift */; };
8F46BCAD1EE0B52D00DF275A /* mnist-conv1_b in Resources */ = {isa = PBXBuildFile; fileRef = 8F46BCA51EE0B52D00DF275A /* mnist-conv1_b */; };
Expand Down Expand Up @@ -75,7 +73,6 @@
files = (
CFC14FFA1F396B6A00ACC4A1 /* MetalBender.framework in Embed Frameworks */,
8F7C22351F30EE6E006DA176 /* MetalPerformanceShadersProxy.framework in Embed Frameworks */,
8F0693381F3527F60004D3E9 /* MetalPerformanceShadersStub.framework in Embed Frameworks */,
8FCD3EA81ED8BE1F00B1AAE6 /* SwiftProtobuf.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
Expand Down Expand Up @@ -140,7 +137,6 @@
files = (
CFC14FF91F396B6A00ACC4A1 /* MetalBender.framework in Frameworks */,
8F7C22341F30EE6E006DA176 /* MetalPerformanceShadersProxy.framework in Frameworks */,
8F0693371F3527F60004D3E9 /* MetalPerformanceShadersStub.framework in Frameworks */,
8FCD3EA71ED8BE1F00B1AAE6 /* SwiftProtobuf.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
10 changes: 10 additions & 0 deletions Example/Example.xcodeproj/xcshareddata/xcschemes/Example.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "28F828DF1C4B714D00330CF4"
BuildableName = "ExampleUITests.xctest"
BlueprintName = "ExampleUITests"
ReferencedContainer = "container:Example.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
Expand Down
4 changes: 2 additions & 2 deletions MetalBender.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "MetalBender"
s.version = "0.1.0"
s.version = "0.2.0"
s.summary = "Bender is an abstraction layer over MetalPerformanceShaders useful for working with neural networks."
s.homepage = "https://github.com/xmartlabs/Bender"
s.license = { type: 'MIT', file: 'LICENSE' }
Expand All @@ -11,5 +11,5 @@ Pod::Spec.new do |s|
s.requires_arc = true
s.ios.source_files = 'Sources/**/*.{swift,metal}'
s.dependency 'SwiftProtobuf', '0.9.903'
s.dependency 'MetalPerformanceShadersProxy', '0.1.3'
s.dependency 'MetalPerformanceShadersProxy', '0.1.5'
end
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Bender is functional but still under active development and we also want to see

At [Xmartlabs] we were about to start a Machine Learning project and investigated frameworks to use in iOS. We found MetalPerformanceShaders useful but not very user friendly and we saw ourselves repeating a lot of code and information. That is why we starting building a framework to handle that kind of stuff.

We also found ourselves creating scripts to translate the models we had from training with TensorFlow to iOS. This means transposing the weights to the MPSCNN format and also mapping the parameters of the different kinds of layers in TensorFlow to the parameters used by the MPSCNN kernels. TensorFlow can be compiled for iOS but currently it does not support running on GPU which we wanted to do. We also did not want to include TensorFlow's static library into our project. This is why we also started to work on an adapter that would parse a TF graph and translate it to our Bender layers.
We also found ourselves creating scripts to translate the models we had from training with TensorFlow to iOS. This means transposing the weights to the MPSCNN format and also mapping the parameters of the different kinds of layers in TensorFlow to the parameters used by the MPSCNN kernels. TensorFlow can be compiled for iOS but currently it does not support running on GPU which we wanted to do. We also did not want to include TensorFlow's static library into our project. This is why we also started to work on an adapter that would parse a TF graph and translate it to our Bender layers.

## Usage

Expand All @@ -57,7 +57,8 @@ let network = Network(device: device, inputSize: LayerSize(f: 3, w: 256), parame

// Convert a graph from TensorFlow
let url = Bundle.main.url(forResource: "myGraph", withExtension: "pb")!
TFConverter.default().convertGraph(file: url, type: .binary)
let converter = TFConverter.default()
network.convert(converter: converter, url: url, type: .binary)

// Initialize the network
network.initialize()
Expand Down Expand Up @@ -108,7 +109,7 @@ If you use **Bender** in your app We would love to hear about it! Drop us a line

## Examples

Follow these steps to run the examples:
Follow these steps to run the examples:
* Clone Bender repository (or download it).
* Run `carthage update --platform iOS` in the downloaded folder.
* Open Bender workspace and run the *Example* project.
Expand All @@ -120,7 +121,7 @@ Follow these steps to run the examples:
To install Bender, simply add the following line to your Podfile:

```ruby
pod 'MetalBender', :git => 'https://github.com/xmartlabs/Bender.git'
pod 'MetalBender', '~> 0.2'
```

> Remember that Bender compiles for iOS 10. So you must add `platform :ios, '10.0'` to your Podfile
Expand All @@ -132,7 +133,7 @@ pod 'MetalBender', :git => 'https://github.com/xmartlabs/Bender.git'
To install Bender, add the following line to your Cartfile:

```ogdl
github "xmartlabs/Bender"
github "xmartlabs/Bender" ~> 0.2
```

Then run:
Expand All @@ -141,7 +142,7 @@ Then run:
carthage update --platform iOS
```

Finally, drag the built `.framework` binaries for `MetalBender`, `MetalPerformanceShadersProxy`, `MetalPerformanceShadersStub` and `SwiftProtobuf` to your application's Xcode project.
Finally, drag the built `.framework` binaries for `MetalBender`, `MetalPerformanceShadersProxy` and `SwiftProtobuf` to your application's Xcode project.

## Author

Expand Down

0 comments on commit 73a62e6

Please sign in to comment.