Skip to main content

Auto-scaling tensor dimensions for PyTorch and TensorFlow based on available RAM

Project description

Deep Dimensions

Multi-framework tensor dimension auto-scaling for PyTorch and TensorFlow

Automatically scales tensor dimensions based on available system memory. Prevents OOM errors by intelligently reducing dimensions while preserving aspect ratios.

Installation

pip install deep-dimensions

# Or with specific framework
pip install deep-dimensions[pytorch]
pip install deep-dimensions[tensorflow]
pip install deep-dimensions[all]

Quick Start

from deep_dimensions import AutoScaler

# Auto-detects framework (PyTorch or TensorFlow)
scaler = AutoScaler()

# Scale dimensions to fit memory
dims = scaler.scale_dimensions((4096, 4096, 3))
print(f"Scaled: {dims}")

# Create a memory-safe tensor
tensor = scaler.create_scaled_tensor((8192, 8192, 3), fill_value=0.0)

Framework-Specific Usage

PyTorch

import torch
from deep_dimensions import AutoScaler, ScalingConfig

config = ScalingConfig(
    memory_threshold=0.7,      # Use max 70% of available memory
    strategy="exponential",    # Reduce larger dims more aggressively
    device="auto",             # CPU or CUDA auto-detection
)

scaler = AutoScaler(config, framework="pytorch")
tensor = scaler.create_scaled_tensor((2048, 2048, 3), dtype=torch.float16)

TensorFlow

import tensorflow as tf
from deep_dimensions import AutoScaler, ScalingConfig

config = ScalingConfig(
    memory_threshold=0.6,
    strategy="linear",
)

scaler = AutoScaler(config, framework="tensorflow")
tensor = scaler.create_scaled_tensor((2048, 2048, 3), dtype=tf.float32)

Core API

Method Description
scale_dimensions(dims, dtype) Returns scaled dimensions that fit in memory
create_scaled_tensor(dims, dtype, fill_value) Creates a tensor with auto-scaled dimensions
scale_dimensions_with_info(dims, dtype) Returns ScalingResult with metadata
can_fit(dims, dtype) Checks if dimensions fit in available memory
get_memory_info() Returns current memory status
estimate_memory(dims, dtype) Estimates memory needed for dimensions

Configuration

from deep_dimensions import ScalingConfig

config = ScalingConfig(
    memory_threshold=0.8,      # Max memory usage ratio (0-1)
    safety_margin=0.1,         # Additional safety buffer
    device="auto",             # "cpu", "cuda", or "auto"
    strategy="linear",         # "linear" or "exponential"
    framework="auto",          # "pytorch", "tensorflow", or "auto"
    min_dimensions=(32, 32),   # Minimum allowed dimensions
    max_dimensions=(4096, 4096),  # Maximum allowed dimensions
)

Scaling Strategies

Linear: Reduces all dimensions proportionally

(1024, 1024, 3) → (512, 512, 2)  # All dims scaled ~50%

Exponential: Reduces larger dimensions more aggressively

(1024, 256, 3) → (256, 128, 3)   # Larger dims reduced more

Architecture

┌─────────────────────────────────────────────────────────┐
│                     AutoScaler                          │
│  (Public API - Facade Pattern)                          │
├─────────────────────────────────────────────────────────┤
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐  │
│  │MemoryMonitor │  │DimensionCalc │  │FrameworkReg  │  │
│  └──────────────┘  └──────────────┘  └──────────────┘  │
├─────────────────────────────────────────────────────────┤
│         IMemoryProvider     IScalingStrategy            │
│         IFrameworkAdapter                               │
│  (Abstractions - Dependency Inversion)                  │
├─────────────────────────────────────────────────────────┤
│  ┌────────────┐ ┌────────────┐ ┌────────────┐          │
│  │SystemMem   │ │LinearScale │ │PyTorchAdpt │          │
│  │CUDAMem     │ │ExpoScale   │ │TFAdapter   │          │
│  └────────────┘ └────────────┘ └────────────┘          │
│  (Implementations - Strategy Pattern)                   │
└─────────────────────────────────────────────────────────┘

Design Principles

  • SOLID: Single responsibility, Open/closed, Interface segregation
  • Dependency Inversion: Core depends on abstractions, not implementations
  • Strategy Pattern: Pluggable scaling algorithms
  • Facade Pattern: Simple API hiding complex internals
  • Immutability: Config and result objects are frozen dataclasses
  • Fail-Fast: Validates inputs at boundaries with clear errors

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

deep_dimensions-0.2.1.tar.gz (30.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

deep_dimensions-0.2.1-py3-none-any.whl (39.0 kB view details)

Uploaded Python 3

File details

Details for the file deep_dimensions-0.2.1.tar.gz.

File metadata

  • Download URL: deep_dimensions-0.2.1.tar.gz
  • Upload date:
  • Size: 30.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for deep_dimensions-0.2.1.tar.gz
Algorithm Hash digest
SHA256 785067685d0293088d493b24934d0abb830c8c50b962934b0d6f1f3c96927818
MD5 fd47750fc594a40479116122fab33e03
BLAKE2b-256 173185f1e757c7bc04ddb4fe2074c7b870136f599bbe54b7188b9706b5cf9502

See more details on using hashes here.

File details

Details for the file deep_dimensions-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for deep_dimensions-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 454ec2315db83f3fc531d820fa0bd3c20d6fa292b321786178fe6c591fa2fcfb
MD5 bbc64e37dde04253d586b3885177eb69
BLAKE2b-256 c1428b73fca6084e8b6930f0c99346adfe94a0e5710ec4755cebc1d3448fc9a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page