2020-02-11 03:55:03 +08:00
|
|
|
""" Layer/Module Helpers
|
2020-02-10 06:46:28 +08:00
|
|
|
|
2020-07-28 04:44:56 +08:00
|
|
|
Hacked together by / Copyright 2020 Ross Wightman
|
2020-02-10 06:46:28 +08:00
|
|
|
"""
|
|
|
|
from itertools import repeat
|
2021-02-10 20:54:35 +08:00
|
|
|
import collections.abc
|
2020-02-10 06:46:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
# From PyTorch internals
|
|
|
|
def _ntuple(n):
|
|
|
|
def parse(x):
|
2022-07-29 07:18:18 +08:00
|
|
|
if isinstance(x, collections.abc.Iterable) and not isinstance(x, str):
|
2022-12-23 09:19:45 +08:00
|
|
|
return tuple(x)
|
2020-02-10 06:46:28 +08:00
|
|
|
return tuple(repeat(x, n))
|
|
|
|
return parse
|
|
|
|
|
|
|
|
|
2020-10-14 04:33:44 +08:00
|
|
|
to_1tuple = _ntuple(1)
|
|
|
|
to_2tuple = _ntuple(2)
|
|
|
|
to_3tuple = _ntuple(3)
|
|
|
|
to_4tuple = _ntuple(4)
|
|
|
|
to_ntuple = _ntuple
|
2020-02-10 06:46:28 +08:00
|
|
|
|
2020-02-11 03:55:03 +08:00
|
|
|
|
2021-05-15 06:50:00 +08:00
|
|
|
def make_divisible(v, divisor=8, min_value=None, round_limit=.9):
|
2021-01-28 14:06:57 +08:00
|
|
|
min_value = min_value or divisor
|
|
|
|
new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
|
|
|
|
# Make sure that round down does not go down by more than 10%.
|
2021-05-15 06:50:00 +08:00
|
|
|
if new_v < round_limit * v:
|
2021-01-28 14:06:57 +08:00
|
|
|
new_v += divisor
|
2021-05-29 11:47:24 +08:00
|
|
|
return new_v
|
2022-08-23 08:42:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
def extend_tuple(x, n):
|
|
|
|
# pdas a tuple to specified n by padding with last value
|
|
|
|
if not isinstance(x, (tuple, list)):
|
|
|
|
x = (x,)
|
|
|
|
else:
|
|
|
|
x = tuple(x)
|
|
|
|
pad_n = n - len(x)
|
|
|
|
if pad_n <= 0:
|
|
|
|
return x[:n]
|
|
|
|
return x + (x[-1],) * pad_n
|