Skip to content

Commit 4e4a27e

Browse files
authored
Support hf_token in CM docker runs (#114)
* Support GH_PAT for windows in push-mlperf-inference-results-to-github * Added huggingface-cli script * Added support to modify cache via meta
1 parent 50c059c commit 4e4a27e

File tree

7 files changed

+90
-2
lines changed

7 files changed

+90
-2
lines changed

automation/script/module.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def __init__(self, cmind, automation_file):
3232
self.run_state['fake_deps'] = False
3333
self.run_state['parent'] = None
3434
self.run_state['version_info'] = []
35+
self.run_state['cache'] = False
3536

3637
self.file_with_cached_state = 'cm-cached-state.json'
3738

@@ -74,7 +75,8 @@ def __init__(self, cmind, automation_file):
7475
'accept_license',
7576
'skip_system_deps',
7677
'git_ssh',
77-
'gh_token']
78+
'gh_token',
79+
'hf_token']
7880

7981
############################################################
8082

@@ -826,6 +828,7 @@ def _run(self, i):
826828
'alias', '')
827829
run_state['script_repo_git'] = script_artifact.repo_meta.get(
828830
'git', False)
831+
run_state['cache'] = meta.get('cache', False)
829832

830833
if not recursion:
831834
run_state['script_entry_repo_to_report_errors'] = meta.get(
@@ -1125,7 +1128,7 @@ def _run(self, i):
11251128
# Check if the output of a selected script should be cached
11261129
cache = False if i.get(
11271130
'skip_cache',
1128-
False) else meta.get(
1131+
False) else run_state.get(
11291132
'cache',
11301133
False)
11311134
cache = cache or (
@@ -6163,6 +6166,9 @@ def update_state_from_meta(meta, env, state, const, const_state, deps, post_deps
61636166
Internal: update env and state from meta
61646167
"""
61656168

6169+
if meta.get('cache', '') != '':
6170+
run_state['cache'] = meta['cache']
6171+
61666172
default_env = meta.get('default_env', {})
61676173
for key in default_env:
61686174
env.setdefault(key, default_env[key])

script/get-generic-python-lib/detect-version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33

44
package_name = os.environ.get('CM_GENERIC_PYTHON_PACKAGE_NAME', '')
5+
package_name = package_name.split("[")[0]
56

67
filename = 'tmp-ver.out'
78

script/get-huggingface-cli/_cm.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
alias: get-huggingface-cli
2+
automation_alias: script
3+
automation_uid: 5b4e0237da074764
4+
cache: false
5+
can_force_cache: true
6+
category: DevOps automation
7+
clean_files: []
8+
deps:
9+
- tags: detect,os
10+
tags:
11+
- get
12+
- huggingface
13+
- hf-cli
14+
- huggingface-cli
15+
- cli
16+
input_mapping:
17+
token: CM_HF_TOKEN
18+
uid: e9488a272f1d4160
19+
deps:
20+
- tags: get,generic-python-lib,_package.huggingface_hub[cli]
21+
variations:
22+
with-login:
23+
cache: true
24+
force_cache: true
25+
env:
26+
CM_HF_DO_LOGIN: yes
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from cmind import utils
2+
import os
3+
4+
5+
def preprocess(i):
6+
env = i['env']
7+
if env.get('CM_HF_TOKEN', '') != '':
8+
env['CM_HF_LOGIN_CMD'] = f"""git config --global credential.helper store && huggingface-cli login --token {env['CM_HF_TOKEN']} --add-to-git-credential
9+
"""
10+
elif str(env.get('CM_HF_DO_LOGIN')).lower() in ["yes", "1", "true"]:
11+
env['CM_HF_LOGIN_CMD'] = f"""git config --global credential.helper store && huggingface-cli login
12+
"""
13+
return {'return': 0}
14+
15+
16+
def postprocess(i):
17+
env = i['env']
18+
19+
r = i['automation'].parse_version({'match_text': r'huggingface_hub\s*version:\s*([\d.]+)',
20+
'group_number': 1,
21+
'env_key': 'CM_GITHUBCLI_VERSION',
22+
'which_env': i['env']})
23+
if r['return'] > 0:
24+
return r
25+
26+
version = r['version']
27+
28+
print(i['recursion_spaces'] + ' Detected version: {}'.format(version))
29+
30+
return {'return': 0, 'version': version}

script/get-huggingface-cli/run.bat

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@echo off
2+
REM Check if the environment variable CM_HF_LOGIN_CMD is defined and not empty
3+
IF DEFINED CM_HF_LOGIN_CMD (
4+
echo %CM_HF_LOGIN_CMD%
5+
call %CM_HF_LOGIN_CMD%
6+
IF ERRORLEVEL 1 (
7+
echo Command failed with error code %ERRORLEVEL%
8+
exit /b %ERRORLEVEL%
9+
)
10+
)
11+
12+
REM Run the Hugging Face CLI version command and save output
13+
huggingface-cli version > tmp-ver.out
14+

script/get-huggingface-cli/run.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
if [[ -n ${CM_HF_LOGIN_CMD} ]]; then
3+
echo "${CM_HF_LOGIN_CMD}"
4+
eval ${CM_HF_LOGIN_CMD}
5+
test $? -eq 0 || exit $?
6+
fi
7+
huggingface-cli version > tmp-ver.out

script/get-ml-model-huggingface-zoo/_cm.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ uid: 53cf8252a443446a
3535
variations:
3636
clone-repo:
3737
deps:
38+
- tags: get,hf-cli,_with-token
39+
enable_if_env:
40+
CM_HF_TOKEN:
41+
- on
3842
- env:
3943
CM_GIT_CHECKOUT_PATH_ENV_NAME: CM_ML_MODEL_PATH
4044
tags: get,git,repo,_lfs

0 commit comments

Comments
 (0)