[GlobalOptimization] Split constants larger than the max allocation size (#25242) A constant whose storage exceeds the max allocation size of its resource config cannot be packed within that limit. PackConstants places such constants in a separate resource, but the size will still exceed the limit. The problem only shows up at runtime, when the device fails to allocate the buffer. This adds `iree-global-opt-split-large-constants`. It splits a private, immutable global above the limit into shards along its outermost non-unit dimension. Each resulting shard is a contiguous byte range of the original constant and fits into one allocation. The initial value can be an inline dense constant, which is sliced, or a named parameter, which is not read: each shard refers to its byte range of the same parameter through the `offset` and `length` config entries, relative to any offset the parameter already has. The uses of its loads are rewritten to use the shards: * An input of a structured op whose split dimension is indexed by a single loop: the op is split along that loop with TilingInterface, one op per shard. The results of a parallel loop are inserted into the destination. The ops of a reduction loop accumulate into the previous result. This covers matmuls with a large weight, such as a tied LM head. * A value read with `tensor.extract` in the body of an elementwise `linalg.generic`, e.g., an embedding lookup: a chain of ops, one per shard. Each reads its shard at the rebased, clamped index and keeps the previous op's element unless the index lies in its shard. A constant with any other use is left unchanged with a warning, as is one that cannot be split: mutable or public globals, values that are neither inline dense constants nor named parameters, parameters whose length does not match their type or whose offset is not an integer, element types not stored at 8, 16, 32 or 64 bits, or a single index of the split dimension that alone exceeds the limit. The limit is the max allocation size from `ResourceConfigAttr::lookup`, so `--iree-stream-resource-max-allocation-size` unless the target sets one. It defaults to INT64_MAX, so the pass changes nothing unless a limit is set. The pass runs after transposes have been folded into their consumers, so that an oversized constant is read by the op that indexes it, and before constant expressions are hoisted and evaluated into new globals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Stefan Schuermans <schuermans@roofline.ai>
diff --git a/compiler/src/iree/compiler/GlobalOptimization/BUILD.bazel b/compiler/src/iree/compiler/GlobalOptimization/BUILD.bazel index 70adc3a..508af2e 100644 --- a/compiler/src/iree/compiler/GlobalOptimization/BUILD.bazel +++ b/compiler/src/iree/compiler/GlobalOptimization/BUILD.bazel
@@ -105,6 +105,7 @@ "RaiseSpecialOps.cpp", "RemoveZeroExtentTensors.cpp", "SimplifyPackUnpack.cpp", + "SplitLargeConstants.cpp", "TransposePropagationPatterns.cpp", "Utils.cpp", "WarnOnUninitializedValues.cpp", @@ -131,6 +132,7 @@ "//compiler/src/iree/compiler/Dialect/LinalgExt/IR", "//compiler/src/iree/compiler/Dialect/LinalgExt/Transforms", "//compiler/src/iree/compiler/Dialect/LinalgExt/Utils", + "//compiler/src/iree/compiler/Dialect/Stream/IR", "//compiler/src/iree/compiler/Dialect/TensorExt/IR", "//compiler/src/iree/compiler/Dialect/Util/Analysis", "//compiler/src/iree/compiler/Dialect/Util/Analysis/Attributes", @@ -165,6 +167,7 @@ "@llvm-project//mlir:TensorDialect", "@llvm-project//mlir:TensorTransforms", "@llvm-project//mlir:TensorUtils", + "@llvm-project//mlir:TilingInterface", "@llvm-project//mlir:TransformUtils", "@llvm-project//mlir:Transforms", ],
diff --git a/compiler/src/iree/compiler/GlobalOptimization/CMakeLists.txt b/compiler/src/iree/compiler/GlobalOptimization/CMakeLists.txt index 9b9c436..5b5afe6 100644 --- a/compiler/src/iree/compiler/GlobalOptimization/CMakeLists.txt +++ b/compiler/src/iree/compiler/GlobalOptimization/CMakeLists.txt
@@ -99,6 +99,7 @@ "RaiseSpecialOps.cpp" "RemoveZeroExtentTensors.cpp" "SimplifyPackUnpack.cpp" + "SplitLargeConstants.cpp" "TransposePropagationPatterns.cpp" "Utils.cpp" "WarnOnUninitializedValues.cpp" @@ -129,6 +130,7 @@ MLIRTensorDialect MLIRTensorTransforms MLIRTensorUtils + MLIRTilingInterface MLIRTransformUtils MLIRTransforms iree::compiler::Codegen::Common @@ -143,6 +145,7 @@ iree::compiler::Dialect::LinalgExt::IR iree::compiler::Dialect::LinalgExt::Transforms iree::compiler::Dialect::LinalgExt::Utils + iree::compiler::Dialect::Stream::IR iree::compiler::Dialect::TensorExt::IR iree::compiler::Dialect::Util::Analysis iree::compiler::Dialect::Util::Analysis::Attributes
diff --git a/compiler/src/iree/compiler/GlobalOptimization/Passes.cpp b/compiler/src/iree/compiler/GlobalOptimization/Passes.cpp index ba74a25..0732ce9 100644 --- a/compiler/src/iree/compiler/GlobalOptimization/Passes.cpp +++ b/compiler/src/iree/compiler/GlobalOptimization/Passes.cpp
@@ -240,6 +240,10 @@ .addPass(mlir::createCSEPass); mainPassManager.addPass( GlobalOptimization::createConvertStridedContractionToContractionPass()); + // After transposes have been folded into their consumers, so an oversized + // constant is read by the op that indexes it, and before constant + // expressions are hoisted and evaluated into new globals. + mainPassManager.addPass(GlobalOptimization::createSplitLargeConstantsPass()); // Enable data tiling after they are in a canonical form. if (transformOptions.earlyDataTiling) {
diff --git a/compiler/src/iree/compiler/GlobalOptimization/Passes.td b/compiler/src/iree/compiler/GlobalOptimization/Passes.td index f6cec53..5852e6c 100644 --- a/compiler/src/iree/compiler/GlobalOptimization/Passes.td +++ b/compiler/src/iree/compiler/GlobalOptimization/Passes.td
@@ -348,6 +348,41 @@ let summary = "Simplifies tensor pack and unpack ops."; } +def SplitLargeConstantsPass : + Pass<"iree-global-opt-split-large-constants", "mlir::ModuleOp"> { + let summary = "Splits constants larger than the max allocation size into shards."; + let description = [{ + A constant global whose storage exceeds the max allocation size of its + resource config (`--iree-stream-resource-max-allocation-size` unless the + target sets one) cannot be packed into a resource within that limit. This + pass splits such a private, immutable global with an inline dense value or + a named parameter into shards along its outermost non-unit dimension, so + each shard is a contiguous byte range of the original that fits into one + allocation. A shard of a named parameter refers to its range of the same + parameter through the `offset` and `length` config entries. The pass then + rewrites the uses of the global's loads to use the shards: + + * an input of a structured op whose split dimension is indexed by a + single loop: the op is split along that loop, one op per shard. The + results of a parallel loop are inserted into the destination, the ops + of a reduction loop accumulate into the result of the previous one. + This covers e.g. matmuls with a large weight such as a tied LM head. + * a value read with `tensor.extract` in the body of an elementwise + `linalg.generic`, e.g. an embedding lookup: one op per shard reads its + shard at the rebased, clamped index and keeps the previous op's + element unless the index lies in its shard. + + A constant with any other use is left unchanged with a warning. + }]; + let dependentDialects = [ + "mlir::arith::ArithDialect", + "mlir::linalg::LinalgDialect", + "mlir::tensor::TensorDialect", + "IREE::Stream::StreamDialect", + "IREE::Util::UtilDialect", + ]; +} + def GlobalLoopInvariantCodeMotionPass : InterfacePass<"iree-global-opt-loop-invariant-code-motion", "mlir::FunctionOpInterface"> { let summary = "Hoist loop invariants out of loops with zero-trip-check."; }
diff --git a/compiler/src/iree/compiler/GlobalOptimization/SplitLargeConstants.cpp b/compiler/src/iree/compiler/GlobalOptimization/SplitLargeConstants.cpp new file mode 100644 index 0000000..63db04e --- /dev/null +++ b/compiler/src/iree/compiler/GlobalOptimization/SplitLargeConstants.cpp
@@ -0,0 +1,629 @@ +// Copyright 2026 The IREE Authors +// +// Licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include "iree/compiler/Dialect/Flow/IR/FlowTypes.h" +#include "iree/compiler/Dialect/Stream/IR/StreamDialect.h" +#include "iree/compiler/Dialect/Stream/IR/StreamTypes.h" +#include "iree/compiler/Dialect/Util/IR/UtilDialect.h" +#include "iree/compiler/Dialect/Util/IR/UtilOps.h" +#include "iree/compiler/Dialect/Util/IR/UtilTypes.h" +#include "iree/compiler/GlobalOptimization/Passes.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/Support/Alignment.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/MathExtras.h" +#include "mlir/Dialect/Arith/IR/Arith.h" +#include "mlir/Dialect/Linalg/IR/Linalg.h" +#include "mlir/Dialect/Tensor/IR/Tensor.h" +#include "mlir/Dialect/Utils/StaticValueUtils.h" +#include "mlir/IR/AsmState.h" +#include "mlir/IR/BuiltinAttributes.h" +#include "mlir/IR/BuiltinDialect.h" +#include "mlir/IR/DialectResourceBlobManager.h" +#include "mlir/IR/IRMapping.h" +#include "mlir/IR/PatternMatch.h" +#include "mlir/IR/SymbolTable.h" +#include "mlir/Interfaces/TilingInterface.h" + +#define DEBUG_TYPE "iree-global-opt-split-large-constants" + +namespace mlir::iree_compiler::GlobalOptimization { + +#define GEN_PASS_DEF_SPLITLARGECONSTANTSPASS +#include "iree/compiler/GlobalOptimization/Passes.h.inc" + +namespace { + +/// How an oversized global is cut into shards: consecutive ranges of +/// `splitDim`. All dimensions before `splitDim` have extent 1, so every shard +/// is a contiguous byte range of the original value. +struct SplitPlan { + RankedTensorType type; + int64_t splitDim = 0; + // Bytes covered by one index of `splitDim`. + int64_t sliceBytes = 0; + // Offset and extent along `splitDim` of each shard. + SmallVector<int64_t> offsets; + SmallVector<int64_t> sizes; + + int64_t getNumShards() const { return offsets.size(); } + RankedTensorType getShardType(int64_t shard) const { + SmallVector<int64_t> shape(type.getShape()); + shape[splitDim] = sizes[shard]; + return RankedTensorType::get(shape, type.getElementType(), + type.getEncoding()); + } +}; + +/// A use of the oversized value as an input operand of a structured op. The +/// op is split along `loopDim`, the loop that indexes `splitDim` of the value. +struct OperandUse { + TilingInterface op; + unsigned operandNumber; + unsigned loopDim; + bool isReduction; +}; + +/// A use of the oversized value captured in the body of an elementwise +/// `linalg.generic` and read with one `tensor.extract`, e.g. an embedding +/// lookup. +struct GatherUse { + linalg::GenericOp op; + tensor::ExtractOp extractOp; +}; + +struct UsePlan { + SmallVector<OperandUse> operandUses; + SmallVector<GatherUse> gatherUses; +}; + +/// Returns the bytes of the initial value, or std::nullopt if it is not an +/// inline dense value whose bytes can be sliced along the outer dimension. +/// Named parameters are not inline; they are split by referring to byte ranges +/// of the parameter instead (see buildShardParameter). +static std::optional<ArrayRef<char>> getRawBytes(Attribute value) { + if (auto resourceAttr = dyn_cast<DenseResourceElementsAttr>(value)) { + const AsmResourceBlob *blob = resourceAttr.getRawHandle().getBlob(); + if (!blob) { + return std::nullopt; + } + return blob->getData(); + } + if (auto denseAttr = dyn_cast<DenseElementsAttr>(value)) { + if (denseAttr.isSplat()) { + return ArrayRef<char>(); + } + return denseAttr.getRawData(); + } + return std::nullopt; +} + +/// Returns how to split `globalOp`, or std::nullopt when it fits into one +/// allocation or cannot be split (a warning is emitted for the latter). +static std::optional<SplitPlan> planSplit(IREE::Util::GlobalOp globalOp) { + auto type = dyn_cast<RankedTensorType>(globalOp.getType()); + if (!type || !type.hasStaticShape()) { + return std::nullopt; + } + int64_t storageSize = IREE::Util::getRoundedPhysicalStorageSize(type); + int64_t maxSize = + IREE::Stream::ResourceConfigAttr::lookup(globalOp).getMaxAllocationSize(); + if (storageSize <= maxSize) { + return std::nullopt; + } + + auto warn = [&]() { + InFlightDiagnostic diag = globalOp.emitWarning(); + diag << "constant of " << storageSize + << " bytes exceeds the max allocation size of " << maxSize + << " bytes and is not split: "; + return diag; + }; + if (globalOp.isGlobalMutable() || !globalOp.isPrivate()) { + warn() << "only private immutable globals are split"; + return std::nullopt; + } + Attribute initialValue = globalOp.getInitialValueAttr(); + auto parameterAttr = + dyn_cast_if_present<IREE::Flow::NamedParameterAttr>(initialValue); + std::optional<ArrayRef<char>> rawBytes = + initialValue && !parameterAttr ? getRawBytes(initialValue) : std::nullopt; + if (!parameterAttr && !rawBytes) { + warn() << "the initial value is neither an inline dense constant nor a " + "named parameter"; + return std::nullopt; + } + if (parameterAttr && parameterAttr.getConfig() && + parameterAttr.getConfig().contains("offset") && + !parameterAttr.getConfig().getAs<IntegerAttr>("offset")) { + warn() << "the parameter offset is not an integer"; + return std::nullopt; + } + + // Only types stored at their natural width, so storage, raw data and shard + // boundaries all agree byte for byte. + Type elementType = type.getElementType(); + unsigned bitWidth = + elementType.isIntOrFloat() ? elementType.getIntOrFloatBitWidth() : 0; + if (!llvm::is_contained({8u, 16u, 32u, 64u}, bitWidth)) { + warn() << "element type " << elementType + << " is not stored at a natural width of 8, 16, 32 or 64 bits"; + return std::nullopt; + } + int64_t elementBytes = bitWidth / 8; + int64_t expectedBytes = type.getNumElements() * elementBytes; + if (rawBytes && !rawBytes->empty() && + static_cast<int64_t>(rawBytes->size()) != expectedBytes) { + warn() << "the initial value has " << rawBytes->size() + << " bytes, expected " << expectedBytes; + return std::nullopt; + } + if (parameterAttr && parameterAttr.getStorageSize() != expectedBytes) { + warn() << "the parameter has " << parameterAttr.getStorageSize() + << " bytes, expected " << expectedBytes; + return std::nullopt; + } + + SplitPlan plan; + plan.type = type; + ArrayRef<int64_t> shape = type.getShape(); + const auto *it = llvm::find_if(shape, [](int64_t dim) { return dim != 1; }); + if (it == shape.end()) { + warn() << "there is no dimension to split"; + return std::nullopt; + } + plan.splitDim = std::distance(shape.begin(), it); + int64_t extent = shape[plan.splitDim]; + plan.sliceBytes = elementBytes; + for (int64_t dim : shape.drop_front(plan.splitDim + 1)) { + plan.sliceBytes *= dim; + } + if (plan.sliceBytes > maxSize) { + warn() << "one index of dimension " << plan.splitDim << " alone has " + << plan.sliceBytes << " bytes"; + return std::nullopt; + } + + // As few shards as possible, of near-equal size. Balancing the shards never + // exceeds the extent that fits into one allocation. + int64_t maxShardExtent = maxSize / plan.sliceBytes; + int64_t numShards = llvm::divideCeil(extent, maxShardExtent); + int64_t shardExtent = llvm::divideCeil(extent, numShards); + for (int64_t offset = 0; offset < extent; offset += shardExtent) { + plan.offsets.push_back(offset); + plan.sizes.push_back(std::min(shardExtent, extent - offset)); + } + return plan; +} + +/// Checks that every use of `value` (a load of the global being split) can be +/// rewritten to use the shards. Emits a warning at the first one that cannot. +/// `seenOps` holds the ops already planned for a rewrite. It is shared by all +/// loads of the global, as rewriting an op replaces it, so it can be rewritten +/// for only one use. +static FailureOr<UsePlan> +analyzeUses(Value value, const SplitPlan &plan, IREE::Util::GlobalOp globalOp, + llvm::SmallDenseSet<Operation *> &seenOps) { + UsePlan usePlan; + auto unsupported = [&](Operation *user, const Twine &reason) { + InFlightDiagnostic diag = user->emitWarning(); + diag << "cannot split the use of oversized constant @" + << globalOp.getGlobalName().getValue() << ": " << reason; + diag.attachNote(globalOp.getLoc()) << "constant declared here"; + return failure(); + }; + + for (OpOperand &use : value.getUses()) { + Operation *user = use.getOwner(); + + if (auto linalgOp = dyn_cast<linalg::LinalgOp>(user)) { + if (!seenOps.insert(user).second) { + return unsupported(user, "used by more than one operand"); + } + if (!linalgOp.hasPureTensorSemantics() || !linalgOp.isDpsInput(&use) || + !isa<TilingInterface>(user)) { + return unsupported(user, "not an input of a tensor op that can tile"); + } + AffineMap map = linalgOp.getMatchingIndexingMap(&use); + auto dimExpr = dyn_cast<AffineDimExpr>(map.getResult(plan.splitDim)); + if (!dimExpr) { + return unsupported(user, "the split dimension is not indexed by a " + "single loop"); + } + unsigned loopDim = dimExpr.getPosition(); + for (auto [index, expr] : llvm::enumerate(map.getResults())) { + if (index != plan.splitDim && expr.isFunctionOfDim(loopDim)) { + return unsupported(user, "the split loop indexes more than one " + "dimension of the constant"); + } + } + utils::IteratorType iteratorType = + linalgOp.getIteratorTypesArray()[loopDim]; + bool isReduction = iteratorType == utils::IteratorType::reduction; + for (OpOperand &init : linalgOp.getDpsInitsMutable()) { + AffineMap initMap = linalgOp.getMatchingIndexingMap(&init); + int64_t numUses = llvm::count_if(initMap.getResults(), [&](auto e) { + return e.isFunctionOfDim(loopDim); + }); + bool isPlainDim = + llvm::is_contained(initMap.getResults(), + getAffineDimExpr(loopDim, user->getContext())); + if (isReduction ? numUses != 0 : (numUses != 1 || !isPlainDim)) { + return unsupported(user, "the split loop is not a plain dimension " + "of the results"); + } + } + usePlan.operandUses.push_back({cast<TilingInterface>(user), + use.getOperandNumber(), loopDim, + isReduction}); + continue; + } + + if (auto extractOp = dyn_cast<tensor::ExtractOp>(user)) { + auto genericOp = dyn_cast<linalg::GenericOp>(user->getParentOp()); + if (!genericOp || user->getBlock() != genericOp.getBody()) { + return unsupported(user, "not read directly in a linalg.generic body"); + } + if (!seenOps.insert(genericOp).second) { + return unsupported(user, "read more than once in the same op"); + } + if (!genericOp.hasPureTensorSemantics() || + genericOp.getNumDpsInits() != 1 || + genericOp.getNumParallelLoops() != genericOp.getNumLoops() || + llvm::is_contained(genericOp->getOperands(), value)) { + return unsupported(user, "the reading op is not an elementwise " + "linalg.generic with one result"); + } + usePlan.gatherUses.push_back({genericOp, extractOp}); + continue; + } + + return unsupported(user, "unsupported user"); + } + return usePlan; +} + +/// Builds a parameter that refers to `byteLength` bytes at `byteOffset` within +/// `parameterAttr`: the same scope and key, with the offset added to the one +/// the parameter already has. Any other config entries are kept. +static IREE::Flow::NamedParameterAttr +buildShardParameter(IREE::Flow::NamedParameterAttr parameterAttr, + RankedTensorType shardType, int64_t byteOffset, + int64_t byteLength) { + MLIRContext *context = parameterAttr.getContext(); + Builder builder(context); + NamedAttrList config; + int64_t baseOffset = 0; + if (DictionaryAttr configAttr = parameterAttr.getConfig()) { + config.append(configAttr.getValue()); + if (auto offsetAttr = configAttr.getAs<IntegerAttr>("offset")) { + baseOffset = offsetAttr.getInt(); + } + } + config.set("offset", builder.getI64IntegerAttr(baseOffset + byteOffset)); + config.set("length", builder.getI64IntegerAttr(byteLength)); + return IREE::Flow::NamedParameterAttr::get( + context, shardType, parameterAttr.getScope(), parameterAttr.getKey(), + config.getDictionary(context)); +} + +/// Builds the initial value of one shard. +static TypedAttr buildShardValue(Attribute value, const SplitPlan &plan, + int64_t shard, StringRef blobName) { + RankedTensorType shardType = plan.getShardType(shard); + int64_t byteOffset = plan.offsets[shard] * plan.sliceBytes; + int64_t byteLength = plan.sizes[shard] * plan.sliceBytes; + if (auto parameterAttr = dyn_cast<IREE::Flow::NamedParameterAttr>(value)) { + return buildShardParameter(parameterAttr, shardType, byteOffset, + byteLength); + } + ArrayRef<char> rawBytes = *getRawBytes(value); + if (rawBytes.empty()) { + auto splatAttr = cast<DenseElementsAttr>(value); + return DenseElementsAttr::get(shardType, + splatAttr.getSplatValue<Attribute>()); + } + ArrayRef<char> shardBytes = rawBytes.slice(byteOffset, byteLength); + if (auto resourceAttr = dyn_cast<DenseResourceElementsAttr>(value)) { + // The shard is a view into the original blob rather than a copy. Erasing + // the original global does not free its blob: the blob manager keeps it + // until the context is destroyed, so a copy would double the memory. + // This relies on nothing replacing the original blob (e.g., registering a + // new blob using the same name) while the shards live. + // The view is aligned to the original alignment only as far as its offset + // allows; it stays aligned to the element type, as the offset is a + // multiple of a slice of the split dimension. + llvm::Align alignment = llvm::commonAlignment( + llvm::Align(resourceAttr.getRawHandle().getBlob()->getDataAlignment()), + byteOffset); + return DenseResourceElementsAttr::get( + shardType, blobName, + UnmanagedAsmResourceBlob::allocateWithAlign(shardBytes, + alignment.value())); + } + return DenseElementsAttr::getFromRawBuffer(shardType, shardBytes); +} + +/// Splits an op that reads the constant as an input operand into one op per +/// shard, iterating over that shard's range of the split loop. For a parallel +/// loop the results are inserted into the original destination; for a +/// reduction loop each op accumulates into the result of the previous one. +static LogicalResult splitOperandUse(RewriterBase &rewriter, + const OperandUse &use, Value value, + const SplitPlan &plan, + ArrayRef<Value> shards) { + TilingInterface op = use.op; + Location loc = op.getLoc(); + rewriter.setInsertionPoint(op); + SmallVector<Range> domain = op.getIterationDomain(rewriter); + auto dpsOp = cast<DestinationStyleOpInterface>(op.getOperation()); + SmallVector<Value> results = llvm::to_vector(dpsOp.getDpsInits()); + + for (int64_t shard = 0; shard < plan.getNumShards(); ++shard) { + SmallVector<OpFoldResult> offsets, sizes; + for (const Range &range : domain) { + offsets.push_back(range.offset); + sizes.push_back(range.size); + } + offsets[use.loopDim] = rewriter.getIndexAttr(plan.offsets[shard]); + sizes[use.loopDim] = rewriter.getIndexAttr(plan.sizes[shard]); + FailureOr<TilingResult> tiled = + op.getTiledImplementation(rewriter, offsets, sizes); + if (failed(tiled) || tiled->tiledOps.size() != 1) { + return op.emitError("failed to split along loop ") << use.loopDim; + } + Operation *tiledOp = tiled->tiledOps.front(); + + // Read the shard instead of the matching slice of the whole constant. + OpOperand &operand = tiledOp->getOpOperand(use.operandNumber); + auto sliceOp = operand.get().getDefiningOp<tensor::ExtractSliceOp>(); + if (!sliceOp || sliceOp.getSource() != value) { + return op.emitError("expected the split op to read a slice of the " + "constant"); + } + // Create the ops that read the shard before the tiled op that uses them. + rewriter.setInsertionPoint(tiledOp); + SmallVector<OpFoldResult> sliceOffsets = sliceOp.getMixedOffsets(); + OpFoldResult &offset = sliceOffsets[plan.splitDim]; + if (std::optional<int64_t> constOffset = getConstantIntValue(offset)) { + offset = rewriter.getIndexAttr(*constOffset - plan.offsets[shard]); + } else { + Value base = + arith::ConstantIndexOp::create(rewriter, loc, plan.offsets[shard]); + offset = arith::SubIOp::create(rewriter, loc, cast<Value>(offset), base) + .getResult(); + } + Value shardSlice = shards[shard]; + bool isWholeShard = sliceOp.getType() == shardSlice.getType() && + llvm::all_of(sliceOffsets, isZeroInteger) && + llvm::all_of(sliceOp.getMixedStrides(), isOneInteger); + if (!isWholeShard) { + shardSlice = tensor::ExtractSliceOp::create( + rewriter, loc, sliceOp.getType(), shards[shard], sliceOffsets, + sliceOp.getMixedSizes(), sliceOp.getMixedStrides()); + } + rewriter.modifyOpInPlace(tiledOp, [&] { operand.set(shardSlice); }); + if (sliceOp.use_empty()) { + rewriter.eraseOp(sliceOp); + } + rewriter.setInsertionPointAfter(tiledOp); + + if (use.isReduction) { + // The results do not depend on the split loop, so each shard's op takes + // the whole destination: chain them. + if (shard > 0) { + auto tiledDpsOp = cast<DestinationStyleOpInterface>(tiledOp); + for (auto [init, result] : + llvm::zip_equal(tiledDpsOp.getDpsInitsMutable(), results)) { + if (init.get().getType() != result.getType()) { + return op.emitError("expected a reduction to keep the whole " + "destination"); + } + Value oldInit = init.get(); + rewriter.modifyOpInPlace(tiledOp, [&, &init = init, result = result] { + init.set(result); + }); + if (auto oldSlice = oldInit.getDefiningOp<tensor::ExtractSliceOp>(); + oldSlice && oldSlice.use_empty()) { + rewriter.eraseOp(oldSlice); + } + } + } + results = llvm::to_vector(tiled->tiledValues); + continue; + } + for (auto [index, tiledValue] : llvm::enumerate(tiled->tiledValues)) { + SmallVector<OpFoldResult> resultOffsets, resultSizes; + if (failed(op.getResultTilePosition(rewriter, index, offsets, sizes, + resultOffsets, resultSizes))) { + return op.emitError("failed to compute the position of result ") + << index; + } + SmallVector<OpFoldResult> strides(resultOffsets.size(), + rewriter.getIndexAttr(1)); + results[index] = tensor::InsertSliceOp::create( + rewriter, loc, tiledValue, results[index], resultOffsets, resultSizes, + strides); + } + } + rewriter.replaceOp(op, results); + return success(); +} + +/// Rewrites an elementwise op that reads the constant at a computed index (a +/// gather) into a chain of ops, one per shard. Shard k reads its own shard at +/// the clamped, rebased index and keeps the previous op's element unless the +/// index falls into shard k. Elementwise fusion may merge the chain into one +/// dispatch again; each shard stays a binding within the allocation limit. +static void splitGatherUse(RewriterBase &rewriter, GatherUse use, + const SplitPlan &plan, ArrayRef<Value> shards) { + linalg::GenericOp op = use.op; + Location loc = op.getLoc(); + unsigned numInputs = op.getNumDpsInputs(); + SmallVector<AffineMap> maps = op.getIndexingMapsArray(); + AffineMap initMap = maps[numInputs]; + Type elementType = getElementTypeOrSelf(op.getResult(0).getType()); + + Value previous; + for (int64_t shard = 0; shard < plan.getNumShards(); ++shard) { + rewriter.setInsertionPoint(op); + SmallVector<Value> inputs(op.getDpsInputs()); + SmallVector<AffineMap> shardMaps(maps.begin(), maps.begin() + numInputs); + if (previous) { + inputs.push_back(previous); + shardMaps.push_back(initMap); + } + shardMaps.push_back(initMap); + auto shardOp = linalg::GenericOp::create( + rewriter, loc, op.getResultTypes(), inputs, op.getDpsInits(), shardMaps, + op.getIteratorTypesArray()); + shardOp->setDiscardableAttrs(op->getDiscardableAttrDictionary()); + IRMapping mapping; + rewriter.cloneRegionBefore(op.getRegion(), shardOp.getRegion(), + shardOp.getRegion().end(), mapping); + Block *body = shardOp.getBody(); + BlockArgument previousArg; + if (previous) { + previousArg = body->insertArgument(numInputs, elementType, loc); + } + + auto extractOp = cast<tensor::ExtractOp>( + mapping.lookup(use.extractOp.getResult()).getDefiningOp()); + rewriter.setInsertionPoint(extractOp); + SmallVector<Value> indices(extractOp.getIndices()); + Value index = indices[plan.splitDim]; + if (plan.offsets[shard] != 0) { + Value base = + arith::ConstantIndexOp::create(rewriter, loc, plan.offsets[shard]); + index = arith::SubIOp::create(rewriter, loc, index, base); + } + Value last = + arith::ConstantIndexOp::create(rewriter, loc, plan.sizes[shard] - 1); + indices[plan.splitDim] = arith::MinUIOp::create(rewriter, loc, index, last); + Value inShard; + if (previous) { + Value size = + arith::ConstantIndexOp::create(rewriter, loc, plan.sizes[shard]); + inShard = arith::CmpIOp::create(rewriter, loc, arith::CmpIPredicate::ult, + index, size); + } + rewriter.replaceOpWithNewOp<tensor::ExtractOp>(extractOp, shards[shard], + indices); + if (previous) { + auto yieldOp = cast<linalg::YieldOp>(body->getTerminator()); + rewriter.setInsertionPoint(yieldOp); + Value selected = arith::SelectOp::create( + rewriter, loc, inShard, yieldOp.getOperand(0), previousArg); + rewriter.modifyOpInPlace(yieldOp, + [&] { yieldOp->setOperand(0, selected); }); + } + + previous = shardOp.getResult(0); + } + rewriter.replaceOp(op, previous); +} + +struct SplitLargeConstantsPass + : impl::SplitLargeConstantsPassBase<SplitLargeConstantsPass> { + void runOnOperation() override { + ModuleOp moduleOp = getOperation(); + SymbolTable symbolTable(moduleOp); + IRRewriter rewriter(&getContext()); + for (auto globalOp : + llvm::to_vector(moduleOp.getOps<IREE::Util::GlobalOp>())) { + if (failed(splitGlobal(rewriter, symbolTable, globalOp))) { + return signalPassFailure(); + } + } + } + + LogicalResult splitGlobal(RewriterBase &rewriter, SymbolTable &symbolTable, + IREE::Util::GlobalOp globalOp) { + std::optional<SplitPlan> plan = planSplit(globalOp); + if (!plan) { + return success(); + } + + // Check every use before changing anything, so that a constant is either + // fully split or left alone. + SmallVector<IREE::Util::GlobalLoadOp> loadOps; + if (auto uses = SymbolTable::getSymbolUses(globalOp, getOperation())) { + for (const SymbolTable::SymbolUse &use : *uses) { + auto loadOp = dyn_cast<IREE::Util::GlobalLoadOp>(use.getUser()); + if (!loadOp) { + use.getUser()->emitWarning("cannot split the use of oversized " + "constant @") + << globalOp.getGlobalName().getValue() + << ": only util.global.load is supported"; + return success(); + } + loadOps.push_back(loadOp); + } + } + SmallVector<UsePlan> usePlans; + llvm::SmallDenseSet<Operation *> seenOps; + for (IREE::Util::GlobalLoadOp loadOp : loadOps) { + FailureOr<UsePlan> usePlan = + analyzeUses(loadOp.getResult(), *plan, globalOp, seenOps); + if (failed(usePlan)) { + return success(); + } + usePlans.push_back(std::move(*usePlan)); + } + LLVM_DEBUG(llvm::dbgs() + << "splitting @" << globalOp.getGlobalName() << " into " + << plan->getNumShards() << " shards along dimension " + << plan->splitDim << "\n"); + + SmallVector<IREE::Util::GlobalOp> shardOps; + rewriter.setInsertionPointAfter(globalOp); + for (int64_t shard = 0; shard < plan->getNumShards(); ++shard) { + auto shardOp = cast<IREE::Util::GlobalOp>(rewriter.clone(*globalOp)); + SymbolTable::setSymbolName(shardOp, (globalOp.getGlobalName().getValue() + + "_shard" + Twine(shard)) + .str()); + symbolTable.insert(shardOp); + shardOp.setType(plan->getShardType(shard)); + shardOp.setInitialValueAttr( + buildShardValue(globalOp.getInitialValueAttr(), *plan, shard, + shardOp.getGlobalName().getValue())); + shardOps.push_back(shardOp); + rewriter.setInsertionPointAfter(shardOp); + } + + for (auto [loadOp, usePlan] : llvm::zip_equal(loadOps, usePlans)) { + rewriter.setInsertionPoint(loadOp); + SmallVector<Value> shards; + for (IREE::Util::GlobalOp shardOp : shardOps) { + auto shardLoadOp = IREE::Util::GlobalLoadOp::create( + rewriter, loadOp.getLoc(), shardOp); + shardLoadOp.setIsImmutable(loadOp.getIsImmutable()); + shards.push_back(shardLoadOp.getResult()); + } + for (const OperandUse &use : usePlan.operandUses) { + if (failed(splitOperandUse(rewriter, use, loadOp.getResult(), *plan, + shards))) { + return failure(); + } + } + for (const GatherUse &use : usePlan.gatherUses) { + splitGatherUse(rewriter, use, *plan, shards); + } + for (Value shard : shards) { + if (shard.use_empty()) { + rewriter.eraseOp(shard.getDefiningOp()); + } + } + rewriter.eraseOp(loadOp); + } + symbolTable.erase(globalOp); + return success(); + } +}; + +} // namespace +} // namespace mlir::iree_compiler::GlobalOptimization
diff --git a/compiler/src/iree/compiler/GlobalOptimization/test/BUILD.bazel b/compiler/src/iree/compiler/GlobalOptimization/test/BUILD.bazel index e35ecf1..bf94fa8 100644 --- a/compiler/src/iree/compiler/GlobalOptimization/test/BUILD.bazel +++ b/compiler/src/iree/compiler/GlobalOptimization/test/BUILD.bazel
@@ -51,6 +51,8 @@ "propagate_linalg_transpose.mlir", "raise_special_ops.mlir", "remove_zero_extent_tensors.mlir", + "split_large_constants.mlir", + "split_large_constants_resource.mlir", "strided_contraction_to_contraction.mlir", "transformation_pipeline.mlir", "transpose_and_decompose_concat.mlir",
diff --git a/compiler/src/iree/compiler/GlobalOptimization/test/CMakeLists.txt b/compiler/src/iree/compiler/GlobalOptimization/test/CMakeLists.txt index 8977beb..7444e0d 100644 --- a/compiler/src/iree/compiler/GlobalOptimization/test/CMakeLists.txt +++ b/compiler/src/iree/compiler/GlobalOptimization/test/CMakeLists.txt
@@ -48,6 +48,8 @@ "propagate_linalg_transpose.mlir" "raise_special_ops.mlir" "remove_zero_extent_tensors.mlir" + "split_large_constants.mlir" + "split_large_constants_resource.mlir" "strided_contraction_to_contraction.mlir" "transformation_pipeline.mlir" "transpose_and_decompose_concat.mlir"
diff --git a/compiler/src/iree/compiler/GlobalOptimization/test/split_large_constants.mlir b/compiler/src/iree/compiler/GlobalOptimization/test/split_large_constants.mlir new file mode 100644 index 0000000..c7c4d12 --- /dev/null +++ b/compiler/src/iree/compiler/GlobalOptimization/test/split_large_constants.mlir
@@ -0,0 +1,403 @@ +// RUN: iree-opt --split-input-file --verify-diagnostics --iree-global-opt-split-large-constants %s | FileCheck %s + +// 6x4xf16 is 48 bytes; with a 16 byte limit it becomes three 2x4 shards. An +// embedding lookup becomes a chain of ops, one per shard. + +#id = affine_map<(d0, d1) -> (d0)> +#out = affine_map<(d0, d1) -> (d0, d1)> +module @gather attributes { + stream.resources = #stream.resource_config<{max_allocation_size = 16}> +} { + util.global private @table = dense<[ + [0.0, 1.0, 2.0, 3.0], [4.0, 5.0, 6.0, 7.0], [8.0, 9.0, 10.0, 11.0], + [12.0, 13.0, 14.0, 15.0], [16.0, 17.0, 18.0, 19.0], [20.0, 21.0, 22.0, 23.0] + ]> : tensor<6x4xf16> + util.func public @main(%ids: tensor<8xi64>) -> tensor<8x4xf16> { + %table = util.global.load immutable @table : tensor<6x4xf16> + %empty = tensor.empty() : tensor<8x4xf16> + %0 = linalg.generic { + indexing_maps = [#id, #out], iterator_types = ["parallel", "parallel"]} + ins(%ids : tensor<8xi64>) outs(%empty : tensor<8x4xf16>) { + ^bb0(%in: i64, %out: f16): + %i = arith.index_cast %in : i64 to index + %j = linalg.index 1 : index + %v = tensor.extract %table[%i, %j] : tensor<6x4xf16> + linalg.yield %v : f16 + } -> tensor<8x4xf16> + util.return %0 : tensor<8x4xf16> + } +} + +// CHECK-LABEL: module @gather +// CHECK-NOT: util.global private @table = +// CHECK: util.global private @table_shard0 = dense<{{\[\[}}0.000000e+00, {{.+}}, 7.000000e+00]]> : tensor<2x4xf16> +// CHECK: util.global private @table_shard1 = dense<{{\[\[}}8.000000e+00, {{.+}}, 1.500000e+01]]> : tensor<2x4xf16> +// CHECK: util.global private @table_shard2 = dense<{{\[\[}}1.600000e+01, {{.+}}, 2.300000e+01]]> : tensor<2x4xf16> +// CHECK: util.func public @main(%[[IDS:.+]]: tensor<8xi64>) +// CHECK-DAG: %[[S0:.+]] = util.global.load immutable @table_shard0 : tensor<2x4xf16> +// CHECK-DAG: %[[S1:.+]] = util.global.load immutable @table_shard1 : tensor<2x4xf16> +// CHECK-DAG: %[[S2:.+]] = util.global.load immutable @table_shard2 : tensor<2x4xf16> +// CHECK: %[[G0:.+]] = linalg.generic +// CHECK-SAME: ins(%[[IDS]] : tensor<8xi64>) +// CHECK: %[[I0:.+]] = arith.index_cast +// CHECK: %[[LAST0:.+]] = arith.constant 1 : index +// CHECK: %[[CLAMP0:.+]] = arith.minui %[[I0]], %[[LAST0]] +// CHECK: %[[E0:.+]] = tensor.extract %[[S0]][%[[CLAMP0]], %{{.+}}] +// CHECK: linalg.yield %[[E0]] +// CHECK: %[[G1:.+]] = linalg.generic +// CHECK-SAME: ins(%[[IDS]], %[[G0]] : tensor<8xi64>, tensor<8x4xf16>) +// CHECK: ^bb0(%{{.+}}: i64, %[[PREV1:.+]]: f16, %{{.+}}: f16): +// CHECK: %[[I1:.+]] = arith.index_cast +// CHECK: %[[OFF1:.+]] = arith.constant 2 : index +// CHECK: %[[REL1:.+]] = arith.subi %[[I1]], %[[OFF1]] +// CHECK: %[[CLAMP1:.+]] = arith.minui %[[REL1]] +// CHECK: %[[IN1:.+]] = arith.cmpi ult, %[[REL1]] +// CHECK: %[[E1:.+]] = tensor.extract %[[S1]][%[[CLAMP1]], %{{.+}}] +// CHECK: %[[SEL1:.+]] = arith.select %[[IN1]], %[[E1]], %[[PREV1]] +// CHECK: linalg.yield %[[SEL1]] +// CHECK: %[[G2:.+]] = linalg.generic +// CHECK-SAME: ins(%[[IDS]], %[[G1]] : tensor<8xi64>, tensor<8x4xf16>) +// CHECK: arith.constant 4 : index +// CHECK: tensor.extract %[[S2]] +// CHECK: arith.select +// CHECK: util.return %[[G2]] + +// ----- + +// A tied LM head: the oversized weight is N x K, so the split dimension is +// the parallel N loop. Each shard's matmul fills its own columns. + +module @matmul_parallel attributes { + stream.resources = #stream.resource_config<{max_allocation_size = 16}> +} { + util.global private @weight = dense<1.0> : tensor<6x4xf16> + util.func public @main(%lhs: tensor<3x4xf16>) -> tensor<3x6xf32> { + %cst = arith.constant 0.0 : f32 + %weight = util.global.load immutable @weight : tensor<6x4xf16> + %empty = tensor.empty() : tensor<3x6xf32> + %fill = linalg.fill ins(%cst : f32) outs(%empty : tensor<3x6xf32>) -> tensor<3x6xf32> + %0 = linalg.matmul + indexing_maps = [affine_map<(m, n, k) -> (m, k)>, + affine_map<(m, n, k) -> (n, k)>, + affine_map<(m, n, k) -> (m, n)>] + ins(%lhs, %weight : tensor<3x4xf16>, tensor<6x4xf16>) + outs(%fill : tensor<3x6xf32>) -> tensor<3x6xf32> + util.return %0 : tensor<3x6xf32> + } +} + +// CHECK-LABEL: module @matmul_parallel +// CHECK: util.global private @weight_shard0 = dense<1.000000e+00> : tensor<2x4xf16> +// CHECK: util.global private @weight_shard1 = dense<1.000000e+00> : tensor<2x4xf16> +// CHECK: util.global private @weight_shard2 = dense<1.000000e+00> : tensor<2x4xf16> +// CHECK: util.func public @main +// CHECK-DAG: %[[S0:.+]] = util.global.load immutable @weight_shard0 +// CHECK-DAG: %[[S1:.+]] = util.global.load immutable @weight_shard1 +// CHECK-DAG: %[[S2:.+]] = util.global.load immutable @weight_shard2 +// CHECK: %[[FILL:.+]] = linalg.fill +// CHECK: %[[M0:.+]] = linalg.matmul +// CHECK-SAME: ins(%{{.+}}, %[[S0]] : tensor<3x4xf16>, tensor<2x4xf16>) +// CHECK-SAME: -> tensor<3x2xf32> +// CHECK: %[[R0:.+]] = tensor.insert_slice %[[M0]] into %[[FILL]][0, 0] [3, 2] [1, 1] +// CHECK: %[[M1:.+]] = linalg.matmul +// CHECK-SAME: ins(%{{.+}}, %[[S1]] : tensor<3x4xf16>, tensor<2x4xf16>) +// CHECK: %[[R1:.+]] = tensor.insert_slice %[[M1]] into %[[R0]][0, 2] [3, 2] [1, 1] +// CHECK: %[[M2:.+]] = linalg.matmul +// CHECK-SAME: ins(%{{.+}}, %[[S2]] : tensor<3x4xf16>, tensor<2x4xf16>) +// CHECK: %[[R2:.+]] = tensor.insert_slice %[[M2]] into %[[R1]][0, 4] [3, 2] [1, 1] +// CHECK: util.return %[[R2]] + +// ----- + +// The oversized weight is K x N, so the split dimension is the reduction K +// loop. Each shard's matmul accumulates into the previous one's result. + +module @matmul_reduction attributes { + stream.resources = #stream.resource_config<{max_allocation_size = 16}> +} { + util.global private @weight = dense<1.0> : tensor<6x4xf16> + util.func public @main(%lhs: tensor<3x6xf16>) -> tensor<3x4xf32> { + %cst = arith.constant 0.0 : f32 + %weight = util.global.load immutable @weight : tensor<6x4xf16> + %empty = tensor.empty() : tensor<3x4xf32> + %fill = linalg.fill ins(%cst : f32) outs(%empty : tensor<3x4xf32>) -> tensor<3x4xf32> + %0 = linalg.matmul ins(%lhs, %weight : tensor<3x6xf16>, tensor<6x4xf16>) + outs(%fill : tensor<3x4xf32>) -> tensor<3x4xf32> + util.return %0 : tensor<3x4xf32> + } +} + +// CHECK-LABEL: module @matmul_reduction +// CHECK: util.func public @main(%[[LHS:.+]]: tensor<3x6xf16>) +// CHECK-DAG: %[[S0:.+]] = util.global.load immutable @weight_shard0 +// CHECK-DAG: %[[S1:.+]] = util.global.load immutable @weight_shard1 +// CHECK-DAG: %[[S2:.+]] = util.global.load immutable @weight_shard2 +// CHECK: %[[L0:.+]] = tensor.extract_slice %[[LHS]][0, 0] [3, 2] [1, 1] +// CHECK: %[[M0:.+]] = linalg.matmul ins(%[[L0]], %[[S0]] : tensor<3x2xf16>, tensor<2x4xf16>) +// CHECK: %[[L1:.+]] = tensor.extract_slice %[[LHS]][0, 2] [3, 2] [1, 1] +// CHECK: %[[M1:.+]] = linalg.matmul ins(%[[L1]], %[[S1]] : tensor<3x2xf16>, tensor<2x4xf16>) outs(%[[M0]] : tensor<3x4xf32>) +// CHECK: %[[L2:.+]] = tensor.extract_slice %[[LHS]][0, 4] [3, 2] [1, 1] +// CHECK: %[[M2:.+]] = linalg.matmul ins(%[[L2]], %[[S2]] : tensor<3x2xf16>, tensor<2x4xf16>) outs(%[[M1]] : tensor<3x4xf32>) +// CHECK: util.return %[[M2]] + +// ----- + +// A leading unit dimension is skipped: 1x3x4xi16 (24 bytes, 8 per index of +// dimension 1) is split along dimension 1. The shard bytes are checked in +// split_large_constants_resource.mlir, as --split-input-file does not print +// resources. + +{-# + dialect_resources: { + builtin: { + blob: "0x02000000000001000200030004000500060007000800090010001100" + } + } +#-} + +module @resource attributes { + stream.resources = #stream.resource_config<{max_allocation_size = 16}> +} { + util.global private @table = dense_resource<blob> : tensor<1x3x4xi16> + util.func public @main(%lhs: tensor<5x4xi16>) -> tensor<5x1x3xi16> { + %table = util.global.load immutable @table : tensor<1x3x4xi16> + %empty = tensor.empty() : tensor<5x1x3xi16> + %0 = linalg.generic { + indexing_maps = [affine_map<(m, b, n, k) -> (m, k)>, + affine_map<(m, b, n, k) -> (b, n, k)>, + affine_map<(m, b, n, k) -> (m, b, n)>], + iterator_types = ["parallel", "parallel", "parallel", "reduction"]} + ins(%lhs, %table : tensor<5x4xi16>, tensor<1x3x4xi16>) + outs(%empty : tensor<5x1x3xi16>) { + ^bb0(%a: i16, %b: i16, %acc: i16): + %mul = arith.muli %a, %b : i16 + %add = arith.addi %acc, %mul : i16 + linalg.yield %add : i16 + } -> tensor<5x1x3xi16> + util.return %0 : tensor<5x1x3xi16> + } +} + +// CHECK-LABEL: module @resource +// CHECK: util.global private @table_shard0 = dense_resource<table_shard0> : tensor<1x2x4xi16> +// CHECK: util.global private @table_shard1 = dense_resource<table_shard1> : tensor<1x1x4xi16> +// CHECK: %[[G0:.+]] = linalg.generic +// CHECK-SAME: tensor<5x4xi16>, tensor<1x2x4xi16> +// CHECK: %[[R0:.+]] = tensor.insert_slice %[[G0]] into %{{.+}}[0, 0, 0] [5, 1, 2] [1, 1, 1] +// CHECK: %[[G1:.+]] = linalg.generic +// CHECK-SAME: tensor<5x4xi16>, tensor<1x1x4xi16> +// CHECK: tensor.insert_slice %[[G1]] into %[[R0]][0, 0, 2] [5, 1, 1] [1, 1, 1] + +// ----- + +// A named parameter is split without reading it: each shard refers to its byte +// range of the same parameter. 6x4xf16 (48 bytes, 8 per row) becomes three 2x4 +// shards of 16 bytes at offsets 0, 16 and 32. + +module @parameter attributes { + stream.resources = #stream.resource_config<{max_allocation_size = 16}> +} { + util.global private @weight = #flow.parameter.named<"model"::"lm_head.weight"> : tensor<6x4xf16> + util.func public @main(%lhs: tensor<3x4xf16>) -> tensor<3x6xf32> { + %cst = arith.constant 0.0 : f32 + %weight = util.global.load immutable @weight : tensor<6x4xf16> + %empty = tensor.empty() : tensor<3x6xf32> + %fill = linalg.fill ins(%cst : f32) outs(%empty : tensor<3x6xf32>) -> tensor<3x6xf32> + %0 = linalg.matmul + indexing_maps = [affine_map<(m, n, k) -> (m, k)>, + affine_map<(m, n, k) -> (n, k)>, + affine_map<(m, n, k) -> (m, n)>] + ins(%lhs, %weight : tensor<3x4xf16>, tensor<6x4xf16>) + outs(%fill : tensor<3x6xf32>) -> tensor<3x6xf32> + util.return %0 : tensor<3x6xf32> + } +} + +// CHECK-LABEL: module @parameter +// CHECK-NOT: util.global private @weight = +// CHECK: util.global private @weight_shard0 = #flow.parameter.named<"model"::"lm_head.weight", {length = 16 : i64, offset = 0 : i64}> : tensor<2x4xf16> +// CHECK: util.global private @weight_shard1 = #flow.parameter.named<"model"::"lm_head.weight", {length = 16 : i64, offset = 16 : i64}> : tensor<2x4xf16> +// CHECK: util.global private @weight_shard2 = #flow.parameter.named<"model"::"lm_head.weight", {length = 16 : i64, offset = 32 : i64}> : tensor<2x4xf16> +// CHECK: util.func public @main +// CHECK: linalg.matmul +// CHECK-SAME: tensor<3x4xf16>, tensor<2x4xf16> +// CHECK: linalg.matmul +// CHECK-SAME: tensor<3x4xf16>, tensor<2x4xf16> +// CHECK: linalg.matmul +// CHECK-SAME: tensor<3x4xf16>, tensor<2x4xf16> + +// ----- + +// A parameter that already is a range of a larger entry: the shard offsets are +// relative to its offset of 1024, and other config entries are kept. 5x4xf16 +// (40 bytes) becomes shards of 2, 2 and 1 rows, so the last one has 8 bytes. + +#id = affine_map<(d0, d1) -> (d0)> +#out = affine_map<(d0, d1) -> (d0, d1)> +module @parameter_range attributes { + stream.resources = #stream.resource_config<{max_allocation_size = 16}> +} { + util.global private @table = #flow.parameter.named<"model"::"embed", {offset = 1024 : i64, length = 40 : i64, note = "kept"}> : tensor<5x4xf16> + util.func public @main(%ids: tensor<8xi64>) -> tensor<8x4xf16> { + %table = util.global.load immutable @table : tensor<5x4xf16> + %empty = tensor.empty() : tensor<8x4xf16> + %0 = linalg.generic { + indexing_maps = [#id, #out], iterator_types = ["parallel", "parallel"]} + ins(%ids : tensor<8xi64>) outs(%empty : tensor<8x4xf16>) { + ^bb0(%in: i64, %out: f16): + %i = arith.index_cast %in : i64 to index + %j = linalg.index 1 : index + %v = tensor.extract %table[%i, %j] : tensor<5x4xf16> + linalg.yield %v : f16 + } -> tensor<8x4xf16> + util.return %0 : tensor<8x4xf16> + } +} + +// CHECK-LABEL: module @parameter_range +// CHECK-NOT: util.global private @table = +// CHECK: util.global private @table_shard0 = #flow.parameter.named<"model"::"embed", {length = 16 : i64, note = "kept", offset = 1024 : i64}> : tensor<2x4xf16> +// CHECK: util.global private @table_shard1 = #flow.parameter.named<"model"::"embed", {length = 16 : i64, note = "kept", offset = 1040 : i64}> : tensor<2x4xf16> +// CHECK: util.global private @table_shard2 = #flow.parameter.named<"model"::"embed", {length = 8 : i64, note = "kept", offset = 1056 : i64}> : tensor<1x4xf16> +// CHECK: util.func public @main +// CHECK: tensor.extract %{{.+}} : tensor<2x4xf16> +// CHECK: tensor.extract %{{.+}} : tensor<2x4xf16> +// CHECK: tensor.extract %{{.+}} : tensor<1x4xf16> + +// ----- + +// A constant within the limit is left alone. + +module @fits attributes { + stream.resources = #stream.resource_config<{max_allocation_size = 64}> +} { + util.global private @table = dense<1.0> : tensor<6x4xf16> + util.func public @main() -> tensor<6x4xf16> { + %table = util.global.load immutable @table : tensor<6x4xf16> + util.return %table : tensor<6x4xf16> + } +} + +// CHECK-LABEL: module @fits +// CHECK: util.global private @table = dense<1.000000e+00> : tensor<6x4xf16> +// CHECK-NOT: _shard + +// ----- + +// An oversized constant with a use that cannot be split is left alone. + +module @unsupported attributes { + stream.resources = #stream.resource_config<{max_allocation_size = 16}> +} { + // expected-note @below {{constant declared here}} + util.global private @table = dense<1.0> : tensor<6x4xf16> + util.func public @main() -> tensor<6x4xf16> { + %table = util.global.load immutable @table : tensor<6x4xf16> + // expected-warning @below {{cannot split the use of oversized constant @table: unsupported user}} + util.return %table : tensor<6x4xf16> + } +} + +// CHECK-LABEL: module @unsupported +// CHECK: util.global private @table = dense<1.000000e+00> : tensor<6x4xf16> +// CHECK-NOT: _shard + +// ----- + +// An op that uses two loads of the same constant can be rewritten for only one +// of them, so the constant is left alone. + +module @two_loads_one_op attributes { + stream.resources = #stream.resource_config<{max_allocation_size = 32}> +} { + // expected-note @below {{constant declared here}} + util.global private @big = dense<1.0> : tensor<8x8xf16> + util.func public @main(%init: tensor<8x8xf16>) -> tensor<8x8xf16> { + %a = util.global.load immutable @big : tensor<8x8xf16> + %b = util.global.load immutable @big : tensor<8x8xf16> + // expected-warning @below {{cannot split the use of oversized constant @big: used by more than one operand}} + %r = linalg.matmul ins(%a, %b : tensor<8x8xf16>, tensor<8x8xf16>) + outs(%init : tensor<8x8xf16>) -> tensor<8x8xf16> + util.return %r : tensor<8x8xf16> + } +} + +// CHECK-LABEL: module @two_loads_one_op +// CHECK: util.global private @big = dense<1.000000e+00> : tensor<8x8xf16> +// CHECK-NOT: _shard + +// ----- + +// The same for an op that reads two loads of the same constant in its body. + +#id = affine_map<(d0, d1) -> (d0)> +#out = affine_map<(d0, d1) -> (d0, d1)> +module @two_loads_one_gather attributes { + stream.resources = #stream.resource_config<{max_allocation_size = 16}> +} { + // expected-note @below {{constant declared here}} + util.global private @table = dense<1.0> : tensor<6x4xf16> + util.func public @main(%ids: tensor<8xi64>) -> tensor<8x4xf16> { + %a = util.global.load immutable @table : tensor<6x4xf16> + %b = util.global.load immutable @table : tensor<6x4xf16> + %empty = tensor.empty() : tensor<8x4xf16> + %0 = linalg.generic { + indexing_maps = [#id, #out], iterator_types = ["parallel", "parallel"]} + ins(%ids : tensor<8xi64>) outs(%empty : tensor<8x4xf16>) { + ^bb0(%in: i64, %out: f16): + %i = arith.index_cast %in : i64 to index + %j = linalg.index 1 : index + %va = tensor.extract %a[%i, %j] : tensor<6x4xf16> + // expected-warning @below {{cannot split the use of oversized constant @table: read more than once in the same op}} + %vb = tensor.extract %b[%i, %j] : tensor<6x4xf16> + %v = arith.addf %va, %vb : f16 + linalg.yield %v : f16 + } -> tensor<8x4xf16> + util.return %0 : tensor<8x4xf16> + } +} + +// CHECK-LABEL: module @two_loads_one_gather +// CHECK: util.global private @table = dense<1.000000e+00> : tensor<6x4xf16> +// CHECK-NOT: _shard + +// ----- + +// A public constant is not split. + +module @public_global attributes { + stream.resources = #stream.resource_config<{max_allocation_size = 16}> +} { + // expected-warning @below {{constant of 48 bytes exceeds the max allocation size of 16 bytes and is not split: only private immutable globals are split}} + util.global public @table = dense<1.0> : tensor<6x4xf16> +} + +// ----- + +// A parameter whose length does not match its type is not split: the shard +// ranges would not cover the bytes the type describes. + +module @parameter_length_mismatch attributes { + stream.resources = #stream.resource_config<{max_allocation_size = 16}> +} { + // expected-warning @below {{constant of 48 bytes exceeds the max allocation size of 16 bytes and is not split: the parameter has 32 bytes, expected 48}} + util.global private @table = #flow.parameter.named<"model"::"table", {length = 32 : i64}> : tensor<6x4xf16> +} + +// CHECK-LABEL: module @parameter_length_mismatch +// CHECK-NOT: _shard + +// ----- + +// A parameter offset that is not an integer cannot be added to, so the +// parameter is not split. + +module @parameter_offset_not_integer attributes { + stream.resources = #stream.resource_config<{max_allocation_size = 16}> +} { + // expected-warning @below {{constant of 48 bytes exceeds the max allocation size of 16 bytes and is not split: the parameter offset is not an integer}} + util.global private @table = #flow.parameter.named<"model"::"table", {offset = "16"}> : tensor<6x4xf16> +} + +// CHECK-LABEL: module @parameter_offset_not_integer +// CHECK-NOT: _shard
diff --git a/compiler/src/iree/compiler/GlobalOptimization/test/split_large_constants_resource.mlir b/compiler/src/iree/compiler/GlobalOptimization/test/split_large_constants_resource.mlir new file mode 100644 index 0000000..7d05e13 --- /dev/null +++ b/compiler/src/iree/compiler/GlobalOptimization/test/split_large_constants_resource.mlir
@@ -0,0 +1,43 @@ +// RUN: iree-opt --iree-global-opt-split-large-constants %s | FileCheck %s + +// Each shard of a resource is a view of a contiguous byte range of the +// original: 1x3x4xi16 holding 0..9, 16, 17 is split along dimension 1 into rows +// {0, 1} and {2}. The original is 32 byte aligned; a shard keeps that alignment +// only as far as its byte offset allows, so the shard at offset 16 is 16 byte +// aligned. + +{-# + dialect_resources: { + builtin: { + blob: "0x20000000000001000200030004000500060007000800090010001100" + } + } +#-} + +module attributes { + stream.resources = #stream.resource_config<{max_allocation_size = 16}> +} { + util.global private @table = dense_resource<blob> : tensor<1x3x4xi16> + util.func public @main(%lhs: tensor<5x4xi16>) -> tensor<5x1x3xi16> { + %table = util.global.load immutable @table : tensor<1x3x4xi16> + %empty = tensor.empty() : tensor<5x1x3xi16> + %0 = linalg.generic { + indexing_maps = [affine_map<(m, b, n, k) -> (m, k)>, + affine_map<(m, b, n, k) -> (b, n, k)>, + affine_map<(m, b, n, k) -> (m, b, n)>], + iterator_types = ["parallel", "parallel", "parallel", "reduction"]} + ins(%lhs, %table : tensor<5x4xi16>, tensor<1x3x4xi16>) + outs(%empty : tensor<5x1x3xi16>) { + ^bb0(%a: i16, %b: i16, %acc: i16): + %mul = arith.muli %a, %b : i16 + %add = arith.addi %acc, %mul : i16 + linalg.yield %add : i16 + } -> tensor<5x1x3xi16> + util.return %0 : tensor<5x1x3xi16> + } +} + +// CHECK: util.global private @table_shard0 = dense_resource<table_shard0> : tensor<1x2x4xi16> +// CHECK: util.global private @table_shard1 = dense_resource<table_shard1> : tensor<1x1x4xi16> +// CHECK: table_shard0: "0x2000000000000100020003000400050006000700" +// CHECK: table_shard1: "0x100000000800090010001100"