Skip to content

Conversation

misrasaurabh1
Copy link
Contributor

@misrasaurabh1 misrasaurabh1 commented Sep 5, 2025

PR Type

Enhancement


Description

  • Introduce optimized postprocess using np.argpartition

  • Add naive postprocess implementation with np.argsort

  • Reshape logits and select top max_detections

  • Support batch processing of predictions


Diagram Walkthrough

flowchart LR
  P["predictions tuple (bboxes, logits)"]
  S["sigmoid_stable(logits)"]
  F["flatten logits per batch"]
  AP["np.argpartition select top k"]
  AS["np.argsort select top k"]
  SOP["sorted top detections"]
  P -- "extract logits" --> S
  S -- "apply sigmoid" --> F
  F -- "optimized path" --> AP
  F -- "naive path" --> AS
  AP -- "fine sort k" --> SOP
  AS -- "slice top k" --> SOP
Loading

File Walkthrough

Relevant files
Enhancement
after_algo.py
Implement optimized argpartition postprocess                         

codeflash/after_algo.py

  • Implement postprocess using np.argpartition for speed
  • Flatten logits and select top max_detections per batch
  • Sort partitioned indices for correct ranking
+9/-0     
before_algo.py
Add naive argsort-based postprocess                                           

codeflash/before_algo.py

  • Implement basic postprocess using np.argsort
  • Flatten logits and slice top max_detections
  • No partition-based optimization
+7/-0     

Signed-off-by: Saurabh Misra <[email protected]>
Copy link

github-actions bot commented Sep 5, 2025

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Missing Return

The postprocess methods compute sorted_indices but do not return any value, so the results are lost.

def postprocess(self, predictions: tuple[np.ndarray, ...], max_detections: int):
    bboxes, logits = predictions
    batch_size, num_queries, num_classes = logits.shape
    logits_sigmoid = self.sigmoid_stable(logits)
    for batch_idx in range(batch_size):
        logits_flat = logits_sigmoid[batch_idx].reshape(-1)
        # Use argpartition for better performance when max_detections is smaller than logits_flat
        partition_indices = np.argpartition(-logits_flat, max_detections)[:max_detections]
        sorted_indices = partition_indices[np.argsort(-logits_flat[partition_indices])]
Unused Bboxes

The bboxes array is extracted but never used; you should slice and include it in the output.

def postprocess(self, predictions: tuple[np.ndarray, ...], max_detections: int):
    bboxes, logits = predictions
    batch_size, num_queries, num_classes = logits.shape
    logits_sigmoid = self.sigmoid_stable(logits)
Boundary Check

There is no validation that max_detectionslogits_flat.size, which can cause errors when it's larger.

partition_indices = np.argpartition(-logits_flat, max_detections)[:max_detections]
sorted_indices = partition_indices[np.argsort(-logits_flat[partition_indices])]

Copy link

github-actions bot commented Sep 5, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Cap argpartition pivot value

Prevent out-of-bounds errors when max_detections exceeds the length of logits_flat
by capping the partition size. This ensures argpartition always receives a valid k
index and avoids runtime exceptions.

codeflash/after_algo.py [8]

-partition_indices = np.argpartition(-logits_flat, max_detections)[:max_detections]
+k = min(max_detections, logits_flat.size)
+# argpartition pivot should be k-1 for top k elements
+partition_indices = np.argpartition(-logits_flat, k-1)[:k]
Suggestion importance[1-10]: 8

__

Why: Capping max_detections ensures np.argpartition never receives an invalid pivot, preventing potential out-of-bounds errors and runtime exceptions.

Medium
Add return of results list

The function currently never returns any results. Initialize a list before the loop
to collect per-batch detections and return it at the end so callers receive the
computed indices.

codeflash/after_algo.py [1]

 def postprocess(self, predictions: tuple[np.ndarray, ...], max_detections: int):
+    results = []
+    bboxes, logits = predictions
+    batch_size, num_queries, num_classes = logits.shape
+    logits_sigmoid = self.sigmoid_stable(logits)
+    for batch_idx in range(batch_size):
+        logits_flat = logits_sigmoid[batch_idx].reshape(-1)
+        k = min(max_detections, logits_flat.size)
+        partition_indices = np.argpartition(-logits_flat, k-1)[:k]
+        sorted_indices = partition_indices[np.argsort(-logits_flat[partition_indices])]
+        results.append(sorted_indices)
+    return results
Suggestion importance[1-10]: 7

__

Why: Without a return, postprocess produces no output; initializing and returning results makes the function usable by callers.

Medium

Signed-off-by: Saurabh Misra <[email protected]>
Signed-off-by: Saurabh Misra <[email protected]>
Signed-off-by: Saurabh Misra <[email protected]>
Signed-off-by: Saurabh Misra <[email protected]>
Signed-off-by: Saurabh Misra <[email protected]>
Signed-off-by: Saurabh Misra <[email protected]>
Signed-off-by: Saurabh Misra <[email protected]>
@misrasaurabh1 misrasaurabh1 changed the title algo Code diffs Sep 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant