Skip to content

Commit

Permalink
Change Buffer usage
Browse files Browse the repository at this point in the history
  • Loading branch information
NALLEIN committed Jul 9, 2020
1 parent 359dac6 commit fa95ba0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 40 deletions.
6 changes: 3 additions & 3 deletions modules/dnn/src/webgpu/dawnWrapperTest/op_softmax_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ int main(int argc, char** argv )
float inputData1[] = {1, 2, 3, 4, 5, 6, 7, 8};
std::vector<int> shape = {2,4,1}; // outer_size * channels * channel_size

webgpu::Tensor input(inputData1, shape, wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopyDst, webgpu::Format::wFormatFp32);
webgpu::Tensor out(nullptr, shape, wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc, webgpu::Format::wFormatFp32);
webgpu::Tensor input(inputData1, shape, webgpu::Format::wFormatFp32);
webgpu::Tensor out(nullptr, shape, webgpu::Format::wFormatFp32);

webgpu::OpSoftmax op1(1, false);
op1.forward(input, out);

const void * maxdata = op1.max_tensor_->getBuffer()->MapReadAsyncAndWait();
printData(maxdata, op1.max_tensor_->size()/sizeof(float));
const void * sumdata = op1.sum_tensor_->getBuffer()->MapReadAsyncAndWait();
Expand Down
11 changes: 4 additions & 7 deletions modules/dnn/src/webgpu/include/tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ class Buffer;
class Tensor{
public:
Tensor(Format fmt = wFormatFp32);
Tensor( const void* data, size_t size_in_byte,
wgpu::BufferUsage usage,
Format fmt = wFormatInt32); //uniform buffer
Tensor( const void* data, std::vector<int>& shape,
wgpu::BufferUsage usage,
Tensor( const void* data, size_t size_in_byte); //uniform buffer
Tensor( const void* data, std::vector<int>& shape,
Format fmt = wFormatFp32);
const void* map();
const void* mapRead();
void unMap();
Shape getShape() const;
int dimSize(const int dim) const;
Expand All @@ -30,7 +27,7 @@ class Tensor{
Tensor reshape( const void* data, const std::vector<int>& shape,
bool alloc = false,
Format fmt = wFormatInvalid);
Tensor setUniform(const void * data, Format fmt = wFormatInt32);
Tensor fillData(const void * data);
int getFormat() const;
size_t size() const { return size_in_byte_; }
bool isEmpty() { return size_in_byte_ == 0 ? true : false; }
Expand Down
8 changes: 3 additions & 5 deletions modules/dnn/src/webgpu/src/op_softmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,15 @@ bool OpSoftmax::forward(Tensor& in, Tensor& out)
if (max_tensor_ == NULL || sum_tensor_ == NULL)
{
std::vector<int> shape = {outer_size_, channel_size_};
max_tensor_ = new Tensor(NULL, shape, wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc);
sum_tensor_ = new Tensor(NULL, shape, wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc);
max_tensor_ = new Tensor(NULL, shape);
sum_tensor_ = new Tensor(NULL, shape);
}
if(uniformTensor_ == NULL && needsUniform)
{
SoftmaxParam param = {channel_size_, outer_size_, channels_,
log_softmax_ == true ? 1 : 0};
uniformTensor_ = new Tensor((const void*) &param,
sizeof(SoftmaxParam),
wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst,
wFormatInt32);
sizeof(SoftmaxParam));
}

bindTensor( in, 0, bgEntries);
Expand Down
40 changes: 15 additions & 25 deletions modules/dnn/src/webgpu/src/tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,30 @@ Tensor::Tensor(Format fmt) : size_in_byte_(0), format_(fmt)
device_ = wDevice;
}

Tensor::Tensor(const void* data, size_t size_in_byte,
wgpu::BufferUsage usage, Format fmt)
Tensor::Tensor(const void* data, size_t size_in_byte)
{
createContext();
device_ = wDevice;
size_in_byte_ = size_in_byte;
usage_ = usage;
format_ = fmt;
setUniform(data, fmt);
usage_ = wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst;
if (device_ == nullptr)
{
CV_Error(Error::StsError, "device is NULL");
}
fillData(data);
}

Tensor::Tensor(const void* data, std::vector<int>& shape,
wgpu::BufferUsage usage, Format fmt)
Tensor::Tensor(const void* data, std::vector<int>& shape, Format fmt)
{
createContext();
device_ = wDevice;
size_in_byte_ = 0;
usage_ = usage;
usage_ = wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::CopyDst;
format_ = fmt;
reshape(data, shape);
}

const void* Tensor::map()
const void* Tensor::mapRead()
{
return buffer_->MapReadAsyncAndWait();

Expand Down Expand Up @@ -82,36 +83,25 @@ Tensor Tensor::reshape(const void* data, const std::vector<int>& shape,
if (alloc || new_size > size_in_byte_)
alloc = true;
size_in_byte_ = new_size;
if (alloc || !buffer_)
if(alloc)
{
// TODO: specific data type
buffer_.reset(new Buffer(device_, data, size_in_byte_, usage_));
return * this;
}
else if (data)
{
buffer_->setBufferData(data, size_in_byte_);
}
return *this;
fillData(data);
}

Tensor Tensor::setUniform(const void * data, Format fmt)
Tensor Tensor::fillData(const void * data)
{
if (device_ == nullptr)
{
CV_Error(Error::StsError, "device is NULL");
return *this;
}
if (checkFormat(fmt) && fmt != format_) format_ = fmt;
if (!buffer_)
{
// TODO: specific data type
buffer_.reset(new Buffer(device_, data, size_in_byte_, usage_));
}
else if (data)
{
buffer_->setBufferData(data, size_in_byte_);
}
return *this;
return * this;
}

int Tensor::getFormat() const
Expand Down

0 comments on commit fa95ba0

Please sign in to comment.