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

Function _export_speech_transform

examples/models/gemma4/export_gemma4.py:39–94  ·  view source on GitHub ↗

Export speech transform model. Returns ExportedProgram.

(checkpoint_path: str)

Source from the content-addressed store, hash-verified

37
38
39def _export_speech_transform(checkpoint_path: str):
40 """Export speech transform model. Returns ExportedProgram."""
41 from executorch.examples.models.gemma4.speech_transform import (
42 Gemma4SpeechTransformModel,
43 )
44
45 # Load audio config from HF checkpoint if available
46 kwargs = {}
47 try:
48 import json
49 import os
50
51 # Audio feature extractor config is in processor_config.json (not config.json)
52 processor_path = os.path.join(checkpoint_path, "processor_config.json")
53 if os.path.exists(processor_path):
54 with open(processor_path, "r") as f:
55 processor_config = json.load(f)
56 audio_fe = processor_config.get("feature_extractor", {})
57 if audio_fe:
58 field_map = {
59 "sampling_rate": "sample_rate",
60 "feature_size": "n_mels",
61 "min_frequency": "f_min",
62 "max_frequency": "f_max",
63 "mel_floor": "mel_floor",
64 "input_scale_factor": "input_scale_factor",
65 "frame_length": "frame_length",
66 "hop_length": "hop_length",
67 }
68 for hf_key, our_key in field_map.items():
69 if hf_key in audio_fe:
70 kwargs[our_key] = audio_fe[hf_key]
71 logger.info(f"Speech transform config from checkpoint: {kwargs}")
72 except Exception as e:
73 logger.warning(
74 f"Could not load audio config from checkpoint: {e}, using defaults"
75 )
76
77 model = Gemma4SpeechTransformModel(**kwargs)
78 model.eval()
79
80 sample_rate = kwargs.get("sample_rate", 16000)
81 num_samples = sample_rate * 30 # 30s
82 example = torch.randn(num_samples)
83 min_samples = model.frame_length + model.hop_length + 1
84 waveform_dim = torch.export.Dim("waveform_length", min=min_samples, max=960000)
85
86 with torch.no_grad():
87 ep = torch.export.export(
88 model, (example,), dynamic_shapes={"waveform": {0: waveform_dim}}
89 )
90
91 logger.info("Speech transform exported")
92 del model
93 gc.collect()
94 return ep
95
96

Callers 1

_export_componentsFunction · 0.85

Calls 6

itemsMethod · 0.80
infoMethod · 0.80
loadMethod · 0.45
getMethod · 0.45
exportMethod · 0.45

Tested by

no test coverage detected