Comparing version 0.2.3 to 0.2.4
@@ -0,3 +1,8 @@ | ||
import { Initializer } from '../initializers'; | ||
import { NDArray } from '../math/ndarray'; | ||
import { GraphLayers } from './graph_layers'; | ||
export declare class GraphLayers { | ||
private g; | ||
constructor(g: Graph); | ||
dense(name: string, x: Tensor, units: number, activation?: ((x: Tensor) => Tensor) | null, useBias?: boolean, kernelInitializer?: Initializer, biasInitializer?: Initializer): Tensor; | ||
} | ||
export declare class Graph { | ||
@@ -4,0 +9,0 @@ layers: GraphLayers; |
@@ -13,2 +13,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var initializers_1 = require("../initializers"); | ||
var concat3d_util = require("../math/concat3d_util"); | ||
@@ -18,7 +19,29 @@ var conv_util = require("../math/conv_util"); | ||
var util = require("../util"); | ||
var graph_layers_1 = require("./graph_layers"); | ||
var GraphLayers = (function () { | ||
function GraphLayers(g) { | ||
this.g = g; | ||
} | ||
GraphLayers.prototype.dense = function (name, x, units, activation, useBias, kernelInitializer, biasInitializer) { | ||
if (activation === void 0) { activation = null; } | ||
if (useBias === void 0) { useBias = true; } | ||
if (kernelInitializer === void 0) { kernelInitializer = new initializers_1.VarianceScalingInitializer(); } | ||
if (biasInitializer === void 0) { biasInitializer = new initializers_1.ZerosInitializer(); } | ||
var weights = this.g.variable(name + '-weights', kernelInitializer.initialize([x.shape[0], units], x.shape[0], units)); | ||
var out = this.g.matmul(x, weights); | ||
if (useBias) { | ||
var bias = this.g.variable(name + '-bias', biasInitializer.initialize([units], x.shape[0], units)); | ||
out = this.g.add(out, bias); | ||
} | ||
if (activation != null) { | ||
out = activation(out); | ||
} | ||
return out; | ||
}; | ||
return GraphLayers; | ||
}()); | ||
exports.GraphLayers = GraphLayers; | ||
var Graph = (function () { | ||
function Graph() { | ||
this.nodes = []; | ||
this.layers = new graph_layers_1.GraphLayers(this); | ||
this.layers = new GraphLayers(this); | ||
} | ||
@@ -25,0 +48,0 @@ Graph.prototype.variable = function (name, data) { |
@@ -8,5 +8,4 @@ import { NDArrayMath } from '../../math/math'; | ||
protected learningRate: number; | ||
protected momentum: number; | ||
private gamma; | ||
constructor(learningRate: number, momentum: number, gamma: number, specifiedVariableList?: Node[]); | ||
constructor(learningRate: number, gamma: number, specifiedVariableList?: Node[]); | ||
beforeBatch(math: NDArrayMath, batchSize: number, runtime: SessionRuntime, activationArrayMap: TensorArrayMap, gradientArrayMap: SummedTensorArrayMap): void; | ||
@@ -16,5 +15,4 @@ afterBatch(math: NDArrayMath, batchSize: number, runtime: SessionRuntime, activationArrayMap: TensorArrayMap, gradientArrayMap: SummedTensorArrayMap): void; | ||
private accumulatedSquaredGradients; | ||
private m; | ||
private eps; | ||
private g; | ||
} |
@@ -18,9 +18,7 @@ "use strict"; | ||
__extends(RMSPropOptimizer, _super); | ||
function RMSPropOptimizer(learningRate, momentum, gamma, specifiedVariableList) { | ||
function RMSPropOptimizer(learningRate, gamma, specifiedVariableList) { | ||
var _this = _super.call(this, learningRate, specifiedVariableList) || this; | ||
_this.learningRate = learningRate; | ||
_this.momentum = momentum; | ||
_this.gamma = gamma; | ||
_this.accumulatedSquaredGradients = new tensor_array_map_1.TensorArrayMap(); | ||
_this.m = ndarray_1.Scalar.new(momentum); | ||
_this.eps = ndarray_1.Scalar.new(1e-6); | ||
@@ -61,3 +59,2 @@ _this.g = ndarray_1.Scalar.new(_this.gamma); | ||
_super.prototype.dispose.call(this); | ||
this.m.dispose(); | ||
this.eps.dispose(); | ||
@@ -64,0 +61,0 @@ this.g.dispose(); |
@@ -11,2 +11,3 @@ import * as xhr_dataset from './data/xhr-dataset'; | ||
export { XhrDataset, XhrDatasetConfig, XhrModelConfig } from './data/xhr-dataset'; | ||
export { ENV, Features } from './environment'; | ||
export { Graph, Tensor } from './graph/graph'; | ||
@@ -13,0 +14,0 @@ export { AdagradOptimizer } from './graph/optimizers/adagrad_optimizer'; |
@@ -24,2 +24,4 @@ "use strict"; | ||
exports.XhrDataset = xhr_dataset_1.XhrDataset; | ||
var environment_1 = require("./environment"); | ||
exports.ENV = environment_1.ENV; | ||
var graph_1 = require("./graph/graph"); | ||
@@ -26,0 +28,0 @@ exports.Graph = graph_1.Graph; |
@@ -43,3 +43,3 @@ "use strict"; | ||
function NDArrayMathGPU(gpgpu, safeMode) { | ||
if (safeMode === void 0) { safeMode = true; } | ||
if (safeMode === void 0) { safeMode = false; } | ||
var _this = _super.call(this, safeMode) || this; | ||
@@ -46,0 +46,0 @@ _this.binaryCache = {}; |
@@ -36,2 +36,3 @@ import { GPGPUContext } from './webgl/gpgpu_context'; | ||
getValues(): Float32Array; | ||
getValuesAsync(): Promise<Float32Array>; | ||
private uploadToGPU(preferredTexShape?); | ||
@@ -38,0 +39,0 @@ getTexture(preferredShapeRC?: [number, number]): WebGLTexture; |
@@ -13,2 +13,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var environment_1 = require("../environment"); | ||
var util = require("../util"); | ||
@@ -167,2 +168,19 @@ var webgl_util = require("./webgl/webgl_util"); | ||
}; | ||
NDArray.prototype.getValuesAsync = function () { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
if (_this.data.values != null) { | ||
resolve(_this.data.values); | ||
return; | ||
} | ||
if (!environment_1.ENV.get('WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_ENABLED')) { | ||
resolve(_this.getValues()); | ||
return; | ||
} | ||
var queryFn = function () { }; | ||
exports.GPGPU.runQuery(queryFn).then(function () { | ||
resolve(_this.getValues()); | ||
}); | ||
}); | ||
}; | ||
NDArray.prototype.uploadToGPU = function (preferredTexShape) { | ||
@@ -169,0 +187,0 @@ throwIfGPUNotInitialized(); |
@@ -37,2 +37,5 @@ import { WebGLLoseContextExtension } from './webgl_util'; | ||
blockUntilAllProgramsCompleted(): void; | ||
runQuery(queryFn: () => void): Promise<number>; | ||
private runQueryWebGL2(benchmark); | ||
private runQueryWebGL1(benchmark); | ||
private downloadMatrixDriver(texture, downloadAndDecode); | ||
@@ -39,0 +42,0 @@ private setOutputMatrixTextureDriver(outputMatrixTextureMaybePacked, width, height); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var environment_1 = require("../../environment"); | ||
var util = require("../../util"); | ||
var gpgpu_util = require("./gpgpu_util"); | ||
@@ -18,3 +20,3 @@ var tex_util = require("./tex_util"); | ||
} | ||
if (!webgl_util.isWebGL2Enabled()) { | ||
if (environment_1.ENV.get('WEBGL_VERSION') === 1) { | ||
this.textureFloatExtension = | ||
@@ -181,2 +183,58 @@ webgl_util.getExtensionOrThrow(this.gl, 'OES_texture_float'); | ||
}; | ||
GPGPUContext.prototype.runQuery = function (queryFn) { | ||
if (environment_1.ENV.get('WEBGL_VERSION') === 2) { | ||
return this.runQueryWebGL2(queryFn); | ||
} | ||
return this.runQueryWebGL1(queryFn); | ||
}; | ||
GPGPUContext.prototype.runQueryWebGL2 = function (benchmark) { | ||
var _this = this; | ||
var ext = webgl_util.getExtensionOrThrow(this.gl, 'EXT_disjoint_timer_query_webgl2'); | ||
var query = this.gl.createQuery(); | ||
this.gl.beginQuery(ext.TIME_ELAPSED_EXT, query); | ||
benchmark(); | ||
this.gl.endQuery(ext.TIME_ELAPSED_EXT); | ||
return new Promise(function (resolve, reject) { | ||
var queryGPU = function () { | ||
var available = _this.gl | ||
.getQueryParameter(query, _this.gl.QUERY_RESULT_AVAILABLE); | ||
var disjoint = _this.gl.getParameter(ext.GPU_DISJOINT_EXT); | ||
return available && !disjoint; | ||
}; | ||
var getTimeElapsed = function () { | ||
var timeElapsedNanos = _this.gl | ||
.getQueryParameter(query, _this.gl.QUERY_RESULT); | ||
resolve(timeElapsedNanos / 1000000); | ||
}; | ||
var resolveWithWarning = function () { | ||
console.warn('Disjoint query timer never available.'); | ||
resolve(-1); | ||
}; | ||
util.repeatedTry(queryGPU).then(getTimeElapsed).catch(resolveWithWarning); | ||
}); | ||
}; | ||
GPGPUContext.prototype.runQueryWebGL1 = function (benchmark) { | ||
var _this = this; | ||
var ext = webgl_util.getExtensionOrThrow(this.gl, 'EXT_disjoint_timer_query'); | ||
var query = ext.createQueryEXT(); | ||
ext.beginQueryEXT(ext.TIME_ELAPSED_EXT, query); | ||
benchmark(); | ||
ext.endQueryEXT(ext.TIME_ELAPSED_EXT); | ||
return new Promise(function (resolve, reject) { | ||
var queryGPU = function () { | ||
var available = ext.getQueryObjectEXT(query, ext.QUERY_RESULT_AVAILABLE_EXT); | ||
var disjoint = _this.gl.getParameter(ext.GPU_DISJOINT_EXT); | ||
return available && !disjoint; | ||
}; | ||
var getTimeElapsed = function () { | ||
var timeElapsedNanos = ext.getQueryObjectEXT(query, ext.QUERY_RESULT_EXT); | ||
resolve(timeElapsedNanos / 1000000); | ||
}; | ||
var resolveWithWarning = function () { | ||
console.warn('Disjoint query timer never available.'); | ||
resolve(-1); | ||
}; | ||
util.repeatedTry(queryGPU).then(getTimeElapsed).catch(resolveWithWarning); | ||
}); | ||
}; | ||
GPGPUContext.prototype.downloadMatrixDriver = function (texture, downloadAndDecode) { | ||
@@ -183,0 +241,0 @@ this.throwIfDisposed(); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var environment_1 = require("../../environment"); | ||
var tex_util = require("./tex_util"); | ||
@@ -54,3 +55,3 @@ var webgl_util = require("./webgl_util"); | ||
function getTextureInternalFormat(gl, numChannels) { | ||
if (webgl_util.isWebGL2Enabled()) { | ||
if (environment_1.ENV.get('WEBGL_VERSION') === 2) { | ||
if (numChannels === 4) { | ||
@@ -64,3 +65,3 @@ return gl.RGBA32F; | ||
function getTextureFormat(gl, numChannels) { | ||
if (webgl_util.isWebGL2Enabled()) { | ||
if (environment_1.ENV.get('WEBGL_VERSION') === 2) { | ||
if (numChannels === 4) { | ||
@@ -67,0 +68,0 @@ return gl.RGBA; |
@@ -7,3 +7,3 @@ import { GPGPUProgram } from './gpgpu_math'; | ||
userCode: string; | ||
constructor(aSize: number); | ||
constructor(size: number); | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var LogSumExpProgram = (function () { | ||
function LogSumExpProgram(aSize) { | ||
function LogSumExpProgram(size) { | ||
this.variableNames = ['A']; | ||
this.params = []; | ||
this.outputShape = []; | ||
this.userCode = "\n void main() {\n float aMax = getAFlat(0);\n for (int i = 0; i < " + aSize + "; i++) {\n aMax = max(aMax, getAFlat(i));\n }\n\n float expSum = 0.0;\n for (int i = 0; i < " + aSize + "; i++) {\n expSum += exp(getAFlat(i) - aMax);\n }\n\n setOutput(aMax + log(expSum));\n }\n "; | ||
var sizeNearestVec4 = Math.floor(size / 4) * 4; | ||
var sizeVec4Remainder = size % 4; | ||
var r1 = sizeNearestVec4; | ||
var r2 = sizeNearestVec4 + 1; | ||
var r3 = sizeNearestVec4 + 2; | ||
this.userCode = "\n const vec2 ones2 = vec2(1, 1);\n const vec3 ones3 = vec3(1, 1, 1);\n const vec4 ones4 = vec4(1, 1, 1, 1);\n\n void main() {\n vec4 maxVec = vec4(getAFlat(0));\n for (int i = 0; i < " + sizeNearestVec4 + "; i += 4) {\n vec4 aVec = vec4(getAFlat(i), getAFlat(i+1),\n getAFlat(i+2), getAFlat(i+3));\n maxVec = max(maxVec, aVec);\n }\n if (" + (sizeVec4Remainder === 1) + ") {\n maxVec = max(maxVec, vec4(maxVec.xyz, getAFlat(" + r1 + ")));\n } else if (" + (sizeVec4Remainder === 2) + ") {\n vec2 aVec = vec2(getAFlat(" + r1 + "), getAFlat(" + r2 + "));\n maxVec = max(maxVec, vec4(maxVec.xy, aVec));\n } else if (" + (sizeVec4Remainder === 3) + ") {\n vec3 aVec = vec3(getAFlat(" + r1 + "), getAFlat(" + r2 + "), getAFlat(" + r3 + "));\n maxVec = max(maxVec, vec4(maxVec.x, aVec));\n }\n float finalMax = max(maxVec.x, max(maxVec.y, max(maxVec.z, maxVec.w)));\n\n float expSum = 0.0;\n for (int i = 0; i < " + sizeNearestVec4 + "; i += 4) {\n vec4 aVec = vec4(getAFlat(i), getAFlat(i+1),\n getAFlat(i+2), getAFlat(i+3));\n expSum += dot(ones4, exp(aVec - finalMax));\n }\n if (" + (sizeVec4Remainder === 1) + ") {\n expSum += exp(getAFlat(" + r1 + ") - finalMax);\n } else if (" + (sizeVec4Remainder === 2) + ") {\n vec2 aVec = vec2(getAFlat(" + r1 + "), getAFlat(" + r2 + "));\n expSum += dot(ones2, exp(aVec - finalMax));\n } else if (" + (sizeVec4Remainder === 3) + ") {\n vec3 aVec = vec3(getAFlat(" + r1 + "), getAFlat(" + r2 + "), getAFlat(" + r3 + "));\n expSum += dot(ones3, exp(aVec - finalMax));\n }\n\n setOutput(finalMax + log(expSum));\n }\n "; | ||
} | ||
@@ -10,0 +15,0 @@ return LogSumExpProgram; |
@@ -7,3 +7,3 @@ import { GPGPUProgram } from './gpgpu_math'; | ||
userCode: string; | ||
constructor(aSize: number, opType: 'min' | 'max'); | ||
constructor(size: number, op: 'min' | 'max'); | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var MinMaxProgram = (function () { | ||
function MinMaxProgram(aSize, opType) { | ||
function MinMaxProgram(size, op) { | ||
this.variableNames = ['A']; | ||
this.outputShape = []; | ||
this.params = [opType]; | ||
this.userCode = "\n void main() {\n float value = getAFlat(0);\n for (int i = 0; i < " + aSize + "; i++) {\n float candidate = getAFlat(i);\n if (isNaN(candidate)) {\n setOutput(candidate);\n return;\n }\n value = " + opType + "(value, candidate);\n }\n setOutput(value);\n }\n "; | ||
this.params = [op]; | ||
var sizeNearestVec4 = Math.floor(size / 4) * 4; | ||
var sizeVec4Remainder = size % 4; | ||
var r1 = sizeNearestVec4; | ||
var r2 = sizeNearestVec4 + 1; | ||
var r3 = sizeNearestVec4 + 2; | ||
this.userCode = "\n void main() {\n vec4 bestVec = vec4(getAFlat(0));\n for (int i = 0; i < " + sizeNearestVec4 + "; i += 4) {\n vec4 aVec = vec4(getAFlat(i), getAFlat(i+1),\n getAFlat(i+2), getAFlat(i+3));\n if (hasNaN(aVec)) {\n setOutput(getNaN(aVec));\n return;\n }\n bestVec = " + op + "(bestVec, aVec);\n }\n vec4 aVec;\n if (" + (sizeVec4Remainder === 1) + ") {\n aVec = vec4(bestVec.xyz, getAFlat(" + r1 + "));\n } else if (" + (sizeVec4Remainder === 2) + ") {\n aVec = vec4(bestVec.xy, vec2(getAFlat(" + r1 + "), getAFlat(" + r2 + ")));\n } else if (" + (sizeVec4Remainder === 3) + ") {\n aVec = vec4(bestVec.x,\n vec3(getAFlat(" + r1 + "), getAFlat(" + r2 + "), getAFlat(" + r3 + ")));\n }\n if (" + (sizeVec4Remainder > 0) + ") {\n if (hasNaN(aVec)) {\n setOutput(getNaN(aVec));\n return;\n }\n bestVec = " + op + "(bestVec, aVec);\n }\n\n float final = " + op + "(bestVec.x, " + op + "(bestVec.y,\n " + op + "(bestVec.z, bestVec.w)));\n setOutput(final);\n }\n "; | ||
} | ||
@@ -10,0 +15,0 @@ return MinMaxProgram; |
import { GPGPUProgram } from './gpgpu_math'; | ||
export declare class ReduceSumProgram implements GPGPUProgram { | ||
aSize: number; | ||
size: number; | ||
variableNames: string[]; | ||
@@ -8,3 +8,3 @@ params: Array<{}>; | ||
userCode: string; | ||
constructor(aSize: number); | ||
constructor(size: number); | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var ReduceSumProgram = (function () { | ||
function ReduceSumProgram(aSize) { | ||
this.aSize = aSize; | ||
function ReduceSumProgram(size) { | ||
this.size = size; | ||
this.variableNames = ['A']; | ||
this.params = []; | ||
this.outputShape = []; | ||
this.userCode = "\n void main() {\n float sum = 0.0;\n for (int i = 0; i < " + aSize + "; i++) {\n sum += getAFlat(i);\n }\n setOutput(sum);\n }\n "; | ||
var sizeNearestVec4 = Math.floor(size / 4) * 4; | ||
var sizeVec4Remainder = size % 4; | ||
var r1 = sizeNearestVec4; | ||
var r2 = sizeNearestVec4 + 1; | ||
var r3 = sizeNearestVec4 + 2; | ||
this.userCode = "\n void main() {\n const vec2 ones2 = vec2(1);\n const vec3 ones3 = vec3(1);\n const vec4 ones4 = vec4(1);\n\n float sum = 0.0;\n for (int i = 0; i < " + sizeNearestVec4 + "; i += 4) {\n vec4 aVec = vec4(getAFlat(i), getAFlat(i+1),\n getAFlat(i+2), getAFlat(i+3));\n sum += dot(ones4, aVec);\n }\n\n if (" + (sizeVec4Remainder === 1) + ") {\n sum += getAFlat(" + r1 + ");\n } else if (" + (sizeVec4Remainder === 2) + ") {\n vec2 aVec = vec2(getAFlat(" + r1 + "), getAFlat(" + r2 + "));\n sum += dot(ones2, aVec);\n } else if (" + (sizeVec4Remainder === 3) + ") {\n vec3 aVec = vec3(getAFlat(" + r1 + "), getAFlat(" + r2 + "), getAFlat(" + r3 + "));\n sum += dot(ones3, aVec);\n }\n\n setOutput(sum);\n }\n "; | ||
} | ||
@@ -11,0 +16,0 @@ return ReduceSumProgram; |
@@ -70,3 +70,3 @@ "use strict"; | ||
var SAMPLE_4D_SNIPPET = "\nvec2 UVfrom4D(int texNumR, int texNumC, int stride0,\n int stride1, int stride2, int row, int col, int depth,\n int depth2) {\n int index = row * stride0 + col * stride1 + depth * stride2 + depth2;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n"; | ||
var SHADER_PREFIX = "\n precision highp float;\n varying vec2 resultUV;\n const vec2 halfCR = vec2(0.5, 0.5);\n\n float sample(sampler2D texture, vec2 uv) {\n return texture2D(texture, uv).r;\n }\n\n void setOutput(float val) {\n gl_FragColor = vec4(val, 0, 0, 0);\n }\n\n bool isNaN(float val) {\n return val == val ? false : true;\n }\n\n bool hasNaN(vec4 values) {\n return any(notEqual(values, values));\n }\n\n float getNaN(vec4 values) {\n bvec4 equals = equal(values, values);\n if (!equals.w) {\n return values.w;\n } else if (!equals.x) {\n return values.x;\n } else if (!equals.y) {\n return values.y;\n } else if (!equals.z) {\n return values.z;\n }\n return 0.0;\n }\n\n " + SAMPLE_1D_SNIPPET + "\n " + SAMPLE_2D_SNIPPET + "\n " + SAMPLE_3D_SNIPPET + "\n " + SAMPLE_4D_SNIPPET + "\n"; | ||
var SHADER_PREFIX = "\n precision highp float;\n varying vec2 resultUV;\n const vec2 halfCR = vec2(0.5, 0.5);\n\n float sample(sampler2D texture, vec2 uv) {\n return texture2D(texture, uv).r;\n }\n\n void setOutput(float val) {\n gl_FragColor = vec4(val, 0, 0, 0);\n }\n\n bool isNaN(float val) {\n return val == val ? false : true;\n }\n\n bool hasNaN(vec4 values) {\n return any(notEqual(values, values));\n }\n\n float getNaN(vec4 values) {\n return dot(vec4(1), values);\n }\n\n " + SAMPLE_1D_SNIPPET + "\n " + SAMPLE_2D_SNIPPET + "\n " + SAMPLE_3D_SNIPPET + "\n " + SAMPLE_4D_SNIPPET + "\n"; | ||
function getOutput1DCoords(shape, texShape) { | ||
@@ -73,0 +73,0 @@ if (texShape[0] === 1) { |
@@ -14,5 +14,2 @@ export interface WebGLContextAttributes { | ||
export declare function createWebGLRenderingContext(attributes: WebGLContextAttributes): WebGLRenderingContext; | ||
export declare function preferWebGL1(): void; | ||
export declare function preferWebGL2(): void; | ||
export declare function isWebGL2Enabled(): boolean; | ||
export declare function createWebGLRenderingContextFromCanvas(canvas: HTMLCanvasElement, attributes: WebGLContextAttributes): WebGLRenderingContext; | ||
@@ -19,0 +16,0 @@ export declare function callAndCheck<T>(gl: WebGLRenderingContext, func: () => T): T; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var USE_WEBGL2_WHEN_AVAILABLE = true; | ||
var WEBGL2_ENABLED = null; | ||
var MAX_TEXTURE_SIZE = null; | ||
var util = require("../../util"); | ||
var environment_1 = require("../../environment"); | ||
function createWebGLRenderingContext(attributes) { | ||
@@ -14,41 +13,13 @@ var canvas = document.createElement('canvas'); | ||
exports.createWebGLRenderingContext = createWebGLRenderingContext; | ||
function preferWebGL1() { | ||
USE_WEBGL2_WHEN_AVAILABLE = false; | ||
WEBGL2_ENABLED = null; | ||
} | ||
exports.preferWebGL1 = preferWebGL1; | ||
function preferWebGL2() { | ||
USE_WEBGL2_WHEN_AVAILABLE = true; | ||
WEBGL2_ENABLED = null; | ||
} | ||
exports.preferWebGL2 = preferWebGL2; | ||
function isWebGL2Enabled() { | ||
if (!USE_WEBGL2_WHEN_AVAILABLE) { | ||
return false; | ||
} | ||
if (WEBGL2_ENABLED == null) { | ||
var tempCanvas = document.createElement('canvas'); | ||
var gl = tempCanvas.getContext('webgl2'); | ||
if (gl != null) { | ||
WEBGL2_ENABLED = true; | ||
var loseContextExtension = getExtensionOrThrow(gl, 'WEBGL_lose_context'); | ||
loseContextExtension.loseContext(); | ||
} | ||
else { | ||
WEBGL2_ENABLED = false; | ||
} | ||
} | ||
return WEBGL2_ENABLED; | ||
} | ||
exports.isWebGL2Enabled = isWebGL2Enabled; | ||
function createWebGLRenderingContextFromCanvas(canvas, attributes) { | ||
var gl; | ||
if (isWebGL2Enabled()) { | ||
var webglVersion = environment_1.ENV.get('WEBGL_VERSION'); | ||
if (webglVersion === 2) { | ||
gl = canvas.getContext('webgl2', attributes); | ||
} | ||
else { | ||
else if (webglVersion === 1) { | ||
gl = (canvas.getContext('webgl', attributes) || | ||
canvas.getContext('experimental-webgl', attributes)); | ||
} | ||
if (gl == null) { | ||
if (webglVersion === 0 || gl == null) { | ||
throw new Error('This browser does not support WebGL.'); | ||
@@ -194,3 +165,3 @@ } | ||
function getChannelsPerTexture() { | ||
if (isWebGL2Enabled()) { | ||
if (environment_1.ENV.get('WEBGL_VERSION') === 2) { | ||
return 1; | ||
@@ -197,0 +168,0 @@ } |
@@ -21,1 +21,5 @@ export declare type Vector = number[] | Float64Array | Float32Array | Int32Array | Int8Array | Int16Array; | ||
export declare function rightPad(a: string, size: number): string; | ||
export declare function repeatedTry(checkFn: () => boolean, delayFn?: (counter: number) => number, maxCounter?: number): Promise<void>; | ||
export declare function getQueryParams(queryString: string): { | ||
[key: string]: string; | ||
}; |
@@ -185,2 +185,39 @@ "use strict"; | ||
exports.rightPad = rightPad; | ||
function repeatedTry(checkFn, delayFn, maxCounter) { | ||
if (delayFn === void 0) { delayFn = function (counter) { return 0; }; } | ||
return new Promise(function (resolve, reject) { | ||
var tryCount = 0; | ||
var tryFn = function () { | ||
if (checkFn()) { | ||
resolve(); | ||
return; | ||
} | ||
tryCount++; | ||
var nextBackoff = delayFn(tryCount); | ||
if (maxCounter != null && tryCount >= maxCounter) { | ||
reject(); | ||
return; | ||
} | ||
setTimeout(tryFn, nextBackoff); | ||
}; | ||
setTimeout(tryFn, 0); | ||
}); | ||
} | ||
exports.repeatedTry = repeatedTry; | ||
function getQueryParams(queryString) { | ||
var params = {}; | ||
queryString.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g, function (s) { | ||
var t = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
t[_i - 1] = arguments[_i]; | ||
} | ||
decodeParam(params, t[0], t[1]); | ||
return t.join('='); | ||
}); | ||
return params; | ||
} | ||
exports.getQueryParams = getQueryParams; | ||
function decodeParam(params, name, value) { | ||
params[decodeURIComponent(name)] = decodeURIComponent(value || ''); | ||
} | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "deeplearn", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Hardware-accelerated JavaScript library for machine intelligence", | ||
@@ -29,2 +29,3 @@ "private": false, | ||
"tslint": "~5.6.0", | ||
"tslint-no-circular-imports": "~0.2.0", | ||
"typedoc": "~0.8.0", | ||
@@ -31,0 +32,0 @@ "typescript": "2.4.2", |
{ | ||
"extends": ["tslint-no-circular-imports"], | ||
"rules": { | ||
@@ -3,0 +4,0 @@ "array-type": [true, "array-simple"], |
501573
164
10075
20