-
Notifications
You must be signed in to change notification settings - Fork 537
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into refactor-loss
- Loading branch information
Showing
8 changed files
with
201 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
# Copyright 2022 MosaicML LLM Foundry authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from llmfoundry.registry import tokenizers | ||
from llmfoundry.tokenizers.tiktoken import TiktokenTokenizerWrapper | ||
|
||
tokenizers.register('tiktoken', func=TiktokenTokenizerWrapper) | ||
|
||
__all__ = [ | ||
'TiktokenTokenizerWrapper', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Copyright 2024 MosaicML LLM Foundry authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from typing import Any, Dict, Optional | ||
|
||
import pytest | ||
from composer.models.huggingface import maybe_get_underlying_model | ||
from peft import PeftConfig, PeftModel | ||
from transformers import LlamaForCausalLM, PreTrainedModel | ||
|
||
from llmfoundry.models.hf.hf_causal_lm import ComposerHFCausalLM | ||
from llmfoundry.models.utils import init_empty_weights | ||
|
||
|
||
@pytest.mark.gpu | ||
@pytest.mark.parametrize( | ||
'peft_config', | ||
[ | ||
None, | ||
{ | ||
'peft_type': 'LORA', | ||
'task_type': 'CAUSAL_LM', | ||
'lora_alpha': 32, | ||
'r': 2, | ||
'target_modules': [ | ||
'q_proj', | ||
'k_proj', | ||
'v_proj', | ||
], | ||
}, | ||
], | ||
) | ||
def test_hf_transform(peft_config: Optional[dict]): | ||
model_cfg = { | ||
'pretrained_model_name_or_path': 'codellama/CodeLlama-7b-hf', | ||
'config_overrides': { | ||
'num_hidden_layers': 2, | ||
'hidden_size': 32, | ||
'intermediate_size': 64, | ||
}, | ||
'pretrained': False, | ||
'peft_config': peft_config, | ||
'init_device': 'meta', | ||
'tokenizer': 'codellama/CodeLlama-7b-hf', | ||
} | ||
|
||
class TransformedHFCausalLM(ComposerHFCausalLM): | ||
|
||
def transform_model(self, model: PreTrainedModel) -> PreTrainedModel: | ||
assert isinstance(model, LlamaForCausalLM) | ||
with init_empty_weights(): | ||
model.config.num_hidden_layers = 1 | ||
new_model = type(model)(model.config) | ||
return new_model | ||
|
||
def get_peft_config( | ||
self, | ||
peft_config_dict: Dict[str, Any], | ||
) -> PeftConfig: | ||
peft_config_dict['target_modules'] = ['o_proj'] | ||
return super().get_peft_config(peft_config_dict) | ||
|
||
composer_model = TransformedHFCausalLM(**model_cfg) | ||
model = composer_model.model | ||
inner_model = maybe_get_underlying_model(model) | ||
|
||
if peft_config: | ||
peft_model = composer_model.model | ||
assert isinstance(peft_model, PeftModel) | ||
|
||
target_modules = peft_model.peft_config[peft_model.active_adapter | ||
].target_modules | ||
assert list(target_modules) == ['o_proj'] | ||
|
||
assert isinstance(inner_model, LlamaForCausalLM) | ||
assert inner_model.config.num_hidden_layers == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright 2024 MosaicML LLM Foundry authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from typing import Any, Dict, Optional | ||
|
||
from transformers import PreTrainedTokenizer | ||
|
||
from llmfoundry.registry import tokenizers | ||
from llmfoundry.utils import build_tokenizer | ||
|
||
|
||
class DummyTokenizer(PreTrainedTokenizer): | ||
"""A dummy tokenizer that inherits from ``PreTrainedTokenizer``.""" | ||
|
||
def __init__( | ||
self, | ||
model_name: Optional[str] = 'dummy', | ||
**kwargs: Optional[Dict[str, Any]], | ||
): | ||
"""Dummy constructor that has no real purpose.""" | ||
super().__init__( | ||
model_name=model_name, | ||
eos_token='0', | ||
pad_token='1', | ||
**kwargs, | ||
) | ||
|
||
def get_vocab(self) -> Dict[str, int]: | ||
return {} | ||
|
||
|
||
def test_tokenizer_registry(): | ||
tokenizers.register('dummy', func=DummyTokenizer) | ||
tokenizer = build_tokenizer(tokenizer_name='dummy', tokenizer_kwargs={}) | ||
assert type(tokenizer) == DummyTokenizer |