Skip to content

Commit ee5e081

Browse files
committed
fix: enhance layer pair validation in FeatureDistillationStrategy
- Validate exactly 2 parts after splitting by ':' - Check both teacher and student layer names are non-empty after trimming - Reject malformed inputs like "teacher:", ":student", "teacher:student:extra" - Improve error message to clarify expected format
1 parent 06dad2d commit ee5e081

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/KnowledgeDistillation/FeatureDistillationStrategy.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,31 @@ public FeatureDistillationStrategy(string[] layerPairs, double featureWeight = 0
8484
if (_layerPairs.Length == 0)
8585
throw new ArgumentException("At least one layer pair must be specified", nameof(layerPairs));
8686

87-
// Validate layer pair format using explicit Where
88-
var invalidPairs = _layerPairs.Where(pair => string.IsNullOrWhiteSpace(pair) || !pair.Contains(':')).ToArray();
87+
// Validate layer pair format: must be exactly "teacher_layer:student_layer" with both parts non-empty
88+
var invalidPairs = _layerPairs.Where(pair =>
89+
{
90+
if (string.IsNullOrWhiteSpace(pair))
91+
return true;
92+
93+
var parts = pair.Split(':');
94+
95+
// Must have exactly 2 parts
96+
if (parts.Length != 2)
97+
return true;
98+
99+
// Both parts must be non-empty after trimming
100+
if (string.IsNullOrWhiteSpace(parts[0]) || string.IsNullOrWhiteSpace(parts[1]))
101+
return true;
102+
103+
return false;
104+
}).ToArray();
105+
89106
if (invalidPairs.Length > 0)
90107
{
91108
var invalidList = string.Join(", ", invalidPairs.Select(p => $"'{p}'"));
92109
throw new ArgumentException(
93-
$"Invalid layer pair format: {invalidList}. Expected 'teacher_layer:student_layer'",
110+
$"Invalid layer pair format: {invalidList}. Expected 'teacher_layer:student_layer' " +
111+
$"with exactly one colon and both layer names non-empty",
94112
nameof(layerPairs));
95113
}
96114
}

0 commit comments

Comments
 (0)