@@ -42,6 +42,52 @@ bool q8ta_conv2d_check_4w4c_packed_dim_info(const api::PackedDimInfo& info) {
4242 info.outer_packed_dim_block_size == 4 ;
4343}
4444
45+ namespace {
46+
47+ uint64_t q8ta_conv2d_im2col_scratch_limit (ComputeGraph& graph) {
48+ constexpr uint64_t kMaxBatchedIm2ColScratchBytes = 32ULL * 1024ULL * 1024ULL ;
49+ const uint64_t device_scratch_limit =
50+ graph.context ()->adapter_ptr ()->max_buffer_numel ();
51+ return device_scratch_limit < kMaxBatchedIm2ColScratchBytes
52+ ? device_scratch_limit
53+ : kMaxBatchedIm2ColScratchBytes ;
54+ }
55+
56+ bool should_use_q8ta_conv2d_im2col (
57+ ComputeGraph& graph,
58+ const int64_t batch,
59+ const int64_t groups,
60+ const int64_t in_channels_per_group,
61+ const int64_t flattened_kernel_size,
62+ const int64_t out_height,
63+ const int64_t out_width) {
64+ const bool im2col_eligible = in_channels_per_group % 4 == 0 ;
65+ if (!im2col_eligible) {
66+ return false ;
67+ }
68+
69+ const int64_t spatial_out = out_height * out_width;
70+ if (batch > 1 ) {
71+ constexpr int64_t kMinFlattenedKernelSize = 1024 ;
72+ constexpr int64_t kMaxSpatialOutput = 64 ;
73+ const uint64_t scratch_bytes = static_cast <uint64_t >(batch) *
74+ static_cast <uint64_t >(flattened_kernel_size) *
75+ static_cast <uint64_t >(out_height) *
76+ static_cast <uint64_t >(utils::align_up_4 (out_width));
77+ return groups == 1 && flattened_kernel_size >= kMinFlattenedKernelSize &&
78+ spatial_out <= kMaxSpatialOutput &&
79+ scratch_bytes <= q8ta_conv2d_im2col_scratch_limit (graph);
80+ }
81+
82+ if (graph.device_is_mali ()) {
83+ return true ;
84+ }
85+
86+ return groups == 1 && (in_channels_per_group >= 32 || spatial_out <= 4096 );
87+ }
88+
89+ } // namespace
90+
4591//
4692// Workgroup size selection functions
4793//
@@ -70,13 +116,16 @@ GlobalWorkGrid pick_q8ta_conv2d_gwg(
70116 const uint32_t W = graph->size_at <uint32_t >(-1 , output);
71117 const uint32_t H = graph->size_at <uint32_t >(-2 , output);
72118 const uint32_t C = graph->size_at <uint32_t >(-3 , output);
119+ const uint32_t N = graph->size_at <uint32_t >(-4 , output);
73120
74121 // Each thread processes 4 adjacent width positions and 4 channels (4Wx4C
75122 // tile)
76123 const uint32_t W4 = utils::div_up_4 (W);
77124 const uint32_t C4 = utils::div_up_4 (C);
78125
79- return GlobalWorkGrid ({W4 , H, C4 }, kTiledWorkGrid );
126+ return GlobalWorkGrid (
127+ {W4 , utils::safe_downcast<uint32_t >(static_cast <uint64_t >(H) * N), C4 },
128+ kTiledWorkGrid );
80129}
81130
82131/* *
@@ -466,33 +515,33 @@ void q8ta_conv2d_general(
466515
467516void q8ta_conv2d (ComputeGraph& graph, const std::vector<ValueRef>& args) {
468517 const ValueRef input = args.at (0 );
518+ const ValueRef kernel_size_ref = args.at (9 );
469519 const ValueRef groups_ref = args.at (13 );
470520 const ValueRef output = args.at (15 );
471521
472522 const int64_t groups = graph.extract_scalar <int64_t >(groups_ref);
473523 const int64_t in_channels = graph.size_at <int64_t >(-3 , input);
474524 const int64_t in_channels_per_group = in_channels / groups;
525+ const int64_t batch = graph.size_at <int64_t >(-4 , input);
475526
476527 const int64_t H_out = graph.size_at <int64_t >(-2 , output);
477528 const int64_t W_out = graph.size_at <int64_t >(-1 , output);
478- const int64_t spatial_out = H_out * W_out;
479-
480- // Im2col requires input channels per group to be a multiple of 4
481- const bool im2col_eligible = in_channels_per_group % 4 == 0 ;
482-
483- bool use_im2col = false ;
484- if (graph.device_is_mali ()) {
485- // On Mali, im2col is faster than the general shader across the board.
486- use_im2col = im2col_eligible;
487- } else {
488- // Default: on Adreno and unknown GPU architectures, im2col is only
489- // beneficial for ungrouped convolutions with sufficient channel depth or
490- // small spatial output. For grouped convolutions, the general shader is
491- // more efficient (0.7-0.95x regression measured on Adreno).
492- use_im2col = im2col_eligible && groups == 1 &&
493- (in_channels_per_group >= 32 || spatial_out <= 4096 );
529+ int64_t flattened_kernel_size;
530+ {
531+ const auto kernel_size = graph.get_int_list (kernel_size_ref);
532+ flattened_kernel_size = utils::align_up_4 (
533+ in_channels_per_group * kernel_size->at (0 ) * kernel_size->at (1 ));
494534 }
495535
536+ const bool use_im2col = should_use_q8ta_conv2d_im2col (
537+ graph,
538+ batch,
539+ groups,
540+ in_channels_per_group,
541+ flattened_kernel_size,
542+ H_out,
543+ W_out);
544+
496545 if (use_im2col) {
497546 q8ta_conv2d_im2col (graph, args);
498547 } else {
0 commit comments