# Synor Compute SDK for C#/.NET

Access distributed heterogeneous compute at 90% cost reduction.

## Installation

### NuGet

```bash
dotnet add package SynorCompute
```

### Package Manager Console

```powershell
Install-Package SynorCompute
```

## Quick Start

```csharp
using SynorCompute;

var client = new SynorClient("your-api-key");

// Matrix multiplication on GPU
var a = Tensor.Random(512, 512);
var b = Tensor.Random(512, 512);

var result = await client.MatMulAsync(a, b, new MatMulOptions
{
    Precision = Precision.FP16,
    Processor = ProcessorType.GPU
});

if (result.IsSuccess)
{
    Console.WriteLine($"Time: {result.ExecutionTimeMs}ms");
    Console.WriteLine($"Cost: ${result.Cost}");
}
```

## Tensor Operations

```csharp
// Create tensors
var zeros = Tensor.Zeros(3, 3);
var ones = Tensor.Ones(2, 2);
var random = Tensor.Random(10, 10);
var randn = Tensor.Randn(100);
var eye = Tensor.Eye(3);

// From array
float[,] data = { { 1, 2, 3 }, { 4, 5, 6 } };
var tensor = Tensor.FromArray(data);

// From 1D with shape
var data1d = new float[] { 1, 2, 3, 4, 5, 6 };
var tensor1d = new Tensor(data1d, new[] { 2, 3 });

// Operations
var reshaped = tensor.Reshape(3, 2);
var transposed = tensor.Transpose();

// Math
var mean = tensor.Mean();
var sum = tensor.Sum();
var std = tensor.Std();
```

## Async/Await API

```csharp
// Matrix multiplication
var result = await client.MatMulAsync(a, b, new MatMulOptions
{
    Precision = Precision.FP16,
    Processor = ProcessorType.GPU,
    Strategy = BalancingStrategy.Speed
});

// 2D Convolution
var conv = await client.Conv2DAsync(input, kernel, new Conv2DOptions
{
    Stride = (1, 1),
    Padding = (1, 1)
});

// Attention
var attention = await client.AttentionAsync(query, key, value, new AttentionOptions
{
    NumHeads = 8,
    Flash = true
});
```

## LLM Inference

```csharp
// Single response
var response = await client.InferenceAsync("llama-3-70b", "Explain quantum computing",
    new InferenceOptions
    {
        MaxTokens = 512,
        Temperature = 0.7f
    });
Console.WriteLine(response.Result);

// Streaming with IAsyncEnumerable
await foreach (var chunk in client.InferenceStreamAsync("llama-3-70b", "Write a poem"))
{
    Console.Write(chunk);
}
```

## Configuration

```csharp
var config = new SynorConfig
{
    ApiKey = "your-api-key",
    BaseUrl = "https://api.synor.io/compute/v1",
    DefaultProcessor = ProcessorType.GPU,
    DefaultPrecision = Precision.FP16,
    Timeout = TimeSpan.FromSeconds(30),
    Debug = true
};

var client = new SynorClient(config);
```

## Dependency Injection

```csharp
// In Startup.cs or Program.cs
services.AddSynorCompute(options =>
{
    options.ApiKey = Configuration["Synor:ApiKey"];
    options.DefaultProcessor = ProcessorType.GPU;
});

// In your service
public class ComputeService
{
    private readonly ISynorClient _client;

    public ComputeService(ISynorClient client)
    {
        _client = client;
    }

    public async Task<Tensor> ComputeAsync(Tensor a, Tensor b)
    {
        var result = await _client.MatMulAsync(a, b);
        return result.Data;
    }
}
```

## Error Handling

```csharp
try
{
    var result = await client.MatMulAsync(a, b);
}
catch (SynorApiException ex)
{
    Console.WriteLine($"API Error {ex.StatusCode}: {ex.Message}");
}
catch (SynorNetworkException ex)
{
    Console.WriteLine($"Network error: {ex.Message}");
}
catch (SynorException ex)
{
    Console.WriteLine($"Error: {ex.Message}");
}
```

## LINQ Integration

```csharp
// Process multiple tensors
var tensors = new[] { a, b, c, d };
var results = await Task.WhenAll(
    tensors.Select(t => client.MatMulAsync(t, identity))
);
```

## Types

```csharp
// Processor types
public enum ProcessorType
{
    CPU, GPU, TPU, NPU, LPU, FPGA, Auto
}

// Precision
public enum Precision
{
    FP64, FP32, FP16, BF16, INT8, INT4
}

// Job status
public enum JobStatus
{
    Pending, Running, Completed, Failed, Cancelled
}

// Balancing strategy
public enum BalancingStrategy
{
    Speed, Cost, Energy, Latency, Balanced
}
```

## Cancellation Support

```csharp
var cts = new CancellationTokenSource();

// Cancel after 10 seconds
cts.CancelAfter(TimeSpan.FromSeconds(10));

try
{
    var result = await client.MatMulAsync(a, b, cancellationToken: cts.Token);
}
catch (OperationCanceledException)
{
    Console.WriteLine("Operation was cancelled");
}
```

## Requirements

- .NET 6.0 or later
- System.Text.Json

## Testing

```bash
dotnet test
```

## License

MIT
