easy-cs-rec-custommodel 0.8.6__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of easy-cs-rec-custommodel might be problematic. Click here for more details.

Files changed (336) hide show
  1. easy_cs_rec_custommodel-0.8.6.dist-info/LICENSE +203 -0
  2. easy_cs_rec_custommodel-0.8.6.dist-info/METADATA +48 -0
  3. easy_cs_rec_custommodel-0.8.6.dist-info/RECORD +336 -0
  4. easy_cs_rec_custommodel-0.8.6.dist-info/WHEEL +6 -0
  5. easy_cs_rec_custommodel-0.8.6.dist-info/top_level.txt +2 -0
  6. easy_rec/__init__.py +114 -0
  7. easy_rec/python/__init__.py +0 -0
  8. easy_rec/python/builders/__init__.py +0 -0
  9. easy_rec/python/builders/hyperparams_builder.py +78 -0
  10. easy_rec/python/builders/loss_builder.py +333 -0
  11. easy_rec/python/builders/optimizer_builder.py +211 -0
  12. easy_rec/python/builders/strategy_builder.py +44 -0
  13. easy_rec/python/compat/__init__.py +0 -0
  14. easy_rec/python/compat/adam_s.py +245 -0
  15. easy_rec/python/compat/array_ops.py +229 -0
  16. easy_rec/python/compat/dynamic_variable.py +542 -0
  17. easy_rec/python/compat/early_stopping.py +653 -0
  18. easy_rec/python/compat/embedding_ops.py +162 -0
  19. easy_rec/python/compat/embedding_parallel_saver.py +316 -0
  20. easy_rec/python/compat/estimator_train.py +116 -0
  21. easy_rec/python/compat/exporter.py +473 -0
  22. easy_rec/python/compat/feature_column/__init__.py +0 -0
  23. easy_rec/python/compat/feature_column/feature_column.py +3675 -0
  24. easy_rec/python/compat/feature_column/feature_column_v2.py +5233 -0
  25. easy_rec/python/compat/feature_column/sequence_feature_column.py +648 -0
  26. easy_rec/python/compat/feature_column/utils.py +154 -0
  27. easy_rec/python/compat/layers.py +329 -0
  28. easy_rec/python/compat/ops.py +14 -0
  29. easy_rec/python/compat/optimizers.py +619 -0
  30. easy_rec/python/compat/queues.py +311 -0
  31. easy_rec/python/compat/regularizers.py +208 -0
  32. easy_rec/python/compat/sok_optimizer.py +440 -0
  33. easy_rec/python/compat/sync_replicas_optimizer.py +528 -0
  34. easy_rec/python/compat/weight_decay_optimizers.py +475 -0
  35. easy_rec/python/core/__init__.py +0 -0
  36. easy_rec/python/core/easyrec_metrics/__init__.py +24 -0
  37. easy_rec/python/core/easyrec_metrics/distribute_metrics_impl_pai.py +3702 -0
  38. easy_rec/python/core/easyrec_metrics/distribute_metrics_impl_tf.py +3768 -0
  39. easy_rec/python/core/learning_schedules.py +228 -0
  40. easy_rec/python/core/metrics.py +402 -0
  41. easy_rec/python/core/sampler.py +844 -0
  42. easy_rec/python/eval.py +102 -0
  43. easy_rec/python/export.py +150 -0
  44. easy_rec/python/feature_column/__init__.py +0 -0
  45. easy_rec/python/feature_column/feature_column.py +664 -0
  46. easy_rec/python/feature_column/feature_group.py +89 -0
  47. easy_rec/python/hpo/__init__.py +0 -0
  48. easy_rec/python/hpo/emr_hpo.py +140 -0
  49. easy_rec/python/hpo/generate_hpo_sql.py +71 -0
  50. easy_rec/python/hpo/pai_hpo.py +297 -0
  51. easy_rec/python/inference/__init__.py +0 -0
  52. easy_rec/python/inference/csv_predictor.py +189 -0
  53. easy_rec/python/inference/hive_parquet_predictor.py +200 -0
  54. easy_rec/python/inference/hive_predictor.py +166 -0
  55. easy_rec/python/inference/odps_predictor.py +70 -0
  56. easy_rec/python/inference/parquet_predictor.py +147 -0
  57. easy_rec/python/inference/parquet_predictor_v2.py +147 -0
  58. easy_rec/python/inference/predictor.py +621 -0
  59. easy_rec/python/inference/processor/__init__.py +0 -0
  60. easy_rec/python/inference/processor/test.py +170 -0
  61. easy_rec/python/inference/vector_retrieve.py +124 -0
  62. easy_rec/python/input/__init__.py +0 -0
  63. easy_rec/python/input/batch_tfrecord_input.py +117 -0
  64. easy_rec/python/input/criteo_binary_reader.py +259 -0
  65. easy_rec/python/input/criteo_input.py +107 -0
  66. easy_rec/python/input/csv_input.py +175 -0
  67. easy_rec/python/input/csv_input_ex.py +72 -0
  68. easy_rec/python/input/csv_input_v2.py +68 -0
  69. easy_rec/python/input/datahub_input.py +320 -0
  70. easy_rec/python/input/dummy_input.py +58 -0
  71. easy_rec/python/input/hive_input.py +123 -0
  72. easy_rec/python/input/hive_parquet_input.py +140 -0
  73. easy_rec/python/input/hive_rtp_input.py +174 -0
  74. easy_rec/python/input/input.py +1064 -0
  75. easy_rec/python/input/kafka_dataset.py +144 -0
  76. easy_rec/python/input/kafka_input.py +235 -0
  77. easy_rec/python/input/load_parquet.py +317 -0
  78. easy_rec/python/input/odps_input.py +101 -0
  79. easy_rec/python/input/odps_input_v2.py +110 -0
  80. easy_rec/python/input/odps_input_v3.py +132 -0
  81. easy_rec/python/input/odps_rtp_input.py +187 -0
  82. easy_rec/python/input/odps_rtp_input_v2.py +104 -0
  83. easy_rec/python/input/parquet_input.py +397 -0
  84. easy_rec/python/input/parquet_input_v2.py +180 -0
  85. easy_rec/python/input/parquet_input_v3.py +203 -0
  86. easy_rec/python/input/rtp_input.py +225 -0
  87. easy_rec/python/input/rtp_input_v2.py +145 -0
  88. easy_rec/python/input/tfrecord_input.py +100 -0
  89. easy_rec/python/layers/__init__.py +0 -0
  90. easy_rec/python/layers/backbone.py +571 -0
  91. easy_rec/python/layers/capsule_layer.py +176 -0
  92. easy_rec/python/layers/cmbf.py +390 -0
  93. easy_rec/python/layers/common_layers.py +192 -0
  94. easy_rec/python/layers/dnn.py +87 -0
  95. easy_rec/python/layers/embed_input_layer.py +25 -0
  96. easy_rec/python/layers/fm.py +26 -0
  97. easy_rec/python/layers/input_layer.py +396 -0
  98. easy_rec/python/layers/keras/__init__.py +34 -0
  99. easy_rec/python/layers/keras/activation.py +114 -0
  100. easy_rec/python/layers/keras/attention.py +267 -0
  101. easy_rec/python/layers/keras/auxiliary_loss.py +47 -0
  102. easy_rec/python/layers/keras/blocks.py +262 -0
  103. easy_rec/python/layers/keras/bst.py +119 -0
  104. easy_rec/python/layers/keras/custom_ops.py +250 -0
  105. easy_rec/python/layers/keras/data_augment.py +133 -0
  106. easy_rec/python/layers/keras/din.py +67 -0
  107. easy_rec/python/layers/keras/einsum_dense.py +598 -0
  108. easy_rec/python/layers/keras/embedding.py +81 -0
  109. easy_rec/python/layers/keras/fibinet.py +251 -0
  110. easy_rec/python/layers/keras/interaction.py +416 -0
  111. easy_rec/python/layers/keras/layer_norm.py +364 -0
  112. easy_rec/python/layers/keras/mask_net.py +166 -0
  113. easy_rec/python/layers/keras/multi_head_attention.py +717 -0
  114. easy_rec/python/layers/keras/multi_task.py +125 -0
  115. easy_rec/python/layers/keras/numerical_embedding.py +376 -0
  116. easy_rec/python/layers/keras/ppnet.py +194 -0
  117. easy_rec/python/layers/keras/transformer.py +192 -0
  118. easy_rec/python/layers/layer_norm.py +51 -0
  119. easy_rec/python/layers/mmoe.py +83 -0
  120. easy_rec/python/layers/multihead_attention.py +162 -0
  121. easy_rec/python/layers/multihead_cross_attention.py +749 -0
  122. easy_rec/python/layers/senet.py +73 -0
  123. easy_rec/python/layers/seq_input_layer.py +134 -0
  124. easy_rec/python/layers/sequence_feature_layer.py +249 -0
  125. easy_rec/python/layers/uniter.py +301 -0
  126. easy_rec/python/layers/utils.py +248 -0
  127. easy_rec/python/layers/variational_dropout_layer.py +130 -0
  128. easy_rec/python/loss/__init__.py +0 -0
  129. easy_rec/python/loss/circle_loss.py +82 -0
  130. easy_rec/python/loss/contrastive_loss.py +79 -0
  131. easy_rec/python/loss/f1_reweight_loss.py +38 -0
  132. easy_rec/python/loss/focal_loss.py +93 -0
  133. easy_rec/python/loss/jrc_loss.py +128 -0
  134. easy_rec/python/loss/listwise_loss.py +161 -0
  135. easy_rec/python/loss/multi_similarity.py +68 -0
  136. easy_rec/python/loss/pairwise_loss.py +307 -0
  137. easy_rec/python/loss/softmax_loss_with_negative_mining.py +110 -0
  138. easy_rec/python/loss/zero_inflated_lognormal.py +76 -0
  139. easy_rec/python/main.py +878 -0
  140. easy_rec/python/model/__init__.py +0 -0
  141. easy_rec/python/model/autoint.py +73 -0
  142. easy_rec/python/model/cmbf.py +47 -0
  143. easy_rec/python/model/collaborative_metric_learning.py +182 -0
  144. easy_rec/python/model/custom_model.py +323 -0
  145. easy_rec/python/model/dat.py +138 -0
  146. easy_rec/python/model/dbmtl.py +116 -0
  147. easy_rec/python/model/dcn.py +70 -0
  148. easy_rec/python/model/deepfm.py +106 -0
  149. easy_rec/python/model/dlrm.py +73 -0
  150. easy_rec/python/model/dropoutnet.py +207 -0
  151. easy_rec/python/model/dssm.py +154 -0
  152. easy_rec/python/model/dssm_senet.py +143 -0
  153. easy_rec/python/model/dummy_model.py +48 -0
  154. easy_rec/python/model/easy_rec_estimator.py +739 -0
  155. easy_rec/python/model/easy_rec_model.py +467 -0
  156. easy_rec/python/model/esmm.py +242 -0
  157. easy_rec/python/model/fm.py +63 -0
  158. easy_rec/python/model/match_model.py +357 -0
  159. easy_rec/python/model/mind.py +445 -0
  160. easy_rec/python/model/mmoe.py +70 -0
  161. easy_rec/python/model/multi_task_model.py +303 -0
  162. easy_rec/python/model/multi_tower.py +62 -0
  163. easy_rec/python/model/multi_tower_bst.py +190 -0
  164. easy_rec/python/model/multi_tower_din.py +130 -0
  165. easy_rec/python/model/multi_tower_recall.py +68 -0
  166. easy_rec/python/model/pdn.py +203 -0
  167. easy_rec/python/model/ple.py +120 -0
  168. easy_rec/python/model/rank_model.py +485 -0
  169. easy_rec/python/model/rocket_launching.py +203 -0
  170. easy_rec/python/model/simple_multi_task.py +54 -0
  171. easy_rec/python/model/uniter.py +46 -0
  172. easy_rec/python/model/wide_and_deep.py +121 -0
  173. easy_rec/python/ops/1.12/incr_record.so +0 -0
  174. easy_rec/python/ops/1.12/kafka.so +0 -0
  175. easy_rec/python/ops/1.12/libcustom_ops.so +0 -0
  176. easy_rec/python/ops/1.12/libembed_op.so +0 -0
  177. easy_rec/python/ops/1.12/libhiredis.so.1.0.0 +0 -0
  178. easy_rec/python/ops/1.12/librdkafka++.so.1 +0 -0
  179. easy_rec/python/ops/1.12/librdkafka.so.1 +0 -0
  180. easy_rec/python/ops/1.12/libredis++.so +0 -0
  181. easy_rec/python/ops/1.12/libredis++.so.1 +0 -0
  182. easy_rec/python/ops/1.12/libredis++.so.1.2.3 +0 -0
  183. easy_rec/python/ops/1.12/libstr_avx_op.so +0 -0
  184. easy_rec/python/ops/1.12/libwrite_sparse_kv.so +0 -0
  185. easy_rec/python/ops/1.15/incr_record.so +0 -0
  186. easy_rec/python/ops/1.15/kafka.so +0 -0
  187. easy_rec/python/ops/1.15/libcustom_ops.so +0 -0
  188. easy_rec/python/ops/1.15/libembed_op.so +0 -0
  189. easy_rec/python/ops/1.15/libhiredis.so.1.0.0 +0 -0
  190. easy_rec/python/ops/1.15/librdkafka++.so +0 -0
  191. easy_rec/python/ops/1.15/librdkafka++.so.1 +0 -0
  192. easy_rec/python/ops/1.15/librdkafka.so +0 -0
  193. easy_rec/python/ops/1.15/librdkafka.so.1 +0 -0
  194. easy_rec/python/ops/1.15/libredis++.so.1 +0 -0
  195. easy_rec/python/ops/1.15/libstr_avx_op.so +0 -0
  196. easy_rec/python/ops/2.12/libcustom_ops.so +0 -0
  197. easy_rec/python/ops/2.12/libload_embed.so +0 -0
  198. easy_rec/python/ops/2.12/libstr_avx_op.so +0 -0
  199. easy_rec/python/ops/__init__.py +0 -0
  200. easy_rec/python/ops/gen_kafka_ops.py +193 -0
  201. easy_rec/python/ops/gen_str_avx_op.py +28 -0
  202. easy_rec/python/ops/incr_record.py +30 -0
  203. easy_rec/python/predict.py +170 -0
  204. easy_rec/python/protos/__init__.py +0 -0
  205. easy_rec/python/protos/autoint_pb2.py +122 -0
  206. easy_rec/python/protos/backbone_pb2.py +1416 -0
  207. easy_rec/python/protos/cmbf_pb2.py +435 -0
  208. easy_rec/python/protos/collaborative_metric_learning_pb2.py +252 -0
  209. easy_rec/python/protos/custom_model_pb2.py +57 -0
  210. easy_rec/python/protos/dat_pb2.py +262 -0
  211. easy_rec/python/protos/data_source_pb2.py +422 -0
  212. easy_rec/python/protos/dataset_pb2.py +1920 -0
  213. easy_rec/python/protos/dbmtl_pb2.py +191 -0
  214. easy_rec/python/protos/dcn_pb2.py +197 -0
  215. easy_rec/python/protos/deepfm_pb2.py +163 -0
  216. easy_rec/python/protos/dlrm_pb2.py +163 -0
  217. easy_rec/python/protos/dnn_pb2.py +329 -0
  218. easy_rec/python/protos/dropoutnet_pb2.py +239 -0
  219. easy_rec/python/protos/dssm_pb2.py +262 -0
  220. easy_rec/python/protos/dssm_senet_pb2.py +282 -0
  221. easy_rec/python/protos/easy_rec_model_pb2.py +1672 -0
  222. easy_rec/python/protos/esmm_pb2.py +133 -0
  223. easy_rec/python/protos/eval_pb2.py +930 -0
  224. easy_rec/python/protos/export_pb2.py +379 -0
  225. easy_rec/python/protos/feature_config_pb2.py +1359 -0
  226. easy_rec/python/protos/fm_pb2.py +90 -0
  227. easy_rec/python/protos/hive_config_pb2.py +138 -0
  228. easy_rec/python/protos/hyperparams_pb2.py +624 -0
  229. easy_rec/python/protos/keras_layer_pb2.py +692 -0
  230. easy_rec/python/protos/layer_pb2.py +1936 -0
  231. easy_rec/python/protos/loss_pb2.py +1713 -0
  232. easy_rec/python/protos/mind_pb2.py +497 -0
  233. easy_rec/python/protos/mmoe_pb2.py +215 -0
  234. easy_rec/python/protos/multi_tower_pb2.py +295 -0
  235. easy_rec/python/protos/multi_tower_recall_pb2.py +198 -0
  236. easy_rec/python/protos/optimizer_pb2.py +2017 -0
  237. easy_rec/python/protos/pdn_pb2.py +293 -0
  238. easy_rec/python/protos/pipeline_pb2.py +516 -0
  239. easy_rec/python/protos/ple_pb2.py +231 -0
  240. easy_rec/python/protos/predict_pb2.py +1140 -0
  241. easy_rec/python/protos/rocket_launching_pb2.py +169 -0
  242. easy_rec/python/protos/seq_encoder_pb2.py +1084 -0
  243. easy_rec/python/protos/simi_pb2.py +54 -0
  244. easy_rec/python/protos/simple_multi_task_pb2.py +97 -0
  245. easy_rec/python/protos/tf_predict_pb2.py +630 -0
  246. easy_rec/python/protos/tower_pb2.py +661 -0
  247. easy_rec/python/protos/train_pb2.py +1197 -0
  248. easy_rec/python/protos/uniter_pb2.py +307 -0
  249. easy_rec/python/protos/variational_dropout_pb2.py +91 -0
  250. easy_rec/python/protos/wide_and_deep_pb2.py +131 -0
  251. easy_rec/python/test/__init__.py +0 -0
  252. easy_rec/python/test/csv_input_test.py +340 -0
  253. easy_rec/python/test/custom_early_stop_func.py +19 -0
  254. easy_rec/python/test/dh_local_run.py +104 -0
  255. easy_rec/python/test/embed_test.py +155 -0
  256. easy_rec/python/test/emr_run.py +119 -0
  257. easy_rec/python/test/eval_metric_test.py +107 -0
  258. easy_rec/python/test/excel_convert_test.py +64 -0
  259. easy_rec/python/test/export_test.py +513 -0
  260. easy_rec/python/test/fg_test.py +70 -0
  261. easy_rec/python/test/hive_input_test.py +311 -0
  262. easy_rec/python/test/hpo_test.py +235 -0
  263. easy_rec/python/test/kafka_test.py +373 -0
  264. easy_rec/python/test/local_incr_test.py +122 -0
  265. easy_rec/python/test/loss_test.py +110 -0
  266. easy_rec/python/test/odps_command.py +61 -0
  267. easy_rec/python/test/odps_local_run.py +86 -0
  268. easy_rec/python/test/odps_run.py +254 -0
  269. easy_rec/python/test/odps_test_cls.py +39 -0
  270. easy_rec/python/test/odps_test_prepare.py +198 -0
  271. easy_rec/python/test/odps_test_util.py +237 -0
  272. easy_rec/python/test/pre_check_test.py +54 -0
  273. easy_rec/python/test/predictor_test.py +394 -0
  274. easy_rec/python/test/rtp_convert_test.py +133 -0
  275. easy_rec/python/test/run.py +138 -0
  276. easy_rec/python/test/train_eval_test.py +1299 -0
  277. easy_rec/python/test/util_test.py +85 -0
  278. easy_rec/python/test/zero_inflated_lognormal_test.py +53 -0
  279. easy_rec/python/tools/__init__.py +0 -0
  280. easy_rec/python/tools/add_boundaries_to_config.py +67 -0
  281. easy_rec/python/tools/add_feature_info_to_config.py +145 -0
  282. easy_rec/python/tools/convert_config_format.py +48 -0
  283. easy_rec/python/tools/convert_rtp_data.py +79 -0
  284. easy_rec/python/tools/convert_rtp_fg.py +106 -0
  285. easy_rec/python/tools/create_config_from_excel.py +427 -0
  286. easy_rec/python/tools/criteo/__init__.py +0 -0
  287. easy_rec/python/tools/criteo/convert_data.py +157 -0
  288. easy_rec/python/tools/edit_lookup_graph.py +134 -0
  289. easy_rec/python/tools/faiss_index_pai.py +116 -0
  290. easy_rec/python/tools/feature_selection.py +316 -0
  291. easy_rec/python/tools/hit_rate_ds.py +223 -0
  292. easy_rec/python/tools/hit_rate_pai.py +138 -0
  293. easy_rec/python/tools/pre_check.py +120 -0
  294. easy_rec/python/tools/predict_and_chk.py +111 -0
  295. easy_rec/python/tools/read_kafka.py +55 -0
  296. easy_rec/python/tools/split_model_pai.py +286 -0
  297. easy_rec/python/tools/split_pdn_model_pai.py +272 -0
  298. easy_rec/python/tools/test_saved_model.py +80 -0
  299. easy_rec/python/tools/view_saved_model.py +39 -0
  300. easy_rec/python/tools/write_kafka.py +65 -0
  301. easy_rec/python/train_eval.py +325 -0
  302. easy_rec/python/utils/__init__.py +15 -0
  303. easy_rec/python/utils/activation.py +120 -0
  304. easy_rec/python/utils/check_utils.py +87 -0
  305. easy_rec/python/utils/compat.py +14 -0
  306. easy_rec/python/utils/config_util.py +652 -0
  307. easy_rec/python/utils/constant.py +43 -0
  308. easy_rec/python/utils/convert_rtp_fg.py +616 -0
  309. easy_rec/python/utils/dag.py +192 -0
  310. easy_rec/python/utils/distribution_utils.py +268 -0
  311. easy_rec/python/utils/ds_util.py +65 -0
  312. easy_rec/python/utils/embedding_utils.py +73 -0
  313. easy_rec/python/utils/estimator_utils.py +1036 -0
  314. easy_rec/python/utils/export_big_model.py +630 -0
  315. easy_rec/python/utils/expr_util.py +118 -0
  316. easy_rec/python/utils/fg_util.py +53 -0
  317. easy_rec/python/utils/hit_rate_utils.py +220 -0
  318. easy_rec/python/utils/hive_utils.py +183 -0
  319. easy_rec/python/utils/hpo_util.py +137 -0
  320. easy_rec/python/utils/hvd_utils.py +56 -0
  321. easy_rec/python/utils/input_utils.py +108 -0
  322. easy_rec/python/utils/io_util.py +282 -0
  323. easy_rec/python/utils/load_class.py +249 -0
  324. easy_rec/python/utils/meta_graph_editor.py +941 -0
  325. easy_rec/python/utils/multi_optimizer.py +62 -0
  326. easy_rec/python/utils/numpy_utils.py +18 -0
  327. easy_rec/python/utils/odps_util.py +79 -0
  328. easy_rec/python/utils/pai_util.py +86 -0
  329. easy_rec/python/utils/proto_util.py +90 -0
  330. easy_rec/python/utils/restore_filter.py +89 -0
  331. easy_rec/python/utils/shape_utils.py +432 -0
  332. easy_rec/python/utils/static_shape.py +71 -0
  333. easy_rec/python/utils/test_utils.py +866 -0
  334. easy_rec/python/utils/tf_utils.py +56 -0
  335. easy_rec/version.py +4 -0
  336. test/__init__.py +0 -0
