3#include "tensor/shape.hpp"
4#include "tensor/tensorimpl.hpp"
5#include "utils/scopeguard.hpp"
16 thread_local static Device const* defaultDevice;
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;
63 virtual
TensorPtr fromBytesFP32(const
char* buffer,
size_t bufsize,
Shape shape) const noexcept = 0;
66 void setDefaultFloatTensorType();
94 auto oldDefault = defaultDevice;
95 defaultDevice = &device;
101 void Device::setDefaultFloatTensorType<float>();
103 void Device::setDefaultFloatTensorType<double>();
105 inline TensorPtr empty(
const Shape& size, Device
const& device =
Device::getDefault()) {
106 return device.empty(size);
108 inline TensorPtr zeros(
const Shape& size, Device
const& device =
Device::getDefault()) {
109 return device.zeros(size);
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); }
118 return device.constant(value);
120 inline TensorPtr constant(
double value, Device
const& device =
Device::getDefault()) {
121 return device.constant(value);
123 inline TensorPtr constant(InitializerTensor<float>&& value, Device
const& device =
Device::getDefault()) {
124 return device.constant(
std::move(value));
127 template <
typename T>
129 fromBytes(
const char* buffer,
size_t buflen,
const Shape& shape, Device
const& device =
Device::getDefault());
131 inline TensorPtr fromBytes<float>(
const char* buffer,
size_t buflen,
const Shape& shape, Device
const& device) {
132 return device.fromBytesFP32(buffer, buflen, shape);
135 TensorPtr clone(
const TensorPtr& tensor);
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.
static const Device & getDefault() noexcept
Returns the default device for this thread.
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.
The Tensor is a managed pointer to a tensor. It can generally be thought of like an std::unique_ptr<T...