Unverified Commit ffd426ee authored by Joshua Lochner's avatar Joshua Lochner Committed by GitHub
Browse files

[CLAP] Replace hard-coded batch size to enable dynamic ONNX export (#27790)

* [CLAP] Replace hard-coded batch size to enable dynamic ONNX export

* Add back docstring
parent 80377eb0
...@@ -92,6 +92,7 @@ def window_partition(hidden_states, window_size): ...@@ -92,6 +92,7 @@ def window_partition(hidden_states, window_size):
# Adapted from https://github.com/LAION-AI/CLAP/blob/6ad05a971ba0622f6acee8c41993e0d02bbed639/src/open_clip/htsat.py#L263 # Adapted from https://github.com/LAION-AI/CLAP/blob/6ad05a971ba0622f6acee8c41993e0d02bbed639/src/open_clip/htsat.py#L263
def window_reverse(windows, window_size, height, width): def window_reverse(windows, window_size, height, width):
""" """
Merges windows to produce higher resolution features.
Args: Args:
windows (`torch.FloatTensor` of shape `(num_windows * batch_size, window_size, window_size, num_channels)`): windows (`torch.FloatTensor` of shape `(num_windows * batch_size, window_size, window_size, num_channels)`):
Input windows Input windows
...@@ -102,11 +103,10 @@ def window_reverse(windows, window_size, height, width): ...@@ -102,11 +103,10 @@ def window_reverse(windows, window_size, height, width):
width (`int`): width (`int`):
Width of the resized audio Width of the resized audio
""" """
batch_size = int(windows.shape[0] / (height * width / window_size / window_size)) num_channels = windows.shape[-1]
windows = windows.view(-1, height // window_size, width // window_size, window_size, window_size, num_channels)
hidden_states = windows.view(batch_size, height // window_size, width // window_size, window_size, window_size, -1) windows = windows.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, height, width, num_channels)
hidden_states = hidden_states.permute(0, 1, 3, 2, 4, 5).contiguous().view(batch_size, height, width, -1) return windows
return hidden_states
# Copied from transformers.models.roberta.modeling_roberta.create_position_ids_from_input_ids # Copied from transformers.models.roberta.modeling_roberta.create_position_ids_from_input_ids
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment