MLflow 클라이언트 호환성

Tier: Free, Premium, Ultimate Offering: GitLab.com, Self-Managed, GitLab Dedicated Status: Experiment
  • GitLab 15.11에서 실험 릴리스인 Experiment로 도입되었습니다. 기본으로 비활성화 상태인 ml_experiment_tracking라는 플래그와 함께.
note
모델 레지스트리 및 모델 실험 추적은 실험입니다. 모델 실험 추적에 대한 피드백을 제공하세요. 모델 레지스트리에 대한 피드백도 제공하세요.

MLflow는 머신 러닝 실험 추적을 위한 인기 있는 오픈 소스 도구입니다. GitLab 모델 실험 추적 및 GitLab 모델 레지스트리는 MLflow 클라이언트와 호환됩니다. 설정에 기존 코드에는 최소한의 변경이 필요합니다.

GitLab는 MLflow 서버 역할을 합니다. mlflow server를 실행할 필요가 없습니다.

MLflow 클라이언트 통합 활성화

전제 조건:

  • 최소한 Developer 역할과 api 권한이 있는 개인, 프로젝트, 또는 그룹 액세스 토큰
  • 프로젝트 ID. 프로젝트 ID를 찾으려면:
    1. 왼쪽 사이드바에서 검색 또는 이동을 선택하고 프로젝트를 찾습니다.
    2. 설정 > 일반을 선택합니다.

로컬 환경에서 MLflow 클라이언트 호환성을 사용하려면:

  1. 코드를 실행하는 호스트에서 추적 URI 및 토큰 환경 변수를 설정하세요. 이는 로컬 환경, CI 파이프라인 또는 원격 호스트일 수 있습니다. 예:

    export MLFLOW_TRACKING_URI="<귀하의 gitlab 엔드포인트>/api/v4/projects/<귀하의 프로젝트 ID>/ml/mlflow"
    export MLFLOW_TRACKING_TOKEN="<귀하의_액세스_토큰>"
    
  2. 훈련 코드에 mlflow.set_tracking_uri() 호출이 포함되어 있다면 제거하세요.

모델 레지스트리에서는 오른쪽 상단의 수직 탈자()를 선택하여 추적 URI를 복사할 수 있습니다.

모델 실험

훈련 코드를 실행하는 동안 MLflow 클라이언트를 사용하여 GitLab에 실험, 실행, 모델, 모델 버전, 매개변수, 메트릭, 메타데이터 및 아티팩트를 만들 수 있습니다.

실험이 기록되면 /<귀하의 프로젝트>/-/ml/experiments에 나열됩니다.

실행은 후보로 등록되며, 실험, 모델 또는 모델 버전을 선택하여 탐색할 수 있습니다.

후보를 CI/CD 작업에 연결

  • GitLab 16.1에서 도입되었습니다.

훈련 코드가 CI/CD 작업에서 실행 중이면, GitLab은 그 정보를 활용하여 후보 메타데이터를 향상시킬 수 있습니다. 후보를 CI/CD 작업에 연결하려면:

  1. 프로젝트 CI 변수에 다음 변수를 포함하세요:
    • MLFLOW_TRACKING_URI: "<귀하의 gitlab 엔드포인트>/api/v4/projects/<귀하의 프로젝트 ID>/ml/mlflow"
    • MLFLOW_TRACKING_TOKEN: <귀하의_액세스_토큰>
  2. 실행 실행 컨텍스트 내의 훈련 코드에 다음 코드 조각을 추가하세요:

     with mlflow.start_run(run_name=f"Candidate {index}"):
       # 여기에 귀하의 훈련 코드 추가
          
       # 포함해야 할 코드 조각 시작
       if os.getenv('GITLAB_CI'):
         mlflow.set_tag('gitlab.CI_JOB_ID', os.getenv('CI_JOB_ID'))
       # 포함해야 할 코드 조각 끝
    

모델 레지스트리

