1 | /**
|
2 | * @license
|
3 | * Copyright 2017 Google LLC. All Rights Reserved.
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | * =============================================================================
|
16 | */
|
17 | import { env } from './environment';
|
18 | import * as base from './util_base';
|
19 | export * from './util_base';
|
20 | export * from './hash_util';
|
21 | /**
|
22 | * Create typed array for scalar value. Used for storing in `DataStorage`.
|
23 | */
|
24 | export function createScalarValue(value, dtype) {
|
25 | if (dtype === 'string') {
|
26 | return encodeString(value);
|
27 | }
|
28 | return toTypedArray([value], dtype);
|
29 | }
|
30 | function noConversionNeeded(a, dtype) {
|
31 | return (a instanceof Float32Array && dtype === 'float32') ||
|
32 | (a instanceof Int32Array && dtype === 'int32') ||
|
33 | (a instanceof Uint8Array && dtype === 'bool');
|
34 | }
|
35 | export function toTypedArray(a, dtype) {
|
36 | if (dtype === 'string') {
|
37 | throw new Error('Cannot convert a string[] to a TypedArray');
|
38 | }
|
39 | if (Array.isArray(a)) {
|
40 | a = base.flatten(a);
|
41 | }
|
42 | if (env().getBool('DEBUG')) {
|
43 | base.checkConversionForErrors(a, dtype);
|
44 | }
|
45 | if (noConversionNeeded(a, dtype)) {
|
46 | return a;
|
47 | }
|
48 | if (dtype == null || dtype === 'float32' || dtype === 'complex64') {
|
49 | return new Float32Array(a);
|
50 | }
|
51 | else if (dtype === 'int32') {
|
52 | return new Int32Array(a);
|
53 | }
|
54 | else if (dtype === 'bool') {
|
55 | const bool = new Uint8Array(a.length);
|
56 | for (let i = 0; i < bool.length; ++i) {
|
57 | if (Math.round(a[i]) !== 0) {
|
58 | bool[i] = 1;
|
59 | }
|
60 | }
|
61 | return bool;
|
62 | }
|
63 | else {
|
64 | throw new Error(`Unknown data type ${dtype}`);
|
65 | }
|
66 | }
|
67 | /**
|
68 | * Returns the current high-resolution time in milliseconds relative to an
|
69 | * arbitrary time in the past. It works across different platforms (node.js,
|
70 | * browsers).
|
71 | *
|
72 | * ```js
|
73 | * console.log(tf.util.now());
|
74 | * ```
|
75 | *
|
76 | * @doc {heading: 'Util', namespace: 'util'}
|
77 | */
|
78 | export function now() {
|
79 | return env().platform.now();
|
80 | }
|
81 | /**
|
82 | * Returns a platform-specific implementation of
|
83 | * [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).
|
84 | *
|
85 | * If `fetch` is defined on the global object (`window`, `process`, etc.),
|
86 | * `tf.util.fetch` returns that function.
|
87 | *
|
88 | * If not, `tf.util.fetch` returns a platform-specific solution.
|
89 | *
|
90 | * ```js
|
91 | * const resource = await tf.util.fetch('https://unpkg.com/@tensorflow/tfjs');
|
92 | * // handle response
|
93 | * ```
|
94 | *
|
95 | * @doc {heading: 'Util'}
|
96 | */
|
97 | export function fetch(path, requestInits) {
|
98 | return env().platform.fetch(path, requestInits);
|
99 | }
|
100 | /**
|
101 | * Encodes the provided string into bytes using the provided encoding scheme.
|
102 | *
|
103 | * @param s The string to encode.
|
104 | * @param encoding The encoding scheme. Defaults to utf-8.
|
105 | *
|
106 | * @doc {heading: 'Util'}
|
107 | */
|
108 | export function encodeString(s, encoding = 'utf-8') {
|
109 | encoding = encoding || 'utf-8';
|
110 | return env().platform.encode(s, encoding);
|
111 | }
|
112 | /**
|
113 | * Decodes the provided bytes into a string using the provided encoding scheme.
|
114 | * @param bytes The bytes to decode.
|
115 | *
|
116 | * @param encoding The encoding scheme. Defaults to utf-8.
|
117 | *
|
118 | * @doc {heading: 'Util'}
|
119 | */
|
120 | export function decodeString(bytes, encoding = 'utf-8') {
|
121 | encoding = encoding || 'utf-8';
|
122 | return env().platform.decode(bytes, encoding);
|
123 | }
|
124 | //# sourceMappingURL=util.js.map |
\ | No newline at end of file |