Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prototype_source/torchscript_freezing.py 번역 진행 v2 #788

Merged
merged 3 commits into from
Nov 26, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 25 additions & 28 deletions prototype_source/torchscript_freezing.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
"""
Model Freezing in TorchScript
TorchScript로 모델 동결하기
=============================
번역 : `김지호 <https://github.com/jiho3004/>`_

In this tutorial, we introduce the syntax for *model freezing* in TorchScript.
Freezing is the process of inlining Pytorch module parameters and attributes
values into the TorchScript internal representation. Parameter and attribute
values are treated as final values and they cannot be modified in the resulting
Frozen module.
이 튜토리얼에서는, TorchScript로 *모델 동결* 하는 문법을 소개합니다.
동결은 파이토치 모듈의 매개변수와 속성 값들을 TorchScript 내부 표현으로 인라이닝하는 과정입니다.
jih0-kim marked this conversation as resolved.
Show resolved Hide resolved
매개변수와 속성 값들은 최종 값으로 처리되며 동결된 모듈에서 수정될 수 없습니다.

Basic Syntax
기본 문법
------------
Model freezing can be invoked using API below:

모델 동결은 아래 API를 사용하여 호출할 수 있습니다:

``torch.jit.freeze(mod : ScriptModule, names : str[]) -> SciptModule``

Note the input module can either be the result of scripting or tracing.
See https://tutorials.pytorch.kr/beginner/Intro_to_TorchScript_tutorial.html
입력 모듈은 스크립팅(scripting) 혹은 추적(tracing)을 사용한 결과입니다.
`TorchScript 소개 튜토리얼 <https://tutorials.pytorch.kr/beginner/Intro_to_TorchScript_tutorial.html>`_
을 참조하세요.

Next, we demonstrate how freezing works using an example:
다음으로, 예제를 통해 동결이 어떤 방식으로 동작하는지 확인합니다:
"""

import torch, time
Expand Down Expand Up @@ -58,17 +59,15 @@ def version(self):

try:
print(fnet.conv1.bias)
# without exception handling, prints:
# 예외 처리가 없을 시 'conv1' 이라는 이름과 함께 다음을 출력합니다.
# RuntimeError: __torch__.z.___torch_mangle_3.Net does not have a field
# with name 'conv1'
except RuntimeError:
print("field 'conv1' is inlined. It does not exist in 'fnet'")

try:
fnet.version()
# without exception handling, prints:
# 예외 처리가 없을 시 'version' 이라는 이름과 함께 다음을 출력합니다.
# RuntimeError: __torch__.z.___torch_mangle_3.Net does not have a field
# with name 'version'
except RuntimeError:
print("method 'version' is not deleted in fnet. Only 'forward' is preserved")

Expand Down Expand Up @@ -108,27 +107,25 @@ def version(self):
print("Frozen - Inference time: {0:5.2f}".format(end-start), flush =True)

###############################################################
# On my machine, I measured the time:
# 개인 머신에서 시간을 측정한 결과입니다:
#
# * Scripted - Warm up time: 0.0107
# * Frozen - Warm up time: 0.0048
# * Scripted - Inference: 1.35
# * Frozen - Inference time: 1.17

###############################################################
# In our example, warm up time measures the first two runs. The frozen model
# is 50% faster than the scripted model. On some more complex models, we
# observed even higher speed up of warm up time. freezing achieves this speed up
# because it is doing some the work TorchScript has to do when the first couple
# runs are initiated.
# 이 예제에서, 워밍업 시간은 최초 두 번 실행할 때 측정합니다.
# 동결된 모델이 스크립트된 모델보다 50% 더 빠릅니다.
# 보다 복잡한 모델에서는 워밍업 시간이 더욱 빨라집니다.
# 최초 두 번의 실행을 초기화할 때 TorchScript가 해야 할 일의 일부를 동결이 하고 있기 때문에 속도 개선이 일어납니다.
#
# Inference time measures inference execution time after the model is warmed up.
# Although we observed significant variation in execution time, the
# frozen model is often about 15% faster than the scripted model. When input is larger,
# we observe a smaller speed up because the execution is dominated by tensor operations.
# 추론 시간은 모델이 워밍업되고 난 뒤, 추론 시 실행 시간을 측정합니다.
# 실행 시간에서 중요한 변화가 있었는데, 동결된 모델이 스크립트된 모델보다 약 15% 더 빠릅니다.
jih0-kim marked this conversation as resolved.
Show resolved Hide resolved
# 실행 시간은 텐서 연산에 의해 지배되기 때문에 입력의 크기가 더 커지면 속도 개선 정도는 더 작아집니다.
jih0-kim marked this conversation as resolved.
Show resolved Hide resolved

###############################################################
# Conclusion
# 결론
# -----------
# In this tutorial, we learned about model freezing. Freezing is a useful technique to
# optimize models for inference and it also can significantly reduce TorchScript warmup time.
# 이 튜토리얼에서는 모델 동결에 대해 배웠습니다.
# 동결은 추론 시 모델 최적화를 할 수 있는 유용한 기법이며 TorchScript 워밍업 시간을 크게 줄입니다.