Skip to content

Commit

Permalink
Feat (mx): automatic group_dim in layerwise quant
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuseppe5 committed Aug 28, 2024
1 parent 02e7741 commit b9ba558
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/brevitas/quant/solver/act.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright (C) 2023, Advanced Micro Devices, Inc. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause

from warnings import warn

import torch
from torch import nn
from torch import Tensor
Expand Down Expand Up @@ -111,6 +113,33 @@ def scaling_shape(scaling_per_output):
elif scaling_per_output == ScalingPerOutputType.TENSOR:
return SCALAR_SHAPE

@value
def group_dim(module=None, group_size=None):
# Avoid circular import
from brevitas.nn.quant_layer import QuantWeightBiasInputOutputLayer

if group_size is not None and module is not None:
if isinstance(module, QuantWeightBiasInputOutputLayer):
if isinstance(module, nn.Linear):
return -1
elif isinstance(module,
(nn.Conv1d,
nn.Conv2d,
nn.Conv3d,
nn.ConvTranspose1d,
nn.ConvTranspose2d,
nn.ConvTranspose3d)):
warn(
"Group dim is being selected assuming batched input. Using unbatched input will fail and requires manually specification of group_dim"
)
# We are assuming batched input
return 1
else:
raise RuntimeError("Cannot determine automatically group_dim. Please specify")
else:
raise RuntimeError(
f"Cannot determine automatically group_dim for {type(module)}. Please specify")


class SolveActScalingPerOutputChannelShape(ExtendedInjector):

Expand Down

0 comments on commit b9ba558

Please sign in to comment.