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 5 commits into
base: main
Choose a base branch
from
Open

@mtlprintf #418

wants to merge 5 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

@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

I 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.

@christiangnrd christiangnrd mentioned this pull request Sep 18, 2024
2 tasks
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 Show resolved Hide resolved

Metal places output from `@mtlprintf` into a log buffer. The system only removes the messages from the log buffer when the command buffer completes. When the log buffer becomes full, the system drops all subsequent messages.

See also: `@mtlprint`, `@mtlprintln` and `@mtlshow`
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also add the printing docstrings in the API section of the Docs?

@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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants