-
Notifications
You must be signed in to change notification settings - Fork 171
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
Adding metal_utils for iree_utils #1561
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
959be51
Adding metal_utils for iree_utils
Ranvirsv 9efacaa
Add patch for making compile API work for both MEGABYTE and MiniGPT4 …
Abhishek-Varma d49b157
[SD] Update unet in_channels API and add PIL metadata to spec. (#1560)
monorimet 948cef5
Fixing iree-metal-target-platform
Ranvirsv 51f98b9
adding metal to txt2img pipeline
Ranvirsv 9556e69
Merge branch 'main' into metal_for_shark
Ranvirsv 2cf12ea
Fixing Copyright date
Ranvirsv 89d3c17
removing debug prints
Ranvirsv 6fa8404
black lint formating
Ranvirsv 44abd93
Merge branch 'main' into metal_for_shark
Ranvirsv 88753b9
fixing device dump
Ranvirsv 126abcd
Merge branch 'main' into metal_for_shark
powderluv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Copyright 2023 The Nod Team. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# All the iree_vulkan related functionalities go here. | ||
|
||
from os import linesep | ||
from shark.iree_utils._common import run_cmd | ||
import iree.runtime as ireert | ||
from sys import platform | ||
from shark.iree_utils.vulkan_target_env_utils import get_vulkan_target_env_flag | ||
|
||
|
||
def get_metal_device_name(device_num=0): | ||
vulkaninfo_dump, _ = run_cmd("vulkaninfo") | ||
vulkaninfo_dump = vulkaninfo_dump.split(linesep) | ||
vulkaninfo_list = [s.strip() for s in vulkaninfo_dump if "deviceName" in s] | ||
if len(vulkaninfo_list) == 0: | ||
raise ValueError("No device name found in VulkanInfo!") | ||
if len(vulkaninfo_list) > 1: | ||
print("Following devices found:") | ||
for i, dname in enumerate(vulkaninfo_list): | ||
print(f"{i}. {dname}") | ||
print(f"Choosing device: {vulkaninfo_list[device_num]}") | ||
return vulkaninfo_list[device_num] | ||
|
||
|
||
def get_os_name(): | ||
if platform.startswith("linux"): | ||
return "linux" | ||
elif platform == "darwin": | ||
return "macos" | ||
elif platform == "win32": | ||
return "windows" | ||
else: | ||
print("Cannot detect OS type, defaulting to linux.") | ||
return "linux" | ||
|
||
|
||
def get_metal_target_triple(device_name): | ||
"""This method provides a target triple str for specified vulkan device. | ||
|
||
Args: | ||
device_name (str): name of the hardware device to be used with vulkan | ||
|
||
Returns: | ||
str or None: target triple or None if no match found for given name | ||
""" | ||
# Apple Targets | ||
if all(x in device_name for x in ("Apple", "M1")): | ||
triple = "m1-moltenvk-macos" | ||
elif all(x in device_name for x in ("Apple", "M2")): | ||
triple = "m1-moltenvk-macos" | ||
|
||
else: | ||
triple = None | ||
return triple | ||
|
||
|
||
def get_metal_triple_flag(device_name="", device_num=0, extra_args=[]): | ||
for flag in extra_args: | ||
if "-iree-metal-target-platform=" in flag: | ||
print(f"Using target triple {flag.split('=')[1]}") | ||
return None | ||
|
||
if device_name == "" or device_name == [] or device_name is None: | ||
metal_device = get_metal_device_name(device_num=device_num) | ||
else: | ||
metal_device = device_name | ||
triple = get_metal_target_triple(metal_device) | ||
if triple is not None: | ||
print( | ||
f"Found metal device {metal_device}. Using metal target triple {triple}" | ||
) | ||
return f"-iree-metal-target-triple={triple}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, missed that one |
||
print( | ||
"""Optimized kernel for your target device is not added yet. | ||
Contact SHARK Admin on discord[https://discord.com/invite/RUqY2h2s9u] | ||
or pull up an issue.""" | ||
) | ||
print(f"Target : {metal_device}") | ||
return None | ||
|
||
|
||
def get_iree_metal_args(device_num=0, extra_args=[]): | ||
# res_metal_flag = ["--iree-flow-demote-i64-to-i32"] | ||
|
||
res_metal_flag = [] | ||
metal_triple_flag = None | ||
for arg in extra_args: | ||
if "-iree-metal-target-platform=" in arg: | ||
print(f"Using target triple {arg} from command line args") | ||
meatal_triple_flag = arg | ||
break | ||
|
||
if metal_triple_flag is None: | ||
meatal_triple_flag = get_metal_triple_flag( | ||
device_num=device_num, extra_args=extra_args | ||
) | ||
|
||
if meatal_triple_flag is not None: | ||
vulkan_target_env = get_vulkan_target_env_flag(meatal_triple_flag) | ||
res_metal_flag.append(vulkan_target_env) | ||
return res_metal_flag | ||
|
||
|
||
def set_iree_metal_runtime_flags(flags): | ||
for flag in flags: | ||
ireert.flags.parse_flags(flag) | ||
return |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this wont work. I used
But I think Lei suggested using
--dump_devices