Skip to content

Commit cb69335

Browse files
[executorch][muse-glimmer] Route DFlash sampling through CUDA PTE (pytorch#23146)
This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: pytorch#23087 by @Gasoonjia ^ Please use this as the source of truth for the PR details, comments, and reviews ghstack PR base: https://github.com/pytorch/executorch/tree/gh/Gasoonjia/31/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/Gasoonjia/31/head Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/Gasoonjia/30/orig Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/Gasoonjia/31/orig Differential Revision: [D120071927](https://our.internmc.facebook.com/intern/diff/D120071927/) @diff-train-skip-merge --------- Co-authored-by: gasoonjia <gasoonjia@icloud.com> Co-authored-by: Gasoonjia <gasoonjia@meta.com>
1 parent b9b16d4 commit cb69335

5 files changed

Lines changed: 463 additions & 51 deletions

File tree

‎examples/models/muse-glimmer/README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ advanced inputs.
122122
| DFlash | CUDA | `target_forward_from_embeddings`, `target_prefill_from_embeddings`, `embed_text`, `draft_forward`, `draft_prefill`, `dflash_sample_tokens`, `dflash_verify_speculative` |
123123
| DFlash | MLX | `target_forward_from_embeddings`, `embed_text`, `draft_forward` |
124124

125-
Vision adds `vision_encoder` to each method set.
125+
Vision adds `vision_encoder` to each method set. CUDA DFlash samples draft
126+
proposals, verifies them, and samples corrections through its two
127+
`dflash_*` methods, so vocabulary-wide logits and probabilities stay on the
128+
device; MLX DFlash samples on the host.
126129

127130
## Build the runners
128131

‎examples/models/muse-glimmer/export/export_dflash.py‎

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414

1515
import argparse
1616
import gc
17+
from typing import TYPE_CHECKING
1718

1819
import torch
19-
2020
from executorch.examples.models.muse_glimmer.export import common
2121

22+
if TYPE_CHECKING:
23+
from executorch.exir.passes.propagate_device_pass import PropagateDeviceConfig
24+
2225

2326
def validate_dflash_export_options(backend: str) -> None:
2427
if backend not in {"cuda", "mlx"}:
@@ -92,6 +95,26 @@ def _export_cuda_sampler_methods(
9295
}
9396

9497

98+
def _cuda_propagate_device_config() -> dict[str, PropagateDeviceConfig]:
99+
"""Keeps the speculative chain's vocabulary-wide tensors on the device."""
100+
from executorch.exir.passes.propagate_device_pass import PropagateDeviceConfig
101+
102+
device_resident = PropagateDeviceConfig(
103+
skip_h2d_for_method_inputs=True,
104+
skip_d2h_for_method_outputs=True,
105+
)
106+
draft = PropagateDeviceConfig(skip_d2h_for_method_outputs=True)
107+
return {
108+
"embed_text": device_resident,
109+
"target_forward_from_embeddings": device_resident,
110+
"target_prefill_from_embeddings": device_resident,
111+
"draft_forward": draft,
112+
"draft_prefill": draft,
113+
"dflash_sample_tokens": device_resident,
114+
"dflash_verify_speculative": device_resident,
115+
}
116+
117+
95118
def export_dflash(
96119
output_dir: str,
97120
target_gguf: str | None = None,
@@ -326,9 +349,10 @@ def _export_dflash_mlx(
326349
print("=" * 60)
327350
print("Exporting target_forward_from_embeddings...")
328351
print("=" * 60)
329-
with common.BoundMethodForward(
330-
combined, combined.target_forward_from_embeddings
331-
), torch.no_grad():
352+
with (
353+
common.BoundMethodForward(combined, combined.target_forward_from_embeddings),
354+
torch.no_grad(),
355+
):
332356
target_from_embeddings_ep = export(
333357
combined,
334358
(
@@ -497,7 +521,6 @@ def _export_dflash_cuda(
497521
)
498522
from executorch.exir.backend.compile_spec_schema import CompileSpec
499523
from executorch.exir.passes import MemoryPlanningPass
500-
from executorch.exir.passes.propagate_device_pass import PropagateDeviceConfig
501524
from torch.export import Dim, export
502525

503526
max_prefill = 512
@@ -533,9 +556,10 @@ def _export_dflash_cuda(
533556
print("=" * 60)
534557
print("Exporting target_forward_from_embeddings...")
535558
print("=" * 60)
536-
with common.BoundMethodForward(
537-
combined, combined.target_forward_from_embeddings
538-
), torch.no_grad():
559+
with (
560+
common.BoundMethodForward(combined, combined.target_forward_from_embeddings),
561+
torch.no_grad(),
562+
):
539563
target_from_embeddings_ep = export(
540564
combined,
541565
(
@@ -577,13 +601,13 @@ def _export_dflash_cuda(
577601
target_prefill_dim = Dim("target_prefill_seq_len", min=5, max=max_target_prefill)
578602
print("=" * 60)
579603
print(
580-
"Exporting target_prefill_from_embeddings "
581-
f"(T in [5, {max_target_prefill}])..."
604+
f"Exporting target_prefill_from_embeddings (T in [5, {max_target_prefill}])..."
582605
)
583606
print("=" * 60)
584-
with common.BoundMethodForward(
585-
combined, combined.target_prefill_from_embeddings
586-
), torch.no_grad():
607+
with (
608+
common.BoundMethodForward(combined, combined.target_prefill_from_embeddings),
609+
torch.no_grad(),
610+
):
587611
target_prefill_from_embeddings_ep = export(
588612
combined,
589613
(
@@ -745,14 +769,7 @@ def cuda_partitioner(method: str) -> CudaPartitioner:
745769
share_mutable_buffers=_share_graph_mutable_buffers("cuda"),
746770
),
747771
emit_mutable_buffer_names=True,
748-
propagate_device_config={
749-
"target_prefill_from_embeddings": PropagateDeviceConfig(
750-
skip_d2h_for_method_outputs=True
751-
),
752-
"draft_prefill": PropagateDeviceConfig(
753-
skip_d2h_for_method_outputs=True
754-
),
755-
},
772+
propagate_device_config=_cuda_propagate_device_config(),
756773
),
757774
)
758775

0 commit comments

Comments
 (0)