|
| 1 | +#ifndef NATIVE_NET_H |
| 2 | +#define NATIVE_NET_H |
| 3 | + |
| 4 | +#include "torch/torch.h" |
| 5 | + |
| 6 | +namespace dd |
| 7 | +{ |
| 8 | + |
| 9 | + class NativeModule : public torch::nn::Module |
| 10 | + { |
| 11 | + public: |
| 12 | + virtual torch::Tensor forward(torch::Tensor x) = 0; |
| 13 | + virtual ~NativeModule() |
| 14 | + { |
| 15 | + } |
| 16 | + /** |
| 17 | + * \brief see torch::module::to |
| 18 | + * @param device cpu / gpu |
| 19 | + * @param non_blocking |
| 20 | + */ |
| 21 | + virtual void to(torch::Device device, bool non_blocking = false) |
| 22 | + { |
| 23 | + torch::nn::Module::to(device, non_blocking); |
| 24 | + _device = device; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * \brief see torch::module::to |
| 29 | + * @param dtype : torch::kFloat32 or torch::kFloat64 |
| 30 | + * @param non_blocking |
| 31 | + */ |
| 32 | + virtual void to(torch::Dtype dtype, bool non_blocking = false) |
| 33 | + { |
| 34 | + torch::nn::Module::to(dtype, non_blocking); |
| 35 | + _dtype = dtype; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * \brief see torch::module::to |
| 40 | + * @param device cpu / gpu |
| 41 | + * @param dtype : torch::kFloat32 or torch::kFloat64 |
| 42 | + * @param non_blocking |
| 43 | + */ |
| 44 | + virtual void to(torch::Device device, torch::Dtype dtype, |
| 45 | + bool non_blocking = false) |
| 46 | + { |
| 47 | + torch::nn::Module::to(device, dtype, non_blocking); |
| 48 | + _device = device; |
| 49 | + _dtype = dtype; |
| 50 | + } |
| 51 | + |
| 52 | + virtual torch::Tensor cleanup_output(torch::Tensor output) |
| 53 | + { |
| 54 | + return output; |
| 55 | + } |
| 56 | + |
| 57 | + virtual torch::Tensor loss(std::string loss, torch::Tensor input, |
| 58 | + torch::Tensor output, torch::Tensor target) |
| 59 | + = 0; |
| 60 | + |
| 61 | + virtual void update_input_connector(TorchInputInterface &inputc) |
| 62 | + { |
| 63 | + (void)(inputc); |
| 64 | + } |
| 65 | + |
| 66 | + protected: |
| 67 | + torch::Dtype _dtype |
| 68 | + = torch::kFloat32; /**< type of data stored in tensors */ |
| 69 | + torch::Device _device |
| 70 | + = torch::DeviceType::CPU; /**< device to compute on */ |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +#endif |
0 commit comments