Skip to content

Small bugfix in EmplaceAllocations - #15261

Merged
dan-garvey merged 1 commit into
mainfrom
fix_emplace_alloc_bug
Oct 27, 2023
Merged

dan-garvey merged 1 commit into
mainfrom
fix_emplace_alloc_bug

Conversation

@dan-garvey

Copy link
Copy Markdown
Contributor

Added some checks to avoid insertion point errors. This is a bit of a quick fix, as ideally we would find a way to move entire trees up when possible, but I'll file an issue for that.

TODO add a test for this case

@jpienaar

Copy link
Copy Markdown
Member

Btw this seems to avoid failure on the RNG PR too.

@dan-garvey
dan-garvey force-pushed the fix_emplace_alloc_bug branch from e769ad0 to 28b9f58 Compare October 23, 2023 22:22
@dan-garvey

Copy link
Copy Markdown
Contributor Author

@benvanik given the diff on the lit tests, are our new checks too strict? it seems like a lot less is getting fused. I suppose maybe this is expected until we can move whole branches up the tree?

syntax-colored diff:
https://gist.github.com/dan-garvey/a33703bc2ca33dc5b1f296ff08c6a2f1

@qedawkins qedawkins added benchmarks:cuda Run default CUDA benchmarks benchmarks:x86_64 Run default x86_64 benchmarks benchmarks:android-cpu Run default Android CPU benchmarks benchmarks:android-gpu Run default Android GPU benchmarks benchmarks:vulkan-nvidia Run default Vulkan benchmarks on NVIDIA GPU labels Oct 26, 2023
@qedawkins

Copy link
Copy Markdown
Contributor

(If you push another [empty] commit it will run benchmarks if you want some verification)

@benvanik

Copy link
Copy Markdown
Collaborator

haven't had coffee yet but I'm not sure what I'm looking at in that diff :)
what's what?

@dan-garvey
dan-garvey force-pushed the fix_emplace_alloc_bug branch from 28b9f58 to f45721f Compare October 26, 2023 15:39
@dan-garvey

Copy link
Copy Markdown
Contributor Author

Lit test example:

func.func @emplaceDispatch(
    // CHECK-SAME: %[[INPUT:arg[0-9]+]]: !stream.resource<*>, %[[INPUT_SIZE:arg[0-9]+]]: index,
    %input: !stream.resource<*>, %input_size: index,
    // CHECK-SAME: %[[UPDATE_OFFSET:arg[0-9]+]]: index, %[[UPDATE_SIZE:arg[0-9]+]]: index,
    %update_offset: index, %update_size: index,
    // CHECK-SAME: %[[TARGET:arg[0-9]+]]: !stream.resource<*>, %[[TARGET_SIZE:arg[0-9]+]]: index
    %target: !stream.resource<*>, %target_size: index) -> !stream.resource<*> {
  %c0 = arith.constant 0 : index
  // CHECK-DAG: %[[UPDATE_END:.+]] = arith.addi %[[UPDATE_OFFSET]], %[[UPDATE_SIZE]]
  %update_end = arith.addi %update_offset, %update_size : index
  // CHECK: %[[RESULT:.+]] = stream.async.dispatch @ex::@dispatch(%[[INPUT]][{{.+}}], %[[TARGET]][%[[UPDATE_OFFSET]] to %[[UPDATE_END]] for %[[UPDATE_SIZE]]]) :
  // CHECK-SAME: (!stream.resource<*>{%[[INPUT_SIZE]]}, !stream.resource<*>{%[[TARGET_SIZE]]}) -> %[[TARGET]]{%[[TARGET_SIZE]]}
  %update = stream.async.dispatch @ex::@dispatch(%input[%c0 to %input_size for %input_size]) : (!stream.resource<*>{%input_size}) -> !stream.resource<*>{%update_size}
  // CHECK-NOT: stream.async.update
  %result = stream.async.update %update, %target[%update_offset to %update_end] : !stream.resource<*>{%update_size} -> %target as !stream.resource<*>{%target_size}
  // CHECK: return %[[RESULT]]
  return %result : !stream.resource<*>
}

