blob: 011765f986701a96e0e7006d09dd7c71e92cc984 [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
2#
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.
Tim Hall79d07d22020-04-27 18:20:16 +010016# Description:
17# Numerical utilities for various types of rounding etc.
Tim Hall79d07d22020-04-27 18:20:16 +010018import math
Diego Russoea6111a2020-04-14 18:41:58 +010019
Tim Hall79d07d22020-04-27 18:20:16 +010020import numpy as np
21
22
23def round_up(a, b):
24 return ((a + b - 1) // b) * b
25
26
Tim Halld8339a72021-05-27 18:49:40 +010027def round_down(a, b):
28 return (a // b) * b
29
30
Tim Hall79d07d22020-04-27 18:20:16 +010031def round_up_divide(a, b):
32 return (a + b - 1) // b
33
34
35def round_up_to_int(v):
36 return int(math.ceil(v))
37
38
39def round_down_to_power_of_two(v):
40 assert v > 0
41 while v & (v - 1):
42 v &= v - 1
43
44 return v
45
46
47def round_up_to_power_of_two(v):
48 return round_down_to_power_of_two(2 * v - 1)
49
50
51def round_down_log2(v):
52 return int(math.floor(np.log2(v)))
53
54
55def round_up_log2(v):
56 return int(math.ceil(np.log2(v)))
57
58
59def round_to_int(v):
60 return np.rint(v).astype(np.int64)
61
62
63# Performs rounding away from zero.
64# n.b. This is identical to C++11 std::round()
65def round_away_zero(f):
66 r = -0.5 if (f < 0) else 0.5
67 return np.trunc(f + r)
68
69
70def quantise_float32(f, scale=1.0, zero_point=0):
71 return zero_point + int(round_away_zero(np.float32(f) / np.float32(scale)))
72
73
74def clamp_tanh(x):
75 if x <= -4:
76 y = -1.0
77 elif x >= 4:
78 y = 1.0
79 else:
80 y = math.tanh(x)
81 return y
82
83
84def clamp_sigmoid(x):
85 if x <= -8:
86 y = 0.0
87 elif x >= 8:
88 y = 1.0
89 else:
Louis Verhaard8912c532020-09-30 12:11:49 +020090 y = 1 / (1 + math.exp(-x))
Tim Hall79d07d22020-04-27 18:20:16 +010091 return y
Charles Xu3e9c4342020-04-22 08:31:43 +020092
Tim Hallc30f4952020-06-15 20:47:35 +010093
Charles Xu3e9c4342020-04-22 08:31:43 +020094def full_shape(dim, shape, fill):
Louis Verhaard69b31762020-11-17 09:45:20 +010095 """Returns a shape of at least dim dimensions"""
Tim Hallc30f4952020-06-15 20:47:35 +010096 return ([fill] * (dim - len(shape))) + shape
Louis Verhaard0b8268a2020-08-05 16:11:29 +020097
98
99def overlaps(start1, end1, start2, end2):
100 return start1 < end2 and start2 < end1
Dwight Lidman8359a472020-09-28 15:53:40 +0200101
102
103def is_integer(num):
104 if isinstance(num, (int, np.integer)):
105 return True
106 if type(num) is float and num.is_integer():
107 return True
108 if isinstance(num, np.inexact) and np.mod(num, 1) == 0:
109 return True
110 return False