RNN.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #pragma once
  2. #include <ATen/core/Tensor.h>
  3. #include <ATen/native/DispatchStub.h>
  4. namespace at { namespace native {
  5. using lstm_fn = void(*)(Tensor&, Tensor&, Tensor&, const Tensor&, TensorList, TensorList, bool, int64_t, double, bool, bool, bool);
  6. using rnn_fn = void(*)(Tensor&, Tensor&, const Tensor&, const Tensor&, TensorList, bool, int64_t, double, bool, bool, bool);
  7. using lstm_packed_fn = void(*)(Tensor&, Tensor&, Tensor&, const Tensor&, const Tensor&, TensorList, TensorList, bool, int64_t, double, bool, bool);
  8. using rnn_packed_fn = void(*)(Tensor&, Tensor&, const Tensor&, const Tensor&, const Tensor&, TensorList, bool, int64_t, double, bool, bool);
  9. DECLARE_DISPATCH(lstm_fn, lstm_cudnn_stub);
  10. DECLARE_DISPATCH(lstm_fn, lstm_miopen_stub);
  11. DECLARE_DISPATCH(lstm_fn, lstm_mkldnn_stub);
  12. DECLARE_DISPATCH(rnn_fn, gru_cudnn_stub);
  13. DECLARE_DISPATCH(rnn_fn, gru_miopen_stub);
  14. DECLARE_DISPATCH(rnn_fn, rnn_tanh_cudnn_stub);
  15. DECLARE_DISPATCH(rnn_fn, rnn_tanh_miopen_stub);
  16. DECLARE_DISPATCH(rnn_fn, rnn_relu_cudnn_stub);
  17. DECLARE_DISPATCH(rnn_fn, rnn_relu_miopen_stub);
  18. DECLARE_DISPATCH(lstm_packed_fn, lstm_packed_cudnn_stub);
  19. DECLARE_DISPATCH(lstm_packed_fn, lstm_packed_miopen_stub);
  20. DECLARE_DISPATCH(rnn_packed_fn, gru_packed_cudnn_stub);
  21. DECLARE_DISPATCH(rnn_packed_fn, gru_packed_miopen_stub);
  22. DECLARE_DISPATCH(rnn_packed_fn, rnn_tanh_packed_cudnn_stub);
  23. DECLARE_DISPATCH(rnn_packed_fn, rnn_tanh_packed_miopen_stub);
  24. DECLARE_DISPATCH(rnn_packed_fn, rnn_relu_packed_cudnn_stub);
  25. DECLARE_DISPATCH(rnn_packed_fn, rnn_relu_packed_miopen_stub);
  26. inline void check_attributes(const Tensor& input, const TensorList& params, const TensorList& hiddens, bool check_dtype=false) {
  27. auto input_device = input.device();
  28. auto input_dtype = input.scalar_type();
  29. auto check_tensors = [&](const std::string& name, const Tensor& t) {
  30. if (!t.defined()) return;
  31. auto t_device = t.device();
  32. TORCH_CHECK(input_device == t_device,
  33. "Input and ", name, " tensors are not at the same device, found input tensor at ",
  34. input_device, " and ", name, " tensor at ", t_device);
  35. if (check_dtype) {
  36. auto t_dtype = t.scalar_type();
  37. TORCH_CHECK(input_dtype == t_dtype,
  38. "Input and ", name, " tensors are not the same dtype, found input tensor with ",
  39. input_dtype, " and ", name, " tensor with ", t_dtype);
  40. }
  41. };
  42. for (const auto& h : hiddens) check_tensors("hidden", h);
  43. for (const auto& p : params) check_tensors("parameter", p);
  44. }
  45. }} // namespace at::native