pytorch / executorch

On-device AI across mobile, embedded and edge for PyTorch
https://pytorch.org/executorch/
Other
1.92k stars 312 forks source link

Llama Export #6050

Open Vinaysukhesh98 opened 5 days ago

Vinaysukhesh98 commented 5 days ago

🐛 Describe the bug

python -m examples.models.llama2.export_llama --checkpoint /home/mbuhyd/.llama/checkpoints/Llama3.2-1B/consolidated.00.pth --params /home/mbuhyd/.llama/checkpoints/Llama3.2-1B/params.json -kv --use_sdpa_with_kv_cache -X -d bf16 --metadata '{"append_eos_to_prompt": 0, "get_bos_id":128000, "get_eos_ids":[128009, 128001], "get_n_bos": 0, "get_n_eos": 0}' --output_name="llama3_2.pte" / Pytorch/executorch/examples/models/llama2/model.py:102: FutureWarning: You are using torch.load with weights_only=False (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for weights_only will be flipped to True. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via torch.serialization.add_safe_globals. We recommend you start setting weights_only=True for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature. checkpoint = torch.load(checkpoint_path, map_location=device, mmap=True) Traceback (most recent call last): File "/ Pytorch/executorch/extension/llm/custom_ops/sdpa_with_kv_cache.py", line 22, in op = torch.ops.llama.sdpa_with_kv_cache.default ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/mbuhyd/anaconda3/envs/executorch/lib/python3.11/site-packages/torch/_ops.py", line 1232, in getattr raise AttributeError( AttributeError: '_OpNamespace' 'llama' object has no attribute 'sdpa_with_kv_cache'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 198, in _run_module_as_main File "", line 88, in _run_code File "/ Pytorch/executorch/examples/models/llama2/export_llama.py", line 30, in main() # pragma: no cover ^^^^^^ File "/ Pytorch/executorch/examples/models/llama2/export_llama.py", line 26, in main export_llama(modelname, args) File "/ Pytorch/executorch/examples/models/llama2/export_llama_lib.py", line 476, in export_llama builder = _export_llama(modelname, args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/ Pytorch/executorch/examples/models/llama2/export_llama_lib.py", line 575, in _export_llama _prepare_for_llama_export(modelname, args) File "/ Pytorch/executorch/examples/models/llama2/export_llama_lib.py", line 531, in _prepare_for_llama_export .source_transform(_get_source_transforms(modelname, dtype_override, args)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/ Pytorch/executorch/extension/llm/export/builder.py", line 144, in source_transform self.model = transform(self.model) ^^^^^^^^^^^^^^^^^^^^^ File "/ Pytorch/executorch/examples/models/llama2/source_transformation/sdpa.py", line 102, in replace_sdpa_with_custom_op from executorch.extension.llm.custom_ops import sdpa_with_kv_cache # noqa ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/ Pytorch/executorch/extension/llm/custom_ops/sdpa_with_kv_cache.py", line 28, in assert len(libs) == 1, f"Expected 1 library but got {len(libs)}" ^^^^^^^^^^^^^^ AssertionError: Expected 1 library but got 0

Versions

python collect_env.py Collecting environment information... PyTorch version: 2.6.0.dev20241007+cpu Is debug build: False CUDA used to build PyTorch: None ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.5 LTS (x86_64) GCC version: (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0 Clang version: 14.0.0-1ubuntu1.1 CMake version: version 3.30.4 Libc version: glibc-2.35

Python version: 3.11.10 | packaged by conda-forge | (main, Sep 30 2024, 18:08:57) [GCC 13.3.0] (64-bit runtime) Python platform: Linux-6.8.0-45-generic-x86_64-with-glibc2.35 Is CUDA available: False CUDA runtime version: No CUDA CUDA_MODULE_LOADING set to: N/A GPU models and configuration: No CUDA Nvidia driver version: No CUDA cuDNN version: No CUDA HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 39 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 8 On-line CPU(s) list: 0-7 Vendor ID: GenuineIntel Model name: 11th Gen Intel(R) Core(TM) i7-1195G7 @ 2.90GHz CPU family: 6 Model: 140 Thread(s) per core: 2 Core(s) per socket: 4 Socket(s): 1 Stepping: 2 CPU max MHz: 5000.0000 CPU min MHz: 400.0000 BogoMIPS: 5836.80 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l2 cdp_l2 ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid movdiri movdir64b fsrm avx512_vp2intersect md_clear ibt flush_l1d arch_capabilities Virtualization: VT-x L1d cache: 192 KiB (4 instances) L1i cache: 128 KiB (4 instances) L2 cache: 5 MiB (4 instances) L3 cache: 12 MiB (1 instance) NUMA node(s): 1 NUMA node0 CPU(s): 0-7 Vulnerability Gather data sampling: Mitigation; Microcode Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Not affected Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Not affected Vulnerability Spec rstack overflow: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI SW loop, KVM SW loop Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Not affected

Versions of relevant libraries: [pip3] executorch==0.5.0a0+e540bcb [pip3] numpy==1.23.2 [pip3] torch==2.6.0.dev20241007+cpu [pip3] torchao==0.5.0+git0916b5b [pip3] torchaudio==2.5.0.dev20241007+cpu [pip3] torchsr==1.0.4 [pip3] torchvision==0.20.0.dev20241007+cpu [conda] executorch 0.5.0a0+e540bcb pypi_0 pypi [conda] numpy 1.23.2 pypi_0 pypi [conda] torch 2.6.0.dev20241007+cpu pypi_0 pypi [conda] torchaudio 2.5.0.dev20241007+cpu pypi_0 pypi [conda] torchsr 1.0.4 pypi_0 pypi [conda] torchvision 0.20.0.dev20241007+cpu pypi_0 pypi

lucylq commented 3 days ago

Hi @Vinaysukhesh98 , thanks for trying out executorch. Looks like the sdpa custom op library cannot be found. Can you make sure executorch has been set up correctly, and try again?

Following llama 2 setup instructions:

Set up executorch:

# Clone the ExecuTorch repo from GitHub
git clone https://github.com/pytorch/executorch.git
cd executorch

# Update and pull submodules
git submodule sync
git submodule update --init

# Install ExecuTorch pip package and its dependencies, as well as
# development tools like CMake.
# If developing on a Mac, make sure to install the Xcode Command Line Tools first.
./install_requirements.sh --pybind xnnpack

Install the llama2 dependencies:

examples/models/llama2/install_requirements.sh

To test that the sdpa custom op library has been installed correctly, you can try this:

python
>>> from executorch.extension.pybindings import portable_lib
>>> from executorch.extension.llm.custom_ops import sdpa_with_kv_cache

And ensure it succeeds.

theoctopusride commented 3 days ago
(executorch) apps@apps-desktop:~/executorch$ python3
Python 3.10.0 (default, Mar  3 2022, 09:58:08) [GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from executorch.extension.pybindings import portable_lib
/home/apps/executorch/exir/dialects/edge/_ops.py:9: UserWarning: Module executorch was already imported from None, but /home/apps/executorch is being added to sys.path
  import pkg_resources
>>> from executorch.extension.llm.custom_ops import sdpa_with_kv_cache
Traceback (most recent call last):
  File "/home/apps/executorch/extension/llm/custom_ops/sdpa_with_kv_cache.py", line 22, in <module>
    op = torch.ops.llama.sdpa_with_kv_cache.default
  File "/home/apps/miniconda3/envs/executorch/lib/python3.10/site-packages/torch/_ops.py", line 1232, in __getattr__
    raise AttributeError(
AttributeError: '_OpNamespace' 'llama' object has no attribute 'sdpa_with_kv_cache'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/apps/executorch/extension/llm/custom_ops/sdpa_with_kv_cache.py", line 28, in <module>
    assert len(libs) == 1, f"Expected 1 library but got {len(libs)}"
AssertionError: Expected 1 library but got 0
>>> exit()
(executorch) apps@apps-desktop:~/executorch$ python3
Python 3.10.0 (default, Mar  3 2022, 09:58:08) [GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.__version__
'2.6.0.dev20241007+cpu'
>>> 
lucylq commented 4 hours ago

Hey @theoctopusride thanks for trying it out. Could you please share your installation process? Did you run examples/models/llama2/install_requirements.sh?

theoctopusride commented 52 minutes ago

Yes I ran both

./install_requirements.sh --pybind xnnpack

and

examples/models/llama2/install_requirements.sh