peachsim 0.1.1692 → 0.1.1700

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/cli/bridge-flow-runner.ts +61 -39
  2. package/cli/commands/agent-wrapper.ts +21 -10
  3. package/cli/commands/control.ts +27 -7
  4. package/cli/commands/coverage.ts +1 -1
  5. package/cli/commands/detect.ts +17 -4
  6. package/cli/commands/maestro-compare.ts +25 -99
  7. package/cli/help-core.ts +3 -4
  8. package/cli/maestro-compare.ts +172 -4
  9. package/cli/outbound-endpoints.ts +2 -10
  10. package/detox/index.ts +15 -0
  11. package/detox/launch-permissions.ts +169 -0
  12. package/dist-lib/agent-daemon-client.cjs +2 -2
  13. package/dist-lib/agent-events.cjs +2 -2
  14. package/dist-lib/attached-projects.cjs +2 -2
  15. package/dist-lib/auth/shared-session.cjs +2 -2
  16. package/dist-lib/backend-origin.cjs +2 -2
  17. package/dist-lib/beta.cjs +2 -2
  18. package/dist-lib/beta.mjs +1 -1
  19. package/dist-lib/bridge-constants.cjs +2 -2
  20. package/dist-lib/bridge-contract-input.cjs +2 -2
  21. package/dist-lib/bridge-contract-input.mjs +1 -1
  22. package/dist-lib/bridge-contract.cjs +2 -2
  23. package/dist-lib/bridge-contract.mjs +1 -1
  24. package/dist-lib/bundle-channel.cjs +18273 -15925
  25. package/dist-lib/capture-contract.cjs +2 -2
  26. package/dist-lib/capture-contract.mjs +1 -1
  27. package/dist-lib/cloud-contract.cjs +2 -2
  28. package/dist-lib/cloud-contract.mjs +1 -1
  29. package/dist-lib/cloud-test-args.cjs +2 -2
  30. package/dist-lib/cloud-test-args.mjs +1 -1
  31. package/dist-lib/cloud.cjs +2 -2
  32. package/dist-lib/cloud.mjs +1 -1
  33. package/dist-lib/config.cjs +2 -2
  34. package/dist-lib/config.mjs +1 -1
  35. package/dist-lib/detox/index.cjs +104 -3
  36. package/dist-lib/dev-bundle-resolution.cjs +208 -44
  37. package/dist-lib/expo-swift-bundle.cjs +2349 -1778
  38. package/dist-lib/expo-swift-metadata.cjs +2 -2
  39. package/dist-lib/expo-swift-metadata.mjs +1 -1
  40. package/dist-lib/home-paths.cjs +2 -2
  41. package/dist-lib/host/bridge-host.cjs +7885 -3334
  42. package/dist-lib/host/fetch-proxy-handler.cjs +331 -39
  43. package/dist-lib/host/fetch-proxy-overrides.cjs +2 -2
  44. package/dist-lib/host/fetch-proxy-overrides.mjs +1 -1
  45. package/dist-lib/host/replacement-module-handler.cjs +2 -2
  46. package/dist-lib/host/websocket-proxy.cjs +2 -2
  47. package/dist-lib/index.cjs +297 -77
  48. package/dist-lib/jump-to-source-babel.cjs +2 -2
  49. package/dist-lib/jump-to-source-native.cjs +2 -2
  50. package/dist-lib/kotlin-host.cjs +2 -2
  51. package/dist-lib/menu.cjs +2 -2
  52. package/dist-lib/menu.mjs +1 -1
  53. package/dist-lib/metro-fingerprint-registry.cjs +2 -2
  54. package/dist-lib/metro-fingerprint-registry.mjs +1 -1
  55. package/dist-lib/metro-production-bundle.cjs +2 -2
  56. package/dist-lib/metro-production-bundle.mjs +1 -1
  57. package/dist-lib/metro.cjs +294 -74
  58. package/dist-lib/native-app-identity-project.cjs +48 -37
  59. package/dist-lib/native-app-identity-project.mjs +47 -36
  60. package/dist-lib/native-app-identity.cjs +2 -2
  61. package/dist-lib/native-app-identity.mjs +1 -1
  62. package/dist-lib/native-firebase-metadata.cjs +2 -2
  63. package/dist-lib/native-firebase-metadata.mjs +1 -1
  64. package/dist-lib/plist.cjs +2 -2
  65. package/dist-lib/plist.mjs +1 -1
  66. package/dist-lib/profiles.cjs +2 -2
  67. package/dist-lib/public-brand.cjs +2 -2
  68. package/dist-lib/public-origins-lib-esm.json +24 -23
  69. package/dist-lib/public-origins-lib.json +199 -79
  70. package/dist-lib/react-native-host-modules.cjs +2 -2
  71. package/dist-lib/react-native-host-modules.mjs +1 -1
  72. package/dist-lib/reap-browser.cjs +2 -2
  73. package/dist-lib/scripts/dev-server-scanner.cjs +206 -31
  74. package/dist-lib/sdk.cjs +2 -2
  75. package/dist-lib/sdk.mjs +1 -1
  76. package/dist-lib/skills.cjs +19600 -15992
  77. package/dist-lib/swift-compile.cjs +640 -402
  78. package/dist-lib/swift-compile.mjs +595 -357
  79. package/dist-lib/swift-package.cjs +24 -20
  80. package/dist-lib/swift-package.mjs +23 -19
  81. package/dist-lib/swift-submit.cjs +2 -2
  82. package/dist-lib/swift-submit.mjs +1 -1
  83. package/dist-lib/swift-toolchain.cjs +31 -4
  84. package/dist-lib/swift.cjs +2 -2
  85. package/dist-lib/vite-swift.cjs +1559 -1208
  86. package/dist-lib/vite.cjs +251 -42
  87. package/dist-lib/worklet-executable.cjs +2 -2
  88. package/dist-lib/worklet-executable.mjs +1 -1
  89. package/dist-types/packages/peach-engine/src/engine/font-config.d.ts +1 -1
  90. package/dist-types/packages/peach-engine/src/engine/node.d.ts +1 -0
  91. package/dist-types/packages/peach-engine/src/engine/scroll-pan-contacts.d.ts +3 -1
  92. package/dist-types/packages/peach-engine/src/engine/scroll.d.ts +7 -0
  93. package/dist-types/packages/peach-engine/src/engine/types.d.ts +2 -1
  94. package/dist-types/packages/peach-engine/src/remote-stream/cloud-display.d.ts +2 -2
  95. package/dist-types/packages/peach-engine/src/render-worker/host-service-protocol.d.ts +3 -0
  96. package/dist-types/packages/peach-engine/src/render-worker/protocol.d.ts +1 -0
  97. package/dist-types/packages/peach-engine/src/render-worker/scroll-mirror.d.ts +17 -0
  98. package/dist-types/packages/peach-engine/src/render-worker/worker-input-runtime.d.ts +2 -1
  99. package/dist-types/packages/peach-engine/src/render-worker/worklet-runtime.d.ts +3 -0
  100. package/dist-types/packages/peach-globals/src/cameraBridgeContract.d.ts +1 -0
  101. package/dist-types/packages/peach-globals/src/replayBridgeContract.d.ts +1 -0
  102. package/dist-types/packages/peach-globals/src/shellHostBridgeContract.d.ts +3 -0
  103. package/dist-types/packages/peach-kit/src/materials/types.d.ts +1 -0
  104. package/package.json +1 -1
  105. package/runtime-companion.json +1 -1
  106. package/scripts/dev-server-scanner.ts +4 -0
  107. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/Alpha.h +2282 -0
  108. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/BasicImageTypes.h +55 -0
  109. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/Conversion.h +10551 -6
  110. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/Convolution.h +4236 -3
  111. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/Geometry.h +827 -8
  112. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/Histogram.h +1369 -0
  113. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/Morphology.h +1243 -0
  114. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/Transform.h +1747 -0
  115. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/vImage.h +291 -2
  116. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/vImage_CVUtilities.h +1283 -0
  117. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/vImage_Types.h +811 -91
  118. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers/vImage_Utilities.h +1342 -25
  119. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/BNNS/bnns.h +2738 -0
  120. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/BNNS/bnns_constants.h +1685 -0
  121. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/BNNS/bnns_graph.h +913 -0
  122. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/BNNS/bnns_structures.h +1917 -0
  123. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/LinearAlgebra.h +23 -0
  124. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/arithmetic.h +209 -0
  125. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/base.h +104 -0
  126. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/linear_systems.h +77 -0
  127. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/matrix.h +554 -0
  128. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/norms.h +58 -0
  129. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/object.h +186 -0
  130. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/splat.h +86 -0
  131. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/LinearAlgebra/vector.h +364 -0
  132. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Quadrature/Integration.h +195 -0
  133. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Quadrature/Quadrature.h +44 -0
  134. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Sparse/BLAS/BLAS.h +2330 -0
  135. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Sparse/BLAS/Types.h +123 -0
  136. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Sparse/Solve.h +8339 -0
  137. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Sparse/SolveImplementation.h +520 -0
  138. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Sparse/SolveImplementationTyped.h +1893 -0
  139. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/Sparse/Sparse.h +10 -0
  140. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/blas_new.h +1098 -0
  141. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/cblas.h +859 -0
  142. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/cblas_new.h +1134 -0
  143. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/clapack.h +10555 -0
  144. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/fortran_blas.h +695 -0
  145. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/lapack.h +38526 -0
  146. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/lapack_types.h +60 -0
  147. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/lapack_version.h +20 -0
  148. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/thread_api.h +50 -0
  149. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vBasicOps.h +1218 -0
  150. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vBigNum.h +1458 -0
  151. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vDSP.h +10377 -0
  152. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vDSP_translate.h +167 -0
  153. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vForce.h +1243 -0
  154. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vecLib.h +114 -0
  155. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vecLibTypes.h +146 -0
  156. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vectorOps.h +1106 -0
  157. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers/vfp.h +294 -0
  158. package/sdk/ios/System/Library/Frameworks/Accelerate.framework/Headers/Accelerate.h +25 -1
  159. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFArray.h +673 -0
  160. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFAvailability.h +232 -0
  161. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFBase.h +827 -0
  162. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFCalendar.h +121 -0
  163. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFCharacterSet.h +387 -0
  164. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFData.h +79 -0
  165. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFDate.h +122 -0
  166. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFDictionary.h +674 -0
  167. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFError.h +182 -0
  168. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFLocale.h +213 -0
  169. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFNotificationCenter.h +82 -0
  170. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFNumber.h +129 -0
  171. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFSet.h +491 -0
  172. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFString.h +883 -2
  173. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFTimeZone.h +98 -0
  174. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h +11 -229
  175. package/sdk/ios/System/Library/Frameworks/CoreGraphics.framework/Headers/CoreGraphics.h +2 -6
  176. package/sdk/ios/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h +2 -1
  177. package/sdk/ios/System/Library/Frameworks/QuartzCore.framework/Headers/QuartzCore.h +1 -0
  178. package/sdk/ios/System/Library/Frameworks/Security.framework/Headers/SecBase.h +906 -3
  179. package/sdk/ios/System/Library/Frameworks/Security.framework/Headers/SecCertificate.h +613 -0
  180. package/sdk/ios/System/Library/Frameworks/Security.framework/Headers/SecItem.h +1284 -29
  181. package/sdk/ios/System/Library/Frameworks/Security.framework/Headers/SecPolicy.h +441 -0
  182. package/sdk/ios/System/Library/Frameworks/Security.framework/Headers/SecTrust.h +845 -0
  183. package/sdk/ios/System/Library/Frameworks/Security.framework/Headers/Security.h +4 -0
  184. package/sdk/ios/usr/include/AvailabilityMacros.h +3985 -0
  185. package/sdk/ios/usr/include/copyfile.h +143 -0
  186. package/sdk/ios/usr/include/malloc/_malloc.h +143 -0
  187. package/sdk/ios/usr/include/malloc/_malloc_type.h +153 -0
  188. package/sdk/ios/usr/include/malloc/_platform.h +80 -0
  189. package/sdk/ios/usr/include/malloc/_ptrcheck.h +43 -0
  190. package/sdk/ios/usr/include/malloc/malloc.h +768 -0
  191. package/sdk/ios/usr/include/os/availability.h +168 -6
  192. package/sdk/ios/usr/include/os/base.h +351 -0
  193. package/sdk/ios/usr/include/os/clock.h +18 -0
  194. package/sdk/ios/usr/include/os/lock.h +232 -10
  195. package/sdk/ios/usr/include/os/log.h +521 -38
  196. package/sdk/ios/usr/include/os/object.h +320 -3
  197. package/sdk/ios/usr/include/os/trace.h +492 -0
  198. package/sdk/ios/usr/include/os/trace_base.h +167 -0
  199. package/sdk/ios/usr/include/os/workgroup.h +37 -0
  200. package/sdk/ios/usr/include/os/workgroup_base.h +91 -0
  201. package/sdk/ios/usr/include/os/workgroup_interval.h +156 -0
  202. package/sdk/ios/usr/include/os/workgroup_object.h +359 -0
  203. package/sdk/ios/usr/include/os/workgroup_parallel.h +76 -0
  204. package/sdk/ios/usr/include/sys/cdefs.h +38 -0
  205. package/sdk/ios/wasm/include/unistd.h +3 -0
  206. package/skills/contrast/SKILL.md +3 -0
  207. package/src/codex-client.ts +11 -6
  208. package/src/dev-bundle-resolution.ts +124 -48
  209. package/src/expo-native-workspace.ts +176 -0
  210. package/src/expo-swift-host.ts +161 -26
  211. package/src/expo-swift-ready.ts +14 -28
  212. package/src/expoSwiftContract.ts +4 -1
  213. package/src/generated/swift-c-predefines.json +68 -12
  214. package/src/host/bridge-host.ts +5 -0
  215. package/src/installed-package-versions.ts +14 -1
  216. package/src/metro-project-root.ts +44 -33
  217. package/src/native-app-identity-project.ts +1 -0
  218. package/src/nitro-adapter.lock.json +916 -10
  219. package/src/nitro-adapter.ts +33 -24
  220. package/src/nitro-bundle.ts +108 -22
  221. package/src/nitro-compile.ts +32 -5
  222. package/src/nitro-source.ts +13 -3
  223. package/src/nitroAdapterContract.ts +100 -20
  224. package/src/node-http-fetch.ts +116 -0
  225. package/src/packageVersionsContract.ts +46 -0
  226. package/src/serverDiscoveryContract.ts +1 -0
  227. package/src/settingsContract.ts +0 -30
  228. package/src/swift-binary-modules.ts +20 -5
  229. package/src/swift-compile.ts +168 -48
  230. package/src/swift-coverage/xcode-assets.ts +4 -3
  231. package/src/swift-host.ts +14 -3
  232. package/src/swift-objc-compile.ts +32 -5
  233. package/src/swift-objc-metadata.ts +2 -2
  234. package/src/swift-objc-platform.ts +2 -2
  235. package/src/swift-objc-standin.ts +43 -13
  236. package/src/swift-package.ts +33 -26
  237. package/src/swift-source-pods.ts +126 -41
  238. package/src/swift-standin-digest.ts +77 -0
  239. package/src/swift-standin-emit.ts +40 -4
  240. package/src/swift-standins.ts +68 -19
  241. package/src/swift-toolchain.ts +38 -0
  242. package/src/swiftContract.ts +8 -0
  243. package/src/vite-plugin.ts +0 -10
  244. package/src/xcode-project.ts +62 -42
  245. package/src/settings-flags.ts +0 -107
@@ -0,0 +1,1917 @@
1
+ //
2
+ // bnns_structures.h
3
+ // BasicNeuralNetworkSubroutines
4
+ //
5
+ // Copyright © 2017 Apple Inc. All rights reserved.
6
+ //
7
+
8
+ #ifndef __BNNS_STRUCTURES_HEADER__
9
+ #define __BNNS_STRUCTURES_HEADER__
10
+
11
+ #include <stddef.h>
12
+ #include <stdint.h>
13
+ #include <stdbool.h>
14
+ #include <sys/types.h>
15
+ #include "bnns_constants.h"
16
+
17
+ #if __has_include( <Availability.h> )
18
+ # include <Availability.h>
19
+ #else
20
+ # define __API_AVAILABLE(...)
21
+ # define __API_DEPRECATED_WITH_REPLACEMENT(...)
22
+ # define __API_DEPRECATED(...)
23
+ #endif
24
+
25
+ #if __has_feature(assume_nonnull)
26
+ _Pragma("clang assume_nonnull begin")
27
+ #else
28
+ # define _Null_unspecified
29
+ # define _Nullable
30
+ # define _Nonnull
31
+ #endif
32
+
33
+ #pragma mark - User-provided memory functions
34
+
35
+ /*!
36
+
37
+ @abstract Type for user-provided memory allocation function
38
+
39
+ @discussion Should conform to posix_memalign(), and must be compatible with the memory deallocation function.
40
+
41
+ @param memptr Pointer to a <tt>(void *)</tt> receiving the address of the allocated memory.
42
+ @param alignment Requested alignment, must be a power of 2, and at least <tt>sizeof(void *)</tt>.
43
+ @param size Number of bytes to allocate.
44
+
45
+ @return 0 on success, or an error value on failure.
46
+
47
+ */
48
+ typedef int (*BNNSAlloc)(void * _Nullable * _Nullable memptr, size_t alignment, size_t size);
49
+
50
+ /*!
51
+
52
+ @abstract Type for user-provided memory deallocation function
53
+
54
+ @discussion Should conform to free(), and must be compatible with the memory allocation function.
55
+
56
+ @param ptr Address of the block to release.
57
+
58
+ */
59
+ typedef void (*BNNSFree)(void * _Null_unspecified ptr);
60
+
61
+ #pragma mark - Data formats
62
+
63
+ /*!
64
+
65
+ @abstract Common activation function parameters
66
+
67
+ @field function Activation function
68
+ @field alpha Parameter to the activation function
69
+ @field beta Parameter to the activation function
70
+
71
+ @field iscale Scale for integer functions (macOS 10.13, iOS 11, tvOS 11, watchOS 4)
72
+ @field ioffset Offset for integer functions (macOS 10.13, iOS 11, tvOS 11, watchOS 4)
73
+ @field ishift Shift for integer functions (macOS 10.13, iOS 11, tvOS 11, watchOS 4)
74
+
75
+ @field iscale_per_channel Scale per channel for integer functions (macOS 10.13, iOS 11, tvOS 11, watchOS 4)
76
+ @field ioffset_per_channel Offset per channel for integer functions (macOS 10.13, iOS 11, tvOS 11, watchOS 4)
77
+ @field ishift_per_channel Shift per channel for integer functions (macOS 10.13, iOS 11, tvOS 11, watchOS 4)
78
+
79
+ */
80
+ typedef struct {
81
+
82
+ BNNSActivationFunction function;
83
+ float alpha;
84
+ float beta;
85
+
86
+ // The following fields are available in macOS 10.13, iOS 11, tvOS 11, watchOS 4
87
+
88
+ int32_t iscale;
89
+ int32_t ioffset;
90
+ int32_t ishift;
91
+
92
+ const int32_t * _Nullable iscale_per_channel;
93
+ const int32_t * _Nullable ioffset_per_channel;
94
+ const int32_t * _Nullable ishift_per_channel;
95
+
96
+ } BNNSActivation;
97
+
98
+ #define BNNS_MAX_TENSOR_DIMENSION 8
99
+
100
+ /*!
101
+
102
+ @abstract Generic N-dimensional array
103
+
104
+ @discussion This type is used to represent an N-dimensional array of values (N=1,2,3,4).
105
+ The nature and dimension of the data is determined by the layout field.
106
+ Introduced in macOS 10.15, iOS 13, tvOS 13, watchOS 6.
107
+
108
+ @field flags is used to control some behaviors of the NDArray
109
+ @field layout defines the dimension (n) of the array, and how data is stored
110
+ @field size is the number of values in each dimension; only the first n values are used
111
+ @field stride is the increment (in values) between a value and the next in each dimension; only the first n values are used
112
+ A stride value of 0 is interpreted to mean that values are contiguous in this axis (i.e. is treated as stride[i] = size[i-1]*stride[i-1],
113
+ or stride[0] = 1 for the first dimension).
114
+
115
+ @field data points to the data; can be NULL
116
+ @field data_type defines the size and type of the values stored in data
117
+
118
+ @field table_data points to the lookup table; used only when data_type is Indexed<N>
119
+ @field table_data_type defines the size and type of the values stored in table_data; used only when data_type is Indexed<K>
120
+
121
+ @field data_scale is used in the conversion of integer values to floating point; used only when data_type is Int<K> or UInt<K>
122
+ @field data_bias is used in the conversion of integer values to floating point; used only when data_type is Int<K> or UInt<K>
123
+
124
+ */
125
+ typedef struct {
126
+
127
+ BNNSNDArrayFlags flags;
128
+ BNNSDataLayout layout;
129
+
130
+ size_t size[BNNS_MAX_TENSOR_DIMENSION];
131
+ size_t stride[BNNS_MAX_TENSOR_DIMENSION];
132
+
133
+ void * _Nullable data;
134
+ BNNSDataType data_type;
135
+
136
+ void * _Nullable table_data;
137
+ BNNSDataType table_data_type;
138
+
139
+ float data_scale; // 0.0f value will be set to 1.0f during computation
140
+ float data_bias;
141
+
142
+ } BNNSNDArrayDescriptor;
143
+
144
+ /*!
145
+
146
+ @abstract Generic Tensor
147
+
148
+ @discussion
149
+ This type presents a simpler interface than `BNNSNDArrayDescriptor` that avoids unnecessary complexity that
150
+ is not required for the BNNSGraph family of APIs.
151
+
152
+ @field `data_type` - type of data stored in this tensor
153
+ @field `rank` - rank of the tensor, must satisfy `0 <= rank <= BNNS_MAX_TENSOR_DIMENSION`
154
+ @field `shape` - the first `rank` elements give the shape of the tensor. Negative values indicate the dimension has
155
+ unspecified dynamic size.
156
+ @field `stride`- the first `rank` elements give the strides of each dimension. `stride[d]` corresponds to `shape[d]`.
157
+ Negative values indicate the stride is unspecified (normally due to the presence of a dynamic shape).
158
+ Unlike BNNSNDArrayDescriptor, a stride of 0 is not interpreted to mean contiguous, but is taken as a literal 0.
159
+ Unless otherwise stated, all BNNS routines expect data to be in first-major layout (`stride[d] >= stride[d+1]`).
160
+ @field `data` - pointer to memory containing the actual values of the tensor
161
+ @field `data_size_in_bytes` - the extent of `data`, used for bounds checking
162
+ @field `name` - optional name of this tensor. This field is not read by BNNS (but will be set to a pointer into the IR for
163
+ any BNNSTensors returned by BNNS) and is provided as a debugging aide only.
164
+ */
165
+ typedef struct {
166
+
167
+ BNNSDataType data_type;
168
+
169
+ uint8_t rank;
170
+ ssize_t shape[BNNS_MAX_TENSOR_DIMENSION];
171
+ ssize_t stride[BNNS_MAX_TENSOR_DIMENSION];
172
+
173
+ void * _Nullable data;
174
+ size_t data_size_in_bytes;
175
+
176
+ char const * _Nullable name;
177
+
178
+ } BNNSTensor;
179
+
180
+ #pragma mark - Sub Layers Parameters
181
+
182
+ /*!
183
+ @abstract LSTM Gate Layer Descriptor
184
+ @discussion
185
+ gate layer is a building block of LSTM layers
186
+ example of float output
187
+ in - 1D array of iw_desc.size[0] elements
188
+ hidden - 1D array of hw_desc.size[0] elements
189
+ cell - 1D array of cw_desc.size[0] elements
190
+ out - 1D array of iw_desc.size[1] elements
191
+ input_weights - 2D arrary of iw_desc.size[1] X iw_desc.size[0] elements (iw_desc.data points to data)
192
+ hidden_weights - 2D arrary of hw_desc.size[1] X hw_desc.size[0] elements (hw_desc.data points to data)
193
+ cell_weights - 2D arrary of cw_desc.size[1] X cw_desc.size[0] elements (cw_desc.data points to data)
194
+ for (size_t o = 0; o < iw_desc.size[1]; o++)
195
+ {
196
+ float res = bias[o]; // init with bias value
197
+ for (size_t i = 0; i < iw_desc.size[0]; i++) // matrix vector multiply
198
+ res += input[i] * input_weights[o][i];
199
+ for (size_t i = 0; i < hw_desc.size[0]; i++) // matrix vector multiply
200
+ res += hidden[i] * hidden_weights[o][i];
201
+ for (size_t i = 0; i < cw_desc.size[0]; i++) // matrix vector multiply
202
+ res += cell[i] * cell_weights[o][i];
203
+ out[i] = activation.func(res); // apply activation function
204
+ }
205
+
206
+ - num_directions == 1 for unidirectional and 2 for bidirectional
207
+ @field iw_desc input weights descriptor , weights are ordered as [num_layers][num_directions][hidden_size][input_size] (C style multi array notation)
208
+ - iw_desc is duplicated for the case where num_layers > 1 && input_size != hidden_size
209
+ @field hw_desc hidden weights descriptor , weights are ordered as [num_layers][num_directions][hidden_size][hidden_size] (C style multi array notation)
210
+ @field cw_desc cell weights descriptor, weights are ordered as [num_layers][num_directions][hidden_size][hidden_size] (C style multi array notation)
211
+ @field b_desc bias descriptor, bias is ordered as [num_layers][num_directions][hidden_size] (C style multi array notation)
212
+ @field activation activation function
213
+ */
214
+ typedef struct {
215
+ BNNSNDArrayDescriptor iw_desc[2];
216
+ BNNSNDArrayDescriptor hw_desc;
217
+ BNNSNDArrayDescriptor cw_desc;
218
+ BNNSNDArrayDescriptor b_desc;
219
+ BNNSActivation activation;
220
+ } BNNSLSTMGateDescriptor
221
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
222
+
223
+ /*!
224
+ @abstract contains NDarrat descriptors for inputs and outputs of LSTM
225
+ @field data_desc - input / output
226
+ @field hidden_desc - hidden input / output
227
+ @field cell_state_desc - cell state input / output
228
+ */
229
+ typedef struct {
230
+ BNNSNDArrayDescriptor data_desc;
231
+ BNNSNDArrayDescriptor hidden_desc;
232
+ BNNSNDArrayDescriptor cell_state_desc;
233
+ } BNNSLSTMDataDescriptor
234
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
235
+
236
+ /*!
237
+ @abstract Arithmetic struct that hold 1 input and an output descriptor
238
+ @discussion
239
+ @field in input descriptor
240
+ @field in_type input descriptor type
241
+ @field out output descriptor
242
+ @field out_type output descriptor type
243
+
244
+ */
245
+ typedef struct {
246
+ BNNSNDArrayDescriptor in;
247
+ BNNSDescriptorType in_type;
248
+ BNNSNDArrayDescriptor out;
249
+ BNNSDescriptorType out_type;
250
+ } BNNSArithmeticUnary
251
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
252
+
253
+ /*!
254
+ @abstract Arithmetic struct that holds 2 input descriptors and an output descriptor
255
+ @discussion
256
+ @field in1 input1 descriptor
257
+ @field in1_type input1 descriptor type
258
+ @field in2 descriptor
259
+ @field in2_type input2 descriptor type
260
+ @field out output descriptor
261
+ @field out_type output descriptor type
262
+
263
+ */
264
+ typedef struct {
265
+ BNNSNDArrayDescriptor in1;
266
+ BNNSDescriptorType in1_type;
267
+ BNNSNDArrayDescriptor in2;
268
+ BNNSDescriptorType in2_type;
269
+ BNNSNDArrayDescriptor out;
270
+ BNNSDescriptorType out_type;
271
+ } BNNSArithmeticBinary
272
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
273
+
274
+ /*!
275
+ @abstract Arithmetic struct that holds 3 input descriptors and an output descriptor
276
+ @discussion
277
+ @field in1 input1 descriptor
278
+ @field in1_type input1 descriptor type
279
+ @field in2 descriptor
280
+ @field in2_type input2 descriptor type
281
+ @field in3 descriptor
282
+ @field in3_type input3 descriptor type
283
+ @field out output descriptor
284
+ @field out_type output descriptor type
285
+
286
+ */
287
+ typedef struct {
288
+ BNNSNDArrayDescriptor in1;
289
+ BNNSDescriptorType in1_type;
290
+ BNNSNDArrayDescriptor in2;
291
+ BNNSDescriptorType in2_type;
292
+ BNNSNDArrayDescriptor in3;
293
+ BNNSDescriptorType in3_type;
294
+ BNNSNDArrayDescriptor out;
295
+ BNNSDescriptorType out_type;
296
+ } BNNSArithmeticTernary
297
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
298
+
299
+ /*! @abstract Multihead Attention Projection Parameters
300
+ *
301
+ * @description
302
+ * Each of the Query, Key and Value inputs and the output get their own version of this struct.
303
+ * In the backpropagation call, the _delta output reuses this structure to hold partial differentials.
304
+ * See BNNSLayerParametersMultiheadAttention for a full description of the layer.
305
+ *
306
+ * @seealso BNNSLayerParametersMultiheadAttention
307
+ *
308
+ * @field target_desc Descriptor of the main target of the operation - either an input (i.e. query, key or value) or an output (output)
309
+ * @field weights The weights to be used by the initial projection (i.e. Wᴷ)
310
+ * @field bias The bias to be used by the initial projection (i.e. pᴷ). If no bias is required, set bias.data = NULL.
311
+ */
312
+ typedef struct {
313
+ BNNSNDArrayDescriptor target_desc;
314
+ BNNSNDArrayDescriptor weights;
315
+ BNNSNDArrayDescriptor bias;
316
+ } BNNSMHAProjectionParameters;
317
+
318
+ #pragma mark - Layer parameters
319
+
320
+ /*!
321
+
322
+ @abstract Convolution parameters
323
+
324
+ @discussion
325
+ The convolution product Output = Weights &times; Input is defined as follows. Pixel <tt>Output(o,x,y)</tt> of the output image stack receives:
326
+ <br><tt>Output(o,x,y) = &sum;<sub>i,kx,ky</sub> Weight(o,i,kx,ky) * Input(i,x_stride * x + kx*x_dilation_stride,y_stride * y + ky*y_dilation_stride)</tt> with
327
+ <tt>kx=0..k_width-1</tt>, <tt>ky=0..k_height-1</tt>,
328
+ <tt>i=0..in_channels-1</tt>, <tt>o=0..out_channels-1</tt>,
329
+ <tt>x=0..out_width-1</tt>, <tt>y=0..out_height-1</tt>.
330
+
331
+ After the convolution is applied, the output is updated with bias and/or activation function as follows:
332
+ <br><tt>Output(o,x,y) = ActivationFunction( Bias(o) + Output(o,x,y) )</tt>.
333
+
334
+ Dimensions must satisfy:
335
+ <br><tt>in_width + 2 * x_padding >= x_stride * ( out_width - 1 ) + (k_width + (k_width-1)*(x_dilation_stride-1))</tt>, and <tt>in_height + 2 * y_padding >= y_stride * ( out_height - 1 ) + (k_height + (k_height-1)*(y_dilation_stride-1))</tt>.
336
+ <br>A common use case is <tt>x_stride=y_stride=1</tt>, and <tt>x_padding=y_padding=0</tt>. In that case, <tt>in_width >= out_width + k_width - 1</tt>, and <tt>in_height >= out_height + k_height - 1</tt>.
337
+
338
+ Padding is a border of 0 values virtually added to the input image. if asymmetric padding is used, replace x_padding with pad[0]+pad[1] and y_padding with pad[2]+pad[3] in the input shape requirement formula above.
339
+
340
+ Coefficient <tt>Weight(o,i,kx,ky)</tt> for output image <tt>o=0..out_channels-1</tt>, input image <tt>i=0..in_channels-1</tt>, and kernel point (kx,ky) is
341
+ stored in <tt>weights[kx + k_width * (ky + k_height * (i + in_channels * o))]</tt>, where
342
+ the convolution kernel dimensions are <tt>k_width,k_height</tt>.
343
+
344
+ @field bias Layer bias, <tt>out_channels</tt> values, one for each output channel
345
+ @field activation Layer activation function
346
+ @field weights_layout option to choose a different layout in memory for the weights Matrix
347
+ @field x_stride width increment in the input image
348
+ @field y_stride height increment in the input image
349
+ @field x_dilation_stride width increment in the input image while convolving with the kernel. ignored if x_dilation_stride<=1.
350
+ @field y_dilation_stride height increment in the input image while convolving with the kernel. ignored if y_dilation_stride<=1.
351
+ @field x_padding width padding, virtual 0 values added to the left and right of each channel of the input stack
352
+ @field y_padding height padding, virtual 0 values added to the top and bottom of each channel of the input stack
353
+ @field groups convolution group size, ignored if groups <= 1.
354
+ - if groups > 1:
355
+ - input, output, weights and bias will be used for a set of convolutions.
356
+ - input and output channels must be divisible by groups.
357
+ - weight layout must be BNNSDataLayoutConvolutionWeightsOIHW or BNNSDataLayoutConvolutionWeightsOIHrWr
358
+ - weights descriptor size[2] = in_channels/groups and weights descriptor size[3]=output channels, such that for each consecutive convolutions i and i+1 in the group, convolution i weights and convolution i+1 weights are separated by total_weights_size/groups elements
359
+ - when calling apply forward and backward functions, in_stride, out_stride and gradient strides should be the strides for the entire convolution group.
360
+ - transposed convolution gradient computation is not supported when groups>1
361
+ @field pad left, right, up, down zero padding. used for asymmetric padding.
362
+ - ignored if pad is all 0, or if x_padding>0 or y_padding>0.
363
+ - left,up are adjucent to image position 0,0.
364
+ */
365
+ typedef struct {
366
+
367
+ BNNSNDArrayDescriptor i_desc;
368
+ BNNSNDArrayDescriptor w_desc;
369
+ BNNSNDArrayDescriptor o_desc;
370
+
371
+ BNNSNDArrayDescriptor bias;
372
+ BNNSActivation activation;
373
+
374
+ size_t x_stride; // stride 0 will be set 1 during computation
375
+ size_t y_stride; // stride 0 will be set 1 during computation
376
+ size_t x_dilation_stride;
377
+ size_t y_dilation_stride;
378
+ size_t x_padding;
379
+ size_t y_padding;
380
+ size_t groups;
381
+ size_t pad[4];
382
+ } BNNSLayerParametersConvolution
383
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
384
+
385
+ /*!
386
+
387
+ @abstract Fully connected layer parameters
388
+
389
+ @discussion
390
+ The output of a fully connected layer is the result of a matrix-vector product.
391
+ The output vector is defined by <tt>Output(o) = &sum;<sub>i</sub> Weight(o,i) * Input(i)</tt> for <tt>i=0..in_size-1</tt>, <tt>o=0..out_size-1</tt>.
392
+
393
+ Coefficient <tt>Weight(o,i)</tt> is stored in <tt>weights[i + o * in_size]</tt>.
394
+
395
+ After the matrix product, the output is updated with bias and/or activation function as follows:
396
+ <br><tt>Output(o) = ActivationFunction( Bias(o) + Output(o) )</tt>.
397
+
398
+ @field i_desc descriptor of input vector
399
+ @field w_desc weights Matrix coefficients, <tt>in_size * out_size</tt> values, with the layout described in the discussion
400
+ @field o_desc descriptor of output vector
401
+
402
+ @field bias Layer bias, <tt>out_size</tt> values, one for each output component
403
+ @field activation Layer activation function
404
+
405
+ */
406
+ typedef struct {
407
+
408
+ BNNSNDArrayDescriptor i_desc;
409
+ BNNSNDArrayDescriptor w_desc;
410
+ BNNSNDArrayDescriptor o_desc;
411
+
412
+ BNNSNDArrayDescriptor bias;
413
+ BNNSActivation activation;
414
+
415
+ } BNNSLayerParametersFullyConnected
416
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
417
+
418
+ /*!
419
+
420
+ @abstract Pooling layer parameters
421
+
422
+ @discussion
423
+ The pooling is defined as follows. Pixel <tt>Output(o,x,y)</tt> of the output image stack receives:
424
+ <br><tt>Output(o,x,y) = PoolingFunction( Input(o,x_stride * x + kx * x_dilation_stride, y_stride * y + ky * y_dilation_stride) )</tt> with <tt>kx=0..k_width-1</tt>, <tt>ky=0..k_height-1</tt>,
425
+ <tt>o=0..out_channels-1</tt>, <tt>x=0..out_width-1</tt>, <tt>y=0..out_height-1</tt>.
426
+
427
+ After the pooling is applied, the output is updated with bias and/or activation function as follows:
428
+ <br><tt>Output(o,x,y) = ActivationFunction( Bias(o) + Output(o,x,y) )</tt>.
429
+
430
+ Dimensions must satisfy:
431
+ <br><tt>in_width + 2 * x_padding >= x_stride * (out_width - 1) + 1</tt>,
432
+ <br><tt>iin_height + 2 * y_padding >= p->y_stride * (o->height - 1) + 1</tt>.
433
+
434
+ Padding is a border of 0 values virtually added to the input image.
435
+
436
+ @field bias Layer bias, <tt>out_channels</tt> values
437
+ @field activation Layer activation function
438
+
439
+ @field pooling_function Selects the pooling function to apply to each sample
440
+
441
+ @field x_stride width increment in the input image
442
+ @field y_stride height increment in the input image
443
+ @field x_dilation_stride width increment in the input image while convolving with the kernel
444
+ @field y_dilation_stride height increment in the input image while convolving with the kernel
445
+ @field x_padding width padding, virtual 0 values added to the left and right of each channel of the input stack
446
+ @field y_padding height padding, virtual 0 values added to the top and bottom of each channel of the input stack
447
+ @field pad left, right, up, down padding. used for asymmetric padding
448
+ - ignored if pad is all 0, or if x_padding>0 or y_padding>0.
449
+ - left,up are adjucent to image position 0,0.
450
+ */
451
+ typedef struct {
452
+
453
+ BNNSNDArrayDescriptor i_desc;
454
+ BNNSNDArrayDescriptor o_desc;
455
+
456
+ BNNSNDArrayDescriptor bias;
457
+ BNNSActivation activation;
458
+
459
+ BNNSPoolingFunction pooling_function;
460
+
461
+ size_t k_width;
462
+ size_t k_height;
463
+ size_t x_stride; // stride 0 will be set 1 during computation
464
+ size_t y_stride; // stride 0 will be set 1 during computation
465
+ size_t x_dilation_stride;
466
+ size_t y_dilation_stride;
467
+ size_t x_padding;
468
+ size_t y_padding;
469
+ size_t pad[4];
470
+ } BNNSLayerParametersPooling
471
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
472
+
473
+ /*!
474
+
475
+ @abstract Activation/Conversion layer parameters
476
+
477
+ @discussion
478
+ The output of a activation layer is the result of applying activation operation on input data
479
+ Also supports conversion of data types when activation.function == BNNSActivationFunctionIdentity
480
+ and i_desc.data_type != o_desc.data_type
481
+
482
+ @field i_desc descriptor of input
483
+ @field o_desc descriptor of output
484
+ @field activation Layer activation function
485
+ @field axis_flags Flags indicating axes on which to conduct certain activation functions applications (e.g. softmax).
486
+ A value of 0 is interpreted to mean the axis corresponding to i_desc.size[0].
487
+ When used in batch processing, this flag can also specify the batch dimension at the bit one higher than the last dimension of BNNSDataLayout.
488
+ For example, axis_flags = 2 specifies the batch dimension for BNNSDataLayoutVector.
489
+ axis = 8 specifies the batch dimension for BNNSDataLayoutImageCHW.
490
+
491
+ */
492
+ typedef struct {
493
+
494
+ BNNSNDArrayDescriptor i_desc;
495
+ BNNSNDArrayDescriptor o_desc;
496
+
497
+ BNNSActivation activation;
498
+ uint32_t axis_flags;
499
+
500
+ } BNNSLayerParametersActivation
501
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
502
+
503
+ /*!
504
+ @abstract Loss layer parameters (basic version)
505
+ @discussion
506
+ If loss function is SoftmaxCrossEntropy, use BNNSLayerParametersLossSoftmaxCrossEntropy instead of this structure.
507
+ If loss function is SigmoidCrossEntropy, use BNNSLayerParametersLossSigmoidCrossEntropy instead of this structure.
508
+ If loss function is Huber, use BNNSLayerParametersLossHuber instead of this structure.
509
+ If loss function is Yolo, use BNNSLayerParametersLossYolo instead of this structure.
510
+
511
+ compute loss according to loss function and its parameters and reduce loss according to reduction
512
+
513
+ @field i_desc descriptor of input
514
+ @field o_desc descriptor of output
515
+ @field function Loss function
516
+ If SoftmaxCrossEntropy, SigmoidCrossEntropy, Huber or Yolo loss is required, use the specific structures for those loss functions.
517
+ @field reduction loss reduction function to be used.
518
+ */
519
+ typedef struct {
520
+ BNNSLossFunction function;
521
+ BNNSNDArrayDescriptor i_desc;
522
+ BNNSNDArrayDescriptor o_desc;
523
+ BNNSLossReductionFunction reduction;
524
+ } BNNSLayerParametersLossBase
525
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
526
+
527
+ /*!
528
+ @abstract Loss layer parameters (Softmax Cross Entropy version)
529
+ @discussion
530
+ compute loss according to loss function and its parameters and reduce loss according to reduction
531
+
532
+ @field i_desc descriptor of input
533
+ @field o_desc descriptor of output
534
+ @field function Loss function
535
+ @field reduction loss reduction function to be used.
536
+ @field label_smooth smooth n labels. smoothing may not be supported for all loss functions.
537
+ smoothed_labels[i] = labels[i]*(1-label_smooth) + label_smooth/n
538
+ */
539
+ typedef struct {
540
+ // Fields layout compatible with BNNSLayerParametersLoseBase
541
+ BNNSLossFunction function;
542
+ BNNSNDArrayDescriptor i_desc;
543
+ BNNSNDArrayDescriptor o_desc;
544
+ BNNSLossReductionFunction reduction;
545
+
546
+ // Fields specific to Softmax Cross Entropy
547
+ float label_smooth;
548
+ } BNNSLayerParametersLossSoftmaxCrossEntropy
549
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
550
+
551
+ /*!
552
+ @abstract Loss layer parameters (Sigmoid Cross Entropy version)
553
+ @discussion
554
+ compute loss according to loss function and its parameters and reduce loss according to reduction
555
+
556
+ @field i_desc descriptor of input
557
+ @field o_desc descriptor of output
558
+ @field function Loss function
559
+ @field reduction loss reduction function to be used.
560
+ @field label_smooth smooth n labels. smoothing may not be supported for all loss functions.
561
+ smoothed_labels[i] = labels[i]*(1-label_smooth) + label_smooth/2
562
+ */
563
+ typedef struct {
564
+ // Fields layout compatible with BNNSLayerParametersLoseBase
565
+ BNNSLossFunction function;
566
+ BNNSNDArrayDescriptor i_desc;
567
+ BNNSNDArrayDescriptor o_desc;
568
+ BNNSLossReductionFunction reduction;
569
+
570
+ // Fields specific to Softmax Cross Entropy
571
+ float label_smooth;
572
+ } BNNSLayerParametersLossSigmoidCrossEntropy
573
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
574
+
575
+ /*!
576
+ @abstract Loss layer parameters (Huber version)
577
+ @discussion
578
+ compute loss according to loss function and its parameters and reduce loss according to reduction
579
+
580
+ @field i_desc descriptor of input
581
+ @field o_desc descriptor of output
582
+ @field function Loss function
583
+ @field reduction loss reduction function to be used.
584
+ @field huber_delta huber delta
585
+ */
586
+ typedef struct {
587
+ // Fields layout compatible with BNNSLayerParametersLoseBase
588
+ BNNSLossFunction function;
589
+ BNNSNDArrayDescriptor i_desc;
590
+ BNNSNDArrayDescriptor o_desc;
591
+ BNNSLossReductionFunction reduction;
592
+
593
+ // Fields specific to Huber Loss
594
+ float huber_delta;
595
+ } BNNSLayerParametersLossHuber
596
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
597
+
598
+ /*!
599
+ @abstract Loss layer parameters (YoLo version)
600
+ @discussion
601
+ compute loss according to loss function and its parameters and reduce loss according to reduction
602
+ Yolo loss input and ground truth labels data layout expected to be (batch size)x(grid height)x(grid width)x(anchors)x(x,y,w,h,confidence,classes)
603
+
604
+ @field i_desc descriptor of input
605
+ @field o_desc descriptor of output
606
+ @field function Loss function
607
+ @field reduction loss reduction function to be used.
608
+ - reduction must be BNNSLossReductionSum for loss function BNNSLossFunctionYolo
609
+ @field huber_delta huber delta
610
+ - ignored if loss function is not huber or yolo
611
+ - for yolo loss, huber is used for w,h loss. yolo loss huber delta is assumed to be 1 if set to 0
612
+ @field number_of_grid_columns number of columns in grid
613
+ @field number_of_grid_rows number of rows in grid
614
+ @field number_of_anchor_boxes number of anchor boxes in each cell
615
+ @field anchor_box_size anchor box size, should be 5+number of classes
616
+ @field rescore rescore confidence according to prediction vs. ground truth Intersection Over Union (IOU)
617
+ @field scale_xy x,y loss scaling factor
618
+ @field scale_wh w,h loss scaling factor
619
+ @field scale_object confidence (object) loss scaling factor
620
+ @field scale_no_object confidence (no object) scaling factor
621
+ @field scale_classification classification scaling factor
622
+ @field object_minimum_iou minimum iou for treating as object
623
+ @field object_minimum_iou maximum iou for treating as no object
624
+ @field anchors_data anchors data
625
+ -must be consecutive float array: w0,h0,w1,h1, ... w_n,h_n, where n is number_of_anchor_boxes
626
+ */
627
+ typedef struct {
628
+ // Fields layout compatible with BNNSLayerParametersLoseBase
629
+ BNNSLossFunction function;
630
+ BNNSNDArrayDescriptor i_desc;
631
+ BNNSNDArrayDescriptor o_desc;
632
+ BNNSLossReductionFunction reduction;
633
+
634
+ // Fields specific to YoLo Loss
635
+ float huber_delta;
636
+ size_t number_of_grid_columns;
637
+ size_t number_of_grid_rows;
638
+ size_t number_of_anchor_boxes;
639
+ size_t anchor_box_size;
640
+ bool rescore;
641
+ float scale_xy;
642
+ float scale_wh;
643
+ float scale_object;
644
+ float scale_no_object;
645
+ float scale_classification;
646
+ float object_minimum_iou;
647
+ float no_object_maximum_iou;
648
+ float* anchors_data;
649
+ } BNNSLayerParametersLossYolo
650
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
651
+
652
+ /*!
653
+ @abstract SGD with Momentum Optimizer Fields
654
+ @discussion
655
+ BNNSOptimizerSGDMomentumFields structure should be pointed to by OptimizerAlgFields when using an Optimizer filter with optimization function BNNSOptimizerFunctionSGDMomentum.
656
+ The structure will not be cached internally and must be valid when calling the optimizer apply, therefore, values such as learning_rate can be modified between optimizer filter apply calls.
657
+
658
+ The error function E = L + R(W) where L is the loss function and R(W) is the regularization term.
659
+ The gradient g is dE/dW = dL/dW + dR(W)/dW
660
+ dL/dW could be clipped using clip_gradients and scaled using gradient_scale
661
+ dR(W)/dW could be scaled using regularization_scale
662
+
663
+ The SGD with momentum optimizer will calculate the parameter W at time t+1 using accumulator V, gradient g, learning rate lr and momentum m according to sgd_momentum_variant.
664
+
665
+ @field learning_rate learning rate
666
+ @field momentum momentum. setting momentum to 0 will result in vanilla sgd, the accumulation term V will not be computed and associated scratch buffer can be NULL
667
+ @field gradient_scale gradient scaling factor
668
+ @field regularization_scale regularization scaling factor
669
+ @field clip_gradients clip gradient between min and max values
670
+ @field clip_gradients_min minimum gradient values. ignored if clip_gradients is false
671
+ @field clip_gradients_max maximum gradient values. ignored if clip_gradients is false
672
+ @field nesterov nesterov momentum update
673
+ @field regularization_func regularization function
674
+ @field sgd_momentum_variant sgd momentum variant
675
+
676
+ */
677
+ typedef struct {
678
+ float learning_rate;
679
+ float momentum;
680
+ float gradient_scale;
681
+ float regularization_scale;
682
+ bool clip_gradients;
683
+ float clip_gradients_min;
684
+ float clip_gradients_max;
685
+ bool nesterov;
686
+ BNNSOptimizerRegularizationFunction regularization_func;
687
+ BNNSOptimizerSGDMomentumVariant sgd_momentum_variant;
688
+ } BNNSOptimizerSGDMomentumFields
689
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
690
+
691
+ /*!
692
+ @abstract SGD with Momentum and gradient clipping Optimizer Fields
693
+ @discussion
694
+ BNNSOptimizerSGDMomentumFields structure should be pointed to by OptimizerAlgFields when using an Optimizer filter with optimization function BNNSOptimizerFunctionSGDMomentum.
695
+ The structure will not be cached internally and must be valid when calling the optimizer apply, therefore, values such as learning_rate can be modified between optimizer filter apply calls.
696
+
697
+ The error function E = L + R(W) where L is the loss function and R(W) is the regularization term.
698
+ The gradient g is dE/dW = dL/dW + dR(W)/dW
699
+ dL/dW could be clipped using clipping_func and scaled using gradient_scale
700
+ dR(W)/dW could be scaled using regularization_scale
701
+
702
+ The SGD with momentum optimizer will calculate the parameter W at time t+1 using accumulator V, gradient g, learning rate lr and momentum m according to sgd_momentum_variant.
703
+
704
+ @field learning_rate learning rate
705
+ @field momentum momentum. setting momentum to 0 will result in vanilla sgd, the accumulation term V will not be computed and associated scratch buffer can be NULL
706
+ @field gradient_scale gradient scaling factor
707
+ @field regularization_scale regularization scaling factor
708
+ @field nesterov nesterov momentum update
709
+ @field regularization_func regularization function
710
+ @field sgd_momentum_variant sgd momentum variant
711
+ @field clipping_func clipping function
712
+ @field clip_gradients_min minimum gradient values. Used by BNNSOptimizerClippingByValue.
713
+ @field clip_gradients_max maximum gradient values. Used by BNNSOptimizerClippingByValue.
714
+ @field clip_gradients_max_norm max L2-norm. Used by BNNSOptimizerClippingByNorm and BNNSOptimizerClippingByGlobalNorm.
715
+ @field clip_gradients_use_norm If you already know the global L2-norm, you can specify the global L2-norm to use by BNNSOptimizerClippingByGlobalNorm.
716
+ If zero, global L2-norm of gradient tensors is computed and then used.
717
+
718
+ */
719
+ typedef struct {
720
+ float learning_rate;
721
+ float momentum;
722
+ float gradient_scale;
723
+ float regularization_scale;
724
+ bool nesterov;
725
+ BNNSOptimizerRegularizationFunction regularization_func;
726
+ BNNSOptimizerSGDMomentumVariant sgd_momentum_variant;
727
+ BNNSOptimizerClippingFunction clipping_func;
728
+ float clip_gradients_min;
729
+ float clip_gradients_max;
730
+ float clip_gradients_max_norm;
731
+ float clip_gradients_use_norm;
732
+ } BNNSOptimizerSGDMomentumWithClippingFields
733
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
734
+
735
+ /*!
736
+ @abstract Adam and AdamW Fields
737
+ @discussion
738
+ BNNSOptimizerAdamFields structure should be pointed to by OptimizerAlgFields when using an Optimizer filter with one of the optimization functions BNNSOptimizerFunctionAdam, BNNSOptimizerFunctionAdamAMSGrad, BNNSOptimizerFunctionAdamW, or BNNSOptimizerFunctionAdamWAMSGrad.
739
+ The structure will not be cached internally and must be valid when calling the optimizer apply, therefore, values such as learning_rate can be modified between optimizer filter apply calls.
740
+
741
+ The error function E = L + R(W) where L is the loss function and R(W) is the regularization term.
742
+ The gradient g is dE/dW = dL/dW + dR(W)/dW
743
+ dL/dW could be clipped using clip_gradients and scaled using gradient_scale
744
+ For BNNSOptimizerFunctionAdam and BNNSOptimizerFunctionAdamAMSGrad dR(W)/dW could be scaled using regularization_scale (L_1/L_2 regularization).
745
+ For BNNSOptimizerFunctionAdamW and BNNSOptimizerFunctionAdamWAMSGrad, regularization_scale is instead used for the decoupled weight decay parameter
746
+
747
+ Adam Implementation according to ADAM: A METHOD FOR STOCHASTIC OPTIMIZATION by Diederik P. Kingma, Jimmy Lei Ba (https://arxiv.org/pdf/1412.6980.pdf)
748
+
749
+ AdamW Implementation is based on DECOUPLED WEIGHT DECAY REGULARIZATION by I. Loshchilov and F. Hutter
750
+ (https://arxiv.org/pdf/1711.05101.pdf)
751
+
752
+ The AMSGrad variants use the alternative versions of the update step described in
753
+ ON THE CONVERGENCE OF ADAM AND BEYOND by S. Reddi, S. Kale and S. Kumar
754
+ (https://arxiv.org/pdf/1904.09237)
755
+
756
+ @field learning_rate learning rate
757
+ @field beta1 first moment constant. must be in [0,1)
758
+ @field beta2 second moment constant. must be in [0,1)
759
+ @field time_step time step. time step is maintained by user since optimizer is called for multiple layers with same time step. initial value must be 1, increase by 1 after optimizing all the layer paramers in the network.
760
+ @field epsilon epsilon addition for the division in the parameter update stage, note that this is actually epsilon hat from section 2 of the
761
+ Adam paper, which is related to the paper's epsilon through the relation epsilon_hat = epsilon * sqrt(1 - beta2 ** time_step)
762
+ @field gradient_scale gradient scaling factor
763
+ @field regularization_scale regularization scaling factor / decoupled weight decay parameter
764
+ @field clip_gradients clip gradient between min and max values
765
+ @field clip_gradients_min minimum gradient values. ignored if clip_gradients is false
766
+ @field clip_gradients_max maximum gradient values. ignored if clip_gradients is false
767
+ @field regularization_func regularization function (only used by Adam, ignored by AdamW)
768
+ */
769
+ typedef struct {
770
+ float learning_rate;
771
+ float beta1;
772
+ float beta2;
773
+ float time_step;
774
+ float epsilon;
775
+ float gradient_scale;
776
+ float regularization_scale;
777
+ bool clip_gradients;
778
+ float clip_gradients_min;
779
+ float clip_gradients_max;
780
+ BNNSOptimizerRegularizationFunction regularization_func;
781
+ } BNNSOptimizerAdamFields
782
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
783
+
784
+ /*!
785
+ @abstract Adam and AdamW with gradient clipping Fields
786
+ @discussion
787
+ BNNSOptimizerAdamFields structure should be pointed to by OptimizerAlgFields when using an Optimizer filter with one of the optimization functions BNNSOptimizerFunctionAdam, BNNSOptimizerFunctionAdamAMSGrad, BNNSOptimizerFunctionAdamW, or BNNSOptimizerFunctionAdamWAMSGrad.
788
+ The structure will not be cached internally and must be valid when calling the optimizer apply, therefore, values such as learning_rate can be modified between optimizer filter apply calls.
789
+
790
+ The error function E = L + R(W) where L is the loss function and R(W) is the regularization term.
791
+ The gradient g is dE/dW = dL/dW + dR(W)/dW
792
+ dL/dW could be clipped using clipping_func and scaled using gradient_scale
793
+ For BNNSOptimizerFunctionAdam and BNNSOptimizerFunctionAdamAMSGrad dR(W)/dW could be scaled using regularization_scale (L_1/L_2 regularization).
794
+ For BNNSOptimizerFunctionAdamW and BNNSOptimizerFunctionAdamWAMSGrad, regularization_scale is instead used for the decoupled weight decay parameter
795
+
796
+ Adam Implementation according to ADAM: A METHOD FOR STOCHASTIC OPTIMIZATION by Diederik P. Kingma, Jimmy Lei Ba (https://arxiv.org/pdf/1412.6980.pdf)
797
+
798
+ AdamW Implementation is based on DECOUPLED WEIGHT DECAY REGULARIZATION by I. Loshchilov and F. Hutter
799
+ (https://arxiv.org/pdf/1711.05101.pdf)
800
+
801
+ The AMSGrad variants use the alternative versions of the update step described in
802
+ ON THE CONVERGENCE OF ADAM AND BEYOND by S. Reddi, S. Kale and S. Kumar
803
+ (https://arxiv.org/pdf/1904.09237)
804
+
805
+ @field learning_rate learning rate
806
+ @field beta1 first moment constant. must be in [0,1)
807
+ @field beta2 second moment constant. must be in [0,1)
808
+ @field time_step time step. time step is maintained by user since optimizer is called for multiple layers with same time step. initial value must be 1, increase by 1 after optimizing all the layer paramers in the network.
809
+ @field epsilon epsilon addition for the division in the parameter update stage, note that this is actually epsilon hat from section 2 of the
810
+ Adam paper, which is related to the paper's epsilon through the relation epsilon_hat = epsilon * sqrt(1 - beta2 ** time_step)
811
+ @field gradient_scale gradient scaling factor
812
+ @field regularization_scale regularization scaling factor / decoupled weight decay parameter
813
+ @field regularization_func regularization function (only used by Adam, ignored by AdamW)
814
+ @field clipping_func clipping function
815
+ @field clip_gradients_min minimum gradient values. Used by BNNSOptimizerClippingByValue.
816
+ @field clip_gradients_max maximum gradient values. Used by BNNSOptimizerClippingByValue.
817
+ @field clip_gradients_max_norm max L2-norm. Used by BNNSOptimizerClippingByNorm and BNNSOptimizerClippingByGlobalNorm.
818
+ @field clip_gradients_use_norm global norm to use by BNNSOptimizerClippingByGlobalNorm. If zero, global norm of gradient tensors is computed and used.
819
+ */
820
+ typedef struct {
821
+ float learning_rate;
822
+ float beta1;
823
+ float beta2;
824
+ float time_step;
825
+ float epsilon;
826
+ float gradient_scale;
827
+ float regularization_scale;
828
+ BNNSOptimizerRegularizationFunction regularization_func;
829
+ BNNSOptimizerClippingFunction clipping_func;
830
+ float clip_gradients_min;
831
+ float clip_gradients_max;
832
+ float clip_gradients_max_norm;
833
+ float clip_gradients_use_norm;
834
+ } BNNSOptimizerAdamWithClippingFields
835
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
836
+
837
+ /*!
838
+ @abstract RMSProp Fields
839
+ @discussion
840
+ Implements the RMSProp method as described in [1] (centered version) and [2] (original, non-centred version).
841
+
842
+ Weight w_i is updated using one of the formulae
843
+ Uncentered:
844
+ n_i = ɑ n_i + (1 - ɑ) (dL/dw_i)**2
845
+ Δ_i = γ Δ_i - η dL/dw_i / ( sqrt( n_i ) + ε )
846
+ w_i = w_i + Δ_i
847
+ Centered:
848
+ n_i = ɑ n_i + (1 - ɑ) (dL/dw_i)**2
849
+ g_i = ɑ g_i + (1 - ɑ) (dL/dw_i)
850
+ Δ_i = γ Δ_i - η dL/dw_i / ( sqrt( n_i - g_i**2 ) + ε )
851
+ w_i = w_i + Δ_i
852
+ The gradients dL/dw_i may optionally be clipped to a given range.
853
+
854
+ [1] "Generating Sequences With Recurrent Neural Networks", A. Graves, https://arxiv.org/pdf/1308.0850v5.pdf
855
+ [2] "Neural Networks for Machine Learning", Leacture 6a, G. Hinton, http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf
856
+
857
+ @field learning_rate the scheduled learning rate, which will be adjusted by the algorithm (η above)
858
+ @field alpha smoothing constant (ɑ above)
859
+ @field epsilon term added to denominator (ε above)
860
+ @field centered use the centered variant if true
861
+ @field momentum momentum decay rate (γ above) (set to 0 to disable momentum)
862
+ @field gradient_scale gradient scaling factor
863
+ @field regularization_scale regularization scaling factor / decoupled weight decay parameter
864
+ @field clip_gradients clip gradient between min and max values
865
+ @field clip_gradients_min minimum gradient values. ignored if clip_gradients is false
866
+ @field clip_gradients_max maximum gradient values. ignored if clip_gradients is false
867
+ @field regularization_func regularization function (only used by Adam, ignored by AdamW)
868
+ */
869
+ typedef struct {
870
+ float learning_rate;
871
+ float alpha;
872
+ float epsilon;
873
+ bool centered;
874
+ float momentum;
875
+ float gradient_scale;
876
+ float regularization_scale;
877
+ bool clip_gradients;
878
+ float clip_gradients_min;
879
+ float clip_gradients_max;
880
+ BNNSOptimizerRegularizationFunction regularization_func;
881
+ } BNNSOptimizerRMSPropFields
882
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
883
+
884
+ /*!
885
+ @abstract RMSProp with gradient clipping Fields
886
+ @discussion
887
+ Implements the RMSProp method as described in [1] (centered version) and [2] (original, non-centred version).
888
+
889
+ Weight w_i is updated using one of the formulae
890
+ Uncentered:
891
+ n_i = ɑ n_i + (1 - ɑ) (dL/dw_i)**2
892
+ Δ_i = γ Δ_i - η dL/dw_i / ( sqrt( n_i ) + ε )
893
+ w_i = w_i + Δ_i
894
+ Centered:
895
+ n_i = ɑ n_i + (1 - ɑ) (dL/dw_i)**2
896
+ g_i = ɑ g_i + (1 - ɑ) (dL/dw_i)
897
+ Δ_i = γ Δ_i - η dL/dw_i / ( sqrt( n_i - g_i**2 ) + ε )
898
+ w_i = w_i + Δ_i
899
+ The gradients dL/dw_i may optionally be clipped to a given range.
900
+
901
+ [1] "Generating Sequences With Recurrent Neural Networks", A. Graves, https://arxiv.org/pdf/1308.0850v5.pdf
902
+ [2] "Neural Networks for Machine Learning", Leacture 6a, G. Hinton, http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf
903
+
904
+ @field learning_rate the scheduled learning rate, which will be adjusted by the algorithm (η above)
905
+ @field alpha smoothing constant (ɑ above)
906
+ @field epsilon term added to denominator (ε above)
907
+ @field centered use the centered variant if true
908
+ @field momentum momentum decay rate (γ above) (set to 0 to disable momentum)
909
+ @field gradient_scale gradient scaling factor
910
+ @field regularization_scale regularization scaling factor / decoupled weight decay parameter
911
+ @field regularization_func regularization function (only used by Adam, ignored by AdamW)
912
+ @field clipping_func clipping function
913
+ @field clip_gradients_min minimum gradient values. Used by BNNSOptimizerClippingByValue.
914
+ @field clip_gradients_max maximum gradient values. Used by BNNSOptimizerClippingByValue.
915
+ @field clip_gradients_max_norm max L2-norm. Used by BNNSOptimizerClippingByNorm and BNNSOptimizerClippingByGlobalNorm.
916
+ @field clip_gradients_use_norm global norm to use by BNNSOptimizerClippingByGlobalNorm. If zero, global norm of gradient tensors is computed and used.
917
+ */
918
+ typedef struct {
919
+ float learning_rate;
920
+ float alpha;
921
+ float epsilon;
922
+ bool centered;
923
+ float momentum;
924
+ float gradient_scale;
925
+ float regularization_scale;
926
+ BNNSOptimizerRegularizationFunction regularization_func;
927
+ BNNSOptimizerClippingFunction clipping_func;
928
+ float clip_gradients_min;
929
+ float clip_gradients_max;
930
+ float clip_gradients_max_norm;
931
+ float clip_gradients_use_norm;
932
+ } BNNSOptimizerRMSPropWithClippingFields
933
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
934
+
935
+ /*!
936
+ @abstract Normalization layer parameters
937
+ @discussion
938
+ normalize inputs by using mean, variance, beta and gamma.
939
+
940
+ input and output descriptor must have the same dimensions. Only BNNSDataLayoutImageCHW and BNNSDataLayoutVector are supported.
941
+ Vector of length L is equivalent to ImageCHW of dimension Lx1x1 (CxHxW).
942
+ Currently supported data types for input and output are FP32, BF16, and FP16.
943
+
944
+ beta and gamma are trainable parameters. beta and gamma must be the same type i.e. all FP32, BF16, or FP16.
945
+ Moving mean and variance must be in FP32 regardless of the input data type.
946
+ Except for layer norm, beta, gamma, moving mean and moving variance width (descriptor size[0]) must be equal to number of input channels (descriptor size[2]).
947
+ For layer norm, beta and gamma currently must have the same dimensions as the input depending on normalization_axis.
948
+
949
+ momentum is used to update the moving mean and moving variance during training and must be between 0 and 1
950
+
951
+ epsilon is used in the computation of 1/sqrt(variance + epsilon)
952
+
953
+ @field i_desc input descriptor
954
+ @field o_desc output descriptor
955
+ @field beta_desc beta parameter descriptor
956
+ -ignored if pointer is NULL
957
+ @field gamma_desc gamma parameter descriptor
958
+ -ignored if pointer is NULL
959
+ @field moving_mean pointer to the moving mean
960
+ - Only applicable to batch norm and instance norm. Ignored in layer norm and group norm.
961
+ If pointer is NULL, it means moving mean is not tracked in training or not used in inference.
962
+ @field moving_variance pointer to the moving variance
963
+ - Only applicable to batch norm and instance norm. Ignored in layer norm and group norm.
964
+ If pointer is NULL, it means moving variance is not tracked in training or not used in inference.
965
+ @field momentum momentum.
966
+ -must be between 0 and 1.
967
+ @field epsilon epsilon
968
+ @field activation fused activation layer to follow the batch normalization layer. Only elementwise activation function is supported. ie. no softmax/logsoftmax.
969
+ @field num_groups Divide the channels into this number of groups over which normalization statistics are computed.
970
+ The number of channels in i_desc must be divisible by the number of groups. This is only used in group norm.
971
+ @field normalization_axis The starting axis over which normalization applies. This is only used in layer norm.
972
+ normalization_axis = 0 means normalize over CHW.
973
+ normalization_axis = 1 means normalize over HW.
974
+ normalization_axis = 2 means normalize over W.
975
+ */
976
+ typedef struct {
977
+ BNNSNDArrayDescriptor i_desc;
978
+ BNNSNDArrayDescriptor o_desc;
979
+ BNNSNDArrayDescriptor beta_desc;
980
+ BNNSNDArrayDescriptor gamma_desc;
981
+ BNNSNDArrayDescriptor moving_mean_desc;
982
+ BNNSNDArrayDescriptor moving_variance_desc;
983
+ float momentum;
984
+ float epsilon;
985
+ BNNSActivation activation;
986
+ size_t num_groups;
987
+ size_t normalization_axis;
988
+ } BNNSLayerParametersNormalization
989
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
990
+
991
+ /*!
992
+ @abstract Dropout Layer Fields
993
+
994
+ @field i_desc - array descriptor for input
995
+ @field o_desc - array descriptor for output
996
+ @field rate dropout rate is the probability of an element or a group of elements is dropped out. Range in [0, 1)
997
+ @field seed the seed for the random number generator - ignored if 0
998
+ @field control 8-bit bit mask indicating along which dimension the dropout decision is grouped(kept/dropped altogether) - for now only 4 LSBs have effects.
999
+ */
1000
+ typedef struct {
1001
+ BNNSNDArrayDescriptor i_desc;
1002
+ BNNSNDArrayDescriptor o_desc;
1003
+ float rate;
1004
+ uint32_t seed;
1005
+ uint8_t control;
1006
+ } BNNSLayerParametersDropout
1007
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
1008
+
1009
+ /*!
1010
+ @abstract LSTM layer parameters
1011
+ @discussion
1012
+ LSTM layers parameters include:
1013
+ - sequence descriptor to support different batch size for the sequence
1014
+ - 3 input descriptors (input, hidden_input, cell_state_input)
1015
+ - 3 output descriptors (output, hidden_output, cell_state_output)
1016
+ - 4 compute gates (each gate has "extra input weights descriptor" (iw_desc) for the case where num of layers is > 1 and input size != hidden size
1017
+ - activation gate for the hidden output
1018
+ - dropout value
1019
+ - sequence length, number of layers, batch size and bidirectional corresponds to various size fields in the parameters (see details below)
1020
+ it is sufficient to set these values, "0" size in the NDArray descriptors will be treated as these values. nonzero size in the NDArray descriptors that don't match these values will result in BNNS Error
1021
+ - to enable peepholes, set weights pointer in the gates descriptor (for example, set input_gate_layer[0].cw_desc.data)
1022
+ - to enable bias, set bias pointer in the gates descriptor (for example, set forget_gate_layer[0].bias.data)
1023
+ - input_hidden_descriptor will be treated as array's filled with 0 when input_hidden_descriptor.data is null
1024
+ - input_cell_state_descriptor will be treated as array's filled with 0 when input_cell_state_descriptor.data is null
1025
+ example LSTM computation
1026
+ {
1027
+ // input 1D array
1028
+ const float x_0 = input_descriptor.data_desc;
1029
+ const float h_0 = input_hidden_descriptor.data;
1030
+ const float c_0 = input_cell_state_descriptor.data;
1031
+
1032
+ // output 1D array
1033
+ float h_t = output_hidden_descriptor.data;
1034
+ float c_t = output_cell_state_descriptor.data;
1035
+
1036
+ // temporary 1D array
1037
+ float f_t[output_cell_state_descriptor.size[0]];
1038
+ float i_t[output_cell_state_descriptor.size[0]];
1039
+ float g_t[output_cell_state_descriptor.size[0]];
1040
+ float o_t[output_hidden_descriptor.size[0]];
1041
+
1042
+ f_t = forget_gate_result(x_0, h_0, c_0); // see description in BNNSGateDescriptor
1043
+ i_t = input_gate_result(x_0, h_0, c_0);; // see description in BNNSGateDescriptor
1044
+ g_t = candidate_gate_result(x_0, h_0, c_0);; // see description in BNNSGateDescriptor
1045
+
1046
+ for (size_t o = 0; o < output_cell_state_descriptor.size[0]; o++)
1047
+ c_t[o] = f_t[o] * C_t1[o] + i_t[o] * g_t[o];
1048
+
1049
+ o_t = output_gate_result(x_0, h_0, c_t);; // see description in BNNSGateDescriptor
1050
+
1051
+ for (size_t o = 0; o < output_hidden_descriptor.size[0]; o++)
1052
+ h_t[o] = o_t[o] * hidden_activation.func(c_t[o]);
1053
+ }
1054
+
1055
+ example of STACKED LSTM computation (multiple LSTMCell layers, num_layers > 1)
1056
+ {
1057
+ // input 1D array
1058
+ const float x_0[input_size]; // = input_descriptor.data_desc;
1059
+
1060
+ // input 2D arrays
1061
+ const float h_0[num_layers][hidden_size]; // = input_hidden_descriptor.data;
1062
+ const float c_0[num_layers][hidden_size]; // = input_cell_state_descriptor.data;
1063
+
1064
+ // output 1D array
1065
+ float h_t = output_hidden_descriptor.data;
1066
+ float c_t = output_cell_state_descriptor.data;
1067
+
1068
+ (h_t, c_t) = LASTMCell[0](x_0, h_0[0], c_0[0]); // compute first layer with x_0 as input
1069
+ for (size_t l = 1; l < num_layers; l++) (h_t, c_t) = LASTMCell[1](h_t, h_0[l], c_0[l]); // compute remaining layers with h_t as input
1070
+ }
1071
+
1072
+ example of sequence LSTM computation (seq_len > 1)
1073
+ {
1074
+ // input 2D array
1075
+ const float x_0[seq_len][input_size]; // = input_descriptor.data_desc;
1076
+
1077
+ // input 1D array
1078
+ const float h_0 = input_hidden_descriptor.data;
1079
+ const float c_0 = input_cell_state_descriptor.data;
1080
+
1081
+ // output 2D array
1082
+ float output[seq_len][hidden_size]; // = output_descriptor.data_desc
1083
+
1084
+ // output 1D array
1085
+ float h_t = output_hidden_descriptor.data;
1086
+ float c_t = output_cell_state_descriptor.data;
1087
+
1088
+ (h_t, c_t) = LASTMCell[0](x_0[0], h_0, c_0); // compute first input with h_0 and c_0 as inputs
1089
+ for (size_t s = 1; s < seq_len; s++)
1090
+ {
1091
+ output[s-1] = h_t; // store previous hidden output
1092
+ (h_t, c_t) = LASTMCell[0](x_0[s], h_t, c_t); // compute remaining inputs with h_t and c_t as inputs
1093
+ }
1094
+ output[seq_len-1] = h_t; // store last hidden output
1095
+ }
1096
+
1097
+ example of sequence bidirectional LSTM computation (seq_len > 1, batch_size > 1, is_bidirectional == true)
1098
+ {
1099
+ // input 2D array
1100
+ const float x_0[seq_len][batch_size][input_size]; // = input_descriptor.data_desc;
1101
+ const float h_0[2][batch_size][hidden size]; // = input_hidden_descriptor.data;
1102
+ const float c_0[2][batch_size][hidden size]; // = input_cell_state_descriptor.data;
1103
+
1104
+ // output 4D array
1105
+ float output[sequence length][batch_size][2][hidden size]; // = output_descriptor.data_desc
1106
+
1107
+ // output 3D array
1108
+ float h_t[2][batch_size][hidden size]; // = output_hidden_descriptor.data;
1109
+ float c_t[2][batch_size][hidden size]; // = output_cell_state_descriptor.data;
1110
+
1111
+ (h_t[0], c_t[0]) = LASTMCell[0](x_0[0], h_0[0], c_0[0]); // compute first input with h_0 and c_0 of the forward direction as inputs
1112
+ for (size_t s = 1; s < seq_len; s++)
1113
+ {
1114
+ output[s-1][batch_size][0] = h_t; // store previous hidden output for the first direction
1115
+ (h_t[0], c_t[0]) = LASTMCell[0](x_0[s], h_t[0], c_t[0]); // compute remaining inputs with h_t and c_t as inputs
1116
+ }
1117
+ output[seq_len-1][batch_size][0] = h_t; // store last hidden output of the forward direction
1118
+
1119
+ if (is_bidirectional)
1120
+ {
1121
+ (h_t[1], c_t[1]) = LASTMCell[0](x_0[seq_len-1], h_0[1], c_0[1]); // compute last input with h_0 and c_0 of the backward direction as inputs
1122
+ for (size_t s = 1; s < seq_len; s++)
1123
+ {
1124
+ output[s-1][batch_size][1] = h_t; // store previous hidden output for the first direction
1125
+ (h_t[1], c_t[1]) = LASTMCell[0](x_0[seq_len-s], h_t[1], c_t[1]); // compute remaining inputs in reverse order with h_t and c_t as inputs
1126
+ }
1127
+ output[0][batch_size][1] = h_t; // store last hidden output of the backward direction
1128
+ }
1129
+ }
1130
+
1131
+ @field input_size - number of elements in input
1132
+ @field hidden_size - number of elements in hidden and cell state
1133
+ @field num_layers - number of LSTM layers stacked together
1134
+ @field seq_len - size of the sequential input
1135
+ @field dropout - dropout ratio to apply between LSTM layers. doesn't apply to the last stacked layer, ignoerd when num_layers == 1
1136
+ @field lstm_flags - bit control flags (see BNNSLayerFlagsLSTM)
1137
+ @field batch_size - number of input & output samples
1138
+ @field sequence_descriptor - 1D array of sequence length elements (uint) to determine the batch size for each step
1139
+ (seq_len] > 1) and (sequence_descriptor.data == null) same batch_size will be used for all the sequence
1140
+
1141
+ - num_directions == 1 for unidirectional and 2 for bidirectional
1142
+ @field input_descriptor - contain the descriptors of the input, hidden input and cell state input data
1143
+ input_descriptor.data_desc - the layout should be either BNNSDataLayoutSNE with shape (input_size, batch_size, seq_len) or BNNSDataLayoutNSE with shape (input_size, seq_len, batch_size). If no layout is specified, SNE is assumed.
1144
+ input_descriptor.hidden_desc - the hierarchy of the data should be [num_layers][num_directions][batch_size][hidden_size] (C style multi array notation)
1145
+ input_descriptor.cell_state_desc - the hierarchy of the data should be [num_layers][num_directions][batch_size][hidden_size] (C style multi array notation)
1146
+ @field output_descriptor - contain the descriptors of the output, hidden output and cell state output data
1147
+ output_descriptor.data_desc - the layout should be either BNNSDataLayoutSNE with shape (num_directions*hidden_size, batch_size, seq_len) or BNNSDataLayoutNSE with shape (num_directions*hidden_size, seq_len, batch_size). The first dimension can be unpacked as a 2D array with hidden_size as the major dimension (i.e. it can be interpreted as a C array of shape [num_directions][hidden_size]). If no layout is specified, SNE is assumed.
1148
+ output_descriptor.hidden_desc - the last sequence hidden output. the hierarchy of the data should be [num_layers][num_directions][batch_size][hidden_size] (C style multi array notation)
1149
+ output_descriptor.cell_state_desc - the last sequence cell state output. the hierarchy of the data should be [num_layers][num_directions][batch_size][hidden_size] (C style multi array notation)
1150
+ @field forget_gate - forget descriptor (default activation is sigmoid), memory pointers ordered as [num_layers][num_directions][hidden_size][input_size/hidden_size] (C style multi array notation)
1151
+ @field input_gate - input descriptor (default activation is sigmoid), memory pointers ordered as [num_layers][num_directions][hidden_size][input_size/hidden_size] (C style multi array notation)
1152
+ @field candidate_gate - candidate descriptor (default activation is tanh), memory pointers ordered as [num_layers][num_directions][hidden_size][input_size/hidden_size] (C style multi array notation)
1153
+ @field output_gate - output descriptor (default activation is sigmoid), memory pointers ordered as [num_layers][num_directions][hidden_size][input_size/hidden_size] (C style multi array notation)
1154
+ @field hidden_activation - hidden activation layer to compute hidden output (default actviation tanh)
1155
+ */
1156
+ typedef struct __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0)) {
1157
+ size_t input_size;
1158
+ size_t hidden_size;
1159
+ size_t batch_size;
1160
+ size_t num_layers;
1161
+ size_t seq_len;
1162
+ float dropout;
1163
+ uint32_t lstm_flags;
1164
+ BNNSNDArrayDescriptor sequence_descriptor;
1165
+ BNNSLSTMDataDescriptor input_descriptor;
1166
+ BNNSLSTMDataDescriptor output_descriptor;
1167
+ BNNSLSTMGateDescriptor input_gate;
1168
+ BNNSLSTMGateDescriptor forget_gate;
1169
+ BNNSLSTMGateDescriptor candidate_gate;
1170
+ BNNSLSTMGateDescriptor output_gate;
1171
+ BNNSActivation hidden_activation;
1172
+ } BNNSLayerParametersLSTM
1173
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
1174
+
1175
+ /*!
1176
+ @abstract Arithmetic layer parameters
1177
+ @discussion
1178
+ Compute an arithmetic operation described by arithmetic_function.
1179
+ Descriptors are assumed to represent the same data if their descriptors data pointer are the same in which case the descriptor type and descriptor sizes, strides and data type must match.
1180
+ if input descriptors represent the same data, so should their gradient descriptors.
1181
+ For example, for a multiplication operation y=x*x, in1 and in2 are both pointing to x. in1_delta and in2_delta should both be pointing to the same gradient dx.
1182
+
1183
+ @field arithmetic_function arithmetic function
1184
+ @field arithmetic_function_fields a pointer to an arithmetic function field structure as described in BNNSArithmeticFunction, such as BNNSArithmeticBinary for arithmetic function BNNSArithmeticAdd
1185
+ @field activation Layer activation function
1186
+ */
1187
+ typedef struct {
1188
+ BNNSArithmeticFunction arithmetic_function;
1189
+ void* _Nonnull arithmetic_function_fields;
1190
+ BNNSActivation activation;
1191
+ } BNNSLayerParametersArithmetic
1192
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
1193
+
1194
+ /*! @abstract Permute Layer Parameters
1195
+ *
1196
+ * @description
1197
+ * This layer copies one tensor to another whilst permuting the order of the axes.
1198
+ * It is assumed input and output axes are numbered as 0,1, ...N-1, where N is the dimension of the data layout.
1199
+ * the permutation array specifies the input axis source for each output axis, such that the K'th element in the permutation array specify the input axis source for the K'th axis in the output descriptor
1200
+ * for example, a permutation array [2,1,0] applied on a BNNSDataLayoutImageCHW tensor will result in axis reverse such that output axis 0 is input axis 2, output axis 1 is input axis 1, and output axis 2 is input axis 0.
1201
+
1202
+ * notice:
1203
+ * input and output data layout dimensions and data type must match
1204
+ * output tensor sizes must be reordered according to the permutation specified in the permutation array
1205
+ * permutation array values in indexes equal or beyond tenor dimension are being ignored
1206
+ *
1207
+ * @field i_desc input descriptor
1208
+ * @field o_desc output descriptor
1209
+ * @field permutation permutation array
1210
+ */
1211
+ typedef struct {
1212
+ BNNSNDArrayDescriptor i_desc;
1213
+ BNNSNDArrayDescriptor o_desc;
1214
+ size_t permutation[BNNS_MAX_TENSOR_DIMENSION];
1215
+ } BNNSLayerParametersPermute
1216
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
1217
+
1218
+ /*! @abstract Tensor Contraction Layer Parameters
1219
+ *
1220
+ * @discussion
1221
+ * This layer performs an arbitrary tensor contraction.
1222
+ * A tensor contraction is a summation of the elements of two tensors over one or more indices. We represent this
1223
+ * in the operation argument as a string using Einstein's summation convention.
1224
+ * For example, the string
1225
+ * "a_ijp, b_ijq -> o_pq"
1226
+ * would represent the operation
1227
+ * o_pq = alpha * \sum_{i,j} a_ijp * b_ijq.
1228
+ * Inputs may be either trained parameters or inputs.
1229
+ * If the name preceding the underscore is 'w' the tensor is assumed to be a trained parameter (weights), otherwise it is
1230
+ * assumed to be an input (the letter has no other effect and may be whatever the user wishes). At most one of the inputs
1231
+ * may be a trained parameter.
1232
+ * If the names of the left-hand side inputs match, the operation is assumed to be the contraction of a tensor with itself (to e.g.
1233
+ * calculate the Gram matrix or a dot product).
1234
+ * In the case of one of the inputs being a weight or the operation is contraction of a tensor with itself, the regular BNNSFilterApply
1235
+ * and BNNSFilterApplyBatch functions may be used with the resulting layer.
1236
+ * If the operation has two inputs, the BNNSFilterApplyTwoInput or BNNSFilterApplyTwoInputBatch functions must be used instead.
1237
+ *
1238
+ * This layer may also be used to copy from one layer to another with a permutation of indices.
1239
+ * For example, the string
1240
+ * "a_ij -> c_ji"
1241
+ * would represent the operation
1242
+ * C = alpha * A^T.
1243
+ *
1244
+ * Broadcasting is supported by using '*' as the final index, e.g. "a_ij*, b_ij -> c_*" would match additional indices of a and c. * only
1245
+ * ever represents indices that occur on both sides of the operation, and never an index over which summation is performed. A *
1246
+ * on one side will only match indices represent by a * on the other side, and never indices represented by a letter. The wildcard may
1247
+ * occur at either the start or end of the index sequence, however it must occur in the same position (start or end) for all operands.
1248
+ *
1249
+ * Note that where index letters are matched, the sizes of the corresponding dimensions for the tensors must also match, unless one
1250
+ * tensor has size 1, in which case it will be repeated (broadcast) to match the corresponding dimension.
1251
+ *
1252
+ * @field operation string as described above
1253
+ * @field alpha Scaling to be applied to result
1254
+ * @field beta Scaling to be applied to output before adding result. Must be 0.0 or 1.0.
1255
+ * @field iA_desc Descriptor of the A input matrix
1256
+ * @field iB_desc Descriptor of the B input matrix
1257
+ * @field o_desc Descriptor of the output matrix
1258
+ */
1259
+ typedef struct {
1260
+ const char *operation;
1261
+ float alpha;
1262
+ float beta;
1263
+ BNNSNDArrayDescriptor iA_desc;
1264
+ BNNSNDArrayDescriptor iB_desc;
1265
+ BNNSNDArrayDescriptor o_desc;
1266
+ } BNNSLayerParametersTensorContraction
1267
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
1268
+
1269
+ /*! @abstract Gram Matrix Layer Parameters
1270
+ *
1271
+ * @discussion
1272
+ * The Gram matrix is calculated as:
1273
+ * G[ f, c ] = alpha * sum_{i,j} x[ i, j, f ] * x[ i, j, c ]
1274
+ *
1275
+ * Any additional dimensions are broadcast.
1276
+ * This operation is equivalent to using the TensorContraction layer with operation string "x_*ijf, x_*ijc -> G_*fc".
1277
+ *
1278
+ * @field alpha Scaling to be applied to the result
1279
+ * @field i_desc The tensor x.
1280
+ * @field o_desc The tensor G.
1281
+ */
1282
+ typedef struct {
1283
+ float alpha;
1284
+ BNNSNDArrayDescriptor i_desc;
1285
+ BNNSNDArrayDescriptor o_desc;
1286
+ } BNNSLayerParametersGram
1287
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
1288
+
1289
+ /*! @abstract Resize Layer Parameters
1290
+ *
1291
+ * @discussion
1292
+ * Resize from between differently sized tensor using the specified method.
1293
+ *
1294
+ * Resize is only performed in dimensions where tensor sizes differ between input and output.
1295
+ * Resized dimensions must all either upsample or downsample. A mixture of the two is not permitted.
1296
+ *
1297
+ * @field method - The interpolation method to use
1298
+ * @field i_desc - NDArray descriptor for the input tensor.
1299
+ * @field o_desc - NDArray descriptor for the output tensor. Dimensions must be an integral
1300
+ * multiple of those used for i_desc.
1301
+ * @field align_corners - If true, aligns the corners of the upscaling grid to the centre of
1302
+ * scaling dimensions rather than the edges.
1303
+ */
1304
+ typedef struct {
1305
+ BNNSInterpolationMethod method;
1306
+ BNNSNDArrayDescriptor i_desc;
1307
+ BNNSNDArrayDescriptor o_desc;
1308
+ bool align_corners;
1309
+ } BNNSLayerParametersResize
1310
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
1311
+
1312
+ /*! @abstract Crop and Resize Layer Parameters
1313
+ * @discussion only bilinear interpolation is supported now.
1314
+ *
1315
+ * @field normalized_coordinates - If true, the bounding box coordinates are normalized to (0, 1)
1316
+ * @field spatial_scale - Additional spatial scale that multiplies the bounding box coordinates.
1317
+ * @field extrapolation_value - Value used for extrapolation, when applicable. Default to zero.
1318
+ * @field sampling_mode - Sampling mode used to select sample points.
1319
+ * @field box_coordinate_mode - Specifies the convention for specifying the four bounding box coordinates.
1320
+ * @field method - The interpolation method to use
1321
+ */
1322
+ typedef struct {
1323
+ bool normalized_coordinates;
1324
+ float spatial_scale;
1325
+ float extrapolation_value;
1326
+ BNNSLinearSamplingMode sampling_mode;
1327
+ BNNSBoxCoordinateMode box_coordinate_mode;
1328
+ BNNSInterpolationMethod method;
1329
+ } BNNSLayerParametersCropResize
1330
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(13.0, 15.0), ios(16.0, 18.0), watchos(9.0, 11.0), tvos(16.0, 18.0));
1331
+
1332
+ /*! @abstract Broadcast Matrix Multiplication Layer Parameters
1333
+ *
1334
+ * @description
1335
+ * Given two tensors calculate the tensor that results from performing the matrix
1336
+ * multiplication on the last two indices. Indices other than the last two are matched
1337
+ * from the back. If one tensor has higher dimension than the other, the lower rank matrix
1338
+ * is broadcast in the missing dimensions. If a tensor has size 1 in a dimensions, then it
1339
+ * is broadcast to match the size in the matching dimension of the other tensor.
1340
+ * The output tensor may optionally be scaled by a scalar α, and the last two indices of
1341
+ * each tensor may be transposed.
1342
+ *
1343
+ * For example, if we have A_ijk and B_pqrkt, then indices i and r are matched and must
1344
+ * have the same size. The following multiplications are performed:
1345
+ * for p = 0:size(B,0)-1
1346
+ * for q = 0:size(B,1)-1
1347
+ * for r = 0:size(B,2)-1
1348
+ * // Matrix multiplication on last two indices
1349
+ * for j = 0:size(A,1)-1
1350
+ * for t = 0:size(B,4)-1
1351
+ * c_pqrjt = β c_pqrjt + α \sum_k a_rjk b_pqrkt
1352
+ * where alpha is scalar scaling factor.
1353
+ * If dimension i of A had size 1, then we would broadcast in that dimension, with the final
1354
+ * line of pseudocode above becoming
1355
+ * c_pqrjt = β c_pqrjt + α \sum_k a_1jk b_pqrkt
1356
+ *
1357
+ * This layer is equivalent to the tensor contraction layer with one of the operation strings:
1358
+ * transA transB operation
1359
+ * F F "a_*ik, b_*kj -> c_*ij"
1360
+ * F T "a_*ik, b_*jk -> c_*ij"
1361
+ * T F "a_*ki, b_*kj -> c_*ij"
1362
+ * T T "a_*ki, b_*jk -> c_*ij"
1363
+ *
1364
+ * @field alpha - scalar by which to scale the result
1365
+ * @field beta - scalar by which to scale output before adding result. Must be 0.0 or 1.0.
1366
+ * @field transA - if true, transposes the last two dimensions of A
1367
+ * @field transB - if true, transposes the last two dimensions of B
1368
+ * @field quadratic - if true, it is assumed that this operation the multiplication of the matrix with itself, and inputB is ignored.
1369
+ * This will make no difference on inference (except that higher performance may be achieved if quadratic=true),
1370
+ * however the backward gradient calculation will be incorrect (or rather the desired gradient will otherwise be the sum
1371
+ * of inputA_delta and inputB_delta if quadratic=false).
1372
+ * @field a_is_weights - if true, the matrix A is treated as weights (i.e. fixed across all matrices in a batch on inference,
1373
+ * accumulates gradients across a batch during training)
1374
+ * @field b_is_weights - if true, the matrix B is treated as weights (i.e. fixed across all matrices in a batch on inference,
1375
+ * accumulates gradients across a batch during training)
1376
+ * @field iA_desc - descriptor for tensor A
1377
+ * @field iB_desc - descriptor for tensor B
1378
+ * @field o_desc - descriptor for tensor C
1379
+ */
1380
+ typedef struct {
1381
+ float alpha;
1382
+ float beta;
1383
+ bool transA;
1384
+ bool transB;
1385
+ bool quadratic;
1386
+ bool a_is_weights;
1387
+ bool b_is_weights;
1388
+ BNNSNDArrayDescriptor iA_desc;
1389
+ BNNSNDArrayDescriptor iB_desc;
1390
+ BNNSNDArrayDescriptor o_desc;
1391
+ } BNNSLayerParametersBroadcastMatMul
1392
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
1393
+
1394
+ /*! @abstract Multihead Attention Parameters
1395
+ *
1396
+ * @discussion
1397
+ * This layer implements the multihead attention layer as described in the paper
1398
+ * "Attention is All You Need" by A. Vaswani, N. Shazeer, N.Parmar, J. Uszkoreit, L. Jones, A. Gomez, L.Kaiser and I. Polosukhin.
1399
+ *
1400
+ * Each input Q, K, V is projected num_head times and an attention calculation performed on each result, before the concatenated
1401
+ * results are projected down to obtain an output. An optional dropout may be applied to the final result.
1402
+ *
1403
+ * MultiheadAttention(Q, K, V) = concat(head_1, ... head_h) Wᴼ + epᴼᵀ where e is the vector of all 1s, and pᴼ is a bias vector
1404
+ * head_i = Attention(QWᴾ_i + epᴾᵀ, KWᴷ_i + epᴷᵀ, VWⱽ_i + epⱽᵀ) where e is the vector of all 1s, and pᴾ, pᴷ and pⱽ are bias vectors
1405
+ * Attention(Q, K, V) = dropout( softmax( mask( 1/sqrt(d_key) * Q [K; bᴷ; 0]ᵀ ) ) ) [V; bⱽ; 0] where bᴷ and bⱽ are optional bias vectors and 0 row is added iff add_zero_attn is true.
1406
+ * mask(R) = { r_ij + x_ij if key_mask[ j ] is false, where X is the matrix described by the add_to_attention parameter
1407
+ * { -inf if key_mask[ j ] is true
1408
+ *
1409
+ * The following are optional:
1410
+ * - The use of projection bias vectors pᴾ, pᴷ, pⱽ, and pᴼ.
1411
+ * - The use of attention bias vectors bᴷ and bⱽ.
1412
+ * - The addition of the zero attention row.
1413
+ * - The use of a key_mask.
1414
+ * - The addition of X to the attention (this can be controlled on a per-apply call basis).
1415
+ *
1416
+ * The following values are inferred from the input descriptors:
1417
+ * num_heads - number of heads in the model, inferred from query.weights->size[2]
1418
+ * target_length - target sequence length, inferred from query.target_desc->size[0]
1419
+ * source_length - source sequence length, inferred from key.target_desc->size[0]
1420
+ * d_model - dimension of model, inferred from query.target_desc->size[1]
1421
+ * d_key - dimension of key space, inferred from key.weights->size[1]
1422
+ * d_value - dimension of value space, inferred from output.target_desc->size[1]
1423
+ * k_dim - dimension of input key sequence, inferred from key.target_desc->size[1]
1424
+ * v_dim - dimension of input value sequence, inferred from value.target_desc->size[1]
1425
+ * Note: In releases prior to macOS 12.0, iOS 15.0, watchOS 8.0 and tvOS 15.0, k_dim and v_dim must be equal to d_model.
1426
+ *
1427
+ * Typical usage would be to set d_key = d_value = d_model / num_heads such that the total work
1428
+ * involved is similar to that of a single headed attention layer of d_model dimension.
1429
+ *
1430
+ * @field query describes the query-related input parameters and projection:
1431
+ * .target_desc - a 2D tensor of shape target_length x d_model used as an input (Q above)
1432
+ * .weights - a 3D tensor of shape d_model x d_key x num_heads giving a projection for each head from Q
1433
+ * to a d_key size space (Wᴾ above)
1434
+ * .bias - a 2D tensor of shape d_key x num_heads added as a bias during the projection from Q (pᴾ above)
1435
+ * @field key describes the key-related input parameters and projection:
1436
+ * .target_desc - a 2D tensor of shape source_length x k_dim used as an input (K above)
1437
+ * .weights - a 3D tensor of shape k_dim x d_key x num_heads giving a projection for each head from K
1438
+ * to a d_key size space (Wᴷ above)
1439
+ * .bias - a 2D tensor of shape d_key x num_heads added as a bias during the projection from K (pᴷ above)
1440
+ * @field value describes the value-related input parameters and projection:
1441
+ * .target_desc - a 2D tensor of shape source_length x v_dim used as an input (V above)
1442
+ * .weights - a 3D tensor of shape v_dim x d_value x num_heads giving a projection for each head from V
1443
+ * to a d_value size space (Wⱽ above)
1444
+ * .bias - a 2D tensor of shape d_value x num_heads added as a bias during the projection from V (pⱽ above)
1445
+ * @field add_zero_attn If true, a row of zeroes is added to the projected K and to V inputs to the Attention calculation
1446
+ * @field key_attn_bias Optional, a 2D tensor of shape d_key x num_heads that is added as part of the attention calculation (bᴷ above)
1447
+ * If not required, set key_attn_bias.data=NULL. If present, value_attn_bias must also be present.
1448
+ * @field value_attn_bias Optional, a 2D tensor of shape d_value x num_heads that is added as part of the attention calculation (bⱽ above)
1449
+ * If not required, set value_attn_bias.data=NULL. If present, key_attn_bias must also be present.
1450
+ * @field output describes the output tensor and associated projection:
1451
+ * .target_desc - a 2D tensor of shape target_length x d_model used for output of the result
1452
+ * .weights - a 2D tensor of shape num_heads*d_value x d_model used to combine all heads into a single output (Wᴼ above)
1453
+ * .bias - a 1D tensor of shape d_model added to the final result (pᴼ above)
1454
+ * @field dropout the dropout probability (no dropout is performed if dropout is outside the range (0, 1)
1455
+ * @field seed random seed used for dropout layer
1456
+ */
1457
+ typedef struct {
1458
+ BNNSMHAProjectionParameters query;
1459
+ BNNSMHAProjectionParameters key;
1460
+ BNNSMHAProjectionParameters value;
1461
+ bool add_zero_attn;
1462
+ BNNSNDArrayDescriptor key_attn_bias;
1463
+ BNNSNDArrayDescriptor value_attn_bias;
1464
+ BNNSMHAProjectionParameters output;
1465
+ float dropout;
1466
+ uint32_t seed;
1467
+ } BNNSLayerParametersMultiheadAttention
1468
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
1469
+
1470
+ /*!
1471
+ @discussion computing reduction function across selected dimensions
1472
+ specify the reduction axis by setting the o_desc.size[i] =1 (other dimensions must be similar to the input size)
1473
+ example:
1474
+ input [3][2][3], output [3][2][1] would be reduction on the width
1475
+ input [3][2][4], output [1][2][4] would be reduction on the channel
1476
+ input [3][2][4], output [1][2][1] would be reduction on the channel and width
1477
+
1478
+ - support only fp16/fp32 input and output (for now)
1479
+ - reduction function will be computed in fp32
1480
+ - when w_desc.data is set reduction is weighted (for each dimension, w_desc.size[i] == i_desc.size[i])
1481
+ - for SumLog operation, epsilon is added for each log operation
1482
+
1483
+ Sum reduction on channel example:
1484
+ input [3][2][4], output [1][2][4]
1485
+ for (size_t h = 0; h < 2; h++)
1486
+ {
1487
+ for (size_t w = 0; w < 4; w++)
1488
+ {
1489
+ float sum = 0.0f;
1490
+ for (size_t c = 0; c < 3; c++)
1491
+ {
1492
+ float w_val = 1.0f;
1493
+ if (weights.data) w_val = weights.data[c*2*4 + h*4 + w];
1494
+ sum += input.data[c*2*4 + h*4 + w] * w_val;
1495
+ }
1496
+ output.data[h*4 + w] = sum;
1497
+ }
1498
+ }
1499
+
1500
+ Mean by nonzero weights on width example
1501
+ input [3][2][4], output [3][2][1], weights[3][2][4]
1502
+ for (size_t c = 0; c < 3; c++)
1503
+ {
1504
+ for (size_t h = 0; h < 2; h++)
1505
+ {
1506
+ float sum = 0.0f;
1507
+ float count = 0.0f;
1508
+ for (size_t w = 0; w < 4; w++)
1509
+ {
1510
+ if ((weights.data) && (weights.data[c*2*4 + h*4 + w] != 0.0f)
1511
+ {
1512
+ w_val = weights.data[c*2*4 + h*4 + w];
1513
+ count += 1.0f;
1514
+ sum += input.data[c*2*4 + h*4 + w] * w_val;
1515
+ }
1516
+ }
1517
+ output.data[c*2*4 + h] = sum / count;
1518
+ }
1519
+ }
1520
+
1521
+ @field i_desc - input descriptor (pointer, data type, dimensions, dimension stride)
1522
+ @field o_desc - output descriptor (pointer, data type, dimensions, dimension stride)
1523
+ @field w_desc - weights descriptor when needed (pointer, data type, dimensions, dimension stride)
1524
+ @field reduce_func - reduction function across chosen axis
1525
+ @field epsilon - value added to SumLog reduction
1526
+ */
1527
+ typedef struct {
1528
+ BNNSNDArrayDescriptor i_desc;
1529
+ BNNSNDArrayDescriptor o_desc;
1530
+ BNNSNDArrayDescriptor w_desc;
1531
+ BNNSReduceFunction reduce_func;
1532
+ float epsilon;
1533
+ } BNNSLayerParametersReduction;
1534
+
1535
+ /*!
1536
+ @abstract Padding Layer Fields
1537
+
1538
+ @field i_desc tensor descriptor for input
1539
+ @field o_desc tensor descriptor for output. For each dimension of the tensor ‘dim', the output size should equal to i_desc.size[dim] + padding[dim][0] + padding[dim][1].
1540
+ @field padding_size number of padding elements to add before and after the original data.
1541
+ padding[d][0] gives the number of elements before, and padding[d][1] the number of elements after the data in dimension d.
1542
+ Elements of the array that do not correspond to dimensions included in the tensor layout are not accessed.
1543
+ @field padding_mode padding mode
1544
+ @field padding_value the value to fill the padding area if BNNSPaddingModeConstant is used. Up to 4 bytes can be stored, and first 'size' bytes are used depending on the data type.
1545
+ */
1546
+
1547
+ typedef struct {
1548
+ BNNSNDArrayDescriptor i_desc;
1549
+ BNNSNDArrayDescriptor o_desc;
1550
+ size_t padding_size[BNNS_MAX_TENSOR_DIMENSION][2];
1551
+ BNNSPaddingMode padding_mode;
1552
+ uint32_t padding_value;
1553
+ } BNNSLayerParametersPadding
1554
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(11.0, 15.0), ios(14.0, 18.0), watchos(7.0, 11.0), tvos(14.0, 18.0));
1555
+
1556
+ /*!
1557
+ * @abstract Embedding Layer Fields
1558
+ *
1559
+ * @discussion
1560
+ * Implements a lookup table into a dictionary array, the elements of which can be trained parameters.
1561
+ * Given a scalar index i, output is the i-th element of the dictionary array. An input may consist of multiple indices (words) that result in
1562
+ * multiple dictionary items (embeddings) being returned.
1563
+ * For example, if dictionary has shape (3, 4, 5), then a single dictionary item would have shape (3, 4) and input values would be in the
1564
+ * range [0, 4]. If the input shape was thus (6, 7), the output shape would be (3, 4, 6, 7), that is the concatenation of the dicitionary item
1565
+ * shape and the input shape.
1566
+ *
1567
+ * @field flags - Bit field for flags specifying additional behavior, such as scaling gradient by frequency
1568
+ * @field i_desc - Input descriptor for this layer. Must specify a (signed or unsigned) integer type.
1569
+ * @field o_desc - Output descriptor, of shape (dictionary_item.shape, i_desc.shape), where dictionary_item corresponds to the shape
1570
+ * of dictionary without the final index.
1571
+ * @field dictionary - Dictionary of shape dictionary_item with an extra dimension of size num_embeddings appended.
1572
+ * @field padding_idx - If padding_idx is in the range [0, num_embeddings-1], then dictionary[:, padding_idx] is treated as always
1573
+ * containing a zero tensor (forward apply will ignore the contents if dictionary[:, padding_idx], which may however change if an
1574
+ * optimizer step is applied to them).
1575
+ * @field max_norm - If nonzero, then any vector with norm > max_norm will be renormalized to have norm max_norm during forward
1576
+ * lookups. Type of norm is specified by norm_type.
1577
+ * @field norm_type - If max_norm is nonzero, the type of norm to use (specifically the p-norm where p=norm_type). If norm_type=0,
1578
+ * the 2-norm is used.
1579
+ */
1580
+ typedef struct {
1581
+ BNNSEmbeddingFlags flags;
1582
+ BNNSNDArrayDescriptor i_desc;
1583
+ BNNSNDArrayDescriptor o_desc;
1584
+ BNNSNDArrayDescriptor dictionary;
1585
+ size_t padding_idx;
1586
+ float max_norm;
1587
+ float norm_type;
1588
+ } BNNSLayerParametersEmbedding
1589
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(12.0, 15.0), ios(15.0, 18.0), watchos(8.0, 11.0), tvos(15.0, 18.0));
1590
+
1591
+ /*!
1592
+ @abstract Quantization layer parameters
1593
+ @discussion
1594
+ The Quantization layer converts higher precision tensors to lower precision tensors (Quantize) or lower precision tensors to higher precision tensors (Dequantize)
1595
+ The data_scale and data_bias fields in all provided descriptors are ignored.
1596
+
1597
+ @field axis_mask dimension mask
1598
+ axis_mask describes the axis in which scale and bias are applied, if provided.
1599
+ To apply scale/bias on axis i, set axis_mask bit i to 1.
1600
+ axis_mask must have a single axis set to 1, or be equal to 0.
1601
+ The batch dimension may be specified as part of the axis mask by setting axis_mask = (1<<naxis), where naxis is the number of axes specified by the tensor layout
1602
+ If axis_mask is 0, the entire tensor will be quantized/dequantized using scalar scale and bias values, if provided.
1603
+ For example, when using a BNNSDataLayoutImageCHW layout, axis_mask = 0 apply scale/bias on entire tensor, axis_mask = 1 apply scale/bias on width (W), axis_mask = 2 apply scale/bias on height (H), axis_mask = 4 apply scale/bias on channel (C), axis_mask = 8 apply scale/bias on the batch dimension.
1604
+ @field function quantizer function
1605
+ @field i_desc input descriptor
1606
+ @field o_desc output descriptor
1607
+ @field scale optional scale descriptor
1608
+ - ignored if scale.data is null
1609
+ - scale layout must be BNNSDataLayoutVector
1610
+ - scale vector size must be equal to the input tensor axis size, or equal to 1 if axis_mask is 0.
1611
+ @field bias optional bias descriptor
1612
+ - ignored if bias.data is null
1613
+ - bias layout must be BNNSDataLayoutVector
1614
+ - bias vector size must be equal to the input tensor axis size, or equal to 1 if axis_mask is 0.
1615
+ */
1616
+ typedef struct {
1617
+ size_t axis_mask;
1618
+ BNNSQuantizerFunction function;
1619
+ BNNSNDArrayDescriptor i_desc;
1620
+ BNNSNDArrayDescriptor o_desc;
1621
+ BNNSNDArrayDescriptor scale;
1622
+ BNNSNDArrayDescriptor bias;
1623
+ } BNNSLayerParametersQuantization
1624
+ __API_DEPRECATED("Use BNNSGraph* APIs", macos(12.0, 15.0), ios(15.0, 18.0), watchos(8.0, 11.0), tvos(15.0, 18.0));
1625
+
1626
+ /*!
1627
+ @abstract parameters to describe sparsity attributes
1628
+ @discussion use this data structure as hint for BNNSNDArrayFullyConnectedSparsify
1629
+ for BNNSSparsityTypeUnstructured, sparsity_ratio is numerator / denominator
1630
+ @field flags - bit flags to define different states (currently reserved and should be initialized to zero)
1631
+ @field sparsity_ratio - numerator(sparsity_ratio[0]) and denominator(sparsity_ratio[1])indicating sparsity ratio. numerator must be less than denominator.
1632
+ @field sparsity_type - type of sparsity
1633
+ @field target_system - use this to specify different SoC where the inference will run.
1634
+ */
1635
+ typedef struct {
1636
+ uint64_t flags;
1637
+ uint32_t sparsity_ratio[2]; // numerator(sparsity_ratio[0]) and denominator(sparsity_ratio[1])indicating sparsity ratio. numerator must be less than denominator.
1638
+ BNNSSparsityType sparsity_type;
1639
+ BNNSTargetSystem target_system;
1640
+ } BNNSSparsityParameters
1641
+ __API_AVAILABLE(macos(13.0), ios(16.0), watchos(9.0), tvos(16.0));
1642
+
1643
+ #pragma mark - Deprecated
1644
+
1645
+ /*!
1646
+
1647
+ @abstract Image stack descriptor (DEPRECATED, Use BNNSNDArrayDescriptor)
1648
+
1649
+ @discussion
1650
+ An image stack is a sequence of images with the same width and height. Each image in the sequence is called a channel.
1651
+ For example, a RGB image will be stored as three separate channels. A pixel has only one scalar value, stored using the type
1652
+ described by <tt>data_type</tt>.
1653
+
1654
+ Pixel <tt>P(c,x,y)</tt> at position <tt>(x,y)</tt> in channel <tt>c</tt> is stored in
1655
+ <tt>data[x + row_stride * y + image_stride * c]</tt>, with
1656
+ <tt>x=0..width-1</tt>,
1657
+ <tt>y=0..height-1</tt>,
1658
+ <tt>c=0..channels-1</tt>. <tt>row_stride &geq; width</tt>, <tt>image_stride &geq; row_stride * height</tt>.
1659
+
1660
+ Int<n> types are converted to floating point using float Y = DATA_SCALE * (float)X + DATA_BIAS, and back to integer using Int<n> X = convert_and_saturate(Y / DATA_SCALE - DATA_BIAS)
1661
+
1662
+ @field width Image width
1663
+ @field height Image height
1664
+ @field channels Number of images in stack
1665
+ @field row_stride Increment (in values) between image rows
1666
+ @field image_stride Increment (in values) between image channels
1667
+ @field data_type Storage data type for image values. INDEXED data types are not allowed here.
1668
+ @field data_scale Conversion scale for image values, used for INT,UINT data types only
1669
+ @field data_bias Conversion bias for image values, used for INT,UINT data types only
1670
+
1671
+ */
1672
+ typedef struct {
1673
+
1674
+ size_t width;
1675
+ size_t height;
1676
+ size_t channels;
1677
+ size_t row_stride;
1678
+ size_t image_stride;
1679
+
1680
+ BNNSDataType data_type;
1681
+ float data_scale;
1682
+ float data_bias;
1683
+
1684
+ } BNNSImageStackDescriptor
1685
+ __API_DEPRECATED("BNNS switched to new Layer Parameters data structures",macos(10.12, 11.0), ios(10.0, 14.0), watchos(3.0, 7.0), tvos(10.0, 14.0));
1686
+
1687
+ /*!
1688
+
1689
+ @abstract Vector format descriptor (DEPRECATED, Use BNNSNDArrayDescriptor)
1690
+
1691
+ @discussion
1692
+ Represents a vector of dimension <tt>size</tt>.
1693
+ Each vector element is a scalar value, stored using the type specified in <tt>data_type</tt>.
1694
+
1695
+ Component <tt>V(i)</tt> at index <tt>i</tt> is stored in <tt>data[i]</tt>, with <tt>i=0..size-1</tt>.
1696
+
1697
+ Int<n> types are converted to floating point using float Y = DATA_SCALE * (float)X + DATA_BIAS, and back to integer using Int<n> X = convert_and_saturate(Y / DATA_SCALE - DATA_BIAS)
1698
+
1699
+ @field size Vector dimension
1700
+ @field data_type Storage data type for vector values. INDEXED data types are not allowed here.
1701
+ @field data_scale Conversion scale for vector values, used for INT,UINT data types only
1702
+ @field data_bias Conversion bias for vector values, used for INT,UINT data types only
1703
+
1704
+ */
1705
+ typedef struct {
1706
+
1707
+ size_t size;
1708
+
1709
+ BNNSDataType data_type;
1710
+ float data_scale;
1711
+ float data_bias;
1712
+
1713
+ } BNNSVectorDescriptor
1714
+ __API_DEPRECATED("BNNS switched to new Layer Parameters data structures", macos(10.12, 11.0), ios(10.0, 14.0), watchos(3.0, 7.0), tvos(10.0, 14.0));
1715
+
1716
+ /*!
1717
+
1718
+ @abstract Common layer parameters (DEPRECATED, Use BNNSNDArrayDescriptor)
1719
+
1720
+ @discussion Int<n> types are converted to floating point using float Y = DATA_SCALE * (float)X + DATA_BIAS, and back to integer using Int<n> X = convert_and_saturate(Y / DATA_SCALE - DATA_BIAS)
1721
+
1722
+ @field data Pointer to layer values (weights, bias), layout and size are specific to each layer
1723
+ @field data_type Storage data type for the values stored in <tt>data</tt>
1724
+ @field data_scale Conversion scale for values, used for INT data types only, ignored for INDEXED and FLOAT data types
1725
+ @field data_bias Conversion bias for values, used for INT data types only, ignored for INDEXED and FLOAT data types
1726
+ @field data_table Conversion table (256 values) for indexed floating point data, used for INDEXED data types only
1727
+
1728
+ */
1729
+ typedef struct {
1730
+
1731
+ const void * _Nullable data;
1732
+ BNNSDataType data_type;
1733
+ float data_scale;
1734
+ float data_bias;
1735
+ const float * _Nullable data_table;
1736
+
1737
+ } BNNSLayerData
1738
+ __API_DEPRECATED("BNNS switched to new Layer Parameters data structures", macos(10.12, 11.0), ios(10.0, 14.0), watchos(3.0, 7.0), tvos(10.0, 14.0));
1739
+
1740
+ /*!
1741
+
1742
+ @abstract Convolution parameters (DEPRECATED, Use BNNSLayerParametersConvolution)
1743
+
1744
+ @discussion
1745
+ The convolution product Output = Weights &times; Input is defined as follows. Pixel <tt>Output(o,x,y)</tt> of the output image stack receives:
1746
+ <br><tt>Output(o,x,y) = &sum;<sub>i,kx,ky</sub> Weight(o,i,kx,ky) * Input(i,x_stride * x + kx,y_stride * y + ky)</tt> with
1747
+ <tt>kx=0..k_width-1</tt>, <tt>ky=0..k_height-1</tt>,
1748
+ <tt>i=0..in_channels-1</tt>, <tt>o=0..out_channels-1</tt>,
1749
+ <tt>x=0..out_width-1</tt>, <tt>y=0..out_height-1</tt>.
1750
+
1751
+ After the convolution is applied, the output is updated with bias and/or activation function as follows:
1752
+ <br><tt>Output(o,x,y) = ActivationFunction( Bias(o) + Output(o,x,y) )</tt>.
1753
+
1754
+ Dimensions must satisfy:
1755
+ <br><tt>in_width + 2 * x_padding = x_stride * ( out_width - 1 ) + k_width</tt>, and <tt>in_height + 2 * y_padding = y_stride * ( out_height - 1 ) + k_height</tt>.
1756
+ <br>A common use case is <tt>x_stride=y_stride=1</tt>, and <tt>x_padding=y_padding=0</tt>. In that case, <tt>in_width = out_width + k_width - 1</tt>, and <tt>in_height = out_height + k_height - 1</tt>.
1757
+
1758
+ Padding is a border of 0 values virtually added to the input image.
1759
+
1760
+ Coefficient <tt>Weight(o,i,kx,ky)</tt> for output image <tt>o=0..out_channels-1</tt>, input image <tt>i=0..in_channels-1</tt>, and kernel point (kx,ky) is
1761
+ stored in <tt>weights[kx + k_width * (ky + k_height * (i + in_channels * o))]</tt>, where
1762
+ the convolution kernel dimensions are <tt>k_width,k_height</tt>.
1763
+
1764
+ @field x_stride X increment in the input image
1765
+ @field y_stride Y increment in the input image
1766
+ @field x_padding X padding, virtual 0 values added to the left and right of each channel of the input stack
1767
+ @field y_padding Y padding, virtual 0 values added to the top and bottom of each channel of the input stack
1768
+ @field k_width Width of the convolution kernel
1769
+ @field k_height Height of the convolution kernel
1770
+ @field in_channels Number of input channels
1771
+ @field out_channels Number of output channels
1772
+ @field weights Convolution weights, <tt>k_width * k_height * in_channels * out_channels</tt> values, with the layout described in the discussion
1773
+ @field bias Layer bias, <tt>out_channels</tt> values, one for each output channel
1774
+ @field activation Layer activation function
1775
+
1776
+ */
1777
+ typedef struct __API_DEPRECATED_WITH_REPLACEMENT("BNNSLayerParametersConvolution", macos(10.12, 11.0), ios(10.0, 14.0), watchos(3.0, 7.0), tvos(10.0, 14.0)) {
1778
+
1779
+ size_t x_stride;
1780
+ size_t y_stride;
1781
+ size_t x_padding;
1782
+ size_t y_padding;
1783
+ size_t k_width;
1784
+ size_t k_height;
1785
+ size_t in_channels;
1786
+ size_t out_channels;
1787
+
1788
+ BNNSLayerData weights;
1789
+ BNNSLayerData bias;
1790
+ BNNSActivation activation;
1791
+
1792
+ } BNNSConvolutionLayerParameters
1793
+ __API_DEPRECATED_WITH_REPLACEMENT("BNNSLayerParametersConvolution", macos(10.12, 11.0), ios(10.0, 14.0), watchos(3.0, 7.0), tvos(10.0, 14.0));
1794
+
1795
+ /*!
1796
+
1797
+ @abstract Fully connected layer parameters (DEPRECATED, Use BNNSLayerParametersFullyConnected)
1798
+
1799
+ @discussion
1800
+ The output of a fully connected layer is the result of a matrix-vector product.
1801
+ The output vector is defined by <tt>Output(o) = &sum;<sub>i</sub> Weight(o,i) * Input(i)</tt> for <tt>i=0..in_size-1</tt>, <tt>o=0..out_size-1</tt>.
1802
+
1803
+ Coefficient <tt>Weight(o,i)</tt> is stored in <tt>weights[i + o * in_size]</tt>.
1804
+
1805
+ After the matrix product, the output is updated with bias and/or activation function as follows:
1806
+ <br><tt>Output(o) = ActivationFunction( Bias(o) + Output(o) )</tt>.
1807
+
1808
+ @field in_size Size of input vector
1809
+ @field out_size Size of output vector
1810
+ @field weights Matrix coefficients, <tt>in_size * out_size</tt> values, with the layout described in the discussion
1811
+ @field bias Layer bias, <tt>out_size</tt> values, one for each output component
1812
+ @field activation Layer activation function
1813
+
1814
+ */
1815
+ typedef struct __API_DEPRECATED_WITH_REPLACEMENT("BNNSLayerParametersFullyConnected", macos(10.12, 11.0), ios(10.0, 14.0), watchos(3.0, 7.0), tvos(10.0, 14.0)) {
1816
+ size_t in_size;
1817
+ size_t out_size;
1818
+
1819
+ BNNSLayerData weights;
1820
+ BNNSLayerData bias;
1821
+ BNNSActivation activation;
1822
+
1823
+ } BNNSFullyConnectedLayerParameters
1824
+ __API_DEPRECATED_WITH_REPLACEMENT("BNNSLayerParametersFullyConnected", macos(10.12, 11.0), ios(10.0, 14.0), watchos(3.0, 7.0), tvos(10.0, 14.0));
1825
+
1826
+ /*!
1827
+ @abstract Pooling layer parameters (DEPRECATED, Use BNNSLayerParametersPooling)
1828
+
1829
+ @discussion
1830
+ The pooling is defined as follows. Pixel <tt>Output(o,x,y)</tt> of the output image stack receives:
1831
+ <br><tt>Output(o,x,y) = PoolingFunction( Input(o,x_stride * x + kx,y_stride * y + ky) )</tt> with <tt>kx=0..k_width-1</tt>, <tt>ky=0..k_height-1</tt>,
1832
+ <tt>o=0..out_channels-1</tt>, <tt>x=0..out_width-1</tt>, <tt>y=0..out_height-1</tt>.
1833
+
1834
+ After the pooling is applied, the output is updated with bias and/or activation function as follows:
1835
+ <br><tt>Output(o,x,y) = ActivationFunction( Bias(o) + Output(o,x,y) )</tt>.
1836
+
1837
+ Dimensions must satisfy:
1838
+ <br><tt>in_width + 2 * x_padding >= x_stride * (out_width - 1) + 1</tt>,
1839
+ <br><tt>iin_height + 2 * y_padding >= p->y_stride * (o->height - 1) + 1</tt>.
1840
+
1841
+ Padding is a border of 0 values virtually added to the input image.
1842
+
1843
+ @field x_stride X increment in the input image
1844
+ @field y_stride Y increment in the input image
1845
+ @field x_padding X padding, virtual 0 values added to the left and right of each channel of the input stack
1846
+ @field y_padding Y padding, virtual 0 values added to the top and bottom of each channel of the input stack
1847
+ @field k_width Width of the pooling kernel
1848
+ @field k_height Height of the pooling kernel
1849
+ @field in_channels Number of input channels
1850
+ @field out_channels Number of output channels
1851
+ @field pooling_function Selects the pooling function to apply to each sample
1852
+ @field bias Layer bias, <tt>out_channels</tt> values
1853
+ @field activation Layer activation function
1854
+ */
1855
+
1856
+ typedef struct __API_DEPRECATED_WITH_REPLACEMENT("BNNSLayerParametersPooling", macos(10.12, 11.0), ios(10.0, 14.0), watchos(3.0, 7.0), tvos(10.0, 14.0)) {
1857
+ size_t x_stride;
1858
+ size_t y_stride;
1859
+ size_t x_padding;
1860
+ size_t y_padding;
1861
+ size_t k_width;
1862
+ size_t k_height;
1863
+ size_t in_channels;
1864
+ size_t out_channels;
1865
+
1866
+ BNNSPoolingFunction pooling_function;
1867
+ BNNSLayerData bias;
1868
+ BNNSActivation activation;
1869
+
1870
+ } BNNSPoolingLayerParameters
1871
+ __API_DEPRECATED_WITH_REPLACEMENT("BNNSLayerParametersPooling", macos(10.12, 11.0), ios(10.0, 14.0), watchos(3.0, 7.0), tvos(10.0, 14.0));
1872
+
1873
+ #pragma mark - Filter parameters
1874
+
1875
+ /*!
1876
+
1877
+ @abstract Common filter parameters
1878
+
1879
+ @field flags
1880
+ A logical OR of zero or more values from BNNSFlags.
1881
+
1882
+ @field n_threads
1883
+ If 0, use the best number of threads for the current machine.
1884
+ Otherwise, specifies the number of worker threads to execute.
1885
+
1886
+ @field alloc_memory
1887
+ If not NULL, will be called to allocate memory. Otherwise, posix_memalign() will be called.
1888
+ Must be compatible with the free_memory function.
1889
+
1890
+ @field free_memory
1891
+ If not NULL, will be called to deallocate memory. Otherwise, free() will be called.
1892
+ Must be compatible with the alloc_memory function.
1893
+
1894
+ */
1895
+ typedef struct {
1896
+
1897
+ uint32_t flags;
1898
+ size_t n_threads;
1899
+ BNNSAlloc _Nullable alloc_memory;
1900
+ BNNSFree _Nullable free_memory;
1901
+
1902
+ } BNNSFilterParameters;
1903
+
1904
+ #if !__has_include( <Availability.h> )
1905
+ # undef __API_DEPRECATED_WITH_REPLACEMENT
1906
+ # undef __API_DEPRECATED
1907
+ #endif
1908
+
1909
+ #if __has_feature(assume_nonnull)
1910
+ _Pragma("clang assume_nonnull end")
1911
+ #else
1912
+ # undef _Nullable
1913
+ # undef _Null_unspecified
1914
+ # undef _Nonnull
1915
+ #endif
1916
+
1917
+ #endif /* __BNNS_STRUCTURES_HEADER__ */