Conversation
There was a problem hiding this comment.
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
BFloat16struct 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.
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>
710680f to
bd8ded6
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Fix ArgumentException parameter order in ToTensor<T> - Add scalar tensor(BFloat16) overload for 0-D tensor consistency - Align Equals/GetHashCode with ==/!= operators using ToSingle() - Redefine Epsilon as smallest positive value (.NET convention), add MinNormal Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| /// Binary-compatible with c10::BFloat16 in LibTorch. | ||
| /// </summary> | ||
| [StructLayout(LayoutKind.Sequential)] | ||
| public readonly struct BFloat16 : IComparable<BFloat16>, IEquatable<BFloat16>, IComparable, IFormattable |
There was a problem hiding this comment.
Is this BFloat16 different from System.Numerics.BFloat16 that's now available in .NET 11?
There was a problem hiding this comment.
I have to check, but the problem is we are now building with .net 8 only and I don't think directly jumping to 11 would be okay
Fixes #1469