halfmoon-mind commited on
Commit
4a43526
·
verified ·
1 Parent(s): 87565bd

Decoder: upsamplers as transposed convolutions, all on the Neural Engine

Browse files

The decoder's three upsamplers rearranged their channels through reshapes the Neural Engine does not take, so 37 operations ran on the CPU. For one frame they are now 2x2 stride-2 transposed convolutions holding the same weights; the rewritten decoder equals the original to 1.4e-5 in FP32. On iPhone 14 Pro it loads in 0.1 s instead of 13-15 s, runs a tile in 2.0 s instead of 3.1-3.5 s, and a four-tile capture peaked at 0.6 GiB instead of 1.8 GiB, with output 76 dB from the earlier decoder's. Other packages are unchanged.

README.md CHANGED
@@ -50,9 +50,10 @@ Measured on iPhone 14 Pro (A16, iOS 18.4), second run, one model resident at a t
50
  | --- | ---: | ---: |
51
  | Encoder (Neural Engine) | 0.3 s | 1.7 s |
52
  | DiT, seven chunks (GPU) | 2.8 s | 5.0 s |
53
- | Decoder (Neural Engine) | 15.1 s | 3.5 s |
54
 
55
- - **Memory.** The footprint peaked at 1.6 GiB while the decoder loaded.
 
56
  - **Run the DiT on the GPU** (`.cpuAndGPU`). The A16 Neural Engine computed these iOS 18 compressed weights wrongly. FP16 and the VAE were correct there, and the same packages were correct on an M4 Mac's Neural Engine.
57
 
58
  ## Changes from the original weights
@@ -76,6 +77,7 @@ The following were changed:
76
  - Attention is written as matrix products and a softmax.
77
  - In FP32 these chunks equal the original forward pass: the largest difference was 0.
78
  5. **Quantization.** Quantized the DiT weights to 4-bit linear with 32-weight blocks (coremltools 9.0). The VAE stays FP16. On one test frame the decoded image is 34.5 dB from the FP32 model's, and the iPhone 14 Pro output is 56 dB from the Mac's.
 
79
 
80
  The conversion scripts are kept in the app's repository (`research/ReferenceSRStudy`).
81
 
 
50
  | --- | ---: | ---: |
51
  | Encoder (Neural Engine) | 0.3 s | 1.7 s |
52
  | DiT, seven chunks (GPU) | 2.8 s | 5.0 s |
53
+ | Decoder (Neural Engine) | 0.1 s | 2.0 s |
54
 
55
+ - **Memory.** A four-tile capture peaked at 0.6 GiB for the whole app. The decoder before this revision took 13–15 s to load every time,
56
+ also a second time in one launch, held 1.4 GB once loaded, and the capture peaked at 1.8 GiB.
57
  - **Run the DiT on the GPU** (`.cpuAndGPU`). The A16 Neural Engine computed these iOS 18 compressed weights wrongly. FP16 and the VAE were correct there, and the same packages were correct on an M4 Mac's Neural Engine.
58
 
59
  ## Changes from the original weights
 
77
  - Attention is written as matrix products and a softmax.
78
  - In FP32 these chunks equal the original forward pass: the largest difference was 0.
79
  5. **Quantization.** Quantized the DiT weights to 4-bit linear with 32-weight blocks (coremltools 9.0). The VAE stays FP16. On one test frame the decoded image is 34.5 dB from the FP32 model's, and the iPhone 14 Pro output is 56 dB from the Mac's.
80
+ 6. **Decoder upsamplers for the Neural Engine.** Each of the decoder's three upsamplers enlarges with a 1×1×1 convolution and then rearranges its channels, `b (x y z c) f h w -> b c (f z) (h x) (w y)`. Core ML lowered that to reshapes through dimensions of up to 16,777,216, which ran on the CPU between Neural Engine segments (37 operations). For one frame the same result is, for each time index, a 2 × 2 transposed convolution with stride 2 holding the 1×1 convolution's weights, with its position-dependent bias spread the same way from a plane of ones; the time frames the upsampler removes are not computed. In FP32 the rewritten decoder equals the original to 1.4e-5. Every operation now runs on the Neural Engine, and on iPhone 14 Pro its output is 76 dB from the earlier decoder's.
81
 
82
  The conversion scripts are kept in the app's repository (`research/ReferenceSRStudy`).
83
 
