Modern Apps git

← Modern-Apps.git

ml: register-blocked fp16 ConvPoint, det/modnet/scrfd ConvPoint 3-4x on Pixel

Commit
4165c71adc5e75bb0aea10913a847d64d732ee11
Author
vayun-mathur <[email protected]>
When
2026-10-26 01:17 UTC (1 month ago)
Parents
317f9e1

Diff

appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.ktindex d606d14cc9..6e6d8dd7a4 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.kt@@ -231,8 +231,8 @@ object FDroidRepository {                 targetSdk = targetSdk,                 nativeCode = nativeCode,                 whatsNew = whatsNew,-                featuresPublished = featuresPublished,                 requiredFeatures = requiredFeatures,+                featuresPublished = featuresPublished,                 requiredSplitTypes = requiredSplitTypes,             )         }@@ -514,6 +514,8 @@ object FDroidRepository {         val nativeCode: List<String> = emptyList(),         val whatsNew: String? = null,         val requiredFeatures: List<String> = emptyList(),+        /** True when the index carried a `features` field at all, even an empty one. */+        val featuresPublished: Boolean = false,         val requiredSplitTypes: List<String> = emptyList(),     ) @@ -531,24 +533,25 @@ object FDroidRepository {         categories: List<String>,         tvScreenshots: List<String>,         requiredFeatures: List<String>,+        authoritative: Boolean = false,     ): Boolean? {         val names = requiredFeatures.map { it.lowercase() }         if (names.any { AppProvider.FEATURE_LEANBACK in it }) return true-        /** True when the index carried a `features` field at all, even an empty one. */-        val featuresPublished: Boolean = false,         if (categories.any { it.equals(TV_CATEGORY, ignoreCase = true) }) return true         if (tvScreenshots.isNotEmpty()) return true         if (AppProvider.requiresPhoneHardware(requiredFeatures)) return false         return if (authoritative) false else null     } +    /** Repos whose index is built from the APK manifest by this project's own server. */+    private val AUTHORITATIVE_TV_SOURCES = setOf(AppSource.MODERN_APPS, AppSource.PROPRIETARY)+     private const val TV_CATEGORY = "TV"      private val deviceAbis: List<String> by lazy { Build.SUPPORTED_ABIS?.toList().orEmpty() }      /**      * Pick the one APK to advertise for a package, newest release first.-        authoritative: Boolean = false,      *      * A release split per architecture used to resolve to whichever variant happened to be      * enumerated last, so an arm64 phone could be handed the x86_64 build. Everything before@@ -558,9 +561,6 @@ object FDroidRepository {      *      * Variants this device cannot run are dropped outright, so a package whose newest release      * has no build for this architecture falls back to an older one that does rather than-    /** Repos whose index is built from the APK manifest by this project's own server. */-    private val AUTHORITATIVE_TV_SOURCES = setOf(AppSource.MODERN_APPS, AppSource.PROPRIETARY)-      * disappearing.      */     internal fun selectVersion(
library/ml/src/main/rust/shaders/conv_point.comp
diff --git a/library/ml/src/main/rust/shaders/conv_point.comp b/library/ml/src/main/rust/shaders/conv_point.compindex 0c19e15e86..c719527131 100644--- a/library/ml/src/main/rust/shaders/conv_point.comp+++ b/library/ml/src/main/rust/shaders/conv_point.comp@@ -1,5 +1,5 @@ #version 450-//! A `1 x 1` convolution as a tiled matrix multiply, staging weights through shared memory.+//! A `1 x 1` convolution as a register-blocked matrix multiply. //! //! `out[o][p] = act(sum_i W[o][i] * in[i][p] + bias[o])`, where `p` runs over positions. //!@@ -17,15 +17,12 @@ //! //! # The tiling //!-//! One workgroup owns a [`TILE`] x [`TILE`] block of the output. It walks the input channels in-//! steps of [`TILE`], staging the weight block and the activation block into shared memory once-//! per step, and every invocation then reads those from shared memory instead of global. A weight-//! element is fetched once per workgroup rather than once per position, so traffic falls by up to-//! `TILE`.-//!-//! There are 64 invocations and `TILE * TILE` outputs in a tile, so each invocation owns a 2 x 2-//! block. That register blocking is what lets a 16-wide tile run in a 64-invocation workgroup, and-//! `local_size_x` is fixed at 64 in `common.glsl` for every shader.+//! Register-blocked like `conv_point_int8.comp`: each invocation owns four output channels by+//! four positions and the 64 of a workgroup tile a [`TILE`] x [`TILE`] block, with no shared+//! memory or barriers (on Mali those bought no reuse the cache did not already give). Weights are+//! read eight at a time through the `uvec4` view when rows are 16-byte aligned, activations four+//! positions at a time when the position count is a multiple of four. Each output sums its taps in+//! input-channel order from zero and adds the bias last, as the shared-memory version did. //! //! # Where it is not used //!@@ -35,82 +32,87 @@  #include "common.glsl" -/// Output channels and positions per workgroup, and input channels per staging step.-#define TILE 16u+/// The arena again, four fp16 elements per entry, for the wide activation loads.+layout(std430, binding = 0) readonly buffer ArenaQuad {+    f16vec4 arena_quad[];+};++/// Output channels and positions per workgroup: 8 x 8 invocations of 4 x 4 each.+///+/// **Must equal `CONV_POINT_TILE` in `nets`**, which counts the workgroups.+#define TILE 32u -/// The staged blocks. `TILE * TILE` each, so 2 KB of shared memory at fp32.-shared float weight_tile[TILE][TILE];-shared float input_tile[TILE][TILE];+/// Channels and positions per invocation.+#define BLOCK 4u  void main() {-    // The tile this workgroup owns. Uniform across the workgroup, so returning early cannot-    // strand a barrier — every invocation takes the same branch.     uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x;     if (tile >= p.count) {         return;     }     uint positions = p.out_h * p.out_w;     uint tiles_across = (positions + TILE - 1u) / TILE;-    uint channel_base = (tile / tiles_across) * TILE;-    uint position_base = (tile % tiles_across) * TILE;--    // This invocation's 2 x 2 corner of the tile.     uint lid = gl_LocalInvocationID.x;-    uint local_row = (lid >> 3u) * 2u;-    uint local_col = (lid & 7u) * 2u;--    float acc[2][2] = { { 0.0, 0.0 }, { 0.0, 0.0 } };--    for (uint k0 = 0u; k0 < p.in_c; k0 += TILE) {-        // Stage both blocks. 256 elements each, 64 invocations, so four apiece. Out-of-range-        // entries are zeroed rather than skipped, which lets the inner loop run unguarded.-        for (uint n = 0u; n < 4u; n++) {-            uint slot = lid + n * 64u;-            uint r = slot >> 4u;-            uint c = slot & 15u;+    uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK;+    uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK;+    if (position_base >= positions) {+        return;+    } -            uint channel = channel_base + r;-            uint k_weight = k0 + c;-            weight_tile[r][c] = (channel < p.out_c && k_weight < p.in_c)-                ? float(weights[p.weight + channel * p.in_c + k_weight])-                : 0.0;+    // Rows past the last channel read the last row; never stored.+    uint rows[BLOCK];+    vec4 sum[BLOCK];+    for (uint i = 0u; i < BLOCK; i++) {+        rows[i] = p.weight + min(channel_base + i, p.out_c - 1u) * p.in_c;+        sum[i] = vec4(0.0);+    } -            uint k_input = k0 + r;-            uint position = position_base + c;-            input_tile[r][c] = (k_input < p.in_c && position < positions)-                ? float(arena[p.in0 + k_input * positions + position])-                : 0.0;+    bool wide = (p.in_c & 7u) == 0u && (p.weight & 7u) == 0u+        && (positions & 3u) == 0u && (p.in0 & 3u) == 0u;+    if (wide) {+        uint quads = positions >> 2u;+        uint quad = (p.in0 + position_base) >> 2u;+        for (uint k = 0u; k < p.in_c; k += 8u) {+            uvec4 w[BLOCK];+            for (uint i = 0u; i < BLOCK; i++) {+                w[i] = weights128[(rows[i] + k) >> 3u];+            }+            for (uint t = 0u; t < 8u; t++) {+                vec4 x = vec4(arena_quad[quad + (k + t) * quads]);+                for (uint i = 0u; i < BLOCK; i++) {+                    float tap = unpackHalf2x16(w[i][t >> 1u])[t & 1u];+                    sum[i] += tap * x;+                }+            }         }-        barrier();--        for (uint k = 0u; k < TILE; k++) {-            float a0 = weight_tile[local_row][k];-            float a1 = weight_tile[local_row + 1u][k];-            float b0 = input_tile[k][local_col];-            float b1 = input_tile[k][local_col + 1u];-            acc[0][0] += a0 * b0;-            acc[0][1] += a0 * b1;-            acc[1][0] += a1 * b0;-            acc[1][1] += a1 * b1;+    } else {+        // Columns past the last position read as zero and are never stored.+        bvec4 live = lessThan(uvec4(position_base) + uvec4(0u, 1u, 2u, 3u), uvec4(positions));+        for (uint k = 0u; k < p.in_c; k++) {+            uint at = p.in0 + k * positions + position_base;+            vec4 x;+            for (uint j = 0u; j < BLOCK; j++) {+                x[j] = live[j] ? float(arena[at + j]) : 0.0;+            }+            for (uint i = 0u; i < BLOCK; i++) {+                sum[i] += float(weights[rows[i] + k]) * x;+            }         }-        // Both barriers are needed: the second stops the next step's staging from overwriting-        // tiles an invocation is still reading.-        barrier();     } -    for (uint i = 0u; i < 2u; i++) {-        uint channel = channel_base + local_row + i;+    for (uint i = 0u; i < BLOCK; i++) {+        uint channel = channel_base + i;         if (channel >= p.out_c) {             continue;         }         float offset = float(weights[p.bias + channel]);-        for (uint j = 0u; j < 2u; j++) {-            uint position = position_base + local_col + j;+        for (uint j = 0u; j < BLOCK; j++) {+            uint position = position_base + j;             if (position >= positions) {                 continue;             }             uint index = channel * positions + position;-            float value = fused_store(acc[i][j] + offset, p.act, channel, index);+            float value = fused_store(sum[i][j] + offset, p.act, channel, index);             arena[p.out_at + index] = float16_t(value);         }     }
library/ml/src/main/rust/src/nets/mod_part3.rs
diff --git a/library/ml/src/main/rust/src/nets/mod_part3.rs b/library/ml/src/main/rust/src/nets/mod_part3.rsindex 3cccc3c5b6..7b2a44e48c 100644--- a/library/ml/src/main/rust/src/nets/mod_part3.rs+++ b/library/ml/src/main/rust/src/nets/mod_part3.rs@@ -3,7 +3,7 @@ /// Both tiled shaders are dispatched one workgroup per tile, so [`Builder::emit`] has to know /// this to compute [`Push::count`]. Shared by the two so the fp16 and int8 lowerings cannot /// drift apart.-const CONV_POINT_TILE: u32 = 16;+const CONV_POINT_TILE: u32 = 32;  /// `TILE` in `shaders/conv_point_int8.comp`: output channels and positions per workgroup. ///
library/ml/src/main/rust/src/vulkan/parity_part6.rs
diff --git a/library/ml/src/main/rust/src/vulkan/parity_part6.rs b/library/ml/src/main/rust/src/vulkan/parity_part6.rsindex fec17e9401..ec2d056e08 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part6.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part6.rs@@ -642,4 +642,24 @@ fn row_int8_gemms_agree_with_the_reference() {         )), "{what} missed the row path");         compare(&format!("a row int8 gemm, {what}"), plan, source.data().to_vec(), &[&input]);     }-}\ No newline at end of file+}++/// `conv_point.comp`'s wide path (8 weights per `uvec4`, 4 positions per quad): 64 input+/// channels over 12x12 positions into 40 ragged output channels, with a fused residual.+#[test]+#[ignore = "needs a Vulkan device"]+fn a_wide_pointwise_conv_agrees_with_the_reference() {+    let input = spread(64 * 12 * 12, 1.5);+    let source = Invented::new(4);+    let plan = build(&source, &[Shape::new(64, 12, 12)], |b, ids| {+        let x = b.conv(ids[0], 0, 40, (1, 1), (1, 1), (1, 1), (0, 0, 0, 0), 1, Act::Relu);+        let y = b.conv(x, 2, 40, (1, 1), (1, 1), (1, 1), (0, 0, 0, 0), 1, Act::None);+        b.add(x, y)+    });+    assert!(plan.ops.iter().any(|op| matches!(+        op,+        crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPoint, push, .. }+            if push.weight % 8 == 0 && push.in_c % 8 == 0+    )));+    compare("a wide pointwise conv", plan, source.into_data(), &[&input]);+}