You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
在当时,我们并没有专门的 AI Infra 工程师,我们的算法工程师就用他们自己的方式在内存中切换模型。几天以后,我们的代码仓库里面到处都是 this.2 CPU 和 that.2 CUDA。这种方式的上限也就到这里了,而且它因为算法工程师需要处理这种细节,会打断他们的心流。这些能不能变得完全透明呢?毕竟是 Python,在 Python 里面让东西变得完全透明,这还是能做到的。
12
+
13
+
怎么定义“完全透明”?
14
+
从算法工程师的视角来看的话,其实挺清晰的:除非我没有办法了,我不想关心核心算法之外的烂糟事情导致的运行时性能问题,我完全不想关心模型 weight 的 swap in 和 swap out。
Copy file name to clipboardExpand all lines: blog.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ The library also made an unexpected impact, discussed in the ending.
10
10
11
11
It all begins 2 years ago, when we shipped our first try of lowpoly generation mode. The lowpoly mode did not go well, it emits poor results from today's perspective, but we paid a lot for it -- a dedicated GPU only processes single digit tasks per day. It has fine-tuned weights, big enough to drive all other model weights out of VRAM. Worse, we have maybe 3 such models (can't remember the exact number), they constituted a significant part of our inference infra, made a quite unforgiving efficiency ratio. And no, we can't naively load the models just-in-time, it costs 30s, larger than the actual processing time.
12
12
13
-
We don't have dedicated pipeline engineers then, our algorithm devs tried their best to workaround this. Days later, our codebase was littered with `this.to('cpu')` and `that.to('cuda')`. This approach works for a while, but break the flow of our algo devs from time to time. What if things can happen automagically? It's Python, things do happen automagically in Python.
13
+
We don't have dedicated pipeline engineers then, our algorithm devs tried their best to workaround this by swap model weights in and out. Days later, our codebase was littered with `this.to('cpu')` and `that.to('cuda')`. This approach works for a while, but break the flow of our algo devs from time to time. What if things can happen automagically? It's Python, things do happen automagically in Python.
14
14
15
15
## How do you define 'automagically' ?
16
16
@@ -28,7 +28,7 @@ We skip discussing about how monkey-patching is implemented, that's a not-so-int
28
28
29
29
We use `pickle` to serialize our cache result since... we have no choice, and `torch.save` itself uses `pickle`, it's weird not to use it.
30
30
31
-
We use a client/server architecture since we don't want to invalidate our cache when process terminates. There are many subprocess calls could benefit from it.
31
+
We use a client/server architecture since we don't want to invalidate our cache when process terminates. At the same time, subprocess calls could also benefit from already loaded cache.
32
32
33
33
We assume `XXXPipeline.from_pretrained` parameters to be simple hashable things (`str` and things alike) and other models loaded by `overmind` (explained later).
34
34
@@ -38,11 +38,11 @@ The name `overmind` is borrowed from Starcraft, as you may have guessed.
38
38
39
39
We can't naively save `pickle.loads` result in memory and call it a day. After all, on a warmed up scenario, Linux page cache did its job caching on-disk models and we can still see a loading time measured in tens of seconds.
40
40
41
-
The inefficiency comes from memory copying. In Python, even creating millions of objects would cost no more than several hundred ms. However, for a memory copy of 10GiB, it would cost half a second. We must avoid memory copy as much as possible.
41
+
The inefficiency comes from memory copying. In Python, even creating millions of objects would cost no more than a hundred ms. However, for a memory copy of 10GiB, it would cost half a second. We must avoid memory copy as much as possible.
42
42
43
43
Fortunately, most of the big memory chunks are Torch tensors, we can safely address only them and ignore the rest.
44
44
45
-
Actually, I got the knowledge of the internal structure of a Torch tensor in the reduction code while researching the tensor sharing mechanism:
45
+
I got the knowledge of the internal structure of a Torch tensor in the reduction code while researching the tensor sharing mechanism:
46
46
47
47
```python
48
48
# Copied from torch.multiprocessing.reductions, most of the code is removed
0 commit comments