seigot / tetris

A Tetris Game for programming education in Japanese
MIT License
30 stars 107 forks source link

`RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same or input should be a MKLDNN tensor and weight is a dense tensor` #22

Open seigot opened 2 years ago

seigot commented 2 years ago

https://stackoverflow.com/questions/59013109/runtimeerror-input-type-torch-floattensor-and-weight-type-torch-cuda-floatte

> $ git clone https://github.com/bushio/tetris -b submit_level2 tetris.bushio
> $ cd tetris.bushio
> $ python start.py -l2 -m predict
> game_level: 2
> game_time: 180
> RANDOM_SEED: -1
> IS_MODE :predict
> OBSTACLE_HEIGHT: 0
> OBSTACLE_PROBABILITY: 0
> USER_NAME: window_sample
> RESULT_LOG_JSON: result.json
> Python 3.9.9
> CompletedProcess(args='python --version', returncode=0, stderr='')
> model name: DQNv2
> load  sample_weight/cnn_test_ft_level2/tetris_epoch_1948_score271700
> set target network...
> Traceback (most recent call last):
>   File "/home/ubuntu/tmp/tetris_dir/tetris.bushio/game_manager/game_manager.py", line 247, in timerEvent
>     self.nextMove = BLOCK_CONTROLLER_TRAIN.GetNextMove(nextMove, GameStatus,weight=self.weight)
>   File "/home/ubuntu/tmp/tetris_dir/tetris.bushio/game_manager/machine_learning/block_controller_train.py", line 581, in GetNextMove
>     predictions = self.model(next_states)[:, 0]
>   File "/home/ubuntu/venvtest/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
>     return forward_call(*input, **kwargs)
>   File "/home/ubuntu/tmp/tetris_dir/tetris.bushio/game_manager/model/deepqnet.py", line 56, in forward
>     x = self.conv1(x)
>   File "/home/ubuntu/venvtest/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
>     return forward_call(*input, **kwargs)
>   File "/home/ubuntu/venvtest/lib/python3.9/site-packages/torch/nn/modules/container.py", line 141, in forward
>     input = module(input)
>   File "/home/ubuntu/venvtest/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
>     return forward_call(*input, **kwargs)
>   File "/home/ubuntu/venvtest/lib/python3.9/site-packages/torch/nn/modules/conv.py", line 446, in forward
>     return self._conv_forward(input, self.weight, self.bias)
>   File "/home/ubuntu/venvtest/lib/python3.9/site-packages/torch/nn/modules/conv.py", line 442, in _conv_forward
>     return F.conv2d(input, weight, bias, self.stride,
> RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same or input should be a MKLDNN tensor and weight is a dense tensor
> Aborted (core dumped)
> error: subprocess failed.
> (venvtest) ubuntu@ubuntu-ZBOX-EN72080V-EN72070V-EN52060V-EN51660T:~/tmp/tetris_dir/tetris.bushio$ 
seigot commented 2 years ago

Macだと問題なく動くので、cuda周りの有無が影響していそうである

seigot commented 2 years ago

これで起こらなくなる

diff --git a/game_manager/machine_learning/block_controller_train.py b/game_manager/machine_learning/block_controller_train.py
index 27b4786..2b32ffe 100644
--- a/game_manager/machine_learning/block_controller_train.py
+++ b/game_manager/machine_learning/block_controller_train.py
@@ -101,8 +101,8 @@ class Block_Controller(object):
                 self.model = torch.load(self.load_weight)
                 self.model.eval()

-        if torch.cuda.is_available():
-            self.model.cuda()
+#        if torch.cuda.is_available():
+#            self.model.cuda()

         #=====Set hyper parameter=====
         self.batch_size = cfg.train.batch_size
@@ -492,8 +492,8 @@ class Block_Controller(object):
             next_actions, next_states = zip(*next_steps.items())
             next_states = torch.stack(next_states)

-            if torch.cuda.is_available():
-                next_states = next_states.cuda()
+#            if torch.cuda.is_available():
+#                next_states = next_states.cuda()

             self.model.train()
             with torch.no_grad():
@@ -516,8 +516,8 @@ class Block_Controller(object):
                 next2_steps =self.get_next_func(next_backboard,next_piece_id,next_shape_class)
                 next2_actions, next2_states = zip(*next2_steps.items())
                 next2_states = torch.stack(next2_states)
-                if torch.cuda.is_available():
-                    next2_states = next2_states.cuda()
+#                if torch.cuda.is_available():
+#                    next2_states = next2_states.cuda()
                 self.model.train()
                 with torch.no_grad():
                     next_predictions = self.model(next2_states)[:, 0]
@@ -530,8 +530,8 @@ class Block_Controller(object):
                 next2_steps =self.get_next_func(next_backboard,next_piece_id,next_shape_class)
                 next2_actions, next2_states = zip(*next2_steps.items())
                 next2_states = torch.stack(next2_states)
-                if torch.cuda.is_available():
-                    next2_states = next2_states.cuda()
+#                if torch.cuda.is_available():
+#                    next2_states = next2_states.cuda()
                 self.target_model.train()
                 with torch.no_grad():
                     next_predictions = self.target_model(next2_states)[:, 0]
@@ -544,8 +544,8 @@ class Block_Controller(object):
                 next2_steps =self.get_next_func(next_backboard,next_piece_id,next_shape_class)
                 next2_actions, next2_states = zip(*next2_steps.items())
                 next2_states = torch.stack(next2_states)
-                if torch.cuda.is_available():
-                    next2_states = next2_states.cuda()
+#                if torch.cuda.is_available():
+#                    next2_states = next2_states.cuda()
                 self.model.train()
                 with torch.no_grad():
                     next_predictions = self.model(next2_states)[:, 0]
gitauto-ai[bot] commented 1 month ago

Hey, I'm a bit lost here! Not sure which file I should be fixing. Could you give me a bit more to go on? Maybe add some details to the issue or drop a comment with some extra hints? Thanks!

Have feedback or need help? Feel free to email info@gitauto.ai.