This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch.distributed as dist | |
| from torch.nn.parallel import DistributedDataParallel as DDP | |
| dist.init_process_group(backend="nccl") | |
| model = MyModel().cuda() | |
| model = DDP(model, device_ids=[local_rank]) | |
| for batch in dataloader: | |
| optimizer.zero_grad() | |
| outputs = model(batch) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| model = MyModel().cuda() | |
| optimizer = torch.optim.AdamW(model.parameters(), lr=3e-4) | |
| # Compile once, reuse across the training loop | |
| compiled_model = torch.compile(model) | |
| for batch in dataloader: | |
| inputs, targets = batch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from transformers import pipeline | |
| # Load a pretrained NER model for entity extraction | |
| ner_pipeline = pipeline( | |
| "ner", | |
| model="dslim/bert-base-NER", | |
| aggregation_strategy="simple" | |
| ) | |
| def extract_metadata_entities(ocr_text: str) -> dict: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| from sklearn.neighbors import NearestNeighbors | |
| # Rows = patrons (anonymized), columns = books, values = checkout counts | |
| checkout_matrix = np.array([ | |
| [3, 0, 1, 0, 2], | |
| [0, 4, 0, 1, 0], | |
| [2, 0, 3, 0, 1], | |
| [0, 1, 0, 5, 0], | |
| ]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package terraform.finops | |
| deny[msg] { | |
| resource := input.resource_changes[_] | |
| resource.type == "aws_instance" | |
| allowed_types := {"t3.micro", "t3.small", "t3.medium"} | |
| not allowed_types[resource.change.after.instance_type] | |
| msg := sprintf( | |
| "Instance type %v is not in the approved list for this environment", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import boto3 | |
| from datetime import datetime, timedelta | |
| def find_idle_instances(cpu_threshold=5.0, days=7): | |
| ec2 = boto3.client("ec2") | |
| cloudwatch = boto3.client("cloudwatch") | |
| idle_instances = [] | |
| instances = ec2.describe_instances( | |
| Filters=[{"Name": "instance-state-name", "Values": ["running"]}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import boto3 | |
| REQUIRED_TAGS = {"team", "environment", "project"} | |
| def lambda_handler(event, context): | |
| ec2 = boto3.client("ec2") | |
| detail = event["detail"] | |
| if detail["eventName"] != "RunInstances": | |
| return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.cluster import KMeans | |
| import pandas as pd | |
| def classify_workload_patterns(usage_df, n_clusters=4): | |
| """ | |
| Cluster storage volumes by access frequency, size, and | |
| read/write ratio to recommend appropriate storage tiers. | |
| """ | |
| features = usage_df[["avg_daily_accesses", "size_gb", "read_write_ratio"]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| from statsmodels.tsa.holtwinters import ExponentialSmoothing | |
| def forecast_storage_demand(historical_usage, periods_ahead=24): | |
| """ | |
| Forecast storage utilization for the next N hours using | |
| Holt-Winters exponential smoothing. | |
| """ | |
| model = ExponentialSmoothing( | |
| historical_usage, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| import random | |
| import boto3 | |
| from botocore.exceptions import ClientError | |
| s3 = boto3.client("s3") | |
| def upload_with_retry(local_path: str, bucket: str, s3_key: str, max_retries: int = 5) -> bool: | |
| for attempt in range(max_retries): | |
| try: |
NewerOlder