bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
torch_adaptor.h
Go to the documentation of this file.
1#ifndef TORCH_STATE_ADAPTOR_H
2#define TORCH_STATE_ADAPTOR_H
3
4#include "bitrl/base/bitrl_config.h"
5
6#ifdef USE_PYTORCH
7
9
10#include <torch/torch.h>
11#include <vector>
12
13namespace bitrl
14{
15namespace utils
16{
17namespace pytorch
18{
19
24{
25
27
28 static value_type to_torch(const std::vector<real_t> &data, DeviceType dtype = DeviceType::CPU,
29 bool requires_grad = false);
30
31 static value_type to_torch(const std::vector<uint_t> &data, DeviceType dtype = DeviceType::CPU,
32 bool requires_grad = false);
33
34 static value_type to_torch(const std::vector<float_t> &data, DeviceType dtype = DeviceType::CPU,
35 bool requires_grad = false);
36 static value_type to_torch(const std::vector<int_t> &data, DeviceType dtype = DeviceType::CPU,
37 bool requires_grad = false);
38 static value_type to_torch(const std::vector<lint_t> &data, DeviceType dtype = DeviceType::CPU,
39 bool requires_grad = false);
40 static value_type to_torch(const std::vector<bool> &data, DeviceType dtype = DeviceType::CPU,
41 bool requires_grad = false);
42
43 template <typename T>
44 static value_type stack(const std::vector<T> &values, DeviceType type = DeviceType::CPU,
45 bool requires_grad = false);
46
47 // static value_type stack(const std::vector<value_type>& values,
48 // DeviceType type=DeviceType::CPU)const;
49
50 static value_type cat(const std::vector<real_t> &values, DeviceType type = DeviceType::CPU,
51 bool requires_grad = false);
52
53 template <typename T>
54 static value_type stack_as_float(const std::vector<std::vector<T>> &values,
55 DeviceType type = DeviceType::CPU);
56
57 template <typename T> static std::vector<T> to_vector(torch_tensor_t tensor);
58
60 torch_tensor_t operator()(const std::vector<real_t> &data) const;
61 torch_tensor_t operator()(const std::vector<float_t> &data) const;
62 torch_tensor_t operator()(const std::vector<int> &data) const;
63};
64
65template <typename T>
66TorchAdaptor::value_type TorchAdaptor::stack_as_float(const std::vector<std::vector<T>> &values,
67 DeviceType dtype)
68{
69
70 std::vector<std::vector<float_t>> values_(values.size());
71
72 // make the input values floats
73 for (uint_t i = 0; i < values.size(); ++i)
74 {
75 values_[i].resize(values[i].size());
76 for (uint_t j = 0; j < values[i].size(); ++j)
77 {
78 values_[i][j] = static_cast<float>(values[i][j]);
79 }
80 }
81
82 return TorchAdaptor::stack(values_, dtype);
83}
84
85} // namespace pytorch
86} // namespace utils
87} // namespace bitrl
88#endif
89#endif // TORCH_STATE_ADAPTOR_H
Definition torch_adaptor.h:18
Definition bitrl_concepts.h:22
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
torch::Tensor torch_tensor_t
torch_tensor_t
Definition bitrl_types.h:95
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
DeviceType
Enumeration of various device types.
Definition bitsim_types.h:186
The TorchStateAdaptor struct.
Definition torch_adaptor.h:24
static value_type to_torch(const std::vector< float_t > &data, DeviceType dtype=DeviceType::CPU, bool requires_grad=false)
torch_tensor_t operator()(const std::vector< int > &data) const
static value_type to_torch(const std::vector< uint_t > &data, DeviceType dtype=DeviceType::CPU, bool requires_grad=false)
static value_type to_torch(const std::vector< lint_t > &data, DeviceType dtype=DeviceType::CPU, bool requires_grad=false)
torch_tensor_t operator()(const std::vector< float_t > &data) const
static value_type to_torch(const std::vector< real_t > &data, DeviceType dtype=DeviceType::CPU, bool requires_grad=false)
static value_type stack(const std::vector< T > &values, DeviceType type=DeviceType::CPU, bool requires_grad=false)
static std::vector< T > to_vector(torch_tensor_t tensor)
static value_type to_torch(const std::vector< int_t > &data, DeviceType dtype=DeviceType::CPU, bool requires_grad=false)
torch_tensor_t value_type
Definition torch_adaptor.h:26
torch_tensor_t operator()(const std::vector< real_t > &data) const
torch_tensor_t operator()(real_t value) const
static value_type cat(const std::vector< real_t > &values, DeviceType type=DeviceType::CPU, bool requires_grad=false)
static value_type to_torch(const std::vector< bool > &data, DeviceType dtype=DeviceType::CPU, bool requires_grad=false)
static value_type stack_as_float(const std::vector< std::vector< T > > &values, DeviceType type=DeviceType::CPU)
Definition torch_adaptor.h:66