Skip to content
Open
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
2c1abe0
Fixed the CICD for Diffusion
jingyu-ml Sep 10, 2025
c70c778
Merge branch 'main' into jingyux/fixed-trtexec-cicd
jingyu-ml Sep 10, 2025
0b18e50
Update req for diffusers
jingyu-ml Sep 10, 2025
adcc046
Add megatron lora support
jingyu-ml Sep 15, 2025
2cf979f
Merge branch 'main' into jingyux/megatron-lora
jingyu-ml Sep 15, 2025
ec3c17b
Merge branch 'main' into jingyux/megatron-lora
jingyu-ml Sep 16, 2025
e226673
Update
jingyu-ml Sep 17, 2025
31e8875
Merge branch 'main' into jingyux/megatron-lora
jingyu-ml Sep 17, 2025
62c8685
Add more functions
jingyu-ml Sep 18, 2025
e9a78b3
Merge branch 'main' into jingyux/megatron-lora
jingyu-ml Sep 18, 2025
744eef8
Update: to support quantize the lora layers
jingyu-ml Sep 18, 2025
206e44f
Update test cases
jingyu-ml Sep 18, 2025
935f524
Merge branch 'main' into jingyux/megatron-lora
jingyu-ml Sep 18, 2025
d50bc78
Clean up code
jingyu-ml Sep 18, 2025
c22e95d
Clean up code, more
jingyu-ml Sep 18, 2025
c0b955a
more clean up
jingyu-ml Sep 18, 2025
8eeb8e5
Update more, config + conversation
jingyu-ml Sep 19, 2025
cf91aba
Update disable/enable logic
jingyu-ml Sep 19, 2025
a6e19ea
Update restore logic
jingyu-ml Sep 19, 2025
98f2314
Update sharded axis
jingyu-ml Sep 19, 2025
381bf4e
Add disable_adapters enable_adpaters support, removed some codes
jingyu-ml Sep 19, 2025
2617952
Update test cases / update namings
jingyu-ml Sep 20, 2025
f32d6ed
Add test cases
jingyu-ml Sep 20, 2025
1b0f424
Update lora implementations
jingyu-ml Sep 26, 2025
dfd2810
Force to check the mcore model
jingyu-ml Sep 26, 2025
3823c15
minors on the __init__
jingyu-ml Sep 26, 2025
7cf4ac5
More fix to the init
jingyu-ml Sep 26, 2025
3c08dfc
More fix to the init
jingyu-ml Sep 26, 2025
787f6ff
Update the grad for loras
jingyu-ml Sep 26, 2025
a43b6c4
Update
jingyu-ml Sep 26, 2025
9311e04
Merge branch 'main' into jingyux/megatron-lora
jingyu-ml Sep 26, 2025
1d8ba41
minor on the test case
jingyu-ml Sep 26, 2025
d4b8a28
Update the codeowners
jingyu-ml Sep 26, 2025
ffef564
Merge branch 'main' into jingyux/megatron-lora
jingyu-ml Sep 30, 2025
ce6bead
Remove the import error check
jingyu-ml Sep 30, 2025
81f8d06
Update init functions
jingyu-ml Sep 30, 2025
6df1954
Update the comment
jingyu-ml Sep 30, 2025
1bb3985
Some minor updates
jingyu-ml Sep 30, 2025
98ef9fb
Update: removed the permodule restore and state
jingyu-ml Sep 30, 2025
f98711e
Some minor updates
jingyu-ml Sep 30, 2025
8df12bc
Update the test case and some minor updates
jingyu-ml Sep 30, 2025
48e9ab5
Update comments for test cases
jingyu-ml Sep 30, 2025
5318241
Update comments for test cases
jingyu-ml Sep 30, 2025
49a1e65
Merge branch 'main' into jingyux/megatron-lora
jingyu-ml Sep 30, 2025
8c31821
Update test case
jingyu-ml Sep 30, 2025
024d57c
Update on the test case
jingyu-ml Sep 30, 2025
98e4e73
Merge branch 'main' into jingyux/megatron-lora
jingyu-ml Oct 1, 2025
22f55e2
Update Changelog
jingyu-ml Oct 1, 2025
03807e2
Update Changelog
jingyu-ml Oct 1, 2025
9b96fea
Update test case for quantize / lora
jingyu-ml Oct 1, 2025
5030b43
Update the grad and some test cases
jingyu-ml Oct 2, 2025
0b310fb
update init functions
jingyu-ml Oct 4, 2025
0b202b9
Merge branch 'main' into jingyux/megatron-lora
jingyu-ml Oct 4, 2025
82dc269
Change name
jingyu-ml Oct 4, 2025
fad4982
minor
jingyu-ml Oct 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ modelopt/torch/distill @NVIDIA/modelopt-torch-distill-codeowners
modelopt/torch/export @NVIDIA/modelopt-torch-export-codeowners
modelopt/torch/nas @NVIDIA/modelopt-torch-nas-prune-codeowners
modelopt/torch/opt @NVIDIA/modelopt-torch-opt-codeowners
modelopt/torch/peft @NVIDIA/modelopt-torch-peft-codeowners
modelopt/torch/prune @NVIDIA/modelopt-torch-nas-prune-codeowners
modelopt/torch/quantization @NVIDIA/modelopt-torch-quantization-codeowners
modelopt/torch/sparsity @NVIDIA/modelopt-torch-sparsity-codeowners
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@
],
"git.alwaysSignOff": true,
"git.enableCommitSigning": true,
"python-envs.defaultEnvManager": "ms-python.python:conda",
"python-envs.defaultPackageManager": "ms-python.python:conda",
"python-envs.pythonProjects": [],
}
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Model Optimizer Changelog (Linux)
**New Features**

