-
Notifications
You must be signed in to change notification settings - Fork 409
feat(python): add bfloat16 and bfloat16_array support #3329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
asadjan4611
wants to merge
26
commits into
apache:main
from
asadjan4611:feat/python-bfloat16-support
Closed
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
8c7d5c3
feat(python): add bfloat16 and bfloat16_array support
asadjan4611 c89d86e
style(python): fix code formatting for bfloat16 implementation
asadjan4611 b2bb7c6
fix(python): remove trailing newline in bfloat16_array.py to match co…
asadjan4611 6537f74
fix(python): resolve Cython compilation errors for bfloat16
asadjan4611 b6793a5
fix(python): use memcpy for safe type punning in bfloat16 conversion
asadjan4611 e387339
fix(python): use explicit size constant for ARM compatibility
asadjan4611 71241ba
fix(python): correct row schema Arrow conversion type ids
asadjan4611 8066ace
fix(python): build and export bfloat16 python module
asadjan4611 8ed6f57
docs(python): document bfloat16 support and stabilize pure mode seria…
asadjan4611 9fcb74d
fix(python): configure bazel shell for windows editable builds
asadjan4611 8866639
Merge branch 'main' into feat/python-bfloat16-support
asadjan4611 102acfb
fix(python): restore xlang serializer base after conflict merge
asadjan4611 bedb82e
fix(python): restore xlang serializer base in cython runtime
asadjan4611 2f2b6b4
fix(python): align bfloat16 serializers with unified API and typed bu…
asadjan4611 c9e2e12
fix(python): resolve bfloat16 cython build and serializer API drift
asadjan4611 56f45d3
style(python): align bfloat16 files with ci formatter
asadjan4611 efb0100
fix(python): include bfloat16 pxd in format cython target
asadjan4611 f1a3f42
fix(python): resolve bfloat16 cython redeclaration and static method …
asadjan4611 9d74ae2
fix(python): remove obsolete xlang arg for dataclass serializers
asadjan4611 ec59341
fix(python): accept legacy xlang kwarg in dataclass serializers
asadjan4611 848a1cd
fix(python): repair unsigned xlang dataclass refs and retry bazel fetch
asadjan4611 66dbf19
style(python): format setup retry log line
asadjan4611 3ac6ba8
fix(python): remove keyword arguments from cpdef function calls in co…
asadjan4611 308bde9
fix(python): remove keyword arguments from cpdef function calls in st…
asadjan4611 e3a1e63
fix(python): preserve bfloat16 NaN semantics, fix hash contract, expa…
asadjan4611 d5fa636
Merge apache/main into feat/python-bfloat16-support
asadjan4611 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| # distutils: language = c++ | ||
| # cython: embedsignature = True | ||
| # cython: language_level = 3 | ||
|
|
||
| from libc.stdint cimport uint16_t, uint32_t | ||
| from libc.string cimport memcpy | ||
|
|
||
| cdef inline uint16_t float32_to_bfloat16_bits(float value) nogil: | ||
| cdef uint32_t f32_bits | ||
| memcpy(&f32_bits, &value, 4) | ||
| cdef uint16_t bf16_bits = <uint16_t>(f32_bits >> 16) | ||
| cdef uint16_t truncated = <uint16_t>(f32_bits & 0xFFFF) | ||
| if truncated > 0x8000: | ||
| bf16_bits += 1 | ||
| if (bf16_bits & 0x7F80) == 0x7F80: | ||
| bf16_bits = (bf16_bits & 0x8000) | 0x7F80 | ||
| elif truncated == 0x8000 and (bf16_bits & 1): | ||
| bf16_bits += 1 | ||
| if (bf16_bits & 0x7F80) == 0x7F80: | ||
| bf16_bits = (bf16_bits & 0x8000) | 0x7F80 | ||
| return bf16_bits | ||
|
|
||
| cdef inline float bfloat16_bits_to_float32(uint16_t bits) nogil: | ||
| cdef uint32_t f32_bits = <uint32_t>bits << 16 | ||
| cdef float result | ||
| memcpy(&result, &f32_bits, 4) | ||
| return result | ||
|
|
||
|
|
||
| cdef class BFloat16: | ||
| cdef uint16_t _bits | ||
|
|
||
| def __init__(self, value): | ||
| if isinstance(value, BFloat16): | ||
| self._bits = (<BFloat16>value)._bits | ||
| else: | ||
| self._bits = float32_to_bfloat16_bits(<float>float(value)) | ||
|
|
||
| @staticmethod | ||
| def from_bits(uint16_t bits): | ||
| cdef BFloat16 bf16 = BFloat16.__new__(BFloat16) | ||
| bf16._bits = bits | ||
| return bf16 | ||
|
|
||
| def to_bits(self): | ||
| return self._bits | ||
|
|
||
| def to_float32(self): | ||
| return bfloat16_bits_to_float32(self._bits) | ||
|
|
||
| def __float__(self): | ||
| return float(self.to_float32()) | ||
|
|
||
| def __repr__(self): | ||
| return f"BFloat16({self.to_float32()})" | ||
|
|
||
| def __str__(self): | ||
| return str(self.to_float32()) | ||
|
|
||
| def __eq__(self, other): | ||
| if isinstance(other, BFloat16): | ||
| if self.is_nan() or (<BFloat16>other).is_nan(): | ||
| return False | ||
| if self.is_zero() and (<BFloat16>other).is_zero(): | ||
| return True | ||
| return self._bits == (<BFloat16>other)._bits | ||
| return False | ||
|
|
||
| def __hash__(self): | ||
| return hash(self._bits) | ||
|
|
||
| def is_nan(self): | ||
| cdef uint16_t exp = (self._bits >> 7) & 0xFF | ||
| cdef uint16_t mant = self._bits & 0x7F | ||
| return exp == 0xFF and mant != 0 | ||
|
|
||
| def is_inf(self): | ||
| cdef uint16_t exp = (self._bits >> 7) & 0xFF | ||
| cdef uint16_t mant = self._bits & 0x7F | ||
| return exp == 0xFF and mant == 0 | ||
|
|
||
| def is_zero(self): | ||
| return (self._bits & 0x7FFF) == 0 | ||
|
|
||
| def is_finite(self): | ||
| cdef uint16_t exp = (self._bits >> 7) & 0xFF | ||
| return exp != 0xFF | ||
|
|
||
| def is_normal(self): | ||
| cdef uint16_t exp = (self._bits >> 7) & 0xFF | ||
| return exp != 0 and exp != 0xFF | ||
|
|
||
| def is_subnormal(self): | ||
| cdef uint16_t exp = (self._bits >> 7) & 0xFF | ||
| cdef uint16_t mant = self._bits & 0x7F | ||
| return exp == 0 and mant != 0 | ||
|
|
||
| def signbit(self): | ||
| return (self._bits & 0x8000) != 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| import array | ||
|
|
||
| from pyfory.bfloat16 import BFloat16 | ||
|
|
||
|
|
||
| class BFloat16Array: | ||
| def __init__(self, values=None): | ||
| if values is None: | ||
| self._data = array.array("H") | ||
| else: | ||
| self._data = array.array("H", [BFloat16(v).to_bits() if not isinstance(v, BFloat16) else v.to_bits() for v in values]) | ||
|
|
||
| def __len__(self): | ||
| return len(self._data) | ||
|
|
||
| def __getitem__(self, index): | ||
| return BFloat16.from_bits(self._data[index]) | ||
|
|
||
| def __setitem__(self, index, value): | ||
| if isinstance(value, BFloat16): | ||
| self._data[index] = value.to_bits() | ||
| else: | ||
| self._data[index] = BFloat16(value).to_bits() | ||
|
|
||
| def __iter__(self): | ||
| for bits in self._data: | ||
| yield BFloat16.from_bits(bits) | ||
|
|
||
| def __repr__(self): | ||
| return f"BFloat16Array([{', '.join(str(bf16) for bf16 in self)}])" | ||
|
|
||
| def __eq__(self, other): | ||
| if not isinstance(other, BFloat16Array): | ||
| return False | ||
| return self._data == other._data | ||
|
|
||
| def append(self, value): | ||
| if isinstance(value, BFloat16): | ||
| self._data.append(value.to_bits()) | ||
| else: | ||
| self._data.append(BFloat16(value).to_bits()) | ||
|
|
||
| def extend(self, values): | ||
| for value in values: | ||
| self.append(value) | ||
|
|
||
| @property | ||
| def itemsize(self): | ||
| return 2 | ||
|
|
||
| def tobytes(self): | ||
| return self._data.tobytes() | ||
|
|
||
| @classmethod | ||
| def frombytes(cls, data): | ||
| arr = cls() | ||
| arr._data = array.array("H") | ||
| arr._data.frombytes(data) | ||
| return arr |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this, we've removed it in #3348