-
Notifications
You must be signed in to change notification settings - Fork 204
Open
Labels
coreInternal engine: Shape, Storage, TensorEngine, iteratorsInternal engine: Shape, Storage, TensorEngine, iteratorsdocumentation-neededFeature requires documentation after implementation or depiction of lack of documentationFeature requires documentation after implementation or depiction of lack of documentationenhancementNew feature or requestNew feature or requestmissing feature/sNumPy function not yet implemented in NumSharpNumPy function not yet implemented in NumSharp
Milestone
Description
Overview
Add support for float16 (C# System.Half) type to NumSharp. This is a high-priority missing type critical for modern ML workloads.
Problem
NumSharp is missing float16 (half-precision floating point):
>>> import numpy as np
>>> np.array([1.0, 2.0, 3.0], dtype=np.float16)
array([1., 2., 3.], dtype=float16)
>>> np.finfo(np.float16)
finfo(resolution=0.001, min=-6.55040e+04, max=6.55040e+04, dtype=float16)Critical use cases:
- ML inference — Transformers, LLMs, diffusion models use fp16
- GPU interop — CUDA, DirectML, Metal all use fp16
- Memory efficiency — 50% savings vs float32, 75% vs float64
- Model loading — Many
.npymodel weights are fp16
Proposal
Task List
- Add
NPTypeCode.Float16 = 16toNPTypeCode.cs - Add
np.float16type alias innp.cs - Update
NPTypeCodeExtensions.GetTypeCode()to handletypeof(Half) - Update
InfoOf<T>forHalf - Update
UnmanagedStoragetype switches - Update
ArraySliceallocation for Half - Update
_typemap_arr_arrand_typemap_arr_scalarinnp.find_common_type.cs - Update all type switch patterns in DefaultEngine operations
- Add tests for float16 operations
- Test with real fp16
.npyfiles from ML models
C# Type Mapping
| NumPy | C# | Size | Range | Available Since |
|---|---|---|---|---|
| float16 | System.Half | 2 bytes | [-65504, 65504] | .NET 5+ |
.NET Half Support
Half h1 = (Half)1.5f;
Half h2 = (Half)2.5f;
Half sum = h1 + h2; // All arithmetic built-in
Half sqrt = Half.Sqrt(h2); // Math functions available.NET System.Half has full arithmetic and math function support built-in.
Implementation Effort
LOW — System.Half is a built-in .NET type since .NET 5 with full arithmetic support.
Estimated: ~50-100 lines of changes across multiple files.
Related
- Part of NumPy 2.x alignment: [NEP50 Core] Type promotion diverges from NumPy 2.x NEP 50 for unsigned int array + signed int scalar #529
- Will benefit from DynamicMethod IL emission: [Core] Replace ~636K lines of generated math code with DynamicMethod IL emission #544
- Will benefit from SIMD optimization: [Core] SIMD-Optimized IL Emission #545 (Vector256 available)
References
- NumPy float16 docs: https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.float16
- .NET Half struct: https://docs.microsoft.com/en-us/dotnet/api/system.half
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
coreInternal engine: Shape, Storage, TensorEngine, iteratorsInternal engine: Shape, Storage, TensorEngine, iteratorsdocumentation-neededFeature requires documentation after implementation or depiction of lack of documentationFeature requires documentation after implementation or depiction of lack of documentationenhancementNew feature or requestNew feature or requestmissing feature/sNumPy function not yet implemented in NumSharpNumPy function not yet implemented in NumSharp