use cubecl_core::prelude::*;
use cubecl_core::{self as cubecl};
use crate::components::error::MatmulSetupError;
use crate::components::{
AvailableLineSizes, InvalidConfigError, MatmulProblem, MatrixLayout, TileSize,
resource::ComputeResources,
tile::io::{Tile, TileKind},
};
use crate::components::{MatmulLineSizes, MatmulSelection};
use crate::components::{StageIdent, tile::io::TileMut};
use std::{fmt::Debug, hash::Hash};
pub trait TileMatmulFamily: Send + Sync + 'static {
type Matmul<L: Numeric, R: Numeric, A: Numeric>: TileMatmul<
L,
R,
A,
Config = Self::Config,
LhsTile = Self::LhsTile,
RhsTile = Self::RhsTile,
AccTile = Self::AccTile,
OutTile = Self::OutTile,
>;
type LhsTile: TileKind;
type RhsTile: TileKind;
type AccTile: TileKind;
type OutTile: TileKind<ReadWrite>;
type Config: TileConfig;
fn requires_accelerator() -> bool;
fn computation_resources() -> Result<ComputeResources, InvalidConfigError>;
fn setup<Lhs: Numeric, Rhs: Numeric, Acc: Numeric, 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 TileMatmul<L: Numeric, R: Numeric, A: Numeric>: 'static + Send + Sync {
type Config: TileConfig;
type LhsFragment: CubeType;
type RhsFragment: CubeType;
type AccFragment: CubeType;
type LhsTile: TileKind;
type RhsTile: TileKind;
type AccTile: TileKind;
type OutTile: TileKind<ReadWrite>;
fn execute(
lhs: &Self::LhsFragment,
rhs: &Self::RhsFragment,
out: &mut Self::AccFragment,
#[comptime] config: Self::Config,
);
fn allocate_lhs(#[comptime] config: Self::Config) -> Self::LhsFragment;
fn load_lhs<E: Numeric>(
tile: &Tile<Self::LhsTile, E>,
lhs: &mut Self::LhsFragment,
#[comptime] config: Self::Config,
);
fn allocate_rhs(#[comptime] config: Self::Config) -> Self::RhsFragment;
fn load_rhs<E: Numeric>(
tile: &Tile<Self::RhsTile, E>,
rhs: &mut Self::RhsFragment,
#[comptime] config: Self::Config,
);
fn allocate_acc(#[comptime] config: Self::Config) -> Self::AccFragment;
fn load_acc<E: Numeric>(
tile: &Tile<Self::AccTile, E>,
acc: &mut Self::AccFragment,
#[comptime] config: Self::Config,
);
fn write_results<E: Numeric>(
tile: &mut TileMut<Self::OutTile, E>,
out: &Self::AccFragment,
#[comptime] config: Self::Config,
);
}
pub trait TileConfig: Copy + Clone + Eq + PartialEq + Hash + Debug + Send + Sync + 'static {
fn plane_dim(&self) -> u32;
fn matrix_layout(&self, ident: StageIdent) -> MatrixLayout;
fn stage_line_size(&self, ident: StageIdent) -> u32;
fn global_line_size(&self, ident: StageIdent) -> u32;
fn tile_size(&self) -> &TileSize;
}