Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
* `uint1`
* `uint2`
* `uint4`
- 16-bit complex floating point numbers. (See below below for important limitations.)
* `complex32` (matching `numpy.float16`)
* `bcomplex32` (matching `numpy.bfloat16`)

See below for specifications of these number formats.

Expand Down Expand Up @@ -186,6 +189,16 @@ each element of the array is padded up to a byte in memory. The lower two or fou
bits of each byte contain the representation of the number, whereas the remaining
upper bits are ignored.

## `complex32` and `bcomplex32`

Complex types corresponding to `numpy.float16` and `ml_dtypes.bfloat16` respectively.

Because NumPy currently only recognizes built-in complex types the array
attributes `arr.imag` and `arr.real` will give incorrect results
(`.imag` returns an array of complex zeros and `.real` the original values unchanged).
To mitigate this issue we provide `ml_dtypes.imag()` and `ml_dtypes.real()`.
Similarly, other functions may fail to apply the correct complex behavior.

## Quirks of low-precision Arithmetic

If you're exploring the use of low-precision dtypes in your code, you should be
Expand Down
10 changes: 4 additions & 6 deletions ml_dtypes/_src/dtypes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,8 @@ struct TypeDescriptor<bcomplex32> : CustomComplexType<bcomplex32> {
static constexpr const char* kTpDoc =
"complex bfloat16 floating-point values";
// See also bfloat16, the kind argument is tricky to choose well.
static constexpr char kNpyDescrKind = 'W'; // TODO(seberg): better name?
// TODO(phawkins): there doesn't seem to be a way of guaranteeing a type
// character is unique.
static constexpr char kNpyDescrType = 'P'; // TODO(seberg): better name?
static constexpr char kNpyDescrKind = 'V';
static constexpr char kNpyDescrType = 'K';
static constexpr char kNpyDescrByteorder = '=';
};

Expand All @@ -366,10 +364,10 @@ struct TypeDescriptor<complex32> : CustomComplexType<complex32> {
static constexpr const char* kQualifiedTypeName = "ml_dtypes.complex32";
static constexpr const char* kTpDoc = "complex half floating-point values";
// See also bfloat16. `E` type char is used for bfloat16 unfortunately.
Comment thread
seberg marked this conversation as resolved.
static constexpr char kNpyDescrKind = 'W'; // TODO(seberg): better name?
static constexpr char kNpyDescrKind = 'c'; // TODO(seberg): better name?
// TODO(phawkins): there doesn't seem to be a way of guaranteeing a type
// character is unique.
static constexpr char kNpyDescrType = 'O'; // TODO(seberg): better name?
static constexpr char kNpyDescrType = 'J';
static constexpr char kNpyDescrByteorder = '=';
};

Expand Down