(local_dir,
hf_model_path,
compiled_dir,
max_len=512,
model_dir="text_encoder_2",
torch_dtype=torch.float32,
bs=1,
exhaustive_tune=False,
fp16=False,
bf16=True)
| 271 | |
| 272 | |
| 273 | def get_t5_model(local_dir, |
| 274 | hf_model_path, |
| 275 | compiled_dir, |
| 276 | max_len=512, |
| 277 | model_dir="text_encoder_2", |
| 278 | torch_dtype=torch.float32, |
| 279 | bs=1, |
| 280 | exhaustive_tune=False, |
| 281 | fp16=False, |
| 282 | bf16=True): |
| 283 | t5_local_dir = get_local_path(local_dir, model_dir) |
| 284 | onnx_file = "model.onnx" |
| 285 | onnx_path = os.path.join(t5_local_dir, onnx_file) |
| 286 | |
| 287 | def get_compiled_file_name(): |
| 288 | name = f"model_b{bs}" |
| 289 | name += f"_l{max_len}" |
| 290 | if fp16: |
| 291 | name += "_fp16" |
| 292 | elif bf16: |
| 293 | name += "_bf16" |
| 294 | if exhaustive_tune: name += "_exh" |
| 295 | return name + ".mxr" |
| 296 | |
| 297 | t5_compiled_dir = get_local_path(compiled_dir, model_dir) |
| 298 | mxr_file = get_compiled_file_name() |
| 299 | mxr_path = os.path.join(t5_compiled_dir, mxr_file) |
| 300 | |
| 301 | if os.path.isfile(mxr_path): |
| 302 | print(f"found compiled model.. loading T5 encoder from {mxr_path}") |
| 303 | model = MGXModel(mxr_path) |
| 304 | return model |
| 305 | |
| 306 | sample_inputs = (torch.zeros(bs, max_len, dtype=torch.int32), ) |
| 307 | input_names = ["input_ids"] |
| 308 | if not os.path.isfile(onnx_path): |
| 309 | model = T5EncoderModel.from_pretrained(hf_model_path, |
| 310 | subfolder=model_dir, |
| 311 | torch_dtype=torch_dtype) |
| 312 | output_names = ["text_embeddings"] |
| 313 | dynamic_axes = {"input_ids": {0: 'B'}, "text_embeddings": {0: 'B'}} |
| 314 | |
| 315 | with torch.inference_mode(): |
| 316 | torch.onnx.export(model, |
| 317 | sample_inputs, |
| 318 | onnx_path, |
| 319 | export_params=True, |
| 320 | input_names=input_names, |
| 321 | output_names=output_names, |
| 322 | dynamic_axes=dynamic_axes) |
| 323 | |
| 324 | assert os.path.isfile(onnx_path) |
| 325 | print(f"Generating MXR from ONNX file: {onnx_path}") |
| 326 | input_shapes = { |
| 327 | n: list(t.size()) |
| 328 | for n, t in zip(input_names, sample_inputs) |
| 329 | } |
| 330 | model = MGXModel(onnx_path, |
no test coverage detected