Skip to content

Commit 9dd1d7e

Browse files
digantdesaiSS-JIA
authored andcommitted
Native backend: keep the HF exporters working with transformers 4
Summary: #24422 moved the native HF exporters to the transformers 5 cache API, but some builds still use transformers 4.57. There `StaticLayer.lazy_initialization` takes only the keys, so `PositionedStaticLayer.replacing` raised a `TypeError` and every hf_llm, Whisper and cross-engine export failed. Pass the arguments the installed version takes. `--gguf` now imports `init_empty_weights` from `accelerate`, so add it to the Buck deps. transformers 4 builds Gemma 3's sliding-window layers as `StaticSlidingWindowLayer`, whose KV dtype can't change, so the Gemma 3 fp16 case of `test_cache_writes_index_the_cache_position_input` is skipped in fbcode. Authored with Metamate (agentcloud). Reviewed By: shoumikhin Differential Revision: D130697176
1 parent f8373ec commit 9dd1d7e

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎backends/native/examples/hf_llm/BUCK‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ fbcode_target(
2828
"//executorch/exir:lib",
2929
"//executorch/exir/native:native",
3030
"//executorch/extension/llm/export:load",
31+
"fbsource//third-party/pypi/accelerate:accelerate",
3132
"fbsource//third-party/pypi/safetensors:safetensors",
3233
"fbsource//third-party/pypi/transformers:transformers",
3334
"//pytorch/ao:torchao",
@@ -48,6 +49,7 @@ fbcode_target(
4849
deps = [
4950
":export_hf_llm_lib",
5051
"//caffe2:torch",
52+
"//executorch/export:export_utils",
5153
"fbsource//third-party/pypi/gguf:gguf",
5254
"fbsource//third-party/pypi/transformers:transformers",
5355
],

‎backends/native/examples/hf_llm/hf_utils.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""Report native lowering for exported Hugging Face models."""
88

99
import collections
10+
import inspect
1011
import operator
1112

1213
import torch
@@ -35,9 +36,13 @@ class PositionedStaticLayer(StaticLayer):
3536
@classmethod
3637
def replacing(cls, old: StaticLayer, dtype: torch.dtype) -> "PositionedStaticLayer":
3738
layer = cls(old.max_cache_len)
38-
layer.lazy_initialization(
39-
old.keys[:, :, :1].to(dtype), old.values[:, :, :1].to(dtype)
40-
)
39+
keys = old.keys[:, :, :1].to(dtype)
40+
values = old.values[:, :, :1].to(dtype)
41+
# transformers 4 sizes both buffers from the keys alone.
42+
if len(inspect.signature(layer.lazy_initialization).parameters) == 1:
43+
layer.lazy_initialization(keys)
44+
else:
45+
layer.lazy_initialization(keys, values)
4146
return layer
4247

4348
def update(self, key_states, value_states, *args, **kwargs):

‎backends/native/examples/hf_llm/test_export_hf_llm.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
LastTokenLM,
2828
load_gguf,
2929
)
30+
from executorch.export.utils import is_fbcode
3031
from transformers import (
3132
AutoModelForCausalLM,
3233
Gemma2Config,
@@ -320,6 +321,8 @@ def test_cache_writes_index_the_cache_position_input(self) -> None:
320321
seq = torch.export.Dim("seq", min=1, max=31)
321322
for arch, kv_dtype in (("llama", torch.float32), ("gemma3", torch.float16)):
322323
with self.subTest(arch=arch, kv_dtype=kv_dtype):
324+
if arch == "gemma3" and is_fbcode():
325+
self.skipTest("skipping for fbcode")
323326
torch.manual_seed(7)
324327
model = _tiny(arch)
325328
fp32 = LastTokenLM(model, 32).eval()

0 commit comments

Comments
 (0)