MLflow 클라이언트를 사용하여 모델과 모델 버전을 관리할 수도 있습니다. 모델은 /<귀하의 프로젝트>/-/ml/models에 등록됩니다.

모델

모델 생성

client = MlflowClient()
model_name = '<귀하의_모델_이름>'
description = '모델 설명'
model = client.create_registered_model(model_name, description=description)

참고

  • create_registered_model의 인자 tags는 무시됩니다.
  • name은 프로젝트 내에서 고유해야 합니다.
  • name은 기존 실험의 이름일 수 없습니다.

모델 가져오기

client = MlflowClient()
model_name = '<귀하의_모델_이름>'
model = client.get_registered_model(model_name)

모델 업데이트

client = MlflowClient()
model_name = '<귀하의_모델_이름>'
description = '새 설명'
client.update_registered_model(model_name, description=description)

모델 삭제

client = MlflowClient()
model_name = '<귀하의_모델_이름>'
client.delete_registered_model(model_name)

후보를 모델에 로깅

모든 모델은 동일한 이름으로 [model]가 접두어인 실험과 연관되어 있습니다. 후보/실행을 모델에 기록하려면 올바른 이름을 사용하여 실험을 만드세요:

client = MlflowClient()
model_name = '<귀하의_모델_이름>'
exp = client.get_experiment_by_name(f"[model]{model_name}")
run = client.create_run(exp.experiment_id)

모델 버전

모델 버전 생성

client = MlflowClient()
model_name = '<귀하의_모델_이름>'
description = '모델 버전 설명'
model_version = client.create_model_version(model_name, source="", description=description)

버전 매개변수가 전달되지 않으면, 최신 업로드된 버전에서 자동으로 증가합니다. 버전을 설정하려면 모델 버전 생성 중에 태그를 전달하세요. 버전은 SemVer 형식을 따라야 합니다.

client = MlflowClient()
model_name = '<귀하의_모델_이름>'
version = '<귀하의_버전>'
tags = { "gitlab.version": version }
client.create_model_version(model_name, version, description=description, tags=tags)

참고

  • run_id 인자는 무시됩니다. 모든 모델 버전은 후보/실행으로 작동합니다. 실행에서 모델 버전을 생성하는 것은 아직 지원되지 않습니다.
  • source 인자는 무시됩니다. GitLab은 모델 버전 파일에 대한 패키지 위치를 만듭니다.
  • run_link 인자는 무시됩니다.
  • await_creation_for 인자는 무시됩니다.

모델 업데이트

client = MlflowClient()
model_name = '<귀하의_모델_이름>'
version = '<귀하의_버전>'
description = '새 설명'
client.update_model_version(model_name, version, description=description)

모델 버전 가져오기

client = MlflowClient()
model_name = '<귀하의_모델_이름>'
version = '<귀하의_버전>'
client.get_model_version(model_name, version)

모델의 최신 버전 가져오기

client = MlflowClient()
model_name = '<귀하의_모델_이름>'
client.get_latest_versions(model_name)

참고

  • stages 인자는 무시됩니다.
  • 버전은 마지막으로 생성된 순서대로 정렬됩니다.

매개변수와 메트릭을 모델 버전에 로깅

모든 모델 버전은 또한 후보/실행이므로 사용자는 매개변수와 메트릭을 기록할 수 있습니다. 실행 ID는 GitLab의 모델 버전 페이지에서 찾을 수도 있고 MLflow 클라이언트를 사용하여 찾을 수도 있습니다:

client = MlflowClient()
model_name = '<귀하의_모델_이름>'
version = '<귀하의_버전>'
model_version = client.get_model_version(model_name, version)
run_id = model_version.run_id

# 당신의 훈련 코드

client.log_metric(run_id, '<메트릭_이름>', '<메트릭_값>')
client.log_param(run_id, '<매개변수_이름>', '<매개변수_값>')
client.log_batch(run_id, metric_list, param_list, tag_list)

모델 버전에 아티팩트 로깅

