Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B-ASIC - Better ASIC Toolbox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
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
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
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Computer Engineering
B-ASIC - Better ASIC Toolbox
Merge requests
!286
Add quantize function and enums
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Add quantize function and enums
quantization
into
master
Overview
0
Commits
1
Pipelines
3
Changes
4
Merged
Oscar Gustafsson
requested to merge
quantization
into
master
1 year ago
Overview
0
Commits
1
Pipelines
3
Changes
4
Expand
Closes
#194 (closed)
Edited
1 year ago
by
Oscar Gustafsson
0
0
Merge request reports
Compare
master
version 2
7f328992
1 year ago
version 1
b0cf4be7
1 year ago
master (base)
and
version 1
latest version
cb95ee18
1 commit,
1 year ago
version 2
7f328992
1 commit,
1 year ago
version 1
b0cf4be7
1 commit,
1 year ago
4 files
+
107
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
Search (e.g. *.vue) (Ctrl+P)
b_asic/quantization.py
0 → 100644
+
81
−
0
Options
"""
B-ASIC quantization module.
"""
import
math
from
enum
import
Enum
from
b_asic.types
import
Num
class
Quantization
(
Enum
):
"""
Quantization tyoes.
"""
ROUNDING
=
1
TRUNCATION
=
2
MAGNITUDE_TRUNCATION
=
3
JAMMING
=
4
class
Overflow
(
Enum
):
"""
Overflow types.
"""
TWOS_COMPLEMENT
=
1
SATURATION
=
2
def
quantize
(
value
:
Num
,
fractional_bits
:
int
,
integer_bits
:
int
=
1
,
quantization
:
Quantization
=
Quantization
.
TRUNCATION
,
overflow
:
Overflow
=
Overflow
.
TWOS_COMPLEMENT
,
):
"""
Quantize *value*.
Parameters
----------
value : int, float, complex
fractional_bits : int
integer_bits : int, default: 1
quantization : Quantization, default: Quantization.TRUNCATION
overflow : Overflow, default: Overflow.SATURATION
Returns
-------
"""
if
isinstance
(
value
,
complex
):
return
complex
(
quantize
(
value
.
real
,
fractional_bits
=
fractional_bits
,
integer_bits
=
integer_bits
,
quantization
=
quantization
,
overflow
=
overflow
,
),
quantize
(
value
.
imag
,
fractional_bits
=
fractional_bits
,
integer_bits
=
integer_bits
,
quantization
=
quantization
,
overflow
=
overflow
,
),
)
b
=
2
**
fractional_bits
v
=
b
*
value
if
quantization
is
Quantization
.
TRUNCATION
:
v
=
math
.
floor
(
v
)
elif
quantization
is
Quantization
.
ROUNDING
:
v
=
math
.
floor
(
v
+
0.5
)
elif
quantization
is
Quantization
.
MAGNITUDE_TRUNCATION
:
if
v
>=
0
:
v
=
math
.
floor
(
v
)
else
:
v
=
math
.
ceil
(
v
)
else
:
# Quantization.JAMMING
v
=
math
.
floor
(
v
)
|
1
# TODO: overflow handling
return
v
/
b
Loading