Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat(examples): simple vgg trained on cifar #9

Merged
merged 57 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
91751c2
Rename MAKitTestsUtils -> MATestsUtils.
jean-francoisreboud Oct 16, 2022
638485b
Fix retryNumeric.
jean-francoisreboud Oct 16, 2022
8f0614f
More retries in Layer2DFlowReverseTests BN.
jean-francoisreboud Oct 16, 2022
9a622fe
CI ?
jean-francoisreboud Oct 16, 2022
8210b34
CI ?
jean-francoisreboud Oct 16, 2022
b3afdd7
CI ?
jean-francoisreboud Oct 16, 2022
8223070
CI ?
jean-francoisreboud Oct 16, 2022
712a785
CI ?
jean-francoisreboud Oct 16, 2022
3dd59cf
CI ?
jean-francoisreboud Oct 16, 2022
c3f2616
CI ?
jean-francoisreboud Oct 16, 2022
64fadf8
CI ?
jean-francoisreboud Oct 16, 2022
3d48052
CI ?
jean-francoisreboud Oct 16, 2022
d460592
CI ?
jean-francoisreboud Oct 16, 2022
9a72981
CI ?
jean-francoisreboud Oct 16, 2022
c302337
CI ?
jean-francoisreboud Oct 16, 2022
e0befb8
CI ?
jean-francoisreboud Oct 16, 2022
4fef319
CI ?
jean-francoisreboud Oct 16, 2022
baff9e7
CI ?
jean-francoisreboud Oct 16, 2022
26736df
CI ?
jean-francoisreboud Oct 16, 2022
da740f9
CI ?
jean-francoisreboud Oct 16, 2022
b256df2
CI ?
jean-francoisreboud Oct 16, 2022
bb069f1
CI ?
jean-francoisreboud Oct 16, 2022
865371c
CI ?
jean-francoisreboud Oct 16, 2022
d269879
CI ?
jean-francoisreboud Oct 16, 2022
3a44513
CI ?
jean-francoisreboud Oct 17, 2022
ac854f1
CI ?
jean-francoisreboud Oct 17, 2022
961b015
CI ?
jean-francoisreboud Oct 17, 2022
aa6ad02
CI ?
jean-francoisreboud Oct 17, 2022
648c88a
CI ?
jean-francoisreboud Oct 17, 2022
2b72b0a
CI ?
jean-francoisreboud Oct 17, 2022
7a74c72
CI ?
jean-francoisreboud Oct 17, 2022
bc3b1d6
CI ?
jean-francoisreboud Oct 17, 2022
1f93553
CI ?
jean-francoisreboud Oct 17, 2022
5df469a
CI ?
jean-francoisreboud Oct 17, 2022
2acf44f
CI ?
jean-francoisreboud Oct 17, 2022
73a604f
CI ?
jean-francoisreboud Oct 17, 2022
ba162c7
CI ?
jean-francoisreboud Oct 17, 2022
36bc21e
CI ?
jean-francoisreboud Oct 17, 2022
359387d
CI ?
jean-francoisreboud Oct 17, 2022
a0756ea
CI ?
jean-francoisreboud Oct 17, 2022
f216a09
CI ?
jean-francoisreboud Oct 17, 2022
d6b34fa
CI ?
jean-francoisreboud Oct 17, 2022
d2022f7
CI ?
jean-francoisreboud Oct 17, 2022
2e4b1f2
CI ?
jean-francoisreboud Oct 17, 2022
b534525
CI ?
jean-francoisreboud Oct 17, 2022
2277ae4
CI ?
jean-francoisreboud Oct 17, 2022
d8c25a7
CI ?
jean-francoisreboud Oct 17, 2022
3ff31c3
CI ?
jean-francoisreboud Oct 17, 2022
03be162
VGGExample init.
jean-francoisreboud Oct 17, 2022
c6eb213
CI ?
jean-francoisreboud Oct 17, 2022
e1d0fb6
Rework Inputs + outputs API in GPU.
jean-francoisreboud Oct 18, 2022
2d14d61
CI ?
jean-francoisreboud Oct 18, 2022
84aa7ad
Some docstring.
jean-francoisreboud Oct 18, 2022
f336e81
Remove DataSampler, added forgotten Model API.
jean-francoisreboud Oct 18, 2022
e77078c
Fix getLossGPU batchSize + some docstring.
jean-francoisreboud Oct 18, 2022
0a79c05
Some doc.
jean-francoisreboud Oct 18, 2022
4d2695f
Some doc.
jean-francoisreboud Oct 18, 2022
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tests/MAExamples/Base/data/* filter=lfs diff=lfs merge=lfs -text
45 changes: 45 additions & 0 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: examples

on:
workflow_dispatch:
push:
branches:
- main
- release**

jobs:
MAExamples:
runs-on: self-hosted
defaults:
run:
shell: bash -l {0}

steps:
- uses: actions/checkout@v3
with:
lfs: true
- name: Pull lfs
run: git lfs pull

- name: Setup Conda Environment
run: |
conda create --name maexamples-ci python=3.7
conda env list

- name: Install Python Library
working-directory: Tests/MAExamples/Base
run: |
conda activate maexamples-ci
pip="$(dirname `which python`)"/pip
$pip install -e .

- name: Test
run: |
conda activate maexamples-ci
swift test --filter MAExamples

- name: Remove Conda Environment
if: always()
run: |
conda deactivate
conda env remove --name maexamples-ci
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Swift
name: unit-tests

on: [push]

jobs:
Tests:
MAKit:
runs-on: self-hosted

steps:
Expand All @@ -14,4 +14,4 @@ jobs:
- name: Build
run: swift build
- name: Test
run: swift test
run: swift test --filter MAKitTests
52 changes: 52 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/MAKit.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,48 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "MATestsUtils"
BuildableName = "MATestsUtils"
BlueprintName = "MATestsUtils"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "MAData"
BuildableName = "MAData"
BlueprintName = "MAData"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "MAKit_MAExamples"
BuildableName = "MAKit_MAExamples"
BlueprintName = "MAKit_MAExamples"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand All @@ -80,6 +122,16 @@
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "MAExamples"
BuildableName = "MAExamples"
BlueprintName = "MAExamples"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
14 changes: 14 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "pythonkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pvieito/PythonKit",
"state" : {
"branch" : "master",
"revision" : "060e1c8b0d14e4d241b3623fdbe83d0e3c81a993"
}
}
],
"version" : 2
}
17 changes: 13 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ let package = Package(
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "MAKit",
targets: ["MAKit", "MAKitTestsUtils"]
targets: ["MAKit", "MATestsUtils"]
),
],
dependencies: [
.package(
url: "https://github.com/pvieito/PythonKit",
branch: "master"
),
],
dependencies: [],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
Expand All @@ -27,12 +32,16 @@ let package = Package(
]
),
.target(
name: "MAKitTestsUtils",
name: "MATestsUtils",
dependencies: ["MAKit"]
),
.testTarget(
name: "MAKitTests",
dependencies: ["MAKit", "MAKitTestsUtils"]
dependencies: ["MAKit", "MATestsUtils"]
),
.testTarget(
name: "MAExamples",
dependencies: ["MAKit", "PythonKit"]
),
]
)
73 changes: 72 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,75 @@
# MAKit

Maximal Activation Kit.
A deep-learning framework for computer vision interpretability.
A deep-learning framework for computer vision.

It aims at promoting a total control and understanding of the main
operations needed to train deep learning models.

The `Layer` is the essential component needed to build an explicit graph of
operations from the data input to the ground truth.

The `Model` is a component that wraps the different layers together to
call functions in a simple way.

The API explicitly exposes the following functions:

- forward pass
- backward pass
- forward pass for the gradient checking.

## Model

## Layer

# MAExamples

`MAExamples` contains examples that show how to interact with `MAKit`.
The examples are organised as tests so as to demonstrate some capabilities.
They may be run in local (Xcode or command line)
or in the CI (release/main branches or explicit trigger).

The following examples are currently available:

- VGGExample

## VGG Example

This example trains a simple model on the GPU.
The dataset used is CIFAR 10.
We want to train the model to discriminate between 2 labels
(not the 10 available).

### Setup

This example has some Python dependencies. In order to run
the example, we first have to setup the environment:

```bash
conda create --name maexamples python=3.7
conda activate maexamples
cd Tests/MAExamples/Base
pip install -e .
```

Then you should be able to run the tests right from XCode or
with a `bash` command:

```bash
swift test --filter MAExamples
```

You may eventually clean the environment with:

```bash
conda deactivate
conda env remove --name maexamples
```

### Steps

1. Dump the training and testing datasets.
2. Evaluate a random model on the testing dataset: watch a bad performance.
3. Train a model on the training dataset.
4. Evaluate the trained model on the testing dataset:
watch a better performance.
45 changes: 45 additions & 0 deletions Sources/MAKit/Core/Layer/LayerNormalization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,38 @@ public class BatchNormalization: BatchNormalizationBase
/// Slight modification to avoid "divide by 0" errors.
let _Ɛ: Double = 1e-5

///
/// Arrays of weights to scale the normalization result.
/// Shape ~ (nbNeurons,).
///
var _Ɣ: WeightArrays! = nil
///
/// Arrays of biases to add to the normalization result.
/// Shape ~ (nbNeurons,).
///
var _β: WeightArrays! = nil

///
/// List of deviations of data for the different independent batch normalization units.
/// Shape ~ (nbNeurons,).
///
var _σ2 = [Double]()

///
/// List of global averages of data for the different independent batch normalization units.
/// Shape ~ (nbNeurons,).
///
var _Eμ = [Double]()
///
/// List of global deviations of data for the different independent batch normalization units.
/// Shape ~ (nbNeurons,).
///
var _Eσ2 = [Double]()

///
/// The list of data normalized without taking into account the biases and the weights.
/// Shape ~ (nbNeurons, (batch x height x width)).
///
var _xHat = [[Double]]()

/// Weights in the CPU execution context.
Expand Down Expand Up @@ -504,26 +522,53 @@ public class BatchNormalization: BatchNormalizationBase
/// A layer that applies batch normalization in the GPU execution context.
class BatchNormalizationGPU: BatchNormalizationBase
{
///
/// Buffers of weights to scale the normalization result.
/// Shape ~ (nbNeurons,).
///
var _ƔBuffers: IWeightBuffers! = nil
///
/// Buffers of biases to add to the normalization result.
/// Shape ~ (nbNeurons,).
///
var _βBuffers: IWeightBuffers! = nil

///
/// Buffer of averages of data for the different independent batch normalization units.
/// Shape ~ (nbNeurons,).
///
var _μ: MetalBuffer<Float>! = nil
///
/// Buffer of global averages of data for the different independent batch normalization units.
/// Shape ~ (nbNeurons,).
///
var _Eμ: MetalPrivateBuffer<Float>! = nil
///
/// Buffer of deviations of data for the different independent batch normalization units.
/// Shape ~ (nbNeurons,).
///
var _σ2: MetalBuffer<Float>! = nil
///
/// Buffer of global deviations of data for the different independent batch normalization units.
/// Shape ~ (nbNeurons,).
///
var _Eσ2: MetalPrivateBuffer<Float>! = nil

///
/// Buffer of data normalized without taking into account the biases and the weights.
/// Shape ~ (batch, nbNeurons, height, width).
///
var _xHat: MetalBuffer<Float>! = nil

///
/// Buffer used to compute backward pass.
/// Shape ~ (nbNeurons,).
///
var _sum1: MetalBuffer<Float>! = nil
///
/// Buffer used to compute backward pass.
/// Shape ~ (nbNeurons,).
///
var _sum2: MetalBuffer<Float>! = nil

/// GPU device on which model is executed.
Expand Down
30 changes: 29 additions & 1 deletion Sources/MAKit/Core/Model/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,35 @@ public class Model: BaseModel
}
}

// TODO: add elements here.
/// Downscale factor of the resolution (height and width).
public var strideFactor: Double?
{
get {
for layer in layers.reversed()
{
if let layer2D = layer as? Layer2D
{
return layer2D.strideFactor
}
}
return nil
}
}

/// The size of the input image this layer is looking at.
public var receptiveField: Int?
{
get {
for layer in layers.reversed()
{
if let layer2D = layer as? Layer2D
{
return layer2D.receptiveField
}
}
return nil
}
}

/// Get/Set the weights of the different layers.
public var weights: [[Float]]
Expand Down
Loading