GitLab은 MLflow 클라이언트에서 파일을 업로드하기 위해 사용할 수 있는 패키지를 생성합니다.

client = MlflowClient()
model_name = '<your_model_name>'
version = '<your_version>'
model_version = client.get_model_version(model_name, version)
run_id = model_version.run_id

# 여러분의 훈련 코드

client.log_artifact(run_id, '<local/path/to/file.txt>', artifact_path="")
client.log_figure(run_id, figure, artifact_file="my_plot.png")
client.log_dict(run_id, my_dict, artifact_file="my_dict.json")
client.log_image(run_id, image, artifact_file="image.png")

그런 다음 아티팩트는 https/<your project>/-/ml/models/<model_id>/versions/<version_id> 아래에서 이용할 수 있습니다.

모델 버전을 CI/CD 작업에 연결

후보자와 유사하게, 모델 버전을 CI/CD 작업에 연결하는 것도 가능합니다.

client = MlflowClient()
model_name = '<your_model_name>'
version = '<your_version>'
model_version = client.get_model_version(model_name, version)
run_id = model_version.run_id

# 여러분의 훈련 코드

if os.getenv('GITLAB_CI'):
    client.set_tag(model_version.run_id, 'gitlab.CI_JOB_ID', os.getenv('CI_JOB_ID'))

지원되는 MLflow 클라이언트 메서드 및 주의사항

GitLab은 MLflow 클라이언트에서 이러한 메서드를 지원합니다. 다른 메서드는 지원될 수 있지만 테스트되지는 않았습니다. 더 많은 정보는 MLflow 문서에서 찾을 수 있습니다. 아래 메서드들의 MlflowClient 상응편도 같은 주의사항으로 지원됩니다.

메서드 지원 여부 추가된 버전 Comments
get_experiment 15.11  
get_experiment_by_name 15.11  
set_experiment 15.11  
get_run 15.11  
start_run 15.11 (16.3) 이름이 제공되지 않으면 후보자는 임의의 닉네임을 받습니다.
search_runs 15.11 (16.4) experiment_ids는 오름차순 또는 메트릭과 함께 단일 실험 ID만 지원합니다.
log_artifact 주의사항으로 예 15.11 (15.11) artifact_path는 비어 있어야 합니다. 디렉터리를 지원하지 않습니다.
log_artifacts 주의사항으로 예 15.11 (15.11) artifact_path는 비어 있어야 합니다. 디렉터리를 지원하지 않습니다.
log_batch 15.11  
log_metric 15.11  
log_metrics 15.11  
log_param 15.11  
log_params 15.11  
log_figure 15.11  
log_image 15.11  
log_text 주의사항으로 예 15.11 (15.11) 디렉터리를 지원하지 않습니다.
log_dict 주의사항으로 예 15.11 (15.11) 디렉터리를 지원하지 않습니다.
set_tag 15.11  
set_tags 15.11  
set_terminated 15.11  
end_run 15.11  
update_run 15.11  
log_model 부분적으로 예 15.11 (15.11) 아티팩트는 저장하지만 모델 데이터는 저장하지 않습니다. artifact_path는 비어 있어야 합니다.

다른 MLflowClient 메서드:

메서드 지원 여부 추가된 버전 Comments
create_registered_model 주의사항으로 예 16.8 참고 사항
get_registered_model 16.8  
delete_registered_model 16.8  
update_registered_model 16.8  
create_model_version 주의사항으로 예 16.8 참고 사항
get_model_version 16.8  
get_latest_versions 주의사항으로 예 16.8 참고 사항
update_model_version 16.8  
create_registered_model 16.8  
create_registered_model 16.8  

제한 사항

  • GitLab이 지원하는 API는 MLflow 버전 2.7.1에서 정의된 것입니다.
  • 위에 나열되지 않은 MLflow 클라이언트 메서드는 지원되지 않습니다.
  • 실험 및 실행을 생성하는 동안 ExperimentTags가 저장되지만 표시되지는 않습니다.