Skip to content

Instantly share code, notes, and snippets.

@wasertech
Last active June 1, 2022 18:15
Show Gist options
  • Save wasertech/99518a1b43c1cf3be29c9838eec9b763 to your computer and use it in GitHub Desktop.
Save wasertech/99518a1b43c1cf3be29c9838eec9b763 to your computer and use it in GitHub Desktop.
Find all possible batch sizes I could use for STT if I have this amount of samples.
#!/usr/bin/env python
"""
Compute batch sizes by samples.
Find all possible batch sizes I could use for STT if I have this amount of samples.
Copyright (c) 2022, Danny Waser. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
def list_batch_sizes(n_samples: int):
batch_size_list = [1, n_samples] if n_samples > 1 else [1,] if n_samples == 1 else []
for r in range(2, int(s/2)):
d = float( s / r )
if d.is_integer():
if r not in batch_size_list:
batch_size_list.append(r)
if int(d) not in batch_size_list:
batch_size_list.append(int(d))
number_of_samples = input("How much samples to batch?: ")
try:
s = int(number_of_samples)
except ValueError as e:
print("ERROR: Number of samples must be integer.")
raise e
print(f"Finding all possible batch sizes for {str(s)} samples.")
print("Possible batch sizes are:")
print(list_batch_sizes(s))
@wasertech
Copy link
Author

This implementation scales poorly with ridiculously high samples.

How much samples to batch?: 1120566416
Finding all possible batch sizes for 1120566416 samples.
Possible batch sizes are:
[1, 2, 4, 8, 16, 2659, 5318, 10636, 21272, 26339, 42544, 52678, 105356, 210712, 421424, 70035401, 140070802, 280141604, 560283208, 1120566416]

took 3m 47s

I think we can use complex numbers to call a generating function somewhere but I'm not there yet.

@wasertech
Copy link
Author

wasertech commented Jun 1, 2022

This implementation scales poorly with ridiculously high samples.

How much samples to batch?: 1120566416
Finding all possible batch sizes for 1120566416 samples.
Possible batch sizes are:
[1, 2, 4, 8, 16, 2659, 5318, 10636, 21272, 26339, 42544, 52678, 105356, 210712, 421424, 70035401, 140070802, 280141604, 560283208, 1120566416]

took 3m 47s

Well I made it twice as fast with some clever tricks but it's still slow.

❯ ./compute_batch_sizes.py
How much samples to batch?: 1120566416
Finding all possible batch sizes for 1120566416 samples.
Possible batch sizes are:
[1, 2, 4, 8, 16, 2659, 5318, 10636, 21272, 26339, 42544, 52678, 105356, 210712, 421424, 70035401, 140070802, 280141604, 560283208, 1120566416]

took 1m 54s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment