From 98f0344b86f7248d7c1126da5e6653a2fabe1498 Mon Sep 17 00:00:00 2001 From: jiho <62324197+jiho3004@users.noreply.github.com> Date: Sun, 26 Nov 2023 23:09:47 +0900 Subject: [PATCH] =?UTF-8?q?prototype=5Fsource/torchscript=5Ffreezing.py=20?= =?UTF-8?q?=EB=B2=88=EC=97=AD=20=20(#788)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit prototype_source/torchscript_freezing.py 번역 --- prototype_source/torchscript_freezing.py | 53 +++++++++++------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/prototype_source/torchscript_freezing.py b/prototype_source/torchscript_freezing.py index 3ca3227f4..dcf0afaa9 100644 --- a/prototype_source/torchscript_freezing.py +++ b/prototype_source/torchscript_freezing.py @@ -1,23 +1,24 @@ """ -Model Freezing in TorchScript +TorchScript로 모델 동결하기 ============================= +번역 : `김지호 `_ -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 내부 표현으로 인라이닝(inlining)하는 과정입니다. +매개변수와 속성 값들은 최종 값으로 처리되며 동결된 모듈에서 수정될 수 없습니다. -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 소개 튜토리얼 `_ +을 참조하세요. -Next, we demonstrate how freezing works using an example: +다음으로, 예제를 통해 동결이 어떤 방식으로 동작하는지 확인합니다: """ import torch, time @@ -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") @@ -108,7 +107,7 @@ 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 @@ -116,19 +115,17 @@ def version(self): # * 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% 더 빠릅니다. +# 실행 시간은 tensor 연산에 의해 지배되기 때문에 입력의 크기가 더 커지면 속도 개선 정도는 더 작아집니다. ############################################################### -# 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 워밍업 시간을 크게 줄입니다.