Skip to content

Commit

Permalink
Apply isort and black reformatting
Browse files Browse the repository at this point in the history
Signed-off-by: akoumpa <[email protected]>
  • Loading branch information
akoumpa committed Oct 30, 2024
1 parent 227a693 commit 4731c75
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions nemo/collections/llm/gpt/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def torch_dtype_from_mcore_config(config: TransformerConfig):
else:
return torch.float


@dataclass
class GPTConfig(TransformerConfig, io.IOMixin):
# From megatron.core.models.gpt.gpt_model.GPTModel
Expand Down
5 changes: 2 additions & 3 deletions nemo/collections/llm/gpt/model/chatglm.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,14 @@ def config(self) -> "AutoConfig":
)



@io.state_transform(
source_key="embedding.word_embeddings.weight",
target_key="transformer.embedding.word_embeddings.weight",
)
def _export_embedding(ctx: io.TransformCTX, embedding):
megatron_config = ctx.target.config
# prune padding.
return embedding[:megatron_config.vocab_size, :]
return embedding[: megatron_config.vocab_size, :]


@io.state_transform(
Expand All @@ -216,7 +215,7 @@ def _export_embedding(ctx: io.TransformCTX, embedding):
def _export_head(ctx: io.TransformCTX, embedding):
megatron_config = ctx.target.config
# prune padding.
return embedding[:megatron_config.vocab_size, :]
return embedding[: megatron_config.vocab_size, :]


@io.state_transform(
Expand Down
11 changes: 8 additions & 3 deletions nemo/collections/llm/gpt/model/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,12 @@ def convert_state(self, source, target):
"decoder.final_layernorm.weight": "model.norm.weight",
}

return io.apply_transforms(source, target, mapping=mapping, transforms=[_export_qkv, _export_linear_fc1, _export_embedding, _export_head])
return io.apply_transforms(
source,
target,
mapping=mapping,
transforms=[_export_qkv, _export_linear_fc1, _export_embedding, _export_head],
)

@property
def tokenizer(self):
Expand Down Expand Up @@ -431,7 +436,7 @@ def _export_qkv(ctx: io.TransformCTX, linear_qkv):
def _export_embedding(ctx: io.TransformCTX, embedding):
megatron_config = ctx.target.config
# prune padding.
return embedding[:megatron_config.vocab_size, :]
return embedding[: megatron_config.vocab_size, :]


@io.state_transform(
Expand All @@ -441,7 +446,7 @@ def _export_embedding(ctx: io.TransformCTX, embedding):
def _export_head(ctx: io.TransformCTX, embedding):
megatron_config = ctx.target.config
# prune padding.
return embedding[:megatron_config.vocab_size, :]
return embedding[: megatron_config.vocab_size, :]


@io.state_transform(
Expand Down
7 changes: 6 additions & 1 deletion nemo/collections/llm/gpt/model/mistral.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,12 @@ def convert_state(self, source, target):
"decoder.final_layernorm.weight": "model.norm.weight",
}

return io.apply_transforms(source, target, mapping=mapping, transforms=[_export_qkv, _export_linear_fc1, _export_embedding, _export_head])
return io.apply_transforms(
source,
target,
mapping=mapping,
transforms=[_export_qkv, _export_linear_fc1, _export_embedding, _export_head],
)

@property
def tokenizer(self):
Expand Down
7 changes: 6 additions & 1 deletion nemo/collections/llm/gpt/model/mixtral.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,12 @@ def convert_state(self, source, target):
"decoder.final_layernorm.weight": "model.norm.weight",
}

return io.apply_transforms(source, target, mapping=mapping, transforms=[_export_qkv, _export_moe_w1_w3, _export_embedding, _export_head])
return io.apply_transforms(
source,
target,
mapping=mapping,
transforms=[_export_qkv, _export_moe_w1_w3, _export_embedding, _export_head],
)

@property
def tokenizer(self):
Expand Down
4 changes: 3 additions & 1 deletion nemo/collections/llm/gpt/model/nemotron.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ def convert_state(self, source, target):
"decoder.final_layernorm.bias": "model.norm.bias",
}

return io.apply_transforms(source, target, mapping=mapping, transforms=[_export_qkv, _export_embedding, _export_head])
return io.apply_transforms(
source, target, mapping=mapping, transforms=[_export_qkv, _export_embedding, _export_head]
)

@property
def tokenizer(self):
Expand Down
5 changes: 4 additions & 1 deletion nemo/collections/llm/gpt/model/qwen2.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ def convert_state(self, source, target):
}

return io.apply_transforms(
source, target, mapping=mapping, transforms=[_export_qkv, _export_qkv_bias, _export_linear_fc1, _export_embedding, _export_head]
source,
target,
mapping=mapping,
transforms=[_export_qkv, _export_qkv_bias, _export_linear_fc1, _export_embedding, _export_head],
)

@property
Expand Down
6 changes: 3 additions & 3 deletions nemo/collections/llm/gpt/model/starcoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
from pathlib import Path
from typing import TYPE_CHECKING, Annotated, Callable, Optional

import torch
import torch.nn.functional as F
from torch import nn
import torch

from nemo.collections.llm.gpt.model.base import GPTConfig, GPTModel, torch_dtype_from_mcore_config
from nemo.collections.llm.utils import Config
Expand Down Expand Up @@ -233,7 +233,7 @@ def config(self) -> "HFStarcoderConfig":
def _export_embedding(ctx: io.TransformCTX, embedding):
megatron_config = ctx.target.config
# prune padding.
return embedding[:megatron_config.vocab_size, :]
return embedding[: megatron_config.vocab_size, :]


@io.state_transform(
Expand All @@ -243,4 +243,4 @@ def _export_embedding(ctx: io.TransformCTX, embedding):
def _export_head(ctx: io.TransformCTX, embedding):
megatron_config = ctx.target.config
# prune padding.
return embedding[:megatron_config.vocab_size, :]
return embedding[: megatron_config.vocab_size, :]
1 change: 1 addition & 0 deletions nemo/lightning/io/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def load_context(path: Path, subpath: Optional[str] = None, build: bool = True):
path = path / 'context'
return load(path, output_type=TrainerContext, subpath=subpath, build=build)


def model_importer(target: Type[ConnectorMixin], ext: str) -> Callable[[Type[ConnT]], Type[ConnT]]:
"""
Registers an importer for a model with a specified file extension and an optional default path.
Expand Down
3 changes: 2 additions & 1 deletion nemo/lightning/io/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ def nemo_load(

model = load_context(path).model
_trainer = trainer or Trainer(
devices=1, accelerator="cpu" if cpu else "gpu",
devices=1,
accelerator="cpu" if cpu else "gpu",
strategy=MegatronStrategy(ddp="pytorch", setup_optimizers=False),
)

Expand Down

0 comments on commit 4731c75

Please sign in to comment.