SeedVR2Decoder.mlpackage/Data/com.apple.CoreML/model.mlmodel CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:957ab0f87506a39e8e4af168c45bc25e8132718eae672ef2d1ef9ca49e3c4c58
3
- size 230791
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e3bafae7ad0ffbbc3afdefb0bbe694d567e38ba58628d58fb6b9e2a1a323f2c4
3
+ size 231878
SeedVR2Decoder.mlpackage/Data/com.apple.CoreML/weights/weight.bin CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:5f83d24b0a1fc47b1195c78b6bafa7c0fec6881cd1028d76ae1d7ded2d02f226
3
- size 301216640
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7ec1636a919f746a46b2f4d2594929455df0af68aa5009d48ed2ee97c5908cad
3
+ size 301389120
SeedVR2Decoder.mlpackage/Manifest.json CHANGED
@@ -1,18 +1,18 @@
1
  {
2
  "fileFormatVersion": "1.0.0",
3
  "itemInfoEntries": {
4
- "4948C1D4-7FB7-4FF6-A0C0-71D55083BB71": {
5
  "author": "com.apple.CoreML",
6
  "description": "CoreML Model Weights",
7
  "name": "weights",
8
  "path": "com.apple.CoreML/weights"
9
  },
10
- "DC0D5921-32B6-4ACF-BE3D-26487CE3DE9C": {
11
  "author": "com.apple.CoreML",
12
  "description": "CoreML Model Specification",
13
  "name": "model.mlmodel",
14
  "path": "com.apple.CoreML/model.mlmodel"
15
  }
16
  },
17
- "rootModelIdentifier": "DC0D5921-32B6-4ACF-BE3D-26487CE3DE9C"
18
  }
 
1
  {
2
  "fileFormatVersion": "1.0.0",
3
  "itemInfoEntries": {
4
+ "496EF50D-A152-4E54-B135-9B70B2B80EFF": {
5
  "author": "com.apple.CoreML",
6
  "description": "CoreML Model Weights",
7
  "name": "weights",
8
  "path": "com.apple.CoreML/weights"
9
  },
10
+ "A8DE7FB1-A358-400B-BD8D-2BEE9CA2BB29": {
11
  "author": "com.apple.CoreML",
12
  "description": "CoreML Model Specification",
13
  "name": "model.mlmodel",
14
  "path": "com.apple.CoreML/model.mlmodel"
15
  }
16
  },
17
+ "rootModelIdentifier": "A8DE7FB1-A358-400B-BD8D-2BEE9CA2BB29"
18
  }
manifest.json CHANGED
@@ -1,18 +1,18 @@
1
  [
2
  {
3
  "path": "SeedVR2Decoder.mlpackage/Data/com.apple.CoreML/model.mlmodel",
4
- "size": 230791,
5
- "sha256": "957ab0f87506a39e8e4af168c45bc25e8132718eae672ef2d1ef9ca49e3c4c58"
6
  },
7
  {
8
  "path": "SeedVR2Decoder.mlpackage/Data/com.apple.CoreML/weights/weight.bin",
9
- "size": 301216640,
10
- "sha256": "5f83d24b0a1fc47b1195c78b6bafa7c0fec6881cd1028d76ae1d7ded2d02f226"
11
  },
12
  {
13
  "path": "SeedVR2Decoder.mlpackage/Manifest.json",
14
  "size": 617,
15
- "sha256": "159e690a1157ec89df72e77f81e6dc8a10efd95389df76c0193769395ec32849"
16
  },
17
  {
18
  "path": "SeedVR2DiT0.mlpackage/Data/com.apple.CoreML/model.mlmodel",
 
1
  [
2
  {
3
  "path": "SeedVR2Decoder.mlpackage/Data/com.apple.CoreML/model.mlmodel",
4
+ "size": 231878,
5
+ "sha256": "e3bafae7ad0ffbbc3afdefb0bbe694d567e38ba58628d58fb6b9e2a1a323f2c4"
6
  },
7
  {
8
  "path": "SeedVR2Decoder.mlpackage/Data/com.apple.CoreML/weights/weight.bin",
9
+ "size": 301389120,
10
+ "sha256": "7ec1636a919f746a46b2f4d2594929455df0af68aa5009d48ed2ee97c5908cad"
11
  },
12
  {
13
  "path": "SeedVR2Decoder.mlpackage/Manifest.json",
14
  "size": 617,
15
+ "sha256": "f8e781df2c8a93f60c7ae9f5bbe6396143b4aae6e1d8a97315c368814ac152e3"
16
  },
17
  {
18
  "path": "SeedVR2DiT0.mlpackage/Data/com.apple.CoreML/model.mlmodel",