synor/sdk/flutter/example/example.dart
Gulshan Yadav 62ec3c92da feat(sdk): add Flutter/Dart SDK for Synor Compute
Complete SDK implementation for Flutter and Dart applications:

lib/src/types.dart:
- Precision, ProcessorType, Priority, JobStatus enums
- SynorConfig for client configuration
- MatMulOptions, Conv2dOptions, AttentionOptions, InferenceOptions
- PricingInfo and UsageStats data classes
- SynorException for error handling

lib/src/tensor.dart:
- Full Tensor class with shape, dtype, and data
- Factory constructors: zeros, ones, rand, randn, eye, linspace, arange
- Operations: reshape, transpose, flatten
- Statistics: sum, mean, std, min, max, argmin, argmax
- Element-wise: add, sub, mul, div, scalar ops
- Activations: relu, sigmoid, tanh, softmax
- JSON serialization with base64-encoded binary data

lib/src/job.dart:
- JobResult with status, result, timing, and cost
- Job class with WebSocket streaming and HTTP polling
- JobStatusUpdate for real-time progress tracking
- JobBatch for parallel job management

lib/src/client.dart:
- SynorCompute main client
- Operations: matmul, conv2d, attention, elementwise, reduce
- LLM inference with streaming support
- Tensor upload/download/delete
- Job management: submit, cancel, list
- Pricing and usage statistics

Platform support: Android, iOS, Linux, macOS, Web, Windows
2026-01-11 14:27:55 +05:30

178 lines
4.9 KiB
Dart

import 'dart:io';
import 'package:synor_compute/synor_compute.dart';
/// Example usage of Synor Compute SDK for Flutter/Dart
void main() async {
// Initialize client with API key
final client = SynorCompute(
apiKey: Platform.environment['SYNOR_API_KEY'] ?? 'your-api-key',
// Optional: customize defaults
defaultProcessor: ProcessorType.auto,
defaultPrecision: Precision.fp32,
defaultPriority: Priority.normal,
);
try {
// Check service health
final isHealthy = await client.healthCheck();
print('Service healthy: $isHealthy\n');
// Example 1: Matrix multiplication
await matrixMultiplicationExample(client);
// Example 2: Tensor operations
await tensorOperationsExample(client);
// Example 3: LLM inference
await llmInferenceExample(client);
// Example 4: Streaming inference
await streamingInferenceExample(client);
// Example 5: Pricing and usage
await pricingExample(client);
} finally {
// Always dispose client to release resources
client.dispose();
}
}
/// Matrix multiplication example
Future<void> matrixMultiplicationExample(SynorCompute client) async {
print('=== Matrix Multiplication ===');
// Create random matrices
final a = Tensor.rand([256, 512]);
final b = Tensor.rand([512, 256]);
print('A: ${a.shape}');
print('B: ${b.shape}');
// Perform multiplication on GPU with FP16 precision
final result = await client.matmul(
a,
b,
options: MatMulOptions(
precision: Precision.fp16,
processor: ProcessorType.gpu,
priority: Priority.high,
),
);
if (result.isSuccess) {
print('Result: ${result.result!.shape}');
print('Execution time: ${result.executionTimeMs}ms');
print('Cost: \$${result.cost?.toStringAsFixed(6)}');
print('Processor: ${result.processor?.value}');
} else {
print('Error: ${result.error}');
}
print('');
}
/// Local tensor operations example
Future<void> tensorOperationsExample(SynorCompute client) async {
print('=== Tensor Operations ===');
// Create tensors
final x = Tensor.randn([100], mean: 0.0, std: 1.0);
print('Random normal tensor: mean=${x.mean().toStringAsFixed(4)}, '
'std=${x.std().toStringAsFixed(4)}');
// Create identity matrix
final eye = Tensor.eye(4);
print('Identity matrix:\n${eye.toNestedList()}');
// Create linspace
final linspace = Tensor.linspace(0, 10, 5);
print('Linspace [0, 10, 5]: ${linspace.toNestedList()}');
// Reshape operations
final matrix = Tensor.arange(0, 12).reshape([3, 4]);
print('Reshaped [0..12] to [3,4]:\n${matrix.toNestedList()}');
// Transpose
final transposed = matrix.transpose();
print('Transposed to ${transposed.shape}');
// Activations
final input = Tensor(shape: [5], data: [-2.0, -1.0, 0.0, 1.0, 2.0]);
print('ReLU of $input: ${input.relu().toNestedList()}');
print('Sigmoid of $input: ${input.sigmoid().toNestedList()}');
// Softmax
final logits = Tensor(shape: [4], data: [1.0, 2.0, 3.0, 4.0]);
print('Softmax of $logits: ${logits.softmax().toNestedList()}');
print('');
}
/// LLM inference example
Future<void> llmInferenceExample(SynorCompute client) async {
print('=== LLM Inference ===');
final result = await client.inference(
'llama-3-70b',
'What is the capital of France? Answer in one word.',
options: InferenceOptions(
maxTokens: 10,
temperature: 0.1,
processor: ProcessorType.lpu, // Use LPU for LLM
),
);
if (result.isSuccess) {
print('Response: ${result.result}');
print('Time: ${result.executionTimeMs}ms');
} else {
print('Error: ${result.error}');
}
print('');
}
/// Streaming inference example
Future<void> streamingInferenceExample(SynorCompute client) async {
print('=== Streaming Inference ===');
print('Response: ');
await for (final token in client.inferenceStream(
'llama-3-70b',
'Write a short poem about distributed computing.',
options: InferenceOptions(
maxTokens: 100,
temperature: 0.7,
),
)) {
stdout.write(token);
}
print('\n');
}
/// Pricing and usage example
Future<void> pricingExample(SynorCompute client) async {
print('=== Pricing Information ===');
final pricing = await client.getPricing();
print('Current spot prices:');
for (final p in pricing) {
print(' ${p.processor.value.toUpperCase().padRight(8)}: '
'\$${p.pricePerSecond.toStringAsFixed(6)}/sec, '
'${p.availableUnits} units available, '
'${p.utilizationPercent.toStringAsFixed(1)}% utilized');
}
print('');
// Get usage stats
final usage = await client.getUsage();
print('Usage Statistics:');
print(' Total jobs: ${usage.totalJobs}');
print(' Completed: ${usage.completedJobs}');
print(' Failed: ${usage.failedJobs}');
print(' Total compute time: ${usage.totalComputeSeconds.toStringAsFixed(2)}s');
print(' Total cost: \$${usage.totalCost.toStringAsFixed(4)}');
print('');
}