blob: 0294b289562389114e4f1e63ee235e027c5162c3 [file] [log] [blame]
Rickard Bolinbc6ee582022-11-04 08:24:29 +00001# SPDX-FileCopyrightText: Copyright 2020-2021 Arm Limited and/or its affiliates <open-source-office@arm.com>
Tim Hall79d07d22020-04-27 18:20:16 +01002#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# 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, WITHOUT
13# 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.
Rickard Bolinbc6ee582022-11-04 08:24:29 +000016#
Tim Hall79d07d22020-04-27 18:20:16 +010017# Description:
18# Numerical utilities for various types of rounding etc.
Tim Hall79d07d22020-04-27 18:20:16 +010019import math
Diego Russoea6111a2020-04-14 18:41:58 +010020
Tim Hall79d07d22020-04-27 18:20:16 +010021import numpy as np
22
23
24def round_up(a, b):
25 return ((a + b - 1) // b) * b
26
27
Tim Halld8339a72021-05-27 18:49:40 +010028def round_down(a, b):
29 return (a // b) * b
30
31
Tim Hall79d07d22020-04-27 18:20:16 +010032def round_up_divide(a, b):
33 return (a + b - 1) // b
34
35
36def round_up_to_int(v):
37 return int(math.ceil(v))
38
39
40def round_down_to_power_of_two(v):
41 assert v > 0
42 while v & (v - 1):
43 v &= v - 1
44
45 return v
46
47
48def round_up_to_power_of_two(v):
49 return round_down_to_power_of_two(2 * v - 1)
50
51
52def round_down_log2(v):
53 return int(math.floor(np.log2(v)))
54
55
56def round_up_log2(v):
57 return int(math.ceil(np.log2(v)))
58
59
60def round_to_int(v):
61 return np.rint(v).astype(np.int64)
62
63
64# Performs rounding away from zero.
65# n.b. This is identical to C++11 std::round()
66def round_away_zero(f):
67 r = -0.5 if (f < 0) else 0.5
68 return np.trunc(f + r)
69
70
71def quantise_float32(f, scale=1.0, zero_point=0):
72 return zero_point + int(round_away_zero(np.float32(f) / np.float32(scale)))
73
74
75def clamp_tanh(x):
76 if x <= -4:
77 y = -1.0
78 elif x >= 4:
79 y = 1.0
80 else:
81 y = math.tanh(x)
82 return y
83
84
85def clamp_sigmoid(x):
86 if x <= -8:
87 y = 0.0
88 elif x >= 8:
89 y = 1.0
90 else:
Louis Verhaard8912c532020-09-30 12:11:49 +020091 y = 1 / (1 + math.exp(-x))
Tim Hall79d07d22020-04-27 18:20:16 +010092 return y
Charles Xu3e9c4342020-04-22 08:31:43 +020093
Tim Hallc30f4952020-06-15 20:47:35 +010094
Charles Xu3e9c4342020-04-22 08:31:43 +020095def full_shape(dim, shape, fill):
Louis Verhaard69b31762020-11-17 09:45:20 +010096 """Returns a shape of at least dim dimensions"""
Tim Hallc30f4952020-06-15 20:47:35 +010097 return ([fill] * (dim - len(shape))) + shape
Louis Verhaard0b8268a2020-08-05 16:11:29 +020098
99
100def overlaps(start1, end1, start2, end2):
101 return start1 < end2 and start2 < end1
Dwight Lidman8359a472020-09-28 15:53:40 +0200102
103
104def is_integer(num):
105 if isinstance(num, (int, np.integer)):
106 return True
107 if type(num) is float and num.is_integer():
108 return True
109 if isinstance(num, np.inexact) and np.mod(num, 1) == 0:
110 return True
111 return False