iree: topk: Failed to bufferize op

What happened?

Trying to compile the attached mlir file fails in two different ways. Without --iree-opt-level=O3: err_no03.txt

iree-compile: iree/compiler/src/iree/compiler/Codegen/Dialect/GPU/TargetUtils/ConfigUtils.cpp:147: std::optional<GPUMMASchedule> mlir::iree_compiler::IREE::GPU::getMmaScheduleFromProblemAndTarget(IREE::GPU::TargetAttr, mlir::iree_compiler::GPUMatmulShapeType, bool, bool, bool, bool): Assertion `problem.aType == problem.bType && "expected the same aType and bType."' failed.

With --iree-opt-level=O3: err.txt

topk.txt:73:25: error: failed to bufferize op
    %values, %indices = torch.aten.topk %24, %int2_8, %int-1_9, %true, %true_10 : !torch.vtensor<[?,2,2],f32>, !torch.int, !torch.int, !torch.bool, !torch.bool -> !torch.vtensor<[?,2,2],f32>, !torch.vtensor<[?,2,2],si64>
                        ^
topk.txt:73:25: note: see current operation: %24 = "bufferization.alloc_tensor"(%23) <{operandSegmentSizes = array<i32: 0, 1, 0>}> : (tensor<1x1x2xi64>) -> tensor<1x1x2xi64>

Steps to reproduce your issue

Compiled with

iree-compile topk.txt \
  --iree-hal-target-device=hip[0] \
  --iree-hal-executable-debug-level=3 \
  -o=output.vmfb --iree-hip-target=gfx942 --iree-opt-level=O3

What component(s) does this issue relate to?

No response

Version information

Built from source using iree-3.4.0rc20250423.

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 25 (25 by maintainers)

Commits related to this issue

Most upvoted comments

Problems

  • Case 1: topk where only indices result is unused
  • Case 2: topk where only values result is unused

After some discussion with @MaheshRavishankar @Max191 @qedawkins there was consensus that Case 1 can be fixed easily by removing the unused result. For Case 2, the fix is more involved; the root cause of this problem has to do with teaching flow to tie operands during dispatch formation. But to quickly resolve the deepseek issue, we are also adding a band-aid solution at the Codegen level.

The place to switch memory spaces is gpuRequireMemSpaceAllocationFn

This is probably the right fix for this problem, but I think @krzysz00 is right that we need to tread safely.

  • Another possible fix is to make both results optional and remove whichever is unused from the IR entirely

That is also a fix. Actually in this case we should probably just do that. Its a good point and I missed that.

After chatting with @Max191

  1. we verified that thread distribution is happening after the previous change to sort op
    • Correction: The place to switch memory spaces is gpuRequireMemSpaceAllocationFn
    • Another possible fix is to make both results optional and remove whichever is unused from the IR entirely

After chatting with @MaheshRavishankar the work is to make two changes:

  1. Ensure that there is workgroup and thread distribution happening
  2. Make changes to createGPUInferMemorySpacePass to make the default memory space of unused results to private memory space

Ok, I looked further. I think it would be very hacky to fix that earlier. You will have to create a dummy buffer that is not used anywhere and just makes things fragile. The issue is probably back to codegen. We need to distribute this op to threads and have each thread use a private local memory that hopefully gets promoted to registers in LLVM.

Lets forget about the case of lacking O3 optimizations. Fixing with O3 is higher priority

Let me take an initial look at this today.