Hugging Face 설정 클래스

Hugging Face 설정 클래스 (Configuration)

Transformers에서 설정(Configuration)은 모델의 구조를 정의하는 핵심 요소예요. 베이스 클래스 PreTrainedConfig는 로컬 파일·디렉터리, 또는 라이브러리가 제공하는 사전학습 모델 설정(허깅페이스 AWS S3에서 다운로드)을 로드·저장하는 공통 메서드를 구현해요.

각 파생 설정 클래스는 모델 특유의 속성을 구현해요. 모든 설정 클래스에 공통으로 있는 속성은 hidden_size, num_attention_heads, num_hidden_layers이고, 텍스트 모델은 여기에 vocab_size가 추가돼요.

출처: Hugging Face Configuration 문서

PreTrainedConfig

PreTrainedConfig는 모든 설정 클래스의 베이스예요. 여러 모델 설정에 공통인 파라미터와 설정 로드·다운로드·저장 메서드를 처리해요. 설정 파일을 로드해서 모델 초기화에 사용하더라도 모델 가중치는 로드되지 않는다는 점을 기억해 두세요. 설정은 모델의 구성에만 영향을 줘요.

주요 파라미터

( transformers_version: str | None = None, architectures: list[str] | None = None,
 output_hidden_states: bool | None = False, return_dict: bool | None = True,
 dtype: typing.Union[str, ForwardRef('torch.dtype'), NoneType] = None,
 chunk_size_feed_forward: int = 0, is_encoder_decoder: bool = False,
 id2label: dict[int, str] | dict[str, str] | None = None,
 label2id: dict[str, int] | dict[str, str] | None = None,
 problem_type: typing.Optional[...] = None )
  • name_or_path (str, 기본 "") — PreTrainedModel.from_pretrained()에 전달된 문자열 저장
  • output_hidden_states (bool, 기본 False) — 모든 hidden-state를 반환할지
  • output_attentions (bool, 기본 False) — 모든 attention을 반환할지
  • return_dict (bool, 기본 True) — plain tuple 대신 ModelOutput 반환 여부
  • is_encoder_decoder (bool, 기본 False) — 인코더/디코더 모델 여부
  • chunk_size_feed_forward (int, 기본 0) — residual attention 블록의 feed forward 레이어 청크 크기. 0은 청크하지 않음
  • architectures (list[str], 선택) — 사전학습 가중치와 함께 사용 가능한 모델 아키텍처
  • id2label (dict[int, str], 선택) — 인덱스→라벨 매핑
  • label2id (dict[str, int], 선택) — 라벨→인덱스 매핑
  • problem_type (str, 선택) — 시퀀스 분류 모델용. "regression", "single_label_classification", "multi_label_classification" 중 하나

클래스 속성 (파생 클래스가 오버라이드)

  • model_type (str) — JSON 파일에 직렬화되고 AutoConfig에서 올바른 객체를 재생성하는 데 쓰이는 모델 타입 식별자
  • attribute_map (dict[str, str]) — 모델 특유 속성 이름을 표준화된 이름으로 매핑

공통 속성 (모든 하위 클래스 존재)

  • vocab_size (int) — 어휘의 토큰 수. 임베딩 행렬의 첫 번째 차원이기도 함 (텍스트 모달리티가 없는 ViT 같은 모델에는 없을 수 있음)
  • hidden_size (int) — 모델의 hidden size
  • num_attention_heads (int) — 멀티헤드 어텐션 레이어의 헤드 수
  • num_hidden_layers (int) — 모델의 블록 수

설정 저장·로드 메서드

  • from_pretrained() / get_config_dict() — 사전학습 체크포인트에서 설정 로드
  • from_dict(config_dict) — 파이썬 딕셔너리에서 설정 인스턴스 생성
  • from_json_file(json_file) — JSON 파일에서 설정 인스턴스 생성
  • to_dict() / to_diff_dict() — 설정 인스턴스를 딕셔너리로 직렬화 (diff는 기본값 속성 제거)
  • to_json_file(json_file_path, use_diff=True) — JSON 파일로 저장
  • to_json_string(use_diff=True) — JSON 문자열로 직렬화
  • push_to_hub(repo_id, ...) — 허브로 설정 업로드
  • update(config_dict) — 클래스 속성을 딕셔너리로 갱신

to_json_string을 예로 들어 볼게요. use_diffTrue이면 기본 PreTrainedConfig()와의 차이만 JSON 문자열로 직렬화해요.

더 알아보기