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

Function _export_vision_encoder

examples/models/gemma4/export_gemma4.py:406–443  ·  view source on GitHub ↗

Export vision encoder using HF's Gemma4VisionModel. Returns ExportedProgram.

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

Source from the content-addressed store, hash-verified

404
405
406def _export_vision_encoder(
407 checkpoint_path: str, quantize: str, group_size: int = 128, max_patches: int = 1260
408):
409 """Export vision encoder using HF's Gemma4VisionModel. Returns ExportedProgram."""
410 model = _load_hf_vision_encoder(checkpoint_path)
411
412 # Quantize position embedding table (60 MB fp32 -> 15 MB int8)
413 _quantize_position_embedding_table(model)
414
415 if quantize != "none":
416 from executorch.examples.models.gemma4.quant_utils import (
417 apply_linear_quantization,
418 )
419
420 model = apply_linear_quantization(model, quantize, group_size=group_size)
421 model.eval()
422
423 # Dynamic shapes: num_patches must be divisible by pooling_kernel_size^2 (=9)
424 _num_groups = torch.export.Dim("_num_groups", min=1, max=max_patches // 9)
425 num_patches_dim = 9 * _num_groups
426
427 example_pixels = torch.randn(1, max_patches, 3 * 16 * 16)
428 example_positions = torch.zeros(1, max_patches, 2, dtype=torch.long)
429
430 with torch.no_grad():
431 ep = torch.export.export(
432 model,
433 (example_pixels, example_positions),
434 dynamic_shapes={
435 "pixel_values": {1: num_patches_dim},
436 "pixel_position_ids": {1: num_patches_dim},
437 },
438 )
439
440 logger.info("Vision encoder exported")
441 del model
442 gc.collect()
443 return ep
444
445
446def _export_text_decoder(

Callers 1

_export_componentsFunction · 0.85

Calls 6

_load_hf_vision_encoderFunction · 0.85
zerosMethod · 0.80
infoMethod · 0.80
exportMethod · 0.45

Tested by

no test coverage detected