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

Function calibrate_model

examples/mediatek/model_export_scripts/gemma.py:352–376  ·  view source on GitHub ↗
(model, cal_dataset, chunk_idx: str)

Source from the content-addressed store, hash-verified

350
351
352def calibrate_model(model, cal_dataset, chunk_idx: str):
353 with torch.no_grad():
354 for inp in tqdm(cal_dataset, desc="Calibrating Model: "):
355 # pass prompt and response
356 for batch in tqdm(inp[chunk_idx].keys(), desc="Batch: "):
357 if inp[chunk_idx][batch] is not None:
358 inputs_embeds = torch.tensor(inp[chunk_idx][batch]["hidden_state"])
359 pos_emb = torch.tensor(inp[chunk_idx][batch]["pos_emb"])
360 cache = torch.tensor(inp[chunk_idx][batch]["cache"])
361 mask = inp[chunk_idx][batch]["mask"]
362 if isinstance(mask, dict):
363 global_mask = torch.tensor(mask["GLOBAL"])
364 local_mask = torch.tensor(mask["SLIDING_LOCAL"])
365 model(
366 inputs_embeds,
367 global_mask,
368 local_mask,
369 pos_emb,
370 *torch.split(cache, 1, dim=0),
371 )
372 else:
373 mask = torch.tensor(mask)
374 model(
375 inputs_embeds, mask, pos_emb, *torch.split(cache, 1, dim=0)
376 )
377
378
379def export_to_et_ir(

Callers 1

export_to_et_irFunction · 0.70

Calls 2

keysMethod · 0.80
splitMethod · 0.80

Tested by

no test coverage detected