Skip to content

Commit

Permalink
always create storage buffer with 16 bytes alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
fs-eire committed Sep 13, 2022
1 parent ad6bd01 commit aac2fc6
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions js/web/lib/onnxjs/backends/webgpu/gpu-data-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ interface DownloadCacheValue {
data: Promise<ArrayBufferLike>;
}

/**
* normalize the buffer size so that it fits the 128-bits (16 bytes) alignment.
*/
const calcNormalizedBufferSize = (size: number) => Math.ceil(size / 16) * 16;

class GpuDataManagerImpl implements GpuDataManager {
// GPU Data ID => GPU Data ( storage buffer )
storageCache: Map<GpuDataId, StorageCacheValue>;
Expand All @@ -62,10 +67,10 @@ class GpuDataManagerImpl implements GpuDataManager {
const srcArrayBuffer = data.buffer;
const srcOffset = data.byteOffset;
const srcLength = data.byteLength;
const size = calcNormalizedBufferSize(srcLength);

// create gpu buffer
const gpuBuffer =
this.device.createBuffer({mappedAtCreation: true, size: srcLength, usage: GPUBufferUsage.STORAGE});
const gpuBuffer = this.device.createBuffer({mappedAtCreation: true, size, usage: GPUBufferUsage.STORAGE});

// copy (upload) data
const arrayBuffer = gpuBuffer.getMappedRange();
Expand All @@ -89,11 +94,12 @@ class GpuDataManagerImpl implements GpuDataManager {

const elemCount = ShapeUtil.size(dims);
const bufferLength = sizeof(type) * elemCount;
const size = calcNormalizedBufferSize(bufferLength);

// create gpu buffer
const gpuBuffer =
// eslint-disable-next-line no-bitwise
this.device.createBuffer({size: bufferLength, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC});
this.device.createBuffer({size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC});

const gpuData = {id: Guid.create(), type: GpuDataType.default, buffer: gpuBuffer};
this.storageCache.set(gpuData.id, {gpuData, size: bufferLength});
Expand Down

0 comments on commit aac2fc6

Please sign in to comment.