license import { GPGPUProgram , useShapeUniforms } from './gpgpu_math' ; export const LINEAR = `return x;` ; export const ELU = `
  vec4 result;

  result.r = (x.r >= 0.0) ? x.r : (exp(x.r) - 1.0);
  result.g = (x.g >= 0.0) ? x.g : (exp(x.g) - 1.0);
  result.b = (x.b >= 0.0) ? x.b : (exp(x.b) - 1.0);
  result.a = (x.a >= 0.0) ? x.a : (exp(x.a) - 1.0);

  return result;
` ; export const RELU = `
  vec4 result = x * vec4(greaterThanEqual(x, vec4(0.0)));
  bvec4 isNaN = isnan(x);

  result.r = isNaN.r ? x.r : result.r;
  result.g = isNaN.g ? x.g : result.g;
  result.b = isNaN.b ? x.b : result.b;
  result.a = isNaN.a ? x.a : result.a;

  return result;
` ; export const RELU6 = `
  vec4 result = min(x, vec4(6.)) * vec4(greaterThanEqual(x, vec4(0.0)));
  bvec4 isNaN = isnan(x);

  result.r = isNaN.r ? x.r : result.r;
  result.g = isNaN.g ? x.g : result.g;
  result.b = isNaN.b ? x.b : result.b;
  result.a = isNaN.a ? x.a : result.a;

  return result;
` ; export const SIGMOID = `return 1.0 / (1.0 + exp(-1.0 * x));` ; export class UnaryOpPackedProgram implements GPGPUProgram { variableNames = [ 'A' ] ; userCode : string ; enableShapeUniforms : boolean ; outputShape : number [ ] ; packedInputs = true ; packedOutput = true ; constructor ( aShape , opSnippet ) { this . outputShape = aShape ; this . enableShapeUniforms = useShapeUniforms ( this . outputShape . length ) ; this . userCode = `
      vec4 unaryOperation(vec4 x) {
        ${ opSnippet }
      }

      void main() {
        vec4 x = getAAtOutCoords();
        vec4 y = unaryOperation(x);

        setOutput(y);
      }
    ` ; } }