model_operation.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from fate_flow.model import mysql_model_storage, tencent_cos_model_storage
  17. from fate_flow.utils.log_utils import getLogger
  18. from fate_flow.components._base import (
  19. BaseParam,
  20. ComponentBase,
  21. ComponentMeta,
  22. ComponentInputProtocol,
  23. )
  24. from fate_flow.entity.types import ModelStorage
  25. LOGGER = getLogger()
  26. ModelStorageClassMap = {
  27. ModelStorage.MYSQL.value: mysql_model_storage.MysqlModelStorage,
  28. ModelStorage.TENCENT_COS.value: tencent_cos_model_storage.TencentCOSModelStorage,
  29. }
  30. def get_model_storage(parameters):
  31. model_storage = parameters.get("store_address", {}).get("storage")
  32. if not model_storage:
  33. raise TypeError(f"'store_address' is empty.")
  34. if model_storage not in ModelStorageClassMap:
  35. raise ValueError(f"Model storage '{model_storage}' is not supported.")
  36. return ModelStorageClassMap[model_storage]()
  37. model_store_cpn_meta = ComponentMeta("ModelStore")
  38. @model_store_cpn_meta.bind_param
  39. class ModelStoreParam(BaseParam):
  40. def __init__(
  41. self,
  42. model_id: str = None,
  43. model_version: str = None,
  44. store_address: dict = None,
  45. force_update: bool = False,
  46. ):
  47. self.model_id = model_id
  48. self.model_version = model_version
  49. self.store_address = store_address
  50. self.force_update = force_update
  51. def check(self):
  52. return True
  53. @model_store_cpn_meta.bind_runner.on_local
  54. class ModelStore(ComponentBase):
  55. def _run(self, cpn_input: ComponentInputProtocol):
  56. parameters = cpn_input.parameters
  57. model_storage = get_model_storage(parameters)
  58. model_storage.store(
  59. parameters["model_id"], parameters["model_version"],
  60. parameters["store_address"], parameters.get("force_update", False),
  61. )
  62. model_restore_cpn_meta = ComponentMeta("ModelRestore")
  63. @model_restore_cpn_meta.bind_param
  64. class ModelRestoreParam(BaseParam):
  65. def __init__(
  66. self,
  67. model_id: str = None,
  68. model_version: str = None,
  69. store_address: dict = None,
  70. force_update: bool = False,
  71. hash_: str = None,
  72. ):
  73. self.model_id = model_id
  74. self.model_version = model_version
  75. self.store_address = store_address
  76. self.force_update = force_update
  77. self.hash_ = hash_
  78. def check(self):
  79. return True
  80. @model_restore_cpn_meta.bind_runner.on_local
  81. class ModelRestore(ComponentBase):
  82. def _run(self, cpn_input: ComponentInputProtocol):
  83. parameters = cpn_input.parameters
  84. model_storage = get_model_storage(parameters)
  85. model_storage.restore(
  86. parameters["model_id"], parameters["model_version"],
  87. parameters["store_address"], parameters.get("force_update", False),
  88. parameters.get("hash_"),
  89. )