Skip to content

Instantly share code, notes, and snippets.

@AmosLewis
Last active January 25, 2023 19:33
Show Gist options
  • Save AmosLewis/465795bfa1a4004fb96c742666515027 to your computer and use it in GitHub Desktop.
Save AmosLewis/465795bfa1a4004fb96c742666515027 to your computer and use it in GitHub Desktop.
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
from torch.fx.experimental.proxy_tensor import make_fx
from torch._decomp import get_decompositions
import tempfile
import torch_mlir
def prepare_sentence_tokens(hf_model: str, sentence: str):
tokenizer = AutoTokenizer.from_pretrained(hf_model)
return torch.tensor([tokenizer.encode(sentence)])
class HfMaskedLM(torch.nn.Module):
def __init__(self, model_name: str):
super().__init__()
self.model = AutoModelForSequenceClassification.from_pretrained(
model_name, # The pretrained model name.
# The number of output labels--2 for binary classification.
num_labels=2,
# Whether the model returns attentions weights.
output_attentions=False,
# Whether the model returns all hidden-states.
output_hidden_states=False,
torchscript=True,
)
self.model.eval()
def forward(self, tokens):
return self.model.forward(tokens)[0]
hf_minilm_model = "distilgpt2"
test_input = torch.randint(2, (1, 128))
model = HfMaskedLM(hf_minilm_model)
print("model(test_input): ")
print(model(test_input))
fx_g = make_fx(
model,
decomposition_table=get_decompositions(
[
torch.ops.aten.split.Tensor, # use pytorch to decompose split op, otherwise lead to "torch.tensor_static_info_cast" bug when TorchScript IR -> Torch Backend IR
torch.ops.aten.split_with_sizes,
]
),
)(test_input)
# print("fx_g.graph: ")
# print(fx_g.graph)
fx_g.graph.set_codegen(torch.fx.graph.CodeGen())
fx_g.recompile()
def strip_overloads(gm):
"""
Modifies the target of graph nodes in :attr:`gm` to strip overloads.
Args:
gm(fx.GraphModule): The input Fx graph module to be modified
"""
for node in gm.graph.nodes:
if isinstance(node.target, torch._ops.OpOverload):
node.target = node.target.overloadpacket
gm.recompile()
strip_overloads(fx_g)
ts_g = torch.jit.script(fx_g)
# print("ts_g.graph: ")
# print(ts_g.graph)
module = torch_mlir.compile(
ts_g,
(test_input),
torch_mlir.OutputType.TOSA,
use_tracing=True,
verbose=False,
)
# module.dump()
import os
mlir_str = module.operation.get_asm()
dir=tempfile.gettempdir()
with open(os.path.join(dir, "distilgpt_torch_tosa_0123_transformers4.21.2.mlir"), "w") as mlir_file:
mlir_file.write(mlir_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment