[Stream] Respect the parameter offset in SplitParameterEncoder (#25271) A named parameter can refer to a byte range of an archive entry. The `offset` and `length` entries of its config give that range. The encoder used the length, as the parameter's storage size, but always loaded from offset 0. So it read the wrong bytes for any parameter with a nonzero offset. The encoder now loads from the parameter's offset. A parameter without an offset still loads from 0. The new test loads 1024 bytes at offset 4096. It checks both the copy of the unmodified parameter and the input of a hoisted fill. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Stefan Schuermans <schuermans@roofline.ai> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/compiler/src/iree/compiler/Dialect/Stream/Transforms/SplitParameterEncoder.cpp b/compiler/src/iree/compiler/Dialect/Stream/Transforms/SplitParameterEncoder.cpp index a35d797..52d8856 100644 --- a/compiler/src/iree/compiler/Dialect/Stream/Transforms/SplitParameterEncoder.cpp +++ b/compiler/src/iree/compiler/Dialect/Stream/Transforms/SplitParameterEncoder.cpp
@@ -1885,8 +1885,16 @@ Value keyValue = IREE::Util::BufferConstantOp::create( funcBuilder, constantOp.getLoc(), parameterAttr.getKey().getValue()); - // Create zero offset for full parameter load. - Value zeroOffset = i64Set.get(0); + // The parameter may be a byte range of its archive entry, given by + // the `offset` and `length` config entries. The offset defaults to + // zero. The length is part of the parameter's storage size. + int64_t sourceOffset = 0; + if (DictionaryAttr configAttr = parameterAttr.getConfig()) { + if (auto offsetAttr = configAttr.getAs<IntegerAttr>("offset")) { + sourceOffset = offsetAttr.getInt(); + } + } + Value sourceOffsetValue = i64Set.get(sourceOffset); // Retained parameters do not bring their size producers into the // encoder. Materialize the size from the parameter attribute. Value resultSize = indexSet.get(parameterAttr.getStorageSize()); @@ -1896,7 +1904,7 @@ constantOp.getResult().getType(), funcBuilder.getType<IREE::Stream::TimepointType>(), /*await_timepoint=*/lastTimepoint, scopeValue, keyValue, - zeroOffset, resultSize, targetPlan.affinityAttr); + sourceOffsetValue, resultSize, targetPlan.affinityAttr); // Await the result timepoint to get a resolved resource that can be // used by streamable ops without explicit synchronization. auto awaitOp = IREE::Stream::TimepointAwaitOp::create(
diff --git a/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/BUILD.bazel b/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/BUILD.bazel index d6ecb2d..97460f7 100644 --- a/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/BUILD.bazel +++ b/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/BUILD.bazel
@@ -77,6 +77,7 @@ "specialize_dispatches.mlir", "specialize_encodings.mlir", "split_parameter_encoder.mlir", + "split_parameter_encoder_parameter_range.mlir", "split_parameter_encoder_shared_load.mlir", "sync_initializers.mlir", "unify_encoding_for_globals.mlir",
diff --git a/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/CMakeLists.txt b/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/CMakeLists.txt index f9eb0ad..a608fe8 100644 --- a/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/CMakeLists.txt +++ b/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/CMakeLists.txt
@@ -74,6 +74,7 @@ "specialize_dispatches.mlir" "specialize_encodings.mlir" "split_parameter_encoder.mlir" + "split_parameter_encoder_parameter_range.mlir" "split_parameter_encoder_shared_load.mlir" "sync_initializers.mlir" "unify_encoding_for_globals.mlir"
diff --git a/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/split_parameter_encoder_parameter_range.mlir b/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/split_parameter_encoder_parameter_range.mlir new file mode 100644 index 0000000..13e7a34 --- /dev/null +++ b/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/split_parameter_encoder_parameter_range.mlir
@@ -0,0 +1,42 @@ +// RUN: iree-opt --split-input-file --iree-stream-split-parameter-encoder='mode=consolidate output-scope=encoded' %s | FileCheck %s + +// A parameter can refer to a byte range of an archive entry through the +// `offset` and `length` entries of its config. The encoder reads exactly that +// range: the unmodified copy and the input of the hoisted fill both load 1024 +// bytes at offset 4096 of "weight", not the start of the entry. + +// CHECK-LABEL: module @encoder +// CHECK-LABEL: util.func public @__encode_parameters_all( +// CHECK-DAG: %[[LENGTH:.+]] = arith.constant 1024 : index +// CHECK-DAG: %[[MODEL:.+]] = util.buffer.constant : !util.buffer = "model" +// CHECK-DAG: %[[KEY:.+]] = util.buffer.constant : !util.buffer = "weight" +// CHECK-DAG: %[[OFFSET:.+]] = arith.constant 4096 : i64 +// CHECK: stream.async.parameter.load {{.*}} %[[MODEL]]::%[[KEY]][%[[OFFSET]]] : !stream.resource<constant>{%[[LENGTH]]} +// CHECK: stream.async.fill +// CHECK: stream.async.parameter.scatter +// CHECK: stream.async.parameter.load {{.*}} %[[MODEL]]::%[[KEY]][%[[OFFSET]]] : !stream.resource<constant>{%[[LENGTH]]} +// CHECK: stream.async.parameter.scatter +// CHECK: util.return + +// The main module reads only the encoded archive. +// CHECK-LABEL: module @parameter_range +// CHECK-NOT: #stream.parameter.named<"model":: +module @parameter_range { + util.global private @weight : !stream.resource<constant> + util.global private @filled_weight : !stream.resource<constant> + util.initializer { + %c0 = arith.constant 0 : index + %c256 = arith.constant 256 : index + %c1024 = arith.constant 1024 : index + %c42_i32 = arith.constant 42 : i32 + %param = stream.async.constant : !stream.resource<constant>{%c1024} = + #stream.parameter.named<"model"::"weight", {offset = 4096 : i64, length = 1024 : i64}> : vector<1024xi8> + %transferred = stream.async.transfer %param : + !stream.resource<constant>{%c1024} -> !stream.resource<constant>{%c1024} + util.global.store %transferred, @weight : !stream.resource<constant> + %filled = stream.async.fill %c42_i32, %param[%c0 to %c256 for %c256] : + i32 -> %param as !stream.resource<constant>{%c1024} + util.global.store %filled, @filled_weight : !stream.resource<constant> + util.return + } +}