RNN.h 2.7 KB

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