facebookresearch / gtn_applications

Applications using the GTN library and code to reproduce experiments in "Differentiable Weighted Finite-State Transducers"
MIT License
80 stars 7 forks source link

module 'gtn' has no attribute 'Device' in STC #19

Closed LEECHOONGHO closed 2 years ago

LEECHOONGHO commented 2 years ago

I installed gtn by pip install gtn and tested STC like below

from criterions.STC import STC
import random
import torch

stcloss = STC(blank_idx=0, p0=1, plast=1, thalf=1, reduction="none")

batch_size = 2
mel_len = 115
text_len = 25
num_token = 64
inputs = torch.randn(mel_len, batch_size, num_token)
targets = [[random.randint(1, text_len) for _ in range(random.randint(1, num_token))] for _ in range(batch_size)]
loss = stcloss(inputs, targets)

But when I trying to run code like above. The Error Like below occurs.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-b0ecfb513efd> in <module>
      7 inputs = torch.randn(mel_len, batch_size, num_token)
      8 targets = [[random.randint(1, text_len) for _ in range(random.randint(1, num_token))] for _ in range(batch_size)]
----> 9 loss = stcloss(inputs, targets)

~/anaconda3/envs/leecho/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1101                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102             return forward_call(*input, **kwargs)
   1103         # Do not call functions when jit is used
   1104         full_backward_hooks, non_full_backward_hooks = [], []

/data/leecho/xi-stt/xi-stt/model/STCLoss.py in forward(self, inputs, targets)
    212             # concatenate (tokens, <star>, <star>\tokens)
    213             log_probs = torch.cat([log_probs, lse, neglse], dim=2)
--> 214         return STCLoss(log_probs, targets, prob, self.reduction)

/data/leecho/xi-stt/xi-stt/model/STCLoss.py in forward(ctx, inputs, targets, prob, reduction)
     96             emissions_graphs[b] = g_emissions
     97 
---> 98         gtn.parallel_for(process, range(B))
     99 
    100         ctx.auxiliary_data = (losses, scales, emissions_graphs, inputs.shape)

/data/leecho/xi-stt/xi-stt/model/STCLoss.py in process(b)
     71             # create emission graph
     72             g_emissions = gtn.linear_graph(
---> 73                 T, Cstar, gtn.Device(gtn.CPU), inputs.requires_grad
     74             )
     75             cpu_data = inputs[b].cpu().contiguous()

AttributeError: module 'gtn' has no attribute 'Device'

Is there any way to solve this problem?

vineelpratap commented 2 years ago

Hi,

Could you install python bindings from source - https://github.com/gtn-org/gtn#python-bindings-from-source

We haven't updated pip with the latest changes to gtn library.

LEECHOONGHO commented 2 years ago

@vineelpratap Thank you for your reply. I have installed gtn and it's python bindings. But when I Tried to test my installation, same error occurs.

/gtn# python -m unittest discover bindings/python/test
......E.....E...............EEE...........F....
======================================================================
ERROR: test_scalar_ops_grad (test_autograd.AutogradTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/leecho/xi-stt/xi-stt/gtn/bindings/python/test/test_autograd.py", line 106, in test_scalar_ops_grad
    g2nograd = gtn.scalar_graph(4.0, gtn.Device(gtn.CPU), False)
AttributeError: module 'gtn' has no attribute 'Device'

======================================================================
ERROR: test_device (test_bindings.DeviceTesetCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/leecho/xi-stt/xi-stt/gtn/bindings/python/test/test_bindings.py", line 26, in test_device
    self.assertEqual(gtn.Device(gtn.CUDA, 0), gtn.Device(gtn.CUDA, 0))
AttributeError: module 'gtn' has no attribute 'Device'

======================================================================
ERROR: test_scalar_creation (test_creations.CreationsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/leecho/xi-stt/xi-stt/gtn/bindings/python/test/test_creations.py", line 16, in test_scalar_creation
    g = gtn.scalar_graph(weight, gtn.Device(gtn.CPU), False)
AttributeError: module 'gtn' has no attribute 'Device'

======================================================================
ERROR: test_criterions (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_criterions
Traceback (most recent call last):
  File "/root/anaconda3/envs/xi-stt/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/root/anaconda3/envs/xi-stt/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/home/leecho/xi-stt/xi-stt/gtn/bindings/python/test/test_criterions.py", line 11, in <module>
    import gtn.criterion
ModuleNotFoundError: No module named 'gtn.criterion'

======================================================================
ERROR: test_cuda (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_cuda
Traceback (most recent call last):
  File "/root/anaconda3/envs/xi-stt/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/root/anaconda3/envs/xi-stt/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/home/leecho/xi-stt/xi-stt/gtn/bindings/python/test/test_cuda.py", line 5, in <module>
    class CudaTestCase(unittest.TestCase):
  File "/home/leecho/xi-stt/xi-stt/gtn/bindings/python/test/test_cuda.py", line 7, in CudaTestCase
    @unittest.skipIf(not gtn.cuda.is_available(), "CUDA not available")
AttributeError: module 'gtn' has no attribute 'cuda'

======================================================================
FAIL: test_equality (test_utils.UtilsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/leecho/xi-stt/xi-stt/gtn/bindings/python/test/test_utils.py", line 120, in test_equality
    self.assertFalse(gtn.equal(g1, g2))
AssertionError: True is not false

And I got warning message like below when I execute python setup.py develop or python setup.py install

CMake Warning (dev) in CMakeLists.txt:
  Policy CMP0104 is not set: CMAKE_CUDA_ARCHITECTURES now detected for NVCC,
  empty CUDA_ARCHITECTURES not allowed.  Run "cmake --help-policy CMP0104"
  for policy details.  Use the cmake_policy command to set the policy and
  suppress this warning.

  CUDA_ARCHITECTURES is empty for target "gtn".
This warning is for project developers.  Use -Wno-dev to suppress it.
cmake version 3.22.2
cuda version 11.5
cudnn version 8.0
OpenSSL 1.1.1m  14 Dec 2021

It seems that there are no modules related to CUDA.

>>> import gtn
>>> dir(gtn)
['Graph', '__builtins__', '__cached__', '__doc__', '__file__', 
'__loader__', '__name__', '__package__', '__path__', '__spec__',
 '__version__', '_autograd', '_creations', '_functions', '_graph', 
'_parallel', '_rand', '_utils', 'add', 'backward', 'clone', 'closure',
 'compose', 'concat', 'draw', 'epsilon', 'equal', 'forward_score', 
'intersect', 'isomorphic', 'linear_graph', 'load', 'loadtxt', 'negate', 
'os', 'parallel_for', 'project_input', 'project_output', 'rand_equivalent',
 'remove', 'sample', 'save', 'savetxt', 'scalar_graph', 'subprocess', 
'subtract', 'tempfile', 'union', 'viterbi_path', 'viterbi_score', 'write_dot']

Thank you

vineelpratap commented 2 years ago

Hi, I just tried everything in a new environment, and it works for me. Could you please uninstall gtn using pip uninstall gtn and retry again?

Also, are you using the latest commit from https://github.com/gtn-org/gtn ?

LEECHOONGHO commented 2 years ago

@vineelpratap

Sorry for my late. I tried to install gtn in docker and when I execute python setup.py install I got error message like below. I'm using 8 Tesla T8. How can I change Cmakelist.txt for solving this problem?

-- Build files have been written to: /data/leecho/gtn/bindings/python/build/temp.linux-x86_64-3.8
[  4%] Performing update step for 'pybind11'
[  8%] Building CUDA object CMakeFiles/gtn.dir/gtn/cuda/creations.cu.o
[  8%] Building CUDA object CMakeFiles/gtn.dir/gtn/cuda/compose.cu.o
[  8%] Building CUDA object CMakeFiles/gtn.dir/gtn/cuda/cuda.cu.o
nvcc fatal   : Value 'sm_80' is not defined for option 'gpu-architecture'
nvcc fatal   : Value 'sm_80' is not defined for option 'gpu-architecture'
CMakeFiles/gtn.dir/build.make:251: recipe for target 'CMakeFiles/gtn.dir/gtn/cuda/creations.cu.o' failed
make[2]: *** [CMakeFiles/gtn.dir/gtn/cuda/creations.cu.o] Error 1
make[2]: *** Waiting for unfinished jobs....
CMakeFiles/gtn.dir/build.make:264: recipe for target 'CMakeFiles/gtn.dir/gtn/cuda/compose.cu.o' failed
make[2]: *** [CMakeFiles/gtn.dir/gtn/cuda/compose.cu.o] Error 1
nvcc fatal   : Value 'sm_80' is not defined for option 'gpu-architecture'
CMakeFiles/gtn.dir/build.make:277: recipe for target 'CMakeFiles/gtn.dir/gtn/cuda/cuda.cu.o' failed
make[2]: *** [CMakeFiles/gtn.dir/gtn/cuda/cuda.cu.o] Error 1
CMakeFiles/Makefile2:183: recipe for target 'CMakeFiles/gtn.dir/all' failed
make[1]: *** [CMakeFiles/gtn.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
vineelpratap commented 2 years ago

Hi, CUDA is not required for running STC and the criterion run on CPU.

You can add an additional argument to cmake in the setup.py file "-DGTN_BUILD_CUDA=OFF" to disable building CUDA backend.

LEECHOONGHO commented 2 years ago

Thank you for your advice

I finally confirmed the operation of STC.

from model.STCLoss import STC
import random
import torch
from torch.autograd import Variable
stcloss = STC(blank_idx=0, p0=1, plast=1, thalf=1, reduction="none")

batch_size = 2
max_mel_len = 115
max_text_len = 25
num_tokens = 64
inputs = Variable(torch.randn(max_mel_len , batch_size, num_token), requires_grad=True)
targets = [[random.randint(1, max_text_len ) for _ in range(random.randint(1, num_tokens))] for _ in range(batch_size)]
loss = stcloss(inputs, targets)
print(loss)
loss.backward()

tensor(-464.1473, grad_fn=<STCLossFunctionBackward>)
vineelpratap commented 2 years ago

Cool.

Note that STC expects log probabilities as input. You might want to apply logSoftmax(dim=0) on the input before calling stc loss function.