mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-12-22 14:24:22 +00:00
Add default dtype to backend
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include "shape.hpp"
|
||||
#include "tensor.hpp"
|
||||
|
||||
namespace CUDANet {
|
||||
|
||||
@@ -22,7 +24,14 @@ class BackendFactory {
|
||||
};
|
||||
|
||||
class Backend {
|
||||
protected:
|
||||
std::optional<DType> default_dtype;
|
||||
public:
|
||||
|
||||
virtual bool supports_dtype(DType dtype) const = 0;
|
||||
virtual void set_default_dtype(DType dtype) = 0;
|
||||
virtual DType get_default_dtype() const = 0;
|
||||
|
||||
// Memory management
|
||||
virtual void* allocate(size_t bytes) = 0;
|
||||
virtual void deallocate(void* ptr) = 0;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdio>
|
||||
#include <set>
|
||||
|
||||
#include "backend.hpp"
|
||||
#include "tensor.hpp"
|
||||
@@ -29,9 +30,14 @@ namespace CUDANet::Backends {
|
||||
class CUDA : public Backend {
|
||||
private:
|
||||
int device_id;
|
||||
std::set<DType> supported_dtypes;
|
||||
public:
|
||||
CUDA(const BackendConfig& config);
|
||||
|
||||
bool supports_dtype(DType dtype) const override;
|
||||
void set_default_dtype(DType dtype) override;
|
||||
DType get_default_dtype() const override;
|
||||
|
||||
static bool is_cuda_available();
|
||||
void initialize();
|
||||
|
||||
|
||||
@@ -66,6 +66,12 @@ struct Shape {
|
||||
__host__ bool operator!=(const Shape& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
__host__ __device__ bool empty() const {
|
||||
return ndim == 0;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
std::string format_shape(const Shape& shape) {
|
||||
|
||||
@@ -16,11 +16,14 @@ enum class DType
|
||||
// INT32, // Not implemented yet
|
||||
};
|
||||
|
||||
size_t dtype_size(DType dtype);
|
||||
|
||||
class Tensor
|
||||
{
|
||||
public:
|
||||
|
||||
Tensor() = default;
|
||||
Tensor(Shape shape, CUDANet::Backend* backend);
|
||||
Tensor(Shape shape, DType dtype, CUDANet::Backend* backend);
|
||||
|
||||
Tensor(Tensor&& other) noexcept;
|
||||
@@ -30,6 +33,8 @@ public:
|
||||
|
||||
~Tensor();
|
||||
|
||||
DType get_dtype();
|
||||
|
||||
size_t size() const;
|
||||
size_t numel() const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user