UNPKG

3.57 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright 2018 Google Inc. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * =============================================================================
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19var tfjs_1 = require("@tensorflow/tfjs");
20var os_1 = require("os");
21var INT32_MAX = 2147483648;
22/**
23 * Node.js-specific tensor type: int64-type scalar.
24 *
25 * This class is created for a specific purpose: to support
26 * writing `step`s to TensorBoard via op-kernel bindings.
27 * `step` is required to have an int64 dtype, but TensorFlow.js
28 * (tfjs-core) doesn't have a built-in int64 dtype. This is
29 * related to a lack of `Int64Array` or `Uint64Array` typed
30 * array in basic JavaScript.
31 *
32 * This class is introduced as a workaround.
33 */
34var Int64Scalar = /** @class */ (function () {
35 function Int64Scalar(value) {
36 this.value = value;
37 this.dtype = 'int64';
38 this.rank = 1;
39 // The reason why we need to check endianness of the machine here is
40 // negative int64 values and the way in which we represent them
41 // using Int32Arrays in JavaScript. We represent each int64 value with
42 // two consecutive elements of an Int32Array. For positive values,
43 // the high part is simply zero; for negative values, the high part
44 // should be -1. The ordering of the low and high parts assumes
45 // little endian (i.e., least significant digits appear first).
46 // This assumption is checked by the lines below.
47 if (Int64Scalar.endiannessOkay_ == null) {
48 if (os_1.endianness() !== 'LE') {
49 throw new Error("Int64Scalar does not support endianness of this machine: " +
50 ("" + os_1.endianness()));
51 }
52 Int64Scalar.endiannessOkay_ = true;
53 }
54 tfjs_1.util.assert(value > -INT32_MAX && value < INT32_MAX - 1, function () {
55 return "Got a value outside of the bound of values supported for int64 " +
56 ("dtype ([-" + INT32_MAX + ", " + (INT32_MAX - 1) + "]): " + value);
57 });
58 tfjs_1.util.assert(Number.isInteger(value), function () { return "Expected value to be an integer, but got " + value; });
59 // We use two int32 elements to represent a int64 value. This assumes
60 // little endian, which is checked above.
61 var highPart = value >= 0 ? 0 : -1;
62 var lowPart = value % INT32_MAX;
63 this.valueArray_ = new Int32Array([lowPart, highPart]);
64 }
65 Object.defineProperty(Int64Scalar.prototype, "shape", {
66 get: function () {
67 return [];
68 },
69 enumerable: true,
70 configurable: true
71 });
72 Object.defineProperty(Int64Scalar.prototype, "valueArray", {
73 /** Get the Int32Array that represents the int64 value. */
74 get: function () {
75 return this.valueArray_;
76 },
77 enumerable: true,
78 configurable: true
79 });
80 return Int64Scalar;
81}());
82exports.Int64Scalar = Int64Scalar;