/*===- TableGen'erated file -------------------------------------*- C++ -*-===*\ |* *| |* Interface Declarations *| |* *| |* Automatically generated file, do not edit! *| |* *| \*===----------------------------------------------------------------------===*/ namespace mlir::shard { /// Interface for allowing operations to expose information needed to /// shard them. class ShardingInterface; } // namespace mlir::shard namespace mlir::shard { /// Interface for allowing operations to expose information needed to /// shard them. namespace detail { struct ShardingInterfaceInterfaceTraits { struct Concept { /// The methods defined by the interface. SmallVector (*getLoopIteratorTypes)(const Concept *impl, ::mlir::Operation *); SmallVector (*getReductionLoopIteratorKinds)(const Concept *impl, ::mlir::Operation *); SmallVector (*getIndexingMaps)(const Concept *impl, ::mlir::Operation *); FailureOr (*getShardingOption)(const Concept *impl, ::mlir::Operation *, ArrayRef, ArrayRef); FailureOr> (*getShardingAnnotations)(const Concept *impl, ::mlir::Operation *, const ShardingOption &); LogicalResult (*addShardingAnnotations)(const Concept *impl, ::mlir::Operation *, OpBuilder &, const ShardingOption &); LogicalResult (*partition)(const Concept *impl, ::mlir::Operation *, ArrayRef, ArrayRef, ArrayRef, IRMapping&, SymbolTableCollection &, OpBuilder &); }; template class Model : public Concept { public: using Interface = ::mlir::shard::ShardingInterface; Model() : Concept{getLoopIteratorTypes, getReductionLoopIteratorKinds, getIndexingMaps, getShardingOption, getShardingAnnotations, addShardingAnnotations, partition} {} static inline SmallVector getLoopIteratorTypes(const Concept *impl, ::mlir::Operation *tablegen_opaque_val); static inline SmallVector getReductionLoopIteratorKinds(const Concept *impl, ::mlir::Operation *tablegen_opaque_val); static inline SmallVector getIndexingMaps(const Concept *impl, ::mlir::Operation *tablegen_opaque_val); static inline FailureOr getShardingOption(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, ArrayRef operandShardings, ArrayRef resultShardings); static inline FailureOr> getShardingAnnotations(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, const ShardingOption & shardingOption); static inline LogicalResult addShardingAnnotations(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, OpBuilder & b, const ShardingOption & shardingOption); static inline LogicalResult partition(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, ArrayRef partitionedOperands, ArrayRef operandShardings, ArrayRef resultShardings, IRMapping& partitionMap, SymbolTableCollection & symbolTableCollection, OpBuilder & builder); }; template class FallbackModel : public Concept { public: using Interface = ::mlir::shard::ShardingInterface; FallbackModel() : Concept{getLoopIteratorTypes, getReductionLoopIteratorKinds, getIndexingMaps, getShardingOption, getShardingAnnotations, addShardingAnnotations, partition} {} static inline SmallVector getLoopIteratorTypes(const Concept *impl, ::mlir::Operation *tablegen_opaque_val); static inline SmallVector getReductionLoopIteratorKinds(const Concept *impl, ::mlir::Operation *tablegen_opaque_val); static inline SmallVector getIndexingMaps(const Concept *impl, ::mlir::Operation *tablegen_opaque_val); static inline FailureOr getShardingOption(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, ArrayRef operandShardings, ArrayRef resultShardings); static inline FailureOr> getShardingAnnotations(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, const ShardingOption & shardingOption); static inline LogicalResult addShardingAnnotations(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, OpBuilder & b, const ShardingOption & shardingOption); static inline LogicalResult partition(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, ArrayRef partitionedOperands, ArrayRef operandShardings, ArrayRef resultShardings, IRMapping& partitionMap, SymbolTableCollection & symbolTableCollection, OpBuilder & builder); }; template class ExternalModel : public FallbackModel { public: using ConcreteEntity = ConcreteOp; SmallVector getLoopIteratorTypes(::mlir::Operation *tablegen_opaque_val) const; SmallVector getReductionLoopIteratorKinds(::mlir::Operation *tablegen_opaque_val) const; SmallVector getIndexingMaps(::mlir::Operation *tablegen_opaque_val) const; FailureOr getShardingOption(::mlir::Operation *tablegen_opaque_val, ArrayRef operandShardings, ArrayRef resultShardings) const; FailureOr> getShardingAnnotations(::mlir::Operation *tablegen_opaque_val, const ShardingOption &shardingOption) const; LogicalResult addShardingAnnotations(::mlir::Operation *tablegen_opaque_val, OpBuilder &b, const ShardingOption &shardingOption) const; LogicalResult partition(::mlir::Operation *tablegen_opaque_val, ArrayRef partitionedOperands, ArrayRef operandShardings, ArrayRef resultShardings, IRMapping&partitionMap, SymbolTableCollection &symbolTableCollection, OpBuilder &builder) const; }; }; template struct ShardingInterfaceTrait; } // namespace detail class ShardingInterface : public ::mlir::OpInterface { public: using ::mlir::OpInterface::OpInterface; template struct Trait : public detail::ShardingInterfaceTrait {}; /// Returns a list of iterator types that describe the number of loops. /// The iterator types determine how the operation traverses its input and /// output tensors. /// /// Example 1: A gemm op has 3 loops, M, N and K. Their loop iterator /// types are parallel, parallel, reduction. This indicates that M and /// N are traversed in parallel, while the K dimension is used for /// reduction. SmallVector getLoopIteratorTypes(); /// Return the kind of all reduction loop iterators. /// The order is the same as the same as the result from /// `getLoopIteratorTypes`. /// /// Example 1: /// iterator types = (parallel, reduction, parallel, reduction) /// || || /// reduction kinds = ( sum, max) /// /// Example 2: /// A softmax op's loop iterator types are parallel and /// reduction. /// The reduction iterator will be of kind `generic`, since it is non of /// the available presets. SmallVector getReductionLoopIteratorKinds(); /// Return the indexing maps attribute within the current operation. /// Indexing maps determine how indices in the iteration space map to /// tensor indices. They are specified using `affine_map` in MLIR, which /// provides an affine transformation of indices. SmallVector getIndexingMaps(); /// Given that certain operands or results of the operation may have /// sharding annotations, this method leverages this information to /// deduce how the operation should be sharded. /// The passed sharding may be incomplete, this gives freedom for the /// op to select the most appropriate shardings for all the operands /// and results and the op itself. FailureOr getShardingOption(ArrayRef operandShardings, ArrayRef resultShardings); /// Based on a given ShardingOption, get the operand and result /// operations for the operands and results sharding annotations. /// This is what shardings the operands and results need to have in order /// to shard the op according to shardingOption. FailureOr> getShardingAnnotations(const ShardingOption & shardingOption); /// Based on a given ShardingOption, this method adds `shard.shard` /// operations for the operands and results that previously lacked /// sharding annotations. LogicalResult addShardingAnnotations(OpBuilder & b, const ShardingOption & shardingOption); /// Convert self to SPMD form. /// This method is used during the partition pass of a program fully /// annotated with shardings. /// /// The partition algorithm would read the surrounding sharding /// annotations from the IR for each argument/result and prepare /// `operandShardings` and `resultShardings`. /// Values that are not ranked tensors do not have sharding annotations. /// In this case their corresponding Sharding is null. /// /// For convenience it will also prepare `partitionedOperands`, although /// they can be retrieved from the `partitionMap`. /// /// The `partitionMap` contains a mapping from unsharded to /// sharded/partitioned values that are constructed during the partition /// pass. The interface implementation must populate `partitionMap` /// with the mapping for this op's results. /// /// `builder` is set to insert new operations in the appropriate point. /// The implementation should not return the builder to the original /// insertion point. /// It should leave it as is after all insertions are done. /// /// The default implementation does full replication. /// This assumes that all sharding annotations are for full replication. LogicalResult partition(ArrayRef partitionedOperands, ArrayRef operandShardings, ArrayRef resultShardings, IRMapping& partitionMap, SymbolTableCollection & symbolTableCollection, OpBuilder & builder); LogicalResult verifyShardingInterfaceImpl(); void printLoopTypesAndIndexingMaps(raw_ostream &os); }; } // namespace mlir::shard namespace mlir::shard::detail { template struct ShardingInterfaceTrait : public ::mlir::OpInterface::Trait { /// Returns a list of iterator types that describe the number of loops. /// The iterator types determine how the operation traverses its input and /// output tensors. /// /// Example 1: A gemm op has 3 loops, M, N and K. Their loop iterator /// types are parallel, parallel, reduction. This indicates that M and /// N are traversed in parallel, while the K dimension is used for /// reduction. SmallVector getLoopIteratorTypes() { return {}; } /// Return the kind of all reduction loop iterators. /// The order is the same as the same as the result from /// `getLoopIteratorTypes`. /// /// Example 1: /// iterator types = (parallel, reduction, parallel, reduction) /// || || /// reduction kinds = ( sum, max) /// /// Example 2: /// A softmax op's loop iterator types are parallel and /// reduction. /// The reduction iterator will be of kind `generic`, since it is non of /// the available presets. SmallVector getReductionLoopIteratorKinds() { return {}; } /// Return the indexing maps attribute within the current operation. /// Indexing maps determine how indices in the iteration space map to /// tensor indices. They are specified using `affine_map` in MLIR, which /// provides an affine transformation of indices. SmallVector getIndexingMaps() { return {}; } /// Given that certain operands or results of the operation may have /// sharding annotations, this method leverages this information to /// deduce how the operation should be sharded. /// The passed sharding may be incomplete, this gives freedom for the /// op to select the most appropriate shardings for all the operands /// and results and the op itself. FailureOr getShardingOption(ArrayRef operandShardings, ArrayRef resultShardings) { return detail::defaultGetShardingOption( (*static_cast(this)).getOperation(), operandShardings, resultShardings); } /// Based on a given ShardingOption, get the operand and result /// operations for the operands and results sharding annotations. /// This is what shardings the operands and results need to have in order /// to shard the op according to shardingOption. FailureOr> getShardingAnnotations(const ShardingOption & shardingOption) { return detail::defaultGetShardingAnnotations( (*static_cast(this)).getOperation(), shardingOption); } /// Based on a given ShardingOption, this method adds `shard.shard` /// operations for the operands and results that previously lacked /// sharding annotations. LogicalResult addShardingAnnotations(OpBuilder & b, const ShardingOption & shardingOption) { return detail::defaultAddShardingAnnotations( (*static_cast(this)).getOperation(), b, shardingOption); } /// Convert self to SPMD form. /// This method is used during the partition pass of a program fully /// annotated with shardings. /// /// The partition algorithm would read the surrounding sharding /// annotations from the IR for each argument/result and prepare /// `operandShardings` and `resultShardings`. /// Values that are not ranked tensors do not have sharding annotations. /// In this case their corresponding Sharding is null. /// /// For convenience it will also prepare `partitionedOperands`, although /// they can be retrieved from the `partitionMap`. /// /// The `partitionMap` contains a mapping from unsharded to /// sharded/partitioned values that are constructed during the partition /// pass. The interface implementation must populate `partitionMap` /// with the mapping for this op's results. /// /// `builder` is set to insert new operations in the appropriate point. /// The implementation should not return the builder to the original /// insertion point. /// It should leave it as is after all insertions are done. /// /// The default implementation does full replication. /// This assumes that all sharding annotations are for full replication. LogicalResult partition(ArrayRef partitionedOperands, ArrayRef operandShardings, ArrayRef resultShardings, IRMapping& partitionMap, SymbolTableCollection & symbolTableCollection, OpBuilder & builder) { partitionFullyReplicatedOperation( *(*static_cast(this)).getOperation(), partitionedOperands, operandShardings, resultShardings, partitionMap, symbolTableCollection, builder); return success(); } }; } // namespace mlir::shard::detail namespace mlir::shard { template SmallVector detail::ShardingInterfaceInterfaceTraits::Model::getLoopIteratorTypes(const Concept *impl, ::mlir::Operation *tablegen_opaque_val) { return (llvm::cast(tablegen_opaque_val)).getLoopIteratorTypes(); } template SmallVector detail::ShardingInterfaceInterfaceTraits::Model::getReductionLoopIteratorKinds(const Concept *impl, ::mlir::Operation *tablegen_opaque_val) { return (llvm::cast(tablegen_opaque_val)).getReductionLoopIteratorKinds(); } template SmallVector detail::ShardingInterfaceInterfaceTraits::Model::getIndexingMaps(const Concept *impl, ::mlir::Operation *tablegen_opaque_val) { return (llvm::cast(tablegen_opaque_val)).getIndexingMaps(); } template FailureOr detail::ShardingInterfaceInterfaceTraits::Model::getShardingOption(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, ArrayRef operandShardings, ArrayRef resultShardings) { return (llvm::cast(tablegen_opaque_val)).getShardingOption(operandShardings, resultShardings); } template FailureOr> detail::ShardingInterfaceInterfaceTraits::Model::getShardingAnnotations(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, const ShardingOption & shardingOption) { return (llvm::cast(tablegen_opaque_val)).getShardingAnnotations(shardingOption); } template LogicalResult detail::ShardingInterfaceInterfaceTraits::Model::addShardingAnnotations(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, OpBuilder & b, const ShardingOption & shardingOption) { return (llvm::cast(tablegen_opaque_val)).addShardingAnnotations(b, shardingOption); } template LogicalResult detail::ShardingInterfaceInterfaceTraits::Model::partition(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, ArrayRef partitionedOperands, ArrayRef operandShardings, ArrayRef resultShardings, IRMapping& partitionMap, SymbolTableCollection & symbolTableCollection, OpBuilder & builder) { return (llvm::cast(tablegen_opaque_val)).partition(partitionedOperands, operandShardings, resultShardings, partitionMap, symbolTableCollection, builder); } template SmallVector detail::ShardingInterfaceInterfaceTraits::FallbackModel::getLoopIteratorTypes(const Concept *impl, ::mlir::Operation *tablegen_opaque_val) { return static_cast(impl)->getLoopIteratorTypes(tablegen_opaque_val); } template SmallVector detail::ShardingInterfaceInterfaceTraits::FallbackModel::getReductionLoopIteratorKinds(const Concept *impl, ::mlir::Operation *tablegen_opaque_val) { return static_cast(impl)->getReductionLoopIteratorKinds(tablegen_opaque_val); } template SmallVector detail::ShardingInterfaceInterfaceTraits::FallbackModel::getIndexingMaps(const Concept *impl, ::mlir::Operation *tablegen_opaque_val) { return static_cast(impl)->getIndexingMaps(tablegen_opaque_val); } template FailureOr detail::ShardingInterfaceInterfaceTraits::FallbackModel::getShardingOption(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, ArrayRef operandShardings, ArrayRef resultShardings) { return static_cast(impl)->getShardingOption(tablegen_opaque_val, operandShardings, resultShardings); } template FailureOr> detail::ShardingInterfaceInterfaceTraits::FallbackModel::getShardingAnnotations(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, const ShardingOption & shardingOption) { return static_cast(impl)->getShardingAnnotations(tablegen_opaque_val, shardingOption); } template LogicalResult detail::ShardingInterfaceInterfaceTraits::FallbackModel::addShardingAnnotations(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, OpBuilder & b, const ShardingOption & shardingOption) { return static_cast(impl)->addShardingAnnotations(tablegen_opaque_val, b, shardingOption); } template LogicalResult detail::ShardingInterfaceInterfaceTraits::FallbackModel::partition(const Concept *impl, ::mlir::Operation *tablegen_opaque_val, ArrayRef partitionedOperands, ArrayRef operandShardings, ArrayRef resultShardings, IRMapping& partitionMap, SymbolTableCollection & symbolTableCollection, OpBuilder & builder) { return static_cast(impl)->partition(tablegen_opaque_val, partitionedOperands, operandShardings, resultShardings, partitionMap, symbolTableCollection, builder); } template SmallVector detail::ShardingInterfaceInterfaceTraits::ExternalModel::getLoopIteratorTypes(::mlir::Operation *tablegen_opaque_val) const { return {}; } template SmallVector detail::ShardingInterfaceInterfaceTraits::ExternalModel::getReductionLoopIteratorKinds(::mlir::Operation *tablegen_opaque_val) const { return {}; } template SmallVector detail::ShardingInterfaceInterfaceTraits::ExternalModel::getIndexingMaps(::mlir::Operation *tablegen_opaque_val) const { return {}; } template FailureOr detail::ShardingInterfaceInterfaceTraits::ExternalModel::getShardingOption(::mlir::Operation *tablegen_opaque_val, ArrayRef operandShardings, ArrayRef resultShardings) const { return detail::defaultGetShardingOption( (llvm::cast(tablegen_opaque_val)).getOperation(), operandShardings, resultShardings); } template FailureOr> detail::ShardingInterfaceInterfaceTraits::ExternalModel::getShardingAnnotations(::mlir::Operation *tablegen_opaque_val, const ShardingOption &shardingOption) const { return detail::defaultGetShardingAnnotations( (llvm::cast(tablegen_opaque_val)).getOperation(), shardingOption); } template LogicalResult detail::ShardingInterfaceInterfaceTraits::ExternalModel::addShardingAnnotations(::mlir::Operation *tablegen_opaque_val, OpBuilder &b, const ShardingOption &shardingOption) const { return detail::defaultAddShardingAnnotations( (llvm::cast(tablegen_opaque_val)).getOperation(), b, shardingOption); } template LogicalResult detail::ShardingInterfaceInterfaceTraits::ExternalModel::partition(::mlir::Operation *tablegen_opaque_val, ArrayRef partitionedOperands, ArrayRef operandShardings, ArrayRef resultShardings, IRMapping&partitionMap, SymbolTableCollection &symbolTableCollection, OpBuilder &builder) const { partitionFullyReplicatedOperation( *(llvm::cast(tablegen_opaque_val)).getOperation(), partitionedOperands, operandShardings, resultShardings, partitionMap, symbolTableCollection, builder); return success(); } } // namespace mlir::shard