Skip to content

Add support for bfloat16 type#1544

Open
alinpahontu2912 wants to merge 6 commits intodotnet:mainfrom
alinpahontu2912:fix_bfloat
Open

Add support for bfloat16 type#1544
alinpahontu2912 wants to merge 6 commits intodotnet:mainfrom
alinpahontu2912:fix_bfloat

Conversation

@alinpahontu2912
Copy link
Member

Fixes #1469

alinpahontu2912 and others added 6 commits February 13, 2026 15:31
Added BFloat16 case to PrintValue() and ToCSharpString() so that
bfloat16 (and float16) tensors can be printed in all string styles.
Add test coverage for the bfloat16/float16 printing fix:
- TestBFloat16ScalarToString: scalar tensor in jlstr, npstr, cstr formats
- TestBFloat16TensorToString: 1-D and 2-D tensors in numpy, julia, csharp formats plus print()
- TestFloat16TensorToString: 1-D tensors in csharp and numpy formats

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Implement a custom BFloat16 struct (2 bytes, binary-compatible with
c10::BFloat16) with round-to-nearest-even conversion matching PyTorch.

New features:
- BFloat16 struct with arithmetic, comparison, and conversion operators
- data<BFloat16>() and item<BFloat16>() for zero-copy tensor data access
- torch.tensor(BFloat16[], ...) factory overloads for 1D-4D arrays
- Scalar implicit conversion and ToBFloat16() extraction
- ToBFloat16() tensor extension method
- ToTensor<T> support for BFloat16
- as_tensor() BFloat16 overloads
- frombuffer offset fix for BFloat16 and Float16 (2-byte types)

Fixes dotnet#1469

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@alinpahontu2912 alinpahontu2912 changed the title Fix bfloat16 tensor printing Add support for bfloat16 type Feb 25, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds comprehensive support for the bfloat16 type to TorchSharp, fixing issue #1469 where bfloat16 tensors could not be printed. The implementation includes a new BFloat16 struct, tensor factory methods, conversion utilities, and extensive test coverage. Additionally, the PR fixes missing ToString support for Float16 types.

Changes:

  • Introduced a new BFloat16 struct with IEEE 754-compliant conversion logic using round-to-nearest-even
  • Added tensor factory methods, Scalar conversions, and extension methods for BFloat16
  • Fixed missing ToCSharpString and PrintValue handling for both BFloat16 and Float16 types
  • Comprehensive test coverage including ToString, data accessors, round-trip conversions, and multidimensional tensor creation

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/TorchSharp/BFloat16.cs New BFloat16 struct with conversion operators, arithmetic operators, and comparison operators
src/TorchSharp/Scalar.cs Added BFloat16 implicit conversion and extension methods for ToScalar/ToBFloat16
src/TorchSharp/Tensor/Tensor.cs Updated ValidateType, ToCSharpString, PrintValue to handle BFloat16; added BFloat16 to type mapping; fixed Float16 ToString support
src/TorchSharp/Tensor/TensorExtensionMethods.cs Added BFloat16 support to ToTensor generic methods and new ToBFloat16 extension
src/TorchSharp/Tensor/Factories/tensor_BFloat16.cs Complete set of tensor factory methods for BFloat16 arrays and multidimensional arrays
src/TorchSharp/Tensor/Factories/as_tensor.cs Added as_tensor overloads for BFloat16 arrays and IList
src/TorchSharp/Tensor/Factories/Tensor.Factories.cs Added BFloat16 and Float16 to offset calculation in frombuffer
test/TorchSharpTest/TestTorchTensor.cs Comprehensive tests for BFloat16 ToString, data access, round-trip conversion, tensor creation, struct operations, and Float16 ToString

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bfloat16 tensor cannot be printed

2 participants