@@ -0,0 +1,630 @@
1
+ # -*- encoding:utf-8 -*-
2
+ # Copyright (c) Alibaba, Inc. and its affiliates.
3
+
4
+ import json
5
+ import logging
6
+ import os
7
+ import time
8
+
9
+ import numpy as np
10
+ import tensorflow as tf
11
+ from google.protobuf import json_format
12
+ from tensorflow.core.protobuf import config_pb2
13
+ from tensorflow.python.framework import ops
14
+ from tensorflow.python.ops.variables import global_variables
15
+ from tensorflow.python.platform.gfile import DeleteRecursively
16
+ from tensorflow.python.platform.gfile import Exists
17
+ from tensorflow.python.platform.gfile import GFile
18
+ from tensorflow.python.platform.gfile import Remove
19
+ from tensorflow.python.saved_model import signature_constants
20
+ from tensorflow.python.training.device_setter import replica_device_setter
21
+ from tensorflow.python.training.monitored_session import ChiefSessionCreator
22
+ from tensorflow.python.training.monitored_session import Scaffold
23
+ from tensorflow.python.training.saver import export_meta_graph
24
+
25
+ import easy_rec
26
+ from easy_rec.python.utils import constant
27
+ from easy_rec.python.utils import estimator_utils
28
+ from easy_rec.python.utils import io_util
29
+ from easy_rec.python.utils import proto_util
30
+ from easy_rec.python.utils.meta_graph_editor import EMBEDDING_INITIALIZERS
31
+ from easy_rec.python.utils.meta_graph_editor import MetaGraphEditor
32
+
33
+ if tf.__version__ >= '2.0':
34
+ from tensorflow.python.framework.ops import disable_eager_execution
35
+ disable_eager_execution()
36
+
37
+ ConfigProto = config_pb2.ConfigProto
38
+ GPUOptions = config_pb2.GPUOptions
39
+
40
+ INCR_UPDATE_SIGNATURE_KEY = 'incr_update_sig'
41
+
42
+
43
+ def export_big_model(export_dir, pipeline_config, redis_params,
44
+ serving_input_fn, estimator, checkpoint_path, verbose):
45
+ for key in redis_params:
46
+ logging.info('%s: %s' % (key, redis_params[key]))
47
+
48
+ redis_cache_names = []
49
+ for feature_config in pipeline_config.feature_configs:
50
+ if feature_config.is_cache:
51
+ if feature_config.feature_name:
52
+ redis_cache_names.append(feature_config.feature_name)
53
+ else:
54
+ redis_cache_names.append(feature_config.input_names[0])
55
+ logging.info('The list of cache names: %s' % ','.join(redis_cache_names))
56
+
57
+ write_kv_lib_path = os.path.join(easy_rec.ops_dir, 'libembed_op.so')
58
+ kv_module = tf.load_op_library(write_kv_lib_path)
59
+
60
+ try:
61
+ sparse_kv_lib_path = os.path.join(easy_rec.ops_dir, 'libwrite_sparse_kv.so')
62
+ sparse_kv_module = tf.load_op_library(sparse_kv_lib_path)
63
+ except Exception as ex:
64
+ logging.warning('load libwrite_sparse_kv.so failed: %s' % str(ex))
65
+ sparse_kv_module = None
66
+ if not checkpoint_path:
67
+ checkpoint_path = estimator_utils.latest_checkpoint(
68
+ pipeline_config.model_dir)
69
+ logging.info('checkpoint_path = %s' % checkpoint_path)
70
+
71
+ server = None
72
+ cluster = None
73
+ if 'TF_CONFIG' in os.environ:
74
+ # change chief to master
75
+ tf_config = estimator_utils.chief_to_master()
76
+ if tf_config['task']['type'] == 'ps':
77
+ cluster = tf.train.ClusterSpec(tf_config['cluster'])
78
+ server = tf.train.Server(
79
+ cluster, job_name='ps', task_index=tf_config['task']['index'])
80
+ server.join()
81
+ elif tf_config['task']['type'] == 'master':
82
+ if 'ps' in tf_config['cluster']:
83
+ cluster = tf.train.ClusterSpec(tf_config['cluster'])
84
+ server = tf.train.Server(cluster, job_name='master', task_index=0)
85
+ server_target = server.target
86
+ logging.info('server_target = %s' % server_target)
87
+
88
+ serving_input = serving_input_fn()
89
+ features = serving_input.features
90
+ inputs = serving_input.receiver_tensors
91
+
92
+ if cluster:
93
+ logging.info('cluster = ' + str(cluster))
94
+ with tf.device(
95
+ replica_device_setter(
96
+ worker_device='/job:master/task:0', cluster=cluster)):
97
+ outputs = estimator._export_model_fn(features, None, None,
98
+ estimator.params).predictions
99
+
100
+ meta_graph_def = export_meta_graph()
101
+ redis_embedding_version = redis_params.get('redis_embedding_version', '')
102
+ if not redis_embedding_version:
103
+ meta_graph_def.meta_info_def.meta_graph_version =\
104
+ str(int(time.time()))
105
+ else:
106
+ meta_graph_def.meta_info_def.meta_graph_version = redis_embedding_version
107
+
108
+ logging.info('meta_graph_version = %s' %
109
+ meta_graph_def.meta_info_def.meta_graph_version)
110
+
111
+ embed_var_parts = {}
112
+ embed_norm_name = {}
113
+ embed_spos = {}
114
+ # pai embedding variable
115
+ embedding_vars = {}
116
+ norm_name_to_ids = {}
117
+ for x in global_variables():
118
+ if 'EmbeddingVariable' in str(type(x)):
119
+ norm_name, part_id = proto_util.get_norm_embed_name(x.name)
120
+ norm_name_to_ids[norm_name] = 1
121
+ tmp_export = x.export()
122
+ if x.device not in embedding_vars:
123
+ embedding_vars[x.device] = [(norm_name, tmp_export.keys,
124
+ tmp_export.values)]
125
+ else:
126
+ embedding_vars[x.device].append(
127
+ (norm_name, tmp_export.keys, tmp_export.values))
128
+ elif '/embedding_weights:' in x.name or '/embedding_weights/part_' in x.name:
129
+ norm_name, part_id = proto_util.get_norm_embed_name(x.name)
130
+ norm_name_to_ids[norm_name] = 1
131
+ embed_norm_name[x] = norm_name
132
+ if norm_name not in embed_var_parts:
133
+ embed_var_parts[norm_name] = {part_id: x}
134
+ else:
135
+ embed_var_parts[norm_name][part_id] = x
136
+
137
+ for tid, t in enumerate(norm_name_to_ids.keys()):
138
+ norm_name_to_ids[t] = str(tid)
139
+
140
+ is_cache_from_redis = [ # noqa: F841
141
+ proto_util.is_cache_from_redis(x, redis_cache_names)
142
+ for x in norm_name_to_ids
143
+ ]
144
+
145
+ for x in embed_norm_name:
146
+ embed_norm_name[x] = norm_name_to_ids[embed_norm_name[x]]
147
+
148
+ total_num = 0
149
+ for norm_name in embed_var_parts:
150
+ parts = embed_var_parts[norm_name]
151
+ spos = 0
152
+ part_ids = list(parts.keys())
153
+ part_ids.sort()
154
+ total_num += len(part_ids)
155
+ for part_id in part_ids:
156
+ embed_spos[parts[part_id]] = spos
157
+ spos += parts[part_id].get_shape()[0]
158
+
159
+ redis_url = redis_params.get('redis_url', '')
160
+ redis_passwd = redis_params.get('redis_passwd', '')
161
+ logging.info('will export to redis: %s %s' % (redis_url, redis_passwd))
162
+
163
+ if redis_params.get('redis_write_kv', ''):
164
+ # group embed by devices
165
+ per_device_vars = {}
166
+ for x in embed_norm_name:
167
+ if x.device not in per_device_vars:
168
+ per_device_vars[x.device] = [x]
169
+ else:
170
+ per_device_vars[x.device].append(x)
171
+
172
+ all_write_res = []
173
+ for tmp_dev in per_device_vars:
174
+ tmp_vars = per_device_vars[tmp_dev]
175
+ with tf.device(tmp_dev):
176
+ tmp_names = [embed_norm_name[v] for v in tmp_vars]
177
+ tmp_spos = [np.array(embed_spos[v], dtype=np.int64) for v in tmp_vars]
178
+ write_kv_res = kv_module.write_kv(
179
+ tmp_names,
180
+ tmp_vars,
181
+ tmp_spos,
182
+ url=redis_url,
183
+ password=redis_passwd,
184
+ timeout=redis_params.get('redis_timeout', 1500),
185
+ version=meta_graph_def.meta_info_def.meta_graph_version,
186
+ threads=redis_params.get('redis_threads', 5),
187
+ batch_size=redis_params.get('redis_batch_size', 32),
188
+ expire=redis_params.get('redis_expire', 24),
189
+ verbose=verbose)
190
+ all_write_res.append(write_kv_res)
191
+
192
+ for tmp_dev in embedding_vars:
193
+ with tf.device(tmp_dev):
194
+ tmp_vs = embedding_vars[tmp_dev]
195
+ tmp_sparse_names = [norm_name_to_ids[x[0]] for x in tmp_vs]
196
+ tmp_sparse_keys = [x[1] for x in tmp_vs]
197
+ tmp_sparse_vals = [x[2] for x in tmp_vs]
198
+ write_sparse_kv_res = sparse_kv_module.write_sparse_kv(
199
+ tmp_sparse_names,
200
+ tmp_sparse_vals,
201
+ tmp_sparse_keys,
202
+ url=redis_url,
203
+ password=redis_passwd,
204
+ timeout=redis_params.get('redis_timeout', 1500),
205
+ version=meta_graph_def.meta_info_def.meta_graph_version,
206
+ threads=redis_params.get('redis_threads', 5),
207
+ batch_size=redis_params.get('redis_batch_size', 32),
208
+ expire=redis_params.get('redis_expire', 24),
209
+ verbose=verbose)
210
+ all_write_res.append(write_sparse_kv_res)
211
+
212
+ session_config = ConfigProto(
213
+ allow_soft_placement=True, log_device_placement=False)
214
+ chief_sess_creator = ChiefSessionCreator(
215
+ master=server.target if server else '',
216
+ checkpoint_filename_with_path=checkpoint_path,
217
+ config=session_config)
218
+ with tf.train.MonitoredSession(
219
+ session_creator=chief_sess_creator,
220
+ hooks=None,
221
+ stop_grace_period_secs=120) as sess:
222
+ dump_flags = sess.run(all_write_res)
223
+ logging.info('write embedding to redis succeed: %s' % str(dump_flags))
224
+ else:
225
+ logging.info('will skip write embedding to redis because '
226
+ 'redis_write_kv is set to 0.')
227
+
228
+ # delete embedding_weights collections so that it could be re imported
229
+ tmp_drop = []
230
+ for k in meta_graph_def.collection_def:
231
+ v = meta_graph_def.collection_def[k]
232
+ if len(
233
+ v.node_list.value) > 0 and 'embedding_weights' in v.node_list.value[0]:
234
+ tmp_drop.append(k)
235
+ for k in tmp_drop:
236
+ meta_graph_def.collection_def.pop(k)
237
+
238
+ meta_graph_editor = MetaGraphEditor(
239
+ os.path.join(easy_rec.ops_dir, 'libembed_op.so'),
240
+ None,
241
+ redis_url,
242
+ redis_passwd,
243
+ redis_timeout=redis_params.get('redis_timeout', 600),
244
+ redis_cache_names=redis_cache_names,
245
+ meta_graph_def=meta_graph_def,
246
+ norm_name_to_ids=norm_name_to_ids,
247
+ debug_dir=export_dir if verbose else '')
248
+ meta_graph_editor.edit_graph()
249
+ tf.reset_default_graph()
250
+
251
+ saver = tf.train.import_meta_graph(meta_graph_editor._meta_graph_def)
252
+ graph = tf.get_default_graph()
253
+
254
+ embed_name_to_id_file = os.path.join(export_dir, 'embed_name_to_ids.txt')
255
+ with GFile(embed_name_to_id_file, 'w') as fout:
256
+ for tmp_norm_name in norm_name_to_ids:
257
+ fout.write('%s\t%s\n' % (tmp_norm_name, norm_name_to_ids[tmp_norm_name]))
258
+ ops.add_to_collection(
259
+ tf.GraphKeys.ASSET_FILEPATHS,
260
+ tf.constant(
261
+ embed_name_to_id_file, dtype=tf.string, name='embed_name_to_ids.txt'))
262
+
263
+ export_dir = os.path.join(export_dir,
264
+ meta_graph_def.meta_info_def.meta_graph_version)
265
+ export_dir = io_util.fix_oss_dir(export_dir)
266
+ logging.info('export_dir=%s' % export_dir)
267
+ if Exists(export_dir):
268
+ logging.info('will delete old dir: %s' % export_dir)
269
+ DeleteRecursively(export_dir)
270
+
271
+ builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
272
+ tensor_info_inputs = {}
273
+ for tmp_key in inputs:
274
+ tmp = graph.get_tensor_by_name(inputs[tmp_key].name)
275
+ tensor_info_inputs[tmp_key] = \
276
+ tf.saved_model.utils.build_tensor_info(tmp)
277
+ tensor_info_outputs = {}
278
+ for tmp_key in outputs:
279
+ tmp = graph.get_tensor_by_name(outputs[tmp_key].name)
280
+ tensor_info_outputs[tmp_key] = \
281
+ tf.saved_model.utils.build_tensor_info(tmp)
282
+ signature = (
283
+ tf.saved_model.signature_def_utils.build_signature_def(
284
+ inputs=tensor_info_inputs,
285
+ outputs=tensor_info_outputs,
286
+ method_name=signature_constants.PREDICT_METHOD_NAME))
287
+
288
+ session_config = ConfigProto(
289
+ allow_soft_placement=True, log_device_placement=True)
290
+
291
+ saver = tf.train.Saver()
292
+ with tf.Session(target=server.target if server else '') as sess:
293
+ saver.restore(sess, checkpoint_path)
294
+
295
+ builder.add_meta_graph_and_variables(
296
+ sess, [tf.saved_model.tag_constants.SERVING],
297
+ signature_def_map={
298
+ signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature,
299
+ },
300
+ assets_collection=ops.get_collection(ops.GraphKeys.ASSET_FILEPATHS),
301
+ saver=saver,
302
+ strip_default_attrs=True,
303
+ clear_devices=True)
304
+ builder.save()
305
+
306
+ # remove temporary files
307
+ Remove(embed_name_to_id_file)
308
+ return export_dir
309
+
310
+
311
+ def export_big_model_to_oss(export_dir, pipeline_config, oss_params,
312
+ serving_input_fn, estimator, checkpoint_path,
313
+ verbose):
314
+ for key in oss_params:
315
+ logging.info('%s: %s' % (key, oss_params[key]))
316
+
317
+ write_kv_lib_path = os.path.join(easy_rec.ops_dir, 'libembed_op.so')
318
+ kv_module = tf.load_op_library(write_kv_lib_path)
319
+
320
+ if not checkpoint_path:
321
+ checkpoint_path = estimator_utils.latest_checkpoint(
322
+ pipeline_config.model_dir)
323
+ logging.info('checkpoint_path = %s' % checkpoint_path)
324
+
325
+ server = None
326
+ cluster = None
327
+ if 'TF_CONFIG' in os.environ:
328
+ # change chief to master
329
+ tf_config = estimator_utils.chief_to_master()
330
+ if tf_config['task']['type'] == 'ps':
331
+ cluster = tf.train.ClusterSpec(tf_config['cluster'])
332
+ server = tf.train.Server(
333
+ cluster, job_name='ps', task_index=tf_config['task']['index'])
334
+ server.join()
335
+ elif tf_config['task']['type'] == 'master':
336
+ if 'ps' in tf_config['cluster']:
337
+ cluster = tf.train.ClusterSpec(tf_config['cluster'])
338
+ server = tf.train.Server(cluster, job_name='master', task_index=0)
339
+ server_target = server.target
340
+ logging.info('server_target = %s' % server_target)
341
+
342
+ serving_input = serving_input_fn()
343
+ features = serving_input.features
344
+ inputs = serving_input.receiver_tensors
345
+
346
+ if cluster:
347
+ logging.info('cluster = ' + str(cluster))
348
+ with tf.device(
349
+ replica_device_setter(
350
+ worker_device='/job:master/task:0', cluster=cluster)):
351
+ outputs = estimator._export_model_fn(features, None, None,
352
+ estimator.params).predictions
353
+
354
+ meta_graph_def = export_meta_graph()
355
+ meta_graph_def.meta_info_def.meta_graph_version = str(int(time.time()))
356
+ oss_embedding_version = oss_params.get('oss_embedding_version', '')
357
+ if not oss_embedding_version:
358
+ meta_graph_def.meta_info_def.meta_graph_version =\
359
+ str(int(time.time()))
360
+ else:
361
+ meta_graph_def.meta_info_def.meta_graph_version = oss_embedding_version
362
+
363
+ logging.info('meta_graph_version = %s' %
364
+ meta_graph_def.meta_info_def.meta_graph_version)
365
+
366
+ embed_var_parts = {}
367
+ embed_norm_name = {}
368
+ embed_spos = {}
369
+ # pai embedding variable
370
+ embedding_vars = {}
371
+ norm_name_to_ids = {}
372
+ for x in global_variables():
373
+ tf.logging.info('global var: %s %s %s' % (x.name, str(type(x)), x.device))
374
+ if 'EmbeddingVariable' in str(type(x)):
375
+ norm_name, part_id = proto_util.get_norm_embed_name(x.name)
376
+ norm_name_to_ids[norm_name] = 1
377
+ tmp_export = x.export()
378
+ if x.device not in embedding_vars:
379
+ embedding_vars[x.device] = [(norm_name, tmp_export.keys,
380
+ tmp_export.values, part_id)]
381
+ else:
382
+ embedding_vars[x.device].append(
383
+ (norm_name, tmp_export.keys, tmp_export.values, part_id))
384
+ elif '/embedding_weights:' in x.name or '/embedding_weights/part_' in x.name:
385
+ norm_name, part_id = proto_util.get_norm_embed_name(x.name)
386
+ norm_name_to_ids[norm_name] = 1
387
+ embed_norm_name[x] = norm_name
388
+ if norm_name not in embed_var_parts:
389
+ embed_var_parts[norm_name] = {part_id: x}
390
+ else:
391
+ embed_var_parts[norm_name][part_id] = x
392
+
393
+ for tid, t in enumerate(norm_name_to_ids.keys()):
394
+ norm_name_to_ids[t] = str(tid)
395
+
396
+ for x in embed_norm_name:
397
+ embed_norm_name[x] = norm_name_to_ids[embed_norm_name[x]]
398
+
399
+ total_num = 0
400
+ for norm_name in embed_var_parts:
401
+ parts = embed_var_parts[norm_name]
402
+ spos = 0
403
+ part_ids = list(parts.keys())
404
+ part_ids.sort()
405
+ total_num += len(part_ids)
406
+ for part_id in part_ids:
407
+ embed_spos[parts[part_id]] = spos
408
+ spos += parts[part_id].get_shape()[0]
409
+
410
+ oss_path = oss_params.get('oss_path', '')
411
+ oss_endpoint = oss_params.get('oss_endpoint', '')
412
+ oss_ak = oss_params.get('oss_ak', '')
413
+ oss_sk = oss_params.get('oss_sk', '')
414
+ logging.info('will export to oss: %s %s %s %s', oss_path, oss_endpoint,
415
+ oss_ak, oss_sk)
416
+
417
+ if oss_params.get('oss_write_kv', ''):
418
+ # group embed by devices
419
+ per_device_vars = {}
420
+ for x in embed_norm_name:
421
+ if x.device not in per_device_vars:
422
+ per_device_vars[x.device] = [x]
423
+ else:
424
+ per_device_vars[x.device].append(x)
425
+
426
+ all_write_res = []
427
+ for tmp_dev in per_device_vars:
428
+ tmp_vars = per_device_vars[tmp_dev]
429
+ with tf.device(tmp_dev):
430
+ tmp_names = [embed_norm_name[v] for v in tmp_vars]
431
+ tmp_spos = [np.array(embed_spos[v], dtype=np.int64) for v in tmp_vars]
432
+ write_kv_res = kv_module.oss_write_kv(
433
+ tmp_names,
434
+ tmp_vars,
435
+ tmp_spos,
436
+ osspath=oss_path,
437
+ endpoint=oss_endpoint,
438
+ ak=oss_ak,
439
+ sk=oss_sk,
440
+ threads=oss_params.get('oss_threads', 5),
441
+ timeout=5,
442
+ expire=5,
443
+ verbose=verbose)
444
+ all_write_res.append(write_kv_res)
445
+
446
+ for tmp_dev in embedding_vars:
447
+ with tf.device(tmp_dev):
448
+ tmp_vs = embedding_vars[tmp_dev]
449
+ tmp_sparse_names = [norm_name_to_ids[x[0]] for x in tmp_vs]
450
+ tmp_sparse_keys = [x[1] for x in tmp_vs]
451
+ tmp_sparse_vals = [x[2] for x in tmp_vs]
452
+ tmp_part_ids = [x[3] for x in tmp_vs]
453
+ write_sparse_kv_res = kv_module.oss_write_sparse_kv(
454
+ tmp_sparse_names,
455
+ tmp_sparse_vals,
456
+ tmp_sparse_keys,
457
+ tmp_part_ids,
458
+ osspath=oss_path,
459
+ endpoint=oss_endpoint,
460
+ ak=oss_ak,
461
+ sk=oss_sk,
462
+ version=meta_graph_def.meta_info_def.meta_graph_version,
463
+ threads=oss_params.get('oss_threads', 5),
464
+ verbose=verbose)
465
+ all_write_res.append(write_sparse_kv_res)
466
+
467
+ session_config = ConfigProto(
468
+ allow_soft_placement=True, log_device_placement=False)
469
+ chief_sess_creator = ChiefSessionCreator(
470
+ master=server.target if server else '',
471
+ checkpoint_filename_with_path=checkpoint_path,
472
+ config=session_config)
473
+ with tf.train.MonitoredSession(
474
+ session_creator=chief_sess_creator,
475
+ hooks=None,
476
+ stop_grace_period_secs=120) as sess:
477
+ dump_flags = sess.run(all_write_res)
478
+ logging.info('write embedding to oss succeed: %s' % str(dump_flags))
479
+ else:
480
+ logging.info('will skip write embedding to oss because '
481
+ 'oss_write_kv is set to 0.')
482
+
483
+ # delete embedding_weights collections so that it could be re imported
484
+ tmp_drop = []
485
+ for k in meta_graph_def.collection_def:
486
+ v = meta_graph_def.collection_def[k]
487
+ if len(
488
+ v.node_list.value) > 0 and 'embedding_weights' in v.node_list.value[0]:
489
+ tmp_drop.append(k)
490
+ for k in tmp_drop:
491
+ meta_graph_def.collection_def.pop(k)
492
+
493
+ meta_graph_editor = MetaGraphEditor(
494
+ os.path.join(easy_rec.ops_dir, 'libembed_op.so'),
495
+ None,
496
+ oss_path=oss_path,
497
+ oss_endpoint=oss_endpoint,
498
+ oss_ak=oss_ak,
499
+ oss_sk=oss_sk,
500
+ oss_timeout=oss_params.get('oss_timeout', 1500),
501
+ meta_graph_def=meta_graph_def,
502
+ norm_name_to_ids=norm_name_to_ids,
503
+ incr_update_params=oss_params.get('incr_update', None),
504
+ debug_dir=export_dir if verbose else '')
505
+ meta_graph_editor.edit_graph_for_oss()
506
+ tf.reset_default_graph()
507
+
508
+ saver = tf.train.import_meta_graph(meta_graph_editor._meta_graph_def)
509
+ graph = tf.get_default_graph()
510
+
511
+ embed_name_to_id_file = os.path.join(export_dir, 'embed_name_to_ids.txt')
512
+ with GFile(embed_name_to_id_file, 'w') as fout:
513
+ for tmp_norm_name in norm_name_to_ids:
514
+ fout.write('%s\t%s\n' % (tmp_norm_name, norm_name_to_ids[tmp_norm_name]))
515
+ ops.add_to_collection(
516
+ ops.GraphKeys.ASSET_FILEPATHS,
517
+ tf.constant(
518
+ embed_name_to_id_file, dtype=tf.string, name='embed_name_to_ids.txt'))
519
+
520
+ if 'incr_update' in oss_params:
521
+ dense_train_vars_path = os.path.join(
522
+ os.path.dirname(checkpoint_path), constant.DENSE_UPDATE_VARIABLES)
523
+ ops.add_to_collection(
524
+ ops.GraphKeys.ASSET_FILEPATHS,
525
+ tf.constant(
526
+ dense_train_vars_path,
527
+ dtype=tf.string,
528
+ name=constant.DENSE_UPDATE_VARIABLES))
529
+
530
+ asset_file = 'incr_update.txt'
531
+ asset_file_path = os.path.join(export_dir, asset_file)
532
+ with GFile(asset_file_path, 'w') as fout:
533
+ incr_update = oss_params['incr_update']
534
+ incr_update_json = {}
535
+ if 'kafka' in incr_update:
536
+ incr_update_json['storage'] = 'kafka'
537
+ incr_update_json['kafka'] = json.loads(
538
+ json_format.MessageToJson(
539
+ incr_update['kafka'], preserving_proto_field_name=True))
540
+ elif 'datahub' in incr_update:
541
+ incr_update_json['storage'] = 'datahub'
542
+ incr_update_json['datahub'] = json.loads(
543
+ json_format.MessageToJson(
544
+ incr_update['datahub'], preserving_proto_field_name=True))
545
+ elif 'fs' in incr_update:
546
+ incr_update_json['storage'] = 'fs'
547
+ incr_update_json['fs'] = {'incr_save_dir': incr_update['fs'].mount_path}
548
+ json.dump(incr_update_json, fout, indent=2)
549
+
550
+ ops.add_to_collection(
551
+ ops.GraphKeys.ASSET_FILEPATHS,
552
+ tf.constant(asset_file_path, dtype=tf.string, name=asset_file))
553
+
554
+ export_dir = os.path.join(export_dir,
555
+ meta_graph_def.meta_info_def.meta_graph_version)
556
+ export_dir = io_util.fix_oss_dir(export_dir)
557
+ logging.info('export_dir=%s' % export_dir)
558
+ if Exists(export_dir):
559
+ logging.info('will delete old dir: %s' % export_dir)
560
+ DeleteRecursively(export_dir)
561
+
562
+ builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
563
+ tensor_info_inputs = {}
564
+ for tmp_key in inputs:
565
+ tmp = graph.get_tensor_by_name(inputs[tmp_key].name)
566
+ tensor_info_inputs[tmp_key] = \
567
+ tf.saved_model.utils.build_tensor_info(tmp)
568
+
569
+ tensor_info_outputs = {}
570
+ for tmp_key in outputs:
571
+ tmp = graph.get_tensor_by_name(outputs[tmp_key].name)
572
+ tensor_info_outputs[tmp_key] = \
573
+ tf.saved_model.utils.build_tensor_info(tmp)
574
+ signature = (
575
+ tf.saved_model.signature_def_utils.build_signature_def(
576
+ inputs=tensor_info_inputs,
577
+ outputs=tensor_info_outputs,
578
+ method_name=signature_constants.PREDICT_METHOD_NAME))
579
+
580
+ if 'incr_update' in oss_params:
581
+ incr_update_inputs = meta_graph_editor.sparse_update_inputs
582
+ incr_update_outputs = meta_graph_editor.sparse_update_outputs
583
+ incr_update_inputs.update(meta_graph_editor.dense_update_inputs)
584
+ incr_update_outputs.update(meta_graph_editor.dense_update_outputs)
585
+ tensor_info_incr_update_inputs = {}
586
+ tensor_info_incr_update_outputs = {}
587
+ for tmp_key in incr_update_inputs:
588
+ tmp = graph.get_tensor_by_name(incr_update_inputs[tmp_key].name)
589
+ tensor_info_incr_update_inputs[tmp_key] = \
590
+ tf.saved_model.utils.build_tensor_info(tmp)
591
+ for tmp_key in incr_update_outputs:
592
+ tmp = graph.get_tensor_by_name(incr_update_outputs[tmp_key].name)
593
+ tensor_info_incr_update_outputs[tmp_key] = \
594
+ tf.saved_model.utils.build_tensor_info(tmp)
595
+ incr_update_signature = (
596
+ tf.saved_model.signature_def_utils.build_signature_def(
597
+ inputs=tensor_info_incr_update_inputs,
598
+ outputs=tensor_info_incr_update_outputs,
599
+ method_name=signature_constants.PREDICT_METHOD_NAME))
600
+ else:
601
+ incr_update_signature = None
602
+
603
+ session_config = ConfigProto(
604
+ allow_soft_placement=True, log_device_placement=True)
605
+
606
+ saver = tf.train.Saver()
607
+ with tf.Session(target=server.target if server else '') as sess:
608
+ saver.restore(sess, checkpoint_path)
609
+ main_op = tf.group([
610
+ Scaffold.default_local_init_op(),
611
+ ops.get_collection(EMBEDDING_INITIALIZERS)
612
+ ])
613
+ incr_update_sig_map = {
614
+ signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature
615
+ }
616
+ if incr_update_signature is not None:
617
+ incr_update_sig_map[INCR_UPDATE_SIGNATURE_KEY] = incr_update_signature
618
+ builder.add_meta_graph_and_variables(
619
+ sess, [tf.saved_model.tag_constants.SERVING],
620
+ signature_def_map=incr_update_sig_map,
621
+ assets_collection=ops.get_collection(ops.GraphKeys.ASSET_FILEPATHS),
622
+ saver=saver,
623
+ main_op=main_op,
624
+ strip_default_attrs=True,
625
+ clear_devices=True)
626
+ builder.save()
627
+
628
+ # remove temporary files
629
+ Remove(embed_name_to_id_file)
630
+ return export_dir