Skip to content

Instantly share code, notes, and snippets.

@teamdandelion
Created May 4, 2018 01:16
Show Gist options
  • Save teamdandelion/7473c7a05d1656efb03b0d2d2cecab52 to your computer and use it in GitHub Desktop.
Save teamdandelion/7473c7a05d1656efb03b0d2d2cecab52 to your computer and use it in GitHub Desktop.
tfjs failure
Couldn't parse line number in error:
tfjs@latest:1
precision highp float;
precision highp int;
varying vec2 resultUV;
const vec2 halfCR = vec2(0.5, 0.5);
bool isNaN(float val) {
float v1 = val * val;
float v2 = val * val;
return v1 == v2 ? false : true;
}
bool hasNaN(vec4 values) {
vec4 v1 = values * values;
vec4 v2 = values * values;
return any(notEqual(v1, v2));
}
float getNaN(vec4 values) {
return dot(vec4(1), values);
}
int round(float value) {
return int(floor(value + 0.5));
}
int imod(int x, int y) {
return x - y * (x / y);
}
const vec2 randomConst = vec2(
23.14069263277926, // e^pi (Gelfond's constant)
2.665144142690225 // 2^sqrt(2) (Gelfond–Schneider constant)
);
float random(float seed) {
return fract(cos(dot(resultUV * seed, randomConst)) * 12345.6789);
}
vec2 UVfrom1D(int texNumR, int texNumC, int index) {
int texR = index / texNumC;
int texC = index - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
vec2 UVfrom2D(int texNumR, int texNumC, int numC, int row, int col) {
int index = row * numC + col;
int texR = index / texNumC;
int texC = index - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
vec2 UVfrom3D(int texNumR, int texNumC, int stride0,
int stride1, int row, int col, int depth) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * stride0 + col * stride1 + depth;
int texR = index / texNumC;
int texC = index - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
vec2 UVfrom4D(int texNumR, int texNumC, int stride0,
int stride1, int stride2, int row, int col, int depth,
int depth2) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * stride0 + col * stride1 + depth * stride2 + depth2;
int texR = index / texNumC;
int texC = index - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
float sampleTexture(sampler2D textureSampler, vec2 uv) {
return texture2D(textureSampler, uv).r;
}
void setOutput(float val) {
gl_FragColor = vec4(val, 0, 0, 0);
}
uniform sampler2D x;
ivec2 getOutputCoords() {
return ivec2(resultUV.yx * vec2(10000, 100));
}
float getXFlat(int index) {
vec2 uv = UVfrom1D(10000, 10000, index);
return sampleTexture(x, uv);
}
float getX(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(10000.0, 10000.0);
return sampleTexture(x, uv);
}
const float initializationValue = -1.0 / 0.0;
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
float getValue(int batch, int inIdx) {
return getX(batch, inIdx);
}
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int outIdx = coords[1];
int inOffset = outIdx * 100;
vec4 minMaxValue = vec4(-1.0 / 0.0);
float sumValue = 0.0;
for (int i = 0; i < 100; i += 4) {
int inIdx = inOffset + i;
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
getValue(batch, inIdx + 3)
);
if (false) {
sumValue += dot(values, ones);
} else {
minMaxValue = max(values, minMaxValue);
}
}
int inIdx = inOffset + 100;
if (false) {
vec4 values = vec4(
getValue(batch, inIdx),
initializationValue,
initializationValue,
initializationValue
);
if (false) {
sumValue += dot(values, ones);
} else {
minMaxValue = max(values, minMaxValue);
}
} else if (false) {
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
initializationValue,
initializationValue
);
if (false) {
sumValue += dot(values, ones);
} else {
minMaxValue = max(values, minMaxValue);
}
} else if (false) {
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
initializationValue
);
if (false) {
sumValue += dot(values, ones);
} else {
minMaxValue = max(values, minMaxValue);
}
}
setOutput(max(max(max(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3]));
}
const size = 10000
let t = tf.zeros([size, size])
const result = tf.tidy(() => {
for (let i = 0; i < 10; i++) {
t.max().print()
let newT = t.matMul(t);
t.dispose()
t = newT;
}
return t;
});
result.max().print();
result
.max(1)
.sub(result.min(1))
.max()
.print();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment