MCPcopy Create free account
hub / github.com/pytorch/executorch / _export_audio_encoder

Function _export_audio_encoder

examples/models/gemma4/export_gemma4.py:244–280  ·  view source on GitHub ↗

Export audio encoder using HF's Gemma4AudioModel. Returns ExportedProgram.

(checkpoint_path: str, quantize: str, group_size: int = 128)

Source from the content-addressed store, hash-verified

242
243
244def _export_audio_encoder(checkpoint_path: str, quantize: str, group_size: int = 128):
245 """Export audio encoder using HF's Gemma4AudioModel. Returns ExportedProgram."""
246 # Patch HF modules for export compatibility
247 _patch_hf_audio_for_export()
248
249 # Load audio encoder from HF checkpoint
250 model = _load_hf_audio_encoder(checkpoint_path)
251
252 if quantize != "none":
253 from executorch.examples.models.gemma4.quant_utils import (
254 apply_linear_quantization,
255 )
256
257 model = apply_linear_quantization(model, quantize, group_size=group_size)
258 model.eval()
259
260 # Dynamic shapes: num_frames = 48*k - 25
261 _num_frames = torch.export.Dim("_num_frames", min=2, max=63)
262 num_frames_dim = 48 * _num_frames - 25
263 valid_frames = 48 * 63 - 25 # 2999
264 example_features = torch.randn(1, valid_frames, 128)
265 example_mask = torch.ones(1, valid_frames, dtype=torch.bool)
266
267 with torch.no_grad():
268 ep = torch.export.export(
269 model,
270 (example_features, example_mask),
271 dynamic_shapes={
272 "features": {1: num_frames_dim},
273 "mask": {1: num_frames_dim},
274 },
275 )
276
277 logger.info("Audio encoder exported")
278 del model
279 gc.collect()
280 return ep
281
282
283class _HFVisionEncoderWithProjection(torch.nn.Module):

Callers 1

_export_componentsFunction · 0.85

Calls 6

_load_hf_audio_encoderFunction · 0.85
onesMethod · 0.80
infoMethod · 0.80
exportMethod · 0.45

Tested by

no test coverage detected