Skip to content

Commit 38b49eb

Browse files
committed
fix: add comprehensive validation to DistributedTeacherModel constructor
The constructor was accessing _workers[0] without validating the array, which could cause IndexOutOfRangeException or inconsistent behavior. Added validation to fail fast in the constructor: 1. Check that workers array is non-empty (Length > 0) 2. Check that no worker is null 3. Check that all workers have the same OutputDimension This prevents runtime errors when OutputDimension or GetLogits is invoked and provides clear, actionable error messages indicating which validation failed. Addresses code review comment from @coderabbitai.
1 parent 2bc248f commit 38b49eb

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/KnowledgeDistillation/Teachers/DistributedTeacherModel.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@ public DistributedTeacherModel(
1818
AggregationMode aggregation = AggregationMode.Average)
1919
{
2020
_workers = workers ?? throw new ArgumentNullException(nameof(workers));
21+
22+
// Validate workers array is non-empty
23+
if (_workers.Length == 0)
24+
throw new ArgumentException("Workers array cannot be empty", nameof(workers));
25+
26+
// Validate no worker is null
27+
for (int i = 0; i < _workers.Length; i++)
28+
{
29+
if (_workers[i] == null)
30+
throw new ArgumentException($"Worker at index {i} is null", nameof(workers));
31+
}
32+
33+
// Validate all workers have the same output dimension
34+
int expectedOutputDim = _workers[0].OutputDimension;
35+
for (int i = 1; i < _workers.Length; i++)
36+
{
37+
if (_workers[i].OutputDimension != expectedOutputDim)
38+
throw new ArgumentException(
39+
$"Worker at index {i} has OutputDimension {_workers[i].OutputDimension}, " +
40+
$"but expected {expectedOutputDim} (from worker 0). " +
41+
$"All workers must have the same OutputDimension.",
42+
nameof(workers));
43+
}
44+
2145
_aggregation = aggregation;
2246
}
2347

0 commit comments

Comments
 (0)