Skip to content

Instantly share code, notes, and snippets.

@mohashari
mohashari / snippet-1.py
Created July 5, 2026 01:00
Speculative Decoding in Production: Accelerating LLM Inference via Custom Draft Models — code snippets
import torch
def verify_draft_tokens(
target_probs: torch.Tensor, # Shape: (K+1, vocab_size)
draft_probs: torch.Tensor, # Shape: (K, vocab_size)
draft_tokens: torch.Tensor, # Shape: (K,)
) -> tuple[torch.Tensor, int]:
"""
Executes rejection sampling to verify K draft tokens against target probabilities.
Returns a tensor of accepted tokens and the index of the first rejected token.
@mohashari
mohashari / snippet-1.hcl
Created July 4, 2026 01:03
Automating Database Schema Migrations in CI/CD: Multi-Tenant Zero-Downtime Rollouts with Atlas and GitHub Actions — code snippets
env "local" {
src = "file://schema.hcl"
dev = "docker://postgres/15/dev?search_path=public"
migration {
dir = "file://migrations"
}
format {
migrate {
diff = "{{ sql . \" \" }}"
}
@mohashari
mohashari / snippet-1.yaml
Created July 4, 2026 01:02
Fine-Tuning LoRA Adapters on Kubernetes: Orchestrating Distributed Training with Ray and Ludwig — code snippets
apiVersion: ray.io/v1
kind: RayCluster
metadata:
name: ray-cluster-lora
namespace: ml-platform
spec:
rayVersion: '2.35.0'
headGroupSpec:
rayStartParams:
dashboard-host: '0.0.0.0'
@mohashari
mohashari / snippet-1.go
Created July 4, 2026 01:02
Mitigating SSRF in Serverless: Designing a Dynamic Envoy Proxy Sidecar for AWS ECS Tasks — code snippets
package main
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"time"
@mohashari
mohashari / snippet-1.sql
Created July 4, 2026 01:01
Custom MySQL Binlog Parsers: Building a Real-Time Outbox Pattern Processor in Go — code snippets
CREATE TABLE IF NOT EXISTS outbox_events (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
aggregate_type VARCHAR(255) NOT NULL,
aggregate_id VARCHAR(255) NOT NULL,
event_type VARCHAR(255) NOT NULL,
payload JSON NOT NULL,
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6),
INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
@mohashari
mohashari / snippet-1.go
Created July 4, 2026 01:00
Debugging HTTP/2 Flow Control Exhaustion: Analyzing gRPC Stream Stalls in Production — code snippets
// Typical goroutine block stack trace found during a pprof capture:
// goroutine 4821 [semacquire, 10 minutes]:
// google.golang.org/grpc/internal/transport.(*controlBuffer).get(0xc0001a23c0, 0x1)
// /go/pkg/mod/google.golang.org/grpc@v1.64.0/internal/transport/controlbuf.go:394 +0x95
// google.golang.org/grpc/internal/transport.(*http2Server).Write(0xc00021c000, 0xc0001bc300, 0xc0003c2000, 0x0)
// /go/pkg/mod/google.golang.org/grpc@v1.64.0/internal/transport/http2_server.go:882 +0x14c
// google.golang.org/grpc.(*serverStream).SendMsg(0xc0003c2000, {0x18ba620, 0xc00045e4c0})
// /go/pkg/mod/google.golang.org/grpc@v1.64.0/stream.go:1422 +0x125
// main.(*ReporterServer).StreamReports(0xc0002ba140, 0xc00028e180, 0xc0003c2000)
// /workspace/main.go:84 +0xbc
@mohashari
mohashari / snippet-1.hcl
Created July 3, 2026 12:54
Preventing Credential Leakage: Automated Just-In-Time AWS IAM Session Token Generation in GitHub Actions using OIDC and Vault — code snippets
resource "vault_jwt_auth_backend" "github" {
description = "JWT authentication backend for GitHub Actions"
path = "github-actions"
oidc_discovery_url = "https://token.actions.githubusercontent.com"
bound_issuer = "https://token.actions.githubusercontent.com"
}
@mohashari
mohashari / snippet-1.txt
Created July 3, 2026 12:53
Building Custom TensorRT-LLM Plugins for Low-Latency FP8 Inference at Scale — code snippets
#pragma once
#include <NvInferRuntime.h>
#include <vector>
#include <string>
namespace tensorrt_llm::plugins {
class FusedSwiGLUQuantPlugin : public nvinfer1::IPluginV2DynamicExt {
public:
FusedSwiGLUQuantPlugin(float scale, bool has_dynamic_scale);
@mohashari
mohashari / snippet-1.txt
Created July 1, 2026 01:03
Designing an Anomaly Detection Engine for High-Cardinality Metrics Using Holt-Winters in Rust — code snippets
// The core math engine for additive Holt-Winters triple exponential smoothing.
// Maintains rolling level, trend, and seasonal components in a memory-efficient manner.
pub struct HoltWinters {
alpha: f64, // Level smoothing parameter (0 < alpha < 1)
beta: f64, // Trend smoothing parameter (0 < beta < 1)
gamma: f64, // Seasonal smoothing parameter (0 < gamma < 1)
period: usize, // Seasonality period length (e.g. 288 steps for 24h at 5m resolution)
level: f64, // Current estimate of the base level
trend: f64, // Current estimate of the trend direction
@mohashari
mohashari / snippet-1.py
Created July 1, 2026 01:02
Optimizing Key-Value Cache Management in LLM Serving with PagedAttention and CUDA IPC — code snippets
import torch
from typing import List, Set, Dict
class PhysicalBlockAllocator:
def __init__(
self,
num_blocks: int,
block_size: int,
num_layers: int,
num_heads: int,