Repository navigation
Expand file tree
/
Copy pathmath_log_domain.v
More file actions
139 lines (129 loc) · 4.92 KB
/
Copy pathmath_log_domain.v
File metadata and controls
139 lines (129 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
module vtl
import math
// signbit returns true for values with a negative sign bit, including -0 and
// negative NaNs. Signed integers are true when negative; unsigned and boolean
// values are always false.
pub fn (t &Tensor[T]) signbit[T]() !&Tensor[bool] {
$if T is bool || T is int || T is i8 || T is i16 || T is i32 || T is i64 || T is u8 || T is u16 || T is u32 || T is u64 || T is f32 || T is f64 {
mut result := empty[bool](t.shape.clone(), memory: .row_major)
for flat_index in 0 .. t.size {
$if T is f32 || T is f64 {
result.data.data[flat_index] = math.signbit(f64(t.get_nth(flat_index)))
} $else $if T is int || T is i8 || T is i16 || T is i32 || T is i64 {
result.data.data[flat_index] = t.get_nth(flat_index) < 0
} $else {
result.data.data[flat_index] = false
}
}
return result
} $else {
return error('signbit requires a real numeric tensor')
}
}
fn logaddexp_value(x f64, y f64) f64 {
if math.is_nan(x) || math.is_nan(y) {
return math.nan()
}
if x == y && math.is_inf(x, 0) {
return x
}
maximum := math.max(x, y)
if math.is_inf(maximum, 1) {
return maximum
}
minimum := math.min(x, y)
return maximum + math.log1p(math.exp(minimum - maximum))
}
fn logaddexp2_value(x f64, y f64) f64 {
if math.is_nan(x) || math.is_nan(y) {
return math.nan()
}
if x == y && math.is_inf(x, 0) {
return x
}
maximum := math.max(x, y)
if math.is_inf(maximum, 1) {
return maximum
}
minimum := math.min(x, y)
return maximum + math.log2(1.0 + math.exp2(minimum - maximum))
}
// copysign returns the magnitude of `magnitude` with the sign bit of `sign`.
// Both f32 and f64 inputs broadcast to a common output shape.
pub fn copysign[T](magnitude &Tensor[T], sign &Tensor[T]) !&Tensor[T] {
$if T is f32 || T is f64 {
shape := broadcast_shapes(magnitude.shape, sign.shape)!
absolute := magnitude.broadcast_to(shape)!
signs := sign.broadcast_to(shape)!
mut result := empty[T](shape, memory: .row_major)
for flat_index in 0 .. result.size {
magnitude_offset := broadcast_tensor_offset(flat_index, shape, absolute.strides)
sign_offset := broadcast_tensor_offset(flat_index, shape, signs.strides)
result.data.data[flat_index] = T(math.copysign(f64(absolute.data.data[magnitude_offset]),
f64(signs.data.data[sign_offset])))
}
return result
} $else {
return error('copysign requires f32 or f64 tensors of the same type')
}
}
// logaddexp computes log(exp(a) + exp(b)) elementwise using a stable form.
// The f32/f64 operands broadcast to a common output shape.
pub fn logaddexp[T](a &Tensor[T], b &Tensor[T]) !&Tensor[T] {
$if T is f32 || T is f64 {
shape := broadcast_shapes(a.shape, b.shape)!
left := a.broadcast_to(shape)!
right := b.broadcast_to(shape)!
mut result := empty[T](shape, memory: .row_major)
for flat_index in 0 .. result.size {
left_offset := broadcast_tensor_offset(flat_index, shape, left.strides)
right_offset := broadcast_tensor_offset(flat_index, shape, right.strides)
result.data.data[flat_index] = T(logaddexp_value(f64(left.data.data[left_offset]),
f64(right.data.data[right_offset])))
}
return result
} $else {
return error('logaddexp requires f32 or f64 tensors of the same type')
}
}
// logaddexp_scalar computes log(exp(x) + exp(scalar)) elementwise with a
// stable form, preserving the tensor's shape and f32/f64 dtype.
pub fn logaddexp_scalar[T](tensor &Tensor[T], scalar T) !&Tensor[T] {
return logaddexp_scalar_impl[T](tensor, scalar, false)
}
// logaddexp_scalar_left computes log(exp(scalar) + exp(x)) elementwise with a
// stable form, preserving the tensor's shape and f32/f64 dtype.
pub fn logaddexp_scalar_left[T](scalar T, tensor &Tensor[T]) !&Tensor[T] {
return logaddexp_scalar_impl[T](tensor, scalar, true)
}
fn logaddexp_scalar_impl[T](tensor &Tensor[T], scalar T, scalar_on_left bool) !&Tensor[T] {
$if T is f32 || T is f64 {
return tensor.map_values(fn [scalar, scalar_on_left] [T](value T) T {
if scalar_on_left {
return T(logaddexp_value(f64(scalar), f64(value)))
}
return T(logaddexp_value(f64(value), f64(scalar)))
})
} $else {
return error('logaddexp scalar operations require f32 or f64 values of the same type')
}
}
// logaddexp2 computes log2(2^a + 2^b) elementwise using a stable form.
// The f32/f64 operands broadcast to a common output shape.
pub fn logaddexp2[T](a &Tensor[T], b &Tensor[T]) !&Tensor[T] {
$if T is f32 || T is f64 {
shape := broadcast_shapes(a.shape, b.shape)!
left := a.broadcast_to(shape)!
right := b.broadcast_to(shape)!
mut result := empty[T](shape, memory: .row_major)
for flat_index in 0 .. result.size {
left_offset := broadcast_tensor_offset(flat_index, shape, left.strides)
right_offset := broadcast_tensor_offset(flat_index, shape, right.strides)
result.data.data[flat_index] = T(logaddexp2_value(f64(left.data.data[left_offset]),
f64(right.data.data[right_offset])))
}
return result
} $else {
return error('logaddexp2 requires f32 or f64 tensors of the same type')
}
}