Skip to content

Commit

Permalink
recipes_source/torch_logs.py ๋ฒˆ์—ญ (#963)
Browse files Browse the repository at this point in the history
* translate beginner_source/torchtext_custom_dataset_tutorial.py
  • Loading branch information
hyoyoung authored Nov 30, 2024
1 parent 5fc8a7d commit a321092
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
7 changes: 4 additions & 3 deletions beginner_source/torchtext_custom_dataset_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
* ๋ฌธ์žฅ์— ๋ณ€ํ™˜ ์ ์šฉํ•˜๊ธฐ
* ๋ฒ„ํ‚ท ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ
๋‹ค์Œ ์˜์–ด์—์„œ ๋…์ผ์–ด ๋ฒˆ์—ญ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ์…‹์„ ์ค€๋น„ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
์˜์–ด์—์„œ ๋…์ผ์–ด ๋ฒˆ์—ญ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ์…‹์„ ์ค€๋น„ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
`Tatoeba Project <https://tatoeba.org/en>`_๊ฐ€ ์ œ๊ณตํ•˜๋Š” ํƒญ์œผ๋กœ ๊ตฌ๋ถ„๋œ ๋…์ผ์–ด-์˜์–ด ๋ฌธ์žฅ ์Œ์„ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
์ด ๋ฐ์ดํ„ฐ๋Š” `๋‹ค์šด๋กœ๋“œ ๋งํฌ <https://www.manythings.org/anki/deu-eng.zip>`__ ์—์„œ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Expand Down Expand Up @@ -83,7 +84,7 @@
# ์œ„์˜ ์ฝ”๋“œ ๋ธ”๋ก์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค
#
# 1. 2๋ฒˆ์งธ ์ค„์—์„œ ํŒŒ์ผ ์ด๋ฆ„์˜ ๋ฐ˜๋ณต๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค
# 2. 3๋ฒˆ์งธ ์ค„์—์„œ ํ•ด๋‹น ๋ฐ˜๋ณต๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด `FileOpener`์— ์ „๋‹ฌํ•˜๊ณ ,
# 2. 3๋ฒˆ์งธ ์ค„์—์„œ ํ•ด๋‹น ๋ฐ˜๋ณต๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋ฅผ `FileOpener`์— ์ „๋‹ฌํ•˜๊ณ ,
# ํŒŒ์ผ์„ ์ฝ๊ธฐ ๋ชจ๋“œ๋กœ ์—ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
# 3. 4๋ฒˆ์งธ ์ค„์—์„œ ํ•ด๋‹น ํŒŒ์ผ์„ ํŒŒ์‹ฑํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
# ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ํƒญ์œผ๋กœ ๊ตฌ๋ถ„๋œ ํŒŒ์ผ์˜ ๊ฐ๊ฐ ์ค„(row)์ด ์žˆ๋Š” ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ํŠœํ”Œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
Expand Down Expand Up @@ -330,7 +331,7 @@ def separateSourceTarget(sequence_pairs):
# %%
# ์ด์ œ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค
#
# ํŽ˜๋”ฉ
# ํŒจ๋”ฉ
# -------
# ์•ž์„œ ์„ค๋ช…ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ์–ดํœ˜๋ฅผ ๊ตฌ์ถ•ํ•  ๋•Œ, ๋ฐฐ์น˜์—์„œ ๋ชจ๋“  ์‹œํ€€์Šค๋ฅผ ๋™์ผํ•œ ๊ธธ์ด๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด
# ์งง์€ ๋ฌธ์žฅ์€ ํŒจ๋”ฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํŒจ๋”ฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Expand Down
52 changes: 28 additions & 24 deletions recipes_source/torch_logs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""
(beta) Using TORCH_LOGS python API with torch.compile
(beta) torch.compile๊ณผ ํ•จ๊ป˜ TORCH_LOGS ํŒŒ์ด์ฌ API ์‚ฌ์šฉํ•˜๊ธฐ
==========================================================================================
**Author:** `Michael Lazos <https://github.com/mlazos>`_
**์ €์ž:** `Michael Lazos <https://github.com/mlazos>`_
**๋ฒˆ์—ญ:** `์žฅํšจ์˜ <https://github.com/hyoyoung>`_
"""

import logging
Expand All @@ -10,29 +11,33 @@
#
# This tutorial introduces the ``TORCH_LOGS`` environment variable, as well as the Python API, and
# demonstrates how to apply it to observe the phases of ``torch.compile``.
# ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ``TORCH_LOGS`` ํ™˜๊ฒฝ ๋ณ€์ˆ˜์™€ ํ•จ๊ป˜ Python API๋ฅผ ์†Œ๊ฐœํ•˜๊ณ ,
# ์ด๋ฅผ ์ ์šฉํ•˜์—ฌ ``torch.compile``์˜ ๋‹จ๊ณ„๋ฅผ ๊ด€์ฐฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
#
# .. note::
#
# This tutorial requires PyTorch 2.2.0 or later.
# ์ด ํŠœํ† ๋ฆฌ์–ผ์—๋Š” PyTorch 2.2.0 ์ด์ƒ ๋ฒ„์ „์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
#
#


######################################################################
# Setup
# ์„ค์ •
# ~~~~~~~~~~~~~~~~~~~~~
# In this example, we'll set up a simple Python function which performs an elementwise
# add and observe the compilation process with ``TORCH_LOGS`` Python API.
# ์ด ์˜ˆ์ œ์—์„œ๋Š” ์š”์†Œ๋ณ„ ๋ง์…ˆ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํŒŒ์ด์ฌ ํ•จ์ˆ˜๋ฅผ ์„ค์ •ํ•˜๊ณ 
# ``TORCH_LOGS`` ํŒŒ์ด์ฌ API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํŒŒ์ผ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ด€์ฐฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
#
# .. note::
#
# There is also an environment variable ``TORCH_LOGS``, which can be used to
# change logging settings at the command line. The equivalent environment
# variable setting is shown for each example.
# ๋ช…๋ น์ค„์—์„œ ๋กœ๊น… ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”
# ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ``TORCH_LOGS``๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์˜ˆ์ œ์— ํ•ด๋‹นํ•˜๋Š”
# ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •์ด ํ‘œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

import torch

# exit cleanly if we are on a device that doesn't support torch.compile
# torch.compile์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ธฐ๊ธฐ์ธ ๊ฒฝ์šฐ ์™„์ „ํžˆ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
if torch.cuda.get_device_capability() < (7, 0):
print("Skipping because torch.compile is not supported on this device.")
else:
Expand All @@ -45,52 +50,51 @@ def fn(x, y):
inputs = (torch.ones(2, 2, device="cuda"), torch.zeros(2, 2, device="cuda"))


# print separator and reset dynamo
# between each example
# ๊ฐ ์˜ˆ์ œ ์‚ฌ์ด์˜ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  dynamo๋ฅผ resetํ•ฉ๋‹ˆ๋‹ค
def separator(name):
print(f"==================={name}=========================")
torch._dynamo.reset()


separator("Dynamo Tracing")
# View dynamo tracing
# dynamo tracing ๋ณด๊ธฐ
# TORCH_LOGS="+dynamo"
torch._logging.set_logs(dynamo=logging.DEBUG)
fn(*inputs)

separator("Traced Graph")
# View traced graph
# traced ๊ทธ๋ž˜ํ”„ ๋ณด๊ธฐ
# TORCH_LOGS="graph"
torch._logging.set_logs(graph=True)
fn(*inputs)

separator("Fusion Decisions")
# View fusion decisions
# fusion decision ๋ณด๊ธฐ
# TORCH_LOGS="fusion"
torch._logging.set_logs(fusion=True)
fn(*inputs)

separator("Output Code")
# View output code generated by inductor
# inductor๊ฐ€ ์ƒ์„ฑํ•œ ๊ฒฐ๊ณผ ์ฝ”๋“œ ๋ณด๊ธฐ
# TORCH_LOGS="output_code"
torch._logging.set_logs(output_code=True)
fn(*inputs)

separator("")

######################################################################
# Conclusion
# ๊ฒฐ๋ก 
# ~~~~~~~~~~
#
# In this tutorial we introduced the TORCH_LOGS environment variable and python API
# by experimenting with a small number of the available logging options.
# To view descriptions of all available options, run any python script
# which imports torch and set TORCH_LOGS to "help".
# ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ช‡ ๊ฐ€์ง€ ๋กœ๊น… ์˜ต์…˜์„ ์‹คํ—˜ํ•˜์—ฌ
# TORCH_LOGS ํ™˜๊ฒฝ ๋ณ€์ˆ˜์™€ Python API๋ฅผ ์†Œ๊ฐœํ–ˆ์Šต๋‹ˆ๋‹ค.
# ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์˜ต์…˜์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋ณด๋ ค๋ฉด
# ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ์—์„œ import torch๋ฅผ ์‹คํ–‰ํ•˜๊ณ  TORCH_LOGS๋ฅผ "help"๋กœ ์„ค์ •ํ•˜์„ธ์š”.
#
# Alternatively, you can view the `torch._logging documentation`_ to see
# descriptions of all available logging options.
# ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š”, `torch._logging ๋ฌธ์„œ`_ ๋ฅผ ๋ณด๋ฉด,
# ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋กœ๊น… ์˜ต์…˜์— ๋Œ€ํ•œ ์„ค๋ช…์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
#
# For more information on torch.compile, see the `torch.compile tutorial`_.
# torch.compile์— ๊ด€ํ•œ ๋” ๋งŽ์€ ์ •๋ณด๋Š”, `torch.compile ํŠœํ† ๋ฆฌ์–ผ`_๋ฅผ ๋ณด์„ธ์š”.
#
# .. _torch._logging documentation: https://pytorch.org/docs/main/logging.html
# .. _torch.compile tutorial: https://tutorials.pytorch.kr/intermediate/torch_compile_tutorial.html
# .. _torch._logging ๋ฌธ์„œ: https://pytorch.org/docs/main/logging.html
# .. _torch.compile ํŠœํ† ๋ฆฌ์–ผ: https://tutorials.pytorch.kr/intermediate/torch_compile_tutorial.html

0 comments on commit a321092

Please sign in to comment.