From 27a3b90105c27135924a357fb72c4d6bfa5e33d7 Mon Sep 17 00:00:00 2001 From: Charlie Ruan <53290280+CharlieFRuan@users.noreply.github.com> Date: Sun, 26 May 2024 11:57:00 -0700 Subject: [PATCH] [Web] Add dtype and offset for CreateView in runtime (#17028) --- web/src/runtime.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/web/src/runtime.ts b/web/src/runtime.ts index 080003b4f0a9..fd7bcc6ab23b 100644 --- a/web/src/runtime.ts +++ b/web/src/runtime.ts @@ -519,11 +519,20 @@ export class NDArray implements Disposable { /** * Create a view of the array. * @param shape The shape of the view. + * @param dtype The data type of the new array. * @returns The new sliced ndarray. */ - view(shape: Array): NDArray { + view(shape: Array, dtype?: string): NDArray { const shapeArray = shape.map((value) => new Scalar(value, "int")); - return this.ctx.ndarrayCreateView(this, this.ctx.makeShapeTuple(...shapeArray)); + if (dtype === undefined) { + dtype = this.dtype; + } + return this.ctx.ndarrayCreateView( + this, + this.ctx.makeShapeTuple(...shapeArray), + this.dtype, + /*relative_byte_offset=*/ new Scalar(0, "int"), + ); } /**