Skip to content

Add serialization support for AOPerModuleConfig #2186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions test/quantization/test_config_serialization.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD 3-Clause license found in the
# LICENSE file in the root directory of this source tree.

import json
import os
import tempfile
Expand All @@ -14,6 +20,7 @@
config_to_dict,
)
from torchao.quantization.quant_api import (
AOPerModuleConfig,
Float8DynamicActivationFloat8WeightConfig,
Float8WeightOnlyConfig,
FPXWeightOnlyConfig,
Expand Down Expand Up @@ -63,6 +70,14 @@
# Sparsity configs
SemiSparseWeightConfig(),
BlockSparseWeightConfig(blocksize=128),
AOPerModuleConfig({}),
AOPerModuleConfig({"_default": Int4WeightOnlyConfig(), "linear1": None}),
AOPerModuleConfig(
{
"linear1": Int4WeightOnlyConfig(),
"linear2": Int8DynamicActivationInt4WeightConfig(),
}
),
]


Expand Down
8 changes: 8 additions & 0 deletions torchao/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ def config_from_dict(data: Dict[str, Any]) -> AOBaseConfig:
else item
for item in value
]
elif isinstance(value, dict):
# Handle dicts of possible configs
processed_data[key] = {
k: config_from_dict(v)
if isinstance(v, dict) and "_type" in v and "_data" in v
else v
for k, v in value.items()
}
else:
processed_data[key] = value

Expand Down
Loading