-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add float16 support with configurable conversion behavior (#49)
* Enhance dtype support by adding float16 type and conversion methods - Introduced a new dtype "<f2" for float16 with size 16 and Uint16Array as its array constructor. - Implemented `float16ToFloat32Array` method to convert Uint16Array (float16) to Float32Array. - Added static method `float16ToFloat32` for converting individual float16 values to float32. - Updated the `Parsed` type to handle float16 conversion when necessary. This update improves the handling of float16 data types in the npyjs library. * feat: Add convertFloat16 option and improve test infrastructure - Add `convertFloat16` constructor option to control float16 conversion behavior - Fix test data generation script to use correct paths - Update records.json format for better git diff visibility - Fix float16 test timeout issues - Ensure test data consistency between runs The `convertFloat16` option (defaults to true) allows users to: - true: automatically convert float16 to float32 (default) - false: keep raw uint16 values for custom handling
- Loading branch information
Showing
10 changed files
with
396 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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 |
---|---|---|
@@ -1,14 +1,46 @@ | ||
import numpy as np | ||
import json | ||
import os | ||
import pathlib | ||
from pathlib import Path | ||
|
||
records = {} | ||
# Get the script's directory and create data directory relative to it | ||
script_dir = Path(__file__).parent | ||
data_dir = script_dir / "data" | ||
data_dir.mkdir(parents=True, exist_ok=True) | ||
|
||
# Load existing records if any | ||
records_file = script_dir / "records.json" | ||
if os.path.exists(records_file): | ||
with open(records_file) as f: | ||
print(f"Loading records from {records_file}") | ||
records = json.load(f) | ||
else: | ||
records = {} | ||
|
||
# Generate test data for each combination | ||
for dimensions in [(10,), (65, 65), (100, 100, 100), (4, 4, 4, 4, 4)]: | ||
for dtype in ["int8", "int16", "int64", "float32", "float64"]: | ||
for dtype in ["int8", "int16", "int64", "float16", "float32", "float64"]: | ||
name = f"./data/{'x'.join(str(i) for i in dimensions)}-{dtype}" | ||
|
||
# Skip if file already exists | ||
if name in records: | ||
continue | ||
|
||
data = np.random.randint(0, 255, dimensions).astype(dtype) | ||
# Store the last 5 values consistently for all types | ||
records[name] = data.ravel()[-5:].tolist() | ||
np.save(name, data) | ||
json.dump( | ||
records, open("records.json", "w"), | ||
) | ||
|
||
# Save file using the correct path | ||
file_path = script_dir / name.lstrip("./") | ||
file_path.parent.mkdir(parents=True, exist_ok=True) | ||
np.save(file_path, data) | ||
|
||
# Save records in a pretty, sorted format | ||
with open(records_file, 'w') as f: | ||
json.dump( | ||
records, | ||
f, | ||
indent=4, | ||
sort_keys=True | ||
) |
Oops, something went wrong.