From 8613dc830596d1b679c9ef703e3e9ea29388d6d2 Mon Sep 17 00:00:00 2001 From: Daniel Smilkov Date: Wed, 31 Jan 2018 22:01:41 -0500 Subject: [PATCH] Fix dl.clone to do a shallow copy. Also remove backend.clone() (#627) --- src/math/array_ops.ts | 3 +-- src/math/backends/backend.ts | 2 -- src/math/backends/backend_cpu.ts | 4 ---- src/math/backends/backend_webgl.ts | 25 ------------------------- src/math/backends/kernel_registry.ts | 4 ---- 5 files changed, 1 insertion(+), 37 deletions(-) diff --git a/src/math/array_ops.ts b/src/math/array_ops.ts index 497a481bc4..074359fa66 100644 --- a/src/math/array_ops.ts +++ b/src/math/array_ops.ts @@ -58,8 +58,7 @@ export class Ops { /** Creates a ndarray with the same values/shape as the specified ndarray. */ @operation static clone(x: T): T { - const newValues = util.copyTypedArray(x.dataSync(), x.dtype); - return NDArray.make(x.shape, {values: newValues}, x.dtype) as T; + return NDArray.make(x.shape, {dataId: x.dataId}, x.dtype) as T; } @operation diff --git a/src/math/backends/backend.ts b/src/math/backends/backend.ts index 9f62bfcfa9..5fb58e78a9 100644 --- a/src/math/backends/backend.ts +++ b/src/math/backends/backend.ts @@ -48,8 +48,6 @@ export interface MathBackend extends NDArrayStorage, BackendTimer { a: Array2D, b: Array2D, aOrientation: MatrixOrientation, bOrientation: MatrixOrientation): Array2D; - clone(ndarray: T): T; - slice1D(x: Array1D, begin: number, size: number): Array1D; slice2D(x: Array2D, begin: [number, number], size: [number, number]): Array2D; slice3D(x: Array3D, begin: [number, number, number], size: [ diff --git a/src/math/backends/backend_cpu.ts b/src/math/backends/backend_cpu.ts index 9f5cb6ad5a..a0fedae152 100644 --- a/src/math/backends/backend_cpu.ts +++ b/src/math/backends/backend_cpu.ts @@ -129,10 +129,6 @@ export class MathBackendCPU implements MathBackend { } } - clone(x: T): T { - return NDArray.make(x.shape, {values: new Float32Array(x.dataSync())}) as T; - } - slice1D(x: Array1D, begin: number, size: number): Array1D { const newVals = x.dataSync().slice(begin, begin + size); return Array1D.new(newVals, x.dtype); diff --git a/src/math/backends/backend_webgl.ts b/src/math/backends/backend_webgl.ts index 4fe9bfce6c..6d488ee89d 100644 --- a/src/math/backends/backend_webgl.ts +++ b/src/math/backends/backend_webgl.ts @@ -39,7 +39,6 @@ import {ConcatProgram} from './webgl/concat_gpu'; import {Conv2DDerBiasProgram, Conv2DDerFilterProgram, Conv2DDerInputProgram} from './webgl/conv_backprop_gpu'; import {Conv2DProgram} from './webgl/conv_gpu'; import {DepthwiseConv2DProgram} from './webgl/conv_gpu_depthwise'; -import {Copy2DProgram} from './webgl/copy_gpu'; import {FromPixelsProgram} from './webgl/from_pixels_gpu'; import {GatherProgram} from './webgl/gather_gpu'; import {GPGPUContext} from './webgl/gpgpu_context'; @@ -283,19 +282,6 @@ export class MathBackendWebGL implements MathBackend { return this.gpgpu; } - clone(x: T): T { - this.throwIfNoData(x.dataId); - this.uploadToGPU(x.dataId); - const {texShape} = this.texData[x.dataId]; - // Pretend the source was in logical shape that matches the texture shape. - const source = x.as2D(texShape[0], texShape[1]); - // Do the same for output. - const output = this.makeOutputArray(texShape, x.dtype); - this.copy2D(source, [0, 0], texShape, output, [0, 0], texShape); - // Get back to the original logical shape. - return output.reshape(x.shape) as T; - } - slice1D(x: Array1D, begin: number, size: number): Array1D { const program = new SliceProgram([size]); const customSetup = program.getCustomSetupFunc([begin]); @@ -330,17 +316,6 @@ export class MathBackendWebGL implements MathBackend { return this.compileAndRun(program, [x]); } - private copy2D( - source: Array2D, sourceBeginRowCol: [number, number], - sourceSizeRowCol: [number, number], dest: Array2D, - destBeginRowCol: [number, number], - destSizeRowCol: [number, number]): void { - const program = new Copy2DProgram(sourceSizeRowCol[1], destSizeRowCol[1]); - const customSetup = program.getCustomSetupFunc( - sourceBeginRowCol, destBeginRowCol, destSizeRowCol); - this.compileAndRun(program, [source], dest, customSetup); - } - // Concats 2d tensors along axis=1. See comments in MathBackend.concat(). concat(a: Array2D, b: Array2D): Array2D { const program = new ConcatProgram(a.shape, b.shape); diff --git a/src/math/backends/kernel_registry.ts b/src/math/backends/kernel_registry.ts index ded4518fff..39aa94d4ee 100644 --- a/src/math/backends/kernel_registry.ts +++ b/src/math/backends/kernel_registry.ts @@ -60,9 +60,6 @@ executeKernel, O extends return backend.matMul( config.inputs.a, config.inputs.b, config.args.aOrientation, config.args.bOrientation) as O; - } else if (kernelName === 'Clone') { - const config = inputAndArgs as UnaryNode['inputAndArgs']; - return backend.clone(config.inputs.x) as O; } else if (kernelName === 'Slice1D') { const config = inputAndArgs as Slice1DNode['inputAndArgs']; return backend.slice1D( @@ -363,7 +360,6 @@ executeKernel, O extends export interface KernelConfigRegistry { MatMul: MatMulNode; - Clone: UnaryNode; Slice1D: Slice1DNode; Slice2D: Slice2DNode; Slice3D: Slice3DNode;