Skip to content

Tracing does not understand encodings #20450

Open

Description

@hanhanW

The tracing does not work with encodings after we introduce flow/stream.tensor.encode ops. #20447 is a workaround that introduces new dispatches and trace the original data, which can help debugging. It prevents compilation failures when adding dispatch tracing.

However, I think the missing support is encoding specialization for stream.tensor.encode op. To support it, we'd add TensorPhase trait to the op. The stream.tensor.trace op is lowered to stream.tensor.export (with hal.buffer_view type) and hal.buffer_view.trace op.

struct TensorTraceOpPattern
: public StreamConversionPattern<IREE::Stream::TensorTraceOp> {
using StreamConversionPattern::StreamConversionPattern;
LogicalResult
matchAndRewrite(IREE::Stream::TensorTraceOp traceOp, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
SmallVector<Value> bufferViews;
auto resourceEncodingDims = adaptor.getResourceEncodingDims();
for (auto [resource, resourceSize, resourceEncoding] : llvm::zip_equal(
adaptor.getResources(), adaptor.getResourceSizes(),
adaptor.getResourceEncodings().getAsRange<TypeAttr>())) {
int64_t dynamicDimCount =
cast<ShapedType>(resourceEncoding.getValue()).getNumDynamicDims();
bufferViews.push_back(rewriter.create<IREE::Stream::TensorExportOp>(
traceOp.getLoc(), rewriter.getType<IREE::HAL::BufferViewType>(),
resource, resourceEncoding,
resourceEncodingDims.take_front(dynamicDimCount), resourceSize,
/*affinity=*/IREE::Stream::AffinityAttr{}));
resourceEncodingDims = resourceEncodingDims.drop_front(dynamicDimCount);
}
rewriter.replaceOpWithNewOp<IREE::HAL::BufferViewTraceOp>(
traceOp, traceOp.getKeyAttr(), bufferViews);
return success();
}
};

Later on, TensorExportBufferViewOpPattern converts the export op to hal.buffer_view.create op. IMO, we can query the shape from the serialized encoding, but not only from the source encoding dims. The encoding attribute can calculate the storage buffer and it should also be able to provide the transformed dynamic dimension sizes. This new interface method could be reused in codegen to materialize bindings, IMO.

// NOTE: we should have verified supported encodings/types at entry into the
// HAL pipeline.
auto encodingType = rewriter.create<IREE::HAL::EncodingTypeOp>(
loc, tensorType.getEncoding());
auto elementType = rewriter.create<IREE::HAL::ElementTypeOp>(
loc, tensorType.getElementType());
// Flatten static + dynamic shape dimensions.
SmallVector<Value> dims;
unsigned dynamicIdx = 0;
for (int64_t idx = 0; idx < tensorType.getRank(); ++idx) {
if (tensorType.isDynamicDim(idx)) {
dims.push_back(dynamicDims[dynamicIdx++]);
} else {
dims.push_back(rewriter.create<arith::ConstantIndexOp>(
loc, tensorType.getDimSize(idx)));
}
}

Activity

  1. self-assigned this
    on Apr 2, 2025
  2. added
    enhancement ➕
    on Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement ➕

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions