use cubecl_core::prelude::*;
use cubecl_core::{self as cubecl};
use crate::components::global::memory::GlobalMemoryConfig;
use crate::components::{AccG, error::MatmulSetupError};
use crate::components::{
AvailableLineSizes, MatmulPrecision, MatmulProblem, MatrixLayout, TilingScheme,
global::{PlaneRoleConfig, SpecializedLoadingSides, multi_stage::EventLoadingMode},
stage::StageConfig,
};
use crate::components::{LhsG, MatmulIdent, MatmulLineSizes, MatmulSelection, RhsG};
use crate::components::{global::RoleRuleConfig, stage::StageMemoryConfig};
use cubecl_std::{
CubeOption,
tensor::{View, layout::Coords2d},
};
use std::{fmt::Debug, hash::Hash};
use super::read::ReaderMode;
pub trait GlobalMatmulFamily: Send + Sync + 'static {
type Matmul<MP: MatmulPrecision>: GlobalMatmul<MP, Config = Self::Config>;
type Config: GlobalConfig;
fn setup<MP: MatmulPrecision, R: Runtime>(
client: &ComputeClient<R::Server>,
problem: &MatmulProblem,
selection: &MatmulSelection,
matmul_line_sizes: &MatmulLineSizes,
) -> Result<Self::Config, MatmulSetupError>;
fn filter_line_sizes(available_line_sizes: AvailableLineSizes) -> AvailableLineSizes {
available_line_sizes
}
}
#[cube]
pub trait GlobalMatmul<MP: MatmulPrecision>: 'static + Send + Sync {
type Config: GlobalConfig;
type LhsGlobalReader: CubeType;
type RhsGlobalReader: CubeType;
type AccGlobalReader: CubeType;
type GlobalWriter: CubeType;
type Accumulators: CubeType;
fn execute(
lhs_reader: Self::LhsGlobalReader,
rhs_reader: Self::RhsGlobalReader,
acc_reader: Self::AccGlobalReader,
writer: Self::GlobalWriter,
acc: &mut Self::Accumulators,
k_range: (u32, u32),
#[comptime] config: Self::Config,
);
fn init_lhs_global_reader(
lhs: View<Line<LhsG<MP>>, Coords2d>,
#[comptime] config: Self::Config,
) -> Self::LhsGlobalReader;
fn init_rhs_global_reader(
rhs: View<Line<RhsG<MP>>, Coords2d>,
#[comptime] config: Self::Config,
) -> Self::RhsGlobalReader;
fn init_acc_global_reader(
acc: CubeOption<View<Line<AccG<MP>>, Coords2d>>,
#[comptime] config: Self::Config,
) -> Self::AccGlobalReader;
fn init_accumulators(#[comptime] config: Self::Config) -> Self::Accumulators;
fn init_global_writer(
out: View<Line<AccG<MP>>, Coords2d, ReadWrite>,
#[comptime] config: Self::Config,
) -> Self::GlobalWriter;
}
pub trait GlobalConfig:
Copy + Clone + Eq + PartialEq + Hash + Debug + Send + Sync + 'static
{
type StageConfig: StageConfig;
fn stage_config(&self) -> Self::StageConfig;
fn stage_memory_config(&self, ident: MatmulIdent) -> StageMemoryConfig {
self.stage_config().stage_memory_config(ident.into_stage())
}
fn global_memory_config(&self, ident: MatmulIdent) -> GlobalMemoryConfig {
GlobalMemoryConfig {
elements_in_tile_row: self.tiling_scheme().elements_in_tile_row(ident),
elements_in_tile_col: self.tiling_scheme().elements_in_tile_col(ident),
elements_in_stage_row: self.tiling_scheme().elements_in_stage_row(ident),
elements_in_stage_col: self.tiling_scheme().elements_in_stage_col(ident),
global_line_size: self.global_line_size(ident),
check_row_bounds: self.check_row_bounds(ident),
check_col_bounds: self.check_col_bounds(ident),
matrix_layout: self.matrix_layout(ident),
}
}
fn global_line_size(&self, ident: MatmulIdent) -> u32;
fn tiling_scheme(&self) -> TilingScheme {
self.stage_config().tiling_scheme()
}
fn matrix_layout(&self, ident: MatmulIdent) -> MatrixLayout;
fn num_loading_planes(&self, ident: MatmulIdent) -> u32;
fn plane_role_config(&self) -> PlaneRoleConfig;
fn specialized_loading_sides(&self) -> SpecializedLoadingSides;
fn role_rule_config(&self) -> RoleRuleConfig {
self.plane_role_config().rule
}
fn plane_dim(&self) -> u32;
fn check_row_bounds(&self, ident: MatmulIdent) -> bool;
fn check_col_bounds(&self, ident: MatmulIdent) -> bool;
fn check_k_bounds(&self) -> bool;
fn precompute_job(&self) -> bool;
fn num_stages(&self, ident: MatmulIdent) -> u32;
fn reader_mode(&self) -> ReaderMode;
fn event_loading_mode(&self, ident: MatmulIdent) -> EventLoadingMode;
fn quantized(&self) -> bool {
self.stage_config().quantized()
}
fn cube_dim(&self) -> CubeDim;
}