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 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 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 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 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 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(''); }