-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathExport_All.py
More file actions
166 lines (121 loc) · 5.07 KB
/
Copy pathExport_All.py
File metadata and controls
166 lines (121 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Databricks notebook source
# MAGIC %md
# MAGIC ##Export All
# MAGIC
# MAGIC ##Export all the MLflow registered models and all experiments of a tracking server.
# MAGIC
# MAGIC **Widgets**
# MAGIC * `1. Output directory` - shared directory between source and destination workspaces.
# MAGIC * `2. Stages` - comma seperated stages to be exported.
# MAGIC * `3. Export latest versions` - export all or just the "latest" versions.
# MAGIC * `4. Run start date` - Export runs after this UTC date (inclusive). Example: `2023-04-05`.
# MAGIC * `5. Export permissions` - export Databricks permissions.
# MAGIC * `6. Export deleted runs`
# MAGIC * `7. Export version MLflow model`
# MAGIC * `8. Notebook formats`
# MAGIC * `9. Use threads`
# COMMAND ----------
# MAGIC %run ./Common
# COMMAND ----------
from mlflow_export_import.bulk import config
import time
import os
from datetime import datetime
# COMMAND ----------
dbutils.widgets.text("output_dir","")
output_dir = dbutils.widgets.get("output_dir")
output_dir = output_dir.replace("dbfs:","/dbfs")
dbutils.widgets.multiselect("stages", "Production", ["Production","Staging","Archived","None"])
stages = dbutils.widgets.get("stages")
dbutils.widgets.dropdown("export_latest_versions","false",["true","false"])
export_latest_versions = dbutils.widgets.get("export_latest_versions") == "true"
dbutils.widgets.text("run_start_date", "")
run_start_date = dbutils.widgets.get("run_start_date")
dbutils.widgets.dropdown("export_permissions","false",["true","false"])
export_permissions = dbutils.widgets.get("export_permissions") == "true"
dbutils.widgets.text("task_index", "1")
task_index = int(dbutils.widgets.get("task_index"))
dbutils.widgets.text("num_tasks", "1")
num_tasks = int(dbutils.widgets.get("num_tasks"))
dbutils.widgets.text("run_timestamp", "")
run_timestamp = dbutils.widgets.get("run_timestamp")
dbutils.widgets.text("jobrunid", "")
jobrunid = dbutils.widgets.get("jobrunid")
dbutils.widgets.text("model_file_name", "")
model_file_name = dbutils.widgets.get("model_file_name")
dbutils.widgets.dropdown("source_model_registry","unity_catalog",["unity_catalog","workspace_registry"])
source_model_registry = dbutils.widgets.get("source_model_registry")
dbutils.widgets.dropdown("Cloud","azure",["azure","aws","gcp"])
cloud = dbutils.widgets.get("Cloud")
if run_start_date=="": run_start_date = None
print("output_dir:", output_dir)
print("stages:", stages)
print("export_latest_versions:", export_latest_versions)
print("run_start_date:", run_start_date)
print("export_permissions:", export_permissions)
print("task_index:", task_index)
print("num_tasks:", num_tasks)
print("run_timestamp:", run_timestamp)
print("jobrunid:", jobrunid)
print("model_file_name:", model_file_name)
print("source_model_registry:", source_model_registry)
# COMMAND ----------
checkpoint_dir_experiment = os.path.join(output_dir, run_timestamp,"checkpoint", "experiments")
try:
if not os.path.exists(checkpoint_dir_experiment):
os.makedirs(checkpoint_dir_experiment, exist_ok=True)
print(f"checkpoint_dir_experiment: created {checkpoint_dir_experiment}")
except Exception as e:
raise Exception(f"Failed to create directory {checkpoint_dir_experiment}: {e}")
# COMMAND ----------
checkpoint_dir_model = os.path.join(output_dir, run_timestamp,"checkpoint", "models")
try:
if not os.path.exists(checkpoint_dir_model):
os.makedirs(checkpoint_dir_model, exist_ok=True)
print(f"checkpoint_dir_model: created {checkpoint_dir_model}")
except Exception as e:
raise Exception(f"Failed to create directory {checkpoint_dir_model}: {e}")
# COMMAND ----------
output_dir = os.path.join(output_dir, run_timestamp, jobrunid, str(task_index))
output_dir
# COMMAND ----------
log_path=f"/tmp/exportall_{task_index}.log"
log_path
# COMMAND ----------
# curr_timestamp = datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
# log_path = f"{output_dir}/export_all_{task_index}_{curr_timestamp}.log"
# COMMAND ----------
config.log_path=log_path
config.target_model_registry=source_model_registry
# COMMAND ----------
from mlflow_export_import.bulk.export_all import export_all
export_all(
output_dir = output_dir,
stages = stages,
export_latest_versions = export_latest_versions,
run_start_time = run_start_date,
export_permissions = export_permissions,
export_deleted_runs = False,
export_version_model = False,
notebook_formats = ['SOURCE'],
use_threads = True,
task_index = task_index,
num_tasks = num_tasks,
checkpoint_dir_experiment = checkpoint_dir_experiment,
checkpoint_dir_model = checkpoint_dir_model,
model_names = model_file_name
)
# COMMAND ----------
time.sleep(10)
# COMMAND ----------
# MAGIC %sh cat /tmp/my.log
# COMMAND ----------
dbfs_log_path = f"{output_dir}/export_all_{task_index}.log"
if dbfs_log_path.startswith("/Workspace"):
dbfs_log_path=dbfs_log_path.replace("/Workspace","file:/Workspace")
dbfs_log_path = dbfs_log_path.replace("/dbfs","dbfs:")
dbfs_log_path
# COMMAND ----------
dbutils.fs.cp(f"file:{log_path}", dbfs_log_path)
# COMMAND ----------
print(dbutils.fs.head(dbfs_log_path))