Following this instruction :
1. Strictly adhere to PEP-8 standards, ensuring code is clean, well-structured, and follows Python's style guide for readability.
2. Use appropriate modules like `typing` for type hints, ensuring code clarity, maintainability, and leveraging Python's standard libraries effectively.
3. Focus on time, space, robustness, scalability; write optimized, efficient code that can handle large datasets and scale seamlessly.
4. Implement comprehensive error handling, catching exceptions gracefully, providing meaningful error messages, and ensuring code robustness under unexpected conditions.
5. Ensure the developer agent writes advanced, maintainable Python code, balancing performance and readability while adhering to best practices.

TASK : Generalised robustness metrics Pipeline (logits or predictions or loss values)

NOTE : using the only torch module we have implement below scratch 

Problem Statement:
In Grammatical Error Detection, these metrics evaluate the accuracy and effectiveness of grammar correction models in detecting and correcting errors. Here are the mathematical formulations:

---

### 1. **GLEU Score**  
The GLEU score, adapted for grammatical error detection, measures n-gram overlap between error-corrected text and reference text, focusing on both fluency and grammatical accuracy. It is calculated similarly to BLEU but penalizes repeated n-grams, providing a balanced evaluation of grammatical correction quality.

For a maximum of \( N \)-grams and a batch of size \( B \), the GLEU score is:
\[
\text{GLEU} = \frac{1}{B} \sum_{i=1}^{B} \frac{\sum_{n=1}^{N} \sum_{g \in G_n} \min(\text{count}_\text{gen}(g), \text{count}_\text{ref}(g))}{\sum_{n=1}^{N} \sum_{g \in G_n} \text{count}_\text{gen}(g)}
\]
where:
- \( G_n \) is the set of \( n \)-grams in the generated (corrected) text.
- \( \text{count}_\text{gen}(g) \) and \( \text{count}_\text{ref}(g) \) are the counts of n-gram \( g \) in the generated and reference texts, respectively.
- \( N \) is typically set to 4 (for unigrams, bigrams, trigrams, and 4-grams).

This score reflects the accuracy and fluency of corrections based on n-gram matching.

---

### 2. **ERRANT Score**  
ERRANT is a framework that categorizes grammatical error corrections and calculates the accuracy of error correction across different error types. Here, ERRANT Score measures the proportion of errors correctly corrected.

The ERRANT score for a batch of size \( B \) is calculated as:
\[
\text{ERRANT} = \frac{1}{B} \sum_{i=1}^{B} \frac{\text{Corrected Errors}_i}{\text{Total Errors}_i}
\]
where:
- \( \text{Corrected Errors}_i \) is the number of errors correctly corrected in instance \( i \).
- \( \text{Total Errors}_i \) is the total number of errors in the reference for instance \( i \).

This ratio provides a measure of the error correction accuracy, where a higher ERRANT score indicates more accurate correction.

---

### 3. **Precision (Grammar Detection)**  
Precision in grammar detection measures the fraction of correctly identified grammar errors out of all identified errors, assessing the accuracy of the model in detecting errors without falsely identifying correct text as erroneous.

The precision for a batch of size \( B \) is given by:
\[
\text{Precision (Grammar Detection)} = \frac{1}{B} \sum_{i=1}^{B} \frac{\text{True Positives}_i}{\text{True Positives}_i + \text{False Positives}_i}
\]
where:
- \( \text{True Positives}_i \) represents correctly identified grammar errors in instance \( i \).
- \( \text{False Positives}_i \) represents non-errors incorrectly marked as errors in instance \( i \).

---

These metrics offer a well-rounded evaluation of grammatical error detection, covering accuracy in error correction, completeness, and precision in identifying errors.