Refactor and optimizations
This commit is contained in:
@@ -2,24 +2,29 @@
|
||||
|
||||
namespace App\Services\IterationEventBuffer;
|
||||
|
||||
use App\Events\PerceptronTrainingIteration;
|
||||
|
||||
class PerceptronLimitedEpochEventBuffer implements IPerceptronIterationEventBuffer
|
||||
{
|
||||
private array $data;
|
||||
private array $data = [];
|
||||
|
||||
private int $underSizeIncreaseCount = 0;
|
||||
private ?int $activeEpoch = null;
|
||||
|
||||
private bool $shouldBroadcastEpoch = false;
|
||||
|
||||
public function __construct(
|
||||
private string $sessionId,
|
||||
private string $trainingId,
|
||||
private int $epochInterval,
|
||||
private int $sizeIncreaseStart = 10,
|
||||
) {
|
||||
$this->data = [];
|
||||
}
|
||||
) {}
|
||||
|
||||
public function flush(): void
|
||||
{
|
||||
event(new \App\Events\PerceptronTrainingIteration($this->data, $this->sessionId, $this->trainingId));
|
||||
if ($this->data === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
event(new PerceptronTrainingIteration($this->data, $this->sessionId, $this->trainingId));
|
||||
$this->data = [];
|
||||
}
|
||||
|
||||
@@ -32,16 +37,28 @@ class PerceptronLimitedEpochEventBuffer implements IPerceptronIterationEventBuff
|
||||
'weights' => $synaptic_weights,
|
||||
];
|
||||
|
||||
$lastEpoch = $this->data[0]['epoch'] ?? null;
|
||||
if ($this->data && $lastEpoch !== $epoch) { // Current Epoch has changed from the last one
|
||||
if ($lastEpoch == 1 || $lastEpoch % $this->epochInterval === 0) { // The last saved epoch need to be sent
|
||||
$this->flush(); // Flush all data from the previous epoch
|
||||
} else {
|
||||
$this->data = []; // We clear the data without sending it as we are saving the next epoch data
|
||||
}
|
||||
|
||||
$lastEpoch = $epoch;
|
||||
if ($this->activeEpoch !== $epoch) {
|
||||
$this->flush();
|
||||
$this->activeEpoch = $epoch;
|
||||
$this->shouldBroadcastEpoch = $epoch === 1 || $epoch % $this->epochInterval === 0;
|
||||
}
|
||||
|
||||
if (! $this->shouldBroadcastEpoch) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->data[] = $newData;
|
||||
|
||||
if ($this->payloadExceedsLimit() || count($this->data) >= config('perceptron.broadcast_iteration_size')) {
|
||||
$this->flush();
|
||||
}
|
||||
}
|
||||
|
||||
private function payloadExceedsLimit(): bool
|
||||
{
|
||||
return strlen(json_encode([
|
||||
'iterations' => $this->data,
|
||||
'trainingId' => $this->trainingId,
|
||||
], JSON_THROW_ON_ERROR)) > config('broadcasting.broadcast_max_payload_size');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user