Add local kernel loading support to KernelConfig().#42800
Conversation
|
cc @MekkCyber |
|
Hi @zheliuyu ! Thank you for this nice PR, let me know when it's ready |
|
After submitting the draft yesterday, some unknown CI errors appeared, but they seem to have been resolved now. So, this PR is ready for review. @MekkCyber @Rocketknight1 Let me offer some context for the review:
|
| 2. Each kernel value is either a string of the form 'org/repo:layer_name' or a dict mapping device types ("cuda", "rocm", "xpu", "npu") to such strings. | ||
| 3. Each device key in a dict is one of "cuda", "rocm", "xpu", or "npu". | ||
| 4. Each repo_name is a valid repository and layer name in the format 'org/repo:layer_name' (i.e., a string containing both a slash and a colon). | ||
| 5. If a local path is detected, it should be in the format '/abs-path:package_name:layer_name'. |
There was a problem hiding this comment.
let's just simplify to /abs-path:layer_name
There was a problem hiding this comment.
Thanks for your reply.
For LocalLayerRepository, the package_name parameter appears to be required. https://github.com/huggingface/kernels/blob/main/src/kernels/layer/layer.py#L119
There was a problem hiding this comment.
but we should be able to get it from the path directly no ?
|
Cool! Commenting to add some docs for this once #42277 is merged |
|
@MekkCyber Simplified kernel path. Test passedkernel_mapping = {
"RMSNorm":
"/root/liger_kernels:LigerRMSNorm",
}
kernel_config = KernelConfig(kernel_mapping)
# Load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto",
kernel_config=kernel_config
) |
| if kernel is not None and kernel[0] == "/": | ||
| self.check_kernel_from_local = True |
There was a problem hiding this comment.
shouldn't be done here, let's ask the user to do this instead, in the __init__ we add an other argument use_local_kernels
There was a problem hiding this comment.
It's a good idea to let users decide on local kernel loading. 🍻
Co-authored-by: Mohamed Mekkouri <[email protected]>
MekkCyber
left a comment
There was a problem hiding this comment.
Thanks for iterating! only few nits and good to go
| inherit_mapping = not self.use_local_kernel | ||
| with use_kernel_mapping(kernel_config.kernel_mapping, inherit_mapping=inherit_mapping): |
There was a problem hiding this comment.
| inherit_mapping = not self.use_local_kernel | |
| with use_kernel_mapping(kernel_config.kernel_mapping, inherit_mapping=inherit_mapping): | |
| inherit_mapping = not kernel_config.use_local_kernel | |
| with use_kernel_mapping(kernel_config.kernel_mapping, inherit_mapping=inherit_mapping): |
| def __init__(self, kernel_mapping={}): | ||
| self.kernel_mapping = kernel_mapping | ||
| self.registered_layer_names = {} | ||
| self.use_local_kernel = False |
There was a problem hiding this comment.
we need to set self.use_local_kernel to use_local_kernel passed in the constructor, and defaults to False
Test passedkernel_mapping = {
"RMSNorm":
"/root/liger_kernels:LigerRMSNorm",
}
+ kernel_config = KernelConfig(kernel_mapping, use_local_kernel=True)
# Load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto",
use_kernels=True,
kernel_config=kernel_config
) |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
* add add_to_mapping_local for KernelConfig * refactor kernel_mapping format * lint code * specify the kernel path * fix `abs/path` Co-authored-by: Mohamed Mekkouri <[email protected]> * rename check_kernel_from_local to use_local_kernel --------- Co-authored-by: Mohamed Mekkouri <[email protected]>
What does this PR do?
Add local kernel loading support to transformers.KernelConfig().
Relaed issue: #42739
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Test
Output: