-
Notifications
You must be signed in to change notification settings - Fork 216
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
Use dispatcher API #83
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,13 @@ | |
|
||
import argparse | ||
import numpy as np | ||
import os | ||
import glob | ||
import torch | ||
import torch.utils.cpp_extension | ||
import pkg_resources | ||
|
||
import python.lltm_baseline | ||
import cpp.lltm | ||
|
||
|
||
def check_equal(first, second, verbose): | ||
if verbose: | ||
|
@@ -19,8 +21,7 @@ def check_equal(first, second, verbose): | |
print("x = {}".format(x.flatten())) | ||
print("y = {}".format(y.flatten())) | ||
print('-' * 80) | ||
np.testing.assert_allclose(x, y, err_msg="Index: {}".format(i)) | ||
|
||
np.testing.assert_allclose(x, y, rtol=2e-6, atol=2e-7, err_msg="Index: {}".format(i)) | ||
|
||
def zero_grad(variables): | ||
for variable in variables: | ||
|
@@ -33,14 +34,16 @@ def get_grads(variables): | |
|
||
def check_forward(variables, with_cuda, verbose): | ||
baseline_values = python.lltm_baseline.LLTMFunction.apply(*variables) | ||
cpp_values = cpp.lltm.LLTMFunction.apply(*variables) | ||
cpp_variables = [v.cpu() for v in variables] | ||
cpp_values = torch.ops.myops.lltm(*cpp_variables) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like it's something you already call from LLTMFunction in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this has been migrated into an operator that is registered via dispatcher API, python implementation of If I understand the intent of the code correctly, this use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C++ LLTMFunction impl is primarily used for re-dispatching from autograd op, as per: https://pytorch.org/tutorials/advanced/dispatcher.html#adding-autograd-support |
||
|
||
print('Forward: Baseline (Python) vs. C++ ... ', end='') | ||
check_equal(baseline_values, cpp_values, verbose) | ||
print('Ok') | ||
|
||
if with_cuda: | ||
cuda_values = cuda.lltm.LLTMFunction.apply(*variables) | ||
cuda_variables = [v.cuda() for v in variables] | ||
cuda_values = torch.ops.myops.lltm(*cuda_variables) | ||
print('Forward: Baseline (Python) vs. CUDA ... ', end='') | ||
check_equal(baseline_values, cuda_values, verbose) | ||
print('Ok') | ||
|
@@ -53,7 +56,7 @@ def check_backward(variables, with_cuda, verbose): | |
|
||
zero_grad(variables) | ||
|
||
cpp_values = cpp.lltm.LLTMFunction.apply(*variables) | ||
cpp_values = torch.ops.myops.lltm(*variables) | ||
(cpp_values[0] + cpp_values[1]).sum().backward() | ||
grad_cpp = get_grads(variables) | ||
|
||
|
@@ -63,7 +66,7 @@ def check_backward(variables, with_cuda, verbose): | |
|
||
if with_cuda: | ||
zero_grad(variables) | ||
cuda_values = cuda.lltm.LLTMFunction.apply(*variables) | ||
cuda_values = torch.ops.myops.lltm(*variables) | ||
(cuda_values[0] + cuda_values[1]).sum().backward() | ||
grad_cuda = get_grads(variables) | ||
|
||
|
@@ -81,9 +84,22 @@ def check_backward(variables, with_cuda, verbose): | |
parser.add_argument('-v', '--verbose', action='store_true') | ||
options = parser.parse_args() | ||
|
||
LIB_EXT = torch.utils.cpp_extension.LIB_EXT | ||
cpp_module_path = os.path.dirname( | ||
pkg_resources.resource_filename( | ||
pkg_resources.Requirement.parse('lltm_cpp'), "lltm_cpp.py")) | ||
cpp_lib_path = glob.glob(os.path.join(cpp_module_path, f"lltm_cpp*{LIB_EXT}"))[0] | ||
torch.ops.load_library(cpp_lib_path) | ||
|
||
if options.cuda: | ||
import cuda.lltm | ||
device = torch.device("cuda") | ||
|
||
cuda_module_path = os.path.dirname( | ||
pkg_resources.resource_filename( | ||
pkg_resources.Requirement.parse('lltm_cuda'), "lltm_cuda.py")) | ||
cuda_lib_path = glob.glob(os.path.join(cuda_module_path, f"lltm_cuda*{LIB_EXT}"))[0] | ||
torch.ops.load_library(cuda_lib_path) | ||
else: | ||
device = torch.device("cpu") | ||
|
||
|
@@ -100,6 +116,7 @@ def check_backward(variables, with_cuda, verbose): | |
|
||
variables = [X, W, b, h, C] | ||
|
||
|
||
if 'forward' in options.direction: | ||
check_forward(variables, options.cuda, options.verbose) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like all the changes you made in benchmark.py are not used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The op is called from LLTM module class'
forward()
See {cpp|cuda}/lltm.py