Before my patch:

  func.func @emplaceDispatch(%arg0: !stream.resource<*>, %arg1: index, %arg2: index, %arg3: index, %arg4: !stream.resource<*>, %arg5: index) -> !stream.resource<*> {
    %c0 = arith.constant 0 : index
    %0 = arith.addi %arg2, %arg3 : index
    %1 = stream.async.dispatch @ex::@dispatch(%arg0[%c0 to %arg1 for %arg1], %arg4[%arg2 to %0 for %arg3]) : (!stream.resource<*>{%arg1}, !stream.resource<*>{%arg5}) -> %arg4{%arg5}
    return %1 : !stream.resource<*>
  }
}

After:

  func.func @emplaceDispatch(%arg0: !stream.resource<*>, %arg1: index, %arg2: index, %arg3: index, %arg4: !stream.resource<*>, %arg5: index) -> !stream.resource<*> {
    %c0 = arith.constant 0 : index
    %0 = arith.addi %arg2, %arg3 : index
    %1 = stream.async.dispatch @ex::@dispatch(%arg0[%c0 to %arg1 for %arg1]) : (!stream.resource<*>{%arg1}) -> !stream.resource<*>{%arg3}
    %2 = stream.async.update %1, %arg4[%arg2 to %0] : !stream.resource<*>{%arg3} -> %arg4 as !stream.resource<*>{%arg5}
    return %2 : !stream.resource<*>
  }
}

Looking at the after, we can see that the update didn't get fused

@benvanik

Copy link
Copy Markdown
Collaborator

yeah that's not good as nothing should prevent that from happening

@benvanik

Copy link
Copy Markdown
Collaborator

I suspect this, which you shouldn't need: (the result of the update will never be usable for the dispatch, as the update always happens after the dispatch)

      if (!IREE::Util::isValueUsableForOp(updateOp.getResult(), dispatchOp))
        break;

Added some checks to avoid insertion point errors. This
is a bit of a quick fix, as ideally we would find a way to move
entire trees up when possible, but I'll file an issue for that
@dan-garvey
dan-garvey force-pushed the fix_emplace_alloc_bug branch from f45721f to 4a99e8e Compare October 26, 2023 15:52
@dan-garvey

Copy link
Copy Markdown
Contributor Author

facepalm

@github-actions

Copy link
Copy Markdown

Abbreviated Benchmark Summary

@ commit 416f88cec83a29b6e756f7abf20a29b4b23b4a28 (vs. base 5610d8cf7c06b71fd3281fb3fa2e8e67fe378d57)

Regressed Latencies 🚩

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
GPT2\_117M\_TF\_1X1XI32(stablehlo) [armv8.2-a-generic-linux\_android29-llvm\_cpu][default-flags] local\_sync(embedded\_elf)[full-inference,default-flags] with zeros @ pixel-6-pro[big-core] 171.935 (vs. 142.461, 20.69%↑) 171.971 0.382
GPT2\_117M\_TF\_1X4XI32(stablehlo) [armv8.2-a-generic-linux\_android29-llvm\_cpu][default-flags] local\_sync(embedded\_elf)[full-inference,default-flags] with zeros @ pixel-6-pro[big-core] 263.821 (vs. 248.270, 6.26%↑) 263.933 0.511
DeepLabV3\_fp32(tflite) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][default-flags] local\_task(embedded\_elf)[1-thread,full-inference,default-flags] with zeros @ c2-standard-16[cpu] 35.679 (vs. 34.198, 4.33%↑) 35.674 0.224

Improved Latencies 🎉

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
MobileBertSquad\_fp16(tflite) [arm-valhall-vulkan\_android31-vulkan\_spirv][experimental-flags,fuse-padding,max-concurrency,repeated-kernel,demote-f32-to-f16] vulkan(none)[full-inference,experimental-flags] with zeros @ pixel-6-pro[gpu] 2.516 (vs. 2.850, 11.70%↓) 2.513 0.021
MobileBertSquad\_int8(tflite) [arm-valhall-vulkan\_android31-vulkan\_spirv][experimental-flags,fuse-padding,max-concurrency] vulkan(none)[full-inference,default-flags] with zeros @ pixel-6-pro[gpu] 66.986 (vs. 75.487, 11.26%↓) 66.958 0.274
MobileBertSquad\_fp16(tflite) [arm-valhall-vulkan\_android31-vulkan\_spirv][default-flags,demote-f32-to-f16] vulkan(none)[full-inference,default-flags] with zeros @ pixel-6-pro[gpu] 72.959 (vs. 81.973, 11.00%↓) 72.927 0.327

