libdl  0.0.1
Simple yet powerful deep learning
Loading...
Searching...
No Matches
device.hpp
1#pragma once
2
3#include "tensor/shape.hpp"
4#include "tensor/tensorimpl.hpp"
5#include "utils/scopeguard.hpp"
6
7namespace dl {
8 class Device {
9 private:
16 thread_local static Device const* defaultDevice;
17
18 Device(const Device& other) = delete;
19 Device(Device&& other) = delete;
20 Device& operator=(const Device& other) = delete;
21 Device& operator=(Device&& other) = delete;
22
23 protected:
24 Device() = default;
25
26 public:
27 virtual ~Device() = default;
28
32 static Device const& cpu;
33
41 virtual TensorPtr empty(Shape shape, bool requiresGrad = false) const noexcept = 0;
49 virtual TensorPtr zeros(Shape shape, bool requiresGrad = false) const noexcept = 0;
57 virtual TensorPtr ones(Shape shape, bool requiresGrad = false) const noexcept = 0;
58 virtual TensorPtr rand(Shape shape, bool requiresGrad = false) const noexcept = 0;
59 virtual TensorPtr constant(int value, bool requiresGrad = false) const noexcept = 0;
60 virtual TensorPtr constant(float value, bool requiresGrad = false) const noexcept = 0;
61 virtual TensorPtr constant(double value, bool requiresGrad = false) const noexcept = 0;
62 virtual TensorPtr constant(InitializerTensor<float>&& value, bool requiresGrad = false) const noexcept = 0;
63 virtual TensorPtr fromBytesFP32(const char* buffer, size_t bufsize, Shape shape) const noexcept = 0;
64
65 template <typename T>
66 void setDefaultFloatTensorType();
67
76 static const Device& getDefault() noexcept { return *defaultDevice; }
77
93 static auto changeDefaultDevice(Device& device) {
94 auto oldDefault = defaultDevice;
95 defaultDevice = &device;
96 return dl::utils::ScopeGuard([oldDefault] { Device::defaultDevice = oldDefault; });
97 }
98 };
99
100 template <>
101 void Device::setDefaultFloatTensorType<float>();
102 template <>
103 void Device::setDefaultFloatTensorType<double>();
104
105 inline TensorPtr empty(const Shape& size, Device const& device = Device::getDefault()) {
106 return device.empty(size);
107 }
108 inline TensorPtr zeros(const Shape& size, Device const& device = Device::getDefault()) {
109 return device.zeros(size);
110 }
111 inline TensorPtr ones(const Shape& size, Device const& device = Device::getDefault()) { return device.ones(size); }
112 inline TensorPtr rand(const Shape& size, Device const& device = Device::getDefault()) { return device.rand(size); }
113 TensorPtr zeros_like(const TensorPtr& tensor, Device const& device = Device::getDefault());
114 TensorPtr ones_like(const TensorPtr& tensor, Device const& device = Device::getDefault());
115 TensorPtr rand_like(const TensorPtr& tensor, Device const& device = Device::getDefault());
116 inline TensorPtr constant(int value, Device const& device = Device::getDefault()) { return device.constant(value); }
117 inline TensorPtr constant(float value, Device const& device = Device::getDefault()) {
118 return device.constant(value);
119 }
120 inline TensorPtr constant(double value, Device const& device = Device::getDefault()) {
121 return device.constant(value);
122 }
123 inline TensorPtr constant(InitializerTensor<float>&& value, Device const& device = Device::getDefault()) {
124 return device.constant(std::move(value));
125 }
126
127 template <typename T>
128 TensorPtr
129 fromBytes(const char* buffer, size_t buflen, const Shape& shape, Device const& device = Device::getDefault());
130 template <>
131 inline TensorPtr fromBytes<float>(const char* buffer, size_t buflen, const Shape& shape, Device const& device) {
132 return device.fromBytesFP32(buffer, buflen, shape);
133 }
134
135 TensorPtr clone(const TensorPtr& tensor);
136} // namespace dl
virtual TensorPtr zeros(Shape shape, bool requiresGrad=false) const noexcept=0
Creates a new tensor of the specified shape. All entries are initialized to zero.
virtual TensorPtr empty(Shape shape, bool requiresGrad=false) const noexcept=0
Creates a new tensor of the specified shape without initializing the memory.
static Device const & cpu
The CPU-device implementation.
Definition device.hpp:32
static const Device & getDefault() noexcept
Returns the default device for this thread.
Definition device.hpp:76
virtual TensorPtr ones(Shape shape, bool requiresGrad=false) const noexcept=0
Creates a new tensor of the specified shape. All entries are initialized to one.
static auto changeDefaultDevice(Device &device)
Temporarily modifies the default device.
Definition device.hpp:93
The Tensor is a managed pointer to a tensor. It can generally be thought of like an std::unique_ptr<T...
Definition tensorptr.hpp:45
T move(T... args)
T rand(T... args)