change double for loop to list comprehension for upgrading speed
Browse files```python
import timeit
class DummyConfig:
def __init__(self, max_num_grids=10, use_1x1_grid=False, anyres=True):
self.max_num_grids = max_num_grids
self.use_1x1_grid = use_1x1_grid
self.anyres = anyres
config = DummyConfig()
def original_version():
possible_resolutions = []
if config.anyres:
assert config.max_num_grids > 0
for i in range(1, config.max_num_grids + 1):
for j in range(1, config.max_num_grids + 1):
if i == 1 and j == 1 and not config.use_1x1_grid:
continue
if i * j <= config.max_num_grids:
possible_resolutions.append([i, j])
return possible_resolutions
def list_comprehension_version():
possible_resolutions = []
if config.anyres:
assert config.max_num_grids > 0
possible_resolutions = [
[i, j]
for i in range(1, config.max_num_grids + 1)
for j in range(1, (config.max_num_grids // i) + 1)
if not (i == 1 and j == 1 and not config.use_1x1_grid)
]
return possible_resolutions
# 측정
print("original_version:", timeit.timeit(original_version, number=10000))
print("list_comprehension_version:", timeit.timeit(list_comprehension_version, number=10000))
```
```python
# original_version: 0.19971796300001188 0.10782223799999713
# list_comprehension_version: 0.15134139399999924 0.0473939370000096
```
timeit을 사용해 두 코드의 성능 비교를 했을때 list_comprehension쪽이 더 빠르며 이론적으로도 더 빠른것으로 알고 있습니다. grid 설정으로 적절한 해상도 목록을 반환하는데 시간을 줄일 수 있습니다.
- modeling_hyperclovax.py +6 -6
@@ -465,12 +465,12 @@ class HCXVisionForCausalLM(PreTrainedModel, GenerationMixin):
|
|
465 |
possible_resolutions = []
|
466 |
if config.anyres:
|
467 |
assert config.max_num_grids > 0
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
|
475 |
possible_resolutions = [
|
476 |
[ys * vision_config.image_size, xs * vision_config.image_size] for ys, xs in possible_resolutions
|
|
|
465 |
possible_resolutions = []
|
466 |
if config.anyres:
|
467 |
assert config.max_num_grids > 0
|
468 |
+
possible_resolutions = [
|
469 |
+
[i, j]
|
470 |
+
for i in range(1, config.max_num_grids + 1)
|
471 |
+
for j in range(1, (config.max_num_grids // i) + 1)
|
472 |
+
if not (i == 1 and j == 1 and not config.use_1x1_grid)
|
473 |
+
]
|
474 |
|
475 |
possible_resolutions = [
|
476 |
[ys * vision_config.image_size, xs * vision_config.image_size] for ys, xs in possible_resolutions
|