cubek_convolution/components/global/read/strategy/
async_full_strided.rs1use cubecl::{
2 prelude::*,
3 std::tensor::layout::{Layout, LayoutExpand},
4 {ir::DeviceProperties, prelude::barrier::Barrier},
5};
6use cubek_matmul::components::{
7 global::{
8 GlobalReaderConfig, PlaneFlowPartition,
9 memory::GlobalIterator,
10 multi_stage::LoadMaxRoundPlaneCount,
11 read::{
12 FullLoadingStrategy, LoadingJob, LoadingValidation, async_barrier::AsyncCopy,
13 async_full_strided::AsyncFullStridedLoading as MatmulStridedLoading,
14 stage::FullStageLayout,
15 },
16 },
17 stage::{StridedStageFamily, StridedStageMemory},
18};
19use cubek_matmul::definition::{MatmulElems, MatmulProblem};
20use cubek_std::{InvalidConfigError, StageIdent, tile::StridedTilingLayout};
21
22use crate::components::global::{
23 args::RuntimeArgs,
24 read::strategy::async_copy::{ASYNC_COPY_WIDTH, async_copy_from},
25};
26
27#[derive(CubeType, Clone, Copy)]
28pub struct AsyncFullStridedLoading {}
31
32impl LoadingValidation for AsyncFullStridedLoading {
33 fn validate_with_config(
34 device_props: &DeviceProperties,
35 config: &GlobalReaderConfig,
36 ) -> Result<(), InvalidConfigError> {
37 MatmulStridedLoading::validate_with_config(device_props, config)
38 }
39
40 fn validate_with_problem(
41 problem: &MatmulProblem,
42 dtypes: &MatmulElems,
43 ident: StageIdent,
44 ) -> Result<(), InvalidConfigError> {
45 MatmulStridedLoading::validate_with_problem(problem, dtypes, ident)
46 }
47}
48
49impl LoadMaxRoundPlaneCount for AsyncFullStridedLoading {
50 fn max_round_plane_count(
51 elements_per_tile: u32,
52 tiles_per_stage: u32,
53 vector_size: VectorSize,
54 plane_dim: u32,
55 dtype: StorageType,
56 ) -> u32 {
57 MatmulStridedLoading::max_round_plane_count(
58 elements_per_tile,
59 tiles_per_stage,
60 vector_size,
61 plane_dim,
62 dtype,
63 )
64 }
65}
66
67#[cube]
68impl FullLoadingStrategy<RuntimeArgs> for AsyncFullStridedLoading {
69 type TilingLayout = StridedTilingLayout;
70 type SyncStrategy = AsyncCopy;
71 type Job<EG: Numeric, NG: Size, ES: Numeric, NS: Size> = AsyncFullStridedJob;
72 type Stage = StridedStageFamily;
73 fn new_job<EG: Numeric, NG: Size, ES: Numeric, NS: Size>(
74 runtime_args: RuntimeArgs,
75 #[comptime] config: GlobalReaderConfig,
76 ) -> Self::Job<EG, NG, ES, NS> {
77 let type_size = ES::type_size_bits().comptime();
78 let vector_size = ASYNC_COPY_WIDTH / type_size as u32;
79 let num_stage_vectors = config.smem_config.elements_per_stage() / vector_size;
80 let unit_count = config.loading_planes_count() * config.plane_dim;
81 let num_tasks_per_unit = num_stage_vectors / unit_count;
82
83 let unit_position_base = PlaneFlowPartition::new(config.plane_flow_config.partition_rule)
84 .load_index(config.input_load_flow)
85 * config.plane_dim
86 + UNIT_POS_X;
87
88 AsyncFullStridedJob {
89 unit_position_base,
90 runtime_args,
91 num_tasks_per_unit,
92 unit_count,
93 copy_vector_size: vector_size,
94 }
95 }
96}
97
98#[derive(CubeType, Clone)]
99#[expand(derive(Clone))]
100pub struct AsyncFullStridedJob {
101 unit_position_base: u32,
102 runtime_args: RuntimeArgs,
103
104 #[cube(comptime)]
105 num_tasks_per_unit: u32,
106 #[cube(comptime)]
107 unit_count: u32,
108 #[cube(comptime)]
109 copy_vector_size: u32,
110}
111
112#[cube]
113impl<EG: Numeric, NG: Size, ES: Numeric, NS: Size>
114 LoadingJob<EG, NG, ES, NS, StridedTilingLayout, AsyncCopy> for AsyncFullStridedJob
115{
116 type Stage = StridedStageFamily;
117
118 fn execute_task(
119 this: &mut Self,
120 #[comptime] task_id: u32,
121 global_iter: &GlobalIterator<Vector<EG, NG>>,
122 stage: &mut StridedStageMemory<ES, NS, StridedTilingLayout>,
123 _barrier: &Shared<Barrier>,
124 #[comptime] config: GlobalReaderConfig,
125 ) {
126 let unit_position = this.unit_position_base + task_id * this.unit_count;
127 let unit_position_abs = unit_position * this.copy_vector_size;
128
129 let layout = FullStageLayout::new(config.smem_config);
130 let view = global_iter.view();
131
132 let pos = layout.to_source_pos(unit_position_abs);
133 let stage_offset = unit_position_abs / stage.smem.vector_size() as u32;
134
135 async_copy_from(
136 view,
137 pos,
138 stage,
139 stage_offset,
140 &this.runtime_args,
141 global_iter.offset(),
142 config,
143 this.copy_vector_size,
144 );
145 }
146
147 fn task_count(this: &Self) -> comptime_type!(u32) {
148 this.num_tasks_per_unit
149 }
150}