Skip to content

Commit 9222580

Browse files
authored
Add docker_split_mlc_run_cmd to split Dockerfile MLC run flow into cacheable layers (mlcommons#924)
1 parent 75eb391 commit 9222580

5 files changed

Lines changed: 20 additions & 9 deletions

File tree

automation/script/docker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ def dockerfile(self_module, input_params):
2525
# Step 2: Process Dockerfile-related configurations
2626
env = input_params.get('env', {})
2727

28-
host_only_env_keys = [ "HOME", "USER" ]
28+
host_only_env_keys = ["HOME", "USER"]
2929
for key in host_only_env_keys:
3030
if key in env:
31-
del(env[key])
31+
del (env[key])
3232

3333
state_data = input_params.get('state', {})
3434
constant_vars = input_params.get('const', {})
@@ -226,10 +226,10 @@ def docker_run(self_module, i):
226226
show_time = i.get('show_time', False)
227227
logger = self_module.logger
228228
env = i.get('env', {})
229-
host_only_env_keys = [ "HOME", "USER" ]
229+
host_only_env_keys = ["HOME", "USER"]
230230
for key in host_only_env_keys:
231231
if key in env:
232-
del(env[key])
232+
del (env[key])
233233

234234
self_module.env = env
235235

automation/script/docker_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ def prepare_docker_inputs(input_params, docker_settings,
142142
"mlc_repos", "skip_mlc_sys_upgrade", "extra_sys_deps", "image_name",
143143
"gh_token", "fake_run_deps", "run_final_cmds", "real_run", "copy_files", "path", "user", "env", "build_env"
144144
]
145+
keys += ["cache", "split_mlc_run_cmd"]
145146

146147
if run_stage:
147148
keys += [

script/build-dockerfile/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ mlcr build,dockerfile
7474
| `--run_cmd` | | | `` |
7575
| `--run_cmd_extra` | | | `` |
7676
| `--script_tags` | | | `` |
77+
| `--split_mlc_run_cmd` | Split dependent MLC run commands into separate Dockerfile RUN layers (effective with cache enabled) | | `` |
7778
| `--skip_mlc_sys_upgrade` | | | `` |
7879
| `--user` | | | `` |
7980
### Generic Script Inputs

script/build-dockerfile/customize.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,17 @@ def preprocess(i):
427427
env['MLC_DOCKER_RUN_CMD']
428428

429429
logger.info(env['MLC_DOCKER_RUN_CMD'])
430+
split_mlc_run_cmd = is_true(env.get('MLC_DOCKER_SPLIT_MLC_RUN_CMD', ''))
431+
if split_mlc_run_cmd and is_false(env.get('MLC_DOCKER_CACHE', "True")):
432+
logger.warning(
433+
"Ignoring docker_split_mlc_run_cmd since docker cache is disabled.")
434+
split_mlc_run_cmd = False
435+
430436
fake_run = env.get("MLC_DOCKER_FAKE_RUN_OPTION",
431437
" --fake_run") + dockerfile_env_input_string
438+
enable_fake_deps = env.get('MLC_DOCKER_FAKE_DEPS')
432439
fake_run = fake_run + \
433-
" --fake_deps" if env.get('MLC_DOCKER_FAKE_DEPS') else fake_run
440+
" --fake_deps" if enable_fake_deps else fake_run
434441

435442
x = 'RUN ' + _run_secret_prefix + env['MLC_DOCKER_RUN_CMD']
436443

@@ -441,8 +448,9 @@ def preprocess(i):
441448
if run_cmd_extra != '':
442449
x += ' ' + run_cmd_extra
443450

444-
if env.get('MLC_DOCKER_RUN_SCRIPT_TAGS', '') != '' and is_true(env.get(
445-
'MLC_DOCKER_ADD_DEPENDENT_SCRIPTS_RUN_COMMANDS', '')):
451+
if env.get('MLC_DOCKER_RUN_SCRIPT_TAGS', '') != '' and (
452+
split_mlc_run_cmd or is_true(
453+
env.get('MLC_DOCKER_ADD_DEPENDENT_SCRIPTS_RUN_COMMANDS', ''))):
446454
mlc_input = {'action': 'run',
447455
'automation': 'script',
448456
'tags': f"""{env['MLC_DOCKER_RUN_SCRIPT_TAGS']}""",
@@ -452,11 +460,11 @@ def preprocess(i):
452460
'fake_run': True,
453461
'fake_deps': True
454462
}
455-
r = self_module.mlc.access(mlc_input)
463+
r = automation.action_object.access(mlc_input)
456464
if r['return'] > 0:
457465
return r
458466
print_deps = r['new_state']['print_deps']
459-
fake_run_str = " --fake_run" if env.get('MLC_DOCKER_FAKE_DEPS') else ""
467+
fake_run_str = " --fake_run" if enable_fake_deps else ""
460468
cmds = ["RUN " + dep for dep in print_deps]
461469
for cmd in cmds:
462470
f.write(cmd + fake_run_str + EOL)

script/build-dockerfile/meta.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ input_mapping:
6060
run_cmd: MLC_DOCKER_RUN_CMD
6161
run_cmd_extra: MLC_DOCKER_RUN_CMD_EXTRA
6262
script_tags: MLC_DOCKER_RUN_SCRIPT_TAGS
63+
split_mlc_run_cmd: MLC_DOCKER_SPLIT_MLC_RUN_CMD
6364
skip_mlc_sys_upgrade: MLC_DOCKER_SKIP_MLC_SYS_UPGRADE
6465
user: MLC_DOCKER_USER
6566

0 commit comments

Comments
 (0)