[Top 3 out of 14 results showed]

No improved or regressed compilation metrics 🏖️

For more information:

Source Workflow Run

@dan-garvey
dan-garvey marked this pull request as ready for review October 26, 2023 19:19
@dan-garvey

Copy link
Copy Markdown
Contributor Author

Based on the discussion in mai-tai today, should I assume these benchmark changes are spurious?

@qedawkins

Copy link
Copy Markdown
Contributor

They can be noisy so I think so? This might be hard to tell because my guess would be that typically the gains from this pass are relatively small compared to kernels. When you need it it's really important, but those benchmarks are quite noisy. I think if it's passing the lit tests it's fine (+ the change makes sense).

@jpienaar

Copy link
Copy Markdown
Member

If really concerned, try running locally before and after a few runs (would be less noisy). These seem within noise level though. And also without this we get some weird numerical failures, so I'd rather than a hit and not have to chase down numerics and then improve performance.

@qedawkins

Copy link
Copy Markdown
Contributor

If really concerned, try running locally before and after a few runs (would be less noisy). These seem within noise level though. And also without this we get some weird numerical failures, so I'd rather than a hit and not have to chase down numerics and then improve performance.

re: the numerical error, might be good to re-check. The initial version of this patch essentially made it so that the pattern never applied, and with this patch, if the pattern successfully applied before I think it should still apply.

@jpienaar

Copy link
Copy Markdown
Member

Ah good to know, let me check

@benvanik

Copy link
Copy Markdown
Collaborator

Biggest detectable changes would be memory consumption at runtime - did we ever get tracking of that?

@jpienaar

Copy link
Copy Markdown
Member

If really concerned, try running locally before and after a few runs (would be less noisy). These seem within noise level though. And also without this we get some weird numerical failures, so I'd rather than a hit and not have to chase down numerics and then improve performance.

re: the numerical error, might be good to re-check. The initial version of this patch essentially made it so that the pattern never applied, and with this patch, if the pattern successfully applied before I think it should still apply.

And yeah you are correct, now it fails again with wrong result.

@qedawkins

Copy link
Copy Markdown
Contributor

Any reason not to land this though? Seems like it's required for some Turbine/LLM work.

@benvanik

Copy link
Copy Markdown
Collaborator

I think it's good to land but we should make sure we aren't regressing too much on memory consumption without knowing it - this pass not running can lead to 2-10x peak memory consumption and on 7-70B models can be ... real bad :)
The fix is critical for correctness so I wouldn't block it, but it would mean we'd need to prioritize fixing the regression as a follow-on. Being blind to such important metrics sucks :(

@dan-garvey
dan-garvey merged commit f29895e into main Oct 27, 2023
@dan-garvey

Copy link
Copy Markdown
Contributor Author

@benvanik I don't see an issue for tracking memory consumption at runtime. I'll add it and bring it up on the next mai-tai. Seems pretty critical.

ramiro050 pushed a commit to ramiro050/iree that referenced this pull request Dec 19, 2023
Added some checks to avoid insertion point errors. This is a bit of a
quick fix, as ideally we would find a way to move entire trees up when
possible, but I'll file an issue for that.
benvanik added a commit that referenced this pull request Mar 4, 2024
#15261 was aggressive in disabling the pass for any offset/size
SSA values computed as part of updates. In most programs the offset
arith ops would end up after the dispatches we would want to emplace
and this pass wouldn't touch them. Now we try to move the arith ops
up before the dispatch so we can do the placement.
benvanik added a commit that referenced this pull request Mar 4, 2024
#15261 was aggressive in disabling the pass for any offset/size SSA
values computed as part of updates. In most programs the offset arith
ops would end up after the dispatches we would want to emplace and this
pass wouldn't touch them. Now we try to move the arith ops up before the
dispatch so we can do the placement.

Progress on #16647. This allows us to emplace llama2 variable updates
with dynamic offsets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

benchmarks:android-cpu Run default Android CPU benchmarks benchmarks:android-gpu Run default Android GPU benchmarks benchmarks:cuda Run default CUDA benchmarks benchmarks:vulkan-nvidia Run default Vulkan benchmarks on NVIDIA GPU benchmarks:x86_64 Run default x86_64 benchmarks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants