Enhancing WASM API & Migrating To Next.js Frontend
Hey guys! Today, we're diving deep into how we can enhance our WASM API and migrate to a Next.js frontend. This is a game-changer for our project, as it will allow us to expose the full potential of our Rust coreβs neural network functionality and create a more interactive and scalable platform. Let's break it down!
Overview π
Our primary goal is to take our current setup, which only scratches the surface of what our Rust core can do, and transform it into a robust, user-friendly ML experimentation platform. This involves three key steps:
- Expanding the WASM API: We need to expose all the awesome features currently locked away in our Rust core.
- Migrating to Next.js: Say goodbye to Vanilla JS! We're moving to a modern, scalable frontend framework.
- Creating an interactive ML platform: Think rich visualizations, real-time updates, and a seamless user experience.
This is going to be a fantastic journey, and I'm super excited to share the details with you.
Current State Analysis β β
Before we jump into the solutions, let's take a good look at where we stand. This will help us understand the scope of the project and the impact of the changes we're about to make.
Available in Rust Core (Fully Implemented) β
Our Rust core is a powerhouse! It's packed with features that we can leverage. Hereβs a quick rundown:
- Optimizers: Weβve got a whole toolbox of optimizers, including SGD, SGD with Momentum, Adam, RMSProp, and GradientDescent. These are the engines that drive our neural networks.
- Task Types: Whether it's BinaryClassification,MultiClassification, orRegression, weβve got the tasks covered. This means we can tackle a wide range of machine-learning problems.
- Regularization: L1, L2, and Elastic Net regularization are at our disposal via RegularizationConfig. This helps prevent overfitting and ensures our models generalize well.
- Loss Functions: We support BinaryCrossEntropy, CrossEntropy, and MeanSquaredError. These functions are crucial for training our models effectively.
- Comprehensive Metrics: We're tracking everything from Accuracy, Precision, and Recall to F1Score and MSE. This gives us a detailed view of our model's performance.
- Data Processing: Standardization, normalization, and train/validation splits are all handled. Preprocessing data is a breeze!
- Built-in Datasets: Boston Housing (regression), Iris (multiclass), and Breast Cancer (binary) datasets are ready to go. This makes it easy to get started with experimentation.
- Model Architecture: Flexible layer composition with various activations means we can design complex neural networks.
Itβs like having a fully equipped lab ready for any experiment!
Current WASM API Limitations (src/adapters/presentation/wasm/bindings.rs) β
Now, here's the catch. Our current WASM API doesn't expose all these fantastic features. It's like having a sports car but only being able to drive it in first gear. Hereβs what weβre missing:
- Optimizer: Only SGD is hardcoded (line 491: let mut optimizer = SGD::new(...)). We need to let users choose their optimizer!
- Task Support: We're limited to binary classification. It's time to open up to multi-classification and regression tasks.
- No Regularization: L1/L2/Elastic Net are not exposed. We're missing out on crucial regularization techniques.
- Limited Metrics: Only basic accuracy is available. We need a more comprehensive view of our model's performance.
- No Dataset Loading: Built-in datasets are inaccessible from the web. Let's unlock those datasets!
- Fixed Architecture: No dynamic model composition. Users should be able to design their own architectures.
Current Frontend Limitations (www/ - Vanilla JS) β
Our frontend, built with Vanilla JS, is also holding us back. It's functional, but it lacks the robustness and scalability we need for a rich ML platform. Think of it as the basic starter house before we build our dream mansion.
- Technology Stack: Plain HTML/CSS/JS with basic Chart.js. It's time for an upgrade!
- UI Controls: Limited to 5 basic hyperparameters. We need more control over our experiments.
- Model Configuration: Single hidden layer only. We can do so much better!
- Data Sources: Artificial 2D points generation only. Letβs bring in real-world datasets.
- Visualization: Basic loss/accuracy line charts. We need more insightful visualizations.
- User Experience: Static forms, no real-time interaction. Let's make it dynamic and engaging.
Proposed Solution: Next.js Migration + Full Feature Exposure π¨
Okay, guys, hereβs the plan! We're going to migrate our frontend to Next.js and fully expose the features of our Rust core through an enhanced WASM API. This will give us a powerful, flexible, and user-friendly platform for ML experimentation.
Phase 1: Next.js Frontend Setup
First things first, letβs set up our shiny new Next.js frontend. This is the foundation upon which we'll build our interactive ML platform.
- [ ] Initialize Next.js 14+ with App Router
- TypeScript configuration: We're going to use TypeScript for type safety and a better development experience.
- Tailwind CSS for styling: Tailwind will help us create a modern, responsive design quickly.
- Component-based architecture: This will make our codebase modular and maintainable.
 
- [ ] WASM Integration Setup
- Dynamic import strategy for WebAssembly modules: We'll load WASM modules dynamically to improve performance.
- Client-side only rendering for WASM components: WASM components will run on the client-side for responsiveness.
- Proper TypeScript definitions: We'll create TypeScript definitions for our WASM API to ensure type safety.
 
- [ ] Modern Development Workflow
- Hot reload for rapid iteration: Changes will be reflected in real-time during development.
- Component story development: We'll use a component story workflow to build and test UI components in isolation.
- Proper state management (Zustand/Context): Weβll use Zustand or React Context for efficient state management.
 
Phase 2: Enhanced WASM API
Next up, let's enhance our WASM API to expose all the cool features of our Rust core. This is where we unlock the full potential of our backend.
- [ ] Optimizer Selection API
#[wasm_bindgen]
pub enum OptimizerType { SGD, SGDMomentum, Adam, RMSProp, GradientDescent }
#[wasm_bindgen]
pub struct OptimizerConfig {
    optimizer_type: OptimizerType,
    learning_rate: f64,
    // Dynamic parameters based on optimizer type
    momentum: Option<f64>,     // SGD Momentum
    beta1: Option<f64>,        // Adam
    beta2: Option<f64>,        // Adam  
    epsilon: Option<f64>,      // Adam, RMSProp
}
- [ ] Task Type Support
#[wasm_bindgen]
pub struct TrainingRequest {
    task_kind: TaskKind,
    optimizer_config: OptimizerConfig,
    regularization: Option<RegularizationConfig>,
    model_architecture: Vec<LayerConfig>,
    // ... other configs
}
- [ ] Regularization Configuration
#[wasm_bindgen]
pub struct JsRegularizationConfig {
    l1_lambda: f64,
    l2_lambda: f64, 
    apply_to_bias: bool,
}
- [ ] Built-in Dataset Access
#[wasm_bindgen]
pub fn load_boston_housing() -> JsResult<JsDataset>;
#[wasm_bindgen] 
pub fn load_iris_dataset() -> JsResult<JsDataset>;
#[wasm_bindgen]
pub fn load_breast_cancer_dataset() -> JsResult<JsDataset>;
- [ ] Comprehensive Metrics API
#[wasm_bindgen]
pub fn compute_classification_metrics(predictions: &JsTensor, targets: &JsTensor) -> JsClassificationMetrics;
#[wasm_bindgen]
pub fn compute_regression_metrics(predictions: &JsTensor, targets: &JsTensor) -> JsRegressionMetrics;
Phase 3: Rich Next.js Interface Components
Now for the fun part! Weβre going to build a set of rich, interactive components in Next.js that will make our ML platform a joy to use. Think intuitive controls, real-time feedback, and beautiful visualizations.
ποΈ Optimizer Configuration Panel
<OptimizerSelector 
  selected={optimizer}
  onChange={setOptimizer}
  hyperparameters={{
    learningRate: 0.001,
    momentum: 0.9,
    beta1: 0.9,
    beta2: 0.999
  }}
/>
ποΈ Model Architecture Builder
<ModelArchitectureBuilder
  layers={[
    { type: 'Dense', units: 64, activation: 'ReLU' },
    { type: 'Dense', units: 32, activation: 'ReLU' },
    { type: 'Dense', units: 1, activation: 'Linear' }
  ]}
  onLayerAdd={handleAddLayer}
  onLayerRemove={handleRemoveLayer}
/>
π Interactive Dataset Explorer
<DatasetExplorer
  availableDatasets={['Boston Housing', 'Iris', 'Breast Cancer', 'Custom Upload']}
  selected={dataset}
  onSelect={setDataset}
  previewData={dataPreview}
  taskType={taskType}
/>
π Real-time Training Dashboard
<TrainingDashboard
  metrics={trainingMetrics}
  lossHistory={lossHistory}
  validationHistory={validationHistory}
  currentEpoch={epoch}
  isTraining={isTraining}
/>
π§ Regularization Controls
<RegularizationPanel
  l1Lambda={l1Lambda}
  l2Lambda={l2Lambda}
  onL1Change={setL1Lambda}
  onL2Change={setL2Lambda}
  lossVisualization={regularizedLoss}
/>
π― Task-Specific Views
- Classification: Confusion matrix, ROC curves, decision boundaries
- Regression: Residual plots, prediction vs actual scatter
- Multi-task: Tabbed interface with appropriate visualizations
Phase 4: Advanced Features
Once we have the basics in place, we can start adding some really cool advanced features. This is where we take our platform from great to exceptional.
- [ ] Model Export/Import
- Save trained models as JSON
- Load pre-trained models
- Model comparison interface
 
- [ ] Hyperparameter Optimization
- Grid search interface
- Random search
- Bayesian optimization (future)
 
- [ ] Interactive Visualizations
- Real-time decision boundary updates (2D classification)
- Loss landscape visualization
- Gradient flow visualization
 
- [ ] Educational Features
- Step-by-step training explanation
- Mathematical formula display
- Interactive tutorials
 
Technical Implementation Strategy ποΈ
Let's dive into the nitty-gritty details of how we're going to structure our project and integrate WASM with Next.js.
Directory Structure
We'll be using a clear and organized directory structure to keep our codebase maintainable and scalable.
βββ frontend/                 # New Next.js application
β   βββ src/
β   β   βββ app/             # App router pages
β   β   βββ components/      # Reusable UI components
β   β   βββ hooks/          # Custom React hooks for WASM
β   β   βββ lib/            # WASM integration utilities
β   β   βββ stores/         # State management
β   β   βββ types/          # TypeScript definitions
β   βββ public/             # Static assets + WASM files
β   βββ next.config.js      # Next.js configuration
βββ src/                     # Existing Rust code
βββ pkg/                    # Generated WASM bindings
WASM Integration Pattern
We'll use a custom React hook to load and manage our WASM module. This will make it easy to access WASM functions from our components.
// hooks/useWASM.ts
export const useWASM = () => {
  const [wasmModule, setWasmModule] = useState<typeof import('../pkg/multilayer_perceptron') | null>(null);
  
  useEffect(() => {
    import('../pkg/multilayer_perceptron').then(setWasmModule);
  }, []);
  
  return wasmModule;
};
Build Configuration
We'll configure Next.js to handle WASM files and static site generation.
// next.config.js
module.exports = {
  output: 'export',  // Static site generation
  trailingSlash: true,
  images: { unoptimized: true },
  webpack: (config) => {
    config.experiments = { ...config.experiments, asyncWebAssembly: true };
    return config;
  }
};
Success Criteria π―
How will we know if we've succeeded? Here are the criteria we'll be using to measure our progress.
Functional Requirements
- [ ] All 5 optimizers selectable with appropriate hyperparameters
- [ ] Support for all 3 task types (binary/multiclass/regression)
- [ ] L1/L2/Elastic Net regularization with real-time effect visualization
- [ ] All built-in datasets loadable and explorable
- [ ] Dynamic model architecture with 1-5 hidden layers
- [ ] Comprehensive metrics appropriate to each task type
User Experience Requirements
- [ ] Modern, responsive design with smooth interactions
- [ ] Real-time parameter updates with immediate visual feedback
- [ ] Progressive disclosure of advanced features
- [ ] Educational tooltips and explanations
- [ ] Mobile-friendly responsive layout
Technical Requirements
- [ ] Static site generation for GitHub Pages deployment
- [ ] Bundle size optimization (target: <2MB total)
- [ ] TypeScript type safety throughout
- [ ] Component reusability and maintainability
- [ ] Cross-browser compatibility (Chrome, Firefox, Safari, Edge)
Deployment Strategy π¦
Let's talk about how we'll deploy our enhanced platform. We'll use a combination of manual steps and CI/CD to ensure a smooth process.
Development Workflow
- WASM Build: wasm-pack build --target web --out-dir pkg
- Frontend Dev: cd frontend && npm run dev
- Production Build: npm run build && npm run export
- GitHub Pages: Deploy frontend/out/directory
CI/CD Pipeline Enhancement
We'll set up a CI/CD pipeline using GitHub Actions to automate our build and deployment process.
# .github/workflows/deploy.yml (updated)
- name: Build WASM
  run: wasm-pack build --target web --out-dir pkg --release
  
- name: Build Next.js  
  run: cd frontend && npm ci && npm run build
  
- name: Deploy to GitHub Pages
  uses: peaceiris/actions-gh-pages@v3
  with:
    github_token: ${{ secrets.GITHUB_TOKEN }}
    publish_dir: ./frontend/out
Design Philosophy π¨
Our design philosophy will guide us in creating a platform that is not only powerful but also user-friendly and educational.
Progressive Enhancement
- Start with basic functionality, layered with advanced features
- Clear visual hierarchy guiding users from simple to complex workflows
- Graceful degradation for less capable browsers
Educational Focus
- Make ML concepts accessible through interactive visualization
- Provide contextual help and mathematical explanations
- Showcase the impact of different choices in real-time
Performance First
- Lazy loading of heavy components
- Optimized WASM bundle splitting
- Efficient React rendering with proper memoization
Implementation Timeline π
Hereβs a rough timeline for our project. Keep in mind that this is subject to change as we progress.
Week 1-2: Foundation
- Next.js setup with TypeScript + Tailwind
- Basic WASM integration and type definitions
- Core component structure
Week 3-4: WASM API Expansion
- Enhanced optimizer, task, and regularization APIs
- Built-in dataset loading capabilities
- Comprehensive metrics computation
Week 5-6: Core UI Components
- Optimizer selection and configuration
- Model architecture builder
- Dataset explorer and task selection
Week 7-8: Advanced Features
- Real-time training dashboard
- Interactive visualizations
- Export/import functionality
Week 9-10: Polish & Deploy
- Performance optimization
- Cross-browser testing
- Documentation and tutorials
- Production deployment
This enhancement will transform our current proof-of-concept into a comprehensive, educational neural network experimentation platform that showcases the full capabilities of the Rust implementation through a modern, accessible web interface. Iβm stoked to see this all come together, guys!