Skip to content

Tensors being hoisted to device 0 regardless of original affinity #20427

Closed

Description

@Alex-Vasile

What happened?

Compiling the attached MLIR (or a 2 layer toy llama) to FLOW dialect causes most (but not all) of the weights to be hoisted. However, they all appear to be hoisted by default onto device 0, even if they already had a different affinity explicitly marks.

Steps to reproduce your issue

  1. Compile with
iree-compile model.mlir.txt \
  --iree-hal-target-device=hip[0] --iree-hal-target-device=hip[1] \
  --iree-hal-target-device=hip[2] --iree-hal-target-device=hip[3] \
  --iree-hal-target-device=hip[4] --iree-hal-target-device=hip[5] \
  --iree-hal-target-device=hip[6] --iree-hal-target-device=hip[7] \
  --iree-hal-executable-debug-level=3 \
  -o=flow.mlir -compile-to=flow --iree-hip-target=gfx942
  1. Open flow.mlir and look at the resulting @__hoisted_tensor_

What component(s) does this issue relate to?

No response

Version information

iree-3.4.0rc20250325

Additional context

No response

Activity

  1. added
    bug 🐞
    on Mar 31, 2025
  2. IanWood1 commented on Jul 7, 2025

    @IanWood1
    Member

    It looks like when values are hoisted they are assigned the default affinity. The new global with device 0 affinity is being created here:

    auto globalOp = moduleBuilder.create<IREE::Util::GlobalOp>(
    loc, getHoistedName(globalType), false, globalType);
    moduleSymbols.insert(globalOp);
    SymbolTable::setSymbolVisibility(globalOp,
    SymbolTable::Visibility::Private);
    globalOp->setDialectAttrs(dialectAttrs);

    The value of dialectAttrs is stream.affinity.default = #hal.device.affinity<@__device_0> from the enclosing module. I'm not sure if this is expected. Should util.initializers be running on the default device?

  3. benvanik commented on Jul 14, 2025

    @benvanik
    Collaborator

    When that pass runs changes in affinity are ok - analysis should always get back to a similar result. That is to say, if you're having issues it's probably something further on and not something that needs fixing in HoistIntoGlobals. I'll see if I can take a look and identify what's going on, but if you had some IR to point me at where you're seeing what you don't expect that'll help.

  4. benvanik commented on Jul 16, 2025

    @benvanik
    Collaborator

    AFAICT this is functioning correctly. stream.affinity.default is only used to resolve affinities when they cannot otherwise be assigned via analysis. When hoisting the new initializers capture the default that was used by the origin of the slice being hoisted so they can preserve the behavior and in this case that happens to be device 0: if no hoisting was performed from the origin function and analysis failed device 0 would be used in the same way. Once affinities are assigned all parameters seem to end up in the right place for me. If you think they aren't, please post full compile output.

    e.g.

      util.global private @__hoisted_tensor_256x128xf16_42 : !stream.resource<constant>
      util.global private @__hoisted_tensor_256x128xf16_42__size : index
      util.initializer attributes {stream.affinity.default = #hal.device.affinity<@__device_0>} {
        %__auto.output.weight.shard.0 = util.global.load @__auto.output.weight.shard.0 : !stream.resource<constant>
        %__auto.output.weight.shard.0__size = util.global.load @__auto.output.weight.shard.0__size : index
        %0 = stream.async.transfer %__auto.output.weight.shard.0 : !stream.resource<constant>{%__auto.output.weight.shard.0__size} from(#hal.device.affinity<@__device_0>) -> to(#hal.device.affinity<@__device_0>) !stream.resource<*>{%__auto.output.weight.shard.0__size}
        %1 = stream.tensor.sizeof on(#hal.device.affinity<@__device_0>) tensor<32768xf32> : index
        %2 = stream.tensor.clone on(#hal.device.affinity<@__device_0>) %0 : tensor<256x128xf32> in !stream.resource<*>{%__auto.output.weight.shard.0__size} -> tensor<32768xf32> in !stream.resource<*>{%1}
        %3 = stream.tensor.sizeof on(#hal.device.affinity<@__device_0>) tensor<32768xf16> : index
        %4 = stream.tensor.dispatch on(#hal.device.affinity<@__device_0>) @_initializer_2_dispatch_0::@_initializer_2_dispatch_0_elementwise_32768_f32xf16(%2) : (tensor<32768xf32> in !stream.resource<*>{%1}) -> tensor<32768xf16> in !stream.resource<*>{%3}
        %5 = stream.tensor.sizeof on(#hal.device.affinity<@__device_0>) tensor<256x128xf16> : index
        %6 = stream.tensor.clone on(#hal.device.affinity<@__device_0>) %4 : tensor<32768xf16> in !stream.resource<*>{%3} -> tensor<256x128xf16> in !stream.resource<*>{%5}
        %7 = stream.async.transfer %6 : !stream.resource<*>{%5} from(#hal.device.affinity<@__device_0>) -> to(#hal.device.affinity<@__device_0>) !stream.resource<constant>{%5}
        util.global.store %7, @__hoisted_tensor_256x128xf16_42 : !stream.resource<constant>
        util.global.store %5, @__hoisted_tensor_256x128xf16_42__size : index
        util.return
      }
      util.global private @__hoisted_tensor_256x128xf16_43 : !stream.resource<constant>
      util.global private @__hoisted_tensor_256x128xf16_43__size : index
      util.initializer attributes {stream.affinity.default = #hal.device.affinity<@__device_0>} {
        %__auto.output.weight.shard.1 = util.global.load @__auto.output.weight.shard.1 : !stream.resource<constant>
        %__auto.output.weight.shard.1__size = util.global.load @__auto.output.weight.shard.1__size : index
        %0 = stream.async.transfer %__auto.output.weight.shard.1 : !stream.resource<constant>{%__auto.output.weight.shard.1__size} from(#hal.device.affinity<@__device_1>) -> to(#hal.device.affinity<@__device_1>) !stream.resource<*>{%__auto.output.weight.shard.1__size}
        %1 = stream.tensor.sizeof on(#hal.device.affinity<@__device_1>) tensor<32768xf32> : index
        %2 = stream.tensor.clone on(#hal.device.affinity<@__device_1>) %0 : tensor<256x128xf32> in !stream.resource<*>{%__auto.output.weight.shard.1__size} -> tensor<32768xf32> in !stream.resource<*>{%1}
        %3 = stream.tensor.sizeof on(#hal.device.affinity<@__device_1>) tensor<32768xf16> : index
        %4 = stream.tensor.dispatch on(#hal.device.affinity<@__device_1>) @_initializer_2_dispatch_0::@_initializer_2_dispatch_0_elementwise_32768_f32xf16(%2) : (tensor<32768xf32> in !stream.resource<*>{%1}) -> tensor<32768xf16> in !stream.resource<*>{%3}
        %5 = stream.tensor.sizeof on(#hal.device.affinity<@__device_1>) tensor<256x128xf16> : index
        %6 = stream.tensor.clone on(#hal.device.affinity<@__device_1>) %4 : tensor<32768xf16> in !stream.resource<*>{%3} -> tensor<256x128xf16> in !stream.resource<*>{%5}
        %7 = stream.async.transfer %6 : !stream.resource<*>{%5} from(#hal.device.affinity<@__device_1>) -> to(#hal.device.affinity<@__device_1>) !stream.resource<constant>{%5}
        util.global.store %7, @__hoisted_tensor_256x128xf16_43 : !stream.resource<constant>
        util.global.store %5, @__hoisted_tensor_256x128xf16_43__size : index
        util.return
      }

    (note that in the second initializer all the ops are explicitly on device 1 as intended, as analysis was able to fully resolve their affinities based on usage)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐞

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions