Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AGENTS.md
115 changes: 115 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Build and Test Commands

### Prerequisites
- Target framework: `.NET 6.0` / `.NET Standard 2.0`
- Solution file: `TensorFlow.NET.sln`

### Build Commands
```powershell
# Restore dependencies
dotnet restore TensorFlow.NET.sln

# Build entire solution (Debug / Release)
dotnet build TensorFlow.NET.sln -c Debug
dotnet build TensorFlow.NET.sln -c Release
```

### Testing Commands
```powershell
# Run all unit test suites
dotnet test TensorFlow.NET.sln --verbosity normal

# Run a specific test project
dotnet test test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj
dotnet test test/TensorFlowNET.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj
dotnet test test/TensorFlowNET.Graph.UnitTest/TensorFlowNET.Graph.UnitTest.csproj
dotnet test test/TensorFlow.Kernel.UnitTest/TensorFlow.Kernel.UnitTest.csproj
dotnet test test/TensorFlowNET.Native.UnitTest/Tensorflow.Native.UnitTest.csproj

# Run a specific test by fully qualified name
dotnet test --filter "FullyQualifiedName=TensorFlowNET.UnitTest.Basics.EagerModeTest"

# Run tests matching a name pattern
dotnet test --filter "Name~EagerMode"
```

### Switching CPU / GPU Redist Packages for Local Testing
The tests depend on `tools/Tensorflow.UnitTest.RedistHolder` to provide the native TensorFlow C-API runtime binaries:
```powershell
# Default / CPU redist
dotnet add tools/Tensorflow.UnitTest.RedistHolder package SciSharp.TensorFlow.Redist

# Windows GPU redist (CUDA & cuDNN required)
dotnet remove tools/Tensorflow.UnitTest.RedistHolder package SciSharp.TensorFlow.Redist
dotnet add tools/Tensorflow.UnitTest.RedistHolder package SciSharp.TensorFlow.Redist-Windows-GPU
```

---

## Architecture & Code Structure

**TensorFlow.NET (TF.NET)** is a pure .NET Standard / C# binding and implementation of TensorFlow (targeting TensorFlow v2.10 C-API / Python parity).

### High-Level Subsystems

```
src/
├── TensorFlowNET.Core/ # Core binding layer (Tensorflow.Binding)
│ ├── CApi/ # Low-level P/Invoke native interop (c_api.cs, tf_status, etc.)
│ ├── Tensors/ # Tensor, TensorShape, DType, memory management & disposables
│ ├── Eager/ # Eager execution engine, EagerTensor, Context
│ ├── Grafts / Gradients/ # Automatic differentiation (GradientTape)
│ ├── Operations/ # Math, array, nn, control flow graph & eager ops
│ ├── Variables/ # ResourceVariable, RefVariable, VariableScope
│ ├── NumPy/ # NumPy array & tensor interoperability (np.* helpers)
│ └── Protobuf/ # Generated TensorFlow protobuf definitions (GraphDef, NodeDef, etc.)
├── TensorFlowNET.Keras/ # High-level Keras neural network API (Tensorflow.Keras)
│ ├── Engine/ # Model, Layer, Sequential, Functional, Training/Eval loop
│ ├── Layers/ # Core, Convolutional, Recurrent, Normalization, Reshaping layers
│ ├── Optimizers/ # Adam, SGD, RMSprop, AdaGrad and base optimizer
│ ├── Losses/ & Metrics/ # Loss functions (MSE, CrossEntropy) & evaluation metrics
│ ├── Initializers/ # Weight initializers (Glorot, He, RandomNormal, etc.)
│ └── Utils/ # Keras serialization, dataset pipelines, shape inference
├── TensorFlowNET.Text/ # NLP text processing, tokenization, and embeddings
├── TensorflowNET.Hub/ # Pretrained TensorFlow Hub model loading and execution
└── TensorFlowNET.Recommenders/ # Recommender systems and ranking architectures
```

### Key Architectural Concepts
- **Source of Truth (SOT)**: The official Python TensorFlow codebase located at `refs/py-tensorflow-sot` (`D:\Users\samuel\source\repos\NT\refs\py-tensorflow-sot`) serves as the strict reference implementation for all mathematical behavior, operator semantics, graph/eager execution semantics, and API signatures. Reference files under `refs/` are read-only.
- **Branching, Tagging & Python TensorFlow Version Mapping**:
- **Version Mapping Scheme**: TF.NET versions directly mirror TensorFlow Python versions:
- `v0.150.x` / `v0.15.x` -> Python TensorFlow `v2.15` / `v1.15`
- `master` / `v0.110.x` / `v0.100.x` -> Python TensorFlow `v2.11` / `v2.10`
- `v0.6x` -> Python TensorFlow `v2.6`
- `v0.40` -> Python TensorFlow `v2.4`
- **Branches**:
- `master`: Main development line targeting parity with TensorFlow 2.10+.
- Maintenance / version branches (e.g. `v0.6x`, `v0.15-tensorflow1.15`): Target specific legacy TensorFlow releases.
- **Tags**: Semantic versioning prefixed with `v` (e.g. `v0.110.4-Transformer-Model`, `v0.100.5`), used by CI/CD packaging to determine the base version.
- **Releases & CI/CD Pipeline**:
- Automatic packaging triggered on PR merge to `master` with `auto-release` label (`.github/workflows/release_prepare.yml` -> `release.yml`).
- Produces `-nightly` packages pushed to MyGet feed (`https://www.myget.org/F/scisharp/api/v3/index.json`) and stable releases pushed to NuGet (`TensorFlow.NET`, `TensorFlow.Keras`, `Tensorflow.Hub`).
- **Python / C# Idiom Parity**: Standard static imports provide syntax matching Python TensorFlow:
```csharp
using static Tensorflow.Binding;
using static Tensorflow.KerasApi;
using Tensorflow;
using Tensorflow.NumPy;
```
- **Execution Modes**: Supports both `Eager` mode (imperative tensor operations) and `Graph` mode (`tf.Graph`, `tf.Session`, `tf.placeholder`).
- **Native Interop Lifecycle**: Native `c_api` allocations (`SafeHandle`, `TF_Tensor`, `TF_Status`, `TF_Operation`) are wrapped via `DisposableObject` and memory lifetime management in `TensorFlowNET.Core`.

---

## Roadmap & SOT Feature Gap Objectives

The official engineering roadmap targeting modern Python TensorFlow (v2.16 LTS through v2.22+) parity and .NET 8/9 runtime modernization is maintained in **[ROADMAP.md](ROADMAP.md)**.


2 changes: 2 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
-->
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn),1573,1591,1712</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>13.0</LangVersion>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions GEMINI.md
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,29 @@ More adcanced examples could be found in [TensorFlow.NET Examples](https://githu

## Version Relationships

| TensorFlow.NET Versions | tensorflow 1.14, cuda 10.0 | tensorflow 1.15, cuda 10.0 | tensorflow 2.3, cuda 10.1 | tensorflow 2.4, cuda 11 | tensorflow 2.7, cuda 11 |tensorflow 2.10, cuda 11 |
| -------------------------- | ------------- | -------------- | ------------- | ------------- | ------------ | ------------ |
| tf.net 0.10x, tf.keras 0.10 | | | | | | x |
| tf.net 0.7x, tf.keras 0.7 | | | | | x | |
| tf.net 0.4x, tf.keras 0.5 | | | | x | | |
| tf.net 0.3x, tf.keras 0.4 | | | x | | | |
| tf.net 0.2x | | x | x | | | |
| tf.net 0.15 | x | x | | | | |
| tf.net 0.14 | x | | | | | |
| TensorFlow.NET Versions | tensorflow 1.14, cuda 10.0 | tensorflow 1.15, cuda 10.0 | tensorflow 2.3, cuda 10.1 | tensorflow 2.4, cuda 11 | tensorflow 2.7, cuda 11 |tensorflow 2.10, cuda 11 | tensorflow 2.15+ (target) |
| -------------------------- | ------------- | -------------- | ------------- | ------------- | ------------ | ------------ | ------------ |
| tf.net 0.150x, tf.keras 0.150| | | | | | | x |
| tf.net 0.10x, tf.keras 0.10 | | | | | | x | |
| tf.net 0.7x, tf.keras 0.7 | | | | | x | | |
| tf.net 0.4x, tf.keras 0.5 | | | | x | | | |
| tf.net 0.3x, tf.keras 0.4 | | | x | | | | |
| tf.net 0.2x | | x | x | | | | |
| tf.net 0.15 | x | x | | | | | |
| tf.net 0.14 | x | | | | | | |


```
tf.net 0.4x -> tf native 2.4
tf.net 0.6x -> tf native 2.6
tf.net 0.7x -> tf native 2.7
tf.net 0.10x -> tf native 2.10
tf.net 0.150x -> tf native 2.15
```

## Roadmap & SOT Objectives (TensorFlow 2.16 - 2.22+ Parity)

The official engineering roadmap targeting modern Python TensorFlow (v2.16 LTS through v2.22+) parity and .NET 8/9 runtime modernization is maintained in **[ROADMAP.md](ROADMAP.md)**.
...
```

Expand Down
104 changes: 104 additions & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Comprehensive Roadmap: Next Version Release of TensorFlow.NET (TF 2.16 - 2.22+ Parity)

## Context & Objectives
This document establishes the official engineering roadmap to release the next major generation of **TensorFlow.NET** (`TensorFlow.NET`, `TensorFlow.Keras`, and `SciSharp.TensorFlow.Redist`).

The overarching goal is to achieve architectural, mathematical, and behavioral parity with modern Python TensorFlow (v2.16 LTS through v2.22+ located in `refs/py-tensorflow-sot`), transitioning to a modern **.NET 8.0 / .NET 9.0** high-performance runtime baseline while preserving backward compatibility for existing enterprise consumers.

---

## Architectural Decisions & Foundations (Settled Design Tree)

1. **Target SOT Baseline**: Staged rollout starting with **TensorFlow 2.16 LTS** (Milestone 1: Keras 3 engine + C-API modernization) progressing to **TensorFlow 2.22** (Milestone 2: Sub-byte FP8/Int4 quantization + latest TSL kernels).
2. **Runtime Framework**: Pure **.NET 8.0 / .NET 9.0** baseline. Drops `.NET Standard 2.0` and `.NET 6.0` legacy constraints to leverage `System.Runtime.Intrinsics` (AVX-512 / ARM Neon), zero-copy `Span<T>` / `Memory<T>`, and native `Half` / `Int128`.
3. **Keras Modernization**: Complete clean-room rewrite of **Keras 3** as primary (`Tensorflow.Keras.*`), featuring pure symbolic `KerasTensor` DAGs, stateless layers, and universal `.keras` zip archive serialization. Legacy Keras 2.x code is segregated into `Tensorflow.Keras.Legacy` with `[Obsolete]` migration guides for seamless retro-compatibility.
4. **Gradient Parity**: Programmatic generation of all ~382 op gradients via a dedicated C# Roslyn CLI tool (`tools/TensorFlow.GradientGen`) parsing Python SOT AST.
5. **Memory & Buffer Lifecycle**: Zero-copy unmanaged memory managers (`UnmanagedMemoryManager<T>`), strict `SafeHandle` wrapping for native pointers, and SIMD hardware acceleration.
6. **Testing & Parity Verification**: Hybrid verification strategy using static golden fixtures (`.npy`/`.json`) for 100% headless CI runs and a live Python SOT dual-runner tool for active development.

---

## Phased Implementation Roadmap

### Phase 1: Runtime Baseline & Foundational C-API / DTypes [COMPLETED]
- [x] **1.1 Project & Solution Modernization (.NET 8.0 / 9.0)**: Target `<TargetFrameworks>net8.0;net9.0</TargetFrameworks>`, `<AllowUnsafeBlocks>true</AllowUnsafeBlocks>`, C# 13.
- [x] **1.2 Native C-API Bindings & Status Payloads**: Implement `TF_SetPayload`, `TF_ForEachPayload`, `TF_SetStatusFromIOError`, `TF_TensorBitcastFrom`, `TF_TensorIsAligned`, `TF_TensorDefaultAlignment`, `TF_TensorElementCount`.
- [x] **1.3 DType Modernization & Bug Fixes**: Correct `float16`/`bfloat16` mappings in `dtypes.cs`, add full FP8 and sub-byte type enum definitions (`TF_FLOAT8_E5M2`..`TF_FLOAT4_E2M1FN`).

---

### Phase 2: Op Gradients Programmatic Generator & NumPy Parity [UPCOMING]

#### 2.1 Programmatic Gradient Generation Tool (`tools/TensorFlow.GradientGen`)
- Develop standalone CLI tool `tools/TensorFlow.GradientGen` to:
- Parse Python AST and gradient registration decorators (`@ops.RegisterGradient`) across all files in `refs/py-tensorflow-sot/tensorflow/python/ops/*_grad.py`.
- Emit idiomatic C# partial classes in `src/TensorFlowNET.Core/Gradients/Generated/` implementing `[RegisterGradient("OpName")]`.
- Expand registration from current 92 ops to full SOT parity (~382 ops) across math, nn, image, linalg, sparse, and tensor arrays.
- **Second-Order Gradients**:
- Implement nested gradient formulations for `SoftsignGrad`, `ReluGrad`, `TanhGrad`, `SoftplusGrad`, `SigmoidGrad`, `SqrtGrad`, and `FusedBatchNormGrad`.

#### 2.2 NumPy Operators & Multi-Axis Slicing Parity
- **Numerical Parity (`np.isclose` & `np.allclose`)**:
- Implement complete tolerance math in `src/TensorFlowNET.Core/NumPy/Numpy.cs` and `NumPy.Logical.cs`:
$$\text{diff} \le \text{atol} + \text{rtol} \times |b|$$
- Implement integer overflow prevention (`maximum(a, b) - minimum(a, b)`) and `equal_nan` handling.
- **Dynamic Multidimensional Slicing (`src/TensorFlowNET.Core/Tensors/Tensor.Indexing.cs`)**:
- Add full support for Ellipsis (`...`), `NewAxis` / `None`, negative step strides, and dynamic tensor-valued slice indices using the native `StridedSlice` kernel.

---

### Phase 3: Clean-Room Keras 3 Engine & Modern Serialization

#### 3.1 Symbolic Tracing DAG (`KerasTensor`)
- Decouple Keras functional construction from active native C-API `tf.Graph` instances.
- Introduce `KerasTensor` representing symbolic shapes, dtypes, and inbound/outbound node connections.
- Implement topological sort in `src/TensorFlowNET.Keras/Engine/Functional.cs` to resolve `Functional` execution graphs purely from `KerasTensor` outputs.
- Move legacy graph-coupled Keras 2 classes to `src/TensorFlowNET.Keras/Legacy/` (`Tensorflow.Keras.Legacy` namespace) and tag with `[Obsolete]` migration attributes.

#### 3.2 Universal `.keras` Zip Archive Serialization
- Implement native `.keras` zip archive reading and writing in `src/TensorFlowNET.Keras/Saving/`:
- `config.json`: Model topology and layer hyperparameters.
- `metadata.json`: Keras version, build timestamp, framework signatures.
- `model.weights.h5` / `variables.safetensors`: Serialized weight arrays.
- Align `get_config()` and `from_config()` serialization schemas across all layers, optimizers, losses, and metrics with Python Keras 3.
- Maintain legacy loaders for SavedModel (`saved_model.pb`) and `.h5`.

#### 3.3 Modular Step Execution Loop
- Refactor `Model` execution pipeline into modular, overridable step methods:
- `train_step(data)`
- `test_step(data)`
- `predict_step(data)`
- Refactor `LossesContainer` and `MetricsContainer` for consistent reduction, masking, and sample weight support.

---

### Phase 4: Verification, Parity Test Harness & Release Pipeline

#### 4.1 Hybrid Parity Test Harness
- **Static Golden Fixtures (`test/TensorFlowNET.UnitTest/Parity/`)**:
- Generate golden test vectors (`.npy`/`.json`) from Python SOT for all newly generated op gradients, NumPy operators, and Keras 3 layers.
- Ensure fast, deterministic, 100% headless CI test execution.
- **Live Dual-Runner Tool (`tools/TensorFlow.ParityRunner`)**:
- CLI tool running side-by-side execution in C# and Python (via local `.venv`), asserting identical outputs within numerical tolerances for new ops.

#### 4.2 Release Packaging & Publishing
- Bump version to `v0.200.0` (or `v1.0.0`) in `Directory.Build.props`.
- Update GitHub Actions CI/CD workflows (`.github/workflows/build_and_test.yml`, `release.yml`):
- Add multi-platform matrix builds (Windows, Linux, macOS ARM64/x64).
- Package and publish nightly builds to MyGet and stable packages to NuGet (`TensorFlow.NET`, `TensorFlow.Keras`, `SciSharp.TensorFlow.Redist`).

---

### Key File Locations for Changes

| Subsystem | Target Files |
| :--- | :--- |
| **Project & Build** | `Directory.Build.props`, `TensorFlow.NET.sln`, `.github/workflows/build_and_test.yml` |
| **C-API & Status** | `src/TensorFlowNET.Core/Status/c_api.status.cs`, `Status.cs`, `c_api.tensor.cs` |
| **DTypes & Memory** | `src/TensorFlowNET.Core/Tensors/dtypes.cs`, `TF_DataType.cs`, `Tensor.cs` |
| **Gradient Generator** | `tools/TensorFlow.GradientGen/`, `src/TensorFlowNET.Core/Gradients/Generated/` |
| **NumPy & Slicing** | `src/TensorFlowNET.Core/NumPy/Numpy.cs`, `NumPy.Logical.cs`, `Tensor.Indexing.cs` |
| **Keras 3 Engine** | `src/TensorFlowNET.Keras/Engine/KerasTensor.cs`, `Functional.cs`, `Layer.cs`, `Model.cs` |
| **Keras Legacy** | `src/TensorFlowNET.Keras/Legacy/` |
| **Serialization** | `src/TensorFlowNET.Keras/Saving/KerasZipSaver.cs`, `Saving/KerasZipLoader.cs` |
| **Test Harness** | `tools/TensorFlow.ParityRunner/`, `test/TensorFlowNET.UnitTest/Parity/` |
Loading
Loading