- Add flag ``op_types_to_exclude_fp16`` in ONNX quantization to exclude ops from being converted to FP16/BF16. Alternatively, for custom TensorRT ops, this can also be done by indicating ``'fp32'`` precision in ``trt_plugins_precision``.
- Add LoRA mode support for MCore: ``mtpf.update_model(model, LORA_CFG)``.

0.37 (2025-09-xx)
^^^^^^^^^^^^^^^^^
Expand Down
21 changes: 21 additions & 0 deletions modelopt/torch/peft/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""PEFT API subpackage for torch."""

from . import mode
from .config import *
from .conversion import *
from .convert import *
159 changes: 159 additions & 0 deletions modelopt/torch/peft/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Configuration classes for PEFT methods."""

from collections.abc import Callable

from pydantic import field_validator

from modelopt.torch.opt.config import ModeloptBaseConfig, ModeloptField

__all__ = ["ExportPEFTConfig", "PEFTAttributeConfig", "PEFTConfig"]


class PEFTAttributeConfig(ModeloptBaseConfig):
"""Configuration for PEFT adapter attributes."""

enable: bool = ModeloptField(
default=True,
title="Enable adapter",
description="If True, enables the adapter. If False, by-passes the adapter.",
)

rank: int = ModeloptField(
default=64,
title="LoRA rank",
description=(
"The rank (dimension) of the LoRA matrices. "
"Higher rank allows more expressiveness but uses more memory."
),
)

scale: float = ModeloptField(
default=1.0,
title="LoRA scaling factor",
description="Scaling factor for the LoRA output. Controls the magnitude of the adaptation.",
)

lora_a_init: str = ModeloptField(
default="kaiming_init",
title="LoRA A matrix initializer",
description="Custom initialization function for LoRA A matrix. Default to Kaiming uniform initialization.",
)

lora_b_init: str = ModeloptField(
default="zero_init",
title="LoRA B matrix initializer",
description="Custom initialization function for LoRA B matrix. Default to zero initialization.",
)

@field_validator("lora_a_init", "lora_b_init")
@classmethod
def validate_init_method(cls, v):
"""Validate initialization method is supported."""
valid_methods = {"kaiming_init", "zero_init"}
if v not in valid_methods:
raise ValueError(
f"Invalid initialization method: {v}. Supported methods: {', '.join(valid_methods)}"
)
return v

@field_validator("rank")
@classmethod
def validate_rank(cls, v):
"""Validate rank is positive."""
if v < 1:
raise ValueError("rank must be a positive integer")
return v

@field_validator("scale")
@classmethod
def validate_scale(cls, v):
"""Validate scale is positive."""
if v <= 0:
raise ValueError("scale must be a positive number")
return v


# Type alias for adapter configuration
PEFTAdapterCfgType = dict[str | Callable, PEFTAttributeConfig | dict]


class PEFTConfig(ModeloptBaseConfig):
"""Default configuration for ``peft`` mode."""

adapter_name: str = ModeloptField(
default="default",
title="Adapter name",
description="Name of the adapter to create or update.",
validate_default=True,
)

adapter_cfg: PEFTAdapterCfgType = ModeloptField(
default={"*": {"rank": 64}},
title="Adapter configuration",
description="Configuration for adapters. Maps module patterns to PEFTAttributeConfig or dict.",
validate_default=True,
)

adapter_type: str = ModeloptField(
default="lora",
title="Adapter type",
description="Type of PEFT adapter to use. Currently only 'lora' is supported.",
validate_default=True,
)

freeze_base_model: bool = ModeloptField(
default=True,
title="Freeze base weights during training",
description="Whether to freeze the base model weights; in most cases, this should be set to True.",
validate_default=True,
)

freeze_lora_weights: bool = ModeloptField(
default=False,
title="Freeze lora weights during training",
description="Whether to freeze the lora model weights; in most cases, this should be set to False.",
validate_default=True,
)

@field_validator("adapter_type")
@classmethod
def validate_adapter_type(cls, v):
"""Validate adapter type."""
if v not in ["lora"]:
raise ValueError(f"Unsupported adapter type: {v}. Only 'lora' is currently supported.")
return v

@field_validator("adapter_cfg")
@classmethod
def validate_adapter_cfg(cls, v):
"""Validate and convert adapter configurations."""
validated_cfg = {}
for key, value in v.items():
if isinstance(value, dict) and not isinstance(value, PEFTAttributeConfig):
# Convert dict to PEFTAttributeConfig to trigger validation
try:
validated_cfg[key] = PEFTAttributeConfig(**value)
except Exception as e:
raise ValueError(f"Invalid adapter configuration for '{key}': {e}")
else:
validated_cfg[key] = value
return validated_cfg


class ExportPEFTConfig(ModeloptBaseConfig):
"""An empty config."""
Loading
Loading