Skip to content

Commit

Permalink
simpler webgpu copyin [pr]
Browse files Browse the repository at this point in the history
can directly construct the buffer instead of init then assign.
also put standard lib import together
  • Loading branch information
chenyuxyz committed Dec 12, 2024
1 parent db76586 commit 2ed673f
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions tinygrad/runtime/ops_webgpu.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import functools
import functools, struct
from tinygrad.device import Compiled, Allocator, Compiler
from tinygrad.renderer.wgsl import WGSLRenderer
from tinygrad.helpers import round_up
import wgpu
import struct

def create_uniform(wgpu_device, val) -> wgpu.GPUBuffer:
buf = wgpu_device.create_buffer(size=4, usage=wgpu.BufferUsage.UNIFORM | wgpu.BufferUsage.COPY_DST)
Expand Down Expand Up @@ -47,10 +46,8 @@ def __init__(self, dev): self.dev = dev
def _alloc(self, size: int, options):
return self.dev.create_buffer(size=round_up(size, 4), usage=wgpu.BufferUsage.STORAGE | wgpu.BufferUsage.COPY_DST | wgpu.BufferUsage.COPY_SRC)
def _copyin(self, dest, src: memoryview):
if src.nbytes % 4:
padded_src = bytearray(round_up(src.nbytes, 4))
padded_src[:src.nbytes] = src
self.dev.queue.write_buffer(dest, 0, padded_src if src.nbytes % 4 else src)
if (pad := src.nbytes % 4): src = memoryview(bytearray(src) + bytearray(pad))
self.dev.queue.write_buffer(dest, 0, src)
def _copyout(self, dest: memoryview, src):
buffer_data = self.dev.queue.read_buffer(src, 0)
dest[:] = buffer_data[:dest.nbytes] if src._nbytes > dest.nbytes else buffer_data
Expand Down

0 comments on commit 2ed673f

Please sign in to comment.