Initial implementation of tensor working compiling
This commit is contained in:
parent
b81896a780
commit
ad18c2e546
12 changed files with 417 additions and 210 deletions
|
|
@ -762,7 +762,7 @@ class Shader
|
|||
* GLSL compiler
|
||||
* @return The compiled SPIR-V binary in unsigned int32 format
|
||||
*/
|
||||
static std::vector<uint32_t> compileSources(
|
||||
static std::vector<uint32_t> compile_sources(
|
||||
const std::vector<std::string>& sources,
|
||||
const std::vector<std::string>& files = {},
|
||||
const std::string& entryPoint = "main",
|
||||
|
|
@ -783,7 +783,7 @@ class Shader
|
|||
* GLSL compiler
|
||||
* @return The compiled SPIR-V binary in unsigned int32 format
|
||||
*/
|
||||
static std::vector<uint32_t> compileSource(
|
||||
static std::vector<uint32_t> compile_source(
|
||||
const std::string& source,
|
||||
const std::string& entryPoint = "main",
|
||||
std::vector<std::pair<std::string, std::string>> definitions = {},
|
||||
|
|
@ -818,6 +818,14 @@ class Tensor
|
|||
eHost = 1, ///< Type is host memory, source and destination
|
||||
eStorage = 2, ///< Type is Device memory (only)
|
||||
};
|
||||
enum class TensorDataTypes
|
||||
{
|
||||
eBool = 0,
|
||||
eInt = 1,
|
||||
eUnsignedInt = 2,
|
||||
eFloat = 3,
|
||||
eDouble = 4,
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor with data provided which would be used to create the
|
||||
|
|
@ -831,14 +839,78 @@ class Tensor
|
|||
*/
|
||||
Tensor(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
|
||||
std::shared_ptr<vk::Device> device,
|
||||
const std::vector<float>& data,
|
||||
void* data,
|
||||
uint32_t elementTotalCount,
|
||||
uint32_t elementMemorySize,
|
||||
const TensorDataTypes& dataType,
|
||||
const TensorTypes& tensorType = TensorTypes::eDevice);
|
||||
|
||||
/**
|
||||
* Destructor which is in charge of freeing vulkan resources unless they
|
||||
* have been provided externally.
|
||||
*/
|
||||
~Tensor();
|
||||
virtual ~Tensor();
|
||||
|
||||
/**
|
||||
* Returns the size/magnitude of the Tensor, which will be the total number
|
||||
* of elements across all dimensions
|
||||
*
|
||||
* @return Unsigned integer representing the total number of elements
|
||||
*/
|
||||
// TODO: move to cpp
|
||||
virtual uint32_t size() {
|
||||
return this->mSize;
|
||||
}
|
||||
|
||||
// TODO: move to cpp
|
||||
virtual uint32_t dataTypeMemorySize() {
|
||||
return this->mDataTypeMemorySize;
|
||||
}
|
||||
|
||||
// TODO: move to cpp
|
||||
virtual uint32_t memorySize() {
|
||||
return this->mSize * this->mDataTypeMemorySize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the underlying data type of the Tensor
|
||||
*
|
||||
* @return Data type of tensor of type kp::Tensor::TensorDataTypes
|
||||
*/
|
||||
virtual TensorDataTypes dataType() {
|
||||
return this->mDataType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps data from the Host Visible GPU memory into the data vector. It
|
||||
* requires the Tensor to be of staging type for it to work.
|
||||
*/
|
||||
virtual void mapDataFromHostMemory();
|
||||
/**
|
||||
* Maps data from the data vector into the Host Visible GPU memory. It
|
||||
* requires the tensor to be of staging type for it to work.
|
||||
*/
|
||||
virtual void mapDataIntoHostMemory();
|
||||
|
||||
// TODO: Decide whether this is one we prefer to have also overriden in the underlying tensorView
|
||||
// TODO: move to cpp
|
||||
virtual void getRawData(void* data) {
|
||||
this->rawMapDataFromHostMemory(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets / resets the vector data of the tensor. This function does not
|
||||
* perform any copies into GPU memory and is only performed on the host.
|
||||
*/
|
||||
virtual void setRawData(void* data, uint32_t elementTotalCount, uint32_t elementMemorySize) {
|
||||
if (elementTotalCount * elementMemorySize != this->memorySize()) {
|
||||
throw std::runtime_error(
|
||||
"Kompute Tensor Cannot set data of different sizes");
|
||||
}
|
||||
this->mSize = elementTotalCount;
|
||||
this->mDataTypeMemorySize = elementMemorySize;
|
||||
this->rawMapDataIntoHostMemory(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to trigger reinitialisation of the tensor buffer and memory with
|
||||
|
|
@ -847,8 +919,9 @@ class Tensor
|
|||
* @param data Vector of data to use to initialise vector from
|
||||
* @param tensorType The type to use for the tensor
|
||||
*/
|
||||
void rebuild(const std::vector<float>& data,
|
||||
TensorTypes tensorType = TensorTypes::eDevice);
|
||||
void rebuild(void* data,
|
||||
uint32_t elementTotalCount,
|
||||
uint32_t elementMemorySize);
|
||||
|
||||
/**
|
||||
* Destroys and frees the GPU resources which include the buffer and memory.
|
||||
|
|
@ -862,32 +935,6 @@ class Tensor
|
|||
*/
|
||||
bool isInit();
|
||||
|
||||
/**
|
||||
* Returns the vector of data currently contained by the Tensor. It is
|
||||
* important to ensure that there is no out-of-sync data with the GPU
|
||||
* memory.
|
||||
*
|
||||
* @return Reference to vector of elements representing the data in the
|
||||
* tensor.
|
||||
*/
|
||||
std::vector<float>& data();
|
||||
/**
|
||||
* Overrides the subscript operator to expose the underlying data's
|
||||
* subscript operator which in this case would be its underlying
|
||||
* vector's.
|
||||
*
|
||||
* @param i The index where the element will be returned from.
|
||||
* @return Returns the element in the position requested.
|
||||
*/
|
||||
float& operator[](int index);
|
||||
/**
|
||||
* Returns the size/magnitude of the Tensor, which will be the total number
|
||||
* of elements across all dimensions
|
||||
*
|
||||
* @return Unsigned integer representing the total number of elements
|
||||
*/
|
||||
uint32_t size();
|
||||
|
||||
/**
|
||||
* Retrieve the tensor type of the Tensor
|
||||
*
|
||||
|
|
@ -895,12 +942,6 @@ class Tensor
|
|||
*/
|
||||
TensorTypes tensorType();
|
||||
|
||||
/**
|
||||
* Sets / resets the vector data of the tensor. This function does not
|
||||
* perform any copies into GPU memory and is only performed on the host.
|
||||
*/
|
||||
void setData(const std::vector<float>& data);
|
||||
|
||||
/**
|
||||
* Records a copy from the memory of the tensor provided to the current
|
||||
* thensor. This is intended to pass memory into a processing, to perform
|
||||
|
|
@ -963,17 +1004,57 @@ class Tensor
|
|||
* @return Descriptor buffer info with own buffer
|
||||
*/
|
||||
vk::DescriptorBufferInfo constructDescriptorBufferInfo();
|
||||
/**
|
||||
* Maps data from the Host Visible GPU memory into the data vector. It
|
||||
* requires the Tensor to be of staging type for it to work.
|
||||
*/
|
||||
void mapDataFromHostMemory();
|
||||
/**
|
||||
* Maps data from the data vector into the Host Visible GPU memory. It
|
||||
* requires the tensor to be of staging type for it to work.
|
||||
*/
|
||||
void mapDataIntoHostMemory();
|
||||
|
||||
protected:
|
||||
void rawMapDataFromHostMemory(void* data) {
|
||||
|
||||
KP_LOG_DEBUG("Kompute Tensor mapping data from host buffer");
|
||||
|
||||
std::shared_ptr<vk::DeviceMemory> hostVisibleMemory = nullptr;
|
||||
|
||||
if (this->mTensorType == TensorTypes::eHost) {
|
||||
hostVisibleMemory = this->mPrimaryMemory;
|
||||
} else if (this->mTensorType == TensorTypes::eDevice) {
|
||||
hostVisibleMemory = this->mStagingMemory;
|
||||
} else {
|
||||
KP_LOG_WARN(
|
||||
"Kompute Tensor mapping data not supported on storage tensor");
|
||||
return;
|
||||
}
|
||||
|
||||
vk::DeviceSize bufferSize = this->memorySize();
|
||||
void* mapped = this->mDevice->mapMemory(
|
||||
*hostVisibleMemory, 0, bufferSize, vk::MemoryMapFlags());
|
||||
vk::MappedMemoryRange mappedMemoryRange(*hostVisibleMemory, 0, bufferSize);
|
||||
this->mDevice->invalidateMappedMemoryRanges(mappedMemoryRange);
|
||||
memcpy(data, mapped, bufferSize);
|
||||
this->mDevice->unmapMemory(*hostVisibleMemory);
|
||||
}
|
||||
|
||||
void rawMapDataIntoHostMemory(void* data) {
|
||||
KP_LOG_DEBUG("Kompute Tensor mapping data into host buffer");
|
||||
|
||||
std::shared_ptr<vk::DeviceMemory> hostVisibleMemory = nullptr;
|
||||
|
||||
if (this->mTensorType == TensorTypes::eHost) {
|
||||
hostVisibleMemory = this->mPrimaryMemory;
|
||||
} else if (this->mTensorType == TensorTypes::eDevice) {
|
||||
hostVisibleMemory = this->mStagingMemory;
|
||||
} else {
|
||||
KP_LOG_WARN(
|
||||
"Kompute Tensor mapping data not supported on storage tensor");
|
||||
return;
|
||||
}
|
||||
|
||||
vk::DeviceSize bufferSize = this->memorySize();
|
||||
|
||||
void* mapped = this->mDevice->mapMemory(
|
||||
*hostVisibleMemory, 0, bufferSize, vk::MemoryMapFlags());
|
||||
memcpy(mapped, data, bufferSize);
|
||||
vk::MappedMemoryRange mappedRange(*hostVisibleMemory, 0, bufferSize);
|
||||
this->mDevice->flushMappedMemoryRanges(1, &mappedRange);
|
||||
this->mDevice->unmapMemory(*hostVisibleMemory);
|
||||
}
|
||||
private:
|
||||
// -------------- NEVER OWNED RESOURCES
|
||||
std::shared_ptr<vk::PhysicalDevice> mPhysicalDevice;
|
||||
|
|
@ -990,9 +1071,10 @@ class Tensor
|
|||
bool mFreeStagingMemory = false;
|
||||
|
||||
// -------------- ALWAYS OWNED RESOURCES
|
||||
std::vector<float> mData;
|
||||
|
||||
TensorTypes mTensorType = TensorTypes::eDevice;
|
||||
TensorTypes mTensorType;
|
||||
TensorDataTypes mDataType;
|
||||
uint32_t mSize;
|
||||
uint32_t mDataTypeMemorySize;
|
||||
|
||||
void allocateMemoryCreateGPUResources(); // Creates the vulkan buffer
|
||||
void createBuffer(std::shared_ptr<vk::Buffer> buffer,
|
||||
|
|
@ -1012,9 +1094,98 @@ class Tensor
|
|||
vk::MemoryPropertyFlags getPrimaryMemoryPropertyFlags();
|
||||
vk::BufferUsageFlags getStagingBufferUsageFlags();
|
||||
vk::MemoryPropertyFlags getStagingMemoryPropertyFlags();
|
||||
uint64_t memorySize();
|
||||
|
||||
};
|
||||
|
||||
// TODO: Limit T to be only float, bool, double, etc
|
||||
template <typename T>
|
||||
class TensorView: public Tensor
|
||||
{
|
||||
public:
|
||||
TensorView(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
|
||||
std::shared_ptr<vk::Device> device,
|
||||
const std::vector<T>& data,
|
||||
const TensorTypes& tensorType = TensorTypes::eDevice)
|
||||
: Tensor(physicalDevice, device, (void*)data.data(), data.size(), sizeof(T), this->dataType())
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
~TensorView() {
|
||||
|
||||
}
|
||||
|
||||
void rebuild(const std::vector<T>& data,
|
||||
TensorTypes tensorType = TensorTypes::eDevice) {
|
||||
|
||||
this->mData = data;
|
||||
Tensor::rebuild(data.data(), data.size(), sizeof(T));
|
||||
}
|
||||
|
||||
std::vector<T>& data() {
|
||||
return this->mData;
|
||||
}
|
||||
|
||||
T& operator[](int index) {
|
||||
return this->mData[index];
|
||||
}
|
||||
|
||||
void setData(const std::vector<T>& data) {
|
||||
|
||||
if (data.size() != this->mData.size()) {
|
||||
throw std::runtime_error(
|
||||
"Kompute TensorView Cannot set data of different sizes");
|
||||
}
|
||||
|
||||
this->mData = data;
|
||||
|
||||
Tensor::setRawData(this->mData.data(), this->mData.size(), sizeof(T));
|
||||
}
|
||||
|
||||
void setRawData(void* data, uint32_t elementTotalCount, uint32_t elementMemorySize) override
|
||||
{
|
||||
assert(elementMemorySize == sizeof(T));
|
||||
|
||||
this->mData = { (T*)data, ((T*)data) + elementTotalCount };
|
||||
Tensor::setRawData(this->mData.data(), this->mData.size(), sizeof(T));
|
||||
}
|
||||
|
||||
TensorDataTypes dataType() override;
|
||||
|
||||
uint32_t size() override {
|
||||
return this->mData.size();
|
||||
}
|
||||
|
||||
uint32_t memorySize() override {
|
||||
return this->mData.size() * sizeof(T);
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps data from the Host Visible GPU memory into the data vector. It
|
||||
* requires the Tensor to be of staging type for it to work.
|
||||
*/
|
||||
void mapDataFromHostMemory() override {
|
||||
KP_LOG_DEBUG("Kompute TensorView mapDataFromHostMemory copying data");
|
||||
|
||||
this->rawMapDataFromHostMemory(this->mData.data());
|
||||
}
|
||||
/**
|
||||
* Maps data from the data vector into the Host Visible GPU memory. It
|
||||
* requires the tensor to be of staging type for it to work.
|
||||
*/
|
||||
void mapDataIntoHostMemory() override {
|
||||
KP_LOG_DEBUG("Kompute TensorView mapDataIntoHostMemory copying data");
|
||||
|
||||
this->rawMapDataIntoHostMemory(this->mData.data());
|
||||
}
|
||||
|
||||
private:
|
||||
// -------------- ALWAYS OWNED RESOURCES
|
||||
std::vector<T> mData;
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // End namespace kp
|
||||
|
||||
namespace kp {
|
||||
|
|
@ -1883,9 +2054,22 @@ class Manager
|
|||
* @param tensorType The type of tensor to initialize
|
||||
* @returns Shared pointer with initialised tensor
|
||||
*/
|
||||
std::shared_ptr<Tensor> tensor(
|
||||
const std::vector<float>& data,
|
||||
Tensor::TensorTypes tensorType = Tensor::TensorTypes::eDevice);
|
||||
template <typename T>
|
||||
std::shared_ptr<TensorView<T>> tensor(
|
||||
const std::vector<T>& data,
|
||||
Tensor::TensorTypes tensorType = Tensor::TensorTypes::eDevice)
|
||||
{
|
||||
KP_LOG_DEBUG("Kompute Manager tensor creation triggered");
|
||||
|
||||
std::shared_ptr<TensorView<T>> tensor{ new kp::TensorView<T>(
|
||||
this->mPhysicalDevice, this->mDevice, data, tensorType) };
|
||||
|
||||
if (this->mManageResources) {
|
||||
this->mManagedTensors.push_back(tensor);
|
||||
}
|
||||
|
||||
return tensor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a managed algorithm that will be destroyed by this manager
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue