Skip to content

Commit

Permalink
Feat (core): add permute_dims to all reshape fns (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
volcacius authored Jul 17, 2023
1 parent 2622a2b commit ae7da18
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/brevitas/core/function_wrapper/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class OverOutputChannelView(brevitas.jit.ScriptModule):
torch.Size([16, 200])
"""

def __init__(self, permute_dims: Optional[Tuple[int, ...]]) -> None:
def __init__(self, permute_dims: Optional[Tuple[int, ...]] = None) -> None:
super(OverOutputChannelView, self).__init__()
if permute_dims is not None:
self.permute_impl = PermuteDims(permute_dims)
Expand All @@ -86,13 +86,18 @@ class OverBatchOverTensorView(brevitas.jit.ScriptModule):
torch.Size([8, 250])
"""

def __init__(self) -> None:
def __init__(self, permute_dims: Optional[Tuple[int, ...]] = None) -> None:
super(OverBatchOverTensorView, self).__init__()
if permute_dims is not None:
self.permute_impl = PermuteDims(permute_dims)
else:
self.permute_impl = Identity()

@brevitas.jit.script_method
def forward(self, x: torch.Tensor):
shape = over_batch_over_tensor(x)
return x.reshape(shape)
y = self.permute_impl(x)
shape = over_batch_over_tensor(y)
return y.reshape(shape)


class OverBatchOverOutputChannelView(brevitas.jit.ScriptModule):
Expand All @@ -107,13 +112,18 @@ class OverBatchOverOutputChannelView(brevitas.jit.ScriptModule):
torch.Size([8, 10, 25])
"""

def __init__(self) -> None:
def __init__(self, permute_dims: Optional[Tuple[int, ...]] = None) -> None:
super(OverBatchOverOutputChannelView, self).__init__()
if permute_dims is not None:
self.permute_impl = PermuteDims(permute_dims)
else:
self.permute_impl = Identity()

@brevitas.jit.script_method
def forward(self, x: torch.Tensor):
shape = over_batch_over_output_channels(x)
return x.reshape(shape)
y = self.permute_impl(x)
shape = over_batch_over_output_channels(y)
return y.reshape(shape)


class StatsInputViewShapeImpl(object):
Expand Down

0 comments on commit ae7da18

Please sign in to comment.