Skip to content

Conversation

@92HyungjunOh
Copy link

@92HyungjunOh 92HyungjunOh commented Sep 23, 2025

What does this PR do?

moreh pipeline 과 rope cache 기능을 추가함.

  1. Pipeline을 사용하기 위해서는 아래의 코드가 python 레벨에서 호출이 되어야함.
hidden_states = torch.moreh.pipeline_assign(hidden_states)

이를 위해 해당 코드를 추가함.

  1. Mistral의 Rope를 cache 버전을 사용하도록 함.
    기존 MistralRotaryEmbedding 에서는 forward 마다 매번 cos, sin tensor를 타켓 하드웨어(GPU, NPU) 위에서 연산하였음.
    이 값을 계산하기위해서는 hidden의 dtype과 shape 정보만 있으면 미리 계산을 해두는 최적화를 적용 할 수 있음.
    (진짜 목적은 cache 버전을 사용하지 않으면 MAF의 Pipeline 실행이 안됨)

참고 코드.

구버전 HuggingFace의 llama Code.
https://github.com/huggingface/transformers/blob/9c804f7ec42c94289ce52eaa84eed32f770311d7/src/transformers/models/deprecated/open_llama/modeling_open_llama.py#L109

MAF의 rope 코드 (CPU에서 미리 연산을 수행해둠)
https://github.com/moreh-dev/framework/blob/df54f28ce96ff43dce4c0b40a0aeb7bff7fd6b0c/IR/driver/pytorch/torch/moreh_ops/rotary_embedding.py#L35

아래는 MAF에서 호출하는 예제 입니다.
https://github.com/moreh-dev/framework/pull/8819

sh 파일은 안올려놨는데 크게 다를건 없고 모델이름을 gpt2-small-moreh 이렇게 실행하면 됩니다.

json 예제

{
  "attention_dropout": 0.0,
  "bos_token_id": 1,
  "eos_token_id": 2,
  "head_dim": null,
  "hidden_act": "silu",
  "hidden_size": 2048,
  "initializer_range": 0.02,
  "intermediate_size": 7168,
  "max_position_embeddings": 131072,
  "model_type": "mistral-moreh",
  "num_attention_heads": 16,
  "num_hidden_layers": 7,
  "num_key_value_heads": 8,
  "pad_token_id": 2,
  "rms_norm_eps": 1e-06,
  "rope_theta": 10000.0,
  "sliding_window": null,
  "tie_word_embeddings": false,
  "transformers_version": "4.42.4",
  "use_cache": true,
  "vocab_size": 32000,
  "moreh_config": {
    "pipeline_layers": [3],
    "rope_cache": true
  }
}

@92HyungjunOh 92HyungjunOh marked this pull request as ready for review September 23, 2025 12:16
@92HyungjunOh 92HyungjunOh merged commit b6f47b1 into v4.42.4-moreh Sep 23, 2025
3 checks passed
@92HyungjunOh 92HyungjunOh deleted the MAF-18836-GPT_MISTAL_PP_ROPE branch September 23, 2025 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants