hpi.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from os import PathLike
  15. from pathlib import Path
  16. from typing import Dict, List, Literal, Optional, Tuple, TypedDict, Union
  17. from pydantic import BaseModel
  18. from typing_extensions import TypeAlias
  19. from ...utils.flags import FLAGS_json_format_model
  20. class PaddleInferenceInfo(BaseModel):
  21. trt_dynamic_shapes: Optional[Dict[str, List[List[int]]]] = None
  22. trt_dynamic_shape_input_data: Optional[Dict[str, List[List[float]]]] = None
  23. class TensorRTInfo(BaseModel):
  24. dynamic_shapes: Optional[Dict[str, List[List[int]]]] = None
  25. class InferenceBackendInfoCollection(BaseModel):
  26. paddle_infer: Optional[PaddleInferenceInfo] = None
  27. tensorrt: Optional[TensorRTInfo] = None
  28. # Does using `TypedDict` make things more convenient?
  29. class HPIInfo(BaseModel):
  30. backend_configs: Optional[InferenceBackendInfoCollection] = None
  31. # For multi-backend inference only
  32. InferenceBackend: TypeAlias = Literal[
  33. "paddle", "openvino", "onnxruntime", "tensorrt", "om"
  34. ]
  35. ModelFormat: TypeAlias = Literal["paddle", "onnx", "om"]
  36. class ModelPaths(TypedDict, total=False):
  37. paddle: Tuple[Path, Path]
  38. onnx: Path
  39. om: Path
  40. def get_model_paths(
  41. model_dir: Union[str, PathLike], model_file_prefix: str
  42. ) -> ModelPaths:
  43. model_dir = Path(model_dir)
  44. model_paths: ModelPaths = {}
  45. pd_model_path = None
  46. if FLAGS_json_format_model:
  47. if (model_dir / f"{model_file_prefix}.json").exists():
  48. pd_model_path = model_dir / f"{model_file_prefix}.json"
  49. else:
  50. if (model_dir / f"{model_file_prefix}.json").exists():
  51. pd_model_path = model_dir / f"{model_file_prefix}.json"
  52. elif (model_dir / f"{model_file_prefix}.pdmodel").exists():
  53. pd_model_path = model_dir / f"{model_file_prefix}.pdmodel"
  54. if pd_model_path and (model_dir / f"{model_file_prefix}.pdiparams").exists():
  55. model_paths["paddle"] = (
  56. pd_model_path,
  57. model_dir / f"{model_file_prefix}.pdiparams",
  58. )
  59. if (model_dir / f"{model_file_prefix}.onnx").exists():
  60. model_paths["onnx"] = model_dir / f"{model_file_prefix}.onnx"
  61. if (model_dir / f"{model_file_prefix}.om").exists():
  62. model_paths["om"] = model_dir / f"{model_file_prefix}.om"
  63. return model_paths