Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
qim3d
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
QIM
Tools
qim3d
Commits
28318fca
Commit
28318fca
authored
1 year ago
by
fima
Browse files
Options
Downloads
Patches
Plain Diff
Hotfix for the scale_to_float16 function
parent
a854d29a
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
qim3d/utils/internal_tools.py
+28
-37
28 additions, 37 deletions
qim3d/utils/internal_tools.py
with
28 additions
and
37 deletions
qim3d/utils/internal_tools.py
+
28
−
37
View file @
28318fca
...
...
@@ -17,6 +17,7 @@ from fastapi import FastAPI
import
gradio
as
gr
from
uvicorn
import
run
def
mock_plot
():
"""
Creates a mock plot of a sine wave.
...
...
@@ -267,6 +268,7 @@ def get_port_dict():
return
port_dict
def
run_gradio_app
(
gradio_interface
,
host
=
"
0.0.0.0
"
):
# Get port using the QIM API
...
...
@@ -299,49 +301,38 @@ def get_css():
parent_directory
=
os
.
path
.
abspath
(
os
.
path
.
join
(
current_directory
,
os
.
pardir
))
css_path
=
os
.
path
.
join
(
parent_directory
,
"
css
"
,
"
gradio.css
"
)
with
open
(
css_path
,
'
r
'
)
as
file
:
with
open
(
css_path
,
"
r
"
)
as
file
:
css_content
=
file
.
read
()
return
css_content
def
scale_to_float16
(
arr
):
def
scale_to_float16
(
arr
:
np
.
ndarray
):
"""
Scale
a NumPy array to fit within the limits of
the float16 data type.
Scale
the input array to
the float16 data type.
Parameters:
-
arr (n
umpy
.ndarray):
The i
nput array to be scaled.
arr (n
p
.ndarray):
I
nput array to be scaled.
Returns:
- numpy
.ndarray:
The s
caled array
,
with
values adjusted to fit within the limits of float16 data type
.
np
.ndarray:
S
caled array with
dtype=np.float16
.
This function takes a NumPy array as input and checks if its maximum and minimum values
exceed the limits of the float16 data type. If necessary, it scales the positive and negative
parts of the array independently to fit within the range of float16.
This function scales the input array to the float16 data type, ensuring that the
maximum value of the array does not exceed the maximum representable value
for float16. If the maximum value of the input array exceeds the maximum
representable value for float16, the array is scaled down proportionally
to fit within the float16 range.
"""
#
D
et
ermin
e maximum
and minimum values of the array
#
G
et
th
e maximum
value to comprare with the float16 maximum value
arr_max
=
np
.
max
(
arr
)
arr_min
=
np
.
min
(
arr
)
# Check if scaling is necessary for positive and negative parts separately
if
arr_max
>
np
.
finfo
(
np
.
float16
).
max
:
pos_scaled_arr
=
np
.
interp
(
arr
[
arr
>=
0
],
(
0
,
arr_max
),
(
0
,
np
.
finfo
(
np
.
float16
).
max
))
else
:
pos_scaled_arr
=
arr
[
arr
>=
0
].
astype
(
np
.
float16
)
if
arr_min
<
-
np
.
finfo
(
np
.
float16
).
max
:
neg_scaled_arr
=
np
.
interp
(
arr
[
arr
<
0
],
(
arr_min
,
0
),
(
-
np
.
finfo
(
np
.
float16
).
max
,
0
))
else
:
neg_scaled_arr
=
arr
[
arr
<
0
].
astype
(
np
.
float16
)
# Combine the scaled positive and negative parts
scaled_arr
=
np
.
concatenate
((
neg_scaled_arr
,
pos_scaled_arr
))
float16_max
=
np
.
finfo
(
np
.
float16
).
max
# Reshape the scaled array to match the original shape
scaled_arr
=
scaled_arr
.
reshape
(
arr
.
shape
)
# If the maximum value of the array exceeds the float16 maximum value, scale the array
if
arr_max
>
float16_max
:
arr
=
(
arr
/
arr_max
)
*
float16_max
# Convert the scaled array to float16 data type
scaled_arr
=
scaled_
arr
.
astype
(
np
.
float16
)
arr
=
arr
.
astype
(
np
.
float16
)
return
scaled_arr
\ No newline at end of file
return
arr
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment