tvm_runtime.js 39.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
/**
 * TVM Javascript web runtime library.
 *
 * @projectname tvm
 * @version 0.1
 */
/* eslint no-unused-vars: "off" */
/* eslint no-unexpected-multiline: "off" */
/* eslint indent: "off" */
10
/* eslint no-console: "off" */
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/**
 * TVM Runtime namespace.
 * Provide tvm_runtime.create to create a {@link tvm.TVMRuntime}.
 *
 * @namespace tvm_runtime
 */
var tvm_runtime = tvm_runtime || {};

/**
 * TVM root namespace.
 * The classes inside this namespace need to be constructed by factory functions.
 * Use {@link tvm_runtime}.create to get started.
 *
 * @namespace tvm
 */
(function() {
  /**
   * TVMRuntime object for interacting with TVM runtime.
   * This object can be constructed using {@link tvm_runtime}.create
   *
   * @class
   * @memberof tvm
   */
  function TVMRuntime() {
35 36
    "use strict";
    var runtime_ref = this;
37 38
    // Utility function to throw error
    function throwError(message) {
39 40 41
      if (typeof runtime_ref.logger !== "undefined") {
        runtime_ref.logger(message);
      }
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
      if (typeof Error !== "undefined") {
        throw new Error(message);
      }
      throw message;
    }
    var Module = this.Module;
    var Runtime = this.Runtime;
    if (typeof Module === "undefined") {
      throwError("Emscripten Module is not available");
    }
    // constants
    var SIZEOF_POINTER = 4;
    var SIZEOF_SIZE_T = 4;
    var SIZEOF_FLOAT = 4;
    var SIZEOF_INT = 4;
    var SIZEOF_INT8 = 1;
    var SIZEOF_INT64 = 8;
    var SIZEOF_DOUBLE = 8;
    var SIZEOF_TYPE = 4;
    var SIZEOF_CTX = SIZEOF_INT + SIZEOF_INT;
    var SIZEOF_TVMVALUE = SIZEOF_DOUBLE;
    var ARRAY_OFFSET_DATA = 0;
    var ARRAY_OFFSET_CTX = ARRAY_OFFSET_DATA + SIZEOF_POINTER;
    var ARRAY_OFFSET_DEV_TYPE = ARRAY_OFFSET_CTX;
    var ARRAY_OFFSET_DEV_ID = ARRAY_OFFSET_CTX + SIZEOF_INT;
    var ARRAY_OFFSET_NDIM = ARRAY_OFFSET_CTX + SIZEOF_CTX;
    var ARRAY_OFFSET_DTYPE = ARRAY_OFFSET_NDIM + SIZEOF_INT;
    var ARRAY_OFFSET_DTYPE_CODE = ARRAY_OFFSET_DTYPE;
    var ARRAY_OFFSET_DTYPE_BITS = ARRAY_OFFSET_DTYPE_CODE + SIZEOF_INT8;
    var ARRAY_OFFSET_DTYPE_LANES = ARRAY_OFFSET_DTYPE_BITS + SIZEOF_INT8;
    var ARRAY_OFFSET_SHAPE = ARRAY_OFFSET_DTYPE + SIZEOF_TYPE;
    var ARRAY_OFFSET_STRIDES = ARRAY_OFFSET_STRIDES + SIZEOF_POINTER;
    var ARRAY_OFFSET_BYTE_OFFSET = ARRAY_OFFSET_STRIDES + SIZEOF_POINTER;
    // Type codes
    var kInt = 0;
    var kUInt = 1;
    var kFloat = 2;
    var kHandle = 3;
    var kNull = 4;
    var kTVMType = 5;
    var kTVMContext = 6;
    var kArrayHandle = 7;
    var kNodeHandle = 8;
    var kModuleHandle = 9;
    var kFuncHandle = 10;
    var kStr = 11;
    var kBytes = 12;
    //-----------------------------------------
    // TVM CWrap library
    // ----------------------------------------
    var TVMGetLastError = Module.cwrap(
      "TVMGetLastError",
      "string", // const char*
      []);

    var TVMAPISetLastError = Module.cwrap
    ("TVMAPISetLastError",
     null,
     ["string" // const char*
     ]);

    var TVMModImport = Module.cwrap
    ("TVMModImport",
     "number",
     ["number", // TVMModuleHandle mod
      "number"  // TVMModuleHandle dep
     ]);

    var TVMModGetFunction = Module.cwrap
    ("TVMModGetFunction",
     "number",
     ["number", // TVMModuleHandle mod
      "string", // const char* func_name
      "number", // int query_imports
      "number"  // TVMFunctionHandle *out
     ]);

    var TVMModFree = Module.cwrap
    ("TVMModFree",
     "number",
     ["number"  // TVMModeHandle mod
     ]);

    var TVMFuncFree = Module.cwrap
    ("TVMFuncFree",
     "number",
     ["number"  // TVMFunctionHandle func
     ]);

    var TVMFuncCall = Module.cwrap
    ("TVMFuncCall",
     "number",
     ["number", // TVMFunctionHandle func
      "number", // TVMValue* arg_values
      "number", // int* arg_tcodes
      "number", // int num_args
      "number", // int ret_val
      "number"  // int ret_type_code
     ]);

    var TVMCFuncSetReturn = Module.cwrap
    ("TVMCFuncSetReturn",
     "number",
     ["number", // TVMRetValueHandle ret
      "number", // TVMValue* value
      "number", // int* type_code
      "number" // int num_ret
     ]);

    var TVMCbArgToReturn = Module.cwrap
    ("TVMCbArgToReturn",
     "number",
     ["number", // TVMValue* value
      "number"  // int code
     ]);

    var TVMFuncCreateFromCFunc = Module.cwrap
    ("TVMFuncCreateFromCFunc",
     "number",
     ["number", // TVMPackedCFunc func,
      "number", // void* resource_handle
      "number", // TVMPackedCFuncFinalizer fin
      "number"  // TVMFunctionHandle *out
     ]);

    var TVMFuncRegisterGlobal = Module.cwrap
    ("TVMFuncRegisterGlobal",
     "number",
     ["string", // name
      "number", // TVMFunctionHandle f
      "number"  // int override
     ]);

    var TVMFuncGetGlobal = Module.cwrap
    ("TVMFuncGetGlobal",
     "number",
     ["string", // const char* name
      "number"  // TVMFunctionHandle* out
     ]);

    var TVMFuncListGlobalNames = Module.cwrap
    ("TVMFuncListGlobalNames",
     "number",
     ["number", // int* out_size
      "number"  // const char*** out_array
     ]);


    var TVMArrayAlloc = Module.cwrap
    ("TVMArrayAlloc",
     "number",
     ["number", // const tvm_index_t* shape
      "number", // int ndim
      "number", // int dtype_code
      "number", // int dtype_bits
      "number", // int dtype_lanes
      "number", // int device_type
      "number", // int device_id
      "number"  // int TVMArrayHandle* out
     ]);

    var TVMArrayFree = Module.cwrap
    ("TVMArrayFree",
     "number",
     ["number"  // TVMArrayHandle handle
     ]);

    var TVMArrayCopyFromTo = Module.cwrap
    ("TVMArrayCopyFromTo",
     "number",
     ["number", // TVMArrayHandle from
      "number" // TVMArrayHandle to
     ]);

    var TVMArrayCopyFromBytes = Module.cwrap
    ("TVMArrayCopyFromBytes",
     "number",
     ["number", // TVMArrayHandle handle
      "number", // int data
      "number"  // size_t nbytes
     ]);

    var TVMArrayCopyToBytes = Module.cwrap
    ("TVMArrayCopyToBytes",
     "number",
     ["number", // TVMArrayHandle handle
      "number", // int data
      "number"  // size_t nbytes
     ]);

232 233 234 235 236 237 238 239
    var TVMModLoadFromFile = Module.cwrap
    ("TVMModLoadFromFile",
     "number",
     ["string", // const char* file_name
      "string", // const char* format
      "number"  // TVMModuleHandle* out
     ])

240 241 242 243 244 245 246 247 248
    //-----------------------------------------
    // Static utility functions
    // ----------------------------------------
    this.assert = function(condition, message) {
      if (!condition) {
        message = message || "assert failed";
        throwError(message);
      }
    };
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    /**
     * Logging function.
     * Override this to change logger behavior.
     *
     * @param {string} message
     */
    this.logger = function(message) {
      console.log(message);
    };

    function logging(message) {
      runtime_ref.logger(message);
    }
    // Override print error to logging
    Module.printErr = logging;
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    var CHECK = this.assert;

    function TVM_CALL(ret) {
      if (ret != 0) {
        throwError(TVMGetLastError());
      }
    }

    function CInt64ArrayToJS(ptr, size) {
      var ret = [];
      for (var i = 0; i < size; ++i) {
        ret.push(Module.getValue(ptr + i * SIZEOF_INT64, "i64"));
      }
      return ret;
    }

    function CStringToJS(ptr) {
      var ret = [];
      var ch = 1;
      while (ch != 0) {
        ch = Module.getValue(ptr, "i8");
        if (ch != 0) {
          ret.push(String.fromCharCode(ch));
        }
        ++ptr;
      }
      return ret.join("");
    }

    function CBytesToJS(ptr) {
      var data = Module.getValue(ptr, "*");
      var size = Module.getValue(ptr + SIZEOF_POINTER, "i32");
      var ret = new Uint8Array(new ArrayBuffer(size));
      ret.set(new Uint8Array(Module.HEAPU8.buffer, data, size));
      return ret;
    }

    function StringToUint8Array(str) {
302
      var arr = new Uint8Array(str.length + 1);
303 304 305
      for(var i = 0; i < str.length; ++i) {
        arr[i] = str.charCodeAt(i);
      }
306
      arr[str.length] = 0;
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
      return arr;
    }
    //-----------------------------------------
    // Class declarations
    // ----------------------------------------
    function CBuffer(nbytes) {
      this.data = Module._malloc(nbytes);
    }

    function RefTVMValue() {
      this.data = Module._malloc(SIZEOF_TVMVALUE);
    }

    function TVMArgs(nargs) {
      this.nargs = nargs;
      this.value = Module._malloc(SIZEOF_TVMVALUE * nargs);
      this.tcode = Module._malloc(SIZEOF_INT * nargs);
      this.temp = [];
    }

    function TVMType(code, bits, lanes) {
      this.code = code;
      this.bits = bits;
      this.lanes = lanes;
    }
    /**
     * TVM device context.
     * @class
     * @memberof tvm
     */
    function TVMContext(device_type, device_id) {
      this.device_type = device_type;
      this.device_id = device_id;
    }
    /**
     * TVM  n-dimensional array.
     *
     * Use {@link tvm.TVMRuntime}.empty to create an instance.
     * @class
     * @memberof tvm
     */
    function NDArray(handle) {
      this.handle = handle;
      this.ndim = Module.getValue(this.handle + ARRAY_OFFSET_NDIM, "i32");
      // shape
      var cshape = Module.getValue(this.handle + ARRAY_OFFSET_SHAPE, "*");
      this.shape = CInt64ArrayToJS(cshape, this.ndim);
      // dtype
      var code = Module.getValue(this.handle + ARRAY_OFFSET_DTYPE_CODE, "i8");
      var bits = Module.getValue(this.handle + ARRAY_OFFSET_DTYPE_BITS, "i8");
      var lanes = Module.getValue(this.handle + ARRAY_OFFSET_DTYPE_LANES, "i16");
      var dtype =  new TVMType(code, bits, lanes);
      this.dtype = dtype;
      this.BYTES_PER_ELEMENT = (dtype.bits * dtype.lanes / 8);
      // ctx
      var device_type = Module.getValue(this.handle + ARRAY_OFFSET_DEV_TYPE, "i32");
      var device_id = Module.getValue(this.handle + ARRAY_OFFSET_DEV_ID, "i32");
      this.context = new TVMContext(device_type, device_id);
      // byte_offset
      this.byteOffset = Module.getValue(this.handle + ARRAY_OFFSET_BYTE_OFFSET, "i64");
    }

    function TVMFunction(handle) {
      this.handle = handle;
    }
    /**
     * Module container of TVM generated functions.
     *
     * @class
     * @memberof tvm
     */
    function TVMModule(handle) {
      this.handle = handle;
    }
    /**
     * A typed scalar constant.
     * This can be used to pass number as integer types to tvm function.
     * Use {@link tvm.TVMRuntime}.constant to create an instance.
     * @class
     * @memberof tvm
     */
    function TVMConstant(value, dtype) {
      this.value = value;
      this.dtype = dtype;
    }
    //-----------------------------------------
    // Private Functions
    // ----------------------------------------
    function getTVMType(dtype) {
      if (dtype instanceof TVMType) return dtype;
      if (typeof dtype == "string") {
      var pattern = dtype;
        var code, bits = 32, lanes = 1;
        if (pattern.substring(0, 5) == "float") {
          pattern = pattern.substring(5, pattern.length);
          code = kFloat;
        } else if (pattern.substring(0, 3) == "int") {
          pattern = pattern.substring(3, pattern.length);
          code = kInt;
        } else if (pattern.substring(0, 4) == "uint") {
          pattern = pattern.substring(4, pattern.length);
          code = kUInt;
        } else if (pattern.substring(0, 6) == "handle") {
          pattern = pattern.substring(5, pattern.length);
          code = kHandle;
          bits = 64;
        } else {
          throw throwError("Unknown dtype " + dtype);
        }
        var arr = pattern.split("x");
        if (arr.length >= 1) {
          var parsed = parseInt(arr[0]);
          if (parsed == arr[0]) {
            bits = parsed;
          }
        }
        if (arr.length >= 2) {
          lanes = parseInt(arr[1]);
        }
        return new TVMType(code, bits, lanes);
      } else {
        throw throwError("Unknown dtype " + dtype);
      }
    }

    function TVMRetValueToJS(vptr, tcode) {
      switch (tcode) {
      case kInt:
      case kUInt: return Module.getValue(vptr, "i64");
      case kFloat: return Module.getValue(vptr, "double");
      case kFuncHandle: return makeTVMFunction(Module.getValue(vptr, "*"));
      case kModuleHandle: return new TVMModule(Module.getValue(vptr, "*"));
      case kNull: return null;
      case kStr: return CStringToJS(Module.getValue(vptr, "*"));
      case kBytes: return CBytesToJS(Module.getValue(vptr, "*"));
      default: throwError("Unsupported return type code=" + tcode);
      }
    }

    function makeTVMFunction(handle) {
      var func = new TVMFunction(handle);
      var ret = function () {
        // alloc
        var args = new TVMArgs(arguments.length);
        var rvalue = new RefTVMValue();
        var rtcode = new RefTVMValue();
        args.setArguments(arguments);
        TVM_CALL(TVMFuncCall(handle, args.value, args.tcode,
                             args.nargs, rvalue.data, rtcode.data));
        var rv = TVMRetValueToJS(rvalue.data, rtcode.asInt());
        // release
        args.release();
        rvalue.release();
        rtcode.release();
        return rv;
      };
      var release = function() {
        func.release();
      };
      ret._tvm_function = func;
      ret.release = release;
      return ret;
    }
    //-----------------------------------------
    // Javascript PackedCallback System
    // ----------------------------------------
    var funcTable = [0];
    var freeFuncId = [];

    function invokeCallback(arg_value, arg_tcode, nargs, ret, handle) {
      var args = [];
      for (var i = 0; i < nargs; ++i) {
        var vptr = arg_value + i * SIZEOF_TVMVALUE;
        var tcode = Module.getValue(arg_tcode + i * SIZEOF_INT, "i32");
        if (tcode == kNodeHandle ||
            tcode == kFuncHandle ||
            tcode == kModuleHandle) {
          TVM_CALL(TVMCbArgToReturn(vptr, tcode));
        }
        args.push(TVMRetValueToJS(vptr, tcode));
      }
      var rv = funcTable[handle].apply(null, args);
      if (typeof rv !== "undefined") {
        // alloc
        var rarg = new TVMArgs(1);
        rarg.setArguments([rv]);
        TVM_CALL(TVMCFuncSetReturn(ret, rarg.value, rarg.tcode, 1));
        // release
        rarg.release();
      }
      return 0;
    }
    function freeCallback(handle) {
      funcTable[handle] = 0;
      freeFuncId.push(handle);
    }
    var fptrInvokeCallback = null;
    var fptrFreeCallback = null;
505 506
    if (typeof Runtime !== "undefined" &&
        typeof Runtime.addFunction !== "undefined") {
507 508 509 510 511 512 513 514 515
      fptrInvokeCallback = Runtime.addFunction(invokeCallback);
      fptrFreeCallback = Runtime.addFunction(freeCallback);
    }
    /**
     * Check if a function is TVM PackedFunc
     * @param {Function} f function to be checked.
     * @return {boolean} Whether f is PackedFunc
     */
    this.isPackedFunc = function(f) {
516
      return (typeof f == "function") && f.hasOwnProperty("_tvm_function");
517 518 519 520 521 522 523 524 525
    };
    var isPackedFunc = this.isPackedFunc;
    /**
     * Convert a javascript function to TVM function.
     * @param {Function} f javascript function.
     * @return {Function} The created TVMFunction.
     */
    this.convertFunc = function(f) {
      if (isPackedFunc(f)) return f;
526 527
      CHECK(fptrInvokeCallback !== null,
            "Emscripten Runtime addFunction is not available");
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
      var fid;
      if (freeFuncId.length != 0) {
        fid = freeFuncId.pop();
      } else {
        fid = funcTable.length;
        funcTable.push(0);
      }
      funcTable[fid] = f;
      // alloc
      var out = new RefTVMValue();
      TVM_CALL(TVMFuncCreateFromCFunc(
        fptrInvokeCallback, fid, fptrFreeCallback, out.data));
      var out_handle = out.asHandle();
      // release
      out.release();
      return makeTVMFunction(out_handle);
    };
    var convertFunc = this.convertFunc;
    //-----------------------------------------
    // Private Class declarations
    // ----------------------------------------
    CBuffer.prototype = {
      /**
       * Finalizer: resources from the object.
       */
      release : function() {
        if (this.data != 0) {
          Module._free(this.data);
          this.data = 0;
        }
      },
    };
    // RefTVMValue
    RefTVMValue.prototype = {
      /**
       * Finalizer: resources from the object.
       */
      release : function() {
        if (this.data != 0) {
          Module._free(this.data);
          this.data = 0;
        }
      },
      asInt : function() {
        return Module.getValue(this.data, "i32");
      },
      asInt64 : function() {
        return Module.getValue(this.data, "i64");
      },
      asDouble : function() {
        return Module.getValue(this.data, "double");
      },
      asHandle : function() {
        return Module.getValue(this.data, "*");
      }
    };
    // TVMArgs
    TVMArgs.prototype = {
      release : function() {
        if (this.value != 0) {
          Module._free(this.value);
          Module._free(this.tcode);
          this.value = 0;
          for (var i = 0; i< this.temp.length; ++i) {
            if (this.temp[i].release instanceof Function) {
              this.temp[i].release();
            }
          }
        }
      },
      setInt : function(index, value) {
        Module.setValue(this.tcode + index * SIZEOF_INT, kInt, "i32");
        Module.setValue(this.value + index * SIZEOF_TVMVALUE, value, "i64");
      },
      setDouble : function(index, value) {
        Module.setValue(this.tcode + index * SIZEOF_INT, kFloat, "i32");
        Module.setValue(this.value + index * SIZEOF_TVMVALUE, value, "double");
      },
      setHandle : function(index, value, tcode) {
        Module.setValue(this.tcode + index * SIZEOF_INT, tcode, "i32");
        Module.setValue(this.value + index * SIZEOF_TVMVALUE, value, "*");
      },
      setString : function(index, value) {
611
        var sdata = new CBuffer(value.length + 1);
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
        Module.HEAPU8.set(StringToUint8Array(value), sdata.data);
        this.temp.push(sdata);
        Module.setValue(this.tcode + index * SIZEOF_INT, kStr, "i32");
        Module.setValue(this.value + index * SIZEOF_TVMVALUE, sdata.data, "*");
      },
      setBytes : function(index, value) {
        CHECK(value instanceof Uint8Array);
        var sdata = new CBuffer(value.length);
        var sheader = new CBuffer(SIZEOF_POINTER + SIZEOF_SIZE_T);
        Module.HEAPU8.set(new Uint8Array(value), sdata.data);
        Module.setValue(sheader.data, sdata.data, "*");
        Module.setValue(sheader.data + SIZEOF_POINTER, value.length, "i32");
        this.temp.push(sdata);
        this.temp.push(sheader);
        Module.setValue(this.tcode + index * SIZEOF_INT, kBytes, "i32");
        Module.setValue(this.value + index * SIZEOF_TVMVALUE, sheader.data, "*");
      },
      setArguments : function(args) {
        for (var i = 0; i < args.length; ++i) {
          var v = args[i];
          var tp = typeof v;
          if (v instanceof NDArray) {
            this.setHandle(i, v.handle, kArrayHandle);
          } else if (v instanceof TVMConstant) {
            var code = getTVMType(v.dtype).code;
            if (code == kInt || code == kUInt) {
              this.setInt(i, v.value);
            } else if (code == kFloat) {
              this.setDouble(i, v.value);
            } else {
              CHECK(code == kHandle);
              this.setHandle(i, v.value, kHandle);
            }
          } else if (tp == "number") {
            this.setDouble(i, v);
647
          } else if (tp == "function" && v.hasOwnProperty("_tvm_function")) {
648 649 650 651 652 653 654 655 656 657 658
            this.setString(i, v._tvm_function.handle, kFuncHandle);
          } else if (v === null) {
            this.setHandle(i, 0, kNull);
          } else if (tp == "string") {
            this.setString(i, v);
          } else if (v instanceof Uint8Array) {
            this.setBytes(i, v);
          } else if (v instanceof Function) {
            v = convertFunc(v);
            this.temp.push(v);
            this.setHandle(i, v._tvm_function.handle, kFuncHandle);
659 660
          } else if (v instanceof TVMModule) {
            this.setHandle(i, v.handle, kModuleHandle);
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
          } else {
            throwError("Unsupported argument type " + tp);
          }
        }
      }
    };
    // TVMType
    var TYPE_CODE2STR = {
      0 : "int",
      1 : "uint",
      2 : "float",
      4 : "handle"
    };

    TVMType.prototype = {
      toString : function() {
        var ret = TYPE_CODE2STR[this.code] + this.bits.toString();
        if (this.lanes != 1) {
          return ret + "x" + this.lanes.toString();
        } else {
          return ret;
        }
      }
    };
    // TVMFunction
    TVMFunction.prototype = {
      release : function() {
        if (this.handle != 0) {
          TVM_CALL(TVMFuncFree(this.handle));
          this.handle = 0;
        }
      }
    };
    // TVMContext
    var CTX_MASK2STR = {
      1 : "cpu",
      2 : "gpu",
      4 : "opencl",
      8 : "metal",
700 701
      9 : "vpi",
      11 : "opengl",
702 703 704 705 706 707 708 709
    };
    var CTX_STR2MASK = {
      "cpu": 1,
      "gpu": 2,
      "cuda": 2,
      "cl": 4,
      "opencl": 4,
      "metal": 8,
710 711
      "vpi": 9,
      "opengl": 11,
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
    };
    TVMContext.prototype = {
      toString : function() {
        return CTX_MASK2STR[this.device_type] + "(" + this.device_id.toString() + ")";
      }
    };
    //-----------------------------------------
    // Public Functions
    // ----------------------------------------
    /**
     * Construct a TVMContext given device type and id.
     *
     * @param {number} device_type, string or int, The device type.
     * @param {number} device_id, the device id.
     * @return {tvm.TVMContext} The created TVMContext
     */
    this.context = function(device_type, device_id) {
      if (typeof device_type == "string") {
        device_type = CTX_STR2MASK[device_type];
      }
      return new TVMContext(device_type, device_id);
    };
    var context = this.context;
    /**
     * Create empty ndarray with given shape.
     *
     * @param {Array.<number>} shape The shape of the array.
     * @param {string} dtype The data type of the array, optional, default="float32"
     * @param {tvm.TVMContext} ctx The context of the array, optional, default=cpu(0).
     * @return {tvm.NDArray} The created ndarray.
     */
    this.empty = function(shape, dtype, ctx) {
      dtype = (typeof dtype !== "undefined") ?  dtype: "float32";
      ctx = (typeof ctx !== "undefined") ?  ctx : context("cpu", 0);
      shape = (typeof shape == "number") ? [shape] : shape;
      // alloc
      var cshape = Module._malloc(SIZEOF_INT64 * shape.length);
      var out = new RefTVMValue();
      for (var i = 0; i < shape.length; ++i) {
        Module.setValue(cshape + i * SIZEOF_INT64, shape[i], "i64");
      }
      dtype = getTVMType(dtype);
      TVM_CALL(TVMArrayAlloc(cshape, shape.length,
                             dtype.code, dtype.bits, dtype.lanes,
                             ctx.device_type, ctx.device_id,
                             out.data));
      var out_handle = out.asHandle();
      // release
      Module._free(cshape);
      out.release();
      return new NDArray(out_handle);
    };
    /**
     * List all global function names in the TVM runtime.
     * @return {Array.<string>} List of global function names.
     */
    this.listGlobalFuncNames = function() {
      // alloc
      var out_size = new RefTVMValue();
      var out_array = new RefTVMValue();
      TVM_CALL(TVMFuncListGlobalNames(out_size.data, out_array.data));
      var length = out_size.asInt();
      var base = out_array.asHandle();
      var names = [];
      for (var i = 0 ; i < length; ++i) {
        names.push(
          CStringToJS(Module.getValue(base + i * SIZEOF_POINTER, "*")));
      }
      // release
      out_size.release();
      out_array.release();
      return names;
    };
785
    var listGlobalFuncNames = this.listGlobalFuncNames;
786 787 788 789
    /**
     * Get a global function from TVM runtime.
     *
     * @param {string} The name of the function.
790
     * @return {Function} The corresponding function, null if function do not exist
791 792 793 794 795 796 797 798
     */
    this.getGlobalFunc = function (name) {
      // alloc
      var out = new RefTVMValue();
      TVM_CALL(TVMFuncGetGlobal(name, out.data));
      var out_handle = out.asHandle();
      // release
      out.release();
799 800 801 802 803
      if (out_handle != 0) {
        return makeTVMFunction(out_handle);
      } else {
        return null;
      }
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
    };
    var getGlobalFunc = this.getGlobalFunc;
    /**
     * Register function to be global function in tvm runtime.
     * @param {string} name The name of the function.
     * @param {Function} f function to be registered.
     * @param {boolean} override Whether overwrite function in existing registry.
     */
    this.registerFunc = function(name, f, override) {
      f = convertFunc(f);
      override = (typeof override !== "undefined") ?  override: false;
      var ioverride = override ? 1 : 0;
      TVM_CALL(TVMFuncRegisterGlobal(name, f._tvm_function.handle, ioverride));
    };
    /**
     * Create a typed scalar constant.
     * This can be used to pass number as integer types to tvm function.
     *
     * @param {number} value The value of the data.
     * @param {string} dtype The data type.
     * @param {tvm.TVMConstant} The created typed scalar.
     */
    this.constant = function(value, dtype) {
      return new TVMConstant(value, dtype);
    };
    //-----------------------------------------
    // Wrap of TVM Functions.
    // ----------------------------------------
832
    var systemFunc = {};
833
    /**
834
     * Get system-wide library module singleton.5A
835 836 837 838
     * System lib is a global module that contains self register functions in startup.
     * @return {tvm.TVMModule} The system module singleton.
     */
    this.systemLib = function() {
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
      if (typeof systemFunc.fGetSystemLib === "undefined") {
        systemFunc.fGetSystemLib = getGlobalFunc("module._GetSystemLib");
      }
      return systemFunc.fGetSystemLib();
    };

    this.startRPCServer = function(url, key, counter) {
      if (typeof key === "undefined") {
        key = "";
      }
      if (typeof counter === "undefined") {
        counter = 1;
      }
      // Node js, import websocket
      var bkey = StringToUint8Array("server:" + key);
854
      bkey = bkey.slice(0, bkey.length - 1);
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
      var server_name = "WebSocketRPCServer[" + key + "]";
      var RPC_MAGIC = 0xff271;
      function checkEndian() {
        var a = new ArrayBuffer(4);
        var b = new Uint8Array(a);
        var c = new Uint32Array(a);
        b[0] = 0x11;
        b[1] = 0x22;
        b[2] = 0x33;
        b[3] = 0x44;
        CHECK(c[0] === 0x44332211, "Need little endian to work");
      }
      checkEndian();
      // start rpc
      function RPCServer(counter) {
        var socket;
        if (typeof module !== "undefined" && module.exports) {
          // WebSocket for nodejs
          const WebSocket = require("ws");
          socket = new WebSocket(url);
        } else {
          socket = new WebSocket(url);
        }
        var self = this;
        socket.binaryType = "arraybuffer";
        this.init = true;
        this.counter = counter;

        if (typeof systemFunc.fcreateServer === "undefined") {
          systemFunc.fcreateServer =
            getGlobalFunc("contrib.rpc._CreateEventDrivenServer");
        }
        if (systemFunc.fcreateServer == null) {
          throwError("RPCServer is not included in runtime");
        }

        var message_handler = systemFunc.fcreateServer(
          function(cbytes) {
            if (socket.readyState == 1) {
              socket.send(cbytes);
              return new TVMConstant(cbytes.length, "int32");
            } else {
              return new TVMConstant(0, "int32");
            }
899
          } , server_name, "%toinit");
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915

        function on_open(event) {
          var intbuf = new Int32Array(1);
          intbuf[0] = RPC_MAGIC;
          socket.send(intbuf);
          intbuf[0] = bkey.length;
          socket.send(intbuf);
          socket.send(bkey);
          logging(server_name + " connected...");
        }

        function on_message(event) {
          if (self.init) {
            var msg = new Uint8Array(event.data);
            CHECK(msg.length >= 4, "Need message header to be bigger than 4");
            var magic = new Int32Array(event.data)[0];
916

917 918 919 920 921 922 923 924 925 926
            if (magic == RPC_MAGIC + 1) {
              throwError("key: " + key + " has already been used in proxy");
            } else if (magic == RPC_MAGIC + 2) {
              logging(server_name + ": RPCProxy do not have matching client key " + key);
            } else {
              CHECK(magic == RPC_MAGIC, url + "is not RPC Proxy");
              self.init = false;
            }
            logging(server_name + "init end...");
            if (msg.length > 4) {
927 928 929
              if (message_handler(
                      new Uint8Array(event.data, 4, msg.length -4),
                      new TVMConstant(3, "int32")) == 0) {
930 931 932 933
                socket.close();
              }
            }
          } else {
934 935
            if (message_handler(new Uint8Array(event.data),
                                new TVMConstant(3, "int32")) == 0) {
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
              socket.close();
            }
          }
        }
        function on_close(event) {
          message_handler.release();
          logging(server_name + ": closed finish...");
          if (!self.init && self.counter != 0) {
            logging(server_name +  ":reconnect to serve another request, session left=" + counter);
            // start a new server.
            new RPCServer(counter - 1);
          }
        }
        socket.addEventListener("open", on_open);
        socket.addEventListener("message", on_message);
        socket.addEventListener("close", on_close);
      }
      return new RPCServer(counter);
954
    };
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983

    /**
     * Load a TVM module from a library file.
     * The file must be present in the Emscripten virtual file system.
     * For example, you can pass "--preload-file file" or "--preload-file dir/"
     * to "emcc" when compiling the TVM library, in order to populate files into
     * the file system.
     * For more detail, see:
     * https://kripken.github.io/emscripten-site/docs/porting/files/packaging_files
     * @param {string} file_name Path of the file to be loaded. The path refers
     * to the Emscripten virtual file system.
     * @param {string} format The format of the file.
     * @return {tvm.TVMModule} The loaded module.
     */
    this.loadModuleFromFile = function (file_name, format) {
      // alloc
      var out = new RefTVMValue();
      TVM_CALL(TVMModLoadFromFile(file_name, format, out.data));
      var out_handle = out.asHandle();
      // release
      out.release();
      if (out_handle != 0) {
        return new TVMModule(out_handle);
      } else {
        return null;
      }
    };
    var loadModuleFromFile = this.loadModuleFromFile;

984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
    /**
     * Wrapper runtime module.
     * Wraps around set_input, load_params, run, and get_output.
     *
     * @class
     * @memberof tvm
     */
    function GraphModule(tvm_graph_module, ctx) {
      CHECK(tvm_graph_module instanceof TVMModule,
            "tvm_graph_module must be TVMModule");
      CHECK(ctx instanceof TVMContext, "ctx must be TVMContext");

      this.tvm_graph_module = tvm_graph_module;
      this.ctx = ctx;
      this._set_input = tvm_graph_module.getFunction("set_input");
      this._load_params = tvm_graph_module.getFunction("load_params");
      this._run = tvm_graph_module.getFunction("run");
      this._get_output = tvm_graph_module.getFunction("get_output");
    };

    GraphModule.prototype = {
      /**
       * Set input to graph module.
       *
       * @param {string} key The name of the input.
       * @param {NDArray} value The input value.
       */
      "set_input" : function(key, value) {
        CHECK(typeof key == "string", "key must be string");
        CHECK(value instanceof NDArray, "value must be NDArray");
        this._set_input(key, value);
      },

      /**
       * Load parameters from serialized byte array of parameter dict.
1019
       *
1020 1021 1022 1023 1024 1025 1026 1027 1028
       * @param {Uint8Array} params The serialized parameter dict.
       */
      "load_params" : function(params) {
        CHECK(params instanceof Uint8Array, "params must be Uint8Array");
        this._load_params(params);
      },

      /**
       * Load parameters from serialized base64 string of parameter dict.
1029
       *
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
       * @param {string} base64_params The serialized parameter dict.
       */
      "load_base64_params" : function(base64_params) {
        CHECK(typeof base64_params == "string", "base64_params must be string");
        var decoded_string = atob(base64_params);
        var decoded_u8 = new Uint8Array(decoded_string.length);
        for (var i = 0; i < decoded_string.length; i++) {
          decoded_u8[i] = decoded_string[i].charCodeAt(0);
        }
        this.load_params(decoded_u8);
      },

      /**
       * Run forward execution of the graph.
       */
      "run" : function() {
        this._run();
      },

      /**
       * Get index-th output to out.
1051
       *
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
       * @param {NDArray} out The output array container.
       * @return {NDArray} The output array container.
       */
      "get_output" : function(index, out) {
        CHECK(typeof index == "number", "index must be number");
        CHECK(out instanceof NDArray, "out must be NDArray");
        this._get_output(new TVMConstant(index, "int32"), out);
        return out;
      }
    };

    /**
     * Create a runtime executor module given a graph and a module.
     * @param {string} graph_json_str The Json string of the graph.
     * @param {TVMModule} libmod The TVM module.
     * @param {TVMContext} ctx The context to deploy the module.
     * @return {GraphModule} Runtime graph module for executing the graph.
     */
    this.createGraphRuntime = function(graph_json_str, libmod, ctx) {
      CHECK(typeof graph_json_str == "string", "graph_json_str must be string");
      CHECK(libmod instanceof TVMModule, "libmod must be TVMModule");
      CHECK(ctx instanceof TVMContext, "ctx must be TVMContext");

      var fcreate = getGlobalFunc("tvm.graph_runtime.create");
      CHECK(fcreate != null, "Cannot find tvm.graph_runtime.create");

      var tvm_graph_module = fcreate(graph_json_str, libmod,
                                     new TVMConstant(ctx.device_type, "int32"),
                                     new TVMConstant(ctx.device_id, "int32"));
1081

1082 1083 1084
      return new GraphModule(tvm_graph_module, ctx);
    };

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
    //-----------------------------------------
    // Class defintions
    // ----------------------------------------
    // NDArray.
    NDArray.prototype = {
      /**
       * Finalizer: resources from the object.
       */
      release : function() {
        if (this.handle != 0) {
          TVM_CALL(TVMArrayFree(this.handle));
          this.handle = 0;
        }
      },
      /**
       * Copy data from another NDArray or javascript array.
       * The number of elements must match.
       *
       * @param {Array} data The source data array.
       */
      copyFrom : function(data) {
        if (data instanceof NDArray) {
          TVM_CALL(TVMArrayCopyFromTo(data.handle, this.handle));
        } else {
          var size = this.shape.reduce(function(a, b) { return a * b; }, 1);
          if (data.length != size) {
            throwError("data size and shape mismatch data.length" + data.length + " vs " + size);
          }
          if (this.dtype == "float32") {
            data = Float32Array.from(data);
          } else if (this.dtype == "float64") {
            data = Float64Array.from(data);
          } else if (this.dtype == "int32") {
            data = Int32Array.from(data);
          } else if (this.dtype == "int8") {
            data = Int8Array.from(data);
          } else if (this.dtype == "uint8") {
            data = Uint8Array.from(data);
          } else {
            throwError("Unsupported data type " + this.dtype);
          }
          return this.copyFromRawBytes(new Uint8Array(data.buffer));
        }
      },
      /**
       * Copy data from raw bytes.
       * @param {Uint8Array} data Uint8Array of bytes.
       */
      copyFromRawBytes : function(data) {
        var size = this.shape.reduce(function(a, b) { return a * b; }, 1);
        var dtype = getTVMType(this.dtype);
        var nbytes = this.BYTES_PER_ELEMENT * size;
        CHECK(data instanceof Uint8Array);
        CHECK(data.length == nbytes,
              "Data length and bytes do not match " + data.length +
              " vs " + nbytes);
        var temp = Module._malloc(nbytes);
        Module.HEAPU8.set(data, temp);
        TVM_CALL(TVMArrayCopyFromBytes(this.handle, temp, nbytes));
        Module._free(temp);
        return this;
      },
      /**
       * Return a copied Uint8Array of the raw bytes in the NDArray.
       * @return {Uint8Array} The created array.
       */
      asRawBytes : function() {
        var size = this.shape.reduce(function(a, b) { return a * b; }, 1);
        var nbytes = this.BYTES_PER_ELEMENT * size;
        var temp = Module._malloc(nbytes);
        TVM_CALL(TVMArrayCopyToBytes(this.handle, temp, nbytes));
        var ret = new Uint8Array(new ArrayBuffer(nbytes));
        ret.set(new Uint8Array(Module.HEAPU8.buffer, temp, nbytes));
        Module._free(temp);
        return ret;
      },
      /**
       * Return Array data content as javascript typed array.
       * @return {TypedArray} The created array.
       */
      asArray : function() {
        if (this.dtype == "float32") {
          return new Float32Array(this.asRawBytes().buffer);
        } else if (this.dtype == "float64") {
          return new Float64Array(this.asRawBytes().buffer);
        } else if (this.dtype == "int32") {
          return new Int32Array(this.asRawBytes().buffer);
        } else if (this.dtype == "int8") {
          return new Int8Array(this.asRawBytes().buffer);
        } else if (this.dtype == "uint8") {
          return new Uint8Array(this.asRawBytes().buffer);
        } else {
          throwError("Unsupported data type " + this.dtype);
        }
      }
    };

    TVMModule.prototype = {
      /**
       * Finalizer: resources from the object.
       */
      release : function() {
        if (this.handle != 0) {
          TVM_CALL(TVMModFree(this.handle));
          this.handle = 0;
        }
      },
      /**
       * Get function from the module.
       * @param {string} name The name of the function.
       * @return {Function} The correspondin function.
       */
      getFunction : function(name) {
        // alloc
        var out = new RefTVMValue();
        TVM_CALL(TVMModGetFunction(this.handle, name, 0, out.data));
        var out_handle = out.asHandle();
        // release
        out.release();
        if (out_handle == 0) {
          throwError("Module has no function " + name);
        }
        return makeTVMFunction(out_handle);
      },
      /**
       * Add module to the import list of current one.
       * @param {tvm.TVMModule} mod The other module to be imported.
       */
      import_module : function(mod) {
        CHECK(mod instanceof TVMModule, "mod must be instance of TVMModule");
        TVM_CALL(TVMModImport(this.handle, mod.handle));
      }
    };
    //-----------------------------------------
    // Static variables.
    // ----------------------------------------
    /** Float32 type */
    this.float32 = "float32";
    /** Int32 type */
    this.int32 = "int32";
  }
  /**
   * Create a TVM runtime given emscripten module.
   * @property {string} create
   * @memberof tvm_runtime
   * @param Module The emscripten module.
   * @return {tvm.TVMRuntime} The created TVM runtime.
   */
1233
  this.create = function(Module) {
1234 1235
    var tvm = {};
    tvm.Module = Module;
1236 1237 1238 1239 1240
    if (typeof Module.addFunction !== "undefined") {
      tvm.Runtime = Module;
    } else {
      tvm.Runtime = Module.Runtime;
    }
1241 1242 1243 1244 1245 1246 1247 1248 1249
    TVMRuntime.apply(tvm);
    return tvm;
  };
}).apply(tvm_runtime);

// export things in node
if (typeof module !== "undefined" && module.exports) {
  module.exports = tvm_runtime;
}