2022-08-24 07:44:36 +08:00
|
|
|
import torch
|
2024-02-07 11:07:59 +08:00
|
|
|
from torch_mlir import torchscript
|
2022-08-24 07:44:36 +08:00
|
|
|
|
|
|
|
from transformers import BertForMaskedLM
|
|
|
|
|
2024-04-29 11:06:01 +08:00
|
|
|
|
2022-08-24 07:44:36 +08:00
|
|
|
# Wrap the bert model to avoid multiple returns problem
|
|
|
|
class BertTinyWrapper(torch.nn.Module):
|
|
|
|
def __init__(self) -> None:
|
|
|
|
super().__init__()
|
2024-04-28 05:16:31 +08:00
|
|
|
self.bert = BertForMaskedLM.from_pretrained(
|
|
|
|
"prajjwal1/bert-tiny", return_dict=False
|
|
|
|
)
|
|
|
|
|
2022-08-24 07:44:36 +08:00
|
|
|
def forward(self, data):
|
|
|
|
return self.bert(data)[0]
|
|
|
|
|
2024-04-28 05:16:31 +08:00
|
|
|
|
2022-08-24 07:44:36 +08:00
|
|
|
model = BertTinyWrapper()
|
|
|
|
model.eval()
|
|
|
|
data = torch.randint(30522, (2, 128))
|
2023-02-02 21:29:47 +08:00
|
|
|
out_stablehlo_mlir_path = "./bert_tiny_stablehlo.mlir"
|
2022-08-24 07:44:36 +08:00
|
|
|
|
2024-04-28 05:16:31 +08:00
|
|
|
module = torchscript.compile(
|
|
|
|
model, data, output_type=torchscript.OutputType.STABLEHLO, use_tracing=True
|
|
|
|
)
|
2023-02-02 21:29:47 +08:00
|
|
|
with open(out_stablehlo_mlir_path, "w", encoding="utf-8") as outf:
|
2024-05-11 00:28:58 +08:00
|
|
|
outf.write(module.operation.get_asm())
|
2022-08-24 07:44:36 +08:00
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
print(f"StableHLO IR of tiny bert successfully written into {out_stablehlo_mlir_path}")
|