Skip to content

[Experimental] Enable kleidi AI examples to run on graviton3 #1721

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

akote123
Copy link

@akote123 akote123 commented Feb 17, 2025

Enable kleidi AI int4 experimental features to run in graviton3.

cc: @metascroy

Copy link

pytorch-bot bot commented Feb 17, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/1721

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot
Copy link
Contributor

Hi @akote123!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 17, 2025
@metascroy
Copy link
Contributor

Hi @akote123, thanks for the PR!

Can you tell me more about which kernels you're trying to run on graviton3?

@akote123
Copy link
Author

akote123 commented Feb 24, 2025

Hi @akote123, thanks for the PR!

Can you tell me more about which kernels you're trying to run on graviton3?

@metascroy ,
I wanted to use torch ao experimental features to quantize the model with int4 kleidi kernels and run quantized model in pytorch.

@akote123 akote123 force-pushed the aruna/aarch64_kleidi branch from 59511cd to 456fecc Compare February 24, 2025 05:39
@akote123 akote123 requested a review from metascroy February 24, 2025 05:41
@metascroy
Copy link
Contributor

metascroy commented Mar 4, 2025

Hi @akote123, thanks for the PR!
Can you tell me more about which kernels you're trying to run on graviton3?

@metascroy , I wanted to use torch ao experimental features to quantize the model with int4 kleidi kernels and run quantized model in pytorch.

Hi @akote123, so there are two kinds of KleidiAI int4 kernels available. One kind is availble in PyTorch itself and models with it can be quantized like this: https://github.com/pytorch/ao/blob/main/torchao/experimental/tests/test_packed_linear_int8_dynamic_activation_intx_weight_layout_target_aten.py#L48-L60

The other belongs in torchao experimental kernels (#1826) and can be built by running:

USE_CPP=1 TORCHAO_BUILD_CPU_AARCH64=1 TORCHAO_BUILD_KLEIDIAI=1 pip install .

from the ao directory (Note that TORCHAO_BUILD_CPU_AARCH64 is automatically set on Arm-based Mac machines).

You can see how to quantize a model using these kernels here: https://github.com/pytorch/ao/blob/main/torchao/experimental/tests/test_int8_dynamic_activation_intx_weight.py#L62-L72 (KleidiAI kernels will only be used with int4, has_weight_zeros=false; otherwise our "universal" kernels will be used. If you build with TORCHAO_BUILD_KLEIDIAI=0, our universal kernels will be used instead of KleidiAI for int4/has_weight_zeros=false, too).

@vctrmn
Copy link

vctrmn commented Apr 28, 2025

Hi @metascroy !

I've been trying to follow your instructions to get KleidiAI int4 kernels working on a Scaleway ARM instance (4x16), but I'm still encountering issues.

I've done the following:

Built and installed KleidiAI (the library is installed at /usr/local/lib/libkleidiai.a)
Built torchao with the flags you mentioned: USE_CPP=1 TORCHAO_BUILD_CPU_AARCH64=1 TORCHAO_BUILD_KLEIDIAI=1 pip install .

However, when I try to run code that uses the KleidiAI kernels, I get this error:

AttributeError: '_OpNamespace' 'torchao' object has no attribute '_pack_8bit_act_4bit_weight'

Exception: TorchAO experimental kernels are not loaded. To install the kernels, run `USE_CPP=1 pip install .` from ao on a machine with an ARM CPU. You can also set target to 'aten' if you are using ARM CPU.

My CPU definitely has the required ARM features (verified with /proc/cpuinfo):

Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs

Including: asimd (NEON), asimddp (Dot Product), etc.

Is there something specific I need to do to get the _pack_8bit_act_4bit_weight operator registered? Are there any diagnostic steps I can take to debug this further?

@metascroy
Copy link
Contributor

metascroy commented Apr 28, 2025

Hi @metascroy !

I've been trying to follow your instructions to get KleidiAI int4 kernels working on a Scaleway ARM instance (4x16), but I'm still encountering issues.

I've done the following:

Built and installed KleidiAI (the library is installed at /usr/local/lib/libkleidiai.a) Built torchao with the flags you mentioned: USE_CPP=1 TORCHAO_BUILD_CPU_AARCH64=1 TORCHAO_BUILD_KLEIDIAI=1 pip install .

However, when I try to run code that uses the KleidiAI kernels, I get this error:

AttributeError: '_OpNamespace' 'torchao' object has no attribute '_pack_8bit_act_4bit_weight'

Exception: TorchAO experimental kernels are not loaded. To install the kernels, run `USE_CPP=1 pip install .` from ao on a machine with an ARM CPU. You can also set target to 'aten' if you are using ARM CPU.

My CPU definitely has the required ARM features (verified with /proc/cpuinfo):

Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs

Including: asimd (NEON), asimddp (Dot Product), etc.

Is there something specific I need to do to get the _pack_8bit_act_4bit_weight operator registered? Are there any diagnostic steps I can take to debug this further?

We currently only enable them on mac platform (https://github.com/pytorch/ao/blob/main/setup.py#L52C1-L56). We could probably relax this condition for linux by dropping the "platform.system() == "Darwin" condition.

Btw: when adding the flag TORCHAO_BUILD_KLEIDIAI=1, you do not need to build/install kleidiai separately. setup.py will do it.

@vctrmn
Copy link

vctrmn commented Apr 29, 2025

@metascroy,

Thanks for your response! I've tried implementing your suggested fix :

build_torchao_experimental = (
    use_cpp == "1"
    and platform.machine().startswith("arm64")
    and (platform.system() == "Darwin" or platform.system() == "Linux")
)

I then did a complete rebuild:

  • rm -rf build/ dist/ *.egg-info/
  • TORCHAO_BUILD_CPU_AARCH64=1 TORCHAO_BUILD_KLEIDIAI=1 USE_CPP=1 python setup.py bdist_wheel
  • TORCHAO_BUILD_CPU_AARCH64=1 TORCHAO_BUILD_KLEIDIAI=1 USE_CPP=1 pip install dist/torchao-*.whl --force-reinstall

However, I'm still getting the same error:

AttributeError: '_OpNamespace' 'torchao' object has no attribute '_pack_8bit_act_4bit_weight'
...
Exception: TorchAO experimental kernels are not loaded.  To install the kernels, run `USE_CPP=1 pip install .` from ao on a machine with an ARM CPU. You can also set target to 'aten' if you are using ARM CPU.

This suggests that the KleidiAI operators aren't being registered properly or aren't loading correctly ?

@metascroy
Copy link
Contributor

@metascroy,

Thanks for your response! I've tried implementing your suggested fix :

build_torchao_experimental = (
    use_cpp == "1"
    and platform.machine().startswith("arm64")
    and (platform.system() == "Darwin" or platform.system() == "Linux")
)

I then did a complete rebuild:

  • rm -rf build/ dist/ *.egg-info/
  • TORCHAO_BUILD_CPU_AARCH64=1 TORCHAO_BUILD_KLEIDIAI=1 USE_CPP=1 python setup.py bdist_wheel
  • TORCHAO_BUILD_CPU_AARCH64=1 TORCHAO_BUILD_KLEIDIAI=1 USE_CPP=1 pip install dist/torchao-*.whl --force-reinstall

However, I'm still getting the same error:

AttributeError: '_OpNamespace' 'torchao' object has no attribute '_pack_8bit_act_4bit_weight'
...
Exception: TorchAO experimental kernels are not loaded.  To install the kernels, run `USE_CPP=1 pip install .` from ao on a machine with an ARM CPU. You can also set target to 'aten' if you are using ARM CPU.

This suggests that the KleidiAI operators aren't being registered properly or aren't loading correctly ?

Moving conversation to issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants