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:

To evaluate text similarity, these metrics use mathematical formulations that focus on vector relationships in embedding space and the correlation between predicted and ground-truth similarities. Here are the equations for each metric:

---

### 1. **Cosine Similarity**  
Cosine similarity measures the cosine of the angle between two vectors, providing a normalized metric of similarity between text embeddings.

For two embeddings \( \mathbf{u} \) and \( \mathbf{v} \) of dimension \( d \), the cosine similarity is:
\[
\text{Cosine Similarity} = \frac{\mathbf{u} \cdot \mathbf{v}}{\|\mathbf{u}\| \times \|\mathbf{v}\|}
= \frac{\sum_{i=1}^{d} u_i \cdot v_i}{\sqrt{\sum_{i=1}^{d} u_i^2} \times \sqrt{\sum_{i=1}^{d} v_i^2}}
\]
where:
- \(\mathbf{u} \cdot \mathbf{v}\) is the dot product of the vectors.
- \(\|\mathbf{u}\|\) and \(\|\mathbf{v}\|\) are the magnitudes (norms) of the vectors.

For a batch of text pairs, compute cosine similarity for each pair independently.

---

### 2. **Pearson Correlation**  
Pearson correlation measures the linear relationship between predicted similarity scores and ground-truth similarity scores, with values ranging from \(-1\) (perfect negative correlation) to \(+1\) (perfect positive correlation).

Given a batch of predictions \(\{p_i\}\) and ground truth scores \(\{g_i\}\), Pearson correlation \(r\) is:
\[
r = \frac{\sum_{i=1}^n (p_i - \bar{p})(g_i - \bar{g})}{\sqrt{\sum_{i=1}^n (p_i - \bar{p})^2} \times \sqrt{\sum_{i=1}^n (g_i - \bar{g})^2}}
\]
where:
- \(\bar{p}\) and \(\bar{g}\) are the means of the predicted and ground-truth scores, respectively.
- \(n\) is the batch size.

---

### 3. **Spearman’s Rank Correlation**  
Spearman’s rank correlation assesses how well the ranks of the predicted and ground-truth scores correspond, handling non-linear relationships. Spearman correlation, denoted by \(\rho\), is calculated by ranking each score and then computing Pearson correlation on the ranks.

For predictions \(\{p_i\}\) and ground truth scores \(\{g_i\}\), assign ranks \(\text{rank}(p_i)\) and \(\text{rank}(g_i)\). Then:
\[
\rho = \frac{\sum_{i=1}^n (\text{rank}(p_i) - \overline{\text{rank}(p)})(\text{rank}(g_i) - \overline{\text{rank}(g)})}{\sqrt{\sum_{i=1}^n (\text{rank}(p_i) - \overline{\text{rank}(p)})^2} \times \sqrt{\sum_{i=1}^n (\text{rank}(g_i) - \overline{\text{rank}(g)})^2}}
\]
Alternatively, Spearman’s rank correlation can also be approximated by:
\[
\rho = 1 - \frac{6 \sum_{i=1}^n d_i^2}{n(n^2 - 1)}
\]
where \(d_i\) is the difference between ranks of the \(i\)-th prediction and ground-truth similarity, and \(n\) is the batch size.

---

### 4. **Sentence Embedding Similarity**  
Sentence embedding similarity measures the closeness of sentence embeddings for a batch of text pairs. While cosine similarity is often used, Euclidean or other distance metrics can also be applied in the embedding space. The cosine similarity (as above) is frequently used as the main measure:
\[
\text{Sentence Embedding Similarity} = \frac{\mathbf{u} \cdot \mathbf{v}}{\|\mathbf{u}\| \times \|\mathbf{v}\|}
\]
where \(\mathbf{u}\) and \(\mathbf{v}\) represent the sentence embeddings of two texts in each pair in the batch.

These metrics, when applied across text embeddings, provide a thorough assessment of text similarity, capturing both absolute and rank-based relationships.