Skip to content

[Types] Add float16 (Half) support #568

@Nucs

Description

@Nucs

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 .npy model weights are fp16

Proposal

Task List

  • Add NPTypeCode.Float16 = 16 to NPTypeCode.cs
  • Add np.float16 type alias in np.cs
  • Update NPTypeCodeExtensions.GetTypeCode() to handle typeof(Half)
  • Update InfoOf<T> for Half
  • Update UnmanagedStorage type switches
  • Update ArraySlice allocation for Half
  • Update _typemap_arr_arr and _typemap_arr_scalar in np.find_common_type.cs
  • Update all type switch patterns in DefaultEngine operations
  • Add tests for float16 operations
  • Test with real fp16 .npy files 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

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreInternal engine: Shape, Storage, TensorEngine, iteratorsdocumentation-neededFeature requires documentation after implementation or depiction of lack of documentationenhancementNew feature or requestmissing feature/sNumPy function not yet implemented in NumSharp

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions