Skip to content

Cache optimization - #3

Open
ShashankFC wants to merge 2 commits into
mainfrom
shashank/cache-optimization
Open

ShashankFC wants to merge 2 commits into
mainfrom
shashank/cache-optimization

Conversation

@ShashankFC

@ShashankFC ShashankFC commented Nov 24, 2025

Copy link
Copy Markdown
Collaborator

EntelligenceAI PR Summary

This PR optimizes the PyTorch autograd profiler by adding caching mechanisms to improve performance in key tracking classes.

  • Added _cache dictionary to EnforceUnique class for memoizing previously seen keys with early return for cached results
  • Introduced _cache dictionary in KinetoStepTracker to store step counts per requester with early return in increment_step()
  • Changed _step_dict from defaultdict(int) to regular dict with explicit initialization in KinetoStepTracker
  • Added safety check to handle empty _step_dict when calculating max() values
  • Refactored erase_step_count() method to store result in variable before returning

@entelligence-ai-pr-reviews

Copy link
Copy Markdown

Entelligence AI Vulnerability Scanner

Status: No security vulnerabilities found

Your code passed our comprehensive security analysis.

Analyzed 1 files in total

@entelligence-ai-pr-reviews

Copy link
Copy Markdown

Review Summary

🏷️ Draft Comments (1)

Skipped posting 1 draft comments that were valid but scored below your review threshold (>=13/15). Feel free to update them here.

torch/autograd/profiler.py (1)

1227-1247: KinetoStepTracker.increment_step introduces a per-requester cache (_cache) that can cause stale step counts and prevent correct global step advancement, leading to missed profiler steps in multi-optimizer or multi-caller scenarios.

📊 Impact Scores:

  • Production Impact: 4/5
  • Fix Specificity: 5/5
  • Urgency Impact: 3/5
  • Total Score: 12/15

🤖 AI Agent Prompt (Copy & Paste Ready):

In torch/autograd/profiler.py, lines 1227-1247, remove the per-requester cache logic (`_cache`) from `KinetoStepTracker.increment_step`. The current implementation returns cached step counts for a requester, which can cause stale values and prevent correct global step advancement, especially in multi-optimizer or multi-caller scenarios. Replace the method body with the original logic that only uses `_step_dict` and always computes the global step from the current state. Do not use or update `_cache` in this method.

@entelligence-ai-pr-reviews

Copy link
Copy Markdown

Walkthrough

This PR introduces performance optimizations to the PyTorch autograd profiler by adding caching mechanisms to two classes. The EnforceUnique class now uses a cache dictionary to memoize previously seen keys, enabling faster lookups before checking the main set. The KinetoStepTracker class implements step count caching per requester, refactors its internal dictionary from defaultdict(int) to a regular dict with explicit initialization, and adds safety checks for empty dictionary scenarios when calculating maximum values. These changes aim to reduce redundant computations and improve profiler performance during training workflows.

Changes

File(s) Summary
torch/autograd/profiler.py Added caching mechanisms to EnforceUnique and KinetoStepTracker classes. Introduced _cache dictionary in EnforceUnique for memoizing previously seen keys. In KinetoStepTracker, added _cache dictionary for storing step counts per requester, changed _step_dict from defaultdict(int) to regular dict with explicit initialization, added safety check for empty _step_dict in max() calculation, and refactored erase_step_count() to store result in variable before returning.

Sequence Diagram

This diagram shows the interactions between components:

sequenceDiagram
    participant Client
    participant DuplicateChecker
    participant KinetoProfile as _KinetoProfile
    participant Kineto as _kineto_step()

    Note over DuplicateChecker,KinetoProfile: New caching mechanism added

    rect rgb(220, 240, 255)
        Note over DuplicateChecker: Duplicate Detection with Cache
        Client->>DuplicateChecker: see(key)
        alt key in cache
            DuplicateChecker-->>Client: return cached True
        else key in seen set
            DuplicateChecker-->>Client: raise RuntimeError (duplicate)
        else new key
            DuplicateChecker->>DuplicateChecker: add to seen set
            DuplicateChecker->>DuplicateChecker: cache[key] = True
            DuplicateChecker-->>Client: return True
        end
    end

    rect rgb(240, 255, 240)
        Note over KinetoProfile,Kineto: Step Increment with Caching
        Client->>KinetoProfile: increment_step(requester)
        alt requester in cache
            KinetoProfile-->>Client: return cached step
        else requester not initialized
            KinetoProfile->>KinetoProfile: init_step_count(requester)
            KinetoProfile->>KinetoProfile: _step_dict[requester] += 1
            KinetoProfile->>KinetoProfile: calculate new_step = max(values)
            alt new_step > current_step
                loop delta times
                    KinetoProfile->>Kineto: _kineto_step()
                end
                KinetoProfile->>KinetoProfile: update _current_step
            end
            KinetoProfile->>KinetoProfile: cache[requester] = _current_step
            KinetoProfile-->>Client: return _current_step
        end
    end

    rect rgb(255, 240, 240)
        Note over KinetoProfile: Cache Invalidation
        Client->>KinetoProfile: remove_requester(requester)
        KinetoProfile->>KinetoProfile: pop from _step_dict
        Note over KinetoProfile: Cache entry remains until next increment
        KinetoProfile-->>Client: return success
    end
Loading

🔗 Cross-Repository Impact Analysis

Enable automatic detection of breaking changes across your dependent repositories. → Set up now

Learn more about Cross-Repository Analysis

What It Does

  • Automatically identifies repositories that depend on this code
  • Analyzes potential breaking changes across your entire codebase
  • Provides risk assessment before merging to prevent cross-repo issues

How to Enable

  1. Visit Settings → Code Management
  2. Configure repository dependencies
  3. Future PRs will automatically include cross-repo impact analysis!

Benefits

  • 🛡️ Prevent breaking changes across repositories
  • 🔍 Catch integration issues before they reach production
  • 📊 Better visibility into your multi-repo architecture

▶️AI Code Reviews for VS Code, Cursor, Windsurf
Install the extension

Note for Windsurf Please change the default marketplace provider to the following in the windsurf settings:

Marketplace Extension Gallery Service URL: https://marketplace.visualstudio.com/_apis/public/gallery

Marketplace Gallery Item URL: https://marketplace.visualstudio.com/items

Entelligence.ai can learn from your feedback. Simply add 👍 / 👎 emojis to teach it your preferences. More shortcuts below

Emoji Descriptions:

  • ⚠️ Potential Issue - May require further investigation.
  • 🔒 Security Vulnerability - Fix to ensure system safety.
  • 💻 Code Improvement - Suggestions to enhance code quality.
  • 🔨 Refactor Suggestion - Recommendations for restructuring code.
  • ℹ️ Others - General comments and information.

Interact with the Bot:

  • Send a message or request using the format:
    @entelligenceai + *your message*
Example: @entelligenceai Can you suggest improvements for this code?
  • Help the Bot learn by providing feedback on its responses.
    @entelligenceai + *feedback*
Example: @entelligenceai Do not comment on `save_auth` function !

Also you can trigger various commands with the bot by doing
@entelligenceai command

The current supported commands are

  1. config - shows the current config
  2. retrigger_review - retriggers the review

More commands to be added soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant