Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@mtlprintf #418

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

@mtlprintf #418

wants to merge 12 commits into from

Conversation

tgymnich
Copy link
Member

@tgymnich tgymnich commented Sep 16, 2024

Implement @mtlprintf and friends using os_log

TODO:

  • Printing of floats does not work since they will be converted to doubles due to the vararg calling convention which will be caught by our IR checker
  • Version check the @mtlprintf macro
  • Add tests
  • Capturing and logging are mutually exclusive

depends on: JuliaGPU/GPUCompiler.jl#630
notify: #226

@tgymnich tgymnich self-assigned this Sep 16, 2024
@tgymnich tgymnich marked this pull request as ready for review September 16, 2024 12:12
@tgymnich
Copy link
Member Author

@maleadt Any idea how we can implement the version check for the @mtlprintf macro? I know we could check the air version inside the kernel but I'd like to avoid that.

Also can we get rid of the double check in check_ir?

@christiangnrd
Copy link
Contributor

Would it be worth benchmarking the performance difference between having logging active vs not?

@tgymnich
Copy link
Member Author

tgymnich commented Sep 17, 2024

Would it be worth benchmarking the performance difference between having logging active vs not?

@christiangnrd Sure. I don't expect there to be much overhead besides allocation of the log buffer and checking it for logs after running a kernel. But we might want to look into only conditionally adding MTLLogState since logging also prevents GPU frame capture.

@christiangnrd christiangnrd mentioned this pull request Sep 17, 2024
2 tasks
@maleadt
Copy link
Member

maleadt commented Sep 17, 2024

@maleadt Any idea how we can implement the version check for the @mtlprintf macro? I know we could check the air version inside the kernel but I'd like to avoid that.

Given that the macro expands way to early, I don't think there's anything we can do but checking in the kernel. Why are you opposed to that? GPUCompiler.jl has infrastructure to optimize those checks away, see e.g. how CUDA.jl exposes the device capability and PTX ISA version to the kernel.

@tgymnich
Copy link
Member Author

We could also wrap the macro and accompanying functions in if Metal.macos_version() >= v"15".

@christiangnrd
Copy link
Contributor

christiangnrd commented Sep 18, 2024

If we do that we should have definitions in both cases and give an informative error if Metal.macos_version() < v"15".

@maleadt
Copy link
Member

maleadt commented Sep 18, 2024

Actually, looks like I provided the run-time queries already:

@device_function @inline metal_version() = SimpleVersion(metal_major(), metal_minor())
@device_function @inline air_version() = SimpleVersion(air_major(), air_minor())

So we can just use that in the generated code, generating an error when emitting code for an older platform. That of course depends on #416 for meaningful reporting, but we'll get there.

I'd rather not simply check based on the macOS version during macro expansion, since we might want to target older Metal versions than the system supports.

Copy link
Contributor

@christiangnrd christiangnrd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! However do you know what's causing the tests to hang?

lib/mtl/command_queue.jl Outdated Show resolved Hide resolved
docs/src/usage/kernel.md Show resolved Hide resolved
@tgymnich
Copy link
Member Author

tgymnich commented Sep 19, 2024

Looks good! However do you know what's causing the tests to hang?

@christiangnrd The hangs are caused by this one line:

@print_and_throw "@mtlprintf requires Metal 3.2 (macOS 15) or higher"

test/output.jl Outdated Show resolved Hide resolved
@christiangnrd
Copy link
Contributor

christiangnrd commented Sep 21, 2024

@maleadt Could we have one of the Apple Silicon runners upgraded to Sequoia so the output tests don't get ignored? Edit: All the runners are running 13.3.1. Should we also have one on macOS 14?

I would also like to see #420 merged first (with benchmarks run on macOS 15) to see how big the impact of enabling logging is.

@tgymnich
Copy link
Member Author

@christiangnrd I recently made changes so that logging (e.g. MTLLogState and friends) is only enabled whenever we actually use the feature.

@christiangnrd
Copy link
Contributor

Just pushed a whitespace-only formatting commit

@christiangnrd
Copy link
Contributor

@christiangnrd I recently made changes so that logging (e.g. MTLLogState and friends) is only enabled whenever we actually use the feature.

In that case I still think we should be able to test on macOS 15, but I think we should merge this as soon as it's ready.

@maleadt
Copy link
Member

maleadt commented Sep 23, 2024

I've upgraded one of the workers to macOS 15:
image

See the macos_version tag which can be used to select on this.

test/output.jl Outdated Show resolved Hide resolved
@christiangnrd
Copy link
Contributor

christiangnrd commented Feb 4, 2025

@tgymnich Tests now seem to be passing locally with the 2 suggestions I just made. Is this still blocked?

I'll take care of the rebase since the conflicts are my fault.

@tgymnich
Copy link
Member Author

tgymnich commented Feb 5, 2025

This is awesome! Thank you so much @christiangnrd.

@tgymnich tgymnich requested review from christiangnrd and maleadt and removed request for maleadt February 5, 2025 00:09
Copy link
Contributor

@christiangnrd christiangnrd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Make sure to check out my commit to make sure I didn't make any undesired changes.

Oh and I guess formatting. I personally wouldn't touch the suggestions for the @objc calls or the test indenting suggestions, but I'll leave it up to you to decide which ones you want to implement (unless @maleadt feels otherwise)

Hopefully #527 will allow us to clean up all the version gates but for now they're necessary.

lib/mtl/log_state.jl Show resolved Hide resolved
@maleadt
Copy link
Member

maleadt commented Feb 6, 2025

How is this not blocked by #433 anymore?

@tgymnich
Copy link
Member Author

tgymnich commented Feb 7, 2025

@maleadt #433 is still an issue on some older devices.
However the main issue with this PR (mentioned here: #418 (comment)) can be resolved by using Core.print instead of Base.print in the command buffer callback.

@maleadt
Copy link
Member

maleadt commented Feb 10, 2025

However the main issue with this PR (mentioned here: #418 (comment)) can be resolved by using Core.print instead of Base.print in the command buffer callback.

I'm having a hard time understanding that. If switching to a non-yielding print fixes the issue, it implies we were running into #532. But that doesn't match you mentioning 100% GPU usage in #433?

@christiangnrd
Copy link
Contributor

FWIW I can replicate the hang but not the 100% GPU usage mentioned in #433.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kernels Things about kernels and how they